FastLED 3.10.8
Loading...
Searching...
No Matches
promise_result.h
Go to the documentation of this file.
1#pragma once
2
9
10#include "fl/stl/variant.h"
11#include "fl/task/promise.h" // For Error type
12#include "fl/stl/noexcept.h"
13
14namespace fl {
15namespace task {
16
40template<typename T>
42public:
46
50
54
58
61 bool ok() const FL_NO_EXCEPT {
62 return mResult.template is<T>();
63 }
64
69 explicit operator bool() const FL_NO_EXCEPT {
70 return ok();
71 }
72
77 const T& value() const FL_NO_EXCEPT {
78 if (!ok()) {
79 // Return static empty object instead of crashing
80 static const T empty{};
81 return empty;
82 }
83 return mResult.template get<T>();
84 }
85
91 if (!ok()) {
92 // Return static empty object instead of crashing
93 static T empty{}; // okay static in header
94 return empty;
95 }
96 return mResult.template get<T>();
97 }
98
103 const Error& error() const FL_NO_EXCEPT {
104 if (ok()) {
105 // Return descriptive error for misuse detection
106 static const Error empty_error("No error - result contains success value");
107 return empty_error;
108 }
109 return mResult.template get<Error>();
110 }
111
116 return ok() ? fl::string() : error().message;
117 }
118
121 const fl::variant<T, Error>& variant() const FL_NO_EXCEPT {
122 return mResult;
123 }
124
125private:
126 fl::variant<T, Error> mResult;
127};
128
133template<typename T>
137
142template<typename T>
146
151template<typename T>
153 return PromiseResult<T>(error);
154}
155
160template<typename T>
164
169template<typename T>
171 return PromiseResult<T>(Error(message));
172}
173
178template<typename T>
180 return PromiseResult<T>(Error(message));
181}
182
183} // namespace task
184} // namespace fl
PromiseResult(const Error &error) FL_NO_EXCEPT
Construct an error PromiseResult.
fl::string error_message() const FL_NO_EXCEPT
Get the error message as a convenience.
PromiseResult(T &&value) FL_NO_EXCEPT
Construct a successful PromiseResult (move)
const Error & error() const FL_NO_EXCEPT
Get the error value.
fl::variant< T, Error > mResult
T & value() FL_NO_EXCEPT
Get the success value (mutable)
PromiseResult(Error &&error) FL_NO_EXCEPT
Construct an error PromiseResult (move)
bool ok() const FL_NO_EXCEPT
Check if the result is successful.
const fl::variant< T, Error > & variant() const FL_NO_EXCEPT
Access the underlying variant (for advanced usage)
const T & value() const FL_NO_EXCEPT
Get the success value (const)
PromiseResult(const T &value) FL_NO_EXCEPT
Construct a successful PromiseResult.
Result type for promise operations.
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
Definition s16x16x4.h:30
PromiseResult< T > make_error(const Error &error) FL_NO_EXCEPT
Helper function to create an error result.
PromiseResult< T > make_success(const T &value) FL_NO_EXCEPT
Helper function to create a successful result.
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
constexpr int type_rank< T >::value
pair_element< I, T1, T2 >::type & get(pair< T1, T2 > &p) FL_NO_EXCEPT
Definition pair.h:119
Base definition for an LED controller.
Promise-based fluent API for FastLED - standalone async primitives.
fl::string message
Definition promise.h:40
Error type for promises.
Definition promise.h:39