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 
29 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
30 #pragma warning(push)
31 #pragma warning(disable: 4251) //for dll export
32 #endif
33 
34 namespace hazelcast {
35  namespace client {
36  namespace serialization {
37  namespace pimpl {
38  class DataOutput;
39 
40  class Data;
41  }
42 
48  class HAZELCAST_API ObjectDataOutput {
49  public:
53  ObjectDataOutput(pimpl::DataOutput& dataOutput, pimpl::PortableContext& portableContext);
54 
59 
63  std::auto_ptr<std::vector<byte> > toByteArray();
64 
69  void write(const std::vector<byte>& bytes);
70 
74  void writeBoolean(bool value);
75 
79  void writeByte(int value);
80 
84  void writeShort(int value);
85 
89  void writeChar(int value);
90 
94  void writeInt(int value);
95 
99  void writeLong(long value);
100 
104  void writeFloat(float value);
105 
109  void writeDouble(double value);
110 
114  void writeUTF(const std::string *value);
115 
119  void writeByteArray(const std::vector<byte> *value);
120 
124  void writeCharArray(const std::vector<char> *value);
125 
129  void writeShortArray(const std::vector<short> *value);
130 
134  void writeIntArray(const std::vector<int> *value);
135 
139  void writeLongArray(const std::vector<long> *value);
140 
144  void writeFloatArray(const std::vector<float> *value);
145 
149  void writeDoubleArray(const std::vector<double> *value);
150 
154  void writeUTFArray(const std::vector<std::string *> *strings);
155 
159  void writeData(const pimpl::Data *value);
160 
166  template <typename T>
167  void writeObject(const Portable *object) {
168  if (isEmpty) return;
169 
170  if (NULL == object) {
171  writeInt(pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_NULL);
172  } else {
173  writeInt(pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_PORTABLE);
174 
175  writeInt(object->getFactoryId());
176  writeInt(object->getClassId());
177 
178  context->getSerializerHolder().getPortableSerializer().write(*this->dataOutput, *object);
179  }
180  }
181 
187  template<typename T>
189  if (isEmpty) return;
190 
191  if (NULL == object) {
192  writeInt(pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_NULL);
193  } else {
194  writeInt(pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_DATA);
195  context->getSerializerHolder().getDataSerializer().write(*this, *object);
196  }
197  }
198 
204  template <typename T>
205  inline void writeObject(const void *serializable) {
206  if (isEmpty) return;
207 
208  if (NULL == serializable) {
209  writeInt(pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_NULL);
210  } else {
211  const T *object = static_cast<const T *>(serializable);
212  int type = object->getTypeId();
213  writeInt(type);
214 
215  boost::shared_ptr<SerializerBase> serializer = context->getSerializerHolder().serializerFor(type);
216 
217  if (NULL == serializer.get()) {
218  const std::string message = "No serializer found for serializerId :"+
219  util::IOUtil::to_string(type) + ", typename :" +
220  typeid(T).name();
221  throw exception::HazelcastSerializationException("ObjectDataOutput::toData", message);
222  }
223 
224  Serializer<T> *s = static_cast<Serializer<T> * >(serializer.get());
225 
226  s->write(*this, *object);
227  }
228  }
229  private:
230  pimpl::DataOutput *dataOutput;
231  pimpl::PortableContext *context;
232  pimpl::SerializerHolder *serializerHolder;
233  bool isEmpty;
234 
235  size_t position();
236 
237  void position(size_t newPos);
238 
240 
241  void operator=(const ObjectDataOutput&);
242  };
243  }
244  }
245 }
246 
247 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
248 #pragma warning(pop)
249 #endif
250 
251 #endif //HAZELCAST_ObjectDataOutput
252 
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:167
Provides serialization methods for primitive types,a arrays of primitive types, Portable, IdentifiedDataSerializable and custom serializables.
Definition: ObjectDataOutput.h:48
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:205
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:188