libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
promise.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <future>
11#include <utility>
13#include <promeki/core/future.h>
14
16
26template <typename T>
27class Promise {
28 public:
30 Promise() = default;
31
33 ~Promise() = default;
34
35 Promise(const Promise &) = delete;
36 Promise &operator=(const Promise &) = delete;
37
39 Promise(Promise &&other) = default;
40
43
49 _promise.set_value(std::move(value));
50 }
51
61 _promise.set_exception(
62 std::make_exception_ptr(PromiseError(error)));
63 }
64
70 return Future<T>(_promise.get_future());
71 }
72
73 private:
77 struct PromiseError {
79 PromiseError(Error e) : error(e) {}
80 };
81
82 std::promise<T> _promise;
83};
84
88template <>
89class Promise<void> {
90 public:
92 Promise() = default;
93
95 ~Promise() = default;
96
97 Promise(const Promise &) = delete;
98 Promise &operator=(const Promise &) = delete;
99 Promise(Promise &&other) = default;
100 Promise &operator=(Promise &&other) = default;
101
105 void setValue() {
106 _promise.set_value();
107 }
108
114 _promise.set_exception(
115 std::make_exception_ptr(PromiseError(error)));
116 }
117
123 return Future<void>(_promise.get_future());
124 }
125
126 private:
127 struct PromiseError {
128 Error error;
129 PromiseError(Error e) : error(e) {}
130 };
131
132 std::promise<void> _promise;
133};
134
Lightweight error code wrapper for the promeki library.
Definition error.h:39
Dynamic array container wrapping std::vector.
Definition list.h:40
~Promise()=default
Destructor.
void setValue()
Signals completion (no value).
Definition promise.h:105
Promise()=default
Constructs a Promise.
void setError(Error error)
Sets an error on the promise.
Definition promise.h:113
Future< void > future()
Returns the Future associated with this Promise.
Definition promise.h:122
Promise wrapping std::promise<T>.
Definition promise.h:27
Promise()=default
Constructs a Promise.
Future< T > future()
Returns the Future associated with this Promise.
Definition promise.h:69
void setError(Error error)
Sets an error on the promise.
Definition promise.h:60
Promise & operator=(Promise &&other)=default
Move assignment.
~Promise()=default
Destructor.
Promise(Promise &&other)=default
Move constructor.
void setValue(T value)
Sets the result value.
Definition promise.h:48
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
const T & value(const Result< T > &r)
Returns the value from a Result.
Definition result.h:56
const Error & error(const Result< T > &r)
Returns the error from a Result.
Definition result.h:65