| Face Engine SDK 5.2.0 A face detection, recognition and tracking engine.

Future.h
1 #pragma once
2
3 //
4 // Future.h
5 // inc
6 //
7 // Created by k.delimbetov on 28.05.2018.
8 //
9
10 #include <cstdint>
11
12 namespace fsdk_internal {
13 #ifndef DOXYGEN_SHOULD_SKIP_THIS
14 template<typename T>
15 class FutureImpl;
16 template<typename T>
17 class PromiseImpl;
18 #endif
19 }
20
21 namespace fsdk {
23 enum class ExecutionPolicy: uint8_t {
24 Sync,
25 Async
26 };
27
29 enum class FuturePromiseState: uint8_t {
30 Empty,
31 Promised,
32 Fulfilled,
33 CancelRequested,
34 Canceled,
35 Failed,
36 Broken
37 };
38
40 class Noncopyable {
41 protected:
42 Noncopyable() = default;
43 ~Noncopyable() = default;
44
45 private:
46 Noncopyable(const Noncopyable&) = delete;
47 Noncopyable& operator=(const Noncopyable&) = delete;
48 };
49
50 template<typename T>
51 class Promise;
52
54 enum class FutureError: uint8_t {
55 Ok,
56 InvalidInput,
57 NotPromised,
58 NotFulfilled,
59 NonWaitable,
60 Timeout,
61 Internal
62 };
63
68 template<typename T>
69 class FSDK_API Future
70 : Noncopyable {
71 public:
73 using USec = int32_t;
75 using Error = FutureError;
77 using DataType = T;
78
79 // MARK: Base
81 Future() noexcept;
82 Future(Future&& mv) noexcept;
83 Future& operator=(Future&&) noexcept;
84 ~Future();
85
87 static void swap(Future& first, Future& second) noexcept;
88
89 // MARK: Interface
96 Result<Error> cancel(const ExecutionPolicy policy) noexcept;
97
102 FuturePromiseState state() const noexcept;
103
110 Result<Error> takeInto(DataType& container) noexcept;
111
117 Result<Error> wait() const noexcept;
118
125 Result<Error> waitFor(const USec usec) const noexcept;
126
127 private:
128 #ifndef DOXYGEN_SHOULD_SKIP_THIS
129 Future(fsdk_internal::FutureImpl<T>* impl) noexcept;
130
131 friend class Promise<T>;
132
133 // MARK: Internal
134 fsdk_internal::FutureImpl<T>* m_pimpl = nullptr;
135 #endif
136 };
137
139 enum class PromiseError: uint8_t {
140 Ok,
141 Internal,
142 StateNotCancelable,
143 StateNotFailable,
144 StateNotFulfillable,
145 StateDoesntAllowFutureCreation
146 };
147
152 template<typename T>
153 class FSDK_API Promise
154 : Noncopyable {
155 public:
157 using Error = PromiseError;
159 using DataType = T;
160
161 // MARK: Base
163 Promise() noexcept;
164 Promise(Promise&&) noexcept;
165 Promise& operator=(Promise&&) noexcept;
166 ~Promise();
167
169 static void swap(Promise& first, Promise& second) noexcept;
170
171 // MARK: Interface
177 Result<Error> cancel() noexcept;
178
184 Result<Error> fail() noexcept;
185
192 Result<Error> fulfill(DataType&& promisedData) noexcept;
193
200 Future<DataType> future(Result<Error>& error) noexcept;
201
206 FuturePromiseState state() const noexcept;
207
208 private:
209 #ifndef DOXYGEN_SHOULD_SKIP_THIS
210 // MARK: Internal
211 fsdk_internal::PromiseImpl<T>* m_pimpl = nullptr;
212 #endif
213 };
214
218 template<>
219 struct ErrorTraits<FutureError> {
220 static bool isOk(const FutureError error) noexcept {
221 return error == FutureError::Ok;
222 }
223
224 static const char* toString (const FutureError error) noexcept {
225 switch(error) {
226 case FutureError::Ok:
227 return "Ok";
228 case FutureError::InvalidInput:
229 return "Invalid data is given as input to some method";
230 case FutureError::NotPromised:
231 return "Error if you call @see Future::cancel on not Promised state";
232 case FutureError::NotFulfilled:
233 return "Error if you call @see Future::takeInto on not Fulfilled state";
234 case FutureError::NonWaitable:
235 return "Error if you try to @see Future::wait on non waitable state";
236 case FutureError::Timeout:
237 return "Error if your @see Future::waitFor method ran out of time";
238 case FutureError::Internal:
239 return "Some internal error";
240 default:
241 return "Unknown error";
242 }
243 }
244 };
245
249 template<>
250 struct ErrorTraits<PromiseError> {
251 static bool isOk(const PromiseError error) noexcept {
252 return error == PromiseError::Ok;
253 }
254
255 static const char* toString (const PromiseError error) noexcept {
256 switch(error) {
257 case PromiseError::Ok:
258 return "Ok";
259 case PromiseError::Internal:
260 return "Internal error.";
261 case PromiseError::StateNotCancelable:
262 return "Error if you call @see Promise::cancel on not CancelRequested state.";
263 case PromiseError::StateNotFailable:
264 return "Error if you call @see Promise::fail on not Promised and not CancelRequested state.";
265 case PromiseError::StateNotFulfillable:
266 return "Error if you call @see Promise::fulfill on not Promised state.";
267 case PromiseError::StateDoesntAllowFutureCreation:
268 return "Error if you call @see Promise::future on not Empty state.";
269 default:
270 return "Unknown error";
271 }
272 }
273 };
274 }
fsdk::FutureError::Ok
@ Ok
All good.
fsdk::Future::Future
Future() noexcept
Constructor for empty useless shell of a future.
fsdk::FutureError::Internal
@ Internal
Some internal error.
fsdk::PromiseError::Internal
@ Internal
Internal error.
fsdk
SDK namespace.
Definition: IAGSEstimator.h:8
fsdk::Promise
Promise class to support asynchronous data retrieval Look into std::promise semantics to understand i...
Definition: Future.h:154
fsdk::Future::USec
int32_t USec
Alias for micro seconds type.
Definition: Future.h:73
fsdk::Promise::Promise
Promise() noexcept
Constructor for empty Promise.
FSDK_API
#define FSDK_API
Dummy.
Definition: Def.h:27
fsdk::FutureError
FutureError
Enumeration of possible Future errors.
Definition: Future.h:54
fsdk::Future
Future class to support asynchronous data retrieval Look into std::future semantics to understand it.
Definition: Future.h:70
fsdk::ExecutionPolicy::Sync
@ Sync
Execute synchronously.
fsdk::PromiseError::Ok
@ Ok
All good.
fsdk::FuturePromiseState::Empty
@ Empty
Lack of internal state: on promise creation and emptying Future.
fsdk::PromiseError
PromiseError
Enumeration of possible Promise errors.
Definition: Future.h:139
fsdk::Future::DataType
T DataType
Alias for stored data type.
Definition: Future.h:77
fsdk::ErrorTraits
Definition: Result.h:10
fsdk::FuturePromiseState
FuturePromiseState
Enumeration of possible future or promise states.
Definition: Future.h:29
fsdk::Noncopyable
Helper class to block copy operator&constructor of its descendants.
Definition: Future.h:40
fsdk::Result
A structure that encapsulates an action result enumeration.
Definition: Result.h:30
fsdk::ExecutionPolicy
ExecutionPolicy
Enum to tell some method how to behave.
Definition: Future.h:23
fsdk::FutureError::InvalidInput
@ InvalidInput
Invalid data is given as input to some method.
fsdk::Promise::DataType
T DataType
Alias for stored data type.
Definition: Future.h:159