Hazelcast C++ Client
Future.h
1 /*
2  * Copyright (c) 2008-2018, 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  return clientInvocationFuture->waitFor(timeoutInMilliseconds) ? future_status::ready : future_status::timeout;
197  }
198 
205  void wait() const {
206  wait_for(INT64_MAX);
207  }
208 
216  bool valid() const {
217  return clientInvocationFuture.get() != NULL;
218  }
219 
220  private:
221  boost::shared_ptr<spi::impl::ClientInvocationFuture> clientInvocationFuture;
222  serialization::pimpl::SerializationService &serializationService;
223  Decoder decoderFunction;
224  };
225 
229  template<>
230  class Future<TypedData> {
231  public:
232  typedef std::auto_ptr<serialization::pimpl::Data> (*Decoder)(protocol::ClientMessage &response);
233 
237  Future(const boost::shared_ptr<spi::impl::ClientInvocationFuture> &invocationFuture,
238  serialization::pimpl::SerializationService &serializationService,
239  Decoder decoder) : clientInvocationFuture(invocationFuture),
240  serializationService(serializationService), decoderFunction(decoder) {
241  }
242 
248  Future(const Future &movedFuture) : clientInvocationFuture(movedFuture.clientInvocationFuture),
249  serializationService(movedFuture.serializationService),
250  decoderFunction(movedFuture.decoderFunction) {
251  const_cast<Future &>(movedFuture).clientInvocationFuture.reset();
252  }
253 
260  Future &operator=(const Future &movedFuture) {
261  this->clientInvocationFuture = movedFuture.clientInvocationFuture;
262  const_cast<Future &>(movedFuture).clientInvocationFuture.reset();
263  this->decoderFunction = movedFuture.decoderFunction;
264  return *this;
265  }
266 
267  virtual ~Future() {
268  }
269 
286  TypedData get() {
287  if (!clientInvocationFuture.get()) {
288  throw exception::FutureUninitialized("Future::get", "Future needs to be initialized. "
289  "It may have been moved from.");
290  }
291 
292  boost::shared_ptr<protocol::ClientMessage> responseMsg = clientInvocationFuture->get();
293 
294  assert(responseMsg.get());
295 
296  clientInvocationFuture.reset();
297 
298  std::auto_ptr<serialization::pimpl::Data> response = decoderFunction(*responseMsg);
299 
300  return TypedData(response, serializationService);
301  }
302 
314  future_status wait_for(int64_t timeoutInMilliseconds) const {
315  if (!clientInvocationFuture.get()) {
316  throw exception::FutureUninitialized("Future::get", "Future needs to be initialized.");
317  }
318 
319  return clientInvocationFuture->waitFor(timeoutInMilliseconds) ? future_status::ready : future_status::timeout;
320  }
321 
328  void wait() const {
329  wait_for(INT64_MAX);
330  }
331 
339  bool valid() const {
340  return clientInvocationFuture.get() != NULL;
341  }
342 
343  private:
344  boost::shared_ptr<spi::impl::ClientInvocationFuture> clientInvocationFuture;
345  serialization::pimpl::SerializationService &serializationService;
346  Decoder decoderFunction;
347  };
348  }
349 }
350 
351 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
352 #pragma warning(pop)
353 #endif
354 
355 #endif /* HAZELCAST_CLIENT_FUTURE_H_ */
bool valid() const
Checks if the future refers to a shared state.
Definition: Future.h:339
bool valid() const
Checks if the future refers to a shared state.
Definition: Future.h:216
void wait() const
Blocks until the result becomes available.
Definition: Future.h:328
void wait() const
Blocks until the result becomes available.
Definition: Future.h:205
Future(const Future &movedFuture)
This is actually a move constructor Constructs a Future with the shared state of movedFuture using mo...
Definition: Future.h:248
Future & operator=(const Future &movedFuture)
Assigns the contents of another future object.
Definition: Future.h:260
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:314
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:237
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