Face Engine SDK  5.8.0
A face detection, recognition and tracking engine.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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,
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>
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,
146  };
147 
152  template<typename T>
153  class FSDK_API Promise
154  : Noncopyable {
155  public:
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<>
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";
229  return "Invalid data is given as input to some method";
231  return "Error if you call @see Future::cancel on not Promised state";
233  return "Error if you call @see Future::takeInto on not Fulfilled state";
235  return "Error if you try to @see Future::wait on non waitable state";
237  return "Error if your @see Future::waitFor method ran out of time";
239  return "Some internal error";
240  default:
241  return "Unknown error";
242  }
243  }
244  };
245 
249  template<>
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";
260  return "Internal error.";
262  return "Error if you call @see Promise::cancel on not CancelRequested state.";
264  return "Error if you call @see Promise::fail on not Promised and not CancelRequested state.";
266  return "Error if you call @see Promise::fulfill on not Promised state.";
268  return "Error if you call @see Promise::future on not Empty state.";
269  default:
270  return "Unknown error";
271  }
272  }
273  };
274 }
State possible after Promised if promise&#39; method.
#define FSDK_API
Dummy.
Definition: Def.h:27
A structure that encapsulates an action result enumeration.
Definition: Result.h:29
Some internal error.
T DataType
Alias for stored data type.
Definition: Future.h:77
FutureError
Enumeration of possible Future errors.
Definition: Future.h:54
Helper class to block copy operator&amp;constructor of its descendants.
Definition: Future.h:40
Definition: Result.h:9
T DataType
Alias for stored data type.
Definition: Future.h:159
ExecutionPolicy
Enum to tell some method how to behave.
Definition: Future.h:23
State indicating some internal error.
State right after future is created from promise.
Execute asynchronously.
Execute synchronously.
Lack of internal state: on promise creation and emptying Future.
Promise class to support asynchronous data retrieval Look into std::promise semantics to understand i...
Definition: Future.h:51
FuturePromiseState
Enumeration of possible future or promise states.
Definition: Future.h:29
State possible if during CancelRequested.
State possible if during Promised.
Invalid data is given as input to some method.
PromiseError
Enumeration of possible Promise errors.
Definition: Future.h:139
Future class to support asynchronous data retrieval Look into std::future semantics to understand it...
Definition: Future.h:69
int32_t USec
Alias for micro seconds type.
Definition: Future.h:73
Error if you try to.