Face Engine SDK  5.23.1
A face detection, recognition and tracking engine.
include/fsdk/Types/Future.h
00001 #pragma once
00002 
00003 #include <fsdk/Def.h>
00004 #include <fsdk/Types/Result.h>
00005 
00006 //
00007 //  Future.h
00008 //  inc
00009 //
00010 //  Created by k.delimbetov on 28.05.2018.
00011 //
00012 
00013 #include <cstdint>
00014 
00015 namespace fsdk_internal {
00016 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00017     template <typename T>
00018     class FutureImpl;
00019     template <typename T>
00020     class PromiseImpl;
00021 #endif
00022 } // namespace fsdk_internal
00023 
00024 namespace fsdk {
00026     enum class ExecutionPolicy : uint8_t {
00027         Sync, 
00028         Async 
00029     };
00030 
00032     enum class FuturePromiseState : uint8_t {
00033         Empty,     
00034         Promised,  
00035         Fulfilled, 
00036         CancelRequested, 
00037         Canceled,        
00038         Failed,          
00039         Broken           
00040     };
00041 
00043     class Noncopyable {
00044     protected:
00045         Noncopyable() = default;
00046         ~Noncopyable() = default;
00047 
00048     private:
00049         Noncopyable(const Noncopyable&) = delete;
00050         Noncopyable& operator=(const Noncopyable&) = delete;
00051     };
00052 
00053     template <typename T>
00054     class Promise;
00055 
00057     enum class FutureError : uint8_t {
00058         Ok,           
00059         InvalidInput, 
00060         NotPromised,  
00061         NotFulfilled, 
00062         NonWaitable,  
00063         Timeout,      
00064         Internal      
00065     };
00066 
00071     template <typename T>
00072     class FSDK_API Future : Noncopyable {
00073     public:
00075         using USec = int32_t;
00077         using Error = FutureError;
00079         using DataType = T;
00080 
00081         // MARK: Base
00083         Future() noexcept;
00084         Future(Future&& mv) noexcept;
00085         Future& operator=(Future&&) noexcept;
00086         ~Future();
00087 
00089         static void swap(Future& first, Future& second) noexcept;
00090 
00091         // MARK: Interface
00098         Result<Error> cancel(const ExecutionPolicy policy) noexcept;
00099 
00104         FuturePromiseState state() const noexcept;
00105 
00112         Result<Error> takeInto(DataType& container) noexcept;
00113 
00119         Result<Error> wait() const noexcept;
00120 
00127         Result<Error> waitFor(const USec usec) const noexcept;
00128 
00129     private:
00130 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00131         Future(fsdk_internal::FutureImpl<T>* impl) noexcept;
00132 
00133         friend class Promise<T>;
00134 
00135         // MARK: Internal
00136         fsdk_internal::FutureImpl<T>* m_pimpl = nullptr;
00137 #endif
00138     };
00139 
00141     enum class PromiseError : uint8_t {
00142         Ok,                 
00143         Internal,           
00144         StateNotCancelable, 
00145         StateNotFailable, 
00146         StateNotFulfillable,           
00147         StateDoesntAllowFutureCreation 
00148     };
00149 
00154     template <typename T>
00155     class FSDK_API Promise : Noncopyable {
00156     public:
00158         using Error = PromiseError;
00160         using DataType = T;
00161 
00162         // MARK: Base
00164         Promise() noexcept;
00165         Promise(Promise&&) noexcept;
00166         Promise& operator=(Promise&&) noexcept;
00167         ~Promise();
00168 
00170         static void swap(Promise& first, Promise& second) noexcept;
00171 
00172         // MARK: Interface
00178         Result<Error> cancel() noexcept;
00179 
00185         Result<Error> fail() noexcept;
00186 
00193         Result<Error> fulfill(DataType&& promisedData) noexcept;
00194 
00201         Future<DataType> future(Result<Error>& error) noexcept;
00202 
00207         FuturePromiseState state() const noexcept;
00208 
00209     private:
00210 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00211         // MARK: Internal
00212         fsdk_internal::PromiseImpl<T>* m_pimpl = nullptr;
00213 #endif
00214     };
00215 
00219     template <>
00220     struct ErrorTraits<FutureError> {
00221         static bool isOk(const FutureError error) noexcept {
00222             return error == FutureError::Ok;
00223         }
00224 
00225         static const char* toString(const FutureError error) noexcept {
00226             switch(error) {
00227             case FutureError::Ok:
00228                 return "Ok";
00229             case FutureError::InvalidInput:
00230                 return "Invalid data is given as input to some method";
00231             case FutureError::NotPromised:
00232                 return "Error if you call @see Future::cancel on not Promised state";
00233             case FutureError::NotFulfilled:
00234                 return "Error if you call @see Future::takeInto on not Fulfilled state";
00235             case FutureError::NonWaitable:
00236                 return "Error if you try to @see Future::wait on non waitable state";
00237             case FutureError::Timeout:
00238                 return "Error if your @see Future::waitFor method ran out of time";
00239             case FutureError::Internal:
00240                 return "Some internal error";
00241             default:
00242                 return "Unknown error";
00243             }
00244         }
00245     };
00246 
00250     template <>
00251     struct ErrorTraits<PromiseError> {
00252         static bool isOk(const PromiseError error) noexcept {
00253             return error == PromiseError::Ok;
00254         }
00255 
00256         static const char* toString(const PromiseError error) noexcept {
00257             switch(error) {
00258             case PromiseError::Ok:
00259                 return "Ok";
00260             case PromiseError::Internal:
00261                 return "Internal error.";
00262             case PromiseError::StateNotCancelable:
00263                 return "Error if you call @see Promise::cancel on not CancelRequested state.";
00264             case PromiseError::StateNotFailable:
00265                 return "Error if you call @see Promise::fail on not Promised and not CancelRequested state.";
00266             case PromiseError::StateNotFulfillable:
00267                 return "Error if you call @see Promise::fulfill on not Promised state.";
00268             case PromiseError::StateDoesntAllowFutureCreation:
00269                 return "Error if you call @see Promise::future on not Empty state.";
00270             default:
00271                 return "Unknown error";
00272             }
00273         }
00274     };
00275 } // namespace fsdk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines