Skip to content

iceoryx_utils/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.
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_UTILS_CXX_OPTIONAL_HPP
#define IOX_UTILS_CXX_OPTIONAL_HPP

#include "iceoryx_utils/cxx/function_ref.hpp"
#include "iceoryx_utils/cxx/helplets.hpp"
#include "iceoryx_utils/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();

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;

    ~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_utils/internal/cxx/optional.inl"

#endif // IOX_UTILS_CXX_OPTIONAL_HPP

Updated on 17 June 2021 at 11:15:26 CEST