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