Skip to content

iceoryx_hoofs/cxx/optional.hpp🔗

Namespaces🔗

Name
iox
building block to easily create free function for logging in a library context
iox::cxx

Classes🔗

Name
struct iox::cxx::nullopt_t
Helper struct which is used to signal an empty optional. It is equivalent to no value.
struct iox::cxx::in_place_t
helper struct which is used to call the in-place-construction constructor
class iox::cxx::optional
Optional implementation from the C++17 standard with C++11. The interface is analog to the C++17 standard and it can be used in factory functions which can fail.

Source code🔗

// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_HOOFS_CXX_OPTIONAL_HPP
#define IOX_HOOFS_CXX_OPTIONAL_HPP

#include "iceoryx_hoofs/cxx/function_ref.hpp"
#include "iceoryx_hoofs/cxx/requires.hpp"
#include "iceoryx_hoofs/cxx/types.hpp"

#include <new> // needed for placement new in the construct_value member function
#include <utility>

namespace iox
{
namespace cxx
{
struct nullopt_t
{
};
constexpr nullopt_t nullopt = nullopt_t();

struct in_place_t
{
};
constexpr in_place_t in_place{};

template <typename T>
class optional
{
  public:
    using type = T;

    optional() noexcept;

    optional(const nullopt_t&) noexcept;

    optional(T&& value) noexcept;

    optional(const T& value) noexcept;

    template <typename... Targs>
    optional(in_place_t, Targs&&... args) noexcept;

    ~optional() noexcept;

    optional(const optional& rhs) noexcept;

    optional(optional&& rhs) noexcept;

    optional& operator=(const optional& rhs) noexcept;

    optional& operator=(optional&& rhs) noexcept;

    constexpr bool operator==(const optional<T>& rhs) const noexcept;

    constexpr bool operator==(const nullopt_t&) const noexcept;

    constexpr bool operator!=(const optional<T>& rhs) const noexcept;

    constexpr bool operator!=(const nullopt_t&) const noexcept;

    template <typename U = T>
    typename std::enable_if<!std::is_same<U, optional<T>&>::value, optional>::type& operator=(U&& value) noexcept;

    const T* operator->() const noexcept;

    const T& operator*() const noexcept;

    T* operator->() noexcept;

    T& operator*() noexcept;

    constexpr explicit operator bool() const noexcept;

    constexpr bool has_value() const noexcept;

    template <typename... Targs>
    T& emplace(Targs&&... args) noexcept;

    void reset() noexcept;

    T& value() & noexcept;

    const T& value() const& noexcept;

    T&& value() && noexcept;

    const T&& value() const&& noexcept;

    template <typename U>
    constexpr T value_or(U&& default_value) const noexcept;

    optional& and_then(const cxx::function_ref<void(T&)>& callable) noexcept;

    const optional& and_then(const cxx::function_ref<void(const T&)>& callable) const noexcept;

    optional& or_else(const cxx::function_ref<void()>& callable) noexcept;

    const optional& or_else(const cxx::function_ref<void()>& callable) const noexcept;

  private:
    alignas(T) byte_t m_data[sizeof(T)];
    bool m_hasValue{false};

  private:
    template <typename... Targs>
    void construct_value(Targs&&... args) noexcept;
    void destruct_value() noexcept;
};

template <typename OptionalBaseType, typename... Targs>
optional<OptionalBaseType> make_optional(Targs&&... args) noexcept;
} // namespace cxx
} // namespace iox

#include "iceoryx_hoofs/internal/cxx/optional.inl"

#endif // IOX_HOOFS_CXX_OPTIONAL_HPP

Updated on 18 December 2023 at 13:11:42 CET