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 #include <vector>
37 #include <boost/shared_ptr.hpp>
38 #include <string>
39 
40 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
41 #pragma warning(push)
42 #pragma warning(disable: 4251) //for dll export
43 #endif
44 
45 namespace hazelcast {
46  namespace client {
47  namespace serialization {
48  class Portable;
49 
50  class IdentifiedDataSerializable;
51 
52  namespace pimpl {
53  class PortableContext;
54 
55  class DataInput;
56 
57  class Data;
58  }
59 
64  class HAZELCAST_API ObjectDataInput {
65  public:
69  ObjectDataInput(pimpl::DataInput&, pimpl::PortableContext&);
70 
75  void readFully(std::vector<byte>& byteArray);
76 
81  int skipBytes(int i);
82 
87  bool readBoolean();
88 
93  byte readByte();
94 
99  short readShort();
100 
105  char readChar();
106 
111  int readInt();
112 
117  long readLong();
118 
123  float readFloat();
124 
129  double readDouble();
130 
135  std::auto_ptr<std::string> readUTF();
136 
141  std::auto_ptr<std::vector<byte> > readByteArray();
142 
147  std::auto_ptr<std::vector<bool> > readBooleanArray();
148 
153  std::auto_ptr<std::vector<char> > readCharArray();
154 
159  std::auto_ptr<std::vector<int> > readIntArray();
160 
165  std::auto_ptr<std::vector<long> > readLongArray();
166 
171  std::auto_ptr<std::vector<double> > readDoubleArray();
172 
177  std::auto_ptr<std::vector<float> > readFloatArray();
178 
183  std::auto_ptr<std::vector<short> > readShortArray();
184 
189  std::auto_ptr<std::vector<std::string> > readUTFArray();
190 
197  template<typename T>
198  std::auto_ptr<T> readObject() {
199  int typeId = readInt();
200  const pimpl::SerializationConstants& constants = portableContext.getConstants();
201  if (constants.CONSTANT_TYPE_NULL == typeId) {
202  return std::auto_ptr<T>();
203  } else {
204  std::auto_ptr<T> result(new T);
205  constants.checkClassType(getHazelcastTypeId(result.get()) , typeId);
206  if (constants.CONSTANT_TYPE_DATA == typeId) {
207  readDataSerializable(reinterpret_cast<IdentifiedDataSerializable *>(result.get()));
208  } else if (constants.CONSTANT_TYPE_PORTABLE == typeId) {
209  readPortable(reinterpret_cast<Portable *>(result.get()));
210  } else {
211  readInternal<T>(typeId, result.get());
212  }
213  return std::auto_ptr<T>(result.release());
214  }
215  }
216 
221  pimpl::Data readData();
222 
226  int position();
227 
232  void position(int newPos);
233 
234  private:
235 
236  template <typename T>
237  void readInternal(int typeId, T * object) {
238  boost::shared_ptr<SerializerBase> serializer = serializerHolder.serializerFor(typeId);
239  if (NULL == serializer.get()) {
240  const std::string message = "No serializer found for serializerId :"+
241  util::IOUtil::to_string(typeId) + ", typename :" +
242  typeid(T).name();
243  throw exception::HazelcastSerializationException("ObjectDataInput::readInternal", message);
244  }
245 
246  Serializer<T> *s = static_cast<Serializer<T> * >(serializer.get());
247  ObjectDataInput objectDataInput(dataInput, portableContext);
248  s->read(objectDataInput, *object);
249  }
250 
251  void readPortable(Portable *object);
252 
253  void readDataSerializable(IdentifiedDataSerializable * object);
254 
255  pimpl::DataInput& dataInput;
256  pimpl::PortableContext& portableContext;
257  pimpl::SerializerHolder& serializerHolder;
258 
259  ObjectDataInput(const ObjectDataInput&);
260 
261  void operator=(const ObjectDataInput&);
262 
263  };
264 
265  template <>
266  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, byte *object);
267 
268  template <>
269  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, bool *object);
270 
271  template <>
272  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, char *object);
273 
274  template <>
275  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, short *object);
276 
277  template <>
278  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, int *object);
279 
280  template <>
281  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, long *object);
282 
283  template <>
284  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, float *object);
285 
286  template <>
287  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, double *object);
288 
289  template <>
290  HAZELCAST_API void ObjectDataInput::readInternal(int typeId, std::string *object);
291  }
292  }
293 }
294 
295 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
296 #pragma warning(pop)
297 #endif
298 
299 #endif /* HAZELCAST_DATA_INPUT */
300 
Raised when an error occur during serialization or deserialization in Hazelcast.
Definition: HazelcastSerializationException.h:33
std::auto_ptr< T > readObject()
Object can be Portable, IdentifiedDataSerializable or custom serializable for custom serialization...
Definition: ObjectDataInput.h:198
Provides deserialization methods for primitives types, arrays of primitive types Portable, IdentifiedDataSerializable and custom serializable types.
Definition: ObjectDataInput.h:64