Hazelcast C++ Client
 All Classes Functions Variables Enumerations Enumerator Pages
ObjectDataInput.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 //
17 // ObjectDataInput.h
18 // Server
19 //
20 // Created by sancar koyunlu on 1/3/13.
21 // Copyright (c) 2013 sancar koyunlu. All rights reserved.
22 //
23 
24 #ifndef HAZELCAST_DATA_INPUT
25 #define HAZELCAST_DATA_INPUT
26 
27 #include "hazelcast/client/exception/IOException.h"
28 #include "hazelcast/client/serialization/Serializer.h"
29 #include "hazelcast/client/serialization/pimpl/SerializerHolder.h"
30 #include "hazelcast/client/serialization/ClassDefinition.h"
31 #include "hazelcast/client/serialization/pimpl/PortableContext.h"
32 #include "hazelcast/client/exception/HazelcastSerializationException.h"
33 #include "hazelcast/client/serialization/pimpl/SerializationConstants.h"
34 #include "hazelcast/util/IOUtil.h"
35 #include "hazelcast/client/serialization/TypeIDS.h"
36 
37 #include <boost/shared_ptr.hpp>
38 #include <vector>
39 #include <string>
40 #include <stdint.h>
41 
42 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
43 #pragma warning(push)
44 #pragma warning(disable: 4251) //for dll export
45 #endif
46 
47 namespace hazelcast {
48  namespace client {
49  namespace serialization {
50  class Portable;
51 
52  class IdentifiedDataSerializable;
53 
54  namespace pimpl {
55  class PortableContext;
56 
57  class DataInput;
58 
59  class Data;
60  }
61 
66  class HAZELCAST_API ObjectDataInput {
67  public:
71  ObjectDataInput(pimpl::DataInput&, pimpl::PortableContext&);
72 
77  void readFully(std::vector<byte>& byteArray);
78 
83  int skipBytes(int i);
84 
89  bool readBoolean();
90 
95  byte readByte();
96 
101  int16_t readShort();
102 
107  char readChar();
108 
113  int32_t readInt();
114 
119  int64_t readLong();
120 
125  float readFloat();
126 
131  double readDouble();
132 
137  std::auto_ptr<std::string> readUTF();
138 
143  std::auto_ptr<std::vector<byte> > readByteArray();
144 
149  std::auto_ptr<std::vector<bool> > readBooleanArray();
150 
155  std::auto_ptr<std::vector<char> > readCharArray();
156 
161  std::auto_ptr<std::vector<int32_t> > readIntArray();
162 
167  std::auto_ptr<std::vector<int64_t> > readLongArray();
168 
173  std::auto_ptr<std::vector<double> > readDoubleArray();
174 
179  std::auto_ptr<std::vector<float> > readFloatArray();
180 
185  std::auto_ptr<std::vector<int16_t> > readShortArray();
186 
191  std::auto_ptr<std::vector<std::string> > readUTFArray();
192 
199  template<typename T>
200  std::auto_ptr<T> readObject() {
201  int32_t typeId = readInt();
202  const pimpl::SerializationConstants& constants = portableContext.getConstants();
203  if (constants.CONSTANT_TYPE_NULL == typeId) {
204  return std::auto_ptr<T>();
205  } else {
206  std::auto_ptr<T> result(new T);
207  constants.checkClassType(getHazelcastTypeId(result.get()) , typeId);
208  fillObject<T>(typeId, result.get());
209  return std::auto_ptr<T>(result.release());
210  }
211  }
212 
217  pimpl::Data readData();
218 
222  int position();
223 
228  void position(int newPos);
229 
230  private:
231 
232  template <typename T>
233  void readInternal(int typeId, T * object) {
234  boost::shared_ptr<SerializerBase> serializer = serializerHolder.serializerFor(typeId);
235  if (NULL == serializer.get()) {
236  const std::string message = "No serializer found for serializerId :"+
237  util::IOUtil::to_string(typeId) + ", typename :" +
238  typeid(T).name();
239  throw exception::HazelcastSerializationException("ObjectDataInput::readInternal", message);
240  }
241 
242  Serializer<T> *s = static_cast<Serializer<T> * >(serializer.get());
243  ObjectDataInput objectDataInput(dataInput, portableContext);
244  s->read(objectDataInput, *object);
245  }
246 
247  template <typename T>
248  void fillObject(int typeId, void *serializable) {
249  readInternal<T>(typeId, (T *) serializable);
250  }
251 
252  template <typename T>
253  void fillObject(int typeId, IdentifiedDataSerializable *serializable) {
254  readDataSerializable(serializable);
255  }
256 
257  template <typename T>
258  void fillObject(int typeId, Portable *serializable) {
259  readPortable(serializable);
260  }
261 
262  void readPortable(Portable *object);
263 
264  void readDataSerializable(IdentifiedDataSerializable * object);
265 
266  pimpl::DataInput& dataInput;
267  pimpl::PortableContext& portableContext;
268  pimpl::SerializerHolder& serializerHolder;
269 
270  ObjectDataInput(const ObjectDataInput&);
271 
272  void operator=(const ObjectDataInput&);
273 
274  };
275 
276  template <>
277  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, byte *object);
278 
279  template <>
280  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, bool *object);
281 
282  template <>
283  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, char *object);
284 
285  template <>
286  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, int16_t *object);
287 
288  template <>
289  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, int32_t *object);
290 
291  template <>
292  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, int64_t *object);
293 
294  template <>
295  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, float *object);
296 
297  template <>
298  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, double *object);
299 
300  template <>
301  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, std::string *object);
302  }
303  }
304 }
305 
306 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
307 #pragma warning(pop)
308 #endif
309 
310 #endif /* HAZELCAST_DATA_INPUT */
311 
std::auto_ptr< T > readObject()
Object can be Portable, IdentifiedDataSerializable or custom serializable for custom serialization...
Definition: ObjectDataInput.h:200
Provides deserialization methods for primitives types, arrays of primitive types Portable, IdentifiedDataSerializable and custom serializable types.
Definition: ObjectDataInput.h:66