FastLED 3.10.8
Loading...
Searching...
No Matches
expected.h
Go to the documentation of this file.
1#pragma once
2
37
38#include "fl/stl/stdint.h"
39#include "fl/stl/variant.h"
40#include "fl/stl/string.h"
41#include "fl/stl/noexcept.h"
42
43namespace fl {
44
60
61// Forward declaration for expected template
62template<typename T, typename E = ResultError> class expected; // IWYU pragma: keep
63
66template<typename E>
67struct ErrorInfo {
70
71 ErrorInfo(E err, const char* msg = nullptr) FL_NO_EXCEPT : code(err), message(msg ? msg : "") {}
72};
73
78template<typename T, typename E>
79class expected {
80public:
82 bool ok() const FL_NO_EXCEPT { return mData.template is<T>(); }
83
85 bool has_value() const FL_NO_EXCEPT { return ok(); }
86
88 E error() const FL_NO_EXCEPT {
89 auto* err = mData.template ptr<ErrorInfo<E>>();
90 return err ? err->code : E{};
91 }
92
94 const char* message() const FL_NO_EXCEPT {
95 auto* err = mData.template ptr<ErrorInfo<E>>();
96 return err ? err->message.c_str() : "";
97 }
98
101 T& value() FL_NO_EXCEPT { return mData.template get<T>(); }
102 const T& value() const FL_NO_EXCEPT { return mData.template get<T>(); }
103
105 explicit operator bool() const FL_NO_EXCEPT { return ok(); }
106
109 expected r;
110 r.mData = fl::move(value);
111 return r;
112 }
113
115 static expected failure(E err, const char* msg = nullptr) FL_NO_EXCEPT {
116 expected r;
117 r.mData = ErrorInfo<E>(err, msg);
118 return r;
119 }
120
122 expected() FL_NO_EXCEPT : mData(ErrorInfo<E>(E{}, nullptr)) {}
123
125 expected(expected&& other) FL_NO_EXCEPT = default;
126
129
132
133private:
134 fl::variant<T, ErrorInfo<E>> mData;
135
136 // Non-copyable
138 expected& operator=(const expected&) FL_NO_EXCEPT = delete;
139};
140
142struct VoidSuccess {};
143
147template<typename E>
148class expected<void, E> {
149public:
150 bool ok() const FL_NO_EXCEPT { return mData.template is<VoidSuccess>(); }
151
152 E error() const FL_NO_EXCEPT {
153 auto* err = mData.template ptr<ErrorInfo<E>>();
154 return err ? err->code : E{};
155 }
156
157 const char* message() const FL_NO_EXCEPT {
158 auto* err = mData.template ptr<ErrorInfo<E>>();
159 return err ? err->message.c_str() : "";
160 }
161
162 explicit operator bool() const FL_NO_EXCEPT { return ok(); }
163
165 expected r;
166 r.mData = VoidSuccess{};
167 return r;
168 }
169
170 static expected failure(E err, const char* msg = nullptr) FL_NO_EXCEPT {
171 expected r;
172 r.mData = ErrorInfo<E>(err, msg);
173 return r;
174 }
175
177 expected() FL_NO_EXCEPT : mData(ErrorInfo<E>(E{}, nullptr)) {}
178
180 expected(expected&& other) FL_NO_EXCEPT = default;
181
184
187 if (other.ok()) {
188 mData = VoidSuccess{};
189 } else {
190 auto* err = other.mData.template ptr<ErrorInfo<E>>();
191 if (err) {
192 mData = ErrorInfo<E>(err->code, err->message.c_str());
193 }
194 }
195 }
196
199 if (this != &other) {
200 if (other.ok()) {
201 mData = VoidSuccess{};
202 } else {
203 auto* err = other.mData.template ptr<ErrorInfo<E>>();
204 if (err) {
205 mData = ErrorInfo<E>(err->code, err->message.c_str());
206 }
207 }
208 }
209 return *this;
210 }
211
214
215private:
217};
218
219} // namespace fl
~expected() FL_NO_EXCEPT=default
Destructor (defaulted - variant handles cleanup)
const char * message() const FL_NO_EXCEPT
Definition expected.h:157
expected(expected &&other) FL_NO_EXCEPT=default
Move constructor (defaulted - variant handles it)
E error() const FL_NO_EXCEPT
Definition expected.h:152
expected() FL_NO_EXCEPT
Default constructor (creates error state)
Definition expected.h:177
expected & operator=(expected &&other) FL_NO_EXCEPT=default
Move assignment (defaulted - variant handles it)
static expected failure(E err, const char *msg=nullptr) FL_NO_EXCEPT
Definition expected.h:170
expected & operator=(const expected &other) FL_NO_EXCEPT
Copy assignment.
Definition expected.h:198
bool ok() const FL_NO_EXCEPT
Definition expected.h:150
static expected success() FL_NO_EXCEPT
Definition expected.h:164
expected(const expected &other) FL_NO_EXCEPT
Copy constructor (needed for some use cases like Impl initialization)
Definition expected.h:186
fl::variant< VoidSuccess, ErrorInfo< E > > mData
Definition expected.h:216
expected & operator=(expected &&other) FL_NO_EXCEPT=default
Move assignment (defaulted - variant handles it)
bool has_value() const FL_NO_EXCEPT
Check if operation succeeded (alias for ok())
Definition expected.h:85
static expected failure(E err, const char *msg=nullptr) FL_NO_EXCEPT
Create error result.
Definition expected.h:115
expected() FL_NO_EXCEPT
Default constructor (creates error state)
Definition expected.h:122
const T & value() const FL_NO_EXCEPT
Definition expected.h:102
bool ok() const FL_NO_EXCEPT
Check if operation succeeded.
Definition expected.h:82
E error() const FL_NO_EXCEPT
Get error code (only meaningful if !ok())
Definition expected.h:88
static expected success(T value) FL_NO_EXCEPT
Create successful result.
Definition expected.h:108
~expected() FL_NO_EXCEPT=default
Destructor (defaulted - variant handles cleanup)
fl::variant< T, ErrorInfo< E > > mData
Definition expected.h:134
T & value() FL_NO_EXCEPT
Get value (only valid if ok() == true)
Definition expected.h:101
const char * message() const FL_NO_EXCEPT
Get error message (only meaningful if !ok())
Definition expected.h:94
expected(expected &&other) FL_NO_EXCEPT=default
Move constructor (defaulted - variant handles it)
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
Definition move.h:30
unsigned char u8
Definition stdint.h:131
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
ResultError
Generic error codes for expected type.
Definition expected.h:47
@ NOT_INITIALIZED
Object not initialized.
Definition expected.h:52
@ ALREADY_INITIALIZED
Object already initialized.
Definition expected.h:53
@ IO_ERROR
Input/output error.
Definition expected.h:58
@ NOT_SUPPORTED
Operation not supported.
Definition expected.h:57
@ UNKNOWN
Unknown or unspecified error.
Definition expected.h:49
@ OUT_OF_RANGE
Value out of valid range.
Definition expected.h:51
@ BUSY
Resource is busy.
Definition expected.h:56
@ ALLOCATION_FAILED
Memory allocation failed.
Definition expected.h:54
pair_element< I, T1, T2 >::type & get(pair< T1, T2 > &p) FL_NO_EXCEPT
Definition pair.h:119
@ TIMEOUT
Operation timed out.
Definition rx.h:154
@ OK
No error (not typically used)
Definition rx.h:69
@ INVALID_ARGUMENT
Invalid input arguments.
Definition rx.h:72
Base definition for an LED controller.
Definition crgb.hpp:179
Dummy type for void expected success state.
Definition expected.h:142
fl::string message
Definition expected.h:69
ErrorInfo(E err, const char *msg=nullptr) FL_NO_EXCEPT
Definition expected.h:71
Error information for expected type.
Definition expected.h:67