Hazelcast C++ Client
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/DataSerializer.h"
30 #include "hazelcast/client/serialization/pimpl/PortableSerializer.h"
31 #include "hazelcast/client/serialization/pimpl/SerializerHolder.h"
32 #include "hazelcast/client/serialization/ClassDefinition.h"
33 #include "hazelcast/client/serialization/pimpl/PortableContext.h"
34 #include "hazelcast/client/exception/HazelcastSerializationException.h"
35 #include "hazelcast/client/serialization/pimpl/SerializationConstants.h"
36 #include "hazelcast/util/IOUtil.h"
37 #include "hazelcast/client/serialization/TypeIDS.h"
38 #include "hazelcast/util/HazelcastDll.h"
39 
40 #include <boost/shared_ptr.hpp>
41 #include <vector>
42 #include <string>
43 #include <stdint.h>
44 #include <memory>
45 
46 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
47 #pragma warning(push)
48 #pragma warning(disable: 4251) //for dll export
49 #endif
50 
51 namespace hazelcast {
52  namespace client {
53  namespace serialization {
54  class Portable;
55 
56  class IdentifiedDataSerializable;
57 
58  namespace pimpl {
59  class PortableContext;
60 
61  class DataInput;
62 
63  class Data;
64  }
65 
70  class HAZELCAST_API ObjectDataInput {
71  public:
75  ObjectDataInput(pimpl::DataInput &dataInput, pimpl::SerializerHolder &serializerHolder);
76 
81  void readFully(std::vector<byte>& byteArray);
82 
87  int skipBytes(int i);
88 
93  bool readBoolean();
94 
99  byte readByte();
100 
105  int16_t readShort();
106 
111  char readChar();
112 
117  int32_t readInt();
118 
123  int64_t readLong();
124 
129  float readFloat();
130 
135  double readDouble();
136 
141  std::auto_ptr<std::string> readUTF();
142 
147  std::auto_ptr<std::vector<byte> > readByteArray();
148 
153  std::auto_ptr<std::vector<bool> > readBooleanArray();
154 
159  std::auto_ptr<std::vector<char> > readCharArray();
160 
165  std::auto_ptr<std::vector<int32_t> > readIntArray();
166 
171  std::auto_ptr<std::vector<int64_t> > readLongArray();
172 
177  std::auto_ptr<std::vector<double> > readDoubleArray();
178 
183  std::auto_ptr<std::vector<float> > readFloatArray();
184 
189  std::auto_ptr<std::vector<int16_t> > readShortArray();
190 
195  std::auto_ptr<std::vector<std::string> > readUTFArray();
196 
201  std::auto_ptr<std::vector<std::string *> > readUTFPointerArray();
202 
209  template<typename T>
210  std::auto_ptr<T> readObject() {
211  int32_t typeId = readInt();
212  return readObject<T>(typeId);
213  }
214 
215  template<typename T>
216  std::auto_ptr<T> readObject(int32_t typeId) {
217  boost::shared_ptr<SerializerBase> serializer = serializerHolder.serializerFor(typeId);
218  if (NULL == serializer.get()) {
219  const std::string message = "No serializer found for serializerId :"+
220  util::IOUtil::to_string(typeId) + ", typename :" +
221  typeid(T).name();
222  throw exception::HazelcastSerializationException("ObjectDataInput::readInternal", message);
223  }
224 
225  switch (typeId) {
226  case serialization::pimpl::SerializationConstants::CONSTANT_TYPE_DATA: {
227  serialization::pimpl::DataSerializer *dataSerializer =
228  static_cast<serialization::pimpl::DataSerializer *>(serializer.get());
229  return dataSerializer->readObject<T>(*this);
230  }
231  case serialization::pimpl::SerializationConstants::CONSTANT_TYPE_PORTABLE: {
232  serialization::pimpl::PortableSerializer *portableSerializer =
233  static_cast<serialization::pimpl::PortableSerializer *>(serializer.get());
234 
235  return portableSerializer->readObject<T>(*this);
236  }
237  default: {
238  boost::shared_ptr<StreamSerializer> streamSerializer = boost::static_pointer_cast<StreamSerializer>(serializer);
239  return std::auto_ptr<T>(getBackwardCompatiblePointer<T>(streamSerializer->read(*this),
240  (T *) NULL));
241  }
242  }
243  }
244 
249  pimpl::Data readData();
250 
254  int position();
255 
260  void position(int newPos);
261 
262  private:
263  pimpl::DataInput& dataInput;
264  pimpl::SerializerHolder& serializerHolder;
265 
266  ObjectDataInput(const ObjectDataInput&);
267 
268  void operator=(const ObjectDataInput&);
269 
270  template <typename T>
271  T *getBackwardCompatiblePointer(void *actualData, const T *typePointer) const {
272  return reinterpret_cast<T *>(actualData);
273  }
274  };
275 
280  template <>
281  HAZELCAST_API std::vector<std::string> *ObjectDataInput::getBackwardCompatiblePointer(void *actualData,
282  const std::vector<std::string> *typePointer) const;
283 
284  }
285  }
286 }
287 
288 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
289 #pragma warning(pop)
290 #endif
291 
292 #endif /* HAZELCAST_DATA_INPUT */
293 
std::auto_ptr< T > readObject()
Object can be Portable, IdentifiedDataSerializable or custom serializable for custom serialization...
Definition: ObjectDataInput.h:210
Provides deserialization methods for primitives types, arrays of primitive types Portable, IdentifiedDataSerializable and custom serializable types.
Definition: ObjectDataInput.h:70
Definition: MapEntryView.h:32