libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
promise.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <future>
14#include <utility>
15#include <promeki/namespace.h>
16#include <promeki/future.h>
17
18PROMEKI_NAMESPACE_BEGIN
19
36template <typename T> class Promise {
37 public:
39 Promise() = default;
40
42 ~Promise() = default;
43
44 Promise(const Promise &) = delete;
45 Promise &operator=(const Promise &) = delete;
46
48 Promise(Promise &&other) = default;
49
51 Promise &operator=(Promise &&other) = default;
52
57 void setValue(T value) { _promise.set_value(std::move(value)); }
58
68 void setError(Error error) { _promise.set_exception(std::make_exception_ptr(PromiseError(error))); }
69
74 Future<T> future() { return Future<T>(_promise.get_future()); }
75
76 private:
77 std::promise<T> _promise;
78};
79
83template <> class Promise<void> {
84 public:
86 Promise() = default;
87
89 ~Promise() = default;
90
91 Promise(const Promise &) = delete;
92 Promise &operator=(const Promise &) = delete;
93 Promise(Promise &&other) = default;
94 Promise &operator=(Promise &&other) = default;
95
99 void setValue() { _promise.set_value(); }
100
109 void setError(Error error) { _promise.set_exception(std::make_exception_ptr(PromiseError(error))); }
110
115 Future<void> future() { return Future<void>(_promise.get_future()); }
116
117 private:
118 std::promise<void> _promise;
119};
120
121PROMEKI_NAMESPACE_END
122
123#endif // PROMEKI_ENABLE_CORE