Hazelcast C++ Client
 All Classes Functions Variables Enumerations Enumerator Pages
ObjectDataOutput.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 // Created by sancar koyunlu on 8/12/13.
18 
19 #ifndef HAZELCAST_ObjectDataOutput
20 #define HAZELCAST_ObjectDataOutput
21 
22 #include "hazelcast/client/serialization/pimpl/SerializationConstants.h"
23 #include "hazelcast/client/exception/HazelcastSerializationException.h"
24 #include "hazelcast/client/serialization/pimpl/SerializerHolder.h"
25 #include "hazelcast/client/serialization/pimpl/PortableContext.h"
26 #include "hazelcast/client/serialization/Serializer.h"
27 #include "hazelcast/util/IOUtil.h"
28 #include "hazelcast/client/serialization/TypeIDS.h"
29 #include "hazelcast/client/PartitionAware.h"
30 
31 #include<stdint.h>
32 
33 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
34 #pragma warning(push)
35 #pragma warning(disable: 4251) //for dll export
36 #endif
37 
38 namespace hazelcast {
39  namespace client {
40  namespace serialization {
41  namespace pimpl {
42  class DataOutput;
43 
44  class Data;
45  }
46 
52  class HAZELCAST_API ObjectDataOutput {
53  public:
57  ObjectDataOutput(pimpl::DataOutput& dataOutput, pimpl::PortableContext& portableContext);
58 
63 
67  std::auto_ptr<std::vector<byte> > toByteArray();
68 
73  void write(const std::vector<byte>& bytes);
74 
78  void writeBoolean(bool value);
79 
83  void writeByte(int32_t value);
84 
89  void writeBytes(const byte *bytes, size_t len);
90 
94  void writeShort(int32_t value);
95 
99  void writeChar(int32_t value);
100 
104  void writeInt(int32_t value);
105 
109  void writeLong(int64_t value);
110 
114  void writeFloat(float value);
115 
119  void writeDouble(double value);
120 
124  void writeUTF(const std::string *value);
125 
129  void writeByteArray(const std::vector<byte> *value);
130 
134  void writeCharArray(const std::vector<char> *value);
135 
139  void writeBooleanArray(const std::vector<bool> *value);
140 
144  void writeShortArray(const std::vector<int16_t> *value);
145 
149  void writeIntArray(const std::vector<int32_t> *value);
150 
154  void writeLongArray(const std::vector<int64_t > *value);
155 
159  void writeFloatArray(const std::vector<float> *value);
160 
164  void writeDoubleArray(const std::vector<double> *value);
165 
169  void writeUTFArray(const std::vector<std::string *> *strings);
170 
174  void writeData(const pimpl::Data *value);
175 
181  template <typename T>
182  void writeObject(const Portable *object) {
183  if (isEmpty) return;
184 
185  if (NULL == object) {
186  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
187  } else {
188  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_PORTABLE);
189 
190  writeInt(object->getFactoryId());
191  writeInt(object->getClassId());
192 
193  context->getSerializerHolder().getPortableSerializer().write(*this->dataOutput, *object);
194  }
195  }
196 
202  template<typename T>
204  if (isEmpty) return;
205 
206  if (NULL == object) {
207  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
208  } else {
209  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_DATA);
210  context->getSerializerHolder().getDataSerializer().write(*this, *object);
211  }
212  }
213 
219  template <typename T>
220  void writeObject(const void *serializable) {
221  if (isEmpty) return;
222 
223  if (NULL == serializable) {
224  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
225  } else {
226  const T *object = static_cast<const T *>(serializable);
227  int32_t type = getHazelcastTypeId(object);
228  writeInt(type);
229 
230  boost::shared_ptr<SerializerBase> serializer = context->getSerializerHolder().serializerFor(type);
231 
232  if (NULL == serializer.get()) {
233  const std::string message = "No serializer found for serializerId :"+
234  util::IOUtil::to_string(type) + ", typename :" +
235  typeid(T).name();
236  throw exception::HazelcastSerializationException("ObjectDataOutput::toData", message);
237  }
238 
239  Serializer<T> *s = static_cast<Serializer<T> * >(serializer.get());
240 
241  s->write(*this, *object);
242  }
243  }
244 
245  template <typename T>
246  void writeObject(const byte *object) {
247  if (NULL == object) {
248  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
249  } else {
250  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_BYTE);
251  writeByte(*object);
252  }
253  }
254 
255  template <typename T>
256  void writeObject(const bool *object) {
257  if (NULL == object) {
258  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
259  } else {
260  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_BOOLEAN);
261  writeBoolean(*object);
262  }
263  }
264 
265  template <typename T>
266  void writeObject(const char *object) {
267  if (NULL == object) {
268  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
269  } else {
270  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_CHAR);
271  writeChar(*object);
272  }
273  }
274 
275  template <typename T>
276  void writeObject(const int16_t *object) {
277  if (NULL == object) {
278  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
279  } else {
280  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_SHORT);
281  writeShort(*object);
282  }
283  }
284 
285  template <typename T>
286  void writeObject(const int32_t *object) {
287  if (NULL == object) {
288  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
289  } else {
290  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_INTEGER);
291  writeInt(*object);
292  }
293  }
294 
295  template <typename T>
296  void writeObject(const int64_t *object) {
297  if (NULL == object) {
298  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
299  } else {
300  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_LONG);
301  writeLong(*object);
302  }
303  }
304 
305  template <typename T>
306  void writeObject(const float *object) {
307  if (NULL == object) {
308  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
309  } else {
310  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_FLOAT);
311  writeFloat(*object);
312  }
313  }
314 
315  template <typename T>
316  void writeObject(const double *object) {
317  if (NULL == object) {
318  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
319  } else {
320  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_DOUBLE);
321  writeDouble(*object);
322  }
323  }
324 
325  template <typename T>
326  void writeObject(const std::string *object) {
327  if (NULL == object) {
328  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
329  } else {
330  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_STRING);
331  writeUTF(object);
332  }
333  }
334  private:
335  pimpl::DataOutput *dataOutput;
336  pimpl::PortableContext *context;
337  pimpl::SerializerHolder *serializerHolder;
338  bool isEmpty;
339 
340  size_t position();
341 
342  void position(size_t newPos);
343 
344  ObjectDataOutput(const ObjectDataOutput&);
345 
346  void operator=(const ObjectDataOutput&);
347  };
348  }
349  }
350 }
351 
352 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
353 #pragma warning(pop)
354 #endif
355 
356 #endif //HAZELCAST_ObjectDataOutput
357 
Base class for custom serialization.
Definition: Serializer.h:110
void writeObject(const Portable *object)
Definition: ObjectDataOutput.h:182
Provides serialization methods for primitive types,a arrays of primitive types, Portable, IdentifiedDataSerializable and custom serializables.
Definition: ObjectDataOutput.h:52
Classes that will be used with hazelcast data structures like IMap, IQueue etc should either inherit ...
Definition: IdentifiedDataSerializable.h:42
void writeObject(const void *serializable)
Definition: ObjectDataOutput.h:220
virtual void write(ObjectDataOutput &out, const Serializable &object)=0
This method writes object to ObjectDataOutput.
Classes that will be used with hazelcast data structures like IMap, IQueue etc should either inherit ...
Definition: Portable.h:52
void writeObject(const IdentifiedDataSerializable *object)
Definition: ObjectDataOutput.h:203