Apache Geode Native C++ Reference 1.15.0
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
33namespace apache {
34namespace geode {
35namespace client {
36namespace serializer {
37
38// Read and write methods for various types
39
40inline void writeObject(apache::geode::client::DataOutput& output,
41 uint8_t value) {
42 output.write(value);
43}
44
45inline void readObject(apache::geode::client::DataInput& input,
46 uint8_t& value) {
47 value = input.read();
48}
49
50inline void writeObject(apache::geode::client::DataOutput& output,
51 int8_t value) {
52 output.write(value);
53}
54
55inline void readObject(apache::geode::client::DataInput& input, int8_t& value) {
56 value = input.read();
57}
58
59inline void writeObject(apache::geode::client::DataOutput& output,
60 const uint8_t* bytes, int32_t len) {
61 output.writeBytes(bytes, len);
62}
63
64inline void readObject(apache::geode::client::DataInput& input, uint8_t*& bytes,
65 int32_t& len) {
66 input.readBytes(&bytes, &len);
67}
68
69inline void writeObject(apache::geode::client::DataOutput& output,
70 const int8_t* bytes, int32_t len) {
71 output.writeBytes(bytes, len);
72}
73
74inline void readObject(apache::geode::client::DataInput& input, int8_t*& bytes,
75 int32_t& len) {
76 input.readBytes(&bytes, &len);
77}
78
79inline void writeObject(apache::geode::client::DataOutput& output,
80 int16_t value) {
81 output.writeInt(value);
82}
83
84inline void readObject(apache::geode::client::DataInput& input,
85 int16_t& value) {
86 value = input.readInt16();
87}
88
89inline void writeObject(apache::geode::client::DataOutput& output,
90 int32_t value) {
91 output.writeInt(value);
92}
93
94inline void readObject(apache::geode::client::DataInput& input,
95 int32_t& value) {
96 value = input.readInt32();
97}
98
99inline void writeObject(apache::geode::client::DataOutput& output,
100 int64_t value) {
101 output.writeInt(value);
102}
103
104inline void readObject(apache::geode::client::DataInput& input,
105 int64_t& value) {
106 value = input.readInt64();
107}
108
109inline void writeObject(apache::geode::client::DataOutput& output,
110 uint16_t value) {
111 output.writeInt(value);
112}
113
114inline void readObject(apache::geode::client::DataInput& input,
115 uint16_t& value) {
116 value = input.readInt16();
117}
118
119inline void writeObject(apache::geode::client::DataOutput& output,
120 uint32_t value) {
121 output.writeInt(value);
122}
123
124inline void readObject(apache::geode::client::DataInput& input,
125 uint32_t& value) {
126 value = input.readInt32();
127}
128
129inline void writeObject(apache::geode::client::DataOutput& output,
130 uint64_t value) {
131 output.writeInt(value);
132}
133
134inline void readObject(apache::geode::client::DataInput& input,
135 uint64_t& value) {
136 value = input.readInt64();
137}
138
139inline void writeObject(apache::geode::client::DataOutput& output, bool value) {
140 output.writeBoolean(value);
141}
142
143inline void readObject(apache::geode::client::DataInput& input, bool& value) {
144 value = input.readBoolean();
145}
146
147inline void readObject(apache::geode::client::DataInput& input,
148 std::vector<bool>::reference value) {
149 value = input.readBoolean();
150}
151
152inline void writeObject(apache::geode::client::DataOutput& output,
153 double value) {
154 output.writeDouble(value);
155}
156
157inline void readObject(apache::geode::client::DataInput& input, double& value) {
158 value = input.readDouble();
159}
160
161inline void writeObject(apache::geode::client::DataOutput& output,
162 float value) {
163 output.writeFloat(value);
164}
165
166inline void readObject(apache::geode::client::DataInput& input, float& value) {
167 value = input.readFloat();
168}
169
170inline void writeObject(apache::geode::client::DataOutput& output,
171 char16_t value) {
172 output.writeInt(static_cast<int16_t>(value));
173}
174
175inline void readObject(apache::geode::client::DataInput& input,
176 char16_t& value) {
177 value = input.readInt16();
178}
179
180inline void readObject(apache::geode::client::DataInput& input,
181 std::string& value) {
182 value = input.readString();
183}
184
185inline void writeObject(apache::geode::client::DataOutput& output,
186 const std::string& value) {
187 output.writeString(value);
188}
189
190template <typename TObj,
191 typename std::enable_if<std::is_base_of<Serializable, TObj>::value,
192 Serializable>::type* = nullptr>
193inline void writeObject(apache::geode::client::DataOutput& output,
194 const std::shared_ptr<TObj>& value) {
195 output.writeObject(value);
196}
197
198template <typename TObj,
199 typename std::enable_if<std::is_base_of<Serializable, TObj>::value,
200 Serializable>::type* = nullptr>
201inline 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
208template <typename TObj, typename TLen>
209inline 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
222template <typename TObj>
223inline 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
231template <typename TObj>
232inline 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
245template <typename TObj, typename TLen>
246inline 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
261template <typename TObj,
262 typename std::enable_if<!std::is_base_of<Serializable, TObj>::value,
263 Serializable>::type* = nullptr>
264inline size_t objectArraySize(const std::vector<TObj>& array) {
265 return sizeof(TObj) * array.size();
266}
267
268template <typename TObj,
269 typename std::enable_if<std::is_base_of<Serializable, TObj>::value,
270 Serializable>::type* = nullptr>
271inline 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
280template <typename TObj, typename TLen,
281 typename std::enable_if<!std::is_base_of<Serializable, TObj>::value,
282 Serializable>::type* = nullptr>
283inline size_t objectSize(const TObj*, TLen len) {
284 return sizeof(TObj) * len;
285}
286
287template <typename TObj, typename TLen,
288 typename std::enable_if<std::is_base_of<Serializable, TObj>::value,
289 Serializable>::type* = nullptr>
290inline 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
305template <typename TObj, typename Allocator>
306inline 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
314inline 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
325template <typename TObj, typename _tail>
326inline 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
338template <typename TKey, typename TValue, typename Hash, typename KeyEqual,
339 typename Allocator>
340inline 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
350inline 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
363template <typename TKey, typename TValue, typename Hash, typename KeyEqual,
364 typename Allocator>
365inline 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
381template <typename TKey, typename Hash, typename KeyEqual, typename Allocator>
382inline 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
391inline 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
401template <typename TKey, typename Hash, typename KeyEqual, typename Allocator>
402inline 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
417template <typename TObj>
418inline TObj zeroObject() {
419 return 0;
420}
421
422template <>
423inline bool zeroObject<bool>() {
424 return false;
425}
426
427template <>
428inline double zeroObject<double>() {
429 return 0.0;
430}
431
432template <>
433inline 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_
Provide operations for reading primitive data values, byte arrays, strings, Serializable objects from...
Definition: DataInput.hpp:59
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:125
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:205
int64_t readInt64()
Read a 64-bit signed integer from the DataInput.
Definition: DataInput.hpp:186
int16_t readInt16()
Read a 16-bit signed integer from the DataInput.
Definition: DataInput.hpp:166
bool readBoolean()
Read a boolean value from the DataInput.
Definition: DataInput.hpp:74
double readDouble()
Read a double precision number from the DataInput.
Definition: DataInput.hpp:262
std::shared_ptr< Serializable > readObject()
Read a Serializable object from the DataInput.
Definition: DataInput.hpp:314
int8_t read()
Read a signed byte from the DataInput.
Definition: DataInput.hpp:66
float readFloat()
Read a float from the DataInput.
Definition: DataInput.hpp:249
int32_t readInt32()
Read a 32-bit signed integer from the DataInput.g.
Definition: DataInput.hpp:174
Provide operations for writing primitive data values, byte arrays, strings, Serializable objects to a...
Definition: DataOutput.hpp:48
void writeDouble(double value)
Write a double precision real number to the DataOutput.
Definition: DataOutput.hpp:254
void writeFloat(float value)
Write a float value to the DataOutput.
Definition: DataOutput.hpp:240
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
void writeInt(uint16_t value)
Write a 16-bit unsigned integer value to the DataOutput.
Definition: DataOutput.hpp:140
void writeBytes(const uint8_t *bytes, int32_t len)
Write an array of unsigned bytes to the DataOutput.
Definition: DataOutput.hpp:80
void write(uint8_t value)
Write an unsigned byte to the DataOutput.
Definition: DataOutput.hpp:55
void writeObject(const std::shared_ptr< PTR > &objptr, bool isDelta=false)
Write a Serializable object to the DataOutput.
Definition: DataOutput.hpp:337
void writeBoolean(bool value)
Write a boolean value to the DataOutput.
Definition: DataOutput.hpp:72

Apache Geode C++ Cache API Documentation