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