Hazelcast C++ Client
 All Classes Functions Variables Enumerations Pages
ObjectDataOutput.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 // 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 
30 #include<stdint.h>
31 
32 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
33 #pragma warning(push)
34 #pragma warning(disable: 4251) //for dll export
35 #endif
36 
37 namespace hazelcast {
38  namespace client {
39  namespace serialization {
40  namespace pimpl {
41  class DataOutput;
42 
43  class Data;
44  }
45 
51  class HAZELCAST_API ObjectDataOutput {
52  public:
56  ObjectDataOutput(pimpl::DataOutput& dataOutput, pimpl::PortableContext& portableContext);
57 
62 
66  std::auto_ptr<std::vector<byte> > toByteArray();
67 
72  void write(const std::vector<byte>& bytes);
73 
77  void writeBoolean(bool value);
78 
82  void writeByte(int value);
83 
88  void writeBytes(const byte *bytes, unsigned int len);
89 
93  void writeShort(int value);
94 
98  void writeChar(int value);
99 
103  void writeInt(int value);
104 
108  void writeLong(int64_t value);
109 
113  void writeFloat(float value);
114 
118  void writeDouble(double value);
119 
123  void writeUTF(const std::string *value);
124 
128  void writeByteArray(const std::vector<byte> *value);
129 
133  void writeCharArray(const std::vector<char> *value);
134 
138  void writeBooleanArray(const std::vector<bool> *value);
139 
143  void writeShortArray(const std::vector<short> *value);
144 
148  void writeIntArray(const std::vector<int> *value);
149 
153  void writeLongArray(const std::vector<long> *value);
154 
158  void writeFloatArray(const std::vector<float> *value);
159 
163  void writeDoubleArray(const std::vector<double> *value);
164 
168  void writeUTFArray(const std::vector<std::string *> *strings);
169 
173  void writeData(const pimpl::Data *value);
174 
180  template <typename T>
181  void writeObject(const Portable *object) {
182  if (isEmpty) return;
183 
184  if (NULL == object) {
185  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
186  } else {
187  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_PORTABLE);
188 
189  writeInt(object->getFactoryId());
190  writeInt(object->getClassId());
191 
192  context->getSerializerHolder().getPortableSerializer().write(*this->dataOutput, *object);
193  }
194  }
195 
201  template<typename T>
203  if (isEmpty) return;
204 
205  if (NULL == object) {
206  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
207  } else {
208  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_DATA);
209  context->getSerializerHolder().getDataSerializer().write(*this, *object);
210  }
211  }
212 
218  template <typename T>
219  void writeObject(const void *serializable) {
220  if (isEmpty) return;
221 
222  if (NULL == serializable) {
223  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
224  } else {
225  const T *object = static_cast<const T *>(serializable);
226  int type = getHazelcastTypeId(object);
227  writeInt(type);
228 
229  boost::shared_ptr<SerializerBase> serializer = context->getSerializerHolder().serializerFor(type);
230 
231  if (NULL == serializer.get()) {
232  const std::string message = "No serializer found for serializerId :"+
233  util::IOUtil::to_string(type) + ", typename :" +
234  typeid(T).name();
235  throw exception::HazelcastSerializationException("ObjectDataOutput::toData", message);
236  }
237 
238  Serializer<T> *s = static_cast<Serializer<T> * >(serializer.get());
239 
240  s->write(*this, *object);
241  }
242  }
243 
244  template <typename T>
245  void writeObject(const byte *object) {
246  if (NULL == object) {
247  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
248  } else {
249  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_BYTE);
250  writeByte(*object);
251  }
252  }
253 
254  template <typename T>
255  void writeObject(const bool *object) {
256  if (NULL == object) {
257  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
258  } else {
259  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_BOOLEAN);
260  writeBoolean(*object);
261  }
262  }
263 
264  template <typename T>
265  void writeObject(const char *object) {
266  if (NULL == object) {
267  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
268  } else {
269  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_CHAR);
270  writeChar(*object);
271  }
272  }
273 
274  template <typename T>
275  void writeObject(const short *object) {
276  if (NULL == object) {
277  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
278  } else {
279  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_SHORT);
280  writeShort(*object);
281  }
282  }
283 
284  template <typename T>
285  void writeObject(const int *object) {
286  if (NULL == object) {
287  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
288  } else {
289  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_INTEGER);
290  writeInt(*object);
291  }
292  }
293 
294  template <typename T>
295  void writeObject(const long *object) {
296  if (NULL == object) {
297  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
298  } else {
299  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_LONG);
300  writeLong(*object);
301  }
302  }
303 
304  template <typename T>
305  void writeObject(const float *object) {
306  if (NULL == object) {
307  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
308  } else {
309  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_FLOAT);
310  writeFloat(*object);
311  }
312  }
313 
314  template <typename T>
315  void writeObject(const double *object) {
316  if (NULL == object) {
317  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
318  } else {
319  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_DOUBLE);
320  writeDouble(*object);
321  }
322  }
323 
324  template <typename T>
325  void writeObject(const std::string *object) {
326  if (NULL == object) {
327  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
328  } else {
329  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_STRING);
330  writeUTF(object);
331  }
332  }
333  private:
334  pimpl::DataOutput *dataOutput;
335  pimpl::PortableContext *context;
336  pimpl::SerializerHolder *serializerHolder;
337  bool isEmpty;
338 
339  size_t position();
340 
341  void position(size_t newPos);
342 
343  ObjectDataOutput(const ObjectDataOutput&);
344 
345  void operator=(const ObjectDataOutput&);
346  };
347  }
348  }
349 }
350 
351 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
352 #pragma warning(pop)
353 #endif
354 
355 #endif //HAZELCAST_ObjectDataOutput
356 
Base class for custom serialization.
Definition: Serializer.h:108
void writeObject(const Portable *object)
Definition: ObjectDataOutput.h:181
Provides serialization methods for primitive types,a arrays of primitive types, Portable, IdentifiedDataSerializable and custom serializables.
Definition: ObjectDataOutput.h:51
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:219
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:202