Scarab  v2.1.0
Project 8 C++ Utility Library
param_node.hh
Go to the documentation of this file.
1 /*
2  * param_node.hh
3  *
4  * Created on: Jan 14, 2014
5  * Author: nsoblath
6  */
7 
8 #ifndef SCARAB_PARAM_NODE_HH_
9 #define SCARAB_PARAM_NODE_HH_
10 
11 #include "param_value.hh"
12 
13 #include <boost/iterator/iterator_adaptor.hpp>
14 #include <boost/type_traits/is_convertible.hpp>
15 #include <boost/utility/enable_if.hpp>
16 
17 #include <map>
18 
19 namespace scarab
20 {
21  class param_value;
22  class param_array;
23 
24  template< class x_key, class x_value, class x_iiterator >
25  class map_deref_iterator : public boost::iterator_adaptor< map_deref_iterator< x_key, x_value, x_iiterator >, x_iiterator, x_value, boost::bidirectional_traversal_tag >
26  {
27  private:
28  // used for the conversion constructor below
29  struct enabler {};
30 
31  public:
33  map_deref_iterator::iterator_adaptor_()
34  {}
35  map_deref_iterator( const x_iiterator& other ) :
36  map_deref_iterator::iterator_adaptor_( other )
37  {}
38 
39  // converts from iterator to const_iterator, but the enable_if business prevents converting from const_iterator to iterator
40  template< class x_other_value, class x_other_iiterator >
41  map_deref_iterator( const map_deref_iterator< x_key, x_other_value, x_other_iiterator > & other, typename boost::enable_if< boost::is_convertible< x_other_value, x_value >, enabler >::type = enabler() ) :
42  map_deref_iterator::iterator_adaptor_( other.base )
43  {}
44 
45  const x_key& name() const
46  {
47  return this->base()->first;
48  }
49 
50  private:
51  friend class boost::iterator_core_access;
52 
53  x_value& dereference() const
54  {
55  return *this->base()->second;
56  }
57 
58  };
59 
60  typedef std::map< std::string, std::unique_ptr< param > > param_node_contents;
61 
64 
65 
66  class SCARAB_API param_node : public param
67  {
68  public:
69  typedef param_node_contents contents;
70  typedef param_node_iterator iterator;
71  typedef param_node_const_iterator const_iterator;
72  typedef contents::value_type contents_type;
73 
74  param_node();
75  param_node( const param_node& orig );
76  param_node( param_node&& orig );
77  virtual ~param_node();
78 
79  param_node& operator=( const param_node& rhs );
80  param_node& operator=( param_node&& rhs );
81 
82  virtual param_ptr_t clone() const;
83  virtual param_ptr_t move_clone();
84 
85  virtual bool is_null() const;
86  virtual bool is_node() const;
87 
88  virtual bool has_subset( const param& a_subset ) const;
89 
90  unsigned size() const;
91  bool empty() const;
92 
93  bool has( const std::string& a_name ) const;
94  unsigned count( const std::string& a_name ) const;
95 
98  std::string get_value( const std::string& a_name ) const;
101  template< typename XValType >
102  XValType get_value( const std::string& a_name ) const;
103 
106  std::string get_value( const std::string& a_name, const std::string& a_default ) const;
107  std::string get_value( const std::string& a_name, const char* a_default ) const;
110  template< typename XValType >
111  XValType get_value( const std::string& a_name, XValType a_default ) const;
112 
115  const param& at( const std::string& a_name ) const;
118  param& at( const std::string& a_name );
119 
122  const param_value& value_at( const std::string& a_name ) const;
125  param_value& value_at( const std::string& a_name );
126 
129  const param_array& array_at( const std::string& a_name ) const;
132  param_array& array_at( const std::string& a_name );
133 
136  const param_node& node_at( const std::string& a_name ) const;
139  param_node& node_at( const std::string& a_name );
140 
143  const param& operator[]( const std::string& a_name ) const;
146  param& operator[]( const std::string& a_name );
147 
149  bool add( const std::string& a_name, const param& a_value );
151  bool add( const std::string& a_name, param&& a_value );
153  bool add( const std::string& a_name, param_ptr_t a_value_ptr );
154 
156  void replace( const std::string& a_name, const param& a_value );
158  void replace( const std::string& a_name, param&& a_value );
160  void replace( const std::string& a_name, param_ptr_t a_value_ptr );
161 
165  void merge( const param_node& a_object );
166 
167  void erase( const std::string& a_name );
168  param_ptr_t remove( const std::string& a_name );
169  void clear();
170 
171  iterator begin();
172  const_iterator begin() const;
173 
174  iterator end();
175  const_iterator end() const;
176 
177  virtual std::string to_string() const;
178 
179  protected:
180  contents f_contents;
181 
182  };
183 
184 
185  template< typename XValType >
186  inline XValType param_node::get_value( const std::string& a_name ) const
187  {
188  return value_at( a_name ).get< XValType >();
189  }
190 
191  template< typename XValType >
192  inline XValType param_node::get_value( const std::string& a_name, XValType a_default ) const
193  {
194  return has( a_name ) ? value_at( a_name ).get< XValType >() : a_default;
195  }
196 
198  {
199  //std::cout << "param_node::clone" << std::endl;
200  return std::unique_ptr< param_node >( new param_node( *this ) );
201  }
202 
204  {
205  return std::unique_ptr< param_node >( new param_node( std::move(*this) ) );
206  }
207 
208  inline bool param_node::is_null() const
209  {
210  return false;
211  }
212 
213  inline bool param_node::is_node() const
214  {
215  return true;
216  }
217 
218  inline unsigned param_node::size() const
219  {
220  return f_contents.size();
221  }
222  inline bool param_node::empty() const
223  {
224  return f_contents.empty();
225  }
226 
227  inline bool param_node::has( const std::string& a_name ) const
228  {
229  return f_contents.count( a_name ) > 0;
230  }
231 
232  inline unsigned param_node::count( const std::string& a_name ) const
233  {
234  return f_contents.count( a_name );
235  }
236 
237  inline std::string param_node::get_value( const std::string& a_name ) const
238  {
239  return value_at( a_name ).to_string();
240  }
241 
242  inline std::string param_node::get_value( const std::string& a_name, const std::string& a_default ) const
243  {
244  return has( a_name ) ? value_at( a_name ).to_string() : a_default;
245  }
246 
247  inline std::string param_node::get_value( const std::string& a_name, const char* a_default ) const
248  {
249  return get_value( a_name, std::string( a_default ) );
250  }
251 
252  inline const param& param_node::at( const std::string& a_name ) const
253  {
254  return *f_contents.at( a_name );
255  }
256 
257  inline param& param_node::at( const std::string& a_name )
258  {
259  return *f_contents.at( a_name );
260  }
261 
262  inline const param_value& param_node::value_at( const std::string& a_name ) const
263  {
264  return at( a_name ).as_value();
265  }
266 
267  inline param_value& param_node::value_at( const std::string& a_name )
268  {
269  return at( a_name ).as_value();
270  }
271 
272  inline const param_array& param_node::array_at( const std::string& a_name ) const
273  {
274  return at( a_name ).as_array();
275  }
276 
277  inline param_array& param_node::array_at( const std::string& a_name )
278  {
279  return at( a_name ).as_array();
280  }
281 
282  inline const param_node& param_node::node_at( const std::string& a_name ) const
283  {
284  return at( a_name ).as_node();
285  }
286 
287  inline param_node& param_node::node_at( const std::string& a_name )
288  {
289  return at( a_name ).as_node();
290  }
291 
292  inline const param& param_node::operator[]( const std::string& a_name ) const
293  {
294  return *f_contents.at( a_name );
295  }
296 
297  inline param& param_node::operator[]( const std::string& a_name )
298  {
299  return *f_contents[ a_name ];
300  }
301 
302  inline bool param_node::add( const std::string& a_name, const param& a_value )
303  {
304  contents::iterator it = f_contents.find( a_name );
305  if( it == f_contents.end() )
306  {
307  f_contents.insert( contents_type( a_name, a_value.clone() ) );
308  return true;
309  }
310  return false;
311  }
312 
313  inline bool param_node::add( const std::string& a_name, param&& a_value )
314  {
315  contents::iterator it = f_contents.find( a_name );
316  if( it == f_contents.end() )
317  {
318  f_contents.insert( contents_type( a_name, a_value.move_clone() ) );
319  return true;
320  }
321  return false;
322  }
323 
324  inline bool param_node::add( const std::string& a_name, param_ptr_t a_value_ptr )
325  {
326  contents::iterator it = f_contents.find( a_name );
327  if( it == f_contents.end() )
328  {
329  f_contents.insert( contents_type( a_name, std::move(a_value_ptr) ) );
330  return true;
331  }
332  return false;
333  }
334 
335  inline void param_node::replace( const std::string& a_name, const param& a_value )
336  {
337  f_contents[ a_name ] = a_value.clone();
338  return;
339  }
340 
341  inline void param_node::replace( const std::string& a_name, param&& a_value )
342  {
343  f_contents[ a_name ] = a_value.move_clone();
344  return;
345  }
346 
347  inline void param_node::replace( const std::string& a_name, param_ptr_t a_value_ptr )
348  {
349  f_contents[ a_name ] = std::move(a_value_ptr);
350  return;
351  }
352 
353  inline void param_node::erase( const std::string& a_name )
354  {
355  contents::iterator it = f_contents.find( a_name );
356  if( it != f_contents.end() )
357  {
358  f_contents.erase( it );
359  }
360  return;
361  }
362 
363  inline param_ptr_t param_node::remove( const std::string& a_name )
364  {
365  contents::iterator it = f_contents.find( a_name );
366  if( it != f_contents.end() )
367  {
368  param_ptr_t removed( std::move( it->second ) );
369  f_contents.erase( it );
370  return removed;
371  }
372  return param_ptr_t();
373  }
374 
375  inline void param_node::clear()
376  {
377  f_contents.clear();
378  return;
379  }
380 
382  {
383  return iterator( f_contents.begin() );
384  }
385 
387  {
388  return const_iterator( f_contents.cbegin() );
389  }
390 
392  {
393  return iterator( f_contents.end() );
394  }
395 
397  {
398  return const_iterator( f_contents.cend() );
399  }
400 
401  SCARAB_API std::ostream& operator<<(std::ostream& out, const param_node& value);
402 
403 } /* namespace scarab */
404 
405 #endif /* SCARAB_PARAM_NODE_HH_ */
void replace(const std::string &a_name, const param &a_value)
Creates a copy of a_value; overwrites if the key exits.
Definition: param_node.hh:335
const param_node & node_at(const std::string &a_name) const
Definition: param_node.hh:282
const param_value & value_at(const std::string &a_name) const
Definition: param_node.hh:262
unsigned count(const std::string &a_name) const
Definition: param_node.hh:232
void erase(const std::string &a_name)
Definition: param_node.hh:353
const param_array & array_at(const std::string &a_name) const
Definition: param_node.hh:272
iterator begin()
Definition: param_node.hh:381
#define SCARAB_API
Definition: scarab_api.hh:24
virtual bool is_null() const
Definition: param_node.hh:208
virtual param_ptr_t move_clone()
Definition: param_node.hh:203
std::string type(const x_type &a_param)
Definition: typename.hh:22
std::map< std::string, std::unique_ptr< param > > param_node_contents
Definition: param_node.hh:60
contents::value_type contents_type
Definition: param_node.hh:72
unsigned size() const
Definition: param_node.hh:218
map_deref_iterator(const map_deref_iterator< x_key, x_other_value, x_other_iiterator > &other, typename boost::enable_if< boost::is_convertible< x_other_value, x_value >, enabler >::type=enabler())
Definition: param_node.hh:41
param_node_iterator iterator
Definition: param_node.hh:70
virtual bool is_node() const
Definition: param_node.hh:213
param_value & as_value()
std::string get_value(const std::string &a_name) const
Definition: param_node.hh:237
bool has(const std::string &a_name) const
Definition: param_node.hh:227
const x_key & name() const
Definition: param_node.hh:45
const param & at(const std::string &a_name) const
Definition: param_node.hh:252
virtual param_ptr_t clone() const
Definition: param_node.hh:197
const param & operator[](const std::string &a_name) const
Definition: param_node.hh:292
param_node_const_iterator const_iterator
Definition: param_node.hh:71
bool empty() const
Definition: param_node.hh:222
SCARAB_API std::ostream & operator<<(std::ostream &out, const param_array &a_value)
Definition: param_array.cc:110
map_deref_iterator(const x_iiterator &other)
Definition: param_node.hh:35
std::unique_ptr< param > param_ptr_t
Definition: param_base.hh:23
param_node & as_node()
std::string to_string(std::uint64_t x)
Definition: date.h:7722
param_array & as_array()
param_ptr_t remove(const std::string &a_name)
Definition: param_node.hh:363
map_deref_iterator< std::string, param, param_node_contents::iterator > param_node_iterator
Definition: param_node.hh:62
bool add(const std::string &a_name, const param &a_value)
Areates a copy of a_value.
Definition: param_node.hh:302
param_node_contents contents
Definition: param_node.hh:69
x_value & dereference() const
Definition: param_node.hh:53
virtual param_ptr_t clone() const
map_deref_iterator< std::string, const param, param_node_contents::const_iterator > param_node_const_iterator
Definition: param_node.hh:63