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 <vector>
36 #include <boost/shared_ptr.hpp>
37 #include <string>
38 
39 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
40 #pragma warning(push)
41 #pragma warning(disable: 4251) //for dll export
42 #endif
43 
44 namespace hazelcast {
45  namespace client {
46  class Portable;
47 
48  class IdentifiedDataSerializable;
49 
50  namespace serialization {
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  boost::shared_ptr<T> readObject() {
199  int typeId = readInt();
200  if (pimpl::SerializationConstants::CONSTANT_TYPE_NULL == typeId) {
201  return boost::shared_ptr<T>(static_cast<T *>(NULL));
202  } else {
203  std::auto_ptr<T> result(new T);
204  if (pimpl::SerializationConstants::CONSTANT_TYPE_DATA == typeId) {
205  readDataSerializable(reinterpret_cast<IdentifiedDataSerializable *>(result.get()));
206  } else if (pimpl::SerializationConstants::CONSTANT_TYPE_PORTABLE == typeId) {
207  readPortable(reinterpret_cast<Portable *>(result.get()));
208  } else {
209  readInternal<T>(typeId, result.get());
210  }
211  return boost::shared_ptr<T>(result.release());
212  }
213  }
214 
219  pimpl::Data readData();
220 
224  int position();
225 
230  void position(int newPos);
231 
232  private:
233 
234  template <typename T>
235  void readInternal(int typeId, T * object) {
236  boost::shared_ptr<SerializerBase> serializer = serializerHolder.serializerFor(typeId);
237  if (NULL == serializer.get()) {
238  const std::string message = "No serializer found for serializerId :"+
239  util::IOUtil::to_string(typeId) + ", typename :" +
240  typeid(T).name();
241  throw exception::HazelcastSerializationException("ObjectDataInput::readInternal", message);
242  }
243 
244  Serializer<T> *s = static_cast<Serializer<T> * >(serializer.get());
245  ObjectDataInput objectDataInput(dataInput, portableContext);
246  s->read(objectDataInput, *object);
247  }
248 
249  void readPortable(Portable *object);
250 
251  void readDataSerializable(IdentifiedDataSerializable * object);
252 
253  pimpl::DataInput& dataInput;
254  pimpl::PortableContext& portableContext;
255  pimpl::SerializerHolder& serializerHolder;
256 
257  ObjectDataInput(const ObjectDataInput&);
258 
259  void operator=(const ObjectDataInput&);
260 
261  };
262  }
263  }
264 }
265 
266 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
267 #pragma warning(pop)
268 #endif
269 
270 #endif /* HAZELCAST_DATA_INPUT */
271 
Raised when an error occur during serialization or deserialization in Hazelcast.
Definition: HazelcastSerializationException.h:33
boost::shared_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