OpenEnroth 73e68f7
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Attributes | Private Types | Static Private Member Functions | Friends | List of all members
IndexedArray< T, FirstIndex, LastIndex, Size > Class Template Reference

#include <IndexedArray.h>

Inheritance diagram for IndexedArray< T, FirstIndex, LastIndex, Size >:

Public Types

using key_type = decltype(FirstIndex)
 

Public Member Functions

constexpr IndexedArray ()
 
constexpr IndexedArray (std::initializer_list< std::pair< key_type, value_type > > init)
 
constexpr Segment< key_typeindices () const
 
constexpr reference at (key_type n)
 
constexpr const_reference at (key_type n) const
 
constexpr reference operator[] (key_type n) noexcept
 
constexpr const_reference operator[] (key_type n) const noexcept
 

Static Public Attributes

static constexpr size_t SIZE = Size
 

Private Types

using base_type = std::array< T, Size >
 

Static Private Member Functions

static constexpr bool is_unique (std::initializer_list< std::pair< key_type, value_type > > init)
 

Friends

bool operator== (const IndexedArray &l, const IndexedArray &r)
 

Detailed Description

template<class T, auto FirstIndex, auto LastIndex, ptrdiff_t Size = static_cast<ptrdiff_t>(LastIndex) - static_cast<ptrdiff_t>(FirstIndex) + 1>
class IndexedArray< T, FirstIndex, LastIndex, Size >

An std::array-like class that supports some additional features:

The template itself can be used in several different ways:

Some code examples:

enum class TriBool {
True,
False,
DontKnow
};
using enum TriBool;
// IndexedArray supports the same initialization syntax as `std::array`.
// And it can also be constructed like an `std::map`.
{True, "true"},
{False, "false"},
{DontKnow, "unknown"}
};
extern TriBool f(int);
// Access IndexedArray elements using enum values as indices.
std::cout << "f(10)=" << defaultMessageMap[f(10)] << std::endl;
// Iterate through the IndexedArray like it's an array...
for (auto &value : userMessageMap)
value.clear();
// ...or get a view of its indices and use it as you would use a traditional array.
for (TriBool i : defaultMessageMap.indices())
userMessageMap[i] = defaultMessageMap[i];
i
Definition: Json_ut.cpp:82
Definition: IndexedArray.h:66
constexpr Segment< key_type > indices() const
Definition: IndexedArray.h:143
Template Parameters
TArray element type.
FirstIndexIndex of the first element. Value must be of enum or integral type.
LastIndexIndex of the last element. The size of the indexed array is LastIndex - FirstIndex + 1.

Member Typedef Documentation

◆ base_type

template<class T , auto FirstIndex, auto LastIndex, ptrdiff_t Size = static_cast<ptrdiff_t>(LastIndex) - static_cast<ptrdiff_t>(FirstIndex) + 1>
using IndexedArray< T, FirstIndex, LastIndex, Size >::base_type = std::array<T, Size>
private

◆ key_type

template<class T , auto FirstIndex, auto LastIndex, ptrdiff_t Size = static_cast<ptrdiff_t>(LastIndex) - static_cast<ptrdiff_t>(FirstIndex) + 1>
using IndexedArray< T, FirstIndex, LastIndex, Size >::key_type = decltype(FirstIndex)

Constructor & Destructor Documentation

◆ IndexedArray() [1/2]

template<class T , auto FirstIndex, auto LastIndex, ptrdiff_t Size = static_cast<ptrdiff_t>(LastIndex) - static_cast<ptrdiff_t>(FirstIndex) + 1>
constexpr IndexedArray< T, FirstIndex, LastIndex, Size >::IndexedArray ( )
inlineconstexpr

Creates an uninitialized indexed array.

This is the constructor that gets called when you use aggregate initialization, so the behavior is the same as with std::array:

IndexedArray<int, TriBool_Size> uninitializedIntegers = {};

If you want to default-initialize array elements, see the other constructor.

◆ IndexedArray() [2/2]

template<class T , auto FirstIndex, auto LastIndex, ptrdiff_t Size = static_cast<ptrdiff_t>(LastIndex) - static_cast<ptrdiff_t>(FirstIndex) + 1>
constexpr IndexedArray< T, FirstIndex, LastIndex, Size >::IndexedArray ( std::initializer_list< std::pair< key_type, value_type > >  init)
inlineconstexpr

An std::map-like constructor for indexed array. The size of the provided initializer list must match array size. Alternatively, this constructor can be used to default-initialize the indexed array using the same syntax as is used for std::array.

Example usage:

enum class Monster {
Peasant,
AzureDragon,
MonsterCount
};
using enum Monster;
{Peasant, 1}
{AzureDragon, 1000}
};
IndexedArray<int, MonsterCount> killCount = {{}}; // ints inside the array are default-initialized to zero.
Parameters
initInitializer list of key-value pairs.

Member Function Documentation

◆ at() [1/2]

template<class T , auto FirstIndex, auto LastIndex, ptrdiff_t Size = static_cast<ptrdiff_t>(LastIndex) - static_cast<ptrdiff_t>(FirstIndex) + 1>
constexpr reference IndexedArray< T, FirstIndex, LastIndex, Size >::at ( key_type  n)
inlineconstexpr

◆ at() [2/2]

template<class T , auto FirstIndex, auto LastIndex, ptrdiff_t Size = static_cast<ptrdiff_t>(LastIndex) - static_cast<ptrdiff_t>(FirstIndex) + 1>
constexpr const_reference IndexedArray< T, FirstIndex, LastIndex, Size >::at ( key_type  n) const
inlineconstexpr

◆ indices()

template<class T , auto FirstIndex, auto LastIndex, ptrdiff_t Size = static_cast<ptrdiff_t>(LastIndex) - static_cast<ptrdiff_t>(FirstIndex) + 1>
constexpr Segment< key_type > IndexedArray< T, FirstIndex, LastIndex, Size >::indices ( ) const
inlineconstexpr

Use this function is you want to iterate over this indexed array like it's a normal array, e.g.:

for (SomeEnum i : array.keys()) {
// use i and array[i]
}
Returns
View over the valid indices for the elements of this indexed array.

◆ is_unique()

template<class T , auto FirstIndex, auto LastIndex, ptrdiff_t Size = static_cast<ptrdiff_t>(LastIndex) - static_cast<ptrdiff_t>(FirstIndex) + 1>
static constexpr bool IndexedArray< T, FirstIndex, LastIndex, Size >::is_unique ( std::initializer_list< std::pair< key_type, value_type > >  init)
inlinestaticconstexprprivate

◆ operator[]() [1/2]

template<class T , auto FirstIndex, auto LastIndex, ptrdiff_t Size = static_cast<ptrdiff_t>(LastIndex) - static_cast<ptrdiff_t>(FirstIndex) + 1>
constexpr const_reference IndexedArray< T, FirstIndex, LastIndex, Size >::operator[] ( key_type  n) const
inlineconstexprnoexcept

◆ operator[]() [2/2]

template<class T , auto FirstIndex, auto LastIndex, ptrdiff_t Size = static_cast<ptrdiff_t>(LastIndex) - static_cast<ptrdiff_t>(FirstIndex) + 1>
constexpr reference IndexedArray< T, FirstIndex, LastIndex, Size >::operator[] ( key_type  n)
inlineconstexprnoexcept

Friends And Related Function Documentation

◆ operator==

template<class T , auto FirstIndex, auto LastIndex, ptrdiff_t Size = static_cast<ptrdiff_t>(LastIndex) - static_cast<ptrdiff_t>(FirstIndex) + 1>
bool operator== ( const IndexedArray< T, FirstIndex, LastIndex, Size > &  l,
const IndexedArray< T, FirstIndex, LastIndex, Size > &  r 
)
friend

Member Data Documentation

◆ SIZE

template<class T , auto FirstIndex, auto LastIndex, ptrdiff_t Size = static_cast<ptrdiff_t>(LastIndex) - static_cast<ptrdiff_t>(FirstIndex) + 1>
constexpr size_t IndexedArray< T, FirstIndex, LastIndex, Size >::SIZE = Size
staticconstexpr

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