A C++ template library for embedded applications
MIT licensed
Designed and
maintained by
John Wellbelove

array_wrapper

This class implements a zero cost C++ interface around a const or non-const C array. It presents the same API as
std::array.

It allows legacy C data to be more easily integrated with C++ interfaces and be accessed in a less error prone fashion.
If C++11 or above is supported then most functions are prefixed with constexpr.

STL equivalent: None.

etl::array_wrapper<typename T, size_t SIZE, T(&ARRAY)[SIZE]>

____________________________________________________________________________________________________

Member types

value_type              T
size_type               size_t
difference_type         ptrdiff_t
reference               value_type&
const_reference         const value_type&
pointer                 value_type*
const_pointer           const value_type*
iterator                Random access iterator
const_iterator          Constant random access iterator
reverse_iterator        std::reverse_iterator<iterator>
const_reverse_iterator  std::reverse_iterator<const_iterator>

____________________________________________________________________________________________________

Member constants

May be used as indexes in to the array.

SIZE     = Size of the array
MAX_SIZE = SIZE,
FRONT    = 0,
BACK     = SIZE - 1,
BEGIN    = 0,
END      = SIZE,
RBEGIN   = SIZE - 1,
REND     = -1

____________________________________________________________________________________________________

Element access


T& at(size_t i)
ETL_CONSTEXPR const T& at(size_t i) const
Returns a reference or const reference to the indexed element. Emits an etl::array_wrapper_bounds if the index is
out of range of the array. If asserts or exceptions are not enabled then undefined behaviour occurs.
____________________________________________________________________________________________________

T& operator[](size_t i)
ETL_CONSTEXPR const T& operator[](size_t i) const
Returns a reference or const reference to the indexed element.
if the index is out of range of the array then undefined behaviour occurs.
____________________________________________________________________________________________________

T& front()
ETL_CONSTEXPR const T& front() const
Returns a reference or const reference to the first element.
____________________________________________________________________________________________________

T& back()
ETL_CONSTEXPR const T& back() const
Returns a reference or const reference to the last element.
____________________________________________________________________________________________________

T* data()
ETL_CONSTEXPR const T* data() const
Returns a pointer or const pointer to the array.
____________________________________________________________________________________________________

Modifiers


void fill(parameter_t value)
Fill a non-cost array with the value.
____________________________________________________________________________________________________

template <typename T, T(&ARRAYOTHER)[SIZE_]>
void swap(etl::array_wrapper<T, SIZE_, ARRAYOTHER>& other)
Swaps the array contents with another.
____________________________________________________________________________________________________

Iterators


ETL_CONSTEXPR iterator begin()
ETL_CONSTEXPR const_iterator begin() const
ETL_CONSTEXPR const_iterator cbegin() const
Returns an iterator to the beginning of the array.
____________________________________________________________________________________________________

ETL_CONSTEXPR iterator end()
ETL_CONSTEXPR const_iterator end() const
ETL_CONSTEXPR const_iterator cend() const
Returns an iterator to the end of the array.
____________________________________________________________________________________________________

ETL_CONSTEXPR iterator rbegin()
ETL_CONSTEXPR const_reverse_iterator rbegin() const
ETL_CONSTEXPR const_reverse_iterator crbegin() const
Returns a reverse iterator to the beginning of the array.
____________________________________________________________________________________________________

ETL_CONSTEXPR iterator rend()
ETL_CONSTEXPR const_reverse_iterator rend() const
ETL_CONSTEXPR const_reverse_iterator crend() const
Returns a reverse iterator to the end of the array.
____________________________________________________________________________________________________

Capacity


ETL_CONSTEXPR size_t size() const
Returns the size of the view.
____________________________________________________________________________________________________

ETL_CONSTEXPR size_t max_size() const
Returns the maximum possible size of the view.
____________________________________________________________________________________________________

Non-member functions


Lexicographically comparisons


== true if the contents of the array views are equal, otherwise false.
!= true if the contents of the array views are not equal, otherwise false.
<  true if the contents of the lhs are lexicographically less than the contents of the rhs,  otherwise false.
<= true if the contents of the lhs are lexicographically less than or equal to the contents of the rhs, otherwise false.
>  true if the contents of the lhs are lexicographically greater than the contents of the rhs,  otherwise false.
>= true if the contents of the lhs are lexicographically greater than or equal to the contents of the rhs, otherwise false

____________________________________________________________________________________________________

Swap


template <typename T, std::size_t SIZE, T(&ARRAYL)[SIZE], T(&ARRAYR)[SIZE]>
void swap(etl::array_wrapper<T, SIZE, ARRAYL>& lhs,
          etl::array_wrapper<T, SIZE, ARRAYR>& rhs)
Swaps the array contents of two arrays.

____________________________________________________________________________________________________

Hash


There is a specialisation for etl::hash for array_wrapper.

____________________________________________________________________________________________________

Macros


A macro is defined to ease the use of the class.
ETL_ARRAY_WRAPPER(type, array);
____________________________________________________________________________________________________

Example


// The address of the array MUST be deducible at compile time..
int cdata = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

typedef ETL_ARRAY_WRAPPER(int, cdata) Data;

// All instances of 'Data' access the SAME array.
Data data;

void Print(Data data)
{
  Data::const_iterator itr = data.begin();

  while (itr != data.end())
  {
    std::cout << *itr++ << " ";
  }
}

Print(data); // Prints "1 2 3 4 5 6 7 8 9 10"

size_t hashvalue = etl::hash<Data>()(data);

// Using indexes
int firstElement = data[Data::FRONT];

int lastElement = data[Data::BACK];

// Using forward indexes
for (int i = Data::BEGIN; i < Data::END; ++i)
{
  std::cout << data[i] << " ";
}

// Using reverse indexes
for (int i = Data::RBEGIN; i < Data::REND; --i)
{
  std::cout << data[i] << " ";
}


array_wrapper.h