Hazelcast C++ Client
 All Classes Functions Variables Enumerations Pages
ObjectDataInput.h
1 /*
2  * Copyright (c) 2008-2015, 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  short readShort();
102 
107  char readChar();
108 
113  int 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<int> > readIntArray();
162 
167  std::auto_ptr<std::vector<long> > 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<short> > readShortArray();
186 
191  std::auto_ptr<std::vector<std::string> > readUTFArray();
192 
199  template<typename T>
200  std::auto_ptr<T> readObject() {
201  int 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  if (constants.CONSTANT_TYPE_DATA == typeId) {
209  readDataSerializable(reinterpret_cast<IdentifiedDataSerializable *>(result.get()));
210  } else if (constants.CONSTANT_TYPE_PORTABLE == typeId) {
211  readPortable(reinterpret_cast<Portable *>(result.get()));
212  } else {
213  readInternal<T>(typeId, result.get());
214  }
215  return std::auto_ptr<T>(result.release());
216  }
217  }
218 
223  pimpl::Data readData();
224 
228  int position();
229 
234  void position(int newPos);
235 
236  private:
237 
238  template <typename T>
239  void readInternal(int typeId, T * object) {
240  boost::shared_ptr<SerializerBase> serializer = serializerHolder.serializerFor(typeId);
241  if (NULL == serializer.get()) {
242  const std::string message = "No serializer found for serializerId :"+
243  util::IOUtil::to_string(typeId) + ", typename :" +
244  typeid(T).name();
245  throw exception::HazelcastSerializationException("ObjectDataInput::readInternal", message);
246  }
247 
248  Serializer<T> *s = static_cast<Serializer<T> * >(serializer.get());
249  ObjectDataInput objectDataInput(dataInput, portableContext);
250  s->read(objectDataInput, *object);
251  }
252 
253  void readPortable(Portable *object);
254 
255  void readDataSerializable(IdentifiedDataSerializable * object);
256 
257  pimpl::DataInput& dataInput;
258  pimpl::PortableContext& portableContext;
259  pimpl::SerializerHolder& serializerHolder;
260 
261  ObjectDataInput(const ObjectDataInput&);
262 
263  void operator=(const ObjectDataInput&);
264 
265  };
266 
267  template <>
268  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, byte *object);
269 
270  template <>
271  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, bool *object);
272 
273  template <>
274  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, char *object);
275 
276  template <>
277  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, short *object);
278 
279  template <>
280  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, int *object);
281 
282  template <>
283  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, long *object);
284 
285  template <>
286  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, float *object);
287 
288  template <>
289  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, double *object);
290 
291  template <>
292  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, std::string *object);
293  }
294  }
295 }
296 
297 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
298 #pragma warning(pop)
299 #endif
300 
301 #endif /* HAZELCAST_DATA_INPUT */
302 
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