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 }
283 
284 #endif //_IGNITE_FUTURE
const ValueType & GetValue() const
Get the set value.
Definition: future.h:114
Future & operator=(const Future< ValueType > &other)
Assignment operator.
Definition: future.h:71
void Cancel()
Cancel related operation.
Definition: future.h:246
void Cancel()
Cancel related operation.
Definition: future.h:126
bool IsReady()
Check if the future ready.
Definition: future.h:138
bool WaitFor(int32_t msTimeout) const
Wait for value to be set for specified time.
Definition: future.h:98
bool WaitFor(int32_t msTimeout) const
Wait for value to be set for specified time.
Definition: future.h:219
Future & operator=(const Future< ValueType > &other)
Assignment operator.
Definition: future.h:192
Future(const Future< ValueType > &src)
Copy constructor.
Definition: future.h:180
void Wait() const
Wait for value to be set.
Definition: future.h:82
bool IsReady()
Check if the future ready.
Definition: future.h:258
void GetValue() const
Wait for operation complition or error.
Definition: future.h:234
void Wait() const
Wait for value to be set.
Definition: future.h:203
Definition: future.h:36
void ValueType
Template value type.
Definition: future.h:173
Future(const Future< ValueType > &src)
Copy constructor.
Definition: future.h:59
Apache Ignite API.
Definition: cache.h:48
T ValueType
Template value type.
Definition: future.h:52
Declares ignite::IgniteError class.
Future class template.
Definition: future.h:46