Scarab  v2.0.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 <stdint.h>
16 
17 
18 namespace scarab
19 {
20  class param_array;
21  class param_node;
22 
23  class SCARAB_API param_value : public param
24  {
25  public:
26  param_value();
27  param_value( bool a_value );
28  param_value( uint8_t a_value );
29  param_value( uint16_t a_value );
30  param_value( uint32_t a_value );
31  param_value( uint64_t a_value );
32  param_value( int8_t a_value );
33  param_value( int16_t a_value );
34  param_value( int32_t a_value );
35  param_value( int64_t a_value );
36  param_value( float a_value );
37  param_value( double a_value );
38  param_value( const std::string& a_value );
39  param_value( const char* a_value );
40  param_value( const param_value& orig );
41  virtual ~param_value();
42 
43  param_value& operator=( const param_value& rhs );
44 
45  virtual param* clone() const;
46 
47  bool empty() const;
48 
49  virtual bool is_null() const;
50  virtual bool is_value() const;
51 
52  virtual bool has_subset( const param& a_subset ) const;
53 
54  std::string type() const;
55  bool is_bool() const;
56  bool is_uint() const;
57  bool is_int() const;
58  bool is_double() const;
59  bool is_string() const;
60 
61  bool as_bool() const;
62  uint64_t as_uint() const;
63  int64_t as_int() const;
64  double as_double() const;
65  const std::string& as_string() const;
66  path as_path() const;
67 
68  template< typename XValType >
69  XValType get() const;
70 
71  void set( bool a_value );
72  void set( uint8_t a_value );
73  void set( uint16_t a_value );
74  void set( uint32_t a_value );
75  void set( uint64_t a_value );
76  void set( int8_t a_value );
77  void set( int16_t a_value );
78  void set( int32_t a_value );
79  void set( int64_t a_value );
80  void set( float a_value );
81  void set( double a_value );
82  void set( const std::string& a_value );
83  void set( const char* a_value );
84  //template< typename XValType >
85  //void set( XValType a_value );
86 
87  //template< typename XStreamableType >
88  //param_value& operator<<( const XStreamableType& a_streamable );
89 
90  virtual std::string to_string() const;
91 
92  void clear();
93 
94  private:
95  union Values
96  {
97  bool f_bool;
98  uint64_t f_uint;
99  int64_t f_int;
100  double f_double;
101  std::string* f_string;
102  } f_value;
103 
105  {
111  k_invalid
112  } f_value_type;
113 
114  mutable std::string f_buffer;
115  };
116 
117  SCARAB_API std::ostream& operator<<(std::ostream& out, const param_value& value);
118 
119  template<>
120  inline bool param_value::get< bool >() const
121  {
122  return as_bool();
123  }
124 
125  template<>
126  inline uint64_t param_value::get< uint64_t >() const
127  {
128  return as_uint();
129  }
130 
131  template<>
132  inline int64_t param_value::get< int64_t >() const
133  {
134  return as_int();
135  }
136 
137  template<>
138  inline double param_value::get< double >() const
139  {
140  return as_double();
141  }
142 
143  template<>
144  inline std::string param_value::get< std::string >() const
145  {
146  return as_string();
147  }
148 
149  template<>
151  {
152  return as_path();
153  }
154 
155  template< typename XValType >
156  XValType param_value::get() const
157  {
158  if( f_value_type == k_bool ) return static_cast< XValType >( as_bool() );
159  else if( f_value_type == k_uint ) return static_cast< XValType >( as_uint() );
160  else if( f_value_type == k_int ) return static_cast< XValType >( as_int() );
161  else if( f_value_type == k_double ) return static_cast< XValType >( as_double() );
162  else if( f_value_type == k_string )
163  {
164  std::stringstream t_conv;
165  t_conv << *f_value.f_string;
166  XValType t_return;
167  t_conv >> t_return;
168  return t_return;
169  }
170  return XValType();
171  }
172 
173 
174  inline param* param_value::clone() const
175  {
176  //std::cout << "param_value::clone" << std::endl;
177  return new param_value( *this );
178  }
179 
180  inline bool param_value::is_null() const
181  {
182  return false;
183  }
184 
185  inline bool param_value::is_value() const
186  {
187  return true;
188  }
189 
190  inline bool param_value::is_bool() const
191  {
192  return f_value_type == k_bool;
193  }
194 
195  inline bool param_value::is_uint() const
196  {
197  return f_value_type == k_uint;
198  }
199 
200  inline bool param_value::is_int() const
201  {
202  return f_value_type == k_int;
203  }
204 
205  inline bool param_value::is_double() const
206  {
207  return f_value_type == k_double;
208  }
209 
210  inline bool param_value::is_string() const
211  {
212  return f_value_type == k_string;
213  }
214 
215  inline void param_value::set( bool a_value )
216  {
217  if( f_value_type == k_string ) delete f_value.f_string;
218  f_value_type = k_bool;
219  f_value.f_bool = a_value;
220  return;
221  }
222 
223  inline void param_value::set( uint8_t a_value )
224  {
225  if( f_value_type == k_string ) delete f_value.f_string;
226  f_value_type = k_uint;
227  f_value.f_uint = a_value;
228  return;
229  }
230 
231  inline void param_value::set( uint16_t a_value )
232  {
233  if( f_value_type == k_string ) delete f_value.f_string;
234  f_value_type = k_uint;
235  f_value.f_uint = a_value;
236  return;
237  }
238 
239  inline void param_value::set( uint32_t a_value )
240  {
241  if( f_value_type == k_string ) delete f_value.f_string;
242  f_value_type = k_uint;
243  f_value.f_uint = a_value;
244  return;
245  }
246 
247  inline void param_value::set( uint64_t a_value )
248  {
249  if( f_value_type == k_string ) delete f_value.f_string;
250  f_value_type = k_uint;
251  f_value.f_uint = a_value;
252  return;
253  }
254 
255  inline void param_value::set( int8_t a_value )
256  {
257  if( f_value_type == k_string ) delete f_value.f_string;
258  f_value_type = k_int;
259  f_value.f_int = a_value;
260  return;
261  }
262 
263  inline void param_value::set( int16_t a_value )
264  {
265  if( f_value_type == k_string ) delete f_value.f_string;
266  f_value_type = k_int;
267  f_value.f_int = a_value;
268  return;
269  }
270 
271  inline void param_value::set( int32_t a_value )
272  {
273  if( f_value_type == k_string ) delete f_value.f_string;
274  f_value_type = k_int;
275  f_value.f_int = a_value;
276  return;
277  }
278 
279  inline void param_value::set( int64_t a_value )
280  {
281  if( f_value_type == k_string ) delete f_value.f_string;
282  f_value_type = k_int;
283  f_value.f_int = a_value;
284  return;
285  }
286 
287  inline void param_value::set( float a_value )
288  {
289  if( f_value_type == k_string ) delete f_value.f_string;
290  f_value_type = k_double;
291  f_value.f_double = a_value;
292  return;
293  }
294 
295  inline void param_value::set( double a_value )
296  {
297  if( f_value_type == k_string ) delete f_value.f_string;
298  f_value_type = k_double;
299  f_value.f_double = a_value;
300  return;
301  }
302 
303  inline void param_value::set( const std::string& a_value )
304  {
305  if( f_value_type == k_string ) delete f_value.f_string;
306  f_value_type = k_string;
307  f_value.f_string = new std::string( a_value );
308  return;
309  }
310 
311  inline void param_value::set( const char* a_value )
312  {
313  if( f_value_type == k_string ) delete f_value.f_string;
314  f_value_type = k_string;
315  f_value.f_string = new std::string( a_value );
316  return;
317  }
318 
319  inline std::string param_value::to_string() const
320  {
321  return as_string();
322  }
323 } /* namespace scarab */
324 
325 #endif /* SCARAB_PARAM_VALUE_HH_ */
fs::path path
Definition: path.hh:25
virtual std::string to_string() const
Definition: param_value.hh:319
#define SCARAB_API
Definition: scarab_api.hh:24
bool is_uint() const
Definition: param_value.hh:195
std::string type(const x_type &a_param)
Definition: typename.hh:22
bool is_bool() const
Definition: param_value.hh:190
std::string f_buffer
Definition: param_value.hh:114
XValType get() const
Definition: param_value.hh:156
virtual bool is_value() const
Definition: param_value.hh:185
bool is_int() const
Definition: param_value.hh:200
std::string param_value::get< std::string >() const
Definition: param_value.hh:144
SCARAB_API std::ostream & operator<<(std::ostream &out, const param_array &a_value)
Definition: param_array.cc:97
bool is_double() const
Definition: param_value.hh:205
scarab::path param_value::get< scarab::path >() const
Definition: param_value.hh:150
bool is_string() const
Definition: param_value.hh:210
void set(bool a_value)
Definition: param_value.hh:215
virtual bool is_null() const
Definition: param_value.hh:180
std::string to_string(std::uint64_t x)
Definition: date.h:7722
virtual param * clone() const
Definition: param_value.hh:174