Scarab  v3.5.3
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  f_contents[it->first] = it->second->clone();
36  }
37  }
38 
40  param( std::move(orig) ),
41  f_contents()
42  {
43  for( contents::const_iterator it = orig.f_contents.begin(); it != orig.f_contents.end(); ++it )
44  {
45  f_contents[it->first] = it->second->move_clone();
46  }
47  orig.clear();
48  }
49 
51  {
52  }
53 
55  {
56  this->param::operator=( rhs );
57  clear();
58  for( contents::const_iterator it = rhs.f_contents.begin(); it != rhs.f_contents.end(); ++it )
59  {
60  f_contents[it->first] = it->second->clone();
61  }
62  return *this;
63  }
64 
66  {
67  this->param::operator=( std::move(rhs) );
68  clear();
69  for( contents::const_iterator it = rhs.f_contents.begin(); it != rhs.f_contents.end(); ++it )
70  {
71  f_contents[it->first] = it->second->move_clone();
72  }
73  rhs.clear();
74  return *this;
75  }
76 
77  bool param_node::has_subset( const param& a_subset ) const
78  {
79  if( ! a_subset.is_node() ) return false;
80  const param_node& t_subset_node = a_subset.as_node();
81  if( t_subset_node.size() > f_contents.size() ) return false;
82  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 )
83  {
84  if( ! has( t_subset_it->first ) ) return false;
85  if( ! f_contents.at( t_subset_it->first )->has_subset( *t_subset_it->second ) ) return false;
86  }
87  return true;
88  }
89 
90  void param_node::merge( const param_node& a_object )
91  {
92  //LDEBUG( dlog, "merging object with " << a_object.size() << " items:\n" << a_object );
93  for( contents::const_iterator it = a_object.f_contents.begin(); it != a_object.f_contents.end(); ++it )
94  {
95  if( ! has( it->first ) )
96  {
97  //LDEBUG( dlog, "do not have object <" << it->first << "> = <" << *it->second << ">" );
98  add( it->first, *it->second );
99  continue;
100  }
101 
102  param& t_param = (*this)[ it->first ];
103  if( t_param.is_value() && it->second->is_value() )
104  {
105  //LDEBUG( dlog, "replacing the value of \"" << it->first << "\" <" << get_value( it->first ) << "> with <" << *it->second << ">" );
106  t_param.as_value() = it->second->as_value();
107  continue;
108  }
109  if( t_param.is_node() && it->second->is_node() )
110  {
111  //LDEBUG( dlog, "merging nodes")
112  t_param.as_node().merge( it->second->as_node() );
113  continue;
114  }
115  if( t_param.is_array() && it->second->is_array() )
116  {
117  //LDEBUG( dlog, "merging array" );
118  t_param.as_array().merge( it->second->as_array() );
119  continue;
120  }
121 
122  //LDEBUG( dlog, "generic replace" );
123  this->replace( it->first, *it->second );
124  }
125  }
126 
127  std::string param_node::to_string() const
128  {
129  stringstream out;
130  string indentation;
131  for ( unsigned i=0; i<param::s_indent_level; ++i )
132  indentation += " ";
133  out << '\n' << indentation << "{\n";
134  param::s_indent_level++;
135  for( contents::const_iterator it = f_contents.begin(); it != f_contents.end(); ++it )
136  {
137  out << indentation << " " << it->first << " : " << *(it->second) << '\n';
138  }
139  param::s_indent_level--;
140  out << indentation << "}\n";
141  return out.str();
142  }
143 
144  SCARAB_API std::ostream& operator<<(std::ostream& out, const param_node& a_value)
145  {
146  return out << a_value.to_string();
147  }
148 
149 } /* namespace scarab */
unsigned size() const
Definition: param_node.hh:189
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:274
param & operator=(const param &rhs)
void merge(const param_node &a_object)
Definition: param_node.cc:90
virtual bool is_value() const
#define SCARAB_API
Definition: scarab_api.hh:24
void merge(const param_array &an_array)
Definition: param_array.cc:105
STL namespace.
static unsigned s_indent_level
Definition: param_base.hh:100
virtual bool has_subset(const param &a_subset) const
Definition: param_node.cc:77
param_value & as_value()
param_node & operator=(const param_node &rhs)
Definition: param_node.cc:54
virtual bool is_array() const
virtual ~param_node()
Definition: param_node.cc:50
SCARAB_API std::ostream & operator<<(std::ostream &out, const param_array &a_value)
Definition: param_array.cc:167
virtual bool is_node() const
param_node & as_node()
param_array & as_array()
bool add(const std::string &a_name, const param &a_value)
Definition: param_node.hh:228
bool has(const std::string &a_name) const
Definition: param_node.hh:198
virtual std::string to_string() const
Definition: param_node.cc:127