iceoryx_doc  1.0.1
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_UTILS_CXX_OPTIONAL_HPP
18 #define IOX_UTILS_CXX_OPTIONAL_HPP
19 
20 #include "iceoryx_utils/cxx/function_ref.hpp"
21 #include "iceoryx_utils/cxx/helplets.hpp"
22 #include "iceoryx_utils/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 
61 template <typename T>
62 class optional
63 {
64  public:
65  using type = T;
66 
70  optional() noexcept;
71 
75  optional(const nullopt_t&) noexcept;
76 
80  optional(T&& value) noexcept;
81 
84  optional(const T& value) noexcept;
85 
87  ~optional() noexcept;
88 
92  optional(const optional& rhs) noexcept;
93 
97  optional(optional&& rhs) noexcept;
98 
104  optional& operator=(const optional& rhs) noexcept;
105 
111  optional& operator=(optional&& rhs) noexcept;
112 
117  constexpr bool operator==(const optional<T>& rhs) const noexcept;
118 
121  constexpr bool operator==(const nullopt_t&) const noexcept;
122 
127  constexpr bool operator!=(const optional<T>& rhs) const noexcept;
128 
131  constexpr bool operator!=(const nullopt_t&) const noexcept;
132 
139  template <typename U = T>
140  typename std::enable_if<!std::is_same<U, optional<T>&>::value, optional>::type& operator=(U&& value) noexcept;
141 
146  const T* operator->() const noexcept;
147 
152  const T& operator*() const noexcept;
153 
158  T* operator->() noexcept;
159 
164  T& operator*() noexcept;
165 
168  constexpr explicit operator bool() const noexcept;
169 
172  constexpr bool has_value() const noexcept;
173 
180  template <typename... Targs>
181  T& emplace(Targs&&... args) noexcept;
182 
186  void reset() noexcept;
187 
192  T& value() & noexcept;
193 
198  const T& value() const& noexcept;
199 
204  T&& value() && noexcept;
205 
210  const T&& value() const&& noexcept;
211 
216  template <typename U>
217  constexpr T value_or(U&& default_value) const noexcept;
218 
223  optional& and_then(const cxx::function_ref<void(T&)>& callable) noexcept;
224 
229  const optional& and_then(const cxx::function_ref<void(const T&)>& callable) const noexcept;
230 
234  optional& or_else(const cxx::function_ref<void()>& callable) noexcept;
235 
239  const optional& or_else(const cxx::function_ref<void()>& callable) const noexcept;
240 
241  private:
242  alignas(T) byte_t m_data[sizeof(T)];
243  bool m_hasValue{false};
244 
245  private:
246  template <typename... Targs>
247  void construct_value(Targs&&... args) noexcept;
248  void destruct_value() noexcept;
249 };
250 
256 template <typename OptionalBaseType, typename... Targs>
257 optional<OptionalBaseType> make_optional(Targs&&... args) noexcept;
258 } // namespace cxx
259 } // namespace iox
260 
261 #include "iceoryx_utils/internal/cxx/optional.inl"
262 
263 #endif // IOX_UTILS_CXX_OPTIONAL_HPP
Definition: function_ref.hpp:32
Optional implementation from the C++17 standard with C++11. The interface is analog to the C++17 stan...
Definition: optional.hpp:63
T & emplace(Targs &&... args) noexcept
A new element is constructed by forwarding the arguments to the constructor of T. If the optional has...
Definition: optional.inl:205
optional & operator=(const optional &rhs) noexcept
Copies an optional. If the optional has a value then the copy assignment of that value is called....
Definition: optional.inl:67
constexpr bool operator!=(const optional< T > &rhs) const noexcept
If the optionals have values it compares these values by using their comparision operator.
Definition: optional.inl:162
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,...
Definition: optional.inl:223
const T & operator*() const noexcept
Returns a reference to the underlying value. If the optional has no value the behavior is undefined....
Definition: optional.inl:180
constexpr bool has_value() const noexcept
Will return true if the optional contains a value, otherwise false.
Definition: optional.inl:217
constexpr bool operator==(const optional< T > &rhs) const noexcept
If the optionals have values it compares these values by using their comparision operator.
Definition: optional.inl:150
const T & value() const &noexcept
Returns a const reference to the underlying value. If the optional has no value the application termi...
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
Definition: optional.inl:290
~optional() noexcept
The destructor will call the destructor of T if a value is set.
Definition: optional.inl:113
const T * operator->() const noexcept
Returns a pointer to the underlying value. If the optional has no value the behavior is undefined....
Definition: optional.inl:174
optional & or_else(const cxx::function_ref< void()> &callable) noexcept
calls the provided callable if the optional does not contain a value
Definition: optional.inl:310
optional() noexcept
Creates an optional which has no value. If you access such an optional via .value() or the arrow oper...
Definition: optional.inl:30
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...
Definition: optional.inl:261
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28
Helper struct which is used to signal an empty optional. It is equivalent to no value.
Definition: optional.hpp:34