Scarab  v2.2.1
Project 8 C++ Utility Library
param_value.hh
Go to the documentation of this file.
1 /*
2  * param_value.hh
3  *
4  * Created on: Jan 14, 2014
5  * Author: nsoblath
6  */
7 
8 #ifndef SCARAB_PARAM_VALUE_HH_
9 #define SCARAB_PARAM_VALUE_HH_
10 
11 #include "param_base.hh"
12 
13 #include "path.hh"
14 
15 #include <boost/variant.hpp>
16 
17 #include <stdint.h>
18 
19 //#include "logger.hh"
20 //LOGGER(pv_h, "param_value.hh")
21 
22 namespace scarab
23 {
24  class param_array;
25  class param_node;
26 
27  class SCARAB_API param_value : public param
28  {
29  public:
30  param_value();
31  param_value( bool a_value );
32  param_value( uint8_t a_value );
33  param_value( uint16_t a_value );
34  param_value( uint32_t a_value );
35  param_value( uint64_t a_value );
36  param_value( int8_t a_value );
37  param_value( int16_t a_value );
38  param_value( int32_t a_value );
39  param_value( int64_t a_value );
40  param_value( float a_value );
41  param_value( double a_value );
42  param_value( const std::string& a_value );
43  param_value( const char* a_value );
44  param_value( const param_value& orig );
45  param_value( param_value&& orig );
46  virtual ~param_value();
47 
48  param_value& operator=( const param_value& rhs );
49  param_value& operator=( param_value&& rhs );
50 
51  virtual param_ptr_t clone() const;
52  virtual param_ptr_t move_clone();
53 
54  bool empty() const;
55 
56  virtual bool is_null() const;
57  virtual bool is_value() const;
58 
59  virtual bool has_subset( const param& a_subset ) const;
60 
61  std::string type() const;
62  bool is_bool() const;
63  bool is_uint() const;
64  bool is_int() const;
65  bool is_double() const;
66  bool is_string() const;
67 
68  bool as_bool() const;
69  uint64_t as_uint() const;
70  int64_t as_int() const;
71  double as_double() const;
72  std::string as_string() const;
73  path as_path() const;
74 
75  template< typename XValType >
76  XValType as() const;
77 
78  void set( bool a_value );
79  void set( uint8_t a_value );
80  void set( uint16_t a_value );
81  void set( uint32_t a_value );
82  void set( uint64_t a_value );
83  void set( int8_t a_value );
84  void set( int16_t a_value );
85  void set( int32_t a_value );
86  void set( int64_t a_value );
87  void set( float a_value );
88  void set( double a_value );
89  void set( const std::string& a_value );
90  void set( const char* a_value );
91  //template< typename XValType >
92  //void set( XValType a_value );
93 
94  //template< typename XStreamableType >
95  //param_value& operator<<( const XStreamableType& a_streamable );
96 
97  virtual std::string to_string() const;
98 
99  void clear();
100 
101  private:
102  boost::variant< bool, uint64_t, int64_t, double, std::string > f_value;
103 
104  //*********************
105  // Visitor Classes
106  //*********************
107 
108  class type_visitor : public boost::static_visitor<>
109  {
110  public:
111  typedef std::string result_type;
112  std::string operator()( bool ) const
113  {
114  return "bool";
115  }
116  std::string operator()( uint64_t ) const
117  {
118  return "uint";
119  }
120  std::string operator()( int64_t ) const
121  {
122  return "int";
123  }
124  std::string operator()( double ) const
125  {
126  return "double";
127  }
128  std::string operator()( const std::string& ) const
129  {
130  return "string";
131  }
132  };
133 
134  template< typename XValType >
135  class get_visitor : public boost::static_visitor<>
136  {
137  public:
138  typedef XValType result_type;
139  XValType operator()( bool a_value ) const
140  {
141  return static_cast< XValType >( a_value );
142  }
143  XValType operator()( uint64_t a_value ) const
144  {
145  return static_cast< XValType >( a_value );
146  }
147  XValType operator()( int64_t a_value ) const
148  {
149  return static_cast< XValType >( a_value );
150  }
151  XValType operator()( double a_value ) const
152  {
153  return static_cast< XValType >( a_value );
154  }
155  XValType operator()( const std::string& a_value ) const
156  {
157  std::stringstream t_conv;
158  t_conv << a_value;
159  XValType t_return;
160  t_conv >> t_return;
161  return t_return;
162  }
163  };
164 
165  class is_bool_visitor : public boost::static_visitor<>
166  {
167  public:
168  typedef bool result_type;
169  bool operator()( bool ) const
170  {
171  return true;
172  }
173  template< typename T >
174  bool operator()( T ) const
175  {
176  return false;
177  }
178  };
179 
180  class is_int_visitor : public boost::static_visitor<>
181  {
182  public:
183  typedef bool result_type;
184  bool operator()( int64_t ) const
185  {
186  return true;
187  }
188  template< typename T >
189  bool operator()( T ) const
190  {
191  return false;
192  }
193  };
194 
195  class is_uint_visitor : public boost::static_visitor<>
196  {
197  public:
198  typedef bool result_type;
199  bool operator()( uint64_t ) const
200  {
201  return true;
202  }
203  template< typename T >
204  bool operator()( T ) const
205  {
206  return false;
207  }
208  };
209 
210  class is_double_visitor : public boost::static_visitor<>
211  {
212  public:
213  typedef bool result_type;
214  bool operator()( double ) const
215  {
216  return true;
217  }
218  template< typename T >
219  bool operator()( T ) const
220  {
221  return false;
222  }
223  };
224 
225  class is_string_visitor : public boost::static_visitor<>
226  {
227  public:
228  typedef bool result_type;
229  bool operator()( const std::string& ) const
230  {
231  return true;
232  }
233  template< typename T >
234  bool operator()( T ) const
235  {
236  return false;
237  }
238  };
239 
240  class as_bool_visitor : public boost::static_visitor<>
241  {
242  public:
243  typedef bool result_type;
244  bool operator()( bool a_value ) const
245  {
246  return a_value;
247  }
248  bool operator()( const std::string& a_value ) const
249  {
250  if( a_value.empty() ) return false;
251 
252  std::string t_str_val;
253  bool t_is_numeric = true;
254  for( std::string::const_iterator t_val_it = a_value.begin(); t_val_it != a_value.end(); ++t_val_it )
255  {
256  t_is_numeric = t_is_numeric && ::isdigit( *t_val_it );
257  t_str_val.push_back( ::tolower( *t_val_it ) );
258  }
259 
260  if( t_is_numeric ) return std::stoi( t_str_val );
261 
262  std::istringstream t_iss_val( t_str_val );
263  bool t_bool_val;
264  t_iss_val >> std::boolalpha >> t_bool_val;
265  return t_bool_val;
266  }
267  template< typename T >
268  bool operator()( T a_value ) const
269  {
270  return a_value != 0;
271  }
272  };
273 
274  class as_uint_visitor : public boost::static_visitor<>
275  {
276  public:
277  typedef uint64_t result_type;
278  uint64_t operator()( const std::string& a_value ) const
279  {
280  return std::stoull( a_value );
281  }
282  template< typename T >
283  uint64_t operator()( T a_value ) const
284  {
285  return (uint64_t)a_value;
286  }
287  };
288 
289  class as_int_visitor : public boost::static_visitor<>
290  {
291  public:
292  typedef int64_t result_type;
293  int64_t operator()( const std::string& a_value ) const
294  {
295  return std::stoll( a_value );
296  }
297  template< typename T >
298  int64_t operator()( T a_value ) const
299  {
300  return (int64_t)a_value;
301  }
302  };
303 
304  class as_double_visitor : public boost::static_visitor<>
305  {
306  public:
307  typedef double result_type;
308  double operator()( const std::string& a_value ) const
309  {
310  return std::stod( a_value );
311  }
312  template< typename T >
313  double operator()( T a_value ) const
314  {
315  return (double)a_value;
316  }
317  };
318 
319  class as_string_visitor : public boost::static_visitor<>
320  {
321  public:
322  typedef std::string result_type;
323  std::string operator()( bool a_value ) const
324  {
325  return a_value ? "true" : "false";
326  }
327  std::string operator()( const std::string& a_value ) const
328  {
329  return a_value;
330  }
331  template< typename T >
332  std::string operator()( T a_value ) const
333  {
334  return std::to_string( a_value );
335  }
336  };
337 
338  class as_path_visitor : public boost::static_visitor<>
339  {
340  public:
342  scarab::path operator()( const std::string& a_value ) const
343  {
344  return scarab::path( a_value );
345  }
346  template< typename T >
348  {
349  return scarab::path();
350  }
351  };
352 
353  class clear_visitor : public boost::static_visitor<>
354  {
355  public:
356  typedef void result_type;
357  void operator()( bool& a_value ) const
358  {
359  a_value = false;
360  }
361  void operator()( std::string& a_value ) const
362  {
363  a_value.clear();
364  }
365  template< typename T >
366  void operator()( T& a_value ) const
367  {
368  a_value = 0;
369  }
370  };
371 
372 
373  };
374 
375  SCARAB_API std::ostream& operator<<(std::ostream& out, const param_value& value);
376 
377  template<>
378  inline bool param_value::as< bool >() const
379  {
380  return as_bool();
381  }
382 
383  template<>
384  inline uint64_t param_value::as< uint64_t >() const
385  {
386  return as_uint();
387  }
388 
389  template<>
390  inline int64_t param_value::as< int64_t >() const
391  {
392  return as_int();
393  }
394 
395  template<>
396  inline double param_value::as< double >() const
397  {
398  return as_double();
399  }
400 
401  template<>
402  inline std::string param_value::as< std::string >() const
403  {
404  return as_string();
405  }
406 
407  template<>
409  {
410  return as_path();
411  }
412 
413  template< typename XValType >
414  XValType param_value::as() const
415  {
416  return boost::apply_visitor( get_visitor< XValType >(), f_value );
417  }
418 
419 
421  {
422  //std::cout << "param_value::clone" << std::endl;
423  return param_ptr_t( new param_value( *this ) );
424  }
425 
427  {
428  return param_ptr_t( new param_value( std::move(*this) ) );
429  }
430 
431  inline std::string param_value::type() const
432  {
433  return boost::apply_visitor( type_visitor(), f_value );
434  }
435 
436  inline bool param_value::is_null() const
437  {
438  return false;
439  }
440 
441  inline bool param_value::is_value() const
442  {
443  return true;
444  }
445 
446  inline bool param_value::is_bool() const
447  {
448  return boost::apply_visitor( is_bool_visitor(), f_value );
449  }
450 
451  inline bool param_value::is_uint() const
452  {
453  return boost::apply_visitor( is_uint_visitor(), f_value );
454  }
455 
456  inline bool param_value::is_int() const
457  {
458  return boost::apply_visitor( is_int_visitor(), f_value );
459  }
460 
461  inline bool param_value::is_double() const
462  {
463  return boost::apply_visitor( is_double_visitor(), f_value );
464  }
465 
466  inline bool param_value::is_string() const
467  {
468  return boost::apply_visitor( is_string_visitor(), f_value );
469  }
470 
471  inline bool param_value::as_bool() const
472  {
473  return boost::apply_visitor( as_bool_visitor(), f_value );
474  }
475 
476  inline uint64_t param_value::as_uint() const
477  {
478  return boost::apply_visitor( as_uint_visitor(), f_value );
479  }
480 
481  inline int64_t param_value::as_int() const
482  {
483  return boost::apply_visitor( as_int_visitor(), f_value );
484  }
485 
486  inline double param_value::as_double() const
487  {
488  return boost::apply_visitor( as_double_visitor(), f_value );
489  }
490 
491  inline std::string param_value::as_string() const
492  {
493  return boost::apply_visitor( as_string_visitor(), f_value );
494  }
495 
496  inline path param_value::as_path() const
497  {
498  return boost::apply_visitor( as_path_visitor(), f_value );
499  }
500 
501  inline void param_value::set( bool a_value )
502  {
503  f_value = a_value;
504  return;
505  }
506 
507  inline void param_value::set( uint8_t a_value )
508  {
509  f_value = uint64_t(a_value);
510  return;
511  }
512 
513  inline void param_value::set( uint16_t a_value )
514  {
515  f_value = uint64_t(a_value);
516  return;
517  }
518 
519  inline void param_value::set( uint32_t a_value )
520  {
521  f_value = uint64_t(a_value);
522  return;
523  }
524 
525  inline void param_value::set( uint64_t a_value )
526  {
527  f_value = a_value;
528  return;
529  }
530 
531  inline void param_value::set( int8_t a_value )
532  {
533  f_value = int64_t(a_value);
534  return;
535  }
536 
537  inline void param_value::set( int16_t a_value )
538  {
539  f_value = int64_t(a_value);
540  return;
541  }
542 
543  inline void param_value::set( int32_t a_value )
544  {
545  f_value = int64_t(a_value);
546  return;
547  }
548 
549  inline void param_value::set( int64_t a_value )
550  {
551  f_value = a_value;
552  return;
553  }
554 
555  inline void param_value::set( float a_value )
556  {
557  f_value = a_value;
558  return;
559  }
560 
561  inline void param_value::set( double a_value )
562  {
563  f_value = a_value;
564  return;
565  }
566 
567  inline void param_value::set( const std::string& a_value )
568  {
569  f_value = a_value;
570  return;
571  }
572 
573  inline void param_value::set( const char* a_value )
574  {
575  f_value = std::string( a_value );
576  return;
577  }
578 
579  inline std::string param_value::to_string() const
580  {
581  return as_string();
582  }
583 
584  inline void param_value::clear()
585  {
586  boost::apply_visitor( clear_visitor(), f_value );
587  return;
588  }
589 
590 } /* namespace scarab */
591 
592 #endif /* SCARAB_PARAM_VALUE_HH_ */
int64_t operator()(T a_value) const
Definition: param_value.hh:298
std::string operator()(double) const
Definition: param_value.hh:124
scarab::path operator()(const std::string &a_value) const
Definition: param_value.hh:342
std::string type() const
Definition: param_value.hh:431
fs::path path
Definition: path.hh:25
virtual std::string to_string() const
Definition: param_value.hh:579
path as_path() const
Definition: param_value.hh:496
bool operator()(const std::string &a_value) const
Definition: param_value.hh:248
#define SCARAB_API
Definition: scarab_api.hh:24
scarab::path operator()(T) const
Definition: param_value.hh:347
std::string operator()(int64_t) const
Definition: param_value.hh:120
bool is_uint() const
Definition: param_value.hh:451
int64_t as_int() const
Definition: param_value.hh:481
std::string operator()(const std::string &) const
Definition: param_value.hh:128
std::string type(const x_type &a_param)
Definition: typename.hh:22
void operator()(T &a_value) const
Definition: param_value.hh:366
boost::variant< bool, uint64_t, int64_t, double, std::string > f_value
Definition: param_value.hh:102
bool is_bool() const
Definition: param_value.hh:446
bool operator()(uint64_t) const
Definition: param_value.hh:199
virtual bool is_value() const
Definition: param_value.hh:441
bool is_int() const
Definition: param_value.hh:456
bool operator()(T a_value) const
Definition: param_value.hh:268
bool operator()(const std::string &) const
Definition: param_value.hh:229
virtual param_ptr_t clone() const
Definition: param_value.hh:420
double operator()(const std::string &a_value) const
Definition: param_value.hh:308
std::string operator()(T a_value) const
Definition: param_value.hh:332
XValType operator()(bool a_value) const
Definition: param_value.hh:139
std::string as_string() const
Definition: param_value.hh:491
XValType as() const
Definition: param_value.hh:414
std::string operator()(uint64_t) const
Definition: param_value.hh:116
void operator()(std::string &a_value) const
Definition: param_value.hh:361
XValType operator()(int64_t a_value) const
Definition: param_value.hh:147
std::string param_value::as< std::string >() const
Definition: param_value.hh:402
XValType operator()(const std::string &a_value) const
Definition: param_value.hh:155
std::string operator()(const std::string &a_value) const
Definition: param_value.hh:327
SCARAB_API std::ostream & operator<<(std::ostream &out, const param_array &a_value)
Definition: param_array.cc:110
uint64_t operator()(T a_value) const
Definition: param_value.hh:283
virtual param_ptr_t move_clone()
Definition: param_value.hh:426
uint64_t operator()(const std::string &a_value) const
Definition: param_value.hh:278
std::unique_ptr< param > param_ptr_t
Definition: param_base.hh:23
std::string operator()(bool) const
Definition: param_value.hh:112
bool is_double() const
Definition: param_value.hh:461
bool is_string() const
Definition: param_value.hh:466
void operator()(bool &a_value) const
Definition: param_value.hh:357
void set(bool a_value)
Definition: param_value.hh:501
virtual bool is_null() const
Definition: param_value.hh:436
std::string to_string(std::uint64_t x)
Definition: date.h:7722
scarab::path param_value::as< scarab::path >() const
Definition: param_value.hh:408
double operator()(T a_value) const
Definition: param_value.hh:313
bool as_bool() const
Definition: param_value.hh:471
int64_t operator()(const std::string &a_value) const
Definition: param_value.hh:293
std::string operator()(bool a_value) const
Definition: param_value.hh:323
XValType operator()(double a_value) const
Definition: param_value.hh:151
double as_double() const
Definition: param_value.hh:486
uint64_t as_uint() const
Definition: param_value.hh:476
XValType operator()(uint64_t a_value) const
Definition: param_value.hh:143
bool operator()(bool a_value) const
Definition: param_value.hh:244