iceoryx_hoofs  2.0.2
optional.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 #ifndef IOX_HOOFS_CXX_OPTIONAL_HPP
18 #define IOX_HOOFS_CXX_OPTIONAL_HPP
19 
20 #include "iceoryx_hoofs/cxx/function_ref.hpp"
21 #include "iceoryx_hoofs/cxx/requires.hpp"
22 #include "iceoryx_hoofs/cxx/types.hpp"
23 
24 #include <new> // needed for placement new in the construct_value member function
25 #include <utility>
26 
27 namespace iox
28 {
29 namespace cxx
30 {
33 struct nullopt_t
34 {
35 };
36 constexpr nullopt_t nullopt = nullopt_t();
37 
39 struct in_place_t
40 {
41 };
42 constexpr in_place_t in_place{};
43 
67 template <typename T>
68 class optional
69 {
70  public:
71  using type = T;
72 
76  optional() noexcept;
77 
81  optional(const nullopt_t&) noexcept;
82 
86  optional(T&& value) noexcept;
87 
90  optional(const T& value) noexcept;
91 
96  template <typename... Targs>
97  optional(in_place_t, Targs&&... args) noexcept;
98 
100  ~optional() noexcept;
101 
105  optional(const optional& rhs) noexcept;
106 
110  optional(optional&& rhs) noexcept;
111 
117  optional& operator=(const optional& rhs) noexcept;
118 
124  optional& operator=(optional&& rhs) noexcept;
125 
130  constexpr bool operator==(const optional<T>& rhs) const noexcept;
131 
134  constexpr bool operator==(const nullopt_t&) const noexcept;
135 
140  constexpr bool operator!=(const optional<T>& rhs) const noexcept;
141 
144  constexpr bool operator!=(const nullopt_t&) const noexcept;
145 
152  template <typename U = T>
153  typename std::enable_if<!std::is_same<U, optional<T>&>::value, optional>::type& operator=(U&& value) noexcept;
154 
159  const T* operator->() const noexcept;
160 
165  const T& operator*() const noexcept;
166 
171  T* operator->() noexcept;
172 
177  T& operator*() noexcept;
178 
181  constexpr explicit operator bool() const noexcept;
182 
185  constexpr bool has_value() const noexcept;
186 
193  template <typename... Targs>
194  T& emplace(Targs&&... args) noexcept;
195 
199  void reset() noexcept;
200 
205  T& value() & noexcept;
206 
211  const T& value() const& noexcept;
212 
217  T&& value() && noexcept;
218 
223  const T&& value() const&& noexcept;
224 
229  template <typename U>
230  constexpr T value_or(U&& default_value) const noexcept;
231 
236  optional& and_then(const cxx::function_ref<void(T&)>& callable) noexcept;
237 
242  const optional& and_then(const cxx::function_ref<void(const T&)>& callable) const noexcept;
243 
247  optional& or_else(const cxx::function_ref<void()>& callable) noexcept;
248 
252  const optional& or_else(const cxx::function_ref<void()>& callable) const noexcept;
253 
254  private:
255  alignas(T) byte_t m_data[sizeof(T)];
256  bool m_hasValue{false};
257 
258  private:
259  template <typename... Targs>
260  void construct_value(Targs&&... args) noexcept;
261  void destruct_value() noexcept;
262 };
263 
269 template <typename OptionalBaseType, typename... Targs>
270 optional<OptionalBaseType> make_optional(Targs&&... args) noexcept;
271 } // namespace cxx
272 } // namespace iox
273 
274 #include "iceoryx_hoofs/internal/cxx/optional.inl"
275 
276 #endif // IOX_HOOFS_CXX_OPTIONAL_HPP
Definition: function_ref.hpp:34
Optional implementation from the C++17 standard with C++11. The interface is analog to the C++17 stan...
Definition: optional.hpp:69
optional & and_then(const cxx::function_ref< void(T &)> &callable) noexcept
calls the provided callable with the optional value as arguments if the optional contains a value
T & value() &noexcept
Returns a reference to the underlying value. If the optional has no value the application terminates....
void reset() noexcept
Calls the destructor of T if the optional has a value. If the optional has no value,...
constexpr bool has_value() const noexcept
Will return true if the optional contains a value, otherwise false.
T & emplace(Targs &&... args) noexcept
A new element is constructed by forwarding the arguments to the constructor of T. If the optional has...
optional() noexcept
Creates an optional which has no value. If you access such an optional via .value() or the arrow oper...
optional & or_else(const cxx::function_ref< void()> &callable) noexcept
calls the provided callable if the optional does not contain a value
constexpr T value_or(U &&default_value) const noexcept
If the optional contains a value a copy of that value is returned, otherwise the default_value is ret...
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29
helper struct which is used to call the in-place-construction constructor
Definition: optional.hpp:40
Helper struct which is used to signal an empty optional. It is equivalent to no value.
Definition: optional.hpp:34