iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
expected_helper.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 by Apex.AI Inc. All rights reserved.
2// Copyright (c) 2025 Contributors to the Eclipse Foundation
3//
4// See the NOTICE file(s) distributed with this work for additional
5// information regarding copyright ownership.
6//
7// This program and the accompanying materials are made available under the
8// terms of the Apache Software License 2.0 which is available at
9// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
10// which is available at https://opensource.org/licenses/MIT.
11//
12// SPDX-License-Identifier: Apache-2.0 OR MIT
13
14#ifndef IOX2_BB_VOCABULARY_EXPECTED_HELPER_HPP
15#define IOX2_BB_VOCABULARY_EXPECTED_HELPER_HPP
16
18
19namespace iox2 {
20namespace legacy {
22struct in_place_t { };
23
24// AXIVION Next Construct AutosarC++19_03-M17.0.2 : in_place is defined within iox namespace which prevents easy
25// misuse
26constexpr in_place_t in_place {};
27
29struct unexpect_t { };
30constexpr unexpect_t unexpect {};
31
34template <typename T>
35using enable_if_non_void_t = typename std::enable_if<!std::is_void<T>::value, T>::type;
36
39template <typename T>
40using enable_if_void_t = typename std::enable_if<std::is_void<T>::value, T>::type;
41
44template <typename T>
45using enable_if_not_lvalue_referece_t = typename std::enable_if<!std::is_lvalue_reference<T>::value, T>::type;
46
47namespace detail {
49template <typename T = void>
50struct ok {
51 // AXIVION Next Construct AutosarC++19_03-A12.1.5 : This is a false positive since there is no fitting constructor
52 // available for delegation
53 explicit ok(const T& t) noexcept
54 : value(t) {
55 }
56
57 // AXIVION Next Construct AutosarC++19_03-A18.9.2 : For universal references std::forward must be used
58 template <typename U = T, typename = enable_if_not_lvalue_referece_t<U>>
59 // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved) perfect forwarding is used
60 explicit ok(T&& t) noexcept
61 : value(std::forward<T>(t)) {
62 }
63
64 // AXIVION Next Construct AutosarC++19_03-A15.4.2, FaultDetection-NoexceptViolations : Intentional behavior. 'ok' is not intended to be used with a type which throws
65 template <typename... Targs>
66 explicit ok(Targs&&... args) noexcept
67 : value(std::forward<Targs>(args)...) {
68 }
69
71};
72
74template <>
75struct ok<void> {
76 // dummy value
77 bool value { true };
78};
79
81template <typename T>
82struct err {
83 // AXIVION Next Construct AutosarC++19_03-A12.1.5 : This is a false positive since there is no fitting constructor
84 // available for delegation
85 explicit err(const T& t) noexcept
86 : value(t) {
87 }
88
89 // AXIVION Next Construct AutosarC++19_03-A18.9.2 : For universal references std::forward must be used
90 template <typename U = T, typename = enable_if_not_lvalue_referece_t<U>>
91 // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved) perfect forwarding is used
92 explicit err(T&& t) noexcept
93 : value(std::forward<T>(t)) {
94 }
95
96 template <typename... Targs>
97 explicit err(Targs&&... args) noexcept
98 : value(std::forward<Targs>(args)...) {
99 }
100
102};
103
105template <typename ValueType, typename ErrorType>
107 public:
109
112 : data(in_place_index<VALUE_INDEX>(), std::forward<Targs>(args)...) {
113 }
114
115 template <typename... Targs>
116 // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved) perfect forwarding is used
118 : data(in_place_index<ERROR_INDEX>(), std::forward<Targs>(args)...) {
119 }
120
121 bool has_value() const {
122 return data.index() == VALUE_INDEX;
123 }
124
125 bool has_error() const {
126 return data.index() == ERROR_INDEX;
127 }
128
129 ValueType& value_unchecked() {
130 return *data.template get_at_index<VALUE_INDEX>();
131 }
132
133 const ValueType& value_unchecked() const {
134 return *data.template get_at_index<VALUE_INDEX>();
135 }
136
138 return *data.template get_at_index<ERROR_INDEX>();
139 }
140
141 const ErrorType& error_unchecked() const {
142 return *data.template get_at_index<ERROR_INDEX>();
143 }
144
145 private:
146 static constexpr uint64_t VALUE_INDEX { 0 };
147 static constexpr uint64_t ERROR_INDEX { 1 };
148
150};
151
153template <typename ErrorType>
155 public:
157
159 // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) Targs is not used but required for template meta-programming
161 : data(in_place_index<VALUE_INDEX>(), DUMMY_VALUE) {
162 }
163
164 template <typename... Targs>
166 : data(in_place_index<ERROR_INDEX>(), std::forward<Targs>(args)...) {
167 }
168
169 bool has_value() const {
170 return data.index() == VALUE_INDEX;
171 }
172
173 bool has_error() const {
174 return data.index() == ERROR_INDEX;
175 }
176
177 void value_unchecked() const {
178 // nothing to do
179 }
180
182 return *data.template get_at_index<ERROR_INDEX>();
183 }
184
185 const ErrorType& error_unchecked() const {
186 return *data.template get_at_index<ERROR_INDEX>();
187 }
188
189 private:
190 static constexpr uint64_t VALUE_INDEX { 0 };
191 static constexpr uint64_t ERROR_INDEX { 1 };
192
193 using DummyValueType = bool;
194 static constexpr DummyValueType DUMMY_VALUE { true };
195
197};
198
199template <typename ErrorType>
200constexpr typename expected_storage<void, ErrorType>::DummyValueType expected_storage<void, ErrorType>::DUMMY_VALUE;
201
203template <typename T, typename E>
207 return lhs.value_unchecked() == rhs.value_unchecked();
208 }
209};
210
212template <typename E>
215 return true;
216 }
217};
218
219} // namespace detail
220} // namespace legacy
221} // namespace iox2
222
223#endif // IOX2_BB_VOCABULARY_EXPECTED_HELPER_HPP
helper class to be able to handle 'void' value type specialization
const ErrorType & error_unchecked() const
const ValueType & value_unchecked() const
expected_storage(unexpect_t, Targs &&... args)
Variant implementation from the C++17 standard with C++11. The interface is inspired by the C++17 sta...
Definition variant.hpp:101
constexpr uint64_t index() const noexcept
returns the index of the stored type in the variant. if the variant does not contain any type it retu...
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
constexpr bool always_false_v
Helper value to bind a static_assert to a type.
constexpr in_place_t in_place
constexpr unexpect_t unexpect
typename std::enable_if<!std::is_lvalue_reference< T >::value, T >::type enable_if_not_lvalue_referece_t
helper trait for SFINEA to disable specific functions for lvalue references
typename std::enable_if< std::is_void< T >::value, T >::type enable_if_void_t
helper trait for SFINEA to disable specific functions for non 'void' value type
static constexpr bool is_same_value_unchecked(const expected_storage< void, E > &, const expected_storage< void, E > &)
helper struct for 'operator==' to be able to handle 'void' value type specialization
static constexpr bool is_same_value_unchecked(const expected_storage< T, E > &lhs, const expected_storage< T, E > &rhs)
helper struct to create an expected which is signalling an error more easily
err(const T &t) noexcept
err(Targs &&... args) noexcept
helper struct to create an expected which is signalling success more easily
ok(Targs &&... args) noexcept
ok(const T &t) noexcept
helper struct to perform an emplacement at a predefined index in the constructor of a variant
Definition variant.hpp:38
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