Face Engine SDK  5.21.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 #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,
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,
148  };
149 
154  template <typename T>
155  class FSDK_API Promise : Noncopyable {
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";
232  return "Error if you call @see Future::cancel on not Promised state";
234  return "Error if you call @see Future::takeInto on not Fulfilled state";
236  return "Error if you try to @see Future::wait on non waitable state";
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.";
263  return "Error if you call @see Promise::cancel on not CancelRequested state.";
265  return "Error if you call @see Promise::fail on not Promised and not CancelRequested state.";
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
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:27
Some internal error.
T DataType
Alias for stored data type.
Definition: Future.h:79
FutureError
Enumeration of possible Future errors.
Definition: Future.h:57
Helper class to block copy operator&amp;constructor of its descendants.
Definition: Future.h:43
Definition: Result.h:8
T DataType
Alias for stored data type.
Definition: Future.h:160
ExecutionPolicy
Enum to tell some method how to behave.
Definition: Future.h:26
State indicating some internal error.
State right after future is created from promise.
Execute asynchronously.
Execute synchronously.
Common SDK definitions.
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:54
FuturePromiseState
Enumeration of possible future or promise states.
Definition: Future.h:32
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:141
Future class to support asynchronous data retrieval Look into std::future semantics to understand it...
Definition: Future.h:72
int32_t USec
Alias for micro seconds type.
Definition: Future.h:75
Error if you try to.