Scarab  v2.2.3
Project 8 C++ Utility Library
param_array.cc
Go to the documentation of this file.
1 /*
2  * param_array.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_array.hh"
15 
16 #include "param_base_impl.hh"
17 #include "param_node.hh"
18 
19 
20 namespace scarab
21 {
22 
24  param(),
25  f_contents()
26  {
27  }
28 
30  param( orig ),
31  f_contents( orig.f_contents.size() )
32  {
33  for( unsigned ind = 0; ind < f_contents.size(); ++ind )
34  {
35  f_contents[ind] = orig.f_contents[ ind ]->clone();
36  }
37  }
38 
40  param( std::move(orig) ),
41  f_contents( orig.f_contents.size() )
42  {
43  for( unsigned ind = 0; ind < f_contents.size(); ++ind )
44  {
45  f_contents[ind] = orig.f_contents[ ind ]->move_clone();
46  }
47  orig.clear();
48  }
49 
51  {
52  }
53 
55  {
56  this->param::operator=( rhs );
57  clear();
58  resize( rhs.size()) ;
59  for( unsigned ind = 0; ind < rhs.f_contents.size(); ++ind )
60  {
61  f_contents[ind] = rhs.f_contents[ ind ]->clone();
62  }
63  return *this;
64  }
65 
67  {
68  this->param::operator=( std::move(rhs) );
69  clear();
70  resize( rhs.size()) ;
71  for( unsigned ind = 0; ind < rhs.f_contents.size(); ++ind )
72  {
73  f_contents[ind] = rhs.f_contents[ ind ]->move_clone();
74  }
75  rhs.clear();
76  return *this;
77  }
78 
79  void param_array::resize( unsigned a_size )
80  {
81  f_contents.resize( a_size );
82  for( auto it = f_contents.begin(); it != f_contents.end(); ++it )
83  {
84  if( ! *it ) it->reset( new param() );
85  }
86  return;
87  }
88 
89  bool param_array::has_subset( const param& a_subset ) const
90  {
91  if( ! a_subset.is_array() ) return false;
92  const param_array& t_subset_array = a_subset.as_array();
93  if( t_subset_array.size() > f_contents.size() ) return false;
94  contents::const_iterator t_this_it = f_contents.begin();
95  contents::const_iterator t_that_it = t_subset_array.f_contents.begin();
96  while( t_that_it != t_subset_array.f_contents.end() ) // loop condition is on a_subset because it's smaller or equal to this
97  {
98  if( ! (*t_this_it)->has_subset( **t_that_it ) ) return false;
99  ++t_this_it;
100  ++t_that_it;
101  }
102  return true;
103  }
104 
105  void param_array::merge( const param_array& a_object )
106  {
107  //LDEBUG( dlog, "merging array with " << a_object.size() << " items:\n" << a_object );
108  if( size() < a_object.size() ) resize( a_object.size() );
109 
110  for( unsigned index = 0; index < size(); ++index )
111  {
112  if( f_contents.at( index )->is_null() )
113  {
114  //LDEBUG( dlog, "have a null object at <" << index << ">; adding <" << a_object[index] << ">" );
115  assign( index, a_object[index] );
116  continue;
117  }
118 
119  param& t_param = (*this)[index];
120  if( t_param.is_value() && a_object[index].is_value() )
121  {
122  //LDEBUG( dlog, "replacing the value at <" << index << "> with <" << a_object[index] << ">" );
123  t_param.as_value() = a_object[index].as_value();
124  continue;
125  }
126  if( t_param.is_node() && a_object[index].is_node() )
127  {
128  //LDEBUG( dlog, "merging nodes at <" << index << ">" )
129  t_param.as_node().merge( a_object[index].as_node() );
130  continue;
131  }
132  if( t_param.is_array() && a_object[index].is_array() )
133  {
134  //LDEBUG( dlog, "merging array at <" << index << ">" );
135  t_param.as_array().merge( a_object[index].as_array() );
136  continue;
137  }
138 
139  //LDEBUG( dlog, "generic replace" );
140  assign( index, a_object[index] );
141  }
142  return;
143  }
144 
145 
146  std::string param_array::to_string() const
147  {
148  stringstream out;
149  string indentation;
150  for ( unsigned i=0; i<param::s_indent_level; ++i )
151  indentation += " ";
152  out << '\n' << indentation << "[\n";
153  param::s_indent_level++;
154  for( contents::const_iterator it = f_contents.begin(); it != f_contents.end(); ++it )
155  {
156  out << indentation << " " << **it << '\n';
157  }
158  param::s_indent_level--;
159  out << indentation << "]\n";
160  return out.str();
161  }
162 
163 
164  SCARAB_API std::ostream& operator<<(std::ostream& out, const param_array& a_value)
165  {
166  return out << a_value.to_string();
167  }
168 
169 } /* namespace scarab */
virtual bool is_node() const
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_array.cc:89
param_array & operator=(const param_array &rhs)
Definition: param_array.cc:54
virtual std::string to_string() const
Definition: param_array.cc:146
virtual bool is_value() const
virtual ~param_array()
Definition: param_array.cc:50
#define SCARAB_API
Definition: scarab_api.hh:24
void merge(const param_array &an_array)
Definition: param_array.cc:105
STL namespace.
virtual bool is_array() const
Definition: param_array.hh:157
static unsigned s_indent_level
Definition: param_base.hh:100
param_value & as_value()
unsigned size() const
Definition: param_array.hh:162
SCARAB_API std::ostream & operator<<(std::ostream &out, const param_array &a_value)
Definition: param_array.cc:164
param_node & as_node()
void assign(unsigned a_index, const param &a_value)
Definition: param_array.hh:209
param_array & as_array()
virtual bool is_array() const
void resize(unsigned a_size)
Definition: param_array.cc:79