12#include <boost/serialization/array.hpp>
13#include <boost/serialization/base_object.hpp>
17#include <initializer_list>
30 template<
class T,
class KeySet,
class Base = std::array<T, KeySet::NofKeys()>>
36 using Key_t =
typename KeySet::Key_t;
45 constexpr EnumMap(std::initializer_list<std::pair<Key_t, Value_t>> init) : Base()
47 for (
const auto& entry : init) {
48 if (KeySet::CheckKey(entry.first)) {
49 (*this)[entry.first] = entry.second;
52 throw std::runtime_error(
"cbm::EnumMap: invalid key passed to the initializer list");
59 constexpr T&
operator[](
Key_t key) {
return Base::operator[](KeySet::Index(key)); }
63 constexpr const T&
operator[](
Key_t key)
const {
return Base::operator[](KeySet::Index(key)); }
68 constexpr const T&
operator[](
size_t index)
const {
return Base::operator[](index); }
74 if (!KeySet::CheckKey(key)) {
75 throw std::out_of_range(
"EnumMap: key out of range");
84 if (!KeySet::CheckKey(key)) {
85 throw std::out_of_range(
"EnumMap: key out of range");
92 constexpr bool CheckKey(
Key_t key)
const {
return KeySet::CheckKey(key); }
100 return KeySet::CheckKey(key);
118 friend class boost::serialization::access;
119 template<
typename Archive>
122 ar& boost::serialization::base_object<Base>(*
this);
constexpr T & at(Key_t key)
Mutable access with bounds checking.
typename KeySet::Key_t Key_t
void serialize(Archive &ar, const unsigned int)
constexpr const T & operator[](size_t index) const
Constant access operator.
constexpr bool CheckKey() const
Checks, if the key is provided in the key set.
constexpr const T & at(Key_t key) const
Constant access with bounds checking.
constexpr bool CheckKey(Key_t key) const
Checks, if the key is provided in the key set.
constexpr EnumMap()
Default constructor.
constexpr const T & operator[](Key_t key) const
Constant access operator.
constexpr EnumMap(std::initializer_list< std::pair< Key_t, Value_t > > init)
Constructor from initializer list.
constexpr T & operator[](Key_t key)
Mutable access operator.