Face Engine SDK  5.23.1
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>
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,
148  };
149 
154  template <typename T>
156  public:
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 <>
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";
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";
240  return "Some internal error";
241  default:
242  return "Unknown error";
243  }
244  }
245  };
246 
250  template <>
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";
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.";
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::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:155
fsdk::Future::USec
int32_t USec
Alias for micro seconds type.
Definition: Future.h:75
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:57
fsdk::Future
Future class to support asynchronous data retrieval Look into std::future semantics to understand it.
Definition: Future.h:72
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:141
fsdk::Future::DataType
T DataType
Alias for stored data type.
Definition: Future.h:79
fsdk::ErrorTraits
Definition: Result.h:8
fsdk::FuturePromiseState
FuturePromiseState
Enumeration of possible future or promise states.
Definition: Future.h:32
fsdk::Noncopyable
Helper class to block copy operator&constructor of its descendants.
Definition: Future.h:43
fsdk::Result
A structure that encapsulates an action result enumeration.
Definition: Result.h:27
fsdk::ExecutionPolicy
ExecutionPolicy
Enum to tell some method how to behave.
Definition: Future.h:26
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:160