MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Conv.h
Go to the documentation of this file.
1 /**********************************************************************
2  ** This program is part of 'MOOSE', the
3  ** Messaging Object Oriented Simulation Environment.
4  ** Copyright (C) 2003-2009 Upinder S. Bhalla. and NCBS
5  ** It is made available under the terms of the
6  ** GNU Lesser General Public License version 2.1
7  ** See the file COPYING.LIB for the full notice.
8  **********************************************************************/
9 #ifndef _CONV_H
10 #define _CONV_H
11 
12 #include <string>
13 
29 static const unsigned int UnreasonablyLargeArray = 1000000;
30 template< class T > class Conv
31 {
32  public:
38  static unsigned int size( const T& val )
39  {
40  return 1 + ( sizeof( T ) - 1 ) / sizeof( double );
41  }
42 
43 
44  static const T& buf2val( double** buf ) {
45  const T* ret = reinterpret_cast< const T* >( *buf );
46  *buf += size( *ret );
47  return *ret;
48  }
49 
56  static void val2buf( const T& val, double** buf ) {
57  *reinterpret_cast< T* >( *buf ) = val;
58  *buf += size( val );
59  }
60 
65  static void str2val( T& val, const string& s ) {
66  istringstream is( s );
67  is >> val;
68  }
69 
74  static void val2str( string& s, const T& val ) {
75  stringstream ss;
76  ss << val;
77  s = ss.str();
78  // ostringstream os( s );
79  // os << val;
80  }
81 
82  static string rttiType() {
83  if ( typeid( T ) == typeid( char ))
84  return "char";
85  if ( typeid( T ) == typeid( int ) )
86  return "int";
87  if ( typeid( T ) == typeid( short ) )
88  return "short";
89  if ( typeid( T ) == typeid( long ) )
90  return "long";
91  if ( typeid( T ) == typeid( unsigned int ) )
92  return "unsigned int";
93  if ( typeid( T ) == typeid( unsigned long ) )
94  return "unsigned long";
95  if ( typeid( T ) == typeid( float ) )
96  return "float";
97  if ( typeid( T ) == typeid( double ) )
98  return "double";
99  if ( typeid( T ) == typeid( Id ) )
100  return "Id";
101  if ( typeid( T ) == typeid( ObjId ) )
102  return "ObjId";
103  return typeid( T ).name(); // this is not portable but may be more useful than "bad"
104  }
105 
106  private:
107 };
108 
113 template<> class Conv< string >
114 {
115  public:
122  static unsigned int size( const string& val )
123  {
124  // extra char for termination of the string.
125  return 1 + val.length() / sizeof( double );
126  }
127 
128  // The return cannot be a reference, because the function may
129  // be called many times in succession.
130  static const string buf2val( double** buf ) {
131  static string ret;
132  ret = reinterpret_cast< const char* >( *buf );
133  *buf += size( ret );
134  return ret;
135  }
136 
143  static void val2buf( const string& val, double** buf ) {
144  char* temp = reinterpret_cast< char* >( *buf );
145  strcpy( temp, val.c_str() );
146  *buf += size( val );
147  }
148 
149  static void str2val( string& val, const string& s ) {
150  val = s;
151  }
152 
153  static void val2str( string& s, const string& val ) {
154  s = val;
155  }
156 
157  static string rttiType() {
158  return "string";
159  }
160  private:
161 };
162 
167 template<> class Conv< double >
168 {
169  public:
173  static unsigned int size( double val )
174  {
175  return 1;
176  }
177 
178  static const double buf2val( double** buf ) {
179  double ret = **buf;
180  (*buf)++;
181  return ret;
182  }
183  static void val2buf( double val, double** buf ) {
184  **buf = val;
185  (*buf)++;
186  }
187 
188  static void str2val( double &val, const string& s ) {
189  val = atof( s.c_str() );
190  }
191 
192  static void val2str( string& s, double val ) {
193  stringstream ss;
194  ss << val;
195  s = ss.str();
196  }
197 
198  static string rttiType() {
199  return "double";
200  }
201  private:
202 };
203 
208 template<> class Conv< float >
209 {
210  public:
214  static unsigned int size( float val )
215  {
216  return 1;
217  }
218 
219  static const float buf2val( double** buf ) {
220  float ret = **buf;
221  (*buf)++;
222  return ret;
223  }
224  static void val2buf( float val, double** buf ) {
225  **buf = val;
226  (*buf)++;
227  }
228 
229  static void str2val( float& val, const string& s ) {
230  val = atof( s.c_str() );
231  }
232 
233  static void val2str( string& s, float val ) {
234  stringstream ss;
235  ss << val;
236  s = ss.str();
237  }
238 
239  static string rttiType() {
240  return "float";
241  }
242  private:
243 };
244 
249 template<> class Conv< unsigned int >
250 {
251  public:
255  static unsigned int size( int val )
256  {
257  return 1;
258  }
259 
260  static const unsigned int buf2val( double** buf ) {
261  unsigned int ret = (unsigned int)**buf;
262  (*buf)++;
263  return ret;
264  }
265  static void val2buf( unsigned int val, double** buf ) {
266  **buf = val;
267  (*buf)++;
268  }
269 
270  static void str2val( unsigned int& val, const string& s ) {
271  val = atoi( s.c_str() );
272  }
273 
274  static void val2str( string& s, unsigned int val ) {
275  stringstream ss;
276  ss << val;
277  s = ss.str();
278  }
279 
280  static string rttiType() {
281  return "unsigned int";
282  }
283  private:
284 };
285 
290 template<> class Conv< int >
291 {
292  public:
296  static unsigned int size( int val )
297  {
298  return 1;
299  }
300 
301  static const int buf2val( double** buf ) {
302  int ret = (int)**buf;
303  (*buf)++;
304  return ret;
305  }
306  static void val2buf( int val, double** buf ) {
307  **buf = val;
308  (*buf)++;
309  }
310 
311  static void str2val( int& val, const string& s ) {
312  val = atoi( s.c_str() );
313  }
314 
315  static void val2str( string& s, int val ) {
316  stringstream ss;
317  ss << val;
318  s = ss.str();
319  }
320 
321  static string rttiType() {
322  return "int";
323  }
324  private:
325 };
326 
327 template<> class Conv< unsigned short >
328 {
329  public:
333  static unsigned int size( unsigned short val )
334  {
335  return 1;
336  }
337 
338  static const unsigned short buf2val( double** buf ) {
339  unsigned short ret = (unsigned int)**buf;
340  (*buf)++;
341  return ret;
342  }
343  static void val2buf( unsigned short val, double** buf ) {
344  **buf = (double)val;
345  (*buf)++;
346  }
347 
348  static void str2val( unsigned short& val, const string& s ) {
349  val = atoi( s.c_str() );
350  }
351 
352  static void val2str( string& s, unsigned short val ) {
353  stringstream ss;
354  ss << val;
355  s = ss.str();
356  }
357 
358  static string rttiType() {
359  return "unsigned short";
360  }
361  private:
362 };
363 
364 template<> class Conv< short >
365 {
366  public:
370  static unsigned int size( short val )
371  {
372  return 1;
373  }
374 
375  static const short buf2val( double** buf ) {
376  short ret = (short)**buf;
377  (*buf)++;
378  return ret;
379  }
380  static void val2buf( short val, double** buf ) {
381  **buf = val;
382  (*buf)++;
383  }
384 
385  static void str2val( short& val, const string& s ) {
386  val = atoi( s.c_str() );
387  }
388 
389  static void val2str( string& s, short val ) {
390  stringstream ss;
391  ss << val;
392  s = ss.str();
393  }
394 
395  static string rttiType() {
396  return "short";
397  }
398  private:
399 };
400 
401 template<> class Conv< bool >
402 {
403  public:
407  static unsigned int size( bool val )
408  {
409  return 1;
410  }
411 
412  static const bool buf2val( double** buf ) {
413  bool ret = (**buf > 0.5);
414  (*buf)++;
415  return ret;
416  }
417  static void val2buf( bool val, double** buf ) {
418  **buf = val;
419  (*buf)++;
420  }
421 
422  static void str2val( bool& val, const string& s ) {
423  if ( s == "0" || s == "false" || s == "False" )
424  val = 0;
425  else
426  val = 1;
427  }
428 
429  static void val2str( string& s, bool val ) {
430  if ( val > 0.5 )
431  s = "1";
432  else
433  s = "0";
434  }
435 
436  static string rttiType() {
437  return "bool";
438  }
439  private:
440 };
441 
448 template<> class Conv< Id >
449 {
450  public:
454  static unsigned int size( Id val )
455  {
456  return 1;
457  }
458 
459  static const Id buf2val( double** buf ) {
460  Id ret( (unsigned int)**buf );
461  (*buf)++;
462  return ret;
463  }
464  static void val2buf( Id id, double** buf ) {
465  **buf = id.value();
466  (*buf)++;
467  }
468 
469  static void str2val( Id& val, const string& s ) {
470  Id temp( s ); // converts the path
471  val = temp;
472  }
473 
474  static void val2str( string& s, Id val ) {
475  s = val.path();
476  }
477 
478  static string rttiType() {
479  return "Id";
480  }
481  private:
482 };
483 
497 template< class T > class Conv< vector< vector< T > > >
498 {
499  public:
500  static unsigned int size( const vector< vector < T > > & val)
501  {
502  unsigned int ret = 1 + val.size();
503  for ( unsigned int i = 0; i < val.size(); ++i ) {
504  if ( val[i].size() > 0 ) {
505  ret += val[i].size() * Conv< T >::size( val[i][0] );
506  } else {
507  T temp = T();
508  ret += val[i].size() * Conv< T >::size( temp );
509  }
510  }
511  return ret;
512  }
513 
514  static const vector< vector< T > > buf2val( double** buf )
515  {
516  static vector< vector< T > > ret;
517  ret.clear();
518  unsigned int numEntries = **buf; // first entry is vec size
519  ret.resize( numEntries );
520  (*buf)++;
521  for ( unsigned int i = 0; i < numEntries; ++i ) {
522  unsigned int rowSize = **buf;
523  (*buf)++;
524  for ( unsigned int j = 0; j < rowSize; ++j )
525  ret[i].push_back( Conv< T >::buf2val( buf ) );
526  }
527  return ret;
528  }
529 
530  static void val2buf( const vector< vector< T > >& val, double**buf )
531  {
532  double* temp = *buf;
533  *temp++ = val.size();
534  for( unsigned int i = 0; i < val.size(); ++i ) {
535  *temp++ = val[i].size();
536  for ( unsigned int j = 0; j < val[i].size(); ++j ) {
537  Conv< T >::val2buf( val[i][j], &temp );
538  }
539  }
540  *buf = temp;
541  }
542 
543  static void str2val( vector< vector< T > >& val, const string& s ) {
544  cout << "Specialized Conv< vector< vector< T > > >::str2val not done\n";
545  }
546 
547  static void val2str( string& s, const vector< vector< T > >& val ) {
548  cout << "Specialized Conv< vector< vector< T > > >::val2str not done\n";
549  }
550  static string rttiType() {
551  string ret = "vector< vector<" + Conv< T >::rttiType() + "> >";
552  return ret;
553  }
554  private:
555 };
556 
564 template< class T > class Conv< vector< T > >
565 {
566  public:
570  static unsigned int size( const vector< T >& val )
571  {
572  unsigned int ret = 1;
573  for ( unsigned int i = 0; i < val.size(); ++i ) {
574  ret += Conv< T >::size( val[i] );
575  }
576  return ret;
577  }
578 
579  static const vector< T > buf2val( double** buf )
580  {
581  static vector< T > ret;
582  ret.clear();
583  unsigned int numEntries = (unsigned int)**buf; // first entry is vec size
584  (*buf)++;
585  for ( unsigned int i = 0; i < numEntries; ++i )
586  ret.push_back( Conv< T >::buf2val( buf ) );
587  return ret;
588  }
589 
590  static void val2buf( const vector< T >& val, double**buf )
591  {
592  double* temp = *buf;
593  *temp++ = val.size();
594  for( unsigned int i = 0; i < val.size(); ++i ) {
595  Conv< T >::val2buf( val[i], &temp );
596  }
597  *buf = temp;
598  }
599 
600  static void str2val( vector< T >& val, const string& s ) {
601  cout << "Specialized Conv< vector< T > >::str2val not done\n";
602  }
603 
604  static void val2str( string& s, const vector< T >& val ) {
605  cout << "Specialized Conv< vector< T > >::val2str not done\n";
606  }
607  static string rttiType() {
608  string ret = "vector<" + Conv< T >::rttiType() + ">";
609  return ret;
610  }
611  private:
612 };
613 
614 #endif // _CONV_H
static void val2buf(Id id, double **buf)
Definition: Conv.h:464
static string rttiType()
Definition: Conv.h:550
static string rttiType()
Definition: Conv.h:239
static void str2val(short &val, const string &s)
Definition: Conv.h:385
static void str2val(vector< vector< T > > &val, const string &s)
Definition: Conv.h:543
static void val2buf(const T &val, double **buf)
Definition: Conv.h:56
static unsigned int size(bool val)
Definition: Conv.h:407
static unsigned int size(const string &val)
Definition: Conv.h:122
static const unsigned short buf2val(double **buf)
Definition: Conv.h:338
static void val2str(string &s, short val)
Definition: Conv.h:389
static void str2val(vector< T > &val, const string &s)
Definition: Conv.h:600
std::string path(const std::string &separator="/") const
Definition: Id.cpp:76
static void val2str(string &s, unsigned int val)
Definition: Conv.h:274
static void val2str(string &s, const vector< T > &val)
Definition: Conv.h:604
static void val2buf(unsigned short val, double **buf)
Definition: Conv.h:343
static string rttiType()
Definition: Conv.h:198
static const vector< T > buf2val(double **buf)
Definition: Conv.h:579
static void val2str(string &s, float val)
Definition: Conv.h:233
static const int buf2val(double **buf)
Definition: Conv.h:301
static void val2buf(short val, double **buf)
Definition: Conv.h:380
static const double buf2val(double **buf)
Definition: Conv.h:178
static string rttiType()
Definition: Conv.h:436
Definition: ObjId.h:20
static void val2buf(const vector< T > &val, double **buf)
Definition: Conv.h:590
static const short buf2val(double **buf)
Definition: Conv.h:375
const int numEntries
Definition: proc.cpp:60
static string rttiType()
Definition: Conv.h:157
static const float buf2val(double **buf)
Definition: Conv.h:219
static void val2str(string &s, unsigned short val)
Definition: Conv.h:352
Definition: Conv.h:30
static const vector< vector< T > > buf2val(double **buf)
Definition: Conv.h:514
static void val2str(string &s, Id val)
Definition: Conv.h:474
static void val2buf(const vector< vector< T > > &val, double **buf)
Definition: Conv.h:530
static void str2val(Id &val, const string &s)
Definition: Conv.h:469
static void str2val(string &val, const string &s)
Definition: Conv.h:149
static void str2val(int &val, const string &s)
Definition: Conv.h:311
static string rttiType()
Definition: Conv.h:395
static void val2str(string &s, const vector< vector< T > > &val)
Definition: Conv.h:547
static unsigned int size(const T &val)
Definition: Conv.h:38
static void val2str(string &s, double val)
Definition: Conv.h:192
static const unsigned int UnreasonablyLargeArray
Definition: Conv.h:29
static void val2str(string &s, int val)
Definition: Conv.h:315
static string rttiType()
Definition: Conv.h:321
static void str2val(double &val, const string &s)
Definition: Conv.h:188
static string rttiType()
Definition: Conv.h:607
static void str2val(float &val, const string &s)
Definition: Conv.h:229
static void val2buf(int val, double **buf)
Definition: Conv.h:306
static const unsigned int buf2val(double **buf)
Definition: Conv.h:260
static void str2val(bool &val, const string &s)
Definition: Conv.h:422
static void str2val(T &val, const string &s)
Definition: Conv.h:65
static void str2val(unsigned short &val, const string &s)
Definition: Conv.h:348
static void val2buf(bool val, double **buf)
Definition: Conv.h:417
static void val2str(string &s, bool val)
Definition: Conv.h:429
static unsigned int size(int val)
Definition: Conv.h:255
static unsigned int size(unsigned short val)
Definition: Conv.h:333
static char name[]
Definition: mfield.cpp:401
static unsigned int size(Id val)
Definition: Conv.h:454
static unsigned int size(short val)
Definition: Conv.h:370
static const T & buf2val(double **buf)
Definition: Conv.h:44
static string rttiType()
Definition: Conv.h:82
static void val2buf(float val, double **buf)
Definition: Conv.h:224
Definition: Id.h:17
static void val2buf(double val, double **buf)
Definition: Conv.h:183
static void val2str(string &s, const T &val)
Definition: Conv.h:74
static unsigned int size(float val)
Definition: Conv.h:214
static unsigned int size(int val)
Definition: Conv.h:296
static const bool buf2val(double **buf)
Definition: Conv.h:412
static void str2val(unsigned int &val, const string &s)
Definition: Conv.h:270
static string rttiType()
Definition: Conv.h:478
static string rttiType()
Definition: Conv.h:358
static const Id buf2val(double **buf)
Definition: Conv.h:459
static string rttiType()
Definition: Conv.h:280
static void val2buf(unsigned int val, double **buf)
Definition: Conv.h:265
static unsigned int size(const vector< vector< T > > &val)
Definition: Conv.h:500
static void val2str(string &s, const string &val)
Definition: Conv.h:153
static void val2buf(const string &val, double **buf)
Definition: Conv.h:143
static const string buf2val(double **buf)
Definition: Conv.h:130
static unsigned int size(double val)
Definition: Conv.h:173
static unsigned int size(const vector< T > &val)
Definition: Conv.h:570