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 size += (*array)->objectSize( );
00301 array++;
00302 }
00303 return size;
00304 }
00305
00306 template <typename TObj, typename TLen>
00307 inline uint32_t objectSize( const TObj* array, TLen len,
00308 gemfire::TypeHelper::no_type isNotSerializable )
00309 {
00310 return ( sizeof( TObj ) * len );
00311 }
00312
00313 template <typename TObj, typename TLen>
00314 inline uint32_t objectSize( const TObj* array, TLen len )
00315 {
00316 return objectSize( array, len, GF_TYPE_IS_SERIALIZABLE_TYPE( TObj ) );
00317 }
00318
00319
00320
00321
00322 template <typename TObj>
00323 inline void writeObject( gemfire::DataOutput& output,
00324 const VectorT< TObj >& value )
00325 {
00326 int32_t len = (int32_t)value.length( );
00327 output.writeInt( len );
00328 for ( typename VectorT< TObj >::Iterator iter = value.begin( );
00329 iter != value.end( ); ++iter ) {
00330 writeObject( output, *iter );
00331 }
00332 }
00333
00334 inline uint32_t objectSize( const VectorOfCacheable& value )
00335 {
00336 uint32_t objectSize = 0;
00337 for ( VectorOfCacheable::Iterator iter = value.begin( );
00338 iter != value.end( ); ++iter ) {
00339 objectSize += (*iter)->objectSize( );
00340 }
00341 return objectSize;
00342 }
00343
00344 template <typename TObj>
00345 inline void readObject( gemfire::DataInput& input,
00346 VectorT< TObj >& value )
00347 {
00348 int32_t len;
00349 input.readInt( &len );
00350 if ( len >= 0 ) {
00351 TObj obj;
00352 for ( int32_t index = 0; index < len; index++ ) {
00353 readObject( input, obj );
00354 value.push_back( obj );
00355 }
00356 }
00357 }
00358
00359
00360 template <typename TKey, typename TValue>
00361 inline void writeObject( gemfire::DataOutput& output,
00362 const HashMapT< TKey, TValue >& value )
00363 {
00364 int32_t len = (int32_t)value.length( );
00365 output.writeInt( len );
00366 if ( len > 0 ) {
00367 for ( typename HashMapT< TKey, TValue >::Iterator iter = value.begin( );
00368 iter != value.end( ); ++iter ) {
00369 writeObject( output, iter.first( ) );
00370 writeObject( output, iter.second( ) );
00371 }
00372 }
00373 }
00374
00375 inline uint32_t objectSize( const HashMapOfCacheable& value )
00376 {
00377 uint32_t objectSize = 0;
00378 for ( HashMapOfCacheable::Iterator iter = value.begin( );
00379 iter != value.end( ); ++iter ) {
00380 objectSize += iter.first( )->objectSize( );
00381 objectSize += iter.second( )->objectSize( );
00382 }
00383 return objectSize;
00384 }
00385
00386 template <typename TKey, typename TValue>
00387 inline void readObject( gemfire::DataInput& input,
00388 HashMapT< TKey, TValue >& value )
00389 {
00390 int32_t len;
00391 input.readInt( &len );
00392 if ( len > 0 ) {
00393 TKey key;
00394 TValue val;
00395 for( int32_t index = 0; index < len; index++ ) {
00396 readObject( input, key );
00397 readObject( input, val );
00398 value.insert( key, val );
00399 }
00400 }
00401 }
00402
00403
00404 template <typename TKey>
00405 inline void writeObject( gemfire::DataOutput& output,
00406 const HashSetT< TKey >& value )
00407 {
00408 int32_t len = (int32_t)value.length( );
00409 output.writeInt( len );
00410 for ( typename HashSetT< TKey >::Iterator iter = value.begin( );
00411 iter != value.end( ); ++iter ) {
00412 writeObject( output, *iter );
00413 }
00414 }
00415
00416 inline uint32_t objectSize( const HashSetOfCacheableKey& value )
00417 {
00418 uint32_t objectSize = 0;
00419 for ( HashSetOfCacheableKey::Iterator iter = value.begin( );
00420 iter != value.end( ); ++iter ) {
00421 objectSize += (*iter)->objectSize( );
00422 }
00423 return objectSize;
00424 }
00425
00426 template <typename TKey>
00427 inline void readObject( gemfire::DataInput& input,
00428 HashSetT< TKey >& value )
00429 {
00430 int32_t len;
00431 input.readInt( &len );
00432 if ( len > 0 ) {
00433 TKey key;
00434 for( int32_t index = 0; index < len; index++ ) {
00435 readObject( input, key );
00436 value.insert( key );
00437 }
00438 }
00439 }
00440
00441
00442
00443
00444
00445 template <typename TObj>
00446 inline TObj zeroObject( )
00447 {
00448 return 0;
00449 }
00450
00451 template <>
00452 inline bool zeroObject<bool>( )
00453 {
00454 return false;
00455 }
00456
00457 template <>
00458 inline double zeroObject<double>( )
00459 {
00460 return 0.0;
00461 }
00462
00463 template <>
00464 inline float zeroObject<float>( )
00465 {
00466 return 0.0F;
00467 }
00468
00469 }
00470 }
00471
00472
00473 #endif // _GEMFIRE_SERIALIZER_HPP_