iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
expected.hpp
Go to the documentation of this file.
1// Copyright (c) 2019 - 2020 by Robert Bosch GmbH. All rights reserved.
2// Copyright (c) 2020 - 2023 by Apex.AI Inc. All rights reserved.
3// Copyright (c) 2025 Contributors to the Eclipse Foundation
4//
5// See the NOTICE file(s) distributed with this work for additional
6// information regarding copyright ownership.
7//
8// This program and the accompanying materials are made available under the
9// terms of the Apache Software License 2.0 which is available at
10// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
11// which is available at https://opensource.org/licenses/MIT.
12//
13// SPDX-License-Identifier: Apache-2.0 OR MIT
14
15#ifndef IOX2_BB_VOCABULARY_EXPECTED_HPP
16#define IOX2_BB_VOCABULARY_EXPECTED_HPP
17
22
23#include <type_traits>
24#include <typeinfo>
25
26namespace iox2 {
27namespace legacy {
28template <typename T = void>
30
31template <typename T>
33
42template <typename T = void, typename = enable_if_void_t<T>>
44
54template <typename T, typename = enable_if_non_void_t<T>>
55detail::ok<T> ok(const T& value);
56
68template <typename T, typename = enable_if_non_void_t<T>, typename = enable_if_not_lvalue_referece_t<T>>
70
81template <typename T, typename... Targs, typename = enable_if_non_void_t<T>>
83
93template <typename T>
95
107template <typename T, typename = enable_if_not_lvalue_referece_t<T>>
109
120template <typename T, typename... Targs>
122
123template <typename ValueType, typename ErrorType>
124class expected;
125
132template <typename ValueType, typename ErrorType>
134 const expected<ValueType, ErrorType>& rhs) noexcept;
135
142template <typename ValueType, typename ErrorType>
144 const expected<ValueType, ErrorType>& rhs) noexcept;
145
149template <typename ValueType, typename ErrorType>
151 public:
154 expected() = delete;
155
158 expected(const expected&) noexcept = default;
159
164 expected(expected&& rhs) noexcept;
165
171 template <typename... Targs>
172 explicit expected(in_place_t, Targs&&... args) noexcept;
173
179 template <typename... Targs>
180 explicit expected(unexpect_t, Targs&&... args) noexcept;
181
184 ~expected() noexcept = default;
185
188 expected& operator=(const expected&) noexcept;
189
194 expected& operator=(expected&& rhs) noexcept;
195
199 //
200 // we would like to use 'return ok(myValue)' with an implicit
201 // conversion to return an expected easily
202 // NOLINTNEXTLINE(hicpp-explicit-conversions)
203 expected(const detail::ok<ValueType>& successValue) noexcept;
204
208 //
209 // we would like to use 'return ok(myValue)' with an implicit
210 // conversion to return an expected easily
211 // NOLINTNEXTLINE(hicpp-explicit-conversions)
212 expected(detail::ok<ValueType>&& successValue) noexcept;
213
218 // we would like to use 'return err(myErrorValue)' with an implicit
219 // conversion to return an expected easily
220 // NOLINTNEXTLINE(hicpp-explicit-conversions)
221 expected(const detail::err<ErrorType>& errorValue) noexcept;
222
227 // we would like to use 'return err(myErrorValue)' with an implicit
228 // conversion to return an expected easily
229 // NOLINTNEXTLINE(hicpp-explicit-conversions)
230 expected(detail::err<ErrorType>&& errorValue) noexcept;
231
232 // AXIVION Next Construct AutosarC++19_03-A13.5.3: Implementation is inspired from std::expected
235 explicit operator bool() const noexcept;
236
239 bool has_value() const noexcept;
240
243 bool has_error() const noexcept;
244
248 ErrorType& error(bb::detail::SourceLocation location = bb::detail::SourceLocation::current()) & noexcept;
249
253 const ErrorType& error(bb::detail::SourceLocation location = bb::detail::SourceLocation::current()) const& noexcept;
254
258 ErrorType&& error(bb::detail::SourceLocation location = bb::detail::SourceLocation::current()) && noexcept;
259
263 const ErrorType&&
264 error(bb::detail::SourceLocation location = bb::detail::SourceLocation::current()) const&& noexcept;
265
271 template <typename U = ValueType>
273 value(bb::detail::SourceLocation location = bb::detail::SourceLocation::current()) & noexcept;
274
280 template <typename U = ValueType>
281 const enable_if_non_void_t<U>&
282 value(bb::detail::SourceLocation location = bb::detail::SourceLocation::current()) const& noexcept;
283
288 template <typename U = ValueType>
290 value(bb::detail::SourceLocation location = bb::detail::SourceLocation::current()) && noexcept;
291
296 template <typename U = ValueType>
297 const enable_if_non_void_t<U>&&
298 value(bb::detail::SourceLocation location = bb::detail::SourceLocation::current()) const&& noexcept;
299
310 template <typename U = ValueType>
311 enable_if_non_void_t<U>& operator*() noexcept;
312
323 template <typename U = ValueType>
324 const enable_if_non_void_t<U>& operator*() const noexcept;
325
335 template <typename U = ValueType>
336 enable_if_non_void_t<U>* operator->() noexcept;
337
347 template <typename U = ValueType>
348 const enable_if_non_void_t<U>* operator->() const noexcept;
349
361 //
362 // AXIVION Next Construct AutosarC++19_03-A13.5.2 , AutosarC++19_03-A13.5.3: see doxygen brief section
363 // template <typename E>
364 // NOLINTNEXTLINE(hicpp-explicit-conversions)
365 operator expected<void, ErrorType>() const noexcept;
366
367 template <typename T, typename E>
368 friend constexpr bool ::iox2::legacy::operator==(const expected<T, E>&, const expected<T, E>&) noexcept;
369
370 private:
371 template <typename U = ValueType>
372 enable_if_non_void_t<U>& value_checked(bb::detail::SourceLocation location) & noexcept;
373 template <typename U = ValueType>
374 const enable_if_non_void_t<U>& value_checked(bb::detail::SourceLocation location) const& noexcept;
375
376 ErrorType& error_checked(bb::detail::SourceLocation location) & noexcept;
377 const ErrorType& error_checked(bb::detail::SourceLocation location) const& noexcept;
378
379 template <typename E = ErrorType>
380 std::enable_if_t<!(std::is_integral<E>::value || std::is_enum<E>::value), void>
381 log_error_unchecked() const noexcept;
382
383 template <typename E = ErrorType>
384 std::enable_if_t<std::is_integral<E>::value, void> log_error_unchecked() const noexcept;
385
386 template <typename E = ErrorType>
387 std::enable_if_t<std::is_enum<E>::value, void> log_error_unchecked() const noexcept;
388
389 private:
390 detail::expected_storage<ValueType, ErrorType> m_store;
391};
392
393} // namespace legacy
394} // namespace iox2
395
396#include "iox2/legacy/detail/expected.inl"
397
398#endif // IOX2_BB_VOCABULARY_EXPECTED_HPP
#define IOX2_NO_DISCARD
IOX2_NO_DISCARD adds the [[nodiscard]] keyword if it is available for the current compiler.
Implementation of the C++23 expected class which can contain an error or a success value.
Definition expected.hpp:150
expected(const expected &) noexcept=default
the copy constructor calls the copy constructor of the contained success value or the error value - d...
expected()=delete
default ctor is deleted since you have to clearly state if the expected contains a success value or a...
expected(in_place_t, Targs &&... args) noexcept
Creates an expected which is signaling success and perfectly forwards the args to the constructor of ...
~expected() noexcept=default
calls the destructor of the success value or error value - depending on what is stored in the expecte...
expected(expected &&rhs) noexcept
the move constructor calls the move constructor of the contained success value or the error value - d...
expected(unexpect_t, Targs &&... args) noexcept
Creates an expected which is signaling an error and perfectly forwards the args to the constructor of...
constexpr bool operator!=(const expected< ValueType, ErrorType > &lhs, const expected< ValueType, ErrorType > &rhs) noexcept
inequality check for two distinct expected types
typename std::enable_if<!std::is_void< T >::value, T >::type enable_if_non_void_t
helper trait for SFINEA to disable specific functions for 'void' value type
detail::ok< void > ok()
convenience function to create an 'expected' with a 'void' value type
constexpr bool always_false_v
Helper value to bind a static_assert to a type.
detail::err< T > err(const T &error)
convenience function to create an 'expected' with an error type by copy
constexpr bool operator==(const expected< ValueType, ErrorType > &lhs, const expected< ValueType, ErrorType > &rhs) noexcept
equality check for two distinct expected types
helper struct to create an expected which is signalling an error more easily
helper struct to create an expected which is signalling success more easily
helper struct which is used to call the in-place-construction constructor
helper struct which is used to call the in-place-construction constructor for error types