Scarab  v2.2.1
Project 8 C++ Utility Library
param_array.hh
Go to the documentation of this file.
1 /*
2  * param_array.hh
3  *
4  * Created on: Jan 14, 2014
5  * Author: nsoblath
6  */
7 
8 #ifndef SCARAB_PARAM_ARRAY_HH_
9 #define SCARAB_PARAM_ARRAY_HH_
10 
11 #include "param_value.hh"
12 
13 #include <boost/iterator/iterator_adaptor.hpp>
14 #include <boost/type_traits/is_convertible.hpp>
15 #include <boost/utility/enable_if.hpp>
16 #include <boost/iterator/indirect_iterator.hpp>
17 
18 #include <deque>
19 
20 
21 namespace scarab
22 {
23  class param_value;
24  class param_node;
25 
26  typedef std::deque< std::unique_ptr< param > > param_array_contents;
27 
28  typedef boost::indirect_iterator< param_array_contents::iterator, param > param_array_iterator;
29  typedef boost::indirect_iterator< param_array_contents::const_iterator, const param > param_array_const_iterator;
30 
31  class SCARAB_API param_array : public param
32  {
33  public:
34  typedef param_array_contents contents;
35  typedef param_array_iterator iterator;
36  typedef param_array_const_iterator const_iterator;
37  typedef contents::reverse_iterator reverse_iterator;
38  typedef contents::const_reverse_iterator const_reverse_iterator;
39  typedef contents::value_type contents_type;
40 
41  public:
42  param_array();
43  param_array( const param_array& orig );
44  param_array( param_array&& orig );
45  virtual ~param_array();
46 
47  param_array& operator=( const param_array& rhs );
48  param_array& operator=( param_array&& rhs );
49 
50  virtual param_ptr_t clone() const;
51  virtual param_ptr_t move_clone();
52 
53  virtual bool is_null() const;
54  virtual bool is_array() const;
55 
56  virtual bool has_subset( const param& a_subset ) const;
57 
58  unsigned size() const;
59  bool empty() const;
60 
63  void resize( unsigned a_size );
64 
67  std::string get_value( unsigned a_index, const std::string& a_default ) const;
68  std::string get_value( unsigned a_index, const char* a_default ) const;
71  template< typename XValType >
72  XValType get_value( unsigned a_index, XValType a_default ) const;
73 
76  const param& operator[]( unsigned a_index ) const;
79  param& operator[]( unsigned a_index );
80 
81  const param& front() const;
82  param& front();
83 
84  const param& back() const;
85  param& back();
86 
87  // assign a copy of a_value to the array at a_index
88  void assign( unsigned a_index, const param& a_value );
89  // directly assign a_value to the array at a_index
90  void assign( unsigned a_index, param&& a_value );
91  // directly assign a_value_ptr to the array at a_index
92  void assign( unsigned a_index, param_ptr_t a_value_ptr );
93  // directly assign a_value to the array at a_index; allows implicit construction with raw types (int, string, etc)
94  void assign( unsigned a_index, param_value&& a_value );
95 
96  void push_back( const param& a_value );
97  void push_back( param&& a_value );
98  void push_back( param_ptr_t a_value_ptr );
99  void push_back( param_value&& a_value );
100 
101  void push_front( const param& a_value );
102  void push_front( param&& a_value );
103  void push_front( param_ptr_t a_value_ptr );
104  void push_front( param_value&& a_value );
105 
106  void append( const param_array& an_array );
107 
108  void erase( unsigned a_index );
109  param_ptr_t remove( unsigned a_index );
110  void clear();
111 
112  iterator begin();
113  const_iterator begin() const;
114 
115  iterator end();
116  const_iterator end() const;
117 
118  reverse_iterator rbegin();
119  const_reverse_iterator rbegin() const;
120 
121  reverse_iterator rend();
122  const_reverse_iterator rend() const;
123 
124  virtual std::string to_string() const;
125 
126  protected:
127  contents f_contents;
128  };
129 
130 
131  template< typename XValType >
132  XValType param_array::get_value( unsigned a_index, XValType a_default ) const
133  {
134  return a_index < size() ? operator[]( a_index ).as_value().as< XValType >() : a_default;
135  }
136 
138  {
139  return std::unique_ptr< param_array >( new param_array( *this ) );
140  }
141 
143  {
144  return std::unique_ptr< param_array >( new param_array( std::move( *this ) ) );
145  }
146 
147  inline bool param_array::is_null() const
148  {
149  return false;
150  }
151 
152  inline bool param_array::is_array() const
153  {
154  return true;
155  }
156 
157  inline unsigned param_array::size() const
158  {
159  return f_contents.size();
160  }
161  inline bool param_array::empty() const
162  {
163  return f_contents.empty();
164  }
165 
166  inline void param_array::resize( unsigned a_size )
167  {
168  f_contents.resize( a_size );
169  return;
170  }
171 
172  inline std::string param_array::get_value( unsigned a_index, const std::string& a_default ) const
173  {
174  return a_index < size() ? operator[]( a_index ).to_string() : a_default;
175  }
176 
177  inline std::string param_array::get_value( unsigned a_index, const char* a_default ) const
178  {
179  return get_value( a_index, std::string( a_default ) );
180  }
181 
182  inline const param& param_array::operator[]( unsigned a_index ) const
183  {
184  return *f_contents.at( a_index );
185  }
186  inline param& param_array::operator[]( unsigned a_index )
187  {
188  return *f_contents.at( a_index );
189  }
190 
191  inline const param& param_array::front() const
192  {
193  return *f_contents.front();
194  }
196  {
197  return *f_contents.front();
198  }
199 
200  inline const param& param_array::back() const
201  {
202  return *f_contents.back();
203  }
205  {
206  return *f_contents.back();
207  }
208 
209  // assign a copy of a_value to the array at a_index
210  inline void param_array::assign( unsigned a_index, const param& a_value )
211  {
212  erase( a_index );
213  f_contents[ a_index ] = a_value.clone();
214  return;
215  }
216  // directly move a_value to the array at a_index
217  inline void param_array::assign( unsigned a_index, param&& a_value )
218  {
219  erase( a_index );
220  f_contents[ a_index ] = a_value.move_clone();
221  return;
222  }
223  // directly assign a_value_ptr to the array at a_index
224  inline void param_array::assign( unsigned a_index, param_ptr_t a_value_ptr )
225  {
226  erase( a_index );
227  f_contents[ a_index ] = std::move(a_value_ptr);
228  return;
229  }
230  // directly move a_value to the array at a_index
231  inline void param_array::assign( unsigned a_index, param_value&& a_value )
232  {
233  erase( a_index );
234  f_contents[ a_index ] = a_value.move_clone();
235  return;
236  }
237 
238  inline void param_array::push_back( const param& a_value )
239  {
240  f_contents.push_back( a_value.clone() );
241  return;
242  }
243  inline void param_array::push_back( param&& a_value )
244  {
245  f_contents.push_back( a_value.move_clone() );
246  return;
247  }
248  inline void param_array::push_back( param_ptr_t a_value_ptr )
249  {
250  f_contents.push_back( std::move(a_value_ptr) );
251  return;
252  }
253  inline void param_array::push_back( param_value&& a_value )
254  {
255  f_contents.push_back( a_value.move_clone() );
256  return;
257  }
258 
259  inline void param_array::push_front( const param& a_value )
260  {
261  f_contents.push_front( a_value.clone() );
262  return;
263  }
264  inline void param_array::push_front( param&& a_value )
265  {
266  f_contents.push_front( a_value.move_clone() );
267  return;
268  }
269  inline void param_array::push_front( param_ptr_t a_value_ptr )
270  {
271  f_contents.push_front( std::move(a_value_ptr) );
272  return;
273  }
274  inline void param_array::push_front( param_value&& a_value )
275  {
276  f_contents.push_front( a_value.move_clone() );
277  return;
278  }
279 
280  inline void param_array::append( const param_array& an_array )
281  {
282  for( const_iterator it = const_iterator(an_array.begin()); it != const_iterator(an_array.end()); ++it )
283  {
284  push_back( *it );
285  }
286  return;
287  }
288 
289  inline void param_array::erase( unsigned a_index )
290  {
291  f_contents[ a_index ].reset();
292  return;
293  }
294  inline param_ptr_t param_array::remove( unsigned a_index )
295  {
296  param_ptr_t t_current( std::move( f_contents[ a_index ] ) );
297  return t_current;
298  }
299  inline void param_array::clear()
300  {
301  f_contents.clear();
302  return;
303  }
304 
306  {
307  return iterator( f_contents.begin() );
308  }
310  {
311  return const_iterator( f_contents.cbegin() );
312  }
313 
315  {
316  return iterator( f_contents.end() );
317  }
319  {
320  return const_iterator( f_contents.cend() );
321  }
322 
324  {
325  return f_contents.rbegin();
326  }
328  {
329  return f_contents.rbegin();
330  }
331 
333  {
334  return f_contents.rend();
335  }
337  {
338  return f_contents.crend();
339  }
340 
341  SCARAB_API std::ostream& operator<<(std::ostream& out, const param_array& value);
342 
343 } /* namespace scarab */
344 
345 #endif /* SCARAB_PARAM_ARRAY_HH_ */
std::deque< std::unique_ptr< param > > param_array_contents
Definition: param_array.hh:24
boost::indirect_iterator< param_array_contents::iterator, param > param_array_iterator
Definition: param_array.hh:28
std::string get_value(unsigned a_index, const std::string &a_default) const
Definition: param_array.hh:172
param_array_iterator iterator
Definition: param_array.hh:35
void push_front(const param &a_value)
Definition: param_array.hh:259
boost::indirect_iterator< param_array_contents::const_iterator, const param > param_array_const_iterator
Definition: param_array.hh:29
#define SCARAB_API
Definition: scarab_api.hh:24
virtual param_ptr_t move_clone()
Definition: param_array.hh:142
virtual bool is_array() const
Definition: param_array.hh:152
virtual param_ptr_t clone() const
Definition: param_array.hh:137
virtual bool is_null() const
Definition: param_array.hh:147
contents::const_reverse_iterator const_reverse_iterator
Definition: param_array.hh:38
unsigned size() const
Definition: param_array.hh:157
void append(const param_array &an_array)
Definition: param_array.hh:280
param_array_contents contents
Definition: param_array.hh:34
param_ptr_t remove(unsigned a_index)
Definition: param_array.hh:294
bool empty() const
Definition: param_array.hh:161
reverse_iterator rend()
Definition: param_array.hh:332
reverse_iterator rbegin()
Definition: param_array.hh:323
SCARAB_API std::ostream & operator<<(std::ostream &out, const param_array &a_value)
Definition: param_array.cc:110
std::unique_ptr< param > param_ptr_t
Definition: param_base.hh:23
const param & operator[](unsigned a_index) const
Definition: param_array.hh:182
std::string to_string(std::uint64_t x)
Definition: date.h:7722
const param & front() const
Definition: param_array.hh:191
void erase(unsigned a_index)
Definition: param_array.hh:289
void assign(unsigned a_index, const param &a_value)
Definition: param_array.hh:210
contents::reverse_iterator reverse_iterator
Definition: param_array.hh:37
param_array_const_iterator const_iterator
Definition: param_array.hh:36
void push_back(const param &a_value)
Definition: param_array.hh:238
contents::value_type contents_type
Definition: param_array.hh:39
virtual param_ptr_t clone() const
const param & back() const
Definition: param_array.hh:200
void resize(unsigned a_size)
Definition: param_array.hh:166