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 #include <ignite/ignite_error.h>
28 
29 namespace ignite
30 {
34  namespace common
35  {
36  // Forward declaration
37  template<typename T>
38  class Promise;
39  }
50  template<typename T>
51  class Future
52  {
53  friend class common::Promise<T>;
54 
55  public:
57  typedef T ValueType;
58 
64  Future(const Future<ValueType>& src) :
65  state(src.state)
66  {
67  // No-op.
68  }
69 
77  {
78  state = other.state;
79 
80  return *this;
81  }
82 
87  void Wait() const
88  {
89  const common::SharedState<ValueType>* state0 = state.Get();
90 
91  assert(state0 != 0);
92 
93  state0->Wait();
94  }
95 
103  bool WaitFor(int32_t msTimeout) const
104  {
105  const common::SharedState<ValueType>* state0 = state.Get();
106 
107  assert(state0 != 0);
108 
109  return state0->WaitFor(msTimeout);
110  }
111 
119  const ValueType& GetValue() const
120  {
121  const common::SharedState<ValueType>* state0 = state.Get();
122 
123  assert(state0 != 0);
124 
125  return state0->GetValue();
126  }
127 
131  void Cancel()
132  {
133  common::SharedState<ValueType>* state0 = state.Get();
134 
135  assert(state0 != 0);
136 
137  state0->Cancel();
138  }
139 
143  bool IsReady()
144  {
145  common::SharedState<ValueType>* state0 = state.Get();
146 
147  assert(state0 != 0);
148 
149  return state0->IsSet();
150  }
151 
152  private:
158  Future(common::concurrent::SharedPointer< common::SharedState<ValueType> > state0) :
159  state(state0)
160  {
161  // No-op.
162  }
163 
165  common::concurrent::SharedPointer< common::SharedState<ValueType> > state;
166  };
167 
171  template<>
172  class Future<void>
173  {
174  friend class common::Promise<void>;
175 
176  public:
178  typedef void ValueType;
179 
185  Future(const Future<ValueType>& src) :
186  state(src.state)
187  {
188  // No-op.
189  }
190 
198  {
199  state = other.state;
200 
201  return *this;
202  }
203 
208  void Wait() const
209  {
210  const common::SharedState<ValueType>* state0 = state.Get();
211 
212  assert(state0 != 0);
213 
214  state0->Wait();
215  }
216 
224  bool WaitFor(int32_t msTimeout) const
225  {
226  const common::SharedState<ValueType>* state0 = state.Get();
227 
228  assert(state0 != 0);
229 
230  return state0->WaitFor(msTimeout);
231  }
232 
239  void GetValue() const
240  {
241  const common::SharedState<ValueType>* state0 = state.Get();
242 
243  assert(state0 != 0);
244 
245  state0->GetValue();
246  }
247 
251  void Cancel()
252  {
253  common::SharedState<ValueType>* state0 = state.Get();
254 
255  assert(state0 != 0);
256 
257  state0->Cancel();
258  }
259 
263  bool IsReady()
264  {
265  common::SharedState<ValueType>* state0 = state.Get();
266 
267  assert(state0 != 0);
268 
269  return state0->IsSet();
270  }
271 
272  private:
278  Future(common::concurrent::SharedPointer< common::SharedState<ValueType> > state0) :
279  state(state0)
280  {
281  // No-op.
282  }
283 
285  common::concurrent::SharedPointer< common::SharedState<ValueType> > state;
286  };
287 
291  template<typename T>
292  class Future< common::concurrent::SharedPointer<T> >
293  {
294  friend class common::Promise< common::concurrent::SharedPointer<T> >;
295 
296  public:
298  typedef T ValueType;
299 
301  typedef common::concurrent::SharedPointer<ValueType> SP_ValueType;
302 
308  Future(const Future<SP_ValueType>& src) :
309  state(src.state)
310  {
311  // No-op.
312  }
313 
321  {
322  if (this != &other)
323  state = other.state;
324 
325  return *this;
326  }
327 
332  void Wait() const
333  {
334  const common::SharedState<SP_ValueType>* state0 = state.Get();
335 
336  assert(state0 != 0);
337 
338  state0->Wait();
339  }
340 
348  bool WaitFor(int32_t msTimeout) const
349  {
350  const common::SharedState<SP_ValueType>* state0 = state.Get();
351 
352  assert(state0 != 0);
353 
354  return state0->WaitFor(msTimeout);
355  }
356 
365  {
366  const common::SharedState<SP_ValueType>* state0 = state.Get();
367 
368  assert(state0 != 0);
369 
370  return state0->GetValue();
371  }
372 
376  void Cancel()
377  {
378  common::SharedState<SP_ValueType>* state0 = state.Get();
379 
380  assert(state0 != 0);
381 
382  state0->Cancel();
383  }
384 
388  bool IsReady()
389  {
390  common::SharedState<SP_ValueType>* state0 = state.Get();
391 
392  assert(state0 != 0);
393 
394  return state0->IsSet();
395  }
396 
397  private:
403  Future(common::concurrent::SharedPointer< common::SharedState<SP_ValueType> > state0) :
404  state(state0)
405  {
406  // No-op.
407  }
408 
410  common::concurrent::SharedPointer< common::SharedState<SP_ValueType> > state;
411  };
412 }
413 
414 #endif //_IGNITE_FUTURE
ignite::Future< void >::ValueType
void ValueType
Template value type.
Definition: future.h:178
ignite::Future< common::concurrent::SharedPointer< T > >::ValueType
T ValueType
Template value type.
Definition: future.h:298
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:103
ignite::Future< common::concurrent::SharedPointer< T > >::IsReady
bool IsReady()
Check if the future ready.
Definition: future.h:388
ignite::Future
Future class template.
Definition: future.h:51
ignite::Future< void >::WaitFor
bool WaitFor(int32_t msTimeout) const
Wait for value to be set for specified time.
Definition: future.h:224
ignite::Future::Wait
void Wait() const
Wait for value to be set.
Definition: future.h:87
ignite::Future< void >::Cancel
void Cancel()
Cancel related operation.
Definition: future.h:251
ignite::Future::Future
Future(const Future< ValueType > &src)
Copy constructor.
Definition: future.h:64
ignite::Future< common::concurrent::SharedPointer< T > >::Wait
void Wait() const
Wait for value to be set.
Definition: future.h:332
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:348
ignite::Future< common::concurrent::SharedPointer< T > >::GetValue
SP_ValueType GetValue() const
Get the set value.
Definition: future.h:364
ignite::Future< common::concurrent::SharedPointer< T > >::SP_ValueType
common::concurrent::SharedPointer< ValueType > SP_ValueType
Template value type shared pointer.
Definition: future.h:301
ignite::Future< void >::IsReady
bool IsReady()
Check if the future ready.
Definition: future.h:263
ignite::Future< common::concurrent::SharedPointer< T > >::Cancel
void Cancel()
Cancel related operation.
Definition: future.h:376
ignite::Future< common::concurrent::SharedPointer< T > >::operator=
Future & operator=(const Future< SP_ValueType > &other)
Assignment operator.
Definition: future.h:320
ignite::Future< void >::Wait
void Wait() const
Wait for value to be set.
Definition: future.h:208
ignite::Future::ValueType
T ValueType
Template value type.
Definition: future.h:57
ignite::Future::operator=
Future & operator=(const Future< ValueType > &other)
Assignment operator.
Definition: future.h:76
ignite::Future::Cancel
void Cancel()
Cancel related operation.
Definition: future.h:131
ignite::Future< void >::Future
Future(const Future< ValueType > &src)
Copy constructor.
Definition: future.h:185
ignite::Future< void >::GetValue
void GetValue() const
Wait for operation complition or error.
Definition: future.h:239
ignite::Future::IsReady
bool IsReady()
Check if the future ready.
Definition: future.h:143
ignite_error.h
ignite::Future::GetValue
const ValueType & GetValue() const
Get the set value.
Definition: future.h:119
ignite::Future< void >::operator=
Future & operator=(const Future< ValueType > &other)
Assignment operator.
Definition: future.h:197