00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef _CONV_H
00010 #define _CONV_H
00011
00012 #include <string>
00013
00029 static const unsigned int UnreasonablyLargeArray = 1000000;
00030 template< class T > class Conv
00031 {
00032 public:
00038 static unsigned int size( const T& val )
00039 {
00040 return 1 + ( sizeof( T ) - 1 ) / sizeof( double );
00041 }
00042
00043
00044 static const T& buf2val( double** buf ) {
00045 const T* ret = reinterpret_cast< const T* >( *buf );
00046 *buf += size( *ret );
00047 return *ret;
00048 }
00049
00056 static void val2buf( const T& val, double** buf ) {
00057 *reinterpret_cast< T* >( *buf ) = val;
00058 *buf += size( val );
00059 }
00060
00065 static void str2val( T& val, const string& s ) {
00066 istringstream is( s );
00067 is >> val;
00068 }
00069
00074 static void val2str( string& s, const T& val ) {
00075 stringstream ss;
00076 ss << val;
00077 s = ss.str();
00078
00079
00080 }
00081
00082 static string rttiType() {
00083 if ( typeid( T ) == typeid( char ))
00084 return "char";
00085 if ( typeid( T ) == typeid( int ) )
00086 return "int";
00087 if ( typeid( T ) == typeid( short ) )
00088 return "short";
00089 if ( typeid( T ) == typeid( long ) )
00090 return "long";
00091 if ( typeid( T ) == typeid( unsigned int ) )
00092 return "unsigned int";
00093 if ( typeid( T ) == typeid( unsigned long ) )
00094 return "unsigned long";
00095 if ( typeid( T ) == typeid( float ) )
00096 return "float";
00097 if ( typeid( T ) == typeid( double ) )
00098 return "double";
00099 if ( typeid( T ) == typeid( Id ) )
00100 return "Id";
00101 if ( typeid( T ) == typeid( ObjId ) )
00102 return "ObjId";
00103 return typeid( T ).name();
00104 }
00105
00106 private:
00107 };
00108
00113 template<> class Conv< string >
00114 {
00115 public:
00122 static unsigned int size( const string& val )
00123 {
00124
00125 return 1 + val.length() / sizeof( double );
00126 }
00127
00128
00129
00130 static const string buf2val( double** buf ) {
00131 static string ret;
00132 ret = reinterpret_cast< const char* >( *buf );
00133 *buf += size( ret );
00134 return ret;
00135 }
00136
00143 static void val2buf( const string& val, double** buf ) {
00144 char* temp = reinterpret_cast< char* >( *buf );
00145 strcpy( temp, val.c_str() );
00146 *buf += size( val );
00147 }
00148
00149 static void str2val( string& val, const string& s ) {
00150 val = s;
00151 }
00152
00153 static void val2str( string& s, const string& val ) {
00154 s = val;
00155 }
00156
00157 static string rttiType() {
00158 return "string";
00159 }
00160 private:
00161 };
00162
00167 template<> class Conv< double >
00168 {
00169 public:
00173 static unsigned int size( double val )
00174 {
00175 return 1;
00176 }
00177
00178 static const double buf2val( double** buf ) {
00179 double ret = **buf;
00180 (*buf)++;
00181 return ret;
00182 }
00183 static void val2buf( double val, double** buf ) {
00184 **buf = val;
00185 (*buf)++;
00186 }
00187
00188 static void str2val( double &val, const string& s ) {
00189 val = atof( s.c_str() );
00190 }
00191
00192 static void val2str( string& s, double val ) {
00193 stringstream ss;
00194 ss << val;
00195 s = ss.str();
00196 }
00197
00198 static string rttiType() {
00199 return "double";
00200 }
00201 private:
00202 };
00203
00208 template<> class Conv< float >
00209 {
00210 public:
00214 static unsigned int size( float val )
00215 {
00216 return 1;
00217 }
00218
00219 static const float buf2val( double** buf ) {
00220 float ret = **buf;
00221 (*buf)++;
00222 return ret;
00223 }
00224 static void val2buf( float val, double** buf ) {
00225 **buf = val;
00226 (*buf)++;
00227 }
00228
00229 static void str2val( float& val, const string& s ) {
00230 val = atof( s.c_str() );
00231 }
00232
00233 static void val2str( string& s, float val ) {
00234 stringstream ss;
00235 ss << val;
00236 s = ss.str();
00237 }
00238
00239 static string rttiType() {
00240 return "float";
00241 }
00242 private:
00243 };
00244
00249 template<> class Conv< unsigned int >
00250 {
00251 public:
00255 static unsigned int size( int val )
00256 {
00257 return 1;
00258 }
00259
00260 static const unsigned int buf2val( double** buf ) {
00261 unsigned int ret = **buf;
00262 (*buf)++;
00263 return ret;
00264 }
00265 static void val2buf( unsigned int val, double** buf ) {
00266 **buf = val;
00267 (*buf)++;
00268 }
00269
00270 static void str2val( unsigned int& val, const string& s ) {
00271 val = atoi( s.c_str() );
00272 }
00273
00274 static void val2str( string& s, unsigned int val ) {
00275 stringstream ss;
00276 ss << val;
00277 s = ss.str();
00278 }
00279
00280 static string rttiType() {
00281 return "unsigned int";
00282 }
00283 private:
00284 };
00285
00290 template<> class Conv< int >
00291 {
00292 public:
00296 static unsigned int size( int val )
00297 {
00298 return 1;
00299 }
00300
00301 static const int buf2val( double** buf ) {
00302 int ret = **buf;
00303 (*buf)++;
00304 return ret;
00305 }
00306 static void val2buf( int val, double** buf ) {
00307 **buf = val;
00308 (*buf)++;
00309 }
00310
00311 static void str2val( int& val, const string& s ) {
00312 val = atoi( s.c_str() );
00313 }
00314
00315 static void val2str( string& s, int val ) {
00316 stringstream ss;
00317 ss << val;
00318 s = ss.str();
00319 }
00320
00321 static string rttiType() {
00322 return "int";
00323 }
00324 private:
00325 };
00326
00327 template<> class Conv< unsigned short >
00328 {
00329 public:
00333 static unsigned int size( unsigned short val )
00334 {
00335 return 1;
00336 }
00337
00338 static const unsigned short buf2val( double** buf ) {
00339 unsigned short ret = **buf;
00340 (*buf)++;
00341 return ret;
00342 }
00343 static void val2buf( unsigned short val, double** buf ) {
00344 **buf = val;
00345 (*buf)++;
00346 }
00347
00348 static void str2val( unsigned short& val, const string& s ) {
00349 val = atoi( s.c_str() );
00350 }
00351
00352 static void val2str( string& s, unsigned short val ) {
00353 stringstream ss;
00354 ss << val;
00355 s = ss.str();
00356 }
00357
00358 static string rttiType() {
00359 return "unsigned short";
00360 }
00361 private:
00362 };
00363
00364 template<> class Conv< short >
00365 {
00366 public:
00370 static unsigned int size( short val )
00371 {
00372 return 1;
00373 }
00374
00375 static const short buf2val( double** buf ) {
00376 short ret = **buf;
00377 (*buf)++;
00378 return ret;
00379 }
00380 static void val2buf( short val, double** buf ) {
00381 **buf = val;
00382 (*buf)++;
00383 }
00384
00385 static void str2val( short& val, const string& s ) {
00386 val = atoi( s.c_str() );
00387 }
00388
00389 static void val2str( string& s, short val ) {
00390 stringstream ss;
00391 ss << val;
00392 s = ss.str();
00393 }
00394
00395 static string rttiType() {
00396 return "short";
00397 }
00398 private:
00399 };
00400
00401 template<> class Conv< bool >
00402 {
00403 public:
00407 static unsigned int size( bool val )
00408 {
00409 return 1;
00410 }
00411
00412 static const bool buf2val( double** buf ) {
00413 bool ret = (**buf > 0.5);
00414 (*buf)++;
00415 return ret;
00416 }
00417 static void val2buf( bool val, double** buf ) {
00418 **buf = val;
00419 (*buf)++;
00420 }
00421
00422 static void str2val( bool& val, const string& s ) {
00423 if ( s == "0" || s == "false" || s == "False" )
00424 val = 0;
00425 else
00426 val = 1;
00427 }
00428
00429 static void val2str( string& s, bool val ) {
00430 if ( val > 0.5 )
00431 s = "1";
00432 else
00433 s = "0";
00434 }
00435
00436 static string rttiType() {
00437 return "bool";
00438 }
00439 private:
00440 };
00441
00448 template<> class Conv< Id >
00449 {
00450 public:
00454 static unsigned int size( Id val )
00455 {
00456 return 1;
00457 }
00458
00459 static const Id buf2val( double** buf ) {
00460 Id ret( **buf );
00461 (*buf)++;
00462 return ret;
00463 }
00464 static void val2buf( Id id, double** buf ) {
00465 **buf = id.value();
00466 (*buf)++;
00467 }
00468
00469 static void str2val( Id& val, const string& s ) {
00470 Id temp( s );
00471 val = temp;
00472 }
00473
00474 static void val2str( string& s, Id val ) {
00475 s = val.path();
00476 }
00477
00478 static string rttiType() {
00479 return "Id";
00480 }
00481 private:
00482 };
00483
00497 template< class T > class Conv< vector< vector< T > > >
00498 {
00499 public:
00500 static unsigned int size( const vector< vector < T > > & val)
00501 {
00502 unsigned int ret = 1 + val.size();
00503 for ( unsigned int i = 0; i < val.size(); ++i ) {
00504 if ( val[i].size() > 0 ) {
00505 ret += val[i].size() * Conv< T >::size( val[i][0] );
00506 } else {
00507 T temp = T();
00508 ret += val[i].size() * Conv< T >::size( temp );
00509 }
00510 }
00511 return ret;
00512 }
00513
00514 static const vector< vector< T > > buf2val( double** buf )
00515 {
00516 static vector< vector< T > > ret;
00517 ret.clear();
00518 unsigned int numEntries = **buf;
00519 ret.resize( numEntries );
00520 (*buf)++;
00521 for ( unsigned int i = 0; i < numEntries; ++i ) {
00522 unsigned int rowSize = **buf;
00523 (*buf)++;
00524 for ( unsigned int j = 0; j < rowSize; ++j )
00525 ret[i].push_back( Conv< T >::buf2val( buf ) );
00526 }
00527 return ret;
00528 }
00529
00530 static void val2buf( const vector< vector< T > >& val, double**buf )
00531 {
00532 double* temp = *buf;
00533 *temp++ = val.size();
00534 for( unsigned int i = 0; i < val.size(); ++i ) {
00535 *temp++ = val[i].size();
00536 for ( unsigned int j = 0; j < val[i].size(); ++j ) {
00537 Conv< T >::val2buf( val[i][j], &temp );
00538 }
00539 }
00540 *buf = temp;
00541 }
00542
00543 static void str2val( vector< vector< T > >& val, const string& s ) {
00544 cout << "Specialized Conv< vector< vector< T > > >::str2val not done\n";
00545 }
00546
00547 static void val2str( string& s, const vector< vector< T > >& val ) {
00548 cout << "Specialized Conv< vector< vector< T > > >::val2str not done\n";
00549 }
00550 static string rttiType() {
00551 string ret = "vector< vector<" + Conv< T >::rttiType() + "> >";
00552 return ret;
00553 }
00554 private:
00555 };
00556
00564 template< class T > class Conv< vector< T > >
00565 {
00566 public:
00570 static unsigned int size( const vector< T >& val )
00571 {
00572 unsigned int ret = 1;
00573 for ( unsigned int i = 0; i < val.size(); ++i ) {
00574 ret += Conv< T >::size( val[i] );
00575 }
00576 return ret;
00577 }
00578
00579 static const vector< T > buf2val( double** buf )
00580 {
00581 static vector< T > ret;
00582 ret.clear();
00583 unsigned int numEntries = **buf;
00584 (*buf)++;
00585 for ( unsigned int i = 0; i < numEntries; ++i )
00586 ret.push_back( Conv< T >::buf2val( buf ) );
00587 return ret;
00588 }
00589
00590 static void val2buf( const vector< T >& val, double**buf )
00591 {
00592 double* temp = *buf;
00593 *temp++ = val.size();
00594 for( unsigned int i = 0; i < val.size(); ++i ) {
00595 Conv< T >::val2buf( val[i], &temp );
00596 }
00597 *buf = temp;
00598 }
00599
00600 static void str2val( vector< T >& val, const string& s ) {
00601 cout << "Specialized Conv< vector< T > >::str2val not done\n";
00602 }
00603
00604 static void val2str( string& s, const vector< T >& val ) {
00605 cout << "Specialized Conv< vector< T > >::val2str not done\n";
00606 }
00607 static string rttiType() {
00608 string ret = "vector<" + Conv< T >::rttiType() + ">";
00609 return ret;
00610 }
00611 private:
00612 };
00613
00614 #endif // _CONV_H