Hazelcast C++ Client
 All Classes Functions Variables Enumerations Enumerator Pages
Future.h
1 /*
2  * Copyright (c) 2008-2017, 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 
23 #include "hazelcast/client/connection/CallFuture.h"
24 #include "hazelcast/client/serialization/pimpl/SerializationService.h"
25 #include "hazelcast/client/connection/CallPromise.h"
26 
27 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
28 #pragma warning(push)
29 #pragma warning(disable: 4251) //for dll export
30 #endif
31 
32 namespace hazelcast {
33  namespace client {
34  class HAZELCAST_API future_status {
35  public:
36  future_status(int value) : value(value) { }
37 
38  enum enum_type {
39  ready,
40  timeout
41  };
42 
43  friend bool operator==(future_status lhs, future_status rhs) {
44  return enum_type(lhs.value) == enum_type(rhs.value);
45  }
46 
47  friend bool operator==(future_status lhs, enum_type rhs) { return enum_type(lhs.value) == rhs; }
48 
49  friend bool operator==(enum_type lhs, future_status rhs) { return lhs == enum_type(rhs.value); }
50 
51  friend bool operator!=(future_status lhs, future_status rhs) {
52  return enum_type(lhs.value) != enum_type(rhs.value);
53  }
54 
55  friend bool operator!=(future_status lhs, enum_type rhs) { return enum_type(lhs.value) != rhs; }
56 
57  friend bool operator!=(enum_type lhs, future_status rhs) { return lhs != enum_type(rhs.value); }
58 
59  friend bool operator<(future_status lhs, future_status rhs) {
60  return enum_type(lhs.value) < enum_type(rhs.value);
61  }
62 
63  friend bool operator<(future_status lhs, enum_type rhs) { return enum_type(lhs.value) < rhs; }
64 
65  friend bool operator<(enum_type lhs, future_status rhs) { return lhs < enum_type(rhs.value); }
66 
67  friend bool operator<=(future_status lhs, future_status rhs) {
68  return enum_type(lhs.value) <= enum_type(rhs.value);
69  }
70 
71  friend bool operator<=(future_status lhs, enum_type rhs) { return enum_type(lhs.value) <= rhs; }
72 
73  friend bool operator<=(enum_type lhs, future_status rhs) { return lhs <= enum_type(rhs.value); }
74 
75  friend bool operator>(future_status lhs, future_status rhs) {
76  return enum_type(lhs.value) > enum_type(rhs.value);
77  }
78 
79  friend bool operator>(future_status lhs, enum_type rhs) { return enum_type(lhs.value) > rhs; }
80 
81  friend bool operator>(enum_type lhs, future_status rhs) { return lhs > enum_type(rhs.value); }
82 
83  friend bool operator>=(future_status lhs, future_status rhs) {
84  return enum_type(lhs.value) >= enum_type(rhs.value);
85  }
86 
87  friend bool operator>=(future_status lhs, enum_type rhs) { return enum_type(lhs.value) >= rhs; }
88 
89  friend bool operator>=(enum_type lhs, future_status rhs) { return lhs >= enum_type(rhs.value); }
90 
91  private:
92  int value;
93  };
94 
95  namespace exception {
96  class HAZELCAST_API FutureUninitialized : public IException {
97  public:
98  FutureUninitialized(const std::string &source, const std::string &message) : IException(source,
99  message) { }
100  };
101  }
102 
110  template<typename V>
111  class Future {
112  public:
113  typedef std::auto_ptr<serialization::pimpl::Data> (*Decoder)(protocol::ClientMessage &response);
114 
118  Future(connection::CallFuture &callFuture,
119  serialization::pimpl::SerializationService &serializationService,
120  Decoder decoder) : callFuture(new connection::CallFuture(callFuture)),
121  serializationService(serializationService), decoderFunction(decoder) {
122  }
123 
129  Future(const Future &movedFuture) : callFuture(movedFuture.callFuture),
130  serializationService(movedFuture.serializationService),
131  decoderFunction(movedFuture.decoderFunction) {
132  }
133 
140  Future &operator=(const Future &movedFuture) {
141  this->callFuture = movedFuture.callFuture;
142  this->decoderFunction = movedFuture.decoderFunction;
143  return *this;
144  }
145 
146  virtual ~Future() {
147  }
148 
165  std::auto_ptr<V> get() {
166  if (!callFuture.get()) {
167  throw exception::FutureUninitialized("Future::get", "Future needs to be initialized. "
168  "It may have been moved from.");
169  }
170 
171  std::auto_ptr<protocol::ClientMessage> responseMsg = callFuture->get();
172 
173  assert(responseMsg.get());
174 
175  callFuture.reset();
176 
177  std::auto_ptr<serialization::pimpl::Data> response = decoderFunction(*responseMsg);
178 
179  std::auto_ptr<V> result = serializationService.toObject<V>(response.get());
180 
181  return result;
182  }
183 
195  future_status wait_for(int64_t timeoutInMilliseconds) const {
196  if (!callFuture.get()) {
197  throw exception::FutureUninitialized("Future::get", "Future needs to be initialized.");
198  }
199 
200  return callFuture->waitFor(timeoutInMilliseconds) ? future_status::ready : future_status::timeout;
201  }
202 
209  void wait() const {
210  wait_for(INT64_MAX);
211  }
212 
220  bool valid() const {
221  return callFuture.get() != NULL;
222  }
223 
224  private:
225  mutable std::auto_ptr<connection::CallFuture> callFuture;
226  serialization::pimpl::SerializationService &serializationService;
227  Decoder decoderFunction;
228  };
229  }
230 }
231 
232 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
233 #pragma warning(pop)
234 #endif
235 
236 #endif /* HAZELCAST_CLIENT_FUTURE_H_ */
Base class for all exception originated from Hazelcast methods.
Definition: IException.h:49
Definition: Future.h:34
This is a unique Future.
Definition: Future.h:111
bool valid() const
Checks if the future refers to a shared state.
Definition: Future.h:220
Future(connection::CallFuture &callFuture, serialization::pimpl::SerializationService &serializationService, Decoder decoder)
This constructor is only for internal use!!!!
Definition: Future.h:118
void wait() const
Blocks until the result becomes available.
Definition: Future.h:209
future_status wait_for(int64_t timeoutInMilliseconds) const
Waits for the result to become available.
Definition: Future.h:195
Future & operator=(const Future &movedFuture)
Assigns the contents of another future object.
Definition: Future.h:140
Future(const Future &movedFuture)
This is actually a move constructor Constructs a Future with the shared state of movedFuture using mo...
Definition: Future.h:129