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

00001 #ifndef _GEMFIRE_SERIALIZER_HPP_
00002 #define _GEMFIRE_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 "DataOutput.hpp"
00015 #include "DataInput.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 serializer
00026   {
00027 
00028     // Read and write methods for various types
00029 
00030     inline void writeObject( gemfire::DataOutput& output, uint8_t value )
00031     {
00032       output.write( value );
00033     }
00034 
00035     inline void readObject( gemfire::DataInput& input, uint8_t& value )
00036     {
00037       input.read( &value );
00038     }
00039 
00040     inline void writeObject( gemfire::DataOutput& output, int8_t value )
00041     {
00042       output.write( value );
00043     }
00044 
00045     inline void readObject( gemfire::DataInput& input, int8_t& value )
00046     {
00047       input.read( &value );
00048     }
00049 
00050     inline void writeObject( gemfire::DataOutput& output, const uint8_t* bytes,
00051                              int32_t len )
00052     {
00053       output.writeBytes( bytes, len );
00054     }
00055 
00056     inline void readObject( gemfire::DataInput& input, uint8_t*& bytes,
00057                             int32_t& len )
00058     {
00059       input.readBytes( &bytes, &len );
00060     }
00061 
00062     inline void writeObject( gemfire::DataOutput& output, const int8_t* bytes,
00063                              int32_t len )
00064     {
00065       output.writeBytes( bytes, len );
00066     }
00067 
00068     inline void readObject( gemfire::DataInput& input, int8_t*& bytes,
00069                             int32_t& len )
00070     {
00071       input.readBytes( &bytes, &len );
00072     }
00073 
00074     inline void writeObject( gemfire::DataOutput& output, int16_t value )
00075     {
00076       output.writeInt( value );
00077     }
00078 
00079     inline void readObject( gemfire::DataInput& input, int16_t& value )
00080     {
00081       input.readInt( &value );
00082     }
00083 
00084     inline void writeObject( gemfire::DataOutput& output, int32_t value )
00085     {
00086       output.writeInt( value );
00087     }
00088 
00089     inline void readObject( gemfire::DataInput& input, int32_t& value )
00090     {
00091       input.readInt( &value );
00092     }
00093 
00094     inline void writeObject( gemfire::DataOutput& output, int64_t value )
00095     {
00096       output.writeInt( value );
00097     }
00098 
00099     inline void readObject( gemfire::DataInput& input, int64_t& value )
00100     {
00101       input.readInt( &value );
00102     }
00103 
00104     inline void writeObject( gemfire::DataOutput& output, uint16_t value )
00105     {
00106       output.writeInt( value );
00107     }
00108 
00109     inline void readObject( gemfire::DataInput& input, uint16_t& value )
00110     {
00111       input.readInt( &value );
00112     }
00113 
00114     inline void writeObject( gemfire::DataOutput& output, uint32_t value )
00115     {
00116       output.writeInt( value );
00117     }
00118 
00119     inline void readObject( gemfire::DataInput& input, uint32_t& value )
00120     {
00121       input.readInt( &value );
00122     }
00123 
00124     inline void writeObject( gemfire::DataOutput& output, uint64_t value )
00125     {
00126       output.writeInt( value );
00127     }
00128 
00129     inline void readObject( gemfire::DataInput& input, uint64_t& value )
00130     {
00131       input.readInt( &value );
00132     }
00133 
00134     inline void writeObject( gemfire::DataOutput& output, bool value )
00135     {
00136       output.writeBoolean( value );
00137     }
00138 
00139     inline void readObject( gemfire::DataInput& input, bool& value )
00140     {
00141       input.readBoolean( &value );
00142     }
00143 
00144     inline void writeObject( gemfire::DataOutput& output, double value )
00145     {
00146       output.writeDouble( value );
00147     }
00148 
00149     inline void readObject( gemfire::DataInput& input, double& value )
00150     {
00151       input.readDouble( &value );
00152     }
00153 
00154     inline void writeObject( gemfire::DataOutput& output, float value )
00155     {
00156       output.writeFloat( value );
00157     }
00158 
00159     inline void readObject( gemfire::DataInput& input, float& value )
00160     {
00161       input.readFloat( &value );
00162     }
00163 
00164     inline void writeObject( gemfire::DataOutput& output, wchar_t value )
00165     {
00166       output.writeInt( (int16_t)value );
00167     }
00168 
00169     inline void readObject( gemfire::DataInput& input, wchar_t& value )
00170     {
00171       int16_t val;
00172       input.readInt( &val );
00173       value = val;
00174     }
00175 
00176     inline void writeObject( gemfire::DataOutput& output, const char* value,
00177                              uint32_t length )
00178     {
00179       output.writeASCII( value, length );
00180     }
00181 
00182     template <typename TLen>
00183     inline void readObject( gemfire::DataInput& input, char*& value,
00184                             TLen& length )
00185     {
00186       uint16_t len;
00187       input.readASCII( &value, &len );
00188       length = len;
00189     }
00190 
00191     inline void writeObject( gemfire::DataOutput& output, const char* value )
00192     {
00193       output.writeASCII( value );
00194     }
00195 
00196     inline void readObject( gemfire::DataInput& input, char*& value )
00197     {
00198       input.readASCII( &value );
00199     }
00200 
00201     inline void writeObject( gemfire::DataOutput& output,
00202                              const wchar_t* value, uint32_t length )
00203     {
00204       output.writeUTF( value, length );
00205     }
00206 
00207     template <typename TLen>
00208     inline void readObject( gemfire::DataInput& input, wchar_t*& value,
00209                             TLen& length )
00210     {
00211       uint16_t len;
00212       input.readUTF( &value, &len );
00213       length = len;
00214     }
00215 
00216     inline void writeObject( gemfire::DataOutput& output,
00217                              const wchar_t* value )
00218     {
00219       output.writeUTF( value );
00220     }
00221 
00222     inline void readObject( gemfire::DataInput& input, wchar_t*& value )
00223     {
00224       input.readUTF( &value );
00225     }
00226 
00227 
00228     // Base Serializable types
00229 
00230     template <typename TObj>
00231     inline void writeObject( gemfire::DataOutput& output,
00232                              const gemfire::SharedPtr< TObj >& value,
00233                              gemfire::TypeHelper::yes_type isSerializable )
00234     {
00235       output.writeObject( value );
00236     }
00237 
00238     template <typename TObj>
00239     inline void writeObject( gemfire::DataOutput& output,
00240                              const gemfire::SharedPtr< TObj >& value )
00241     {
00242       writeObject( output, value, GF_TYPE_IS_SERIALIZABLE_TYPE( TObj ) );
00243     }
00244 
00245     template <typename TObj>
00246     inline void readObject( gemfire::DataInput& input,
00247                             gemfire::SharedPtr< TObj >& value,
00248                             gemfire::TypeHelper::yes_type isSerializable )
00249     {
00250       input.readObject(value, true);
00251     }
00252 
00253     template <typename TObj>
00254     inline void readObject( gemfire::DataInput& input,
00255                             gemfire::SharedPtr< TObj >& value )
00256     {
00257       readObject( input, value, GF_TYPE_IS_SERIALIZABLE_TYPE( TObj ) );
00258     }
00259 
00260 
00261     // For arrays
00262 
00263     template <typename TObj, typename TLen>
00264     inline void writeObject( gemfire::DataOutput& output, const TObj* array,
00265                              TLen len )
00266     {
00267       if ( array == NULL ) {
00268         output.write( (int8_t)-1 );
00269       } else {
00270         output.writeArrayLen( len );
00271         const TObj* endArray = array + len;
00272         while ( array < endArray ) {
00273           writeObject( output, *array++ );
00274         }
00275       }
00276     }
00277 
00278     template <typename TObj, typename TLen>
00279     inline void readObject( gemfire::DataInput& input, TObj*& array,
00280                             TLen& len )
00281     {
00282       input.readArrayLen( &len );
00283       if ( len > 0 ) {
00284         GF_NEW( array, TObj[ len ] );
00285         TObj* startArray = array;
00286         TObj* endArray = array + len;
00287         while ( startArray < endArray ) {
00288           readObject( input, *startArray++ );
00289         }
00290       } else {
00291         array = NULL;
00292       }
00293     }
00294 
00295     template <typename TObj, typename TLen>
00296     inline uint32_t objectSize( const TObj* array, TLen len,
00297                                 gemfire::TypeHelper::yes_type isSerializable )
00298     {
00299       uint32_t size = 0;
00300       const TObj* endArray = array + len;
00301       while ( array < endArray ) {
00302         if ( *array != NULL ) {
00303           size += (*array)->objectSize( );
00304         }
00305         array++;
00306       }
00307       size += (uint32_t)( sizeof( TObj ) * len );
00308       return size;
00309     }
00310 
00311     template <typename TObj, typename TLen>
00312     inline uint32_t objectSize( const TObj* array, TLen len,
00313                                 gemfire::TypeHelper::no_type isNotSerializable )
00314     {
00315       return (uint32_t)( sizeof( TObj ) * len );
00316     }
00317 
00318     template <typename TObj, typename TLen>
00319     inline uint32_t objectSize( const TObj* array, TLen len )
00320     {
00321       return objectSize( array, len, GF_TYPE_IS_SERIALIZABLE_TYPE( TObj ) );
00322     }
00323 
00324 
00325     // For containers vector/hashmap/hashset
00326 
00327     template <typename TObj>
00328     inline void writeObject( gemfire::DataOutput& output,
00329         const VectorT< TObj >& value )
00330     {
00331       int32_t len = (int32_t)value.size();
00332       output.writeArrayLen( len );
00333       for ( typename VectorT< TObj >::Iterator iter = value.begin( );
00334           iter != value.end( ); ++iter ) {
00335         writeObject( output, *iter );
00336       }
00337     }
00338 
00339     inline uint32_t objectSize( const _VectorOfCacheable& value )
00340     {
00341       uint32_t objectSize = 0;
00342       for ( _VectorOfCacheable::Iterator iter = value.begin( );
00343           iter != value.end( ); ++iter ) {
00344         if (*iter != NULLPTR) {
00345           objectSize += (*iter)->objectSize( );
00346         }
00347       }
00348       objectSize += (uint32_t)(sizeof(CacheablePtr) * value.size());
00349       return objectSize;
00350     }
00351 
00352     template <typename TObj>
00353     inline void readObject( gemfire::DataInput& input,
00354         VectorT< TObj >& value )
00355     {
00356       int32_t len;
00357       input.readArrayLen( &len );
00358       if ( len >= 0 ) {
00359         TObj obj;
00360         for ( int32_t index = 0; index < len; index++ ) {
00361           readObject( input, obj );
00362           value.push_back( obj );
00363         }
00364       }
00365     }
00366 
00367 
00368     template <typename TKey, typename TValue>
00369     inline void writeObject( gemfire::DataOutput& output,
00370         const HashMapT< TKey, TValue >& value )
00371     {
00372       int32_t len = (int32_t)value.size();
00373       output.writeArrayLen( len );
00374       if ( len > 0 ) {
00375         for ( typename HashMapT< TKey, TValue >::Iterator iter = value.begin( );
00376             iter != value.end( ); ++iter ) {
00377           writeObject( output, iter.first( ) );
00378           writeObject( output, iter.second( ) );
00379         }
00380       }
00381     }
00382 
00383     inline uint32_t objectSize( const _HashMapOfCacheable& value )
00384     {
00385       uint32_t objectSize = 0;
00386       for ( _HashMapOfCacheable::Iterator iter = value.begin( );
00387           iter != value.end( ); ++iter ) {
00388         objectSize += iter.first( )->objectSize( );
00389         if (iter.second( ) != NULLPTR) {
00390           objectSize += iter.second( )->objectSize( );
00391         }
00392       }
00393       objectSize += (uint32_t)( ( sizeof( CacheableKeyPtr )
00394           + sizeof( CacheablePtr ) ) * value.size());
00395       return objectSize;
00396     }
00397 
00398     template <typename TKey, typename TValue>
00399     inline void readObject( gemfire::DataInput& input,
00400         HashMapT< TKey, TValue >& value )
00401     {
00402       int32_t len;
00403       input.readArrayLen( &len );
00404       if ( len > 0 ) {
00405         TKey key;
00406         TValue val;
00407         for( int32_t index = 0; index < len; index++ ) {
00408           readObject( input, key );
00409           readObject( input, val );
00410           value.insert( key, val );
00411         }
00412       }
00413     }
00414 
00415 
00416     template <typename TKey>
00417     inline void writeObject( gemfire::DataOutput& output,
00418       const HashSetT< TKey >& value )
00419     {
00420       int32_t len = (int32_t)value.size();
00421       output.writeArrayLen( len );
00422       for ( typename HashSetT< TKey >::Iterator iter = value.begin( );
00423         iter != value.end( ); ++iter ) {
00424         writeObject( output, *iter );
00425       }
00426     }
00427 
00428     inline uint32_t objectSize( const _HashSetOfCacheableKey& value )
00429     {
00430       uint32_t objectSize = 0;
00431       for ( _HashSetOfCacheableKey::Iterator iter = value.begin( );
00432         iter != value.end( ); ++iter ) {
00433         if (*iter != NULLPTR) {
00434           objectSize += (*iter)->objectSize( );
00435         }
00436       }
00437       objectSize += (uint32_t)(sizeof(CacheableKeyPtr) * value.size());
00438       return objectSize;
00439     }
00440 
00441     template <typename TKey>
00442     inline void readObject( gemfire::DataInput& input,
00443         HashSetT< TKey >& value )
00444     {
00445       int32_t len;
00446       input.readArrayLen( &len );
00447       if ( len > 0 ) {
00448         TKey key;
00449         for( int32_t index = 0; index < len; index++ ) {
00450           readObject( input, key );
00451           value.insert( key );
00452         }
00453       }
00454     }
00455 
00456 
00457 
00458     // Default value for builtin types
00459 
00460     template <typename TObj>
00461     inline TObj zeroObject( )
00462     {
00463       return 0;
00464     }
00465 
00466     template <>
00467     inline bool zeroObject<bool>( )
00468     {
00469       return false;
00470     }
00471 
00472     template <>
00473     inline double zeroObject<double>( )
00474     {
00475       return 0.0;
00476     }
00477 
00478     template <>
00479     inline float zeroObject<float>( )
00480     {
00481       return 0.0F;
00482     }
00483 
00484   }
00485 }
00486 
00487 
00488 #endif // _GEMFIRE_SERIALIZER_HPP_

GemFire C++ Cache API Documentation