libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
future.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <future>
11#include <chrono>
13#include <promeki/core/result.h>
14
16
38template <typename T>
39class Future {
40 public:
42 Future() = default;
43
45 Future(std::future<T> &&f) : _future(std::move(f)) {}
46
48 ~Future() = default;
49
50 Future(const Future &) = delete;
51 Future &operator=(const Future &) = delete;
52
54 Future(Future &&other) = default;
55
58
63 bool isReady() const {
64 return _future.valid() &&
65 _future.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
66 }
67
76 Result<T> result(unsigned int timeoutMs = 0) {
77 if(!_future.valid()) return Result<T>(T{}, Error::Invalid);
78 if(timeoutMs == 0) {
79 return Result<T>(_future.get(), Error::Ok);
80 }
81 if(_future.wait_for(std::chrono::milliseconds(timeoutMs)) ==
82 std::future_status::ready) {
83 return Result<T>(_future.get(), Error::Ok);
84 }
85 return Result<T>(T{}, Error::Timeout);
86 }
87
92 if(_future.valid()) _future.wait();
93 }
94
101 if(!_future.valid()) return Error::Invalid;
102 if(_future.wait_for(std::chrono::milliseconds(timeoutMs)) ==
103 std::future_status::ready) {
104 return Error::Ok;
105 }
106 return Error::Timeout;
107 }
108
113 bool isValid() const {
114 return _future.valid();
115 }
116
117 private:
118 mutable std::future<T> _future;
119};
120
126template <>
127class Future<void> {
128 public:
130 Future() = default;
131
133 Future(std::future<void> &&f) : _future(std::move(f)) {}
134
136 ~Future() = default;
137
138 Future(const Future &) = delete;
139 Future &operator=(const Future &) = delete;
140 Future(Future &&other) = default;
141 Future &operator=(Future &&other) = default;
142
147 bool isReady() const {
148 return _future.valid() &&
149 _future.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
150 }
151
158 Error result(unsigned int timeoutMs = 0) {
159 if(!_future.valid()) return Error::Invalid;
160 if(timeoutMs == 0) {
161 _future.get();
162 return Error::Ok;
163 }
164 if(_future.wait_for(std::chrono::milliseconds(timeoutMs)) ==
165 std::future_status::ready) {
166 _future.get();
167 return Error::Ok;
168 }
169 return Error::Timeout;
170 }
171
176 if(_future.valid()) _future.wait();
177 }
178
185 if(!_future.valid()) return Error::Invalid;
186 if(_future.wait_for(std::chrono::milliseconds(timeoutMs)) ==
187 std::future_status::ready) {
188 return Error::Ok;
189 }
190 return Error::Timeout;
191 }
192
197 bool isValid() const {
198 return _future.valid();
199 }
200
201 private:
202 mutable std::future<void> _future;
203};
204
Lightweight error code wrapper for the promeki library.
Definition error.h:39
@ Timeout
Operation timed out.
Definition error.h:78
@ Ok
No error.
Definition error.h:51
@ Invalid
Invalid value or argument (EINVAL).
Definition error.h:66
Error result(unsigned int timeoutMs=0)
Waits for completion and returns the status.
Definition future.h:158
bool isValid() const
Returns whether this Future holds a valid shared state.
Definition future.h:197
bool isReady() const
Checks whether the result is ready without blocking.
Definition future.h:147
Error waitForFinished(unsigned int timeoutMs)
Blocks until the result is ready or the timeout expires.
Definition future.h:184
Future(std::future< void > &&f)
Constructs a Future from a std::future.
Definition future.h:133
void waitForFinished()
Blocks until the result is ready.
Definition future.h:175
Future()=default
Constructs an invalid (empty) Future.
~Future()=default
Destructor.
Asynchronous result wrapping std::future<T>.
Definition future.h:39
Future()=default
Constructs an invalid (empty) Future.
Future(std::future< T > &&f)
Constructs a Future from a std::future.
Definition future.h:45
bool isReady() const
Checks whether the result is ready without blocking.
Definition future.h:63
Result< T > result(unsigned int timeoutMs=0)
Returns the result, blocking until available or timeout.
Definition future.h:76
void waitForFinished()
Blocks until the result is ready.
Definition future.h:91
bool isValid() const
Returns whether this Future holds a valid shared state.
Definition future.h:113
Future(Future &&other)=default
Move constructor.
~Future()=default
Destructor.
Future & operator=(Future &&other)=default
Move assignment.
Error waitForFinished(unsigned int timeoutMs)
Blocks until the result is ready or the timeout expires.
Definition future.h:100
Dynamic array container wrapping std::vector.
Definition list.h:40
#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