Scarab  v2.2.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  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  param& t_param = (*this)[ it->first ];
102  if( t_param.is_value() )
103  {
104  //LDEBUG( dlog, "replacing the value of \"" << it->first << "\" <" << get_value( it->first ) << "> with <" << *it->second << ">" );
105  replace( it->first, *it->second );
106  continue;
107  }
108  if( t_param.is_node() && it->second->is_node() )
109  {
110  //LDEBUG( dlog, "merging nodes")
111  t_param.as_node().merge( it->second->as_node() );
112  continue;
113  }
114  if( t_param.is_array() && it->second->is_array() )
115  {
116  //LDEBUG( dlog, "appending array" );
117  t_param.as_array().append( it->second->as_array() );
118  continue;
119  }
120  //LDEBUG( dlog, "generic replace" );
121  this->replace( it->first, *it->second );
122  }
123  }
124 
125  std::string param_node::to_string() const
126  {
127  stringstream out;
128  string indentation;
129  for ( unsigned i=0; i<param::s_indent_level; ++i )
130  indentation += " ";
131  out << '\n' << indentation << "{\n";
132  param::s_indent_level++;
133  for( contents::const_iterator it = f_contents.begin(); it != f_contents.end(); ++it )
134  {
135  out << indentation << " " << it->first << " : " << *(it->second) << '\n';
136  }
137  param::s_indent_level--;
138  out << indentation << "}\n";
139  return out.str();
140  }
141 
142  SCARAB_API std::ostream& operator<<(std::ostream& out, const param_node& a_value)
143  {
144  return out << a_value.to_string();
145  }
146 
147 } /* namespace scarab */
virtual bool is_node() const
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:270
param & operator=(const param &rhs)
void merge(const param_node &a_object)
Definition: param_node.cc:90
virtual bool has_subset(const param &a_subset) const
Definition: param_node.cc:77
virtual bool is_value() const
#define SCARAB_API
Definition: scarab_api.hh:24
STL namespace.
static unsigned s_indent_level
Definition: param_base.hh:98
unsigned size() const
Definition: param_node.hh:187
bool has(const std::string &a_name) const
Definition: param_node.hh:196
param_node & operator=(const param_node &rhs)
Definition: param_node.cc:54
void append(const param_array &an_array)
Definition: param_array.hh:280
virtual std::string to_string() const
Definition: param_node.cc:125
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:110
param_node & as_node()
param_array & as_array()
bool add(const std::string &a_name, const param &a_value)
Definition: param_node.hh:226
virtual bool is_array() const