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

Pivotal GemFire C++ Cache API Documentation