Apache Ignite C++
future.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
24 #ifndef _IGNITE_FUTURE
25 #define _IGNITE_FUTURE
26 
27 #include <ignite/common/shared_state.h>
28 #include <ignite/ignite_error.h>
29 
30 namespace ignite
31 {
32  namespace common
33  {
34  // Forward declaration
35  template<typename T>
36  class Promise;
37  }
38 
45  template<typename T>
46  class Future
47  {
48  friend class common::Promise<T>;
49 
50  public:
52  typedef T ValueType;
53 
59  Future(const Future<ValueType>& src) :
60  state(src.state)
61  {
62  // No-op.
63  }
64 
72  {
73  state = other.state;
74 
75  return *this;
76  }
77 
82  void Wait() const
83  {
84  const common::SharedState<ValueType>* state0 = state.Get();
85 
86  assert(state0 != 0);
87 
88  state0->Wait();
89  }
90 
98  bool WaitFor(int32_t msTimeout) const
99  {
100  const common::SharedState<ValueType>* state0 = state.Get();
101 
102  assert(state0 != 0);
103 
104  return state0->WaitFor(msTimeout);
105  }
106 
114  const ValueType& GetValue() const
115  {
116  const common::SharedState<ValueType>* state0 = state.Get();
117 
118  assert(state0 != 0);
119 
120  return state0->GetValue();
121  }
122 
126  void Cancel()
127  {
128  common::SharedState<ValueType>* state0 = state.Get();
129 
130  assert(state0 != 0);
131 
132  state0->Cancel();
133  }
134 
138  bool IsReady()
139  {
140  common::SharedState<ValueType>* state0 = state.Get();
141 
142  assert(state0 != 0);
143 
144  return state0->IsSet();
145  }
146 
147  private:
153  Future(common::concurrent::SharedPointer< common::SharedState<ValueType> > state0) :
154  state(state0)
155  {
156  // No-op.
157  }
158 
160  common::concurrent::SharedPointer< common::SharedState<ValueType> > state;
161  };
162 
166  template<>
167  class Future<void>
168  {
169  friend class common::Promise<void>;
170 
171  public:
173  typedef void ValueType;
174 
180  Future(const Future<ValueType>& src) :
181  state(src.state)
182  {
183  // No-op.
184  }
185 
193  {
194  state = other.state;
195 
196  return *this;
197  }
198 
203  void Wait() const
204  {
205  const common::SharedState<ValueType>* state0 = state.Get();
206 
207  assert(state0 != 0);
208 
209  state0->Wait();
210  }
211 
219  bool WaitFor(int32_t msTimeout) const
220  {
221  const common::SharedState<ValueType>* state0 = state.Get();
222 
223  assert(state0 != 0);
224 
225  return state0->WaitFor(msTimeout);
226  }
227 
234  void GetValue() const
235  {
236  const common::SharedState<ValueType>* state0 = state.Get();
237 
238  assert(state0 != 0);
239 
240  state0->GetValue();
241  }
242 
246  void Cancel()
247  {
248  common::SharedState<ValueType>* state0 = state.Get();
249 
250  assert(state0 != 0);
251 
252  state0->Cancel();
253  }
254 
258  bool IsReady()
259  {
260  common::SharedState<ValueType>* state0 = state.Get();
261 
262  assert(state0 != 0);
263 
264  return state0->IsSet();
265  }
266 
267  private:
273  Future(common::concurrent::SharedPointer< common::SharedState<ValueType> > state0) :
274  state(state0)
275  {
276  // No-op.
277  }
278 
280  common::concurrent::SharedPointer< common::SharedState<ValueType> > state;
281  };
282 
286  template<typename T>
287  class Future< common::concurrent::SharedPointer<T> >
288  {
289  friend class common::Promise< common::concurrent::SharedPointer<T> >;
290 
291  public:
293  typedef T ValueType;
294 
296  typedef common::concurrent::SharedPointer<ValueType> SP_ValueType;
297 
303  Future(const Future<SP_ValueType>& src) :
304  state(src.state)
305  {
306  // No-op.
307  }
308 
316  {
317  if (this != &other)
318  state = other.state;
319 
320  return *this;
321  }
322 
327  void Wait() const
328  {
329  const common::SharedState<SP_ValueType>* state0 = state.Get();
330 
331  assert(state0 != 0);
332 
333  state0->Wait();
334  }
335 
343  bool WaitFor(int32_t msTimeout) const
344  {
345  const common::SharedState<SP_ValueType>* state0 = state.Get();
346 
347  assert(state0 != 0);
348 
349  return state0->WaitFor(msTimeout);
350  }
351 
360  {
361  const common::SharedState<SP_ValueType>* state0 = state.Get();
362 
363  assert(state0 != 0);
364 
365  return state0->GetValue();
366  }
367 
371  void Cancel()
372  {
373  common::SharedState<SP_ValueType>* state0 = state.Get();
374 
375  assert(state0 != 0);
376 
377  state0->Cancel();
378  }
379 
383  bool IsReady()
384  {
385  common::SharedState<SP_ValueType>* state0 = state.Get();
386 
387  assert(state0 != 0);
388 
389  return state0->IsSet();
390  }
391 
392  private:
398  Future(common::concurrent::SharedPointer< common::SharedState<SP_ValueType> > state0) :
399  state(state0)
400  {
401  // No-op.
402  }
403 
405  common::concurrent::SharedPointer< common::SharedState<SP_ValueType> > state;
406  };
407 }
408 
409 #endif //_IGNITE_FUTURE
ignite::Future< void >::ValueType
void ValueType
Template value type.
Definition: future.h:173
ignite::Future< common::concurrent::SharedPointer< T > >::ValueType
T ValueType
Template value type.
Definition: future.h:293
ignite
Apache Ignite API.
Definition: cache.h:48
ignite::Future::WaitFor
bool WaitFor(int32_t msTimeout) const
Wait for value to be set for specified time.
Definition: future.h:98
ignite::Future< common::concurrent::SharedPointer< T > >::IsReady
bool IsReady()
Check if the future ready.
Definition: future.h:383
ignite::Future
Future class template.
Definition: future.h:46
ignite::Future< void >::WaitFor
bool WaitFor(int32_t msTimeout) const
Wait for value to be set for specified time.
Definition: future.h:219
ignite::Future::Wait
void Wait() const
Wait for value to be set.
Definition: future.h:82
ignite::Future< void >::Cancel
void Cancel()
Cancel related operation.
Definition: future.h:246
ignite::Future::Future
Future(const Future< ValueType > &src)
Copy constructor.
Definition: future.h:59
ignite::Future< common::concurrent::SharedPointer< T > >::Wait
void Wait() const
Wait for value to be set.
Definition: future.h:327
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:343
ignite::Future< common::concurrent::SharedPointer< T > >::GetValue
SP_ValueType GetValue() const
Get the set value.
Definition: future.h:359
ignite::Future< common::concurrent::SharedPointer< T > >::SP_ValueType
common::concurrent::SharedPointer< ValueType > SP_ValueType
Template value type shared pointer.
Definition: future.h:296
ignite::Future< void >::IsReady
bool IsReady()
Check if the future ready.
Definition: future.h:258
ignite::Future< common::concurrent::SharedPointer< T > >::Cancel
void Cancel()
Cancel related operation.
Definition: future.h:371
ignite::Future< common::concurrent::SharedPointer< T > >::operator=
Future & operator=(const Future< SP_ValueType > &other)
Assignment operator.
Definition: future.h:315
ignite::Future< void >::Wait
void Wait() const
Wait for value to be set.
Definition: future.h:203
ignite::Future::ValueType
T ValueType
Template value type.
Definition: future.h:52
ignite::Future::operator=
Future & operator=(const Future< ValueType > &other)
Assignment operator.
Definition: future.h:71
ignite::Future::Cancel
void Cancel()
Cancel related operation.
Definition: future.h:126
ignite::common::Promise
Definition: future.h:36
ignite::Future< void >::Future
Future(const Future< ValueType > &src)
Copy constructor.
Definition: future.h:180
ignite::Future< void >::GetValue
void GetValue() const
Wait for operation complition or error.
Definition: future.h:234
ignite::Future::IsReady
bool IsReady()
Check if the future ready.
Definition: future.h:138
ignite_error.h
ignite::Future::GetValue
const ValueType & GetValue() const
Get the set value.
Definition: future.h:114
ignite::Future< void >::operator=
Future & operator=(const Future< ValueType > &other)
Assignment operator.
Definition: future.h:192