Scarab  v2.4.9
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 < a_object.size(); ++index )
111  {
112  // directly assign if destination location is empty
113  if( f_contents.at( index )->is_null() )
114  {
115  //LDEBUG( dlog, "have a null object at <" << index << ">; adding <" << a_object[index] << ">" );
116  assign( index, a_object[index] );
117  continue;
118  }
119 
120  // overwrite/recurse if destination location matches incoming type
121  param& t_param = (*this)[index];
122  if( t_param.is_value() && a_object[index].is_value() )
123  {
124  //LDEBUG( dlog, "replacing the value at <" << index << "> with <" << a_object[index] << ">" );
125  t_param.as_value() = a_object[index].as_value();
126  continue;
127  }
128  if( t_param.is_node() && a_object[index].is_node() )
129  {
130  //LDEBUG( dlog, "merging nodes at <" << index << ">" )
131  t_param.as_node().merge( a_object[index].as_node() );
132  continue;
133  }
134  if( t_param.is_array() && a_object[index].is_array() )
135  {
136  //LDEBUG( dlog, "merging array at <" << index << ">" );
137  t_param.as_array().merge( a_object[index].as_array() );
138  continue;
139  }
140 
141  // overwrite via direct assignment if destination location does not match incoming type
142  //LDEBUG( dlog, "generic replace" );
143  assign( index, a_object[index] );
144  }
145  return;
146  }
147 
148 
149  std::string param_array::to_string() const
150  {
151  stringstream out;
152  string indentation;
153  for ( unsigned i=0; i<param::s_indent_level; ++i )
154  indentation += " ";
155  out << '\n' << indentation << "[\n";
156  param::s_indent_level++;
157  for( contents::const_iterator it = f_contents.begin(); it != f_contents.end(); ++it )
158  {
159  out << indentation << " " << **it << '\n';
160  }
161  param::s_indent_level--;
162  out << indentation << "]\n";
163  return out.str();
164  }
165 
166 
167  SCARAB_API std::ostream& operator<<(std::ostream& out, const param_array& a_value)
168  {
169  return out << a_value.to_string();
170  }
171 
172 } /* namespace scarab */
param & operator=(const param &rhs)
void merge(const param_node &a_object)
Definition: param_node.cc:90
virtual bool is_value() const
param_array & operator=(const param_array &rhs)
Definition: param_array.cc:54
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.
static unsigned s_indent_level
Definition: param_base.hh:100
virtual std::string to_string() const
Definition: param_array.cc:149
param_value & as_value()
virtual bool is_array() const
Definition: param_array.hh:157
virtual bool is_array() const
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()
void assign(unsigned a_index, const param &a_value)
Definition: param_array.hh:209
param_array & as_array()
virtual bool has_subset(const param &a_subset) const
Definition: param_array.cc:89
unsigned size() const
Definition: param_array.hh:162
void resize(unsigned a_size)
Definition: param_array.cc:79