tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
expected.h
Go to the documentation of this file.
1#ifndef FUSED_EXPECTED_H
2#define FUSED_EXPECTED_H
3
4#include "simple_type_traits.h" // move, forward, is_trivially_destructible, is_nothrow_move_constructible
5
28template <typename E>
30{
32};
33
38template <typename E>
40
84template <typename T, typename E>
85class [[nodiscard]] Expected
86{
87 union
88 {
91 };
92 bool has_value_;
93
101 void destroy() noexcept
102 {
103 if (has_value_)
104 {
105 if constexpr (!is_trivially_destructible_v<T>)
106 {
107 val_.~T();
108 }
109 }
110 }
111
112public:
113 using value_type = T;
114 using error_type = E;
115
116 // -- Construction -------------------------------------------------------
117
122 Expected(T &&v) noexcept(is_nothrow_move_constructible_v<T>)
123 : val_(move(v)), has_value_(true)
124 {
125 }
126
132 : err_(u.error), has_value_(false)
133 {
134 }
135
136 // -- Move semantics (no copy) ------------------------------------------
137
144 Expected(Expected &&other) noexcept(is_nothrow_move_constructible_v<T>)
145 : has_value_(other.has_value_)
146 {
147 if (has_value_)
148 new (&val_) T(move(other.val_));
149 else
150 err_ = other.err_;
151 }
152
159 Expected &operator=(Expected &&other) noexcept(is_nothrow_move_constructible_v<T>)
160 {
161 if (this != &other)
162 {
163 destroy();
164 has_value_ = other.has_value_;
165 if (has_value_)
166 new (&val_) T(move(other.val_));
167 else
168 err_ = other.err_;
169 }
170 return *this;
171 }
172
174 Expected(const Expected &) = delete;
175
177 Expected &operator=(const Expected &) = delete;
178
182 ~Expected() noexcept { destroy(); }
183
184 // -- Observers ----------------------------------------------------------
185
189 explicit operator bool() const noexcept { return has_value_; }
190
194 bool has_value() const noexcept { return has_value_; }
195
196 // -- Value access -------------------------------------------------------
197
202 T &value() noexcept { return val_; }
203
208 const T &value() const noexcept { return val_; }
209
214 T &operator*() noexcept { return val_; }
215
220 const T &operator*() const noexcept { return val_; }
221
226 T *operator->() noexcept { return &val_; }
227
232 const T *operator->() const noexcept { return &val_; }
233
234 // -- Error access -------------------------------------------------------
235
240 E error() const noexcept { return err_; }
241
242 // -- Monadic operations (future) ----------------------------------------
243 // template <typename F> auto and_then(F&& f) -> Expected<...>;
244 // template <typename F> auto transform(F&& f) -> Expected<...>;
245 // template <typename F> auto or_else(F&& f) -> Expected<...>;
246};
247
248#endif // FUSED_EXPECTED_H
A discriminated union holding either a success value or an error.
Definition expected.h:86
E error() const noexcept
Access the error code.
Definition expected.h:240
~Expected() noexcept
Destructor. Destroys the active union member.
Definition expected.h:182
T & operator*() noexcept
Dereference operator — access the success value.
Definition expected.h:214
T val_
Definition expected.h:89
Expected(Expected &&other) noexcept(is_nothrow_move_constructible_v< T >)
Move constructor.
Definition expected.h:144
T * operator->() noexcept
Arrow operator — access members of the success value.
Definition expected.h:226
const T & value() const noexcept
Access the success value by const reference.
Definition expected.h:208
bool has_value() const noexcept
Returns true if the Expected holds a success value.
Definition expected.h:194
Expected & operator=(Expected &&other) noexcept(is_nothrow_move_constructible_v< T >)
Move assignment.
Definition expected.h:159
E err_
Definition expected.h:90
T & value() noexcept
Access the success value by reference.
Definition expected.h:202
Expected(Unexpected< E > u) noexcept
Construct in the error state from an Unexpected tag.
Definition expected.h:131
E error_type
Definition expected.h:114
Expected & operator=(const Expected &)=delete
Deleted — Expected is move-only to prevent accidental copies.
const T & operator*() const noexcept
Dereference operator (const) — access the success value.
Definition expected.h:220
Expected(const Expected &)=delete
Deleted — Expected is move-only to prevent accidental copies.
Expected(T &&v) noexcept(is_nothrow_move_constructible_v< T >)
Construct in the success state by moving a value in.
Definition expected.h:122
T value_type
Definition expected.h:113
const T * operator->() const noexcept
Arrow operator (const) — access members of the success value.
Definition expected.h:232
constexpr remove_reference_t< T > && move(T &&t) noexcept
Cast to rvalue reference (replacement for std::move).
Definition simple_type_traits.h:178
Tag type for constructing an Expected in the error state.
Definition expected.h:30
E error
The error value.
Definition expected.h:31