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 writeShortArray(const std::vector<short> *value);
131 
135  void writeIntArray(const std::vector<int> *value);
136 
140  void writeLongArray(const std::vector<long> *value);
141 
145  void writeFloatArray(const std::vector<float> *value);
146 
150  void writeDoubleArray(const std::vector<double> *value);
151 
155  void writeUTFArray(const std::vector<std::string *> *strings);
156 
160  void writeData(const pimpl::Data *value);
161 
167  template <typename T>
168  void writeObject(const Portable *object) {
169  if (isEmpty) return;
170 
171  if (NULL == object) {
172  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
173  } else {
174  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_PORTABLE);
175 
176  writeInt(object->getFactoryId());
177  writeInt(object->getClassId());
178 
179  context->getSerializerHolder().getPortableSerializer().write(*this->dataOutput, *object);
180  }
181  }
182 
188  template<typename T>
190  if (isEmpty) return;
191 
192  if (NULL == object) {
193  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
194  } else {
195  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_DATA);
196  context->getSerializerHolder().getDataSerializer().write(*this, *object);
197  }
198  }
199 
205  template <typename T>
206  inline void writeObject(const void *serializable) {
207  if (isEmpty) return;
208 
209  if (NULL == serializable) {
210  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
211  } else {
212  const T *object = static_cast<const T *>(serializable);
213  int type = getHazelcastTypeId(object);
214  writeInt(type);
215 
216  boost::shared_ptr<SerializerBase> serializer = context->getSerializerHolder().serializerFor(type);
217 
218  if (NULL == serializer.get()) {
219  const std::string message = "No serializer found for serializerId :"+
220  util::IOUtil::to_string(type) + ", typename :" +
221  typeid(T).name();
222  throw exception::HazelcastSerializationException("ObjectDataOutput::toData", message);
223  }
224 
225  Serializer<T> *s = static_cast<Serializer<T> * >(serializer.get());
226 
227  s->write(*this, *object);
228  }
229  }
230  private:
231  pimpl::DataOutput *dataOutput;
232  pimpl::PortableContext *context;
233  pimpl::SerializerHolder *serializerHolder;
234  bool isEmpty;
235 
236  size_t position();
237 
238  void position(size_t newPos);
239 
241 
242  void operator=(const ObjectDataOutput&);
243  };
244  }
245  }
246 }
247 
248 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
249 #pragma warning(pop)
250 #endif
251 
252 #endif //HAZELCAST_ObjectDataOutput
253 
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:168
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:206
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:189