Hazelcast C++ Client
ObjectDataOutput.h
1 /*
2  * Copyright (c) 2008-2017, 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/pimpl/DataSerializer.h"
27 #include "hazelcast/client/serialization/pimpl/PortableSerializer.h"
28 #include "hazelcast/client/serialization/Serializer.h"
29 #include "hazelcast/util/IOUtil.h"
30 #include "hazelcast/client/serialization/TypeIDS.h"
31 #include "hazelcast/client/PartitionAware.h"
32 
33 #include<stdint.h>
34 
35 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
36 #pragma warning(push)
37 #pragma warning(disable: 4251) //for dll export
38 #endif
39 
40 namespace hazelcast {
41  namespace client {
42  namespace serialization {
43  namespace pimpl {
44  class DataOutput;
45 
46  class Data;
47  }
48 
54  class HAZELCAST_API ObjectDataOutput {
55  public:
59  ObjectDataOutput(pimpl::DataOutput& dataOutput, pimpl::SerializerHolder *serializerHolder);
60 
65 
69  std::auto_ptr<std::vector<byte> > toByteArray();
70 
75  void write(const std::vector<byte>& bytes);
76 
80  void writeBoolean(bool value);
81 
85  void writeByte(int32_t value);
86 
91  void writeBytes(const byte *bytes, size_t len);
92 
96  void writeShort(int32_t value);
97 
101  void writeChar(int32_t value);
102 
106  void writeInt(int32_t value);
107 
111  void writeLong(int64_t value);
112 
116  void writeFloat(float value);
117 
121  void writeDouble(double value);
122 
126  void writeUTF(const std::string *value);
127 
131  void writeByteArray(const std::vector<byte> *value);
132 
136  void writeCharArray(const std::vector<char> *value);
137 
141  void writeBooleanArray(const std::vector<bool> *value);
142 
146  void writeShortArray(const std::vector<int16_t> *value);
147 
151  void writeIntArray(const std::vector<int32_t> *value);
152 
156  void writeLongArray(const std::vector<int64_t > *value);
157 
161  void writeFloatArray(const std::vector<float> *value);
162 
166  void writeDoubleArray(const std::vector<double> *value);
167 
171  void writeUTFArray(const std::vector<std::string *> *strings);
172 
176  void writeData(const pimpl::Data *value);
177 
183  template <typename T>
184  void writeObject(const T *object) {
185  if (isEmpty) return;
186 
187  if (NULL == object) {
188  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
189  } else {
190  int32_t type = getHazelcastTypeId(object);
191 
192  boost::shared_ptr<SerializerBase> serializer = serializerHolder->serializerFor(type);
193 
194  if (NULL == serializer.get()) {
195  const std::string message = "No serializer found for serializerId :"+
196  util::IOUtil::to_string(type) + ", typename :" +
197  typeid(T).name();
198  throw exception::HazelcastSerializationException("ObjectDataOutput::toData", message);
199  }
200 
201  boost::shared_ptr<StreamSerializer> streamSerializer = boost::static_pointer_cast<StreamSerializer>(
202  serializer);
203 
204  writeInt(streamSerializer->getHazelcastTypeId());
205 
206  writeInternal<T>(object, streamSerializer);
207  }
208  }
209 
210  pimpl::DataOutput *getDataOutput() const;
211 
212  private:
213  pimpl::DataOutput *dataOutput;
214  pimpl::PortableContext *context;
215  pimpl::SerializerHolder *serializerHolder;
216  bool isEmpty;
217 
218  size_t position();
219 
220  void position(size_t newPos);
221 
223 
224  void operator=(const ObjectDataOutput&);
225 
233  template <typename T>
234  void writeInternal(const T *object,
235  boost::shared_ptr<StreamSerializer> &streamSerializer) {
236  int32_t typeId = streamSerializer->getHazelcastTypeId();
237  switch (typeId) {
238  case serialization::pimpl::SerializationConstants::CONSTANT_TYPE_DATA: {
239  serialization::pimpl::DataSerializer *dataSerializer =
240  static_cast<serialization::pimpl::DataSerializer *>(streamSerializer.get());
241  return dataSerializer->write(*this, object);
242  }
243  case serialization::pimpl::SerializationConstants::CONSTANT_TYPE_PORTABLE: {
244  serialization::pimpl::PortableSerializer *portableSerializer =
245  static_cast<serialization::pimpl::PortableSerializer *>(streamSerializer.get());
246 
247  return portableSerializer->write(*this, object);
248  }
249  default: {
250  streamSerializer->write(*this, object);
251  }
252  }
253  }
254  };
255 
256  template <>
257  HAZELCAST_API void ObjectDataOutput::writeInternal(const std::vector<std::string> *object,
258  boost::shared_ptr<StreamSerializer> &streamSerializer);
259  }
260  }
261 }
262 
263 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
264 #pragma warning(pop)
265 #endif
266 
267 #endif //HAZELCAST_ObjectDataOutput
268 
void writeObject(const T *object)
Definition: ObjectDataOutput.h:184
Provides serialization methods for primitive types,a arrays of primitive types, Portable, IdentifiedDataSerializable and custom serializables.
Definition: ObjectDataOutput.h:54
Definition: MapEntryView.h:32
Implement this interface and register to the SerializationConfig.
Definition: Serializer.h:71