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/namespace.h"
11#include "fl/variant.h"
12#include "fl/promise.h" // For Error type
13
14namespace fl {
15
39template<typename T>
40class result {
41public:
44 result(const T& value) : mResult(value) {}
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{};
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
121 return mResult;
122 }
123
124private:
126};
127
132template<typename T>
133result<T> make_success(const T& value) {
134 return result<T>(value);
135}
136
141template<typename T>
143 return result<T>(fl::move(value));
144}
145
150template<typename T>
152 return result<T>(error);
153}
154
159template<typename T>
161 return result<T>(fl::move(error));
162}
163
168template<typename T>
170 return result<T>(Error(message));
171}
172
177template<typename T>
178result<T> make_error(const char* message) {
179 return result<T>(Error(message));
180}
181
184template<typename T>
186
187} // namespace fl
result(T &&value)
Construct a successful result (move)
const fl::Variant< T, Error > & variant() const
Access the underlying variant (for advanced usage)
const Error & error() const
Get the error value.
const T & value() const
Get the success value (const)
result(Error &&error)
Construct an error result (move)
result(const Error &error)
Construct an error result.
bool ok() const
Check if the result is successful.
result(const T &value)
Construct a successful result.
T & value()
Get the success value (mutable)
fl::Variant< T, Error > mResult
fl::string error_message() const
Get the error message as a convenience.
Result type for promise operations.
Implements the FastLED namespace macros.
constexpr remove_reference< T >::type && move(T &&t) noexcept
Definition move.h:27
result< T > PromiseResult
Type alias for backwards compatibility.
result< T > make_success(const T &value)
Helper function to create a successful result.
result< T > make_error(const Error &error)
Helper function to create an error result.
pair_element< I, T1, T2 >::type & get(pair< T1, T2 > &p) noexcept
Definition pair.h:113
IMPORTANT!
Definition crgb.h:20
Promise-based fluent API for FastLED - standalone async primitives.
fl::string message
Definition promise.h:54
Error type for promises.
Definition promise.h:53