Scarab  v2.0.0
Project 8 C++ Utility Library
param_node.cc
Go to the documentation of this file.
1 /*
2  * param_node.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_node.hh"
15 
16 #include "param_array.hh"
17 #include "param_base_impl.hh"
18 
19 
20 namespace scarab
21 {
22 
24  param(),
25  f_contents()
26  {
27  }
28 
30  param( orig ),
31  f_contents()
32  {
33  for( contents::const_iterator it = orig.f_contents.begin(); it != orig.f_contents.end(); ++it )
34  {
35  add( it->first, it->second->clone() );
36  }
37  }
38 
40  {
41  clear();
42  }
43 
45  {
46  clear();
47  for( contents::const_iterator it = rhs.f_contents.begin(); it != rhs.f_contents.end(); ++it )
48  {
49  this->replace( it->first, *it->second );
50  }
51  return *this;
52  }
53 
54  bool param_node::has_subset( const param& a_subset ) const
55  {
56  if( ! a_subset.is_node() ) return false;
57  const param_node& t_subset_node = a_subset.as_node();
58  if( t_subset_node.size() > f_contents.size() ) return false;
59  for( contents::const_iterator t_subset_it = t_subset_node.f_contents.begin(); t_subset_it != t_subset_node.f_contents.end(); ++t_subset_it )
60  {
61  if( ! has( t_subset_it->first ) ) return false;
62  if( ! f_contents.at( t_subset_it->first )->has_subset( *t_subset_it->second ) ) return false;
63  }
64  return true;
65  }
66 
67  void param_node::merge( const param_node& a_object )
68  {
69  //LDEBUG( dlog, "merging object with " << a_object.size() << " items:\n" << a_object );
70  for( contents::const_iterator it = a_object.f_contents.begin(); it != a_object.f_contents.end(); ++it )
71  {
72  if( ! has( it->first ) )
73  {
74  //LDEBUG( dlog, "do not have object <" << it->first << "> = <" << *it->second << ">" );
75  add( it->first, *it->second );
76  continue;
77  }
78  param& t_param = (*this)[ it->first ];
79  if( t_param.is_value() )
80  {
81  //LDEBUG( dlog, "replacing the value of \"" << it->first << "\" <" << get_value( it->first ) << "> with <" << *it->second << ">" );
82  replace( it->first, *it->second );
83  continue;
84  }
85  if( t_param.is_node() && it->second->is_node() )
86  {
87  //LDEBUG( dlog, "merging nodes")
88  t_param.as_node().merge( it->second->as_node() );
89  continue;
90  }
91  if( t_param.is_array() && it->second->is_array() )
92  {
93  //LDEBUG( dlog, "appending array" );
94  t_param.as_array().append( it->second->as_array() );
95  continue;
96  }
97  //LDEBUG( dlog, "generic replace" );
98  this->replace( it->first, *it->second );
99  }
100  }
101 
102  std::string param_node::to_string() const
103  {
104  stringstream out;
105  string indentation;
106  for ( unsigned i=0; i<param::s_indent_level; ++i )
107  indentation += " ";
108  out << '\n' << indentation << "{\n";
109  param::s_indent_level++;
110  for( contents::const_iterator it = f_contents.begin(); it != f_contents.end(); ++it )
111  {
112  out << indentation << " " << it->first << " : " << *(it->second) << '\n';
113  }
114  param::s_indent_level--;
115  out << indentation << "}\n";
116  return out.str();
117  }
118 
119  SCARAB_API std::ostream& operator<<(std::ostream& out, const param_node& a_value)
120  {
121  return out << a_value.to_string();
122  }
123 
124 } /* namespace scarab */
virtual bool is_node() const
void replace(const std::string &a_name, const param &a_value)
creates a copy of a_value
Definition: param_node.hh:312
void merge(const param_node &a_object)
Definition: param_node.cc:67
virtual bool has_subset(const param &a_subset) const
Definition: param_node.cc:54
virtual bool is_value() const
#define SCARAB_API
Definition: scarab_api.hh:24
static unsigned s_indent_level
Definition: param_base.hh:67
unsigned size() const
Definition: param_node.hh:206
bool has(const std::string &a_name) const
Definition: param_node.hh:215
param_node & operator=(const param_node &rhs)
Definition: param_node.cc:44
void append(const param_array &an_array)
Definition: param_array.hh:307
virtual std::string to_string() const
Definition: param_node.cc:102
virtual ~param_node()
Definition: param_node.cc:39
SCARAB_API std::ostream & operator<<(std::ostream &out, const param_array &a_value)
Definition: param_array.cc:97
param_node & as_node()
param_array & as_array()
bool add(const std::string &a_name, const param &a_value)
creates a copy of a_value
Definition: param_node.hh:290
virtual bool is_array() const