Scarab  v2.0.0
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 
17 namespace scarab
18 {
19 
21  param(),
22  f_contents()
23  {
24  }
25 
27  param( orig ),
28  f_contents( orig.f_contents.size() )
29  {
30  for( unsigned ind = 0; ind < f_contents.size(); ++ind )
31  {
32  this->assign( ind, orig[ ind ].clone() );
33  }
34  }
35 
37  {
38  clear();
39  }
40 
42  {
43  clear();
44  resize(rhs.size());
45  for( unsigned ind = 0; ind < rhs.f_contents.size(); ++ind )
46  {
47  this->assign( ind, rhs[ ind ].clone() );
48  }
49  return *this;
50  }
51 
52  bool param_array::has_subset( const param& a_subset ) const
53  {
54  if( ! a_subset.is_array() ) return false;
55  const param_array& t_subset_array = a_subset.as_array();
56  if( t_subset_array.size() > f_contents.size() ) return false;
57  contents::const_iterator t_this_it = f_contents.begin();
58  contents::const_iterator t_that_it = t_subset_array.f_contents.begin();
59  while( t_that_it != t_subset_array.f_contents.end() ) // loop condition is on a_subset because it's smaller or equal to this
60  {
61  if( ! (*t_this_it)->has_subset( **t_that_it ) ) return false;
62  ++t_this_it;
63  ++t_that_it;
64  }
65  return true;
66  }
67 
68  void param_array::resize( unsigned a_size )
69  {
70  unsigned curr_size = f_contents.size();
71  for( unsigned ind = a_size; ind < curr_size; ++ind )
72  {
73  delete f_contents[ ind ];
74  }
75  f_contents.resize( a_size );
76  return;
77  }
78 
79  std::string param_array::to_string() const
80  {
81  stringstream out;
82  string indentation;
83  for ( unsigned i=0; i<param::s_indent_level; ++i )
84  indentation += " ";
85  out << '\n' << indentation << "[\n";
86  param::s_indent_level++;
87  for( contents::const_iterator it = f_contents.begin(); it != f_contents.end(); ++it )
88  {
89  out << indentation << " " << **it << '\n';
90  }
91  param::s_indent_level--;
92  out << indentation << "]\n";
93  return out.str();
94  }
95 
96 
97  SCARAB_API std::ostream& operator<<(std::ostream& out, const param_array& a_value)
98  {
99  return out << a_value.to_string();
100  }
101 
102 } /* namespace scarab */
virtual bool has_subset(const param &a_subset) const
Definition: param_array.cc:52
param_array & operator=(const param_array &rhs)
Definition: param_array.cc:41
virtual std::string to_string() const
Definition: param_array.cc:79
virtual ~param_array()
Definition: param_array.cc:36
#define SCARAB_API
Definition: scarab_api.hh:24
virtual param * clone() const
Definition: param_array.hh:168
static unsigned s_indent_level
Definition: param_base.hh:67
unsigned size() const
Definition: param_array.hh:183
SCARAB_API std::ostream & operator<<(std::ostream &out, const param_array &a_value)
Definition: param_array.cc:97
void assign(unsigned a_index, const param &a_value)
Definition: param_array.hh:271
param_array & as_array()
virtual bool is_array() const
void resize(unsigned a_size)
Definition: param_array.cc:68