build-artifacts/win/product/include/gfcpp/PdxAutoSerializer.hpp

00001 #ifndef PDX_AUTO_SERIALIZER_HPP
00002 #define PDX_AUTO_SERIALIZER_HPP
00003 
00004 /*=========================================================================
00005  * Copyright (c) 2002-2014 Pivotal Software, Inc. All Rights Reserved.
00006  * This product is protected by U.S. and international copyright
00007  * and intellectual property laws. Pivotal products are covered by
00008  * more patents listed at http://www.pivotal.io/patents.
00009  *=========================================================================
00010  */
00011 
00012 
00013 #include "gfcpp_globals.hpp"
00014 #include "PdxWriter.hpp"
00015 #include "PdxReader.hpp"
00016 #include "VectorT.hpp"
00017 #include "HashMapT.hpp"
00018 #include "HashSetT.hpp"
00019 #include "GemfireTypeIds.hpp"
00020 #include "TypeHelper.hpp"
00021 
00022 
00023 namespace gemfire
00024 {
00025   namespace PdxAutoSerializable
00026   {
00027     // Read and write methods for various types
00028     //uint8_t
00029     //------------------------------------------------------------------------------------------------
00030 #ifdef _SOLARIS
00031     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  int8_t value )
00032     {
00033        pw->writeByte( fieldName,  value );
00034     }
00035     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  int8_t& value )
00036     {
00037        value = pr->readByte(fieldName);
00038     }   
00039 #else
00040     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  char value )
00041     {
00042       pw->writeChar( fieldName,  value );
00043     }
00044     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  char& value )
00045     {
00046       value = pr->readChar(fieldName) ;
00047     }
00048 #endif
00049     //------------------------------------------------------------------------------------------------
00050     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  wchar_t value )
00051     {
00052       pw->writeWideChar(fieldName, value );
00053     }
00054     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  wchar_t& value )
00055     {
00056       value = pr->readWideChar( fieldName );
00057     }
00058     //------------------------------------------------------------------------------------------------
00059     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  bool value )
00060     {
00061       pw->writeBoolean( fieldName, value );
00062     }
00063     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  bool& value )
00064     {
00065       value = pr->readBoolean( fieldName );
00066     }
00067     //------------------------------------------------------------------------------------------------
00068     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  signed char value )
00069     {
00070       pw->writeByte( fieldName,  value );
00071     }
00072     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  signed char& value )
00073     {
00074       value = pr->readByte(fieldName);
00075     }   
00076     //------------------------------------------------------------------------------------------------
00077     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  int16_t value )
00078     {
00079       pw->writeShort( fieldName, value );
00080     }
00081     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  int16_t& value )
00082     {
00083       value = pr->readShort( fieldName );
00084     }
00085     //------------------------------------------------------------------------------------------------
00086     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  int32_t value )
00087     {
00088       pw->writeInt( fieldName, value );
00089     }
00090     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  int32_t& value )
00091     {
00092       value = pr->readInt( fieldName );
00093     }
00094     //------------------------------------------------------------------------------------------------
00095     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  int64_t value )
00096     {
00097       pw->writeLong( fieldName, value );
00098     }
00099     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  int64_t& value )
00100     {
00101       value = pr->readLong( fieldName);
00102     }
00103     //------------------------------------------------------------------------------------------------
00104     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  float value )
00105     {
00106       pw->writeFloat( fieldName, value );
00107     }
00108     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  float& value )
00109     {
00110       value = pr->readFloat( fieldName );
00111     }
00112     //------------------------------------------------------------------------------------------------
00113     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  double value )
00114     {
00115       pw->writeDouble( fieldName, value );
00116     }
00117     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  double& value )
00118     {
00119       value = pr->readDouble( fieldName );
00120     }
00121     //------------------------------------------------------------------------------------------------
00122     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  CacheableDatePtr value )
00123     {
00124       pw->writeDate( fieldName, value );
00125     }
00126     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  CacheableDatePtr& value )
00127     {
00128       value = pr->readDate( fieldName );
00129     }
00130     //------------------------------------------------------------------------------------------------
00131     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  const char* value )
00132     {
00133       pw->writeString( fieldName, value );
00134     }
00135     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  char*& value )
00136     {
00137       value = pr->readString( fieldName );
00138     }
00139     //------------------------------------------------------------------------------------------------
00140     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName, 
00141                              const wchar_t* value )
00142     {
00143       pw->writeWideString( fieldName, value );
00144     }
00145     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  wchar_t*& value )
00146     {
00147       value = pr->readWideString( fieldName );
00148     }
00149     // ---- helper methods for bytearrayofarray -------
00150 #ifdef _SOLARIS
00151     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName, 
00152                              char** value, int32_t arraySize, int32_t* elemArraySize)
00153     {
00154 
00155         pw->writeArrayOfByteArrays(fieldName, (char**)value, arraySize, elemArraySize);
00156     }
00157     inline void readPdxObject(gemfire::PdxReaderPtr& pr,  const char* fieldName, char**&value, 
00158                                         int32_t& len, int32_t*& Lengtharr) 
00159     {
00160       GF_NEW( Lengtharr, int32_t[len]);
00161       value = (char**)pr->readArrayOfByteArrays(fieldName, len, &Lengtharr);
00162     }
00163 #else
00164     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName, 
00165                              int8_t** value, int32_t arraySize, int32_t* elemArraySize)
00166     {
00167         pw->writeArrayOfByteArrays(fieldName, value, arraySize, elemArraySize);
00168     }
00169     inline void readPdxObject(gemfire::PdxReaderPtr& pr,  const char* fieldName, int8_t**&value, 
00170                                         int32_t& len, int32_t*& Lengtharr) 
00171     {
00172       GF_NEW( Lengtharr, int32_t[len]);
00173       value = (signed char**)pr->readArrayOfByteArrays(fieldName, len, &Lengtharr);
00174     }
00175 #endif
00176 
00177     //------------------------------------------------------------------------------------------------
00178     // Base Serializable types
00179     template <typename TObj>
00180     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName, 
00181                              const gemfire::SharedPtr< TObj >& value,
00182                              gemfire::TypeHelper::yes_type isSerializable )
00183     {
00184       pw->writeObject( fieldName, value );
00185     }
00186 
00187     template <typename TObj>
00188     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName, 
00189                              const gemfire::SharedPtr< TObj >& value )
00190     {
00191       writePdxObject( pw, fieldName, value, GF_TYPE_IS_SERIALIZABLE_TYPE( TObj ) );
00192     }
00193 
00194     template <typename TObj>
00195     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName, 
00196                             gemfire::SharedPtr< TObj >& value,
00197                             gemfire::TypeHelper::yes_type isSerializable )
00198     {
00199        value = dynCast<gemfire::SharedPtr< TObj > >(pr->readObject(fieldName));
00200     }
00201 
00202     template <typename TObj>
00203     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName, 
00204                             gemfire::SharedPtr< TObj >& value )
00205     {
00206       readPdxObject( pr, fieldName, value, GF_TYPE_IS_SERIALIZABLE_TYPE( TObj ) );
00207     }
00208     //------------------------------------------------------------------------------------------------
00209     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  bool* value,
00210                              int32_t len )
00211     {
00212       pw->writeBooleanArray( fieldName, value, len );
00213     }
00214 
00215     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  bool*& value,
00216                             int32_t& len )
00217     {
00218       value = pr->readBooleanArray( fieldName, len );
00219     }
00220     //------------------------------------------------------------------------------------------------
00221     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  wchar_t* value,
00222                              int32_t len )
00223     {
00224       pw->writeWideCharArray( fieldName, value, len );
00225     }
00226 
00227     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  wchar_t*& value,
00228                             int32_t& len )
00229     {
00230       value = pr->readWideCharArray( fieldName, len );
00231     }
00232     //------------------------------------------------------------------------------------------------
00233 #ifdef _SOLARIS
00234     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  int8_t* value,
00235                              int32_t len )
00236     {
00237       pw->writeByteArray( fieldName, value, len );
00238     }
00239 
00240     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  int8_t*& value,
00241                             int32_t& len )
00242     {
00243       value = (int8_t*)pr->readByteArray( fieldName, len );
00244     }
00245 #else
00246     //------------------------------------------------------------------------------------------------
00247     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  char* value,
00248                              int32_t len )
00249     {
00250       pw->writeCharArray( fieldName, value, len );
00251     }
00252 
00253     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  char*& value,
00254                             int32_t& len )
00255     {
00256       value = pr->readCharArray( fieldName, len );
00257     }
00258     //------------------------------------------------------------------------------------------------
00259     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  signed char* value,
00260                              int32_t len )
00261     {
00262       pw->writeByteArray( fieldName, value, len );
00263     }
00264 
00265     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  signed char*& value,
00266                             int32_t& len )
00267     {
00268       value = (signed char*)pr->readByteArray( fieldName, len );
00269     }
00270 #endif
00271     //------------------------------------------------------------------------------------------------
00272     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  int16_t* value,
00273                              int32_t len )
00274     {
00275       pw->writeShortArray( fieldName, value, len );
00276     }
00277 
00278     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  int16_t*& value,
00279                             int32_t& len )
00280     {
00281       value = pr->readShortArray( fieldName, len );
00282     }
00283     //------------------------------------------------------------------------------------------------
00284     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  int32_t* value,
00285                              int32_t len )
00286     {
00287       pw->writeIntArray( fieldName, value, len );
00288     }
00289 
00290     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  int32_t*& value,
00291                             int32_t& len )
00292     {
00293       value = pr->readIntArray( fieldName, len );
00294     }
00295     //------------------------------------------------------------------------------------------------
00296     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  int64_t* value,
00297                              int32_t len )
00298     {
00299       pw->writeLongArray( fieldName, value, len );
00300     }
00301 
00302     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  int64_t*& value,
00303                             int32_t& len )
00304     {
00305       value = pr->readLongArray( fieldName, len );
00306     }
00307     //------------------------------------------------------------------------------------------------
00308     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  float* value,
00309                              int32_t len )
00310     {
00311       pw->writeFloatArray( fieldName, value, len );
00312     }
00313 
00314     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  float*& value,
00315                             int32_t& len )
00316     {
00317       value = pr->readFloatArray( fieldName, len );
00318     }
00319     //------------------------------------------------------------------------------------------------
00320     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  double* value,
00321                              int32_t len )
00322     {
00323       pw->writeDoubleArray( fieldName, value, len );
00324     }
00325 
00326     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  double*& value,
00327                             int32_t& len )
00328     {
00329       value = pr->readDoubleArray( fieldName, len );
00330     }
00331     //------------------------------------------------------------------------------------------------
00332     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  char** value,
00333                              int32_t len )
00334     {
00335       pw->writeStringArray( fieldName, value, len );
00336     }
00337 
00338     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  char**& value,
00339                             int32_t& len )
00340     {
00341       value = pr->readStringArray( fieldName, len );
00342     }
00343     //------------------------------------------------------------------------------------------------
00344     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  wchar_t** value,
00345                              int32_t len )
00346     {
00347       pw->writeWideStringArray( fieldName, value, len );
00348     }
00349 
00350     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  wchar_t**& value,
00351                             int32_t& len )
00352     {
00353       value = pr->readWideStringArray( fieldName, len );
00354     }
00355     //------------------------------------------------------------------------------------------------
00356     inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName,  CacheableObjectArrayPtr value)
00357     {
00358       pw->writeObjectArray( fieldName, value);
00359     }
00360 
00361     inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName,  CacheableObjectArrayPtr& value )
00362     {
00363       value = pr->readObjectArray( fieldName);
00364     }
00365     //------------------------------------------------------------------------------------------------
00366     // For containers vector/hashmap/hashset
00367     //template <typename TObj>
00368     //inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName, 
00369     //    const VectorT< TObj >& value )
00370     //{
00371     //  int32_t len = (int32_t)value.size();
00372     //  pw->writeArrayLen( len );
00373     //  for ( typename VectorT< TObj >::Iterator iter = value.begin( );
00374     //      iter != value.end( ); ++iter ) {
00375     //    writePdxObject( output, *iter );
00376     //  }
00377     //}
00378 
00379     //inline uint32_t objectSize( const _VectorOfCacheable& value )
00380     //{
00381     //  uint32_t objectSize = 0;
00382     //  for ( _VectorOfCacheable::Iterator iter = value.begin( );
00383     //      iter != value.end( ); ++iter ) {
00384     //    if (*iter != NULLPTR) {
00385     //      objectSize += (*iter)->objectSize( );
00386     //    }
00387     //  }
00388     //  objectSize += (sizeof(CacheablePtr) * (uint32_t)value.size());
00389     //  return objectSize;
00390     //}
00391 
00392     //template <typename TObj>
00393     //inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName, 
00394     //    VectorT< TObj >& value )
00395     //{
00396     //  int32_t len;
00397     //  pr->readArrayLen( &len );
00398     //  if ( len >= 0 ) {
00399     //    TObj obj;
00400     //    for ( int32_t index = 0; index < len; index++ ) {
00401     //      readPdxObject( input, obj );
00402     //      value.push_back( obj );
00403     //    }
00404     //  }
00405     //}
00406 
00407 
00408     //template <typename TKey, typename TValue>
00409     //inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName, 
00410     //    const HashMapT< TKey, TValue >& value )
00411     //{
00412     //  int32_t len = (int32_t)value.size();
00413     //  pw->writeArrayLen( len );
00414     //  if ( len > 0 ) {
00415     //    for ( typename HashMapT< TKey, TValue >::Iterator iter = value.begin( );
00416     //        iter != value.end( ); ++iter ) {
00417     //      writePdxObject( output, iter.first( ) );
00418     //      writePdxObject( output, iter.second( ) );
00419     //    }
00420     //  }
00421     //}
00422 
00423     //inline uint32_t objectSize( const _HashMapOfCacheable& value )
00424     //{
00425     //  uint32_t objectSize = 0;
00426     //  for ( _HashMapOfCacheable::Iterator iter = value.begin( );
00427     //      iter != value.end( ); ++iter ) {
00428     //    objectSize += iter.first( )->objectSize( );
00429     //    if (iter.second( ) != NULLPTR) {
00430     //      objectSize += iter.second( )->objectSize( );
00431     //    }
00432     //  }
00433     //  objectSize += ( ( sizeof( CacheableKeyPtr ) + sizeof( CacheablePtr ) )
00434     //      * (uint32_t)value.size());
00435     //  return objectSize;
00436     //}
00437 
00438     //template <typename TKey, typename TValue>
00439     //inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName, 
00440     //    HashMapT< TKey, TValue >& value )
00441     //{
00442     //  int32_t len;
00443     //  pr->readArrayLen( &len );
00444     //  if ( len > 0 ) {
00445     //    TKey key;
00446     //    TValue val;
00447     //    for( int32_t index = 0; index < len; index++ ) {
00448     //      readPdxObject( input, key );
00449     //      readPdxObject( input, val );
00450     //      value.insert( key, val );
00451     //    }
00452     //  }
00453     //}
00454 
00455 
00456     //template <typename TKey>
00457     //inline void writePdxObject( gemfire::PdxWriterPtr& pw,  const char* fieldName, 
00458     //  const HashSetT< TKey >& value )
00459     //{
00460     //  int32_t len = (int32_t)value.size();
00461     //  pw->writeArrayLen( len );
00462     //  for ( typename HashSetT< TKey >::Iterator iter = value.begin( );
00463     //    iter != value.end( ); ++iter ) {
00464     //    writePdxObject( output, *iter );
00465     //  }
00466     //}
00467 
00468     //inline uint32_t objectSize( const _HashSetOfCacheableKey& value )
00469     //{
00470     //  uint32_t objectSize = 0;
00471     //  for ( _HashSetOfCacheableKey::Iterator iter = value.begin( );
00472     //    iter != value.end( ); ++iter ) {
00473     //    if (*iter != NULLPTR) {
00474     //      objectSize += (*iter)->objectSize( );
00475     //    }
00476     //  }
00477     //  objectSize += (sizeof(CacheableKeyPtr) * (uint32_t)value.size());
00478     //  return objectSize;
00479     //}
00480 
00481     //template <typename TKey>
00482     //inline void readPdxObject( gemfire::PdxReaderPtr& pr,  const char* fieldName, 
00483     //    HashSetT< TKey >& value )
00484     //{
00485     //  int32_t len;
00486     //  pr->readArrayLen( &len );
00487     //  if ( len > 0 ) {
00488     //    TKey key;
00489     //    for( int32_t index = 0; index < len; index++ ) {
00490     //      readPdxObject( input, key );
00491     //      value.insert( key );
00492     //    }
00493     //  }
00494     //}
00495 
00496 
00497 
00499 
00500     //template <typename TObj>
00501     //inline TObj zeroObject( )
00502     //{
00503     //  return 0;
00504     //}
00505 
00506     //template <>
00507     //inline bool zeroObject<bool>( )
00508     //{
00509     //  return false;
00510     //}
00511 
00512     //template <>
00513     //inline double zeroObject<double>( )
00514     //{
00515     //  return 0.0;
00516     //}
00517 
00518     //template <>
00519     //inline float zeroObject<float>( )
00520     //{
00521     //  return 0.0F;
00522     //}
00523 
00524   } // namespace PdxAutoSerializable
00525 }
00526 
00527 
00528 #endif // _GEMFIRE_SERIALIZER_HPP_

GemFire C++ Cache API Documentation