CbmRoot
Loading...
Searching...
No Matches
CbmEnumMap.h
Go to the documentation of this file.
1/* Copyright (C) 2025 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
10#pragma once
11
12#include <boost/serialization/array.hpp>
13#include <boost/serialization/base_object.hpp>
14
15#include <algorithm>
16#include <array>
17#include <initializer_list>
18#include <utility>
19
20namespace cbm::util
21{
30 template<class T, class KeySet, class Base = std::array<T, KeySet::NofKeys()>>
31 class EnumMap : private Base {
32 public:
33 using Base_t = Base;
34 using KeySet_t = KeySet;
35 using Value_t = T;
36 using Key_t = typename KeySet::Key_t;
37
38
41 constexpr EnumMap() : Base(){};
42
45 constexpr EnumMap(std::initializer_list<std::pair<Key_t, Value_t>> init) : Base()
46 {
47 for (const auto& entry : init) {
48 if (KeySet::CheckKey(entry.first)) {
49 (*this)[entry.first] = entry.second;
50 }
51 else {
52 throw std::runtime_error("cbm::EnumMap: invalid key passed to the initializer list");
53 }
54 }
55 }
56
59 constexpr T& operator[](Key_t key) { return Base::operator[](KeySet::Index(key)); }
60
63 constexpr const T& operator[](Key_t key) const { return Base::operator[](KeySet::Index(key)); }
64
68 constexpr const T& operator[](size_t index) const { return Base::operator[](index); }
69
72 constexpr T& at(Key_t key)
73 {
74 if (!KeySet::CheckKey(key)) {
75 throw std::out_of_range("EnumMap: key out of range");
76 }
77 return operator[](key);
78 }
79
82 constexpr const T& at(Key_t key) const
83 {
84 if (!KeySet::CheckKey(key)) {
85 throw std::out_of_range("EnumMap: key out of range");
86 }
87 return operator[](key);
88 }
89
92 constexpr bool CheckKey(Key_t key) const { return KeySet::CheckKey(key); }
93
97 template<Key_t key>
98 constexpr bool CheckKey() const
99 {
100 return KeySet::CheckKey(key);
101 }
102
103 using Base::back;
104 using Base::begin;
105 using Base::cbegin;
106 using Base::cend;
107 using Base::crbegin;
108 using Base::crend;
109 using Base::empty; // true, if the size of the map is zero (to be consistent with STL containers)
110 using Base::end;
111 using Base::front;
112 using Base::rbegin;
113 using Base::rend;
114 using Base::size;
115 using Base::swap;
116
117 private:
118 friend class boost::serialization::access;
119 template<typename Archive>
120 void serialize(Archive& ar, const unsigned int)
121 {
122 ar& boost::serialization::base_object<Base>(*this);
123 }
124 };
125} // namespace cbm::util
constexpr T & at(Key_t key)
Mutable access with bounds checking.
Definition CbmEnumMap.h:72
typename KeySet::Key_t Key_t
Definition CbmEnumMap.h:36
void serialize(Archive &ar, const unsigned int)
Definition CbmEnumMap.h:120
constexpr const T & operator[](size_t index) const
Constant access operator.
Definition CbmEnumMap.h:68
constexpr bool CheckKey() const
Checks, if the key is provided in the key set.
Definition CbmEnumMap.h:98
constexpr const T & at(Key_t key) const
Constant access with bounds checking.
Definition CbmEnumMap.h:82
constexpr bool CheckKey(Key_t key) const
Checks, if the key is provided in the key set.
Definition CbmEnumMap.h:92
constexpr EnumMap()
Default constructor.
Definition CbmEnumMap.h:41
constexpr const T & operator[](Key_t key) const
Constant access operator.
Definition CbmEnumMap.h:63
constexpr EnumMap(std::initializer_list< std::pair< Key_t, Value_t > > init)
Constructor from initializer list.
Definition CbmEnumMap.h:45
constexpr T & operator[](Key_t key)
Mutable access operator.
Definition CbmEnumMap.h:59