array_no_mem_check.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Ifdefs
10 *****************************************************************************/
11 
12 #ifndef ECL_CONTAINERS_ARRAY_NO_MEM_CHECK_HPP_
13 #define ECL_CONTAINERS_ARRAY_NO_MEM_CHECK_HPP_
14 #ifndef ECL_MEM_CHECK_ARRAYS
15 
16 /*****************************************************************************
17 ** Includes
18 *****************************************************************************/
19 
20 #include <algorithm> // std::copy
21 #include <cstddef> // size_t
22 #include <iterator>
23 #include "../definitions.hpp"
24 #include "../initialiser.hpp"
25 #include "../stencil.hpp"
26 #include <ecl/config/macros.hpp>
27 #include <ecl/concepts/macros.hpp>
29 #include <ecl/concepts/streams.hpp>
32 
33 /*****************************************************************************
34 ** Namespaces
35 *****************************************************************************/
36 
37 namespace ecl {
38 
39 /*****************************************************************************
40 ** Forward Declarations
41 *****************************************************************************/
42 
43 namespace blueprints {
44 
45 template <typename Type, size_t N> class ArrayFactory;
46 template <typename Derived> class ArrayBluePrint;
47 template <typename Type, size_t Size> class ConstantArray;
48 
49 } // namespace blueprints
50 
51 namespace formatters {
52 
53 template <typename Type, size_t N> class ArrayFormatter;
54 
55 } // namespace formatters
56 
57 /*****************************************************************************
58 ** Interface [Array][Fixed]
59 *****************************************************************************/
105 template<typename Type, std::size_t Size = DynamicStorage>
106 class ECL_PUBLIC Array : public blueprints::ArrayFactory<Type,Size> {
107  public:
108  /*********************
109  ** Typedefs
110  **********************/
111  typedef Type value_type;
112  typedef Type* iterator;
113  typedef const Type* const_iterator;
114  typedef Type& reference;
115  typedef const Type& const_reference;
116  typedef std::size_t size_type;
117  typedef std::ptrdiff_t difference_type;
118  typedef std::reverse_iterator<iterator> reverse_iterator;
119  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
123  /*********************
124  ** Constructors
125  **********************/
130  Array() {};
153  template<typename T>
154  Array(const blueprints::ArrayBluePrint< T > &blueprint) {
156  blueprint.implementApply(*this);
157  }
158 
159  virtual ~Array() {};
160 
161  /*********************
162  ** Assignment
163  **********************/
179  }
180 
181  /*********************
182  ** Iterators
183  **********************/
188  iterator begin() { return elements; }
193  const_iterator begin() const { return elements; }
198  iterator end() { return elements+Size; }
203  const_iterator end() const { return elements+Size; }
208  reverse_iterator rbegin() { return reverse_iterator(end()); }
213  const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
218  reverse_iterator rend() { return reverse_iterator(begin()); }
223  const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
224 
225  /*********************
226  ** Front/Back
227  **********************/
232  reference front() { return elements[0]; }
237  const_reference front() const { return elements[0]; }
242  reference back() { return elements[Size-1]; }
247  const_reference back() const { return elements[Size-1]; }
248 
249  /*********************
250  ** Accessors
251  **********************/
263  Stencil< Array<Type,Size> > stencil(const unsigned int& start_index, const unsigned int& n) {
264  ecl_assert_throw(start_index < Size, StandardException(LOC, OutOfRangeError, "Start index provided is larger than the underlying array size."));
265  ecl_assert_throw(start_index+n <= Size, StandardException(LOC, OutOfRangeError, "Finish index provided is larger than the underlying array size."));
266  return Stencil< Array<Type,Size> >(*this, begin()+start_index, begin()+start_index+n);
267  }
268 
278  reference operator[](size_type i) {
280  return elements[i];
281  }
293  const_reference operator[](size_type i) const {
294  ecl_assert_throw( i<Size, StandardException(LOC,OutOfRangeError));
295  return elements[i];
296  }
307  reference at(size_type i) {
308  if ( i>=Size ) {
309  throw StandardException(LOC,OutOfRangeError);
310  }
311  return elements[i];
312  }
325  const_reference at(size_type i) const {
326  if ( i>=Size ) {
327  throw StandardException(LOC,OutOfRangeError);
328  }
329  return elements[i];
330  }
331 
332  /*********************
333  ** Utilities
334  **********************/
340  static size_type size() { return Size; }
341 
342  /*********************
343  ** Streaming
344  **********************/
352  template <typename OutputStream, typename ElementType, size_t ArraySize>
353  friend OutputStream& operator<<(OutputStream &ostream , const Array<ElementType,ArraySize> &array);
354 
355  private:
356  value_type elements[Size];
357 };
358 
359 /*****************************************************************************
360 ** Implementation [Array][Insertion Operators]
361 *****************************************************************************/
362 
363 template <typename OutputStream, typename ElementType, size_t ArraySize>
364 OutputStream& operator<<(OutputStream &ostream , const Array<ElementType,ArraySize> &array) {
365 
367 
368  ostream << "[ ";
369  for(size_t i = 0; i < ArraySize; ++i )
370  {
371  ostream << array[i] << " ";
372  }
373  ostream << "]";
374  ostream.flush();
375 
376  return ostream;
377 }
378 
379 
380 /*****************************************************************************
381 ** BluePrints
382 *****************************************************************************/
383 
384 namespace blueprints {
385 
410 template <typename Derived>
411 class ArrayBluePrint {
412  public:
425  template <typename BaseType>
426  BaseType implementInstantiate() {
427  return static_cast<Derived*>(this)->instantiate();
428  }
442  template <typename BaseType>
443  void implementApply(BaseType& array) const {
444  static_cast<const Derived*>(this)->apply(array);
445  }
446 
447  virtual ~ArrayBluePrint() {}
448 };
457 template <typename Type, size_t Size>
458 class ConstantArray : public ArrayBluePrint< ConstantArray<Type, Size> > {
459  public:
472  ConstantArray(const Type& value = 0) : val(value) {}
473 
474  virtual ~ConstantArray() {};
475 
487  ecl::Array<Type,Size> array;
488  std::fill_n(array.begin(),Size,val);
489  return array;
490  }
491 
496  void apply(base_type& array) const {
497  std::fill_n(array.begin(),Size,val);
498  }
499 
500  private:
501  const Type& val;
502 };
503 
512 template<typename Type, size_t Size>
513 class ArrayFactory {
514  public:
521  static ConstantArray<Type,Size> Constant(const Type& value) {
522  return ConstantArray<Type,Size>(value);
523  }
524 
525  virtual ~ArrayFactory() {};
526 };
527 
528 } // namespace blueprints
529 } // namespace ecl
530 
531 #endif /* !ECL_MEM_CHECK_ARRAYS */
532 #endif /* ECL_CONTAINERS_ARRAY_NO_MEM_CHECK_HPP_ */
ecl::Array::reference
Type & reference
Definition: array_no_mem_check.hpp:122
ecl_compile_time_concept_check
#define ecl_compile_time_concept_check(Model)
ecl::Array::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: array_no_mem_check.hpp:126
ecl::blueprints::ArrayBluePrint::implementApply
void implementApply(BaseType &array) const
Definition: array_no_mem_check.hpp:453
ecl::blueprints::ConstantArray::ConstantArray
ConstantArray(const Type &value=0)
Constructor that properly configures/initialises the blueprint.
Definition: array_no_mem_check.hpp:480
ecl::Array::difference_type
std::ptrdiff_t difference_type
Definition: array_no_mem_check.hpp:125
ecl::operator<<
OutputStream & operator<<(OutputStream &ostream, Format< std::string > &formatter)
ecl::Array::iterator
Type * iterator
Definition: array_no_mem_check.hpp:120
ecl::blueprints::ConstantArray::val
const Type & val
Definition: array_no_mem_check.hpp:509
ecl::blueprints::ArrayBluePrint::implementInstantiate
BaseType implementInstantiate()
Definition: array_no_mem_check.hpp:436
ecl::Stencil
A safe windowing class that opens onto array-like containers.
Definition: stencil/formatters.hpp:46
ecl::blueprints::ConstantArray
Blueprint for instantiating/configuring an array filled with a constant.
Definition: array_no_mem_check.hpp:61
streams.hpp
ecl::Array< float, N >::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: array_no_mem_check.hpp:127
ecl::blueprints::ConstantArray::~ConstantArray
virtual ~ConstantArray()
Definition: array_no_mem_check.hpp:482
ecl::blueprints::ConstantArray::instantiate
base_type instantiate()
Instantiate a copy of the object that is blueprinted.
Definition: array_no_mem_check.hpp:494
LOC
#define LOC
blueprints.hpp
ecl::StandardException
ecl::blueprints::ArrayFactory::Constant
static ConstantArray< Type, Size > Constant(const Type &value)
Definition: array_no_mem_check.hpp:529
ecl::StreamConcept
ecl::Array::begin
iterator begin()
Definition: array_no_mem_check.hpp:196
ecl_assert_throw
#define ecl_assert_throw(expression, exception)
ecl::Array::size_type
std::size_t size_type
Definition: array_no_mem_check.hpp:124
ecl::formatters::ArrayFormatter
Pseudo formatter for integral type arrays.
Definition: array_dynamic_mem_check.hpp:55
ecl::blueprints::ArrayFactory::~ArrayFactory
virtual ~ArrayFactory()
Definition: array_no_mem_check.hpp:533
ecl::blueprints::ConstantArray::apply
void apply(base_type &array) const
Definition: array_no_mem_check.hpp:504
standard_exception.hpp
ecl::blueprints::ArrayBluePrint
Parent class for array blueprints.
Definition: array_no_mem_check.hpp:60
ecl::BluePrintConcept
ecl::containers::BoundedListInitialiser
Convenience initialiser with bounds checking for fixed size containers.
Definition: initialiser.hpp:61
ecl::blueprints::ConstantArray::base_type
ecl::Array< Type, Size > base_type
Abstract representation of the class to be instantiated/configured.
Definition: array_no_mem_check.hpp:471
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::blueprints::ArrayBluePrint::~ArrayBluePrint
virtual ~ArrayBluePrint()
Definition: array_no_mem_check.hpp:457
ECL_PUBLIC
#define ECL_PUBLIC
ecl::blueprints::ArrayFactory
BluePrint factory for the Array class.
Definition: array_no_mem_check.hpp:59
compile_time_assert.hpp


ecl_containers
Author(s): Daniel Stonier
autogenerated on Sun Aug 2 2020 03:12:14