| 56 | /// This is a lightweight wrapper around a shared PromiseImpl for easy copying/sharing |
| 57 | template<typename T> |
| 58 | class Promise { |
| 59 | public: |
| 60 | /// Create a pending Promise |
| 61 | static Promise<T> create() FL_NOEXCEPT { |
| 62 | auto impl = fl::make_shared<detail::PromiseImpl<T>>(); |
| 63 | return Promise<T>(impl); |
| 64 | } |
| 65 | |
| 66 | /// Create a resolved Promise with value |
| 67 | static Promise<T> resolve(const T& value) FL_NOEXCEPT { // okay static in header |
| 68 | auto p = create(); |
| 69 | p.complete_with_value(value); |
| 70 | return p; |
| 71 | } |
| 72 | |
| 73 | /// Create a resolved Promise with value (move version) |
| 74 | static Promise<T> resolve(T&& value) FL_NOEXCEPT { // okay static in header |
| 75 | auto p = create(); |
| 76 | p.complete_with_value(fl::move(value)); |
| 77 | return p; |
| 78 | } |
| 79 | |
| 80 | /// Create a rejected Promise with error |
| 81 | static Promise<T> reject(const Error& error) FL_NOEXCEPT { // okay static in header |
| 82 | auto p = create(); |
| 83 | p.complete_with_error(error); |
| 84 | return p; |
| 85 | } |
| 86 | |
| 87 | /// Create a rejected Promise with error message |
| 88 | static Promise<T> reject(const fl::string& error_message) FL_NOEXCEPT { // okay static in header |
| 89 | return reject(Error(error_message)); |
| 90 | } |
| 91 | |
| 92 | /// Default constructor - creates invalid Promise |
| 93 | Promise() FL_NOEXCEPT : mImpl(nullptr) {} |
| 94 | |
| 95 | /// Copy constructor (promises are now copyable via shared implementation) |
| 96 | Promise(const Promise& other) = default; |
| 97 | |
| 98 | /// Move constructor |
| 99 | Promise(Promise&& other) FL_NOEXCEPT = default; |
| 100 | |
| 101 | /// Copy assignment operator |
| 102 | Promise& operator=(const Promise& other) = default; |
| 103 | |
| 104 | /// Move assignment operator |
| 105 | Promise& operator=(Promise&& other) FL_NOEXCEPT = default; |
| 106 | |
| 107 | /// Check if Promise is valid |
| 108 | bool valid() const FL_NOEXCEPT { |
| 109 | return mImpl != nullptr; |
| 110 | } |
| 111 | |
| 112 | /// Register success callback - returns reference for chaining |
| 113 | /// @param callback Function to call when Promise resolves successfully |
| 114 | /// @returns Reference to this Promise for chaining |
| 115 | Promise& then(fl::function<void(const T&)> callback) FL_NOEXCEPT { |
nothing calls this directly
no test coverage detected