VMware GemFire Native Client Cache Reference  9.0.6
PdxAutoSerializer.hpp
1 #ifndef PDX_AUTO_SERIALIZER_HPP
2 #define PDX_AUTO_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 "PdxWriter.hpp"
15 #include "PdxReader.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 PdxAutoSerializable
26  {
27  // Read and write methods for various types
28  //uint8_t
29  //------------------------------------------------------------------------------------------------
30 #ifdef _SOLARIS
31  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, int8_t value )
32  {
33  pw->writeByte( fieldName, value );
34  }
35  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, int8_t& value )
36  {
37  value = pr->readByte(fieldName);
38  }
39 #else
40  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, char value )
41  {
42  pw->writeChar( fieldName, value );
43  }
44  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, char& value )
45  {
46  value = pr->readChar(fieldName) ;
47  }
48 #endif
49  //------------------------------------------------------------------------------------------------
50  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, wchar_t value )
51  {
52  pw->writeWideChar(fieldName, value );
53  }
54  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, wchar_t& value )
55  {
56  value = pr->readWideChar( fieldName );
57  }
58  //------------------------------------------------------------------------------------------------
59  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, bool value )
60  {
61  pw->writeBoolean( fieldName, value );
62  }
63  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, bool& value )
64  {
65  value = pr->readBoolean( fieldName );
66  }
67  //------------------------------------------------------------------------------------------------
68  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, signed char value )
69  {
70  pw->writeByte( fieldName, value );
71  }
72  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, signed char& value )
73  {
74  value = pr->readByte(fieldName);
75  }
76  //------------------------------------------------------------------------------------------------
77  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, int16_t value )
78  {
79  pw->writeShort( fieldName, value );
80  }
81  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, int16_t& value )
82  {
83  value = pr->readShort( fieldName );
84  }
85  //------------------------------------------------------------------------------------------------
86  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, int32_t value )
87  {
88  pw->writeInt( fieldName, value );
89  }
90  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, int32_t& value )
91  {
92  value = pr->readInt( fieldName );
93  }
94  //------------------------------------------------------------------------------------------------
95  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, int64_t value )
96  {
97  pw->writeLong( fieldName, value );
98  }
99  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, int64_t& value )
100  {
101  value = pr->readLong( fieldName);
102  }
103  //------------------------------------------------------------------------------------------------
104  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, float value )
105  {
106  pw->writeFloat( fieldName, value );
107  }
108  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, float& value )
109  {
110  value = pr->readFloat( fieldName );
111  }
112  //------------------------------------------------------------------------------------------------
113  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, double value )
114  {
115  pw->writeDouble( fieldName, value );
116  }
117  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, double& value )
118  {
119  value = pr->readDouble( fieldName );
120  }
121  //------------------------------------------------------------------------------------------------
122  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, CacheableDatePtr value )
123  {
124  pw->writeDate( fieldName, value );
125  }
126  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, CacheableDatePtr& value )
127  {
128  value = pr->readDate( fieldName );
129  }
130  //------------------------------------------------------------------------------------------------
131  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, const char* value )
132  {
133  pw->writeString( fieldName, value );
134  }
135  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, char*& value )
136  {
137  value = pr->readString( fieldName );
138  }
139  //------------------------------------------------------------------------------------------------
140  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName,
141  const wchar_t* value )
142  {
143  pw->writeWideString( fieldName, value );
144  }
145  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, wchar_t*& value )
146  {
147  value = pr->readWideString( fieldName );
148  }
149  // ---- helper methods for bytearrayofarray -------
150 #ifdef _SOLARIS
151  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName,
152  char** value, int32_t arraySize, int32_t* elemArraySize)
153  {
154 
155  pw->writeArrayOfByteArrays(fieldName, (char**)value, arraySize, elemArraySize);
156  }
157  inline void readPdxObject(gemfire::PdxReaderPtr& pr, const char* fieldName, char**&value,
158  int32_t& len, int32_t*& Lengtharr)
159  {
160  GF_NEW( Lengtharr, int32_t[len]);
161  value = (char**)pr->readArrayOfByteArrays(fieldName, len, &Lengtharr);
162  }
163 #else
164  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName,
165  int8_t** value, int32_t arraySize, int32_t* elemArraySize)
166  {
167  pw->writeArrayOfByteArrays(fieldName, value, arraySize, elemArraySize);
168  }
169  inline void readPdxObject(gemfire::PdxReaderPtr& pr, const char* fieldName, int8_t**&value,
170  int32_t& len, int32_t*& Lengtharr)
171  {
172  GF_NEW( Lengtharr, int32_t[len]);
173  value = (signed char**)pr->readArrayOfByteArrays(fieldName, len, &Lengtharr);
174  }
175 #endif
176 
177  //------------------------------------------------------------------------------------------------
178  // Base Serializable types
179  template <typename TObj>
180  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName,
181  const gemfire::SharedPtr< TObj >& value,
182  gemfire::TypeHelper::yes_type isSerializable )
183  {
184  pw->writeObject( fieldName, value );
185  }
186 
187  template <typename TObj>
188  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName,
189  const gemfire::SharedPtr< TObj >& value )
190  {
191  writePdxObject( pw, fieldName, value, GF_TYPE_IS_SERIALIZABLE_TYPE( TObj ) );
192  }
193 
194  template <typename TObj>
195  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName,
197  gemfire::TypeHelper::yes_type isSerializable )
198  {
199  value = dynCast<gemfire::SharedPtr< TObj > >(pr->readObject(fieldName));
200  }
201 
202  template <typename TObj>
203  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName,
205  {
206  readPdxObject( pr, fieldName, value, GF_TYPE_IS_SERIALIZABLE_TYPE( TObj ) );
207  }
208  //------------------------------------------------------------------------------------------------
209  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, bool* value,
210  int32_t len )
211  {
212  pw->writeBooleanArray( fieldName, value, len );
213  }
214 
215  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, bool*& value,
216  int32_t& len )
217  {
218  value = pr->readBooleanArray( fieldName, len );
219  }
220  //------------------------------------------------------------------------------------------------
221  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, wchar_t* value,
222  int32_t len )
223  {
224  pw->writeWideCharArray( fieldName, value, len );
225  }
226 
227  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, wchar_t*& value,
228  int32_t& len )
229  {
230  value = pr->readWideCharArray( fieldName, len );
231  }
232  //------------------------------------------------------------------------------------------------
233 #ifdef _SOLARIS
234  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, int8_t* value,
235  int32_t len )
236  {
237  pw->writeByteArray( fieldName, value, len );
238  }
239 
240  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, int8_t*& value,
241  int32_t& len )
242  {
243  value = (int8_t*)pr->readByteArray( fieldName, len );
244  }
245 #else
246  //------------------------------------------------------------------------------------------------
247  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, char* value,
248  int32_t len )
249  {
250  pw->writeCharArray( fieldName, value, len );
251  }
252 
253  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, char*& value,
254  int32_t& len )
255  {
256  value = pr->readCharArray( fieldName, len );
257  }
258  //------------------------------------------------------------------------------------------------
259  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, signed char* value,
260  int32_t len )
261  {
262  pw->writeByteArray( fieldName, value, len );
263  }
264 
265  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, signed char*& value,
266  int32_t& len )
267  {
268  value = (signed char*)pr->readByteArray( fieldName, len );
269  }
270 #endif
271  //------------------------------------------------------------------------------------------------
272  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, int16_t* value,
273  int32_t len )
274  {
275  pw->writeShortArray( fieldName, value, len );
276  }
277 
278  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, int16_t*& value,
279  int32_t& len )
280  {
281  value = pr->readShortArray( fieldName, len );
282  }
283  //------------------------------------------------------------------------------------------------
284  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, int32_t* value,
285  int32_t len )
286  {
287  pw->writeIntArray( fieldName, value, len );
288  }
289 
290  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, int32_t*& value,
291  int32_t& len )
292  {
293  value = pr->readIntArray( fieldName, len );
294  }
295  //------------------------------------------------------------------------------------------------
296  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, int64_t* value,
297  int32_t len )
298  {
299  pw->writeLongArray( fieldName, value, len );
300  }
301 
302  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, int64_t*& value,
303  int32_t& len )
304  {
305  value = pr->readLongArray( fieldName, len );
306  }
307  //------------------------------------------------------------------------------------------------
308  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, float* value,
309  int32_t len )
310  {
311  pw->writeFloatArray( fieldName, value, len );
312  }
313 
314  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, float*& value,
315  int32_t& len )
316  {
317  value = pr->readFloatArray( fieldName, len );
318  }
319  //------------------------------------------------------------------------------------------------
320  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, double* value,
321  int32_t len )
322  {
323  pw->writeDoubleArray( fieldName, value, len );
324  }
325 
326  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, double*& value,
327  int32_t& len )
328  {
329  value = pr->readDoubleArray( fieldName, len );
330  }
331  //------------------------------------------------------------------------------------------------
332  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, char** value,
333  int32_t len )
334  {
335  pw->writeStringArray( fieldName, value, len );
336  }
337 
338  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, char**& value,
339  int32_t& len )
340  {
341  value = pr->readStringArray( fieldName, len );
342  }
343  //------------------------------------------------------------------------------------------------
344  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, wchar_t** value,
345  int32_t len )
346  {
347  pw->writeWideStringArray( fieldName, value, len );
348  }
349 
350  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, wchar_t**& value,
351  int32_t& len )
352  {
353  value = pr->readWideStringArray( fieldName, len );
354  }
355  //------------------------------------------------------------------------------------------------
356  inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName, CacheableObjectArrayPtr value)
357  {
358  pw->writeObjectArray( fieldName, value);
359  }
360 
361  inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName, CacheableObjectArrayPtr& value )
362  {
363  value = pr->readObjectArray( fieldName);
364  }
365  //------------------------------------------------------------------------------------------------
366  // For containers vector/hashmap/hashset
367  //template <typename TObj>
368  //inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName,
369  // const VectorT< TObj >& value )
370  //{
371  // int32_t len = (int32_t)value.size();
372  // pw->writeArrayLen( len );
373  // for ( typename VectorT< TObj >::Iterator iter = value.begin( );
374  // iter != value.end( ); ++iter ) {
375  // writePdxObject( output, *iter );
376  // }
377  //}
378 
379  //inline uint32_t objectSize( const _VectorOfCacheable& value )
380  //{
381  // uint32_t objectSize = 0;
382  // for ( _VectorOfCacheable::Iterator iter = value.begin( );
383  // iter != value.end( ); ++iter ) {
384  // if (*iter != NULLPTR) {
385  // objectSize += (*iter)->objectSize( );
386  // }
387  // }
388  // objectSize += (sizeof(CacheablePtr) * (uint32_t)value.size());
389  // return objectSize;
390  //}
391 
392  //template <typename TObj>
393  //inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName,
394  // VectorT< TObj >& value )
395  //{
396  // int32_t len;
397  // pr->readArrayLen( &len );
398  // if ( len >= 0 ) {
399  // TObj obj;
400  // for ( int32_t index = 0; index < len; index++ ) {
401  // readPdxObject( input, obj );
402  // value.push_back( obj );
403  // }
404  // }
405  //}
406 
407 
408  //template <typename TKey, typename TValue>
409  //inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName,
410  // const HashMapT< TKey, TValue >& value )
411  //{
412  // int32_t len = (int32_t)value.size();
413  // pw->writeArrayLen( len );
414  // if ( len > 0 ) {
415  // for ( typename HashMapT< TKey, TValue >::Iterator iter = value.begin( );
416  // iter != value.end( ); ++iter ) {
417  // writePdxObject( output, iter.first( ) );
418  // writePdxObject( output, iter.second( ) );
419  // }
420  // }
421  //}
422 
423  //inline uint32_t objectSize( const _HashMapOfCacheable& value )
424  //{
425  // uint32_t objectSize = 0;
426  // for ( _HashMapOfCacheable::Iterator iter = value.begin( );
427  // iter != value.end( ); ++iter ) {
428  // objectSize += iter.first( )->objectSize( );
429  // if (iter.second( ) != NULLPTR) {
430  // objectSize += iter.second( )->objectSize( );
431  // }
432  // }
433  // objectSize += ( ( sizeof( CacheableKeyPtr ) + sizeof( CacheablePtr ) )
434  // * (uint32_t)value.size());
435  // return objectSize;
436  //}
437 
438  //template <typename TKey, typename TValue>
439  //inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName,
440  // HashMapT< TKey, TValue >& value )
441  //{
442  // int32_t len;
443  // pr->readArrayLen( &len );
444  // if ( len > 0 ) {
445  // TKey key;
446  // TValue val;
447  // for( int32_t index = 0; index < len; index++ ) {
448  // readPdxObject( input, key );
449  // readPdxObject( input, val );
450  // value.insert( key, val );
451  // }
452  // }
453  //}
454 
455 
456  //template <typename TKey>
457  //inline void writePdxObject( gemfire::PdxWriterPtr& pw, const char* fieldName,
458  // const HashSetT< TKey >& value )
459  //{
460  // int32_t len = (int32_t)value.size();
461  // pw->writeArrayLen( len );
462  // for ( typename HashSetT< TKey >::Iterator iter = value.begin( );
463  // iter != value.end( ); ++iter ) {
464  // writePdxObject( output, *iter );
465  // }
466  //}
467 
468  //inline uint32_t objectSize( const _HashSetOfCacheableKey& value )
469  //{
470  // uint32_t objectSize = 0;
471  // for ( _HashSetOfCacheableKey::Iterator iter = value.begin( );
472  // iter != value.end( ); ++iter ) {
473  // if (*iter != NULLPTR) {
474  // objectSize += (*iter)->objectSize( );
475  // }
476  // }
477  // objectSize += (sizeof(CacheableKeyPtr) * (uint32_t)value.size());
478  // return objectSize;
479  //}
480 
481  //template <typename TKey>
482  //inline void readPdxObject( gemfire::PdxReaderPtr& pr, const char* fieldName,
483  // HashSetT< TKey >& value )
484  //{
485  // int32_t len;
486  // pr->readArrayLen( &len );
487  // if ( len > 0 ) {
488  // TKey key;
489  // for( int32_t index = 0; index < len; index++ ) {
490  // readPdxObject( input, key );
491  // value.insert( key );
492  // }
493  // }
494  //}
495 
496 
497 
499 
500  //template <typename TObj>
501  //inline TObj zeroObject( )
502  //{
503  // return 0;
504  //}
505 
506  //template <>
507  //inline bool zeroObject<bool>( )
508  //{
509  // return false;
510  //}
511 
512  //template <>
513  //inline double zeroObject<double>( )
514  //{
515  // return 0.0;
516  //}
517 
518  //template <>
519  //inline float zeroObject<float>( )
520  //{
521  // return 0.0F;
522  //}
523 
524  } // namespace PdxAutoSerializable
525 }
526 
527 
528 #endif // _GEMFIRE_SERIALIZER_HPP_
This namespace contains all the GemFire C++ API classes, enumerations and globals.
Definition: Assert.hpp:19
#define GF_NEW(v, stmt)
Allocates x and throws OutOfMemoryException if it fails.
Definition: gf_base.hpp:315
Defines a reference counted shared pointer.
Definition: SharedPtr.hpp:35

GemFire C++ Cache API Documentation