iceoryx_hoofs  2.0.2
expected.hpp
1 // Copyright (c) 2019 - 2020 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2020 - 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_EXPECTED_HPP
18 #define IOX_HOOFS_CXX_EXPECTED_HPP
19 
20 #include "iceoryx_hoofs/cxx/attributes.hpp"
21 #include "iceoryx_hoofs/cxx/function_ref.hpp"
22 #include "iceoryx_hoofs/cxx/helplets.hpp"
23 #include "iceoryx_hoofs/cxx/optional.hpp"
24 #include "iceoryx_hoofs/cxx/variant.hpp"
25 
26 #include <utility>
27 
28 namespace iox
29 {
30 namespace cxx
31 {
32 namespace internal
33 {
35 template <typename...>
36 struct IsOptional;
37 } // namespace internal
38 
47 template <typename T = void>
48 struct success
49 {
53  success(const T& t) noexcept;
54 
58  success(T&& t) noexcept;
59  template <typename... Targs>
60 
65  success(Targs&&... args) noexcept;
66 
67  T value;
68 };
69 
77 template <>
78 struct success<void>
79 {
80 };
81 
90 template <typename T>
91 struct error
92 {
96  error(const T& t) noexcept;
97 
101  error(T&& t) noexcept;
102 
107  template <typename... Targs>
108  error(Targs&&... args) noexcept;
109 
110  T value;
111 };
112 
113 template <typename... T>
114 class IOX_NO_DISCARD expected;
115 
145 template <typename ErrorType>
146 class IOX_NO_DISCARD expected<ErrorType>
147 {
148  public:
151  expected() = delete;
152 
155  expected(const expected&) noexcept = default;
156 
159  expected(expected&& rhs) noexcept;
160 
161 #if defined(_WIN32)
164  template <typename ValueType>
165  expected(const expected<ValueType, ErrorType>& rhs) noexcept;
166 
169  template <typename ValueType>
170  expected(expected<ValueType, ErrorType>&& rhs) noexcept;
171 #endif
174  ~expected() noexcept = default;
175 
178  expected& operator=(const expected&) noexcept;
179 
182  expected& operator=(expected&& rhs) noexcept;
183 
184 #if defined(_WIN32)
187  template <typename ValueType>
188  expected& operator=(const expected<ValueType, ErrorType>& rhs) noexcept;
189 
192  template <typename ValueType>
193  expected& operator=(expected<ValueType, ErrorType>&& rhs) noexcept;
194 #endif
195 
198  expected(const success<void>& successValue) noexcept;
199 
203  expected(const error<ErrorType>& errorValue) noexcept;
204 
208  expected(error<ErrorType>&& errorValue) noexcept;
209 
212  static expected create_value() noexcept;
213 
218  template <typename... Targs>
219  static expected create_error(Targs&&... args) noexcept;
220 
223  explicit operator bool() const noexcept;
224 
227  bool has_error() const noexcept;
228 
232  ErrorType& get_error() & noexcept;
233 
237  const ErrorType& get_error() const& noexcept;
238 
242  ErrorType&& get_error() && noexcept;
243 
253  const expected& or_else(const cxx::function_ref<void(ErrorType&)>& callable) const noexcept;
254 
264  expected& or_else(const cxx::function_ref<void(ErrorType&)>& callable) noexcept;
265 
275  const expected& and_then(const cxx::function_ref<void()>& callable) const noexcept;
276 
286  expected& and_then(const cxx::function_ref<void()>& callable) noexcept;
287 
288  private:
289  expected(variant<ErrorType>&& store, const bool hasError) noexcept;
290  variant<ErrorType> m_store;
291  bool m_hasError;
292  static constexpr uint64_t ERROR_INDEX = 0U;
293 };
294 
298 template <typename ValueType, typename ErrorType>
299 class IOX_NO_DISCARD expected<ValueType, ErrorType>
300 {
301  public:
304  expected() = delete;
305 
308  expected(const expected&) noexcept = default;
309 
312  expected(expected&& rhs) noexcept;
313 
316  ~expected() noexcept = default;
317 
320  expected& operator=(const expected&) noexcept;
321 
324  expected& operator=(expected&& rhs) noexcept;
325 
329  expected(const success<ValueType>& successValue) noexcept;
330 
334  expected(success<ValueType>&& successValue) noexcept;
335 
339  expected(const error<ErrorType>& errorValue) noexcept;
340 
344  expected(error<ErrorType>&& errorValue) noexcept;
345 
350  template <typename... Targs>
351  static expected create_value(Targs&&... args) noexcept;
352 
357  template <typename... Targs>
358  static expected create_error(Targs&&... args) noexcept;
359 
362  explicit operator bool() const noexcept;
363 
366  bool has_error() const noexcept;
367 
371  ErrorType& get_error() & noexcept;
372 
376  const ErrorType& get_error() const& noexcept;
377 
381  ErrorType&& get_error() && noexcept;
382 
386  ValueType& value() & noexcept;
387 
391  const ValueType& value() const& noexcept;
392 
396  ValueType&& value() && noexcept;
397 
401  ValueType value_or(const ValueType& value) const noexcept;
402 
406  ValueType value_or(const ValueType& value) noexcept;
407 
408 
418  ValueType& operator*() noexcept;
419 
429  const ValueType& operator*() const noexcept;
430 
438  ValueType* operator->() noexcept;
439 
447  const ValueType* operator->() const noexcept;
448 
460  template <typename T>
461  operator expected<T>() noexcept;
462 
474  template <typename T>
475  operator expected<T>() const noexcept;
476 
486  const expected& or_else(const cxx::function_ref<void(ErrorType&)>& callable) const noexcept;
487 
497  expected& or_else(const cxx::function_ref<void(ErrorType&)>& callable) noexcept;
498 
508  const expected& and_then(const cxx::function_ref<void(ValueType&)>& callable) const noexcept;
509 
519  expected& and_then(const cxx::function_ref<void(ValueType&)>& callable) noexcept;
520 
532  template <typename Optional = ValueType,
533  typename std::enable_if<internal::IsOptional<Optional>::value, int>::type = 0>
534  const expected& and_then(const cxx::function_ref<void(typename Optional::type&)>& callable) const noexcept;
535 
547  template <typename Optional = ValueType,
548  typename std::enable_if<internal::IsOptional<Optional>::value, int>::type = 0>
549  expected& and_then(const cxx::function_ref<void(typename Optional::type&)>& callable) noexcept;
550 
564  template <typename Optional = ValueType,
565  typename std::enable_if<internal::IsOptional<Optional>::value, int>::type = 0>
566  [[deprecated]] const expected& if_empty(const cxx::function_ref<void()>& callable) const noexcept;
567 
581  template <typename Optional = ValueType,
582  typename std::enable_if<internal::IsOptional<Optional>::value, int>::type = 0>
583  [[deprecated]] expected& if_empty(const cxx::function_ref<void()>& callable) noexcept;
584 
585  optional<ValueType> to_optional() const noexcept;
586 
587  private:
588  expected(variant<ValueType, ErrorType>&& f_store, const bool hasError) noexcept;
589  variant<ValueType, ErrorType> m_store;
590  bool m_hasError;
591  static constexpr uint64_t VALUE_INDEX = 0U;
592  static constexpr uint64_t ERROR_INDEX = 1U;
593 };
594 
595 template <typename ErrorType>
596 class IOX_NO_DISCARD expected<void, ErrorType> : public expected<ErrorType>
597 {
598  public:
600 };
601 
602 
603 } // namespace cxx
604 } // namespace iox
605 
606 #include "iceoryx_hoofs/internal/cxx/expected.inl"
607 
608 #endif // IOX_HOOFS_CXX_EXPECTED_HPP
expected(const success< void > &successValue) noexcept
constructs an expected which is signaling success
expected(error< ErrorType > &&errorValue) noexcept
constructs an expected which is signaling an error and stores the error value provided by value
static expected create_value() noexcept
creates an expected which is signaling success
~expected() noexcept=default
calls the destructor of the success value or error value - depending on what is stored in the expecte...
expected(const expected &) noexcept=default
the copy constructor calls the copy constructor of the contained success value or the error value - d...
expected(expected &&rhs) noexcept
the move constructor calls the move constructor of the contained success value or the error value - d...
expected(const error< ErrorType > &errorValue) noexcept
constructs an expected which is signaling an error and stores the error value provided by errorValue
expected()=delete
default ctor is deleted since you have to clearly state if the expected contains a success value or a...
specialization of the expected class which can contain an error as well as a success value
Definition: expected.hpp:300
expected(expected &&rhs) noexcept
the move constructor calls the move constructor of the contained success value or the error value - d...
expected(const expected &) noexcept=default
the copy constructor calls the copy constructor of the contained success value or the error value - d...
~expected() noexcept=default
calls the destructor of the success value or error value - depending on what is stored in the expecte...
expected()=delete
default ctor is deleted since you have to clearly state if the expected contains a success value or a...
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
Variant implementation from the C++17 standard with C++11. The interface is inspired by the C++17 sta...
Definition: variant.hpp:107
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29
helper struct to create an expected which is signalling an error more easily
Definition: expected.hpp:92
error(T &&t) noexcept
constructor which creates a error helper class by moving the value of t
error(const T &t) noexcept
constructor which creates a error helper class by copying the value of t
error(Targs &&... args) noexcept
constructor which creates a error helper class by forwarding arguments to the constructor of T
helper struct to create an error only expected which is signalling success more easily
Definition: expected.hpp:79
helper struct to create an expected which is signalling success more easily
Definition: expected.hpp:49
success(const T &t) noexcept
constructor which creates a success helper class by copying the value of t
success(T &&t) noexcept
constructor which creates a success helper class by moving the value of t
success(Targs &&... args) noexcept
constructor which creates a success helper class by forwarding arguments to the constructor of T