FastLED 3.9.15
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
13namespace fl {
14namespace task {
15
39template<typename T>
41public:
45
49
53
57
60 bool ok() const {
61 return mResult.template is<T>();
62 }
63
68 explicit operator bool() const {
69 return ok();
70 }
71
76 const T& value() const {
77 if (!ok()) {
78 // Return static empty object instead of crashing
79 static const T empty{};
80 return empty;
81 }
82 return mResult.template get<T>();
83 }
84
89 T& value() {
90 if (!ok()) {
91 // Return static empty object instead of crashing
92 static T empty{}; // okay static in header
93 return empty;
94 }
95 return mResult.template get<T>();
96 }
97
102 const Error& error() const {
103 if (ok()) {
104 // Return descriptive error for misuse detection
105 static const Error empty_error("No error - result contains success value");
106 return empty_error;
107 }
108 return mResult.template get<Error>();
109 }
110
115 return ok() ? fl::string() : error().message;
116 }
117
120 const fl::variant<T, Error>& variant() const {
121 return mResult;
122 }
123
124private:
125 fl::variant<T, Error> mResult;
126};
127
132template<typename T>
136
141template<typename T>
145
150template<typename T>
152 return PromiseResult<T>(error);
153}
154
159template<typename T>
161 return PromiseResult<T>(fl::move(error));
162}
163
168template<typename T>
170 return PromiseResult<T>(Error(message));
171}
172
177template<typename T>
178PromiseResult<T> make_error(const char* message) {
179 return PromiseResult<T>(Error(message));
180}
181
182} // namespace task
183} // namespace fl
const T & value() const
Get the success value (const)
PromiseResult(Error &&error)
Construct an error PromiseResult (move)
bool ok() const
Check if the result is successful.
PromiseResult(const T &value)
Construct a successful PromiseResult.
PromiseResult(T &&value)
Construct a successful PromiseResult (move)
fl::variant< T, Error > mResult
const fl::variant< T, Error > & variant() const
Access the underlying variant (for advanced usage)
T & value()
Get the success value (mutable)
PromiseResult(const Error &error)
Construct an error PromiseResult.
const Error & error() const
Get the error value.
fl::string error_message() const
Get the error message as a convenience.
Result type for promise operations.
constexpr remove_reference< T >::type && move(T &&t) FL_NOEXCEPT
Definition s16x16x4.h:28
PromiseResult< T > make_success(const T &value)
Helper function to create a successful result.
PromiseResult< T > make_error(const Error &error)
Helper function to create an error result.
constexpr remove_reference< T >::type && move(T &&t) FL_NOEXCEPT
Definition move.h:28
constexpr int type_rank< T >::value
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
Promise-based fluent API for FastLED - standalone async primitives.
fl::string message
Definition promise.h:40
Error type for promises.
Definition promise.h:39