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 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
31 #pragma warning(push)
32 #pragma warning(disable: 4251) //for dll export
33 #endif
34 
35 namespace hazelcast {
36  namespace client {
37  namespace serialization {
38  namespace pimpl {
39  class DataOutput;
40 
41  class Data;
42  }
43 
49  class HAZELCAST_API ObjectDataOutput {
50  public:
54  ObjectDataOutput(pimpl::DataOutput& dataOutput, pimpl::PortableContext& portableContext);
55 
60 
64  std::auto_ptr<std::vector<byte> > toByteArray();
65 
70  void write(const std::vector<byte>& bytes);
71 
75  void writeBoolean(bool value);
76 
80  void writeByte(int value);
81 
85  void writeShort(int value);
86 
90  void writeChar(int value);
91 
95  void writeInt(int value);
96 
100  void writeLong(long value);
101 
105  void writeFloat(float value);
106 
110  void writeDouble(double value);
111 
115  void writeUTF(const std::string *value);
116 
120  void writeByteArray(const std::vector<byte> *value);
121 
125  void writeCharArray(const std::vector<char> *value);
126 
130  void writeBooleanArray(const std::vector<bool> *value);
131 
135  void writeShortArray(const std::vector<short> *value);
136 
140  void writeIntArray(const std::vector<int> *value);
141 
145  void writeLongArray(const std::vector<long> *value);
146 
150  void writeFloatArray(const std::vector<float> *value);
151 
155  void writeDoubleArray(const std::vector<double> *value);
156 
160  void writeUTFArray(const std::vector<std::string *> *strings);
161 
165  void writeData(const pimpl::Data *value);
166 
172  template <typename T>
173  void writeObject(const Portable *object) {
174  if (isEmpty) return;
175 
176  if (NULL == object) {
177  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
178  } else {
179  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_PORTABLE);
180 
181  writeInt(object->getFactoryId());
182  writeInt(object->getClassId());
183 
184  context->getSerializerHolder().getPortableSerializer().write(*this->dataOutput, *object);
185  }
186  }
187 
193  template<typename T>
195  if (isEmpty) return;
196 
197  if (NULL == object) {
198  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
199  } else {
200  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_DATA);
201  context->getSerializerHolder().getDataSerializer().write(*this, *object);
202  }
203  }
204 
210  template <typename T>
211  void writeObject(const void *serializable) {
212  if (isEmpty) return;
213 
214  if (NULL == serializable) {
215  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
216  } else {
217  const T *object = static_cast<const T *>(serializable);
218  int type = getHazelcastTypeId(object);
219  writeInt(type);
220 
221  boost::shared_ptr<SerializerBase> serializer = context->getSerializerHolder().serializerFor(type);
222 
223  if (NULL == serializer.get()) {
224  const std::string message = "No serializer found for serializerId :"+
225  util::IOUtil::to_string(type) + ", typename :" +
226  typeid(T).name();
227  throw exception::HazelcastSerializationException("ObjectDataOutput::toData", message);
228  }
229 
230  Serializer<T> *s = static_cast<Serializer<T> * >(serializer.get());
231 
232  s->write(*this, *object);
233  }
234  }
235 
236  template <typename T>
237  void writeObject(const byte *object) {
238  if (NULL == object) {
239  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
240  } else {
241  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_BYTE);
242  writeByte(*object);
243  }
244  }
245 
246  template <typename T>
247  void writeObject(const bool *object) {
248  if (NULL == object) {
249  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
250  } else {
251  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_BOOLEAN);
252  writeBoolean(*object);
253  }
254  }
255 
256  template <typename T>
257  void writeObject(const char *object) {
258  if (NULL == object) {
259  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
260  } else {
261  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_CHAR);
262  writeChar(*object);
263  }
264  }
265 
266  template <typename T>
267  void writeObject(const short *object) {
268  if (NULL == object) {
269  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
270  } else {
271  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_SHORT);
272  writeShort(*object);
273  }
274  }
275 
276  template <typename T>
277  void writeObject(const int *object) {
278  if (NULL == object) {
279  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
280  } else {
281  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_INTEGER);
282  writeInt(*object);
283  }
284  }
285 
286  template <typename T>
287  void writeObject(const long *object) {
288  if (NULL == object) {
289  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
290  } else {
291  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_LONG);
292  writeLong(*object);
293  }
294  }
295 
296  template <typename T>
297  void writeObject(const float *object) {
298  if (NULL == object) {
299  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
300  } else {
301  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_FLOAT);
302  writeFloat(*object);
303  }
304  }
305 
306  template <typename T>
307  void writeObject(const double *object) {
308  if (NULL == object) {
309  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
310  } else {
311  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_DOUBLE);
312  writeDouble(*object);
313  }
314  }
315 
316  template <typename T>
317  void writeObject(const std::string *object) {
318  if (NULL == object) {
319  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
320  } else {
321  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_STRING);
322  writeUTF(object);
323  }
324  }
325  private:
326  pimpl::DataOutput *dataOutput;
327  pimpl::PortableContext *context;
328  pimpl::SerializerHolder *serializerHolder;
329  bool isEmpty;
330 
331  size_t position();
332 
333  void position(size_t newPos);
334 
335  ObjectDataOutput(const ObjectDataOutput&);
336 
337  void operator=(const ObjectDataOutput&);
338  };
339  }
340  }
341 }
342 
343 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
344 #pragma warning(pop)
345 #endif
346 
347 #endif //HAZELCAST_ObjectDataOutput
348 
Raised when an error occur during serialization or deserialization in Hazelcast.
Definition: HazelcastSerializationException.h:33
Base class for custom serialization.
Definition: Serializer.h:108
void writeObject(const Portable *object)
Definition: ObjectDataOutput.h:173
Provides serialization methods for primitive types,a arrays of primitive types, Portable, IdentifiedDataSerializable and custom serializables.
Definition: ObjectDataOutput.h:49
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:211
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:194