You.i Engine
YiFuture.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2017. All rights reserved.
2 #ifndef _YI_FUTURE_H_
3 #define _YI_FUTURE_H_
4 
5 #include "signal/YiSignal.h"
6 #include "thread/YiAutoMutex.h"
7 
8 class CYITaskBase;
9 class CYIMutex;
10 class CYIWaitCondition;
11 
23 {
24  friend class CYITaskBase;
25  template<typename T> friend class CYIFuture;
26 
27 public:
28  virtual ~CYIAbstractFuture();
29 
33  bool IsCancelled() const;
34 
38  bool IsCompleted() const;
39 
53  bool Cancel();
54 
62  bool CancelOrWait();
63 
73  bool Wait() const;
74 
86  bool Wait(uint64_t uTimeoutMs) const;
87 
90 
91 protected:
92  void ExclusiveLock() const;
93  void ExclusiveUnlock() const;
94  void NotifyStateChanged() const;
95  bool SetTask(CYITaskBase *pTask);
96  void DisconnectFromTask();
97  bool SetCancelledNonLocking();
98  bool WaitNonLocking() const;
99  bool WaitNonLocking(uint64_t uTimeoutMs) const;
100  bool CancelInternal(bool bWait);
101 
102 private:
104 
105  // Hiding copy constructor and assignment operator
107  CYIAbstractFuture &operator=(const CYIAbstractFuture &);
108 
109  bool m_bCancelled;
110  bool m_bCompleted;
111  mutable CYIMutex m_mutex;
112  mutable CYIWaitCondition m_stateChange;
113  CYITaskBase *m_pTask;
114 };
115 
116 
117 
138 template <typename ResultType = void>
140 {
141  template<typename T> friend class CYITask;
142 
143 public:
144  CYIFuture();
145  virtual ~CYIFuture();
146 
156  bool Get(ResultType *pValue) const;
157 
167  const ResultType &Get() const;
168 
178  bool Get(ResultType *pValue, uint32_t uTimeoutMs) const;
179 
189  const ResultType &Get(uint32_t uTimeoutMs) const;
190 
198  bool Set(const ResultType &rValue);
199 
200 public: // signals
202 
203 protected:
204  bool SetCompleted(const ResultType &rValue);
205 
206 private:
207  ResultType m_result;
208 };
209 
210 
211 
217 template <>
218 class CYIFuture<> : public CYIAbstractFuture
219 {
220  template<typename T> friend class CYITask;
221 
222 public:
223  CYIFuture();
224  virtual ~CYIFuture();
225 
233  bool Set();
234 
235 public: // signals
237 
238 protected:
239  bool SetCompleted();
240 };
241 
242 
243 
249 template <typename ResultType>
250 class CYIFuture<ResultType*> : public CYIAbstractFuture
251 {
252  template<typename T> friend class CYITask;
253 
254 public:
255  CYIFuture();
256  virtual ~CYIFuture();
257 
269  bool Get(ResultType **pValue) const;
270 
284  const ResultType *Get() const;
285 
297  bool Get(ResultType **pValue, uint32_t uTimeoutMs) const;
298 
312  const ResultType *Get(uint32_t uTimeoutMs) const;
313 
327  ResultType* Take();
328 
342  ResultType *Take(uint32_t uTimeoutMs);
343 
353  bool Set(ResultType *pValue);
354 
355 public: // signals
357 
358 protected:
359  bool SetCompleted(ResultType *pValue);
360 
361 private:
362  ResultType *m_pResult;
363  bool m_bHasOwnershipOfResult;
364 };
365 
366 
367 
368 // Use of array types in CYIFuture objects is not supported.
369 template <typename ResultType>
370 class CYIFuture<ResultType[]> : public CYIAbstractFuture
371 {
372 private:
373  CYIFuture();
374 };
375 
376 
381 #include "YiFuture.inl"
382 
383 #endif /* _YI_FUTURE_H_ */
CYISignal Cancelled
A signal triggered when this object is cancelled.
Definition: YiFuture.h:89
Definition: YiTask.h:24
bool IsCompleted() const
virtual ~CYIAbstractFuture()
CYISignal Completed
A signal triggered when this object is marked as completed.
Definition: YiFuture.h:236
void ExclusiveLock() const
Locks both this object and the associated task (if it exists).
Definition: YiMutex.h:110
bool SetTask(CYITaskBase *pTask)
Sets the task asssociated with this object to the provided task. Returns true if the task was associa...
void ExclusiveUnlock() const
Unlocks both this object and the associated task (if it exists).
void DisconnectFromTask()
Removes the task reference, if it exists. If an associated task exists, an attempt is made at cancell...
CYISignal ExecutionStarted
A signal triggered when an associated task begins execution.
Definition: YiFuture.h:88
Definition: YiFuture.h:22
void NotifyStateChanged() const
Notify waiters (e.g. threads blocked on calls to Wait, Get or Take) of state change.
bool IsCancelled() const
bool WaitNonLocking() const
Equivalent to the Wait() function, but the caller must have m_mutex locked.
CYISignal< const ResultType & > Completed
A signal triggered when a value is assigned to this object.
Definition: YiFuture.h:201
bool CancelInternal(bool bWait)
Definition: YiTask.h:168
friend class CYIFuture
Definition: YiFuture.h:25
bool Wait() const
CYISignal Completed
A signal triggered when a value is assigned to this object. Does not pass a pointer to the contained ...
Definition: YiFuture.h:356
Definition: YiFuture.h:139
A class used to block a thread until a condition is met, as signaled by a different thread...
Definition: YiWaitCondition.h:64
Signals and slots are a thread-safe and flexible communication framework that will allow various obje...
Definition: YiSignal.h:164
bool SetCancelledNonLocking()
Sets the &#39;cancelled&#39; flag on this object, if it hasn&#39;t been done already and the future hasn&#39;t had a ...