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