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

GemFire C++ Cache API Documentation