Scarab  v2.0.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, 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  virtual ~param_node();
77 
78  param_node& operator=( const param_node& rhs );
79 
80  virtual param* clone() const;
81 
82  virtual bool is_null() const;
83  virtual bool is_node() const;
84 
85  virtual bool has_subset( const param& a_subset ) const;
86 
87  unsigned size() const;
88  bool empty() const;
89 
90  bool has( const std::string& a_name ) const;
91  unsigned count( const std::string& a_name ) const;
92 
95  std::string get_value( const std::string& a_name ) const;
98  template< typename XValType >
99  XValType get_value( const std::string& a_name ) const;
100 
103  std::string get_value( const std::string& a_name, const std::string& a_default ) const;
104  std::string get_value( const std::string& a_name, const char* a_default ) const;
107  template< typename XValType >
108  XValType get_value( const std::string& a_name, XValType a_default ) const;
109 
112  const param& at( const std::string& a_name ) const;
115  param& at( const std::string& a_name );
116 
119  const param_value& value_at( const std::string& a_name ) const;
122  param_value& value_at( const std::string& a_name );
123 
126  const param_array& array_at( const std::string& a_name ) const;
129  param_array& array_at( const std::string& a_name );
130 
133  const param_node& node_at( const std::string& a_name ) const;
136  param_node& node_at( const std::string& a_name );
137 
140  const param& operator[]( const std::string& a_name ) const;
143  param& operator[]( const std::string& a_name );
144 
146  bool add( const std::string& a_name, const param& a_value );
148  bool add( const std::string& a_name, param* a_value_ptr );
149 
151  void replace( const std::string& a_name, const param& a_value );
153  void replace( const std::string& a_name, param* a_value_ptr );
154 
158  void merge( const param_node& a_object );
159 
160  void erase( const std::string& a_name );
161  param* remove( const std::string& a_name );
162  void clear();
163 
164  iterator begin();
165  const_iterator begin() const;
166 
167  iterator end();
168  const_iterator end() const;
169 
170  virtual std::string to_string() const;
171 
172  protected:
173  contents f_contents;
174 
175  };
176 
177 
178  template< typename XValType >
179  inline XValType param_node::get_value( const std::string& a_name ) const
180  {
181  return value_at( a_name ).get< XValType >();
182  }
183 
184  template< typename XValType >
185  inline XValType param_node::get_value( const std::string& a_name, XValType a_default ) const
186  {
187  return has( a_name ) ? value_at( a_name ).get< XValType >() : a_default;
188  }
189 
190  inline param* param_node::clone() const
191  {
192  //std::cout << "param_node::clone" << std::endl;
193  return new param_node( *this );
194  }
195 
196  inline bool param_node::is_null() const
197  {
198  return false;
199  }
200 
201  inline bool param_node::is_node() const
202  {
203  return true;
204  }
205 
206  inline unsigned param_node::size() const
207  {
208  return f_contents.size();
209  }
210  inline bool param_node::empty() const
211  {
212  return f_contents.empty();
213  }
214 
215  inline bool param_node::has( const std::string& a_name ) const
216  {
217  return f_contents.count( a_name ) > 0;
218  }
219 
220  inline unsigned param_node::count( const std::string& a_name ) const
221  {
222  return f_contents.count( a_name );
223  }
224 
225  inline std::string param_node::get_value( const std::string& a_name ) const
226  {
227  return value_at( a_name ).to_string();
228  }
229 
230  inline std::string param_node::get_value( const std::string& a_name, const std::string& a_default ) const
231  {
232  return has( a_name ) ? value_at( a_name ).to_string() : a_default;
233  }
234 
235  inline std::string param_node::get_value( const std::string& a_name, const char* a_default ) const
236  {
237  return get_value( a_name, std::string( a_default ) );
238  }
239 
240  inline const param& param_node::at( const std::string& a_name ) const
241  {
242  return *f_contents.at( a_name );
243  }
244 
245  inline param& param_node::at( const std::string& a_name )
246  {
247  return *f_contents.at( a_name );
248  }
249 
250  inline const param_value& param_node::value_at( const std::string& a_name ) const
251  {
252  return at( a_name ).as_value();
253  }
254 
255  inline param_value& param_node::value_at( const std::string& a_name )
256  {
257  return at( a_name ).as_value();
258  }
259 
260  inline const param_array& param_node::array_at( const std::string& a_name ) const
261  {
262  return at( a_name ).as_array();
263  }
264 
265  inline param_array& param_node::array_at( const std::string& a_name )
266  {
267  return at( a_name ).as_array();
268  }
269 
270  inline const param_node& param_node::node_at( const std::string& a_name ) const
271  {
272  return at( a_name ).as_node();
273  }
274 
275  inline param_node& param_node::node_at( const std::string& a_name )
276  {
277  return at( a_name ).as_node();
278  }
279 
280  inline const param& param_node::operator[]( const std::string& a_name ) const
281  {
282  return *f_contents.at( a_name );
283  }
284 
285  inline param& param_node::operator[]( const std::string& a_name )
286  {
287  return *f_contents[ a_name ];
288  }
289 
290  inline bool param_node::add( const std::string& a_name, const param& a_value )
291  {
292  contents::iterator it = f_contents.find( a_name );
293  if( it == f_contents.end() )
294  {
295  f_contents.insert( contents_type( a_name, a_value.clone() ) );
296  return true;
297  }
298  return false;
299  }
300 
301  inline bool param_node::add( const std::string& a_name, param* a_value )
302  {
303  contents::iterator it = f_contents.find( a_name );
304  if( it == f_contents.end() )
305  {
306  f_contents.insert( contents_type( a_name, a_value ) );
307  return true;
308  }
309  return false;
310  }
311 
312  inline void param_node::replace( const std::string& a_name, const param& a_value )
313  {
314  erase( a_name );
315  f_contents[ a_name ] = a_value.clone();
316  return;
317  }
318 
319  inline void param_node::replace( const std::string& a_name, param* a_value )
320  {
321  erase( a_name );
322  f_contents[ a_name ] = a_value;
323  return;
324  }
325 
326  inline void param_node::erase( const std::string& a_name )
327  {
328  contents::iterator it = f_contents.find( a_name );
329  if( it != f_contents.end() )
330  {
331  delete it->second;
332  f_contents.erase( it );
333  }
334  return;
335  }
336 
337  inline param* param_node::remove( const std::string& a_name )
338  {
339  contents::iterator it = f_contents.find( a_name );
340  if( it != f_contents.end() )
341  {
342  param* removed = it->second;
343  f_contents.erase( it );
344  return removed;
345  }
346  return NULL;
347  }
348 
349  inline void param_node::clear()
350  {
351  for( contents::iterator it = f_contents.begin(); it != f_contents.end(); ++it )
352  {
353  delete it->second;
354  }
355  f_contents.clear();
356  return;
357  }
358 
360  {
361  return iterator( f_contents.begin() );
362  }
363 
365  {
366  return const_iterator( f_contents.cbegin() );
367  }
368 
370  {
371  return iterator( f_contents.end() );
372  }
373 
375  {
376  return const_iterator( f_contents.cend() );
377  }
378 
379  SCARAB_API std::ostream& operator<<(std::ostream& out, const param_node& value);
380 
381 } /* namespace scarab */
382 
383 #endif /* SCARAB_PARAM_NODE_HH_ */
void replace(const std::string &a_name, const param &a_value)
creates a copy of a_value
Definition: param_node.hh:312
const param_node & node_at(const std::string &a_name) const
Definition: param_node.hh:270
const param_value & value_at(const std::string &a_name) const
Definition: param_node.hh:250
unsigned count(const std::string &a_name) const
Definition: param_node.hh:220
void erase(const std::string &a_name)
Definition: param_node.hh:326
const param_array & array_at(const std::string &a_name) const
Definition: param_node.hh:260
iterator begin()
Definition: param_node.hh:359
#define SCARAB_API
Definition: scarab_api.hh:24
virtual bool is_null() const
Definition: param_node.hh:196
std::string type(const x_type &a_param)
Definition: typename.hh:22
virtual param * clone() const
Definition: param_node.hh:190
contents::value_type contents_type
Definition: param_node.hh:72
unsigned size() const
Definition: param_node.hh:206
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:201
std::map< std::string, param * > param_node_contents
Definition: param_node.hh:60
param_value & as_value()
std::string get_value(const std::string &a_name) const
Definition: param_node.hh:225
bool has(const std::string &a_name) const
Definition: param_node.hh:215
const x_key & name() const
Definition: param_node.hh:45
const param & at(const std::string &a_name) const
Definition: param_node.hh:240
const param & operator[](const std::string &a_name) const
Definition: param_node.hh:280
param_node_const_iterator const_iterator
Definition: param_node.hh:71
param * remove(const std::string &a_name)
Definition: param_node.hh:337
bool empty() const
Definition: param_node.hh:210
SCARAB_API std::ostream & operator<<(std::ostream &out, const param_array &a_value)
Definition: param_array.cc:97
map_deref_iterator(const x_iiterator &other)
Definition: param_node.hh:35
param_node & as_node()
std::string to_string(std::uint64_t x)
Definition: date.h:7722
param_array & as_array()
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)
creates a copy of a_value
Definition: param_node.hh:290
param_node_contents contents
Definition: param_node.hh:69
x_value & dereference() const
Definition: param_node.hh:53
map_deref_iterator< std::string, const param, param_node_contents::const_iterator > param_node_const_iterator
Definition: param_node.hh:63
virtual param * clone() const