Scarab  v2.0.1
Project 8 C++ Utility Library
param_value.cc
Go to the documentation of this file.
1 /*
2  * param_value.cc
3  *
4  * Created on: Jan 14, 2014
5  * Author: nsoblath
6  */
7 
8 #define SCARAB_API_EXPORTS
9 
10 #include <sstream>
11 using std::string;
12 using std::stringstream;
13 
14 #include "param_value.hh"
15 
16 
17 namespace scarab
18 {
19 
21  param(),
22  f_value_type( k_invalid ),
23  f_buffer()
24  {
25  //LWARN( dlog, "param_value constructor: k_invalid" );
26  }
27 
28  param_value::param_value( bool a_value ) :
29  param(),
31  f_buffer()
32  {
33  f_value.f_bool = a_value;
34  //LWARN( dlog, "param_value constructor: bool --> bool" );
35  }
36 
37  param_value::param_value( uint8_t a_value ) :
38  param(),
40  f_buffer()
41  {
42  f_value.f_uint = a_value;
43  //LWARN( dlog, "param_value constructor: uint8 --> uint" );
44  }
45 
46  param_value::param_value( uint16_t a_value ) :
47  param(),
49  f_buffer()
50  {
51  f_value.f_uint = a_value;
52  //LWARN( dlog, "param_value constructor: uint16 --> uint" );
53  }
54 
55  param_value::param_value( uint32_t a_value ) :
56  param(),
58  f_buffer()
59  {
60  f_value.f_uint = a_value;
61  //LWARN( dlog, "param_value constructor: uint32 --> uint" );
62  }
63 
64  param_value::param_value( uint64_t a_value ) :
65  param(),
67  f_buffer()
68  {
69  f_value.f_uint = a_value;
70  //LWARN( dlog, "param_value constructor: uint64 --> uint" );
71  }
72 
73  param_value::param_value( int8_t a_value ) :
74  param(),
76  f_buffer()
77  {
78  f_value.f_int = a_value;
79  //LWARN( dlog, "param_value constructor: int8 --> int" );
80  }
81 
82  param_value::param_value( int16_t a_value ) :
83  param(),
85  f_buffer()
86  {
87  f_value.f_int = a_value;
88  //LWARN( dlog, "param_value constructor: int16 --> int" );
89  }
90 
91 
92  param_value::param_value( int32_t a_value ) :
93  param(),
95  f_buffer()
96  {
97  f_value.f_int = a_value;
98  //LWARN( dlog, "param_value constructor: int32 --> int" );
99  }
100 
101  param_value::param_value( int64_t a_value ) :
102  param(),
103  f_value_type( k_int ),
104  f_buffer()
105  {
106  f_value.f_int = a_value;
107  //LWARN( dlog, "param_value constructor: int64 --> int" );
108  }
109 
110  param_value::param_value( float a_value ) :
111  param(),
113  f_buffer()
114  {
115  f_value.f_double = a_value;
116  //LWARN( dlog, "param_value constructor: float --> double" );
117  }
118 
119  param_value::param_value( double a_value ) :
120  param(),
122  f_buffer()
123  {
124  f_value.f_double = a_value;
125  //LWARN( dlog, "param_value constructor: double --> double" );
126  }
127 
128  param_value::param_value( const char* a_value ) :
129  param(),
131  f_buffer()
132  {
133  f_value.f_string = new string( a_value );
134  //LWARN( dlog, "param_value constructor: char* --> k_string" );
135  }
136 
137  param_value::param_value( const string& a_value ) :
138  param(),
140  f_buffer()
141  {
142  f_value.f_string = new string( a_value );
143  //LWARN( dlog, "param_value constructor: string --> k_string" );
144  }
145 
147  param( orig ),
148  f_value( orig.f_value ),
149  f_value_type( orig.f_value_type ),
150  f_buffer()
151  {
152  if( f_value_type == k_string )
153  {
154  f_value.f_string = new string( *orig.f_value.f_string );
155  }
156  //LWARN( dlog, "param_value copy constructor: " << type() );
157  }
158 
160  {
161  if( f_value_type == k_string )
162  {
163  delete f_value.f_string;
164  }
165  }
166 
168  {
169  if( &rhs == this ) return *this;
170 
171  if( f_value_type == k_string )
172  {
173  delete f_value.f_string;
174  }
175 
176  if( rhs.f_value_type == k_string )
177  {
178  f_value.f_string = new string( *rhs.f_value.f_string );
179  }
180  else
181  {
182  f_value = rhs.f_value;
183  }
185 
186  return *this;
187  }
188 
189  std::string param_value::type() const
190  {
191  switch( f_value_type )
192  {
193  case k_invalid:
194  return string( "invalid" );
195  break;
196  case k_bool:
197  return string( "bool" );
198  break;
199  case k_uint:
200  return string( "uint" );
201  break;
202  case k_int:
203  return string( "int" );
204  break;
205  case k_double:
206  return string( "double" );
207  break;
208  case k_string:
209  return string( "string" );
210  break;
211  }
212  return string( "unknown" );
213  }
214 
215  bool param_value::as_bool() const
216  {
217  if( f_value_type == k_bool ) return f_value.f_bool;
218  else if( f_value_type == k_uint ) return f_value.f_uint != 0;
219  else if( f_value_type == k_int ) return f_value.f_int != 0;
220  else if( f_value_type == k_double ) return f_value.f_double != 0.;
221  else if( f_value_type == k_string )
222  {
223  if( f_value.f_string->empty() ) return false;
224 
225  std::string t_str_val;
226  bool t_is_numeric = true;
227  for( std::string::const_iterator t_val_it = f_value.f_string->begin(); t_val_it != f_value.f_string->end(); ++t_val_it )
228  {
229  t_is_numeric = t_is_numeric && ::isdigit( *t_val_it );
230  t_str_val.push_back( ::tolower( *t_val_it ) );
231  }
232 
233  if( t_is_numeric ) return std::stoi( t_str_val );
234 
235  std::istringstream t_iss_val( t_str_val );
236  bool t_bool_val;
237  t_iss_val >> std::boolalpha >> t_bool_val;
238  return t_bool_val;
239  }
240  return false;
241  }
242 
243  uint64_t param_value::as_uint() const
244  {
245  if( f_value_type == k_bool ) return (uint64_t)f_value.f_bool;
246  else if( f_value_type == k_uint ) return f_value.f_uint;
247  else if( f_value_type == k_int ) return (uint64_t)f_value.f_int;
248  else if( f_value_type == k_double ) return (uint64_t)f_value.f_double;
249  else if( f_value_type == k_string )
250  {
251  std::stringstream t_conv;
252  t_conv << *f_value.f_string;
253  uint64_t t_return;
254  t_conv >> t_return;
255  return t_return;
256  }
257  return 0.;
258  }
259  int64_t param_value::as_int() const
260  {
261  if( f_value_type == k_bool ) return (int64_t)f_value.f_bool;
262  else if( f_value_type == k_uint ) return (int64_t)f_value.f_uint;
263  else if( f_value_type == k_int ) return f_value.f_int;
264  else if( f_value_type == k_double ) return (int64_t)f_value.f_double;
265  else if( f_value_type == k_string )
266  {
267  std::stringstream t_conv;
268  t_conv << *f_value.f_string;
269  int64_t t_return;
270  t_conv >> t_return;
271  return t_return;
272  }
273  return 0.;
274  }
275  double param_value::as_double() const
276  {
277  if( f_value_type == k_bool ) return f_value.f_bool;
278  else if( f_value_type == k_uint ) return (double)f_value.f_uint;
279  else if( f_value_type == k_int ) return (double)f_value.f_int;
280  else if( f_value_type == k_double ) return f_value.f_double;
281  else if( f_value_type == k_string )
282  {
283  std::stringstream t_conv;
284  t_conv << *f_value.f_string;
285  double t_return;
286  t_conv >> t_return;
287  return t_return;
288  }
289  return 0.;
290  }
291  const string& param_value::as_string() const
292  {
293  if( f_value_type == k_string ) return *f_value.f_string;
294 
295  std::stringstream t_conv;
296  if( f_value_type == k_bool ) t_conv << (as_bool() ? "true" : "false");
297  else if( f_value_type == k_uint ) t_conv << as_uint();
298  else if( f_value_type == k_int ) t_conv << as_int();
299  else if( f_value_type == k_double ) t_conv << as_double();
300 
301  t_conv >> f_buffer;
302  return f_buffer;
303  }
304 
306  {
307  if( f_value_type == k_string ) return path( *f_value.f_string );
308  return path();
309  }
310 
312  {
313  if( f_value_type == k_bool ) f_value.f_bool = false;
314  else if( f_value_type == k_uint ) f_value.f_uint = 0;
315  else if( f_value_type == k_int ) f_value.f_int = 0;
316  else if( f_value_type == k_double ) f_value.f_double = 0.;
317  else if( f_value_type == k_string ) f_value.f_string->clear();
318  return;
319  }
320 
321  bool param_value::has_subset( const param& a_subset ) const
322  {
323  if( ! a_subset.is_value() ) return false;
324  return true;
325  }
326 
327  SCARAB_API std::ostream& operator<<(std::ostream& out, const param_value& a_value)
328  {
329  return out << a_value.as_string();
330  }
331 
332 } /* namespace scarab */
std::string type() const
Definition: param_value.cc:189
fs::path path
Definition: path.hh:25
virtual bool has_subset(const param &a_subset) const
Definition: param_value.cc:321
path as_path() const
Definition: param_value.cc:305
virtual bool is_value() const
#define SCARAB_API
Definition: scarab_api.hh:24
int64_t as_int() const
Definition: param_value.cc:259
std::string f_buffer
Definition: param_value.hh:114
union scarab::param_value::Values f_value
enum scarab::param_value::ValueTypes f_value_type
SCARAB_API std::ostream & operator<<(std::ostream &out, const param_array &a_value)
Definition: param_array.cc:97
virtual ~param_value()
Definition: param_value.cc:159
const std::string & as_string() const
Definition: param_value.cc:291
bool as_bool() const
Definition: param_value.cc:215
double as_double() const
Definition: param_value.cc:275
uint64_t as_uint() const
Definition: param_value.cc:243
param_value & operator=(const param_value &rhs)
Definition: param_value.cc:167