Hazelcast C++ Client
ObjectDataOutput.h
1 /*
2  * Copyright (c) 2008-2019, 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 
32 #include<stdint.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::SerializerHolder *serializerHolder);
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(int32_t value);
85 
90  void writeBytes(const byte *bytes, size_t len);
91 
95  void writeShort(int32_t value);
96 
100  void writeChar(int32_t value);
101 
105  void writeInt(int32_t value);
106 
110  void writeLong(int64_t value);
111 
115  void writeFloat(float value);
116 
120  void writeDouble(double value);
121 
125  void writeUTF(const std::string *value);
126 
130  void writeByteArray(const std::vector<byte> *value);
131 
135  void writeCharArray(const std::vector<char> *value);
136 
140  void writeBooleanArray(const std::vector<bool> *value);
141 
145  void writeShortArray(const std::vector<int16_t> *value);
146 
150  void writeIntArray(const std::vector<int32_t> *value);
151 
155  void writeLongArray(const std::vector<int64_t > *value);
156 
160  void writeFloatArray(const std::vector<float> *value);
161 
165  void writeDoubleArray(const std::vector<double> *value);
166 
170  void writeUTFArray(const std::vector<std::string *> *strings);
171 
175  void writeData(const pimpl::Data *value);
176 
182  template <typename T>
183  void writeObject(const T *object) {
184  if (isEmpty) return;
185 
186  if (NULL == object) {
187  writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
188  } else {
189  int32_t type = getHazelcastTypeId(object);
190 
191  boost::shared_ptr<SerializerBase> serializer = serializerHolder->serializerFor(type);
192 
193  if (NULL == serializer.get()) {
194  const std::string message = "No serializer found for serializerId :"+
195  util::IOUtil::to_string(type) + ", typename :" +
196  typeid(T).name();
197  throw exception::HazelcastSerializationException("ObjectDataOutput::toData", message);
198  }
199 
200  boost::shared_ptr<StreamSerializer> streamSerializer = boost::static_pointer_cast<StreamSerializer>(
201  serializer);
202 
203  writeInt(streamSerializer->getHazelcastTypeId());
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:183
Provides serialization methods for primitive types,a arrays of primitive types, Portable, IdentifiedDataSerializable and custom serializables.
Definition: ObjectDataOutput.h:53
PN (Positive-Negative) CRDT counter.
Definition: MapEntryView.h:32
Implement this interface and register to the SerializationConfig.
Definition: Serializer.h:71