Hazelcast C++ Client
ObjectDataInput.h
1 /*
2  * Copyright (c) 2008-2019, 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/ConstantSerializers.h"
32 #include "hazelcast/client/serialization/pimpl/SerializerHolder.h"
33 #include "hazelcast/client/serialization/ClassDefinition.h"
34 #include "hazelcast/client/serialization/pimpl/PortableContext.h"
35 #include "hazelcast/client/exception/HazelcastSerializationException.h"
36 #include "hazelcast/client/serialization/pimpl/SerializationConstants.h"
37 #include "hazelcast/util/IOUtil.h"
38 #include "hazelcast/client/serialization/TypeIDS.h"
39 #include "hazelcast/util/HazelcastDll.h"
40 
41 #include <boost/shared_ptr.hpp>
42 #include <vector>
43 #include <string>
44 #include <stdint.h>
45 #include <memory>
46 
47 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
48 #pragma warning(push)
49 #pragma warning(disable: 4251) //for dll export
50 #endif
51 
52 namespace hazelcast {
53  namespace client {
54  namespace serialization {
55  class Portable;
56 
57  class IdentifiedDataSerializable;
58 
59  namespace pimpl {
60  class PortableContext;
61 
62  class DataInput;
63 
64  class Data;
65  }
66 
71  class HAZELCAST_API ObjectDataInput {
72  public:
76  ObjectDataInput(pimpl::DataInput &dataInput, pimpl::SerializerHolder &serializerHolder);
77 
82  void readFully(std::vector<byte> &byteArray);
83 
88  int skipBytes(int i);
89 
94  bool readBoolean();
95 
100  byte readByte();
101 
106  int16_t readShort();
107 
112  char readChar();
113 
118  int32_t readInt();
119 
124  int64_t readLong();
125 
130  float readFloat();
131 
136  double readDouble();
137 
142  std::auto_ptr<std::string> readUTF();
143 
148  std::auto_ptr<std::vector<byte> > readByteArray();
149 
154  std::auto_ptr<std::vector<bool> > readBooleanArray();
155 
160  std::auto_ptr<std::vector<char> > readCharArray();
161 
166  std::auto_ptr<std::vector<int32_t> > readIntArray();
167 
172  std::auto_ptr<std::vector<int64_t> > readLongArray();
173 
178  std::auto_ptr<std::vector<double> > readDoubleArray();
179 
184  std::auto_ptr<std::vector<float> > readFloatArray();
185 
190  std::auto_ptr<std::vector<int16_t> > readShortArray();
191 
196  std::auto_ptr<std::vector<std::string> > readUTFArray();
197 
202  std::auto_ptr<std::vector<std::string *> > readUTFPointerArray();
203 
210  template<typename T>
211  std::auto_ptr<T> readObject() {
212  int32_t typeId = readInt();
213  return readObject<T>(typeId);
214  }
215 
216  template<typename T>
217  std::auto_ptr<T> readObject(int32_t typeId) {
218  boost::shared_ptr<SerializerBase> serializer = serializerHolder.serializerFor(typeId);
219  if (NULL == serializer.get()) {
220  const std::string message = "No serializer found for serializerId :" +
221  util::IOUtil::to_string(typeId) + ", typename :" +
222  typeid(T).name();
223  throw exception::HazelcastSerializationException("ObjectDataInput::readInternal", message);
224  }
225 
226  return readObjectInternal<T>(typeId, serializer);
227  }
228 
233  pimpl::Data readData();
234 
238  int position();
239 
244  void position(int newPos);
245 
246  private:
247  pimpl::DataInput &dataInput;
248  pimpl::SerializerHolder &serializerHolder;
249 
250  ObjectDataInput(const ObjectDataInput &);
251 
252  void operator=(const ObjectDataInput &);
253 
254  template<typename T>
255  T *getBackwardCompatiblePointer(void *actualData, const T *typePointer) const {
256  return reinterpret_cast<T *>(actualData);
257  }
258 
259  template<typename T>
260  std::auto_ptr<T>
261  readObjectInternal(int32_t typeId, const boost::shared_ptr<SerializerBase> &serializer) {
262  switch (typeId) {
263  case serialization::pimpl::SerializationConstants::CONSTANT_TYPE_DATA: {
264  serialization::pimpl::DataSerializer *dataSerializer =
265  static_cast<serialization::pimpl::DataSerializer *>(serializer.get());
266  return dataSerializer->readObject<T>(*this);
267  }
268  case serialization::pimpl::SerializationConstants::CONSTANT_TYPE_PORTABLE: {
269  serialization::pimpl::PortableSerializer *portableSerializer =
270  static_cast<serialization::pimpl::PortableSerializer *>(serializer.get());
271 
272  return portableSerializer->readObject<T>(*this);
273  }
274  default: {
275  boost::shared_ptr<StreamSerializer> streamSerializer = boost::static_pointer_cast<StreamSerializer>(
276  serializer);
277  return std::auto_ptr<T>(getBackwardCompatiblePointer<T>(streamSerializer->read(*this),
278  (T *) NULL));
279  }
280  }
281  }
282  };
283 
288  template<>
289  HAZELCAST_API std::vector<std::string> *ObjectDataInput::getBackwardCompatiblePointer(void *actualData,
290  const std::vector<std::string> *typePointer) const;
291 
292 
297  template<>
298  std::auto_ptr<HazelcastJsonValue>
299  HAZELCAST_API
300  ObjectDataInput::readObjectInternal(int32_t typeId, const boost::shared_ptr<SerializerBase> &serializer);
301  }
302  }
303 }
304 
305 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
306 #pragma warning(pop)
307 #endif
308 
309 #endif /* HAZELCAST_DATA_INPUT */
310 
std::auto_ptr< T > readObject()
Object can be Portable, IdentifiedDataSerializable or custom serializable for custom serialization...
Definition: ObjectDataInput.h:211
Provides deserialization methods for primitives types, arrays of primitive types Portable, IdentifiedDataSerializable and custom serializable types.
Definition: ObjectDataInput.h:71
PN (Positive-Negative) CRDT counter.
Definition: MapEntryView.h:32