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

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