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