FastLED 3.9.15
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_NOEXCEPT : code(err), message(msg ? msg : "") {}
72};
73
78template<typename T, typename E>
79class expected {
80public:
82 bool ok() const FL_NOEXCEPT { return mData.template is<T>(); }
83
85 bool has_value() const FL_NOEXCEPT { return ok(); }
86
88 E error() const FL_NOEXCEPT {
89 auto* err = mData.template ptr<ErrorInfo<E>>();
90 return err ? err->code : E{};
91 }
92
94 const char* message() const FL_NOEXCEPT {
95 auto* err = mData.template ptr<ErrorInfo<E>>();
96 return err ? err->message.c_str() : "";
97 }
98
101 T& value() FL_NOEXCEPT { return mData.template get<T>(); }
102 const T& value() const FL_NOEXCEPT { return mData.template get<T>(); }
103
105 explicit operator bool() const FL_NOEXCEPT { 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_NOEXCEPT {
116 expected r;
117 r.mData = ErrorInfo<E>(err, msg);
118 return r;
119 }
120
122 expected() FL_NOEXCEPT : mData(ErrorInfo<E>(E{}, nullptr)) {}
123
125 expected(expected&& other) FL_NOEXCEPT = default;
126
129
132
133private:
134 fl::variant<T, ErrorInfo<E>> mData;
135
136 // Non-copyable
137 expected(const expected&) FL_NOEXCEPT = delete;
138 expected& operator=(const expected&) FL_NOEXCEPT = delete;
139};
140
142struct VoidSuccess {};
143
147template<typename E>
148class expected<void, E> {
149public:
150 bool ok() const FL_NOEXCEPT { return mData.template is<VoidSuccess>(); }
151
152 E error() const FL_NOEXCEPT {
153 auto* err = mData.template ptr<ErrorInfo<E>>();
154 return err ? err->code : E{};
155 }
156
157 const char* message() const FL_NOEXCEPT {
158 auto* err = mData.template ptr<ErrorInfo<E>>();
159 return err ? err->message.c_str() : "";
160 }
161
162 explicit operator bool() const FL_NOEXCEPT { 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_NOEXCEPT {
171 expected r;
172 r.mData = ErrorInfo<E>(err, msg);
173 return r;
174 }
175
177 expected() FL_NOEXCEPT : mData(ErrorInfo<E>(E{}, nullptr)) {}
178
180 expected(expected&& other) FL_NOEXCEPT = default;
181
184
186 expected(const expected& other) FL_NOEXCEPT : mData() {
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_NOEXCEPT=default
Destructor (defaulted - variant handles cleanup)
static expected success() FL_NOEXCEPT
Definition expected.h:164
bool ok() const FL_NOEXCEPT
Definition expected.h:150
static expected failure(E err, const char *msg=nullptr) FL_NOEXCEPT
Definition expected.h:170
expected & operator=(const expected &other) FL_NOEXCEPT
Copy assignment.
Definition expected.h:198
fl::variant< VoidSuccess, ErrorInfo< E > > mData
Definition expected.h:216
expected & operator=(expected &&other) FL_NOEXCEPT=default
Move assignment (defaulted - variant handles it)
const char * message() const FL_NOEXCEPT
Definition expected.h:157
expected() FL_NOEXCEPT
Default constructor (creates error state)
Definition expected.h:177
expected(expected &&other) FL_NOEXCEPT=default
Move constructor (defaulted - variant handles it)
E error() const FL_NOEXCEPT
Definition expected.h:152
expected(const expected &other) FL_NOEXCEPT
Copy constructor (needed for some use cases like Impl initialization)
Definition expected.h:186
~expected() FL_NOEXCEPT=default
Destructor (defaulted - variant handles cleanup)
expected(expected &&other) FL_NOEXCEPT=default
Move constructor (defaulted - variant handles it)
E error() const FL_NOEXCEPT
Get error code (only meaningful if !ok())
Definition expected.h:88
expected() FL_NOEXCEPT
Default constructor (creates error state)
Definition expected.h:122
T & value() FL_NOEXCEPT
Get value (only valid if ok() == true)
Definition expected.h:101
expected & operator=(expected &&other) FL_NOEXCEPT=default
Move assignment (defaulted - variant handles it)
static expected failure(E err, const char *msg=nullptr) FL_NOEXCEPT
Create error result.
Definition expected.h:115
bool ok() const FL_NOEXCEPT
Check if operation succeeded.
Definition expected.h:82
fl::variant< T, ErrorInfo< E > > mData
Definition expected.h:134
const T & value() const FL_NOEXCEPT
Definition expected.h:102
bool has_value() const FL_NOEXCEPT
Check if operation succeeded (alias for ok())
Definition expected.h:85
static expected success(T value) FL_NOEXCEPT
Create successful result.
Definition expected.h:108
const char * message() const FL_NOEXCEPT
Get error message (only meaningful if !ok())
Definition expected.h:94
constexpr remove_reference< T >::type && move(T &&t) FL_NOEXCEPT
Definition s16x16x4.h:28
unsigned char u8
Definition stdint.h:131
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
@ TIMEOUT
Operation timed out.
Definition rx.h:153
@ OK
No error (not typically used)
Definition rx.h:69
@ INVALID_ARGUMENT
Invalid input arguments.
Definition rx.h:72
pair_element< I, T1, T2 >::type & get(pair< T1, T2 > &p) FL_NOEXCEPT
Definition pair.h:115
Base definition for an LED controller.
Definition crgb.hpp:179
Dummy type for void expected success state.
Definition expected.h:142
#define FL_NOEXCEPT
ErrorInfo(E err, const char *msg=nullptr) FL_NOEXCEPT
Definition expected.h:71
fl::string message
Definition expected.h:69
Error information for expected type.
Definition expected.h:67