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  namespace serialization {
47  class Portable;
48 
49  class IdentifiedDataSerializable;
50 
51  namespace pimpl {
52  class PortableContext;
53 
54  class DataInput;
55 
56  class Data;
57  }
58 
63  class HAZELCAST_API ObjectDataInput {
64  public:
68  ObjectDataInput(pimpl::DataInput&, pimpl::PortableContext&);
69 
74  void readFully(std::vector<byte>& byteArray);
75 
80  int skipBytes(int i);
81 
86  bool readBoolean();
87 
92  byte readByte();
93 
98  short readShort();
99 
104  char readChar();
105 
110  int readInt();
111 
116  long readLong();
117 
122  float readFloat();
123 
128  double readDouble();
129 
134  std::auto_ptr<std::string> readUTF();
135 
140  std::auto_ptr<std::vector<byte> > readByteArray();
141 
146  std::auto_ptr<std::vector<bool> > readBooleanArray();
147 
152  std::auto_ptr<std::vector<char> > readCharArray();
153 
158  std::auto_ptr<std::vector<int> > readIntArray();
159 
164  std::auto_ptr<std::vector<long> > readLongArray();
165 
170  std::auto_ptr<std::vector<double> > readDoubleArray();
171 
176  std::auto_ptr<std::vector<float> > readFloatArray();
177 
182  std::auto_ptr<std::vector<short> > readShortArray();
183 
188  std::auto_ptr<std::vector<std::string> > readUTFArray();
189 
196  template<typename T>
197  boost::shared_ptr<T> readObject() {
198  int typeId = readInt();
199  if (pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_NULL == typeId) {
200  return boost::shared_ptr<T>(static_cast<T *>(NULL));
201  } else {
202  std::auto_ptr<T> result(new T);
203  if (pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_DATA == typeId) {
204  readDataSerializable(reinterpret_cast<IdentifiedDataSerializable *>(result.get()));
205  } else if (pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_PORTABLE == typeId) {
206  readPortable(reinterpret_cast<Portable *>(result.get()));
207  } else {
208  readInternal<T>(typeId, result.get());
209  }
210  return boost::shared_ptr<T>(result.release());
211  }
212  }
213 
218  pimpl::Data readData();
219 
223  int position();
224 
229  void position(int newPos);
230 
231  private:
232 
233  template <typename T>
234  void readInternal(int typeId, T * object) {
235  boost::shared_ptr<SerializerBase> serializer = serializerHolder.serializerFor(typeId);
236  if (NULL == serializer.get()) {
237  const std::string message = "No serializer found for serializerId :"+
238  util::IOUtil::to_string(typeId) + ", typename :" +
239  typeid(T).name();
240  throw exception::HazelcastSerializationException("ObjectDataInput::readInternal", message);
241  }
242 
243  Serializer<T> *s = static_cast<Serializer<T> * >(serializer.get());
244  ObjectDataInput objectDataInput(dataInput, portableContext);
245  s->read(objectDataInput, *object);
246  }
247 
248  void readPortable(Portable *object);
249 
250  void readDataSerializable(IdentifiedDataSerializable * object);
251 
252  pimpl::DataInput& dataInput;
253  pimpl::PortableContext& portableContext;
254  pimpl::SerializerHolder& serializerHolder;
255 
256  ObjectDataInput(const ObjectDataInput&);
257 
258  void operator=(const ObjectDataInput&);
259 
260  };
261  }
262  }
263 }
264 
265 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
266 #pragma warning(pop)
267 #endif
268 
269 #endif /* HAZELCAST_DATA_INPUT */
270 
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:197
Provides deserialization methods for primitives types, arrays of primitive types Portable, IdentifiedDataSerializable and custom serializable types.
Definition: ObjectDataInput.h:63