Hazelcast C++ Client
Future.h
1 /*
2  * Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (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  * http://www.apache.org/licenses/LICENSE-2.0
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 #ifndef HAZELCAST_CLIENT_FUTURE_H_
17 #define HAZELCAST_CLIENT_FUTURE_H_
18 
19 #include <stdint.h>
20 #include <memory>
21 #include <assert.h>
22 #include <boost/shared_ptr.hpp>
23 
24 #include "hazelcast/client/spi/impl/ClientInvocationFuture.h"
25 #include "hazelcast/client/serialization/pimpl/SerializationService.h"
26 #include "hazelcast/client/TypedData.h"
27 #include "hazelcast/client/protocol/ClientMessage.h"
28 
29 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
30 #pragma warning(push)
31 #pragma warning(disable: 4251) //for dll export
32 #endif
33 
34 namespace hazelcast {
35  namespace client {
36  class HAZELCAST_API future_status {
37  public:
38  future_status(int value) : value(value) { }
39 
40  enum enum_type {
41  ready,
42  timeout
43  };
44 
45  friend bool operator==(future_status lhs, future_status rhs) {
46  return enum_type(lhs.value) == enum_type(rhs.value);
47  }
48 
49  friend bool operator==(future_status lhs, enum_type rhs) { return enum_type(lhs.value) == rhs; }
50 
51  friend bool operator==(enum_type lhs, future_status rhs) { return lhs == enum_type(rhs.value); }
52 
53  friend bool operator!=(future_status lhs, future_status rhs) {
54  return enum_type(lhs.value) != enum_type(rhs.value);
55  }
56 
57  friend bool operator!=(future_status lhs, enum_type rhs) { return enum_type(lhs.value) != rhs; }
58 
59  friend bool operator!=(enum_type lhs, future_status rhs) { return lhs != enum_type(rhs.value); }
60 
61  friend bool operator<(future_status lhs, future_status rhs) {
62  return enum_type(lhs.value) < enum_type(rhs.value);
63  }
64 
65  friend bool operator<(future_status lhs, enum_type rhs) { return enum_type(lhs.value) < rhs; }
66 
67  friend bool operator<(enum_type lhs, future_status rhs) { return lhs < enum_type(rhs.value); }
68 
69  friend bool operator<=(future_status lhs, future_status rhs) {
70  return enum_type(lhs.value) <= enum_type(rhs.value);
71  }
72 
73  friend bool operator<=(future_status lhs, enum_type rhs) { return enum_type(lhs.value) <= rhs; }
74 
75  friend bool operator<=(enum_type lhs, future_status rhs) { return lhs <= enum_type(rhs.value); }
76 
77  friend bool operator>(future_status lhs, future_status rhs) {
78  return enum_type(lhs.value) > enum_type(rhs.value);
79  }
80 
81  friend bool operator>(future_status lhs, enum_type rhs) { return enum_type(lhs.value) > rhs; }
82 
83  friend bool operator>(enum_type lhs, future_status rhs) { return lhs > enum_type(rhs.value); }
84 
85  friend bool operator>=(future_status lhs, future_status rhs) {
86  return enum_type(lhs.value) >= enum_type(rhs.value);
87  }
88 
89  friend bool operator>=(future_status lhs, enum_type rhs) { return enum_type(lhs.value) >= rhs; }
90 
91  friend bool operator>=(enum_type lhs, future_status rhs) { return lhs >= enum_type(rhs.value); }
92 
93  private:
94  int value;
95  };
96 
104  template<typename V>
105  class Future {
106  public:
107  typedef std::auto_ptr<serialization::pimpl::Data> (*Decoder)(protocol::ClientMessage &response);
108 
112  Future(const boost::shared_ptr<spi::impl::ClientInvocationFuture> &invocationFuture,
113  serialization::pimpl::SerializationService &serializationService,
114  Decoder decoder) : clientInvocationFuture(invocationFuture),
115  serializationService(serializationService), decoderFunction(decoder) {
116  }
117 
123  Future(const Future &movedFuture) : clientInvocationFuture(movedFuture.clientInvocationFuture),
124  serializationService(movedFuture.serializationService),
125  decoderFunction(movedFuture.decoderFunction) {
126  const_cast<Future &>(movedFuture).clientInvocationFuture.reset();
127  }
128 
135  Future &operator=(const Future &movedFuture) {
136  this->clientInvocationFuture = movedFuture.clientInvocationFuture;
137  const_cast<Future &>(movedFuture).clientInvocationFuture.reset();
138  this->decoderFunction = movedFuture.decoderFunction;
139  return *this;
140  }
141 
142  virtual ~Future() {
143  }
144 
161  std::auto_ptr<V> get() {
162  if (!clientInvocationFuture.get()) {
163  throw exception::FutureUninitialized("Future::get", "Future needs to be initialized. "
164  "It may have been moved from.");
165  }
166 
167  boost::shared_ptr<protocol::ClientMessage> responseMsg = clientInvocationFuture->get();
168 
169  assert(responseMsg.get());
170 
171  clientInvocationFuture.reset();
172 
173  std::auto_ptr<serialization::pimpl::Data> response = decoderFunction(*responseMsg);
174 
175  std::auto_ptr<V> result = serializationService.toObject<V>(response.get());
176 
177  return result;
178  }
179 
191  future_status wait_for(int64_t timeoutInMilliseconds) const {
192  if (!clientInvocationFuture.get()) {
193  throw exception::FutureUninitialized("Future::get", "Future needs to be initialized.");
194  }
195 
196  try {
197  clientInvocationFuture->get(timeoutInMilliseconds, concurrent::TimeUnit::MILLISECONDS());
198  return future_status::ready;
199  } catch (exception::TimeoutException &) {
200  return future_status::timeout;
201  }
202  }
203 
210  void wait() const {
211  wait_for(INT64_MAX);
212  }
213 
221  bool valid() const {
222  return clientInvocationFuture.get() != NULL;
223  }
224 
225  private:
226  boost::shared_ptr<spi::impl::ClientInvocationFuture> clientInvocationFuture;
227  serialization::pimpl::SerializationService &serializationService;
228  Decoder decoderFunction;
229  };
230 
234  template<>
235  class Future<TypedData> {
236  public:
237  typedef std::auto_ptr<serialization::pimpl::Data> (*Decoder)(protocol::ClientMessage &response);
238 
242  Future(const boost::shared_ptr<spi::impl::ClientInvocationFuture> &invocationFuture,
243  serialization::pimpl::SerializationService &serializationService,
244  Decoder decoder) : clientInvocationFuture(invocationFuture),
245  serializationService(serializationService), decoderFunction(decoder) {
246  }
247 
253  Future(const Future &movedFuture) : clientInvocationFuture(movedFuture.clientInvocationFuture),
254  serializationService(movedFuture.serializationService),
255  decoderFunction(movedFuture.decoderFunction) {
256  const_cast<Future &>(movedFuture).clientInvocationFuture.reset();
257  }
258 
265  Future &operator=(const Future &movedFuture) {
266  this->clientInvocationFuture = movedFuture.clientInvocationFuture;
267  const_cast<Future &>(movedFuture).clientInvocationFuture.reset();
268  this->decoderFunction = movedFuture.decoderFunction;
269  return *this;
270  }
271 
272  virtual ~Future() {
273  }
274 
291  TypedData get() {
292  if (!clientInvocationFuture.get()) {
293  throw exception::FutureUninitialized("Future::get", "Future needs to be initialized. "
294  "It may have been moved from.");
295  }
296 
297  boost::shared_ptr<protocol::ClientMessage> responseMsg = clientInvocationFuture->get();
298 
299  assert(responseMsg.get());
300 
301  clientInvocationFuture.reset();
302 
303  std::auto_ptr<serialization::pimpl::Data> response = decoderFunction(*responseMsg);
304 
305  return TypedData(response, serializationService);
306  }
307 
319  future_status wait_for(int64_t timeoutInMilliseconds) const {
320  if (!clientInvocationFuture.get()) {
321  throw exception::FutureUninitialized("Future::get", "Future needs to be initialized.");
322  }
323 
324  try {
325  clientInvocationFuture->get(timeoutInMilliseconds, concurrent::TimeUnit::MILLISECONDS());
326  return future_status::ready;
327  } catch (exception::TimeoutException &) {
328  return future_status::timeout;
329  }
330  }
331 
338  void wait() const {
339  wait_for(INT64_MAX);
340  }
341 
349  bool valid() const {
350  return clientInvocationFuture.get() != NULL;
351  }
352 
353  private:
354  boost::shared_ptr<spi::impl::ClientInvocationFuture> clientInvocationFuture;
355  serialization::pimpl::SerializationService &serializationService;
356  Decoder decoderFunction;
357  };
358  }
359 }
360 
361 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
362 #pragma warning(pop)
363 #endif
364 
365 #endif /* HAZELCAST_CLIENT_FUTURE_H_ */
bool valid() const
Checks if the future refers to a shared state.
Definition: Future.h:349
bool valid() const
Checks if the future refers to a shared state.
Definition: Future.h:221
void wait() const
Blocks until the result becomes available.
Definition: Future.h:338
void wait() const
Blocks until the result becomes available.
Definition: Future.h:210
Future(const Future &movedFuture)
This is actually a move constructor Constructs a Future with the shared state of movedFuture using mo...
Definition: Future.h:253
Future & operator=(const Future &movedFuture)
Assigns the contents of another future object.
Definition: Future.h:265
Future(const boost::shared_ptr< spi::impl::ClientInvocationFuture > &invocationFuture, serialization::pimpl::SerializationService &serializationService, Decoder decoder)
This constructor is only for internal use!!!!
Definition: Future.h:112
Definition: Future.h:36
future_status wait_for(int64_t timeoutInMilliseconds) const
Waits for the result to become available.
Definition: Future.h:191
This is a unique Future.
Definition: Future.h:105
future_status wait_for(int64_t timeoutInMilliseconds) const
Waits for the result to become available.
Definition: Future.h:319
Future(const boost::shared_ptr< spi::impl::ClientInvocationFuture > &invocationFuture, serialization::pimpl::SerializationService &serializationService, Decoder decoder)
This constructor is only for internal use!!!!
Definition: Future.h:242
PN (Positive-Negative) CRDT counter.
Definition: MapEntryView.h:32
Future & operator=(const Future &movedFuture)
Assigns the contents of another future object.
Definition: Future.h:135
Future(const Future &movedFuture)
This is actually a move constructor Constructs a Future with the shared state of movedFuture using mo...
Definition: Future.h:123
TypedData class is a wrapper class for the serialized binary data.
Definition: TypedData.h:40