You.i Engine
YiFuture.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2020. All rights reserved.
2 #ifndef _YI_FUTURE_H_
3 #define _YI_FUTURE_H_
4 
6 
7 #include "thread/YiTask.h"
8 #include "thread/YiThreadPools.h"
9 
10 class CYITaskBase;
11 
23 {
24  friend class CYITaskBase;
25  template<typename T>
26  friend class CYITask;
27 
28 public:
29  virtual ~CYIAbstractFuture();
30 
34  bool IsCancelled() const;
35 
39  bool IsCompleted() const;
40 
54  bool Cancel();
55 
63  bool CancelOrWait();
64 
74  bool Wait() const;
75 
87  bool Wait(uint64_t timeoutMs) const;
88 
89 public: // signals
91 
92 protected:
93  CYIAbstractFuture(std::shared_ptr<CYIFutureSharedStateBase> pSharedState);
94 
95  std::shared_ptr<CYIFutureSharedStateBase> m_pSharedState;
96 };
97 
137 template<typename ResultType>
139 {
140  friend class CYIFutureSharedState<ResultType>;
141 
142  template<typename Callable>
143  static auto ReturnTypeOf() -> decltype(std::declval<Callable>()(std::declval<CYIFuture<ResultType>>()));
144  template<typename Callable>
145  static auto ReturnTypeOf() -> decltype(std::declval<Callable>()(std::declval<CYITaskBase *>(), std::declval<CYIFuture<ResultType>>()));
146  template<typename Callable>
147  static auto ReturnTypeOf() -> decltype(std::declval<Callable>()());
148  template<typename Callable>
149  static auto ReturnTypeOf() -> decltype(std::declval<Callable>()(std::declval<CYITaskBase *>()));
150 
151 public:
155  CYIFuture();
156 
163  CYIFuture(ResultType (*pDefaultFactory)());
164 
165  CYIFuture(const CYIFuture &other);
166  CYIFuture(CYIFuture &&other) noexcept;
167 
168  CYIFuture &operator=(const CYIFuture &other);
169  CYIFuture &operator=(CYIFuture &&other) noexcept;
170 
176  void Reset();
177 
189  const ResultType &Get(bool *pValueAssigned = nullptr) const;
190 
206  ResultType Get(uint64_t timeoutMs, bool *pValueAssigned = nullptr) const;
207 
215  bool Set(ResultType rValue);
216 
230  ResultType Take(bool *pValueAssigned = nullptr);
231 
245  ResultType Take(uint64_t timeoutMs, bool *pValueAssigned = nullptr);
246 
252  operator const ResultType &() const;
253 
294  template<typename Callable>
296 
297 public: // signals
299 
300 protected:
302  inline CYIFutureSharedState<ResultType> *GetCastSharedState();
303  inline const CYIFutureSharedState<ResultType> *GetCastSharedState() const;
304 };
305 
311 template<>
312 class CYIFuture<void> : public CYIAbstractFuture
313 {
314  template<typename Callable>
315  static auto ReturnTypeOf() -> decltype(std::declval<Callable>()());
316  template<typename Callable>
317  static auto ReturnTypeOf() -> decltype(std::declval<Callable>()(std::declval<CYITaskBase *>()));
318 
319 public:
320  CYIFuture();
321 
327  void Reset();
328 
336  bool Set();
337 
368  template<typename Callable>
370 
371 public: // signals
373 
374 protected:
375  inline CYIFutureSharedState<> *GetCastSharedState();
376 };
377 
381 template<typename ResultType>
382 class CYIFuture<ResultType *> : public CYIAbstractFuture
383 {
384 public:
385  CYIFuture() = delete;
386 };
387 
391 template<typename ResultType>
392 class CYIFuture<ResultType[]> : public CYIAbstractFuture
393 {
394 public:
395  CYIFuture() = delete;
396 };
397 
402 #include "thread/internal/YiFuture.inl"
403 #include "thread/internal/YiTask.inl"
404 
405 #endif /* _YI_FUTURE_H_ */
Definition: YiTask.h:27
bool Wait() const
virtual ~CYIAbstractFuture()
Definition: YiFuture.h:22
RunAsyncMode
An enum that lists the various async running modes for use with the RunAsync() functions.
Definition: YiThreadPools.h:66
CYISignal * pCompleted
A signal triggered when this object is marked as completed.
Definition: YiFuture.h:372
Definition: YiTask.h:148
CYIAbstractFuture(std::shared_ptr< CYIFutureSharedStateBase > pSharedState)
bool IsCompleted() const
Definition: YiTask.h:13
std::shared_ptr< CYIFutureSharedStateBase > m_pSharedState
Definition: YiFuture.h:95
CYISignal * pCancelled
A signal triggered when this object is cancelled.
Definition: YiFuture.h:90
Definition: YiFuture.h:138
CYISignal< CYIFuture< ResultType > > * pCompleted
A signal triggered when a value is assigned to this object. The future value is wrapped in a CYIFutur...
Definition: YiFuture.h:298
Signals and slots are a thread-safe and flexible communication framework that will allow various obje...
Definition: YiSignal.h:168
bool IsCancelled() const