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  writeInt(type);
192 
193  boost::shared_ptr<SerializerBase> serializer = serializerHolder->serializerFor(type);
194 
195  if (NULL == serializer.get()) {
196  const std::string message = "No serializer found for serializerId :"+
197  util::IOUtil::to_string(type) + ", typename :" +
198  typeid(T).name();
199  throw exception::HazelcastSerializationException("ObjectDataOutput::toData", message);
200  }
201 
202  boost::shared_ptr<StreamSerializer> streamSerializer = boost::static_pointer_cast<StreamSerializer>(
203  serializer);
204 
205  writeInternal<T>(object, streamSerializer);
206  }
207  }
208 
209  pimpl::DataOutput *getDataOutput() const;
210 
211  private:
212  pimpl::DataOutput *dataOutput;
213  pimpl::PortableContext *context;
214  pimpl::SerializerHolder *serializerHolder;
215  bool isEmpty;
216 
217  size_t position();
218 
219  void position(size_t newPos);
220 
222 
223  void operator=(const ObjectDataOutput&);
224 
232  template <typename T>
233  void writeInternal(const T *object,
234  boost::shared_ptr<StreamSerializer> &streamSerializer) {
235  int32_t typeId = streamSerializer->getHazelcastTypeId();
236  switch (typeId) {
237  case serialization::pimpl::SerializationConstants::CONSTANT_TYPE_DATA: {
238  serialization::pimpl::DataSerializer *dataSerializer =
239  static_cast<serialization::pimpl::DataSerializer *>(streamSerializer.get());
240  return dataSerializer->write(*this, object);
241  }
242  case serialization::pimpl::SerializationConstants::CONSTANT_TYPE_PORTABLE: {
243  serialization::pimpl::PortableSerializer *portableSerializer =
244  static_cast<serialization::pimpl::PortableSerializer *>(streamSerializer.get());
245 
246  return portableSerializer->write(*this, object);
247  }
248  default: {
249  streamSerializer->write(*this, object);
250  }
251  }
252  }
253  };
254 
255  template <>
256  HAZELCAST_API void ObjectDataOutput::writeInternal(const std::vector<std::string> *object,
257  boost::shared_ptr<StreamSerializer> &streamSerializer);
258  }
259  }
260 }
261 
262 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
263 #pragma warning(pop)
264 #endif
265 
266 #endif //HAZELCAST_ObjectDataOutput
267 
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:64