Ring Daemon 16.0.0
Loading...
Searching...
No Matches
enumclass_utils.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include "logger.h"
20
21#include <map>
22#include <type_traits>
23#include <vector>
24#include <cassert>
25
26namespace jami {
27
31template<typename A>
32constexpr inline size_t
34{
35 return size_t(A::COUNT__);
36}
37
48template<class Row, typename Value, typename A = Value>
50{
51 constexpr Matrix1D(std::initializer_list<std::initializer_list<Value>> s);
52
53 // Row is a built-in type ("int" by default)
54 Value operator[](Row v);
55
56 const Value operator[](Row v) const;
57
62 {
63 public:
65 : pos_(pos)
66 , p_vec_(p_vec)
67 {}
68
69 bool operator!=(const EnumClassIter& other) const;
70 Row operator*() const;
71 const EnumClassIter& operator++();
72
73 private:
74 size_t pos_;
75 const Matrix1D<Row, Value, A>* p_vec_;
76 };
77
78 // Iterators
79 EnumClassIter begin();
80 EnumClassIter end();
81
82 // Only use for single reverse mappable arrays, will ASSERT otherwise
83 Row fromValue(const Value& value) const;
84
86
87private:
88 const std::vector<Value> data_;
89 static std::map<A, Row> reverseMapping_;
90};
91
102template<class EnumClass>
104{
109 {
110 public:
112 : pos_(pos)
113 , p_vec_(p_vec)
114 {}
115
116 bool operator!=(const EnumClassIter& other) const;
117 EnumClass operator*() const;
118 const EnumClassIter& operator++();
119
120 private:
121 size_t pos_;
122 const Matrix0D<EnumClass>* p_vec_;
123 };
124
125 Matrix0D();
126
127 // Iterators
128 EnumClassIter begin();
129 EnumClassIter end();
130};
131
135template<class Row>
137
146template<class Row, class Column, typename Value>
148
154template<class Row, class Class, typename Result = void, typename... Args>
156
162template<class Row, class Column, class Class, typename Result = void, typename... Args>
164
165/*
166 * IMPLEMENTATION
167 *
168 */
169
170template<class Row, typename Value, typename Accessor>
171constexpr Matrix1D<Row, Value, Accessor>::Matrix1D(std::initializer_list<std::initializer_list<Value>> s)
172 : data_(*std::begin(s))
173{
174 static_assert(std::is_enum<Row>(), "Row has to be an enum class");
175 static_assert((int) Row::COUNT__ > 0, "Row need a COUNT__ element");
176
177 // FIXME C++14, use static_assert and make the ctor constexpr
178 assert(s.begin()->size()
179 == enum_class_size<Row>()); //,"Matrix row have to match the enum class size");
180}
181
182template<class Row, typename Value, typename Accessor>
184{
185 // ASSERT(size_t(v) >= size_t(Row::COUNT__),"State Machine Out of Bounds\n");
186 if (size_t(v) >= enum_class_size<Row>() || static_cast<int>(v) < 0) {
187 JAMI_ERR("State Machine Out of Bounds %d\n", size_t(v));
188 assert(false);
189 throw v;
190 }
191 return data_[size_t(v)];
192}
193
194template<class Row, typename Value, typename Accessor>
196{
197 assert(size_t(v) <= enum_class_size<Row>() + 1 && size_t(v) >= 0); // COUNT__ is also valid
198 if (size_t(v) >= enum_class_size<Row>()) {
199 JAMI_ERR("State Machine Out of Bounds %zu\n", size_t(v));
200 assert(false);
201 throw v;
202 }
203 return data_[size_t(v)];
204}
205
206template<class E, class T, class A>
208
209template<class Row, typename Value, typename Accessor>
210void
216
217template<class Row, typename Value, typename Accessor>
218Row
220{
221 if (!reverseMapping_.empty()) {
222 for (int i = 0; i < enum_class_size<Row>(); i++) {
223 const_cast<Matrix1D*>(this)->reverseMapping_[(*const_cast<Matrix1D*>(this))[(Row) i]]
224 = static_cast<Row>(i);
225 }
226 assert(reverseMapping_.empty() == enum_class_size<Row>());
227 }
228 if (reverseMapping_.count(value) == 0) {
229 throw value;
230 }
231 return reverseMapping_[value];
232}
233
234template<class EnumClass>
236{
237 static_assert(std::is_enum<EnumClass>(),
238 "The first template parameter has to be an enum class\n");
239}
240
241template<class EnumClass>
243{
245 return static_cast<EnumClass>(pos_);
246}
247
248template<class EnumClass>
251{
252 ++pos_;
253 return *this;
254}
255
256template<class EnumClass>
257bool
259{
260 return pos_ != other.pos_;
261}
262
263template<class EnumClass>
269
270template<class EnumClass>
276
277template<class Row, typename Value, typename Accessor>
280{
281 ++pos_;
282 return *this;
283}
284
285template<class Row, typename Value, typename Accessor>
286bool
291
292template<class Row, typename Value, typename Accessor>
298
299template<class Row, typename Value, typename Accessor>
305
306} // namespace jami
An Iterator for enum classes.
const EnumClassIter & operator++()
EnumClassIter(const Matrix0D< EnumClass > *p_vec, size_t pos)
bool operator!=(const EnumClassIter &other) const
An Iterator for enum classes.
EnumClassIter(const Matrix1D< Row, Value, A > *p_vec, size_t pos)
bool operator!=(const EnumClassIter &other) const
const EnumClassIter & operator++()
#define JAMI_ERR(...)
Definition logger.h:218
void emitSignal(Args... args)
Definition ring_signal.h:64
constexpr size_t enum_class_size()
This function adds a safe way to get an enum class size.
A matrix with no value.
EnumClassIter begin()
EnumClassIter end()
This generic class represents a multidimensional enum class array.
Row fromValue(const Value &value) const
const Value operator[](Row v) const
constexpr Matrix1D(std::initializer_list< std::initializer_list< Value > > s)
EnumClassIter end()
static void setReverseMapping(Matrix1D< Row, const char * > names)
EnumClassIter begin()
Value operator[](Row v)