SPH
Public Member Functions | Friends | List of all members
StaticArray< T, N, TCounter > Class Template Reference

Array with fixed number of allocated elements. More...

#include <StaticArray.h>

Public Member Functions

 StaticArray ()
 Default constructor, calls default constructor on all elements. More...
 
 StaticArray (const EmptyArray &)
 Initialize an empty array. More...
 
 StaticArray (std::initializer_list< StorageType > list)
 Initialize using initializer_list. More...
 
 StaticArray (const StaticArray &other)=delete
 
 StaticArray (StaticArray &&other)
 
 ~StaticArray ()
 Destructor, destroys all constructed elements in the array. More...
 
StaticArrayoperator= (const StaticArray &other)=delete
 
template<typename U , typename = std::enable_if_t<std::is_default_constructible<T>::value, U>>
StaticArrayoperator= (StaticArray< U, N > &&other)
 Move operator for arrays holding default-constructible types. More...
 
template<typename U , int M, typename = std::enable_if_t<std::is_lvalue_reference<T>::value, U>>
StaticArrayoperator= (StaticArray< U, M > &&other)
 Special assignment operator for array of references on left-hand side. More...
 
StaticArray clone () const
 Clones the array, calling copy constructor on all elements. More...
 
void fill (const T &value)
 Assigns a value to all constructed elements of the array. More...
 
INLINE T & operator[] (const TCounter idx) noexcept
 Returns the element with given index. More...
 
INLINE const T & operator[] (const TCounter idx) const noexcept
 Returns the element with given index. More...
 
constexpr INLINE TCounter maxSize () const noexcept
 Returns the maximum allowed size of the array. More...
 
INLINE TCounter size () const
 Returns the current size of the array (number of constructed elements). More...
 
INLINE bool empty () const
 Return true if the array is empty. More...
 
template<typename U , typename = std::enable_if_t<std::is_constructible<StorageType, U>::value>>
INLINE void push (U &&value)
 Inserts a value to the end of the array using copy/move constructor. More...
 
INLINEpop ()
 Removes and destroys element from the end of the array. More...
 
void resize (const TCounter newSize)
 Changes size of the array. More...
 
INLINE Iterator< StorageType > begin ()
 
INLINE Iterator< const StorageType > begin () const
 
INLINE Iterator< const StorageType > cbegin () const
 
INLINE Iterator< StorageType > end ()
 
INLINE Iterator< const StorageType > end () const
 
INLINE Iterator< const StorageType > cend () const
 
 operator ArrayView< T > ()
 
 operator ArrayView< const T > () const
 
bool operator== (const StaticArray &other) const
 
bool operator!= (const StaticArray &other) const
 

Friends

template<typename TStream >
TStream & operator<< (TStream &stream, const StaticArray &array)
 Prints content of array to stream. More...
 

Detailed Description

template<typename T, int N, typename TCounter = Size>
class StaticArray< T, N, TCounter >

Array with fixed number of allocated elements.

Number of actually created elements can be different and it can also be changed; elements can be created and destroyed, although it is not possible to construct more elements than the maximum allocated number, determined by template parameter N or function maxSize. The array is intentionally non-copyable to avoid accidentaly copying values, where move or pass-by-reference is possible.

Definition at line 19 of file StaticArray.h.

Constructor & Destructor Documentation

◆ StaticArray() [1/5]

template<typename T , int N, typename TCounter = Size>
StaticArray< T, N, TCounter >::StaticArray ( )
inline

Default constructor, calls default constructor on all elements.

Definition at line 28 of file StaticArray.h.

◆ StaticArray() [2/5]

template<typename T , int N, typename TCounter = Size>
StaticArray< T, N, TCounter >::StaticArray ( const EmptyArray )
inline

Initialize an empty array.

This effectively creates an empty stack; all N elements are allocated, but none is constructed and size of the array is zero.

Definition at line 41 of file StaticArray.h.

◆ StaticArray() [3/5]

template<typename T , int N, typename TCounter = Size>
StaticArray< T, N, TCounter >::StaticArray ( std::initializer_list< StorageType >  list)
inline

Initialize using initializer_list.

The size of the list must be smaller or equal to N. All N elements are all allocated, elements are constructed from initializer_list using copy constructor.

Definition at line 49 of file StaticArray.h.

◆ StaticArray() [4/5]

template<typename T , int N, typename TCounter = Size>
StaticArray< T, N, TCounter >::StaticArray ( const StaticArray< T, N, TCounter > &  other)
delete

◆ StaticArray() [5/5]

template<typename T , int N, typename TCounter = Size>
StaticArray< T, N, TCounter >::StaticArray ( StaticArray< T, N, TCounter > &&  other)
inline

Definition at line 59 of file StaticArray.h.

◆ ~StaticArray()

template<typename T , int N, typename TCounter = Size>
StaticArray< T, N, TCounter >::~StaticArray ( )
inline

Destructor, destroys all constructed elements in the array.

Definition at line 68 of file StaticArray.h.

Member Function Documentation

◆ begin() [1/2]

template<typename T , int N, typename TCounter = Size>
INLINE Iterator<StorageType> StaticArray< T, N, TCounter >::begin ( )
inline

Definition at line 198 of file StaticArray.h.

◆ begin() [2/2]

template<typename T , int N, typename TCounter = Size>
INLINE Iterator<const StorageType> StaticArray< T, N, TCounter >::begin ( ) const
inline

Definition at line 202 of file StaticArray.h.

◆ cbegin()

template<typename T , int N, typename TCounter = Size>
INLINE Iterator<const StorageType> StaticArray< T, N, TCounter >::cbegin ( ) const
inline

Definition at line 206 of file StaticArray.h.

◆ cend()

template<typename T , int N, typename TCounter = Size>
INLINE Iterator<const StorageType> StaticArray< T, N, TCounter >::cend ( ) const
inline

Definition at line 218 of file StaticArray.h.

◆ clone()

template<typename T , int N, typename TCounter = Size>
StaticArray StaticArray< T, N, TCounter >::clone ( ) const
inline

Clones the array, calling copy constructor on all elements.

The size of the cloned array corresponds to current size of this array.

Definition at line 110 of file StaticArray.h.

◆ empty()

template<typename T , int N, typename TCounter = Size>
INLINE bool StaticArray< T, N, TCounter >::empty ( ) const
inline

Return true if the array is empty.

Depends only on number of constructed elements, not allocated size.

Definition at line 154 of file StaticArray.h.

◆ end() [1/2]

template<typename T , int N, typename TCounter = Size>
INLINE Iterator<StorageType> StaticArray< T, N, TCounter >::end ( )
inline

Definition at line 210 of file StaticArray.h.

◆ end() [2/2]

template<typename T , int N, typename TCounter = Size>
INLINE Iterator<const StorageType> StaticArray< T, N, TCounter >::end ( ) const
inline

Definition at line 214 of file StaticArray.h.

◆ fill()

template<typename T , int N, typename TCounter = Size>
void StaticArray< T, N, TCounter >::fill ( const T &  value)
inline

Assigns a value to all constructed elements of the array.

Does not resize the array.

Definition at line 121 of file StaticArray.h.

◆ maxSize()

template<typename T , int N, typename TCounter = Size>
constexpr INLINE TCounter StaticArray< T, N, TCounter >::maxSize ( ) const
inlineconstexprnoexcept

Returns the maximum allowed size of the array.

Definition at line 140 of file StaticArray.h.

◆ operator ArrayView< const T >()

template<typename T , int N, typename TCounter = Size>
StaticArray< T, N, TCounter >::operator ArrayView< const T > ( ) const
inline

Definition at line 226 of file StaticArray.h.

◆ operator ArrayView< T >()

template<typename T , int N, typename TCounter = Size>
StaticArray< T, N, TCounter >::operator ArrayView< T > ( )
inline

Definition at line 222 of file StaticArray.h.

◆ operator!=()

template<typename T , int N, typename TCounter = Size>
bool StaticArray< T, N, TCounter >::operator!= ( const StaticArray< T, N, TCounter > &  other) const
inline

Definition at line 242 of file StaticArray.h.

◆ operator=() [1/3]

template<typename T , int N, typename TCounter = Size>
StaticArray& StaticArray< T, N, TCounter >::operator= ( const StaticArray< T, N, TCounter > &  other)
delete

◆ operator=() [2/3]

template<typename T , int N, typename TCounter = Size>
template<typename U , int M, typename = std::enable_if_t<std::is_lvalue_reference<T>::value, U>>
StaticArray& StaticArray< T, N, TCounter >::operator= ( StaticArray< U, M > &&  other)
inline

Special assignment operator for array of references on left-hand side.

Can be used similarly to std::tie; a function may return multiple values by wrapping them into StaticArray<T>, these values can be then saved individual variables by wrapping the variables into StaticArray<T&>. A big advantage over tuple is the option to return a variable number of elements (i.e. number of arguments is not known at compile time).

Definition at line 99 of file StaticArray.h.

◆ operator=() [3/3]

template<typename T , int N, typename TCounter = Size>
template<typename U , typename = std::enable_if_t<std::is_default_constructible<T>::value, U>>
StaticArray& StaticArray< T, N, TCounter >::operator= ( StaticArray< U, N > &&  other)
inline

Move operator for arrays holding default-constructible types.

Resizes array and moves all elements of the rhs array.

Definition at line 83 of file StaticArray.h.

◆ operator==()

template<typename T , int N, typename TCounter = Size>
bool StaticArray< T, N, TCounter >::operator== ( const StaticArray< T, N, TCounter > &  other) const
inline

Definition at line 230 of file StaticArray.h.

◆ operator[]() [1/2]

template<typename T , int N, typename TCounter = Size>
INLINE const T& StaticArray< T, N, TCounter >::operator[] ( const TCounter  idx) const
inlinenoexcept

Returns the element with given index.

Definition at line 134 of file StaticArray.h.

◆ operator[]() [2/2]

template<typename T , int N, typename TCounter = Size>
INLINE T& StaticArray< T, N, TCounter >::operator[] ( const TCounter  idx)
inlinenoexcept

Returns the element with given index.

Definition at line 128 of file StaticArray.h.

◆ pop()

template<typename T , int N, typename TCounter = Size>
INLINE T StaticArray< T, N, TCounter >::pop ( )
inline

Removes and destroys element from the end of the array.

The removed element is returned from the function. Array must not be empty, checked by assert.

Definition at line 170 of file StaticArray.h.

◆ push()

template<typename T , int N, typename TCounter = Size>
template<typename U , typename = std::enable_if_t<std::is_constructible<StorageType, U>::value>>
INLINE void StaticArray< T, N, TCounter >::push ( U &&  value)
inline

Inserts a value to the end of the array using copy/move constructor.

The current size of the array must be less than N, checked by assert.

Definition at line 162 of file StaticArray.h.

◆ resize()

template<typename T , int N, typename TCounter = Size>
void StaticArray< T, N, TCounter >::resize ( const TCounter  newSize)
inline

Changes size of the array.

New size must be between 0 and N. If the array is shrinked, elements from the end of the array are destroyed; if the array is enlarged, new elements are created using default constructor.

Definition at line 182 of file StaticArray.h.

◆ size()

template<typename T , int N, typename TCounter = Size>
INLINE TCounter StaticArray< T, N, TCounter >::size ( ) const
inline

Returns the current size of the array (number of constructed elements).

Can be between 0 and N.

Definition at line 147 of file StaticArray.h.

Friends And Related Function Documentation

◆ operator<<

template<typename T , int N, typename TCounter = Size>
template<typename TStream >
TStream& operator<< ( TStream &  stream,
const StaticArray< T, N, TCounter > &  array 
)
friend

Prints content of array to stream.

Stored values must have overloaded << operator.

Definition at line 250 of file StaticArray.h.


The documentation for this class was generated from the following file: