VMware Tanzu GemFire Native C++ Reference  10.1.5
Serializer.hpp
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #ifndef GEODE_SERIALIZER_H_
21 #define GEODE_SERIALIZER_H_
22 
23 #include <memory>
24 #include <type_traits>
25 #include <unordered_map>
26 #include <unordered_set>
27 #include <vector>
28 
29 #include "DataInput.hpp"
30 #include "DataOutput.hpp"
31 #include "internal/geode_globals.hpp"
32 
33 namespace apache {
34 namespace geode {
35 namespace client {
36 namespace serializer {
37 
38 // Read and write methods for various types
39 
40 inline void writeObject(apache::geode::client::DataOutput& output,
41  uint8_t value) {
42  output.write(value);
43 }
44 
45 inline void readObject(apache::geode::client::DataInput& input,
46  uint8_t& value) {
47  value = input.read();
48 }
49 
50 inline void writeObject(apache::geode::client::DataOutput& output,
51  int8_t value) {
52  output.write(value);
53 }
54 
55 inline void readObject(apache::geode::client::DataInput& input, int8_t& value) {
56  value = input.read();
57 }
58 
59 inline void writeObject(apache::geode::client::DataOutput& output,
60  const uint8_t* bytes, int32_t len) {
61  output.writeBytes(bytes, len);
62 }
63 
64 inline void readObject(apache::geode::client::DataInput& input, uint8_t*& bytes,
65  int32_t& len) {
66  input.readBytes(&bytes, &len);
67 }
68 
69 inline void writeObject(apache::geode::client::DataOutput& output,
70  const int8_t* bytes, int32_t len) {
71  output.writeBytes(bytes, len);
72 }
73 
74 inline void readObject(apache::geode::client::DataInput& input, int8_t*& bytes,
75  int32_t& len) {
76  input.readBytes(&bytes, &len);
77 }
78 
79 inline void writeObject(apache::geode::client::DataOutput& output,
80  int16_t value) {
81  output.writeInt(value);
82 }
83 
84 inline void readObject(apache::geode::client::DataInput& input,
85  int16_t& value) {
86  value = input.readInt16();
87 }
88 
89 inline void writeObject(apache::geode::client::DataOutput& output,
90  int32_t value) {
91  output.writeInt(value);
92 }
93 
94 inline void readObject(apache::geode::client::DataInput& input,
95  int32_t& value) {
96  value = input.readInt32();
97 }
98 
99 inline void writeObject(apache::geode::client::DataOutput& output,
100  int64_t value) {
101  output.writeInt(value);
102 }
103 
104 inline void readObject(apache::geode::client::DataInput& input,
105  int64_t& value) {
106  value = input.readInt64();
107 }
108 
109 inline void writeObject(apache::geode::client::DataOutput& output,
110  uint16_t value) {
111  output.writeInt(value);
112 }
113 
114 inline void readObject(apache::geode::client::DataInput& input,
115  uint16_t& value) {
116  value = input.readInt16();
117 }
118 
119 inline void writeObject(apache::geode::client::DataOutput& output,
120  uint32_t value) {
121  output.writeInt(value);
122 }
123 
124 inline void readObject(apache::geode::client::DataInput& input,
125  uint32_t& value) {
126  value = input.readInt32();
127 }
128 
129 inline void writeObject(apache::geode::client::DataOutput& output,
130  uint64_t value) {
131  output.writeInt(value);
132 }
133 
134 inline void readObject(apache::geode::client::DataInput& input,
135  uint64_t& value) {
136  value = input.readInt64();
137 }
138 
139 inline void writeObject(apache::geode::client::DataOutput& output, bool value) {
140  output.writeBoolean(value);
141 }
142 
143 inline void readObject(apache::geode::client::DataInput& input, bool& value) {
144  value = input.readBoolean();
145 }
146 
147 inline void readObject(apache::geode::client::DataInput& input,
148  std::vector<bool>::reference value) {
149  value = input.readBoolean();
150 }
151 
152 inline void writeObject(apache::geode::client::DataOutput& output,
153  double value) {
154  output.writeDouble(value);
155 }
156 
157 inline void readObject(apache::geode::client::DataInput& input, double& value) {
158  value = input.readDouble();
159 }
160 
161 inline void writeObject(apache::geode::client::DataOutput& output,
162  float value) {
163  output.writeFloat(value);
164 }
165 
166 inline void readObject(apache::geode::client::DataInput& input, float& value) {
167  value = input.readFloat();
168 }
169 
170 inline void writeObject(apache::geode::client::DataOutput& output,
171  char16_t value) {
172  output.writeInt(static_cast<int16_t>(value));
173 }
174 
175 inline void readObject(apache::geode::client::DataInput& input,
176  char16_t& value) {
177  value = input.readInt16();
178 }
179 
180 inline void readObject(apache::geode::client::DataInput& input,
181  std::string& value) {
182  value = input.readString();
183 }
184 
185 inline void writeObject(apache::geode::client::DataOutput& output,
186  const std::string& value) {
187  output.writeString(value);
188 }
189 
190 template <typename TObj,
191  typename std::enable_if<std::is_base_of<Serializable, TObj>::value,
192  Serializable>::type* = nullptr>
193 inline void writeObject(apache::geode::client::DataOutput& output,
194  const std::shared_ptr<TObj>& value) {
195  output.writeObject(value);
196 }
197 
198 template <typename TObj,
199  typename std::enable_if<std::is_base_of<Serializable, TObj>::value,
200  Serializable>::type* = nullptr>
201 inline void readObject(apache::geode::client::DataInput& input,
202  std::shared_ptr<TObj>& value) {
203  value = std::dynamic_pointer_cast<TObj>(input.readObject());
204 }
205 
206 // For arrays
207 
208 template <typename TObj, typename TLen>
209 inline void writeObject(apache::geode::client::DataOutput& output,
210  const TObj* array, TLen len) {
211  if (array == nullptr) {
212  output.write(static_cast<int8_t>(-1));
213  } else {
214  output.writeArrayLen(len);
215  const TObj* endArray = array + len;
216  while (array < endArray) {
217  writeObject(output, *array++);
218  }
219  }
220 }
221 
222 template <typename TObj>
223 inline void writeArrayObject(apache::geode::client::DataOutput& output,
224  const std::vector<TObj>& array) {
225  output.writeArrayLen(static_cast<int32_t>(array.size()));
226  for (auto&& obj : array) {
227  writeObject(output, obj);
228  }
229 }
230 
231 template <typename TObj>
232 inline std::vector<TObj> readArrayObject(
234  std::vector<TObj> array;
235  int len = input.readArrayLength();
236  if (len >= 0) {
237  array.resize(len);
238  for (auto&& obj : array) {
239  readObject(input, obj);
240  }
241  }
242  return array;
243 }
244 
245 template <typename TObj, typename TLen>
246 inline void readObject(apache::geode::client::DataInput& input, TObj*& array,
247  TLen& len) {
248  len = input.readArrayLength();
249  if (len > 0) {
250  _GEODE_NEW(array, TObj[len]);
251  TObj* startArray = array;
252  TObj* endArray = array + len;
253  while (startArray < endArray) {
254  readObject(input, *startArray++);
255  }
256  } else {
257  array = nullptr;
258  }
259 }
260 
261 template <typename TObj,
262  typename std::enable_if<!std::is_base_of<Serializable, TObj>::value,
263  Serializable>::type* = nullptr>
264 inline size_t objectArraySize(const std::vector<TObj>& array) {
265  return sizeof(TObj) * array.size();
266 }
267 
268 template <typename TObj,
269  typename std::enable_if<std::is_base_of<Serializable, TObj>::value,
270  Serializable>::type* = nullptr>
271 inline size_t objectArraySize(const std::vector<TObj>& array) {
272  size_t size = 0;
273  for (auto obj : array) {
274  size += obj.objectArraySize();
275  }
276  size += sizeof(TObj) * array.size();
277  return size;
278 }
279 
280 template <typename TObj, typename TLen,
281  typename std::enable_if<!std::is_base_of<Serializable, TObj>::value,
282  Serializable>::type* = nullptr>
283 inline size_t objectSize(const TObj*, TLen len) {
284  return sizeof(TObj) * len;
285 }
286 
287 template <typename TObj, typename TLen,
288  typename std::enable_if<std::is_base_of<Serializable, TObj>::value,
289  Serializable>::type* = nullptr>
290 inline size_t objectSize(const TObj* array, TLen len) {
291  size_t size = 0;
292  const TObj* endArray = array + len;
293  while (array < endArray) {
294  if (*array != nullptr) {
295  size += (*array)->objectSize();
296  }
297  array++;
298  }
299  size += sizeof(TObj) * len;
300  return size;
301 }
302 
303 // For containers vector/hashmap/hashset
304 
305 template <typename TObj, typename Allocator>
306 inline void writeObject(apache::geode::client::DataOutput& output,
307  const std::vector<TObj, Allocator>& value) {
308  output.writeArrayLen(static_cast<int32_t>(value.size()));
309  for (const auto& v : value) {
310  writeObject(output, v);
311  }
312 }
313 
314 inline size_t objectSize(const std::vector<std::shared_ptr<Cacheable>>& value) {
315  size_t objectSize = 0;
316  for (const auto& iter : value) {
317  if (iter) {
318  objectSize += iter->objectSize();
319  }
320  }
321  objectSize += sizeof(std::shared_ptr<Cacheable>) * value.size();
322  return objectSize;
323 }
324 
325 template <typename TObj, typename _tail>
326 inline void readObject(apache::geode::client::DataInput& input,
327  std::vector<TObj, _tail>& value) {
328  int32_t len = input.readArrayLength();
329  if (len >= 0) {
330  TObj obj;
331  for (int32_t index = 0; index < len; index++) {
332  readObject(input, obj);
333  value.push_back(obj);
334  }
335  }
336 }
337 
338 template <typename TKey, typename TValue, typename Hash, typename KeyEqual,
339  typename Allocator>
340 inline void writeObject(
342  const std::unordered_map<TKey, TValue, Hash, KeyEqual, Allocator>& value) {
343  output.writeArrayLen(static_cast<int32_t>(value.size()));
344  for (const auto& iter : value) {
345  writeObject(output, iter.first);
346  writeObject(output, iter.second);
347  }
348 }
349 
350 inline size_t objectSize(const HashMapOfCacheable& value) {
351  auto objectSize = (sizeof(std::shared_ptr<CacheableKey>) +
352  sizeof(std::shared_ptr<Cacheable>)) *
353  value.size();
354  for (const auto& iter : value) {
355  objectSize += iter.first->objectSize();
356  if (iter.second) {
357  objectSize += iter.second->objectSize();
358  }
359  }
360  return objectSize;
361 }
362 
363 template <typename TKey, typename TValue, typename Hash, typename KeyEqual,
364  typename Allocator>
365 inline void readObject(
367  std::unordered_map<TKey, TValue, Hash, KeyEqual, Allocator>& value) {
368  int32_t len = input.readArrayLength();
369  value.reserve(len);
370  std::shared_ptr<Serializable> key;
371  std::shared_ptr<Serializable> val;
372  for (int32_t index = 0; index < len; index++) {
373  readObject(input, key);
374  readObject(input, val);
375  value.emplace(
376  std::dynamic_pointer_cast<typename TKey::element_type>(key),
377  std::dynamic_pointer_cast<typename TValue::element_type>(val));
378  }
379 }
380 
381 template <typename TKey, typename Hash, typename KeyEqual, typename Allocator>
382 inline void writeObject(
384  const std::unordered_set<TKey, Hash, KeyEqual, Allocator>& value) {
385  output.writeArrayLen(static_cast<int32_t>(value.size()));
386  for (const auto& iter : value) {
387  writeObject(output, iter);
388  }
389 }
390 
391 inline size_t objectSize(const HashSetOfCacheableKey& value) {
392  auto objectSize = sizeof(std::shared_ptr<CacheableKey>) * value.size();
393  for (const auto& iter : value) {
394  if (iter) {
395  objectSize += iter->objectSize();
396  }
397  }
398  return objectSize;
399 }
400 
401 template <typename TKey, typename Hash, typename KeyEqual, typename Allocator>
402 inline void readObject(
404  std::unordered_set<TKey, Hash, KeyEqual, Allocator>& value) {
405  int32_t len = input.readArrayLength();
406  if (len > 0) {
407  std::shared_ptr<Serializable> key;
408  for (int32_t index = 0; index < len; index++) {
409  readObject(input, key);
410  value.insert(std::dynamic_pointer_cast<typename TKey::element_type>(key));
411  }
412  }
413 }
414 
415 // Default value for builtin types
416 
417 template <typename TObj>
418 inline TObj zeroObject() {
419  return 0;
420 }
421 
422 template <>
423 inline bool zeroObject<bool>() {
424  return false;
425 }
426 
427 template <>
428 inline double zeroObject<double>() {
429  return 0.0;
430 }
431 
432 template <>
433 inline float zeroObject<float>() {
434  return 0.0F;
435 }
436 } // namespace serializer
437 } // namespace client
438 } // namespace geode
439 } // namespace apache
440 
441 #endif // GEODE_SERIALIZER_H_
apache::geode::client::DataOutput::writeFloat
void writeFloat(float value)
Write a float value to the DataOutput.
Definition: DataOutput.hpp:240
apache::geode::client::DataOutput::writeDouble
void writeDouble(double value)
Write a double precision real number to the DataOutput.
Definition: DataOutput.hpp:254
apache::geode::client::DataOutput::writeBytes
void writeBytes(const uint8_t *bytes, int32_t len)
Write an array of unsigned bytes to the DataOutput.
Definition: DataOutput.hpp:80
apache::geode::client::DataInput::readArrayLength
int32_t readArrayLength()
Read a 32-bit signed integer array length value from the DataInput in a manner compatible with java s...
Definition: DataInput.hpp:216
apache::geode::client::DataOutput::writeInt
void writeInt(uint16_t value)
Write a 16-bit unsigned integer value to the DataOutput.
Definition: DataOutput.hpp:140
DataInput.hpp
apache::geode::client::DataInput::readBytes
void readBytes(uint8_t **bytes, int32_t *len)
Read an array of unsigned bytes from the DataInput expecting to find the length of array in the strea...
Definition: DataInput.hpp:127
apache::geode::client::DataOutput::write
void write(uint8_t value)
Write an unsigned byte to the DataOutput.
Definition: DataOutput.hpp:55
apache::geode::client::DataInput::readInt64
int64_t readInt64()
Read a 64-bit signed integer from the DataInput.
Definition: DataInput.hpp:194
apache::geode::client::DataOutput::writeObject
void writeObject(const std::shared_ptr< PTR > &objptr, bool isDelta=false)
Write a Serializable object to the DataOutput.
Definition: DataOutput.hpp:337
apache::geode::client::DataOutput
Provide operations for writing primitive data values, byte arrays, strings, Serializable objects to a...
Definition: DataOutput.hpp:48
apache::geode::client::DataInput::read
int8_t read()
Read a signed byte from the DataInput.
Definition: DataInput.hpp:66
apache::geode::client::DataInput::readFloat
float readFloat()
Read a float from the DataInput.
Definition: DataInput.hpp:262
apache::geode::client::DataInput::readObject
std::shared_ptr< Serializable > readObject()
Read a Serializable object from the DataInput.
Definition: DataInput.hpp:341
apache::geode::client::DataInput::readInt16
int16_t readInt16()
Read a 16-bit signed integer from the DataInput.
Definition: DataInput.hpp:168
apache::geode::client::DataInput
Provide operations for reading primitive data values, byte arrays, strings, Serializable objects from...
Definition: DataInput.hpp:59
apache::geode::client::DataInput::readBoolean
bool readBoolean()
Read a boolean value from the DataInput.
Definition: DataInput.hpp:76
apache::geode::client::DataInput::readDouble
double readDouble()
Read a double precision number from the DataInput.
Definition: DataInput.hpp:278
apache::geode::client::DataOutput::writeBoolean
void writeBoolean(bool value)
Write a boolean value to the DataOutput.
Definition: DataOutput.hpp:72
apache::geode::client::DataInput::readInt32
int32_t readInt32()
Read a 32-bit signed integer from the DataInput.g.
Definition: DataInput.hpp:179
apache::geode::client::DataOutput::writeArrayLen
void writeArrayLen(int32_t len)
Write a 32-bit signed integer array length value to the DataOutput in a manner compatible with java s...
Definition: DataOutput.hpp:221

Apache Geode C++ Cache API Documentation