array_dynamic_no_mem_check.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Ifdefs
10 *****************************************************************************/
11 
12 #ifndef ECL_CONTAINERS_ARRAY_DYNAMIC_NO_MEM_CHECK_HPP_
13 #define ECL_CONTAINERS_ARRAY_DYNAMIC_NO_MEM_CHECK_HPP_
14 
15 /*****************************************************************************
16 ** Includes
17 *****************************************************************************/
18 
19 #include <algorithm>
20 #include <ecl/config/macros.hpp>
22 #include "../definitions.hpp"
23 #include "../initialiser.hpp"
24 #include "../stencil.hpp"
25 #include "array_no_mem_check.hpp"
26 
27 /*****************************************************************************
28 ** Namespaces
29 *****************************************************************************/
30 
31 namespace ecl {
32 
33 /*****************************************************************************
34 ** Forward Declarations
35 *****************************************************************************/
36 
37 template <typename Type> class Array<Type,DynamicStorage>;
38 
39 namespace blueprints {
40 
41 template <typename Type> class ConstantDynamicArray;
42 
43 } // namespace blueprints
44 
45 namespace formatters {
46 
47 template <typename Type, size_t N> class ArrayFormatter;
48 
49 } // namespace formatters
50 
51 /*****************************************************************************
52 ** BluePrintFactory
53 *****************************************************************************/
62 template<typename Type>
63 class ECL_PUBLIC BluePrintFactory< Array<Type,DynamicStorage> > {
64  public:
73  static blueprints::ConstantDynamicArray<Type> Constant(size_t size, const Type &value) {
74  return blueprints::ConstantDynamicArray<Type>(size,value);
75  }
76  virtual ~BluePrintFactory() {};
77 };
78 
79 /*****************************************************************************
80 ** Interface [Array][Dynamic]
81 *****************************************************************************/
128 template<typename Type>
129 class ECL_PUBLIC Array<Type,DynamicStorage> : public BluePrintFactory< Array<Type,DynamicStorage> > {
130  public:
131  /*********************
132  ** Typedefs
133  **********************/
134  typedef Type value_type;
135  typedef Type* iterator;
136  typedef const Type* const_iterator;
137  typedef Type& reference;
138  typedef const Type& const_reference;
139  typedef std::size_t size_type;
140  typedef std::ptrdiff_t difference_type;
141  typedef std::reverse_iterator<iterator> reverse_iterator;
142  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
147 
148  /*********************
149  ** Constructors
150  **********************/
156  explicit Array() : buffer_size(0), buffer(NULL) {}
165  explicit Array(const unsigned int reserve_size) : buffer_size(reserve_size), buffer(NULL) {
166  buffer = new Type[reserve_size];
167  };
175  Array(const Array<Type,DynamicStorage>& array) :
176  Factory(),
177  buffer_size(0),
178  buffer(NULL)
179  {
180  if ( array.size() != 0 ) {
181  resize(array.size());
182  std::copy(array.begin(),array.end(),begin());
183  }
184  }
205  template<typename T>
206  Array(const blueprints::ArrayBluePrint< T > &blueprint) : buffer_size(0), buffer(NULL) {
207  // Note we're using a partially specialised parent interface here otherwise the
208  // constructor that reserves sizes as well as the comma initialiser won't
209  // conveniently convert types correctly
210  // (e.g. Array<double> array(4); array = 1,2,3,4; will fail)
211  ecl_compile_time_concept_check(BluePrintConcept<T>);
212  blueprint.implementApply(*this);
213  }
219  ~Array() {
220  if ( buffer != NULL ) {
221  delete[] buffer;
222  }
223  }
224  /*********************
225  ** Assignment
226  **********************/
240  containers::BoundedListInitialiser<value_type,iterator,DynamicStorage> operator<<(const Type &value) {
241  return containers::BoundedListInitialiser<value_type,iterator,DynamicStorage>(value,buffer,buffer_size);
242  }
243 
244  void operator=(const Array<Type,DynamicStorage>& array) {
245  if ( array.size() == 0 ) {
246  clear();
247  } else {
248  resize(array.size());
249  std::copy(array.begin(),array.end(),begin());
250  }
251  }
252 
253  /*********************
254  ** Iterators
255  **********************/
262  iterator begin() {
264  return buffer;
265  }
272  const_iterator begin() const {
273  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
274  return buffer;
275  }
282  iterator end() {
283  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
284  return buffer+buffer_size;
285  }
292  const_iterator end() const {
293  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
294  return buffer+buffer_size;
295  }
302  reverse_iterator rbegin() {
303  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
304  return reverse_iterator(end());
305  }
312  const_reverse_iterator rbegin() const {
313  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
314  return const_reverse_iterator(end());
315  }
322  reverse_iterator rend() {
323  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
324  return reverse_iterator(begin());
325  }
332  const_reverse_iterator rend() const {
333  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
334  return const_reverse_iterator(begin());
335  }
336 
337  /*********************
338  ** Front/Back
339  **********************/
346  reference front() {
347  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
348  return buffer[0];
349  }
356  const_reference front() const {
357  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
358  return buffer[0];
359  }
366  reference back() {
367  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
368  return buffer[buffer_size-1];
369  }
376  const_reference back() const {
377  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
378  return buffer[buffer_size-1];
379  }
380 
381  /*********************
382  ** Accessors
383  **********************/
395  Stencil< Array<Type,DynamicStorage> > stencil(const unsigned int& start_index, const unsigned int& n) {
396  ecl_assert_throw(start_index < size(), StandardException(LOC, OutOfRangeError, "Start index provided is larger than the underlying array size."));
397  ecl_assert_throw(start_index+n <= size(), StandardException(LOC, OutOfRangeError, "Finish index provided is larger than the underlying array size."));
398  return Stencil< Array<Type,DynamicStorage> >(*this, begin()+start_index, begin()+start_index+n);
399  }
409  reference operator[](size_type i) {
410  ecl_assert_throw( i<buffer_size, StandardException(LOC,OutOfRangeError));
411  return buffer[i];
412  }
424  const_reference operator[](size_type i) const {
425  ecl_assert_throw( i<buffer_size, StandardException(LOC,OutOfRangeError));
426  return buffer[i];
427  }
438  reference at(size_type i) {
439  if ( i>=buffer_size ) {
440  throw StandardException(LOC,OutOfRangeError);
441  }
442  return buffer[i];
443  }
456  const_reference at(size_type i) const {
457  if ( i>=buffer_size ) {
458  throw StandardException(LOC,OutOfRangeError);
459  }
460  return buffer[i];
461  }
462 
463  /*********************
464  ** Utilities
465  **********************/
473  size_type size() const { return buffer_size; }
474 
483  void resize( size_t n ) {
484  if ( buffer != NULL ) {
485  delete[] buffer;
486  }
487  buffer = new Type[n];
488  buffer_size = n;
489  }
495  void clear() {
496  if ( buffer != NULL ) {
497  delete[] buffer;
498  buffer = NULL;
499  }
500  buffer_size = 0;
501  }
502 
503  /*********************
504  ** Streaming
505  **********************/
513  template <typename OutputStream, typename ElementType>
514  friend OutputStream& operator<<(OutputStream &ostream , const Array<ElementType,DynamicStorage> &array);
515 
516  private:
517  unsigned int buffer_size;
518  Type *buffer;
519 
520 };
521 
522 /*****************************************************************************
523 ** Implementation [Array]
524 *****************************************************************************/
525 
526 template <typename OutputStream, typename ElementType>
527 OutputStream& operator<<(OutputStream &ostream , const Array<ElementType,DynamicStorage> &array) {
528 
529  ostream << "[ ";
530  for(size_t i = 0; i < array.buffer_size; ++i )
531  {
532  ostream << array[i] << " ";
533  }
534  ostream << "]";
535  ostream.flush();
536 
537  return ostream;
538 }
539 
540 /*****************************************************************************
541 ** BluePrints
542 *****************************************************************************/
543 
544 namespace blueprints {
545 
546 /*****************************************************************************
547 ** Interface [ConstantDynamicArray]
548 *****************************************************************************/
549 
557 template <typename Type>
558 class ConstantDynamicArray: public ArrayBluePrint< ConstantDynamicArray<Type> > {
559  public:
570 
580  ConstantDynamicArray(size_t size, const Type &value ) :
582  val(value)
583  {}
584 
585  virtual ~ConstantDynamicArray() {};
586 
599  std::fill_n(array.begin(),reserve_size,val);
600  return array;
601  }
602 
611  void apply(base_type& array) const {
612  array.resize(reserve_size);
613  std::fill_n(array.begin(),reserve_size,val);
614  }
615 
616  private:
617  size_t reserve_size;
618  Type val;
619 };
620 
621 
622 } // namespace blueprints
623 } // namespace ecl
624 
625 
626 #endif /* ECL_CONTAINERS_ARRAY_DYNAMIC_NO_MEM_CHECK_HPP_ */
ecl::blueprints::ConstantDynamicArray::~ConstantDynamicArray
virtual ~ConstantDynamicArray()
Definition: array_dynamic_mem_check.hpp:718
ecl::Array< Type, DynamicStorage >::size_type
std::size_t size_type
Definition: array_dynamic_mem_check.hpp:146
array_no_mem_check.hpp
Fixed size containers with a few bells and whistles.
ecl::blueprints::ConstantDynamicArray::base_type
ecl::Array< Type, ecl::DynamicStorage > base_type
Abstract representation of the class to be instantiated/configured.
Definition: array_dynamic_mem_check.hpp:696
ecl_compile_time_concept_check
#define ecl_compile_time_concept_check(Model)
ecl::operator<<
OutputStream & operator<<(OutputStream &ostream, Format< std::string > &formatter)
ecl::DynamicStorage
@ DynamicStorage
Definition: definitions.hpp:42
ecl::blueprints::ConstantDynamicArray::apply
void apply(base_type &array) const
Apply the blueprint to configure an existing object.
Definition: array_dynamic_mem_check.hpp:744
ecl::blueprints::ConstantDynamicArray::val
Type val
Definition: array_dynamic_mem_check.hpp:751
ecl::BluePrintFactory< Array< Type, DynamicStorage > >
Blueprint factory for dynamic arrays.
Definition: array_dynamic_mem_check.hpp:68
ecl::blueprints::ConstantDynamicArray::ConstantDynamicArray
ConstantDynamicArray()
Default constructor.
Definition: array_dynamic_mem_check.hpp:702
ecl::blueprints::ConstantDynamicArray::reserve_size
size_t reserve_size
Definition: array_dynamic_mem_check.hpp:750
LOC
#define LOC
ecl::Array< Type, DynamicStorage >::iterator
Type * iterator
Definition: array_dynamic_mem_check.hpp:142
blueprints.hpp
ecl::StandardException
ecl::blueprints::ConstantDynamicArray::instantiate
base_type instantiate()
Instantiate a copy of the object that is blueprinted.
Definition: array_dynamic_mem_check.hpp:730
ecl::Array< Type, DynamicStorage >::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: array_dynamic_mem_check.hpp:149
ecl::Array::begin
iterator begin()
Definition: array_no_mem_check.hpp:196
ecl_assert_throw
#define ecl_assert_throw(expression, exception)
ecl::formatters::ArrayFormatter
Pseudo formatter for integral type arrays.
Definition: array_dynamic_mem_check.hpp:55
ecl::BluePrintFactory
ecl::Array
Fixed size container with a few bells and whistles.
Definition: array_no_mem_check.hpp:112
macros.hpp
ecl::OutOfRangeError
OutOfRangeError
ecl
Embedded control libraries.
ECL_PUBLIC
#define ECL_PUBLIC


ecl_containers
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:34