GridGain C++
future.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 GridGain Systems, Inc. and Contributors.
3  *
4  * Licensed under the GridGain Community Edition License (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
23 #ifndef _IGNITE_FUTURE
24 #define _IGNITE_FUTURE
25 
26 #include <ignite/common/shared_state.h>
27 
28 namespace ignite
29 {
33  namespace common
34  {
35  // Forward declaration
36  template<typename T>
37  class Promise;
38  }
49  template<typename T>
50  class Future
51  {
52  friend class common::Promise<T>;
53 
54  public:
56  typedef T ValueType;
57 
63  Future(const Future<ValueType>& src) :
64  state(src.state)
65  {
66  // No-op.
67  }
68 
76  {
77  state = other.state;
78 
79  return *this;
80  }
81 
86  void Wait() const
87  {
88  const common::SharedState<ValueType>* state0 = state.Get();
89 
90  assert(state0 != 0);
91 
92  state0->Wait();
93  }
94 
102  bool WaitFor(int32_t msTimeout) const
103  {
104  const common::SharedState<ValueType>* state0 = state.Get();
105 
106  assert(state0 != 0);
107 
108  return state0->WaitFor(msTimeout);
109  }
110 
118  const ValueType& GetValue() const
119  {
120  const common::SharedState<ValueType>* state0 = state.Get();
121 
122  assert(state0 != 0);
123 
124  return state0->GetValue();
125  }
126 
130  void Cancel()
131  {
132  common::SharedState<ValueType>* state0 = state.Get();
133 
134  assert(state0 != 0);
135 
136  state0->Cancel();
137  }
138 
142  bool IsReady()
143  {
144  common::SharedState<ValueType>* state0 = state.Get();
145 
146  assert(state0 != 0);
147 
148  return state0->IsSet();
149  }
150 
151  private:
157  Future(common::concurrent::SharedPointer< common::SharedState<ValueType> > state0) :
158  state(state0)
159  {
160  // No-op.
161  }
162 
164  common::concurrent::SharedPointer< common::SharedState<ValueType> > state;
165  };
166 
170  template<>
171  class Future<void>
172  {
173  friend class common::Promise<void>;
174 
175  public:
177  typedef void ValueType;
178 
184  Future(const Future<ValueType>& src) :
185  state(src.state)
186  {
187  // No-op.
188  }
189 
197  {
198  state = other.state;
199 
200  return *this;
201  }
202 
207  void Wait() const
208  {
209  const common::SharedState<ValueType>* state0 = state.Get();
210 
211  assert(state0 != 0);
212 
213  state0->Wait();
214  }
215 
223  bool WaitFor(int32_t msTimeout) const
224  {
225  const common::SharedState<ValueType>* state0 = state.Get();
226 
227  assert(state0 != 0);
228 
229  return state0->WaitFor(msTimeout);
230  }
231 
238  void GetValue() const
239  {
240  const common::SharedState<ValueType>* state0 = state.Get();
241 
242  assert(state0 != 0);
243 
244  state0->GetValue();
245  }
246 
250  void Cancel()
251  {
252  common::SharedState<ValueType>* state0 = state.Get();
253 
254  assert(state0 != 0);
255 
256  state0->Cancel();
257  }
258 
262  bool IsReady()
263  {
264  common::SharedState<ValueType>* state0 = state.Get();
265 
266  assert(state0 != 0);
267 
268  return state0->IsSet();
269  }
270 
271  private:
277  Future(common::concurrent::SharedPointer< common::SharedState<ValueType> > state0) :
278  state(state0)
279  {
280  // No-op.
281  }
282 
284  common::concurrent::SharedPointer< common::SharedState<ValueType> > state;
285  };
286 
290  template<typename T>
291  class Future< common::concurrent::SharedPointer<T> >
292  {
293  friend class common::Promise< common::concurrent::SharedPointer<T> >;
294 
295  public:
297  typedef T ValueType;
298 
300  typedef common::concurrent::SharedPointer<ValueType> SP_ValueType;
301 
307  Future(const Future<SP_ValueType>& src) :
308  state(src.state)
309  {
310  // No-op.
311  }
312 
320  {
321  if (this != &other)
322  state = other.state;
323 
324  return *this;
325  }
326 
331  void Wait() const
332  {
333  const common::SharedState<SP_ValueType>* state0 = state.Get();
334 
335  assert(state0 != 0);
336 
337  state0->Wait();
338  }
339 
347  bool WaitFor(int32_t msTimeout) const
348  {
349  const common::SharedState<SP_ValueType>* state0 = state.Get();
350 
351  assert(state0 != 0);
352 
353  return state0->WaitFor(msTimeout);
354  }
355 
364  {
365  const common::SharedState<SP_ValueType>* state0 = state.Get();
366 
367  assert(state0 != 0);
368 
369  return state0->GetValue();
370  }
371 
375  void Cancel()
376  {
377  common::SharedState<SP_ValueType>* state0 = state.Get();
378 
379  assert(state0 != 0);
380 
381  state0->Cancel();
382  }
383 
387  bool IsReady()
388  {
389  common::SharedState<SP_ValueType>* state0 = state.Get();
390 
391  assert(state0 != 0);
392 
393  return state0->IsSet();
394  }
395 
396  private:
402  Future(common::concurrent::SharedPointer< common::SharedState<SP_ValueType> > state0) :
403  state(state0)
404  {
405  // No-op.
406  }
407 
409  common::concurrent::SharedPointer< common::SharedState<SP_ValueType> > state;
410  };
411 }
412 
413 #endif //_IGNITE_FUTURE
ignite::Future< void >::ValueType
void ValueType
Template value type.
Definition: future.h:177
ignite::Future< common::concurrent::SharedPointer< T > >::ValueType
T ValueType
Template value type.
Definition: future.h:297
ignite
Ignite API.
Definition: cache.h:47
ignite::Future::WaitFor
bool WaitFor(int32_t msTimeout) const
Wait for value to be set for specified time.
Definition: future.h:102
ignite::Future< common::concurrent::SharedPointer< T > >::IsReady
bool IsReady()
Check if the future ready.
Definition: future.h:387
ignite::Future
Future class template.
Definition: future.h:50
ignite::Future< void >::WaitFor
bool WaitFor(int32_t msTimeout) const
Wait for value to be set for specified time.
Definition: future.h:223
ignite::Future::Wait
void Wait() const
Wait for value to be set.
Definition: future.h:86
ignite::Future< void >::Cancel
void Cancel()
Cancel related operation.
Definition: future.h:250
ignite::Future::Future
Future(const Future< ValueType > &src)
Copy constructor.
Definition: future.h:63
ignite::Future< common::concurrent::SharedPointer< T > >::Wait
void Wait() const
Wait for value to be set.
Definition: future.h:331
ignite::Future< common::concurrent::SharedPointer< T > >::WaitFor
bool WaitFor(int32_t msTimeout) const
Wait for value to be set for specified time.
Definition: future.h:347
ignite::Future< common::concurrent::SharedPointer< T > >::GetValue
SP_ValueType GetValue() const
Get the set value.
Definition: future.h:363
ignite::Future< common::concurrent::SharedPointer< T > >::SP_ValueType
common::concurrent::SharedPointer< ValueType > SP_ValueType
Template value type shared pointer.
Definition: future.h:300
ignite::Future< void >::IsReady
bool IsReady()
Check if the future ready.
Definition: future.h:262
ignite::Future< common::concurrent::SharedPointer< T > >::Cancel
void Cancel()
Cancel related operation.
Definition: future.h:375
ignite::Future< common::concurrent::SharedPointer< T > >::operator=
Future & operator=(const Future< SP_ValueType > &other)
Assignment operator.
Definition: future.h:319
ignite::Future< void >::Wait
void Wait() const
Wait for value to be set.
Definition: future.h:207
ignite::Future::ValueType
T ValueType
Template value type.
Definition: future.h:56
ignite::Future::operator=
Future & operator=(const Future< ValueType > &other)
Assignment operator.
Definition: future.h:75
ignite::Future::Cancel
void Cancel()
Cancel related operation.
Definition: future.h:130
ignite::Future< void >::Future
Future(const Future< ValueType > &src)
Copy constructor.
Definition: future.h:184
ignite::Future< void >::GetValue
void GetValue() const
Wait for operation complition or error.
Definition: future.h:238
ignite::Future::IsReady
bool IsReady()
Check if the future ready.
Definition: future.h:142
ignite::Future::GetValue
const ValueType & GetValue() const
Get the set value.
Definition: future.h:118
ignite::Future< void >::operator=
Future & operator=(const Future< ValueType > &other)
Assignment operator.
Definition: future.h:196