Deluge Firmware
1.3.0
Build date: 2026.08.29
Toggle main menu visibility
Loading...
Searching...
No Matches
enum_to_string_map.hpp
1
2
#pragma once
3
4
#include "definitions.h"
5
#include "io/debug/log.h"
6
#include "mem_functions.h"
7
#include <array>
8
#include <cstddef>
9
#include <cstring>
10
#include <utility>
11
template
<
class
Enum, std::
size_t
N>
12
class
EnumStringMap {
13
14
public
:
15
constexpr
EnumStringMap(
const
std::array<std::pair<Enum, const char*>, N>& init) : stringList_() {
16
for
(std::size_t i = 0; i < N; ++i) {
17
const
auto
& p = init[i];
18
const
auto
index =
static_cast<
std::underlying_type_t<Enum>
>
(p.first);
19
stringList_[index] = p.second;
20
}
21
static_assert
(std::is_enum_v<Enum>,
"EnumStringMap requires an enum type"
);
22
static_assert
(N > 0,
"EnumStringMap requires at least one enum value"
);
23
static_assert
(N == std::size(init),
24
"EnumStringMap requires an enum type with the same number of values as the initializer list"
);
25
}
26
27
constexpr
const
char
* operator()(Enum a) {
return
stringList_[
static_cast<
std::underlying_type_t<Enum>
>
(a)]; }
28
30
constexpr
Enum
operator()
(
const
char
* str, Enum fallback) {
31
for
(std::size_t i = 0; i < N; i++) {
32
if
(stringList_[i] !=
nullptr
&& !strcmp(str, stringList_[i])) {
33
return
static_cast<
Enum
>
(i);
34
}
35
}
36
37
char
popup[25];
38
D_PRINTLN(popup,
"no match for:%s"
, str);
39
40
return
fallback;
41
}
42
44
constexpr
Enum
operator()
(
const
char
* str) {
return
(*
this
)(str,
static_cast<
Enum
>
(N - 1)); }
45
46
private
:
47
const
char
* stringList_[N];
48
};
EnumStringMap::operator()
constexpr Enum operator()(const char *str)
Convert string to enum, returning the last mapped enum variant on failure.
Definition
enum_to_string_map.hpp:44
EnumStringMap::operator()
constexpr Enum operator()(const char *str, Enum fallback)
Convert string to enum, returning fallback when the string is not mapped.
Definition
enum_to_string_map.hpp:30