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 
20 
21 
22 
23 #ifndef HAZELCAST_ObjectDataOutput
24 #define HAZELCAST_ObjectDataOutput
25 
26 #include <list>
27 #include "hazelcast/client/serialization/pimpl/SerializationConstants.h"
28 #include "hazelcast/client/exception/HazelcastSerializationException.h"
29 #include "hazelcast/client/serialization/pimpl/SerializerHolder.h"
30 #include "hazelcast/client/serialization/pimpl/PortableContext.h"
31 #include "hazelcast/client/serialization/Serializer.h"
32 #include "hazelcast/util/IOUtil.h"
33 
34 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
35 #pragma warning(push)
36 #pragma warning(disable: 4251) //for dll export
37 #endif
38 
39 namespace hazelcast {
40  namespace client {
41  namespace serialization {
42  namespace pimpl {
43  class DataOutput;
44 
45  class Data;
46  }
47 
53  class HAZELCAST_API ObjectDataOutput {
54  public:
58  ObjectDataOutput(pimpl::DataOutput& dataOutput, pimpl::PortableContext& portableContext);
59 
64 
68  std::auto_ptr<std::vector<byte> > toByteArray();
69 
74  void write(const std::vector<byte>& bytes);
75 
79  void writeBoolean(bool value);
80 
84  void writeByte(int value);
85 
89  void writeShort(int value);
90 
94  void writeChar(int value);
95 
99  void writeInt(int value);
100 
104  void writeLong(long value);
105 
109  void writeFloat(float value);
110 
114  void writeDouble(double value);
115 
119  void writeUTF(const std::string *value);
120 
124  void writeByteArray(const std::vector<byte> *value);
125 
129  void writeCharArray(const std::vector<char> *value);
130 
134  void writeShortArray(const std::vector<short> *value);
135 
139  void writeIntArray(const std::vector<int> *value);
140 
144  void writeLongArray(const std::vector<long> *value);
145 
149  void writeFloatArray(const std::vector<float> *value);
150 
154  void writeDoubleArray(const std::vector<double> *value);
155 
159  void writeUTFArray(const std::vector<std::string *> *strings);
160 
164  void writeData(const pimpl::Data *value);
165 
171  template <typename T>
172  void writeObject(const Portable *object) {
173  if (isEmpty) return;
174 
175  if (NULL == object) {
176  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
177  } else {
178  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_PORTABLE);
179 
180  writeInt(object->getFactoryId());
181  writeInt(object->getClassId());
182 
183  context->getSerializerHolder().getPortableSerializer().write(*this->dataOutput, *object);
184  }
185  }
186 
192  template<typename T>
194  if (isEmpty) return;
195 
196  if (NULL == object) {
197  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
198  } else {
199  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_DATA);
200  context->getSerializerHolder().getDataSerializer().write(*this, *object);
201  }
202  }
203 
209  template <typename T>
210  inline void writeObject(const void *serializable) {
211  if (isEmpty) return;
212 
213  if (NULL == serializable) {
214  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
215  } else {
216  const T *object = static_cast<const T *>(serializable);
217  int type = object->getTypeId();
218  writeInt(type);
219 
220  boost::shared_ptr<SerializerBase> serializer = context->getSerializerHolder().serializerFor(type);
221 
222  if (NULL == serializer.get()) {
223  const std::string message = "No serializer found for serializerId :"+
224  util::IOUtil::to_string(type) + ", typename :" +
225  typeid(T).name();
226  throw exception::HazelcastSerializationException("ObjectDataOutput::toData", message);
227  }
228 
229  Serializer<T> *s = static_cast<Serializer<T> * >(serializer.get());
230 
231  s->write(*this, *object);
232  }
233  }
234  private:
235  pimpl::DataOutput *dataOutput;
236  pimpl::PortableContext *context;
237  pimpl::SerializerHolder *serializerHolder;
238  bool isEmpty;
239 
240  size_t position();
241 
242  void position(size_t newPos);
243 
245 
246  void operator=(const ObjectDataOutput&);
247  };
248  }
249  }
250 }
251 
252 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
253 #pragma warning(pop)
254 #endif
255 
256 #endif //HAZELCAST_ObjectDataOutput
257 
Raised when an error occur during serialization or deserialization in Hazelcast.
Definition: HazelcastSerializationException.h:33
Base class for custom serialization.
Definition: Serializer.h:102
void writeObject(const Portable *object)
Definition: ObjectDataOutput.h:172
Provides serialization methods for primitive types,a arrays of primitive types, Portable, IdentifiedDataSerializable and custom serializables.
Definition: ObjectDataOutput.h:53
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:210
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:193