Coverage Report

Created: 2026-10-19 14:15

src/zserio/Enums.h
Line
Count
Source
1
#ifndef ZSERIO_ENUMS_H_INC
2
#define ZSERIO_ENUMS_H_INC
3
4
#include <algorithm>
5
#include <type_traits>
6
7
#include "zserio/CppRuntimeException.h"
8
#include "zserio/StringView.h"
9
#include "zserio/Types.h"
10
11
// This should be implemented in runtime library header.
12
namespace zserio
13
{
14
15
// forward declarations
16
class BitStreamReader;
17
class BitStreamWriter;
18
19
/**
20
 * Enum traits for Zserio enums.
21
 */
22
template <typename T>
23
struct EnumTraits
24
{};
25
26
/**
27
 * Gets ordinal number of the given enum item.
28
 *
29
 * \param value Enum item.
30
 *
31
 * \return Ordinal number of the enum item.
32
 */
33
template <typename T>
34
size_t enumToOrdinal(T value);
35
36
/**
37
 * Converts the given raw value to an appropriate enum item.
38
 *
39
 * \param rawValue Raw value of the proper underlying type.
40
 *
41
 * \return Enum item corresponding to the rawValue.
42
 *
43
 * \throw CppRuntimeException when the rawValue doesn't match to any enum item.
44
 */
45
template <typename T>
46
T valueToEnum(typename std::underlying_type<T>::type rawValue);
47
48
/**
49
 * Gets the underlying raw value of the given enum item.
50
 *
51
 * \param value Enum item.
52
 *
53
 * \return Raw value.
54
 */
55
template <typename T>
56
constexpr typename std::underlying_type<T>::type enumToValue(T value)
57
766
{
58
766
    return static_cast<typename std::underlying_type<T>::type>(value);
59
766
}
60
61
/**
62
 * Gets the hash code of the given enum item.
63
 *
64
 * \param value Enum item.
65
 *
66
 * \return Hash code of the enum item.
67
 */
68
template <typename T>
69
uint32_t enumHashCode(T value);
70
71
/**
72
 * Converts the given enum item name to an appropriate enum item.
73
 *
74
 * \param itemName Name of the enum item.
75
 *
76
 * \return Enum item corresponding to the itemName.
77
 *
78
 * \throw CppRuntimeException when the itemName doesn't match to any enum item.
79
 */
80
template <typename T>
81
T stringToEnum(StringView itemName)
82
5
{
83
5
    const auto foundIt = std::find_if(EnumTraits<T>::names.begin(), EnumTraits<T>::names.end(),
84
14
            [itemName](const char* enumItemName) { return itemName.compare(enumItemName) == 0; });
85
5
    if (foundIt == EnumTraits<T>::names.end())
86
1
    {
87
1
        throw CppRuntimeException("Enum item '")
88
1
                << itemName << "' doesn't exist in enum '" << EnumTraits<T>::enumName << "'!";
89
1
    }
90
91
4
    const size_t ordinal = static_cast<size_t>(std::distance(EnumTraits<T>::names.begin(), foundIt));
92
4
    return EnumTraits<T>::values[ordinal];
93
5
}
94
95
/**
96
 * Gets the name of the given enum item.
97
 *
98
 * \param value Enum item.
99
 *
100
 * \return Name of the enum item.
101
 */
102
template <typename T>
103
const char* enumToString(T value)
104
26
{
105
26
    return EnumTraits<T>::names[enumToOrdinal(value)];
106
26
}
107
108
/**
109
 * Initializes packing context for the given enum item.
110
 *
111
 * \param context Packing context.
112
 * \param value Enum item.
113
 */
114
template <typename PACKING_CONTEXT, typename T>
115
void initPackingContext(PACKING_CONTEXT& context, T value);
116
117
/**
118
 * Gets bit size of the given enum item.
119
 *
120
 * Note that T can be varuint, so bitSizeOf cannot return constant value and depends on the concrete item.
121
 *
122
 * \param value Enum item.
123
 *
124
 * \return Bit size of the enum item.
125
 */
126
template <typename T>
127
size_t bitSizeOf(T value);
128
129
/**
130
 * Gets bit size of the given enum item when it's inside a packed array.
131
 *
132
 * Note that T can be varuint, so bitSizeOf cannot return constant value and depends on the concrete item.
133
 *
134
 * \param context Packing context.
135
 * \param value Enum item.
136
 *
137
 * \return Bit size of the enum item.
138
 */
139
template <typename PACKING_CONTEXT, typename T>
140
size_t bitSizeOf(PACKING_CONTEXT& context, T value);
141
142
/**
143
 * Initializes offsets for the enum item.
144
 *
145
 * Note that T can be varuint, so initializeOffsets cannot return constant value and
146
 * depends on the concrete item.
147
 *
148
 * \param bitPosition Current bit position.
149
 * \param value Enum item.
150
 *
151
 * \return Updated bit position which points to the first bit after the enum item.
152
 */
153
template <typename T>
154
size_t initializeOffsets(size_t bitPosition, T value);
155
156
/**
157
 * Initializes offsets for the enum item when it's inside a packed array.
158
 *
159
 * Note that T can be varuint, so initializeOffsets cannot return constant value and
160
 * depends on the concrete item.
161
 *
162
 * \param context Packing context.
163
 * \param bitPosition Current bit position.
164
 * \param value Enum item.
165
 *
166
 * \return Updated bit position which points to the first bit after the enum item.
167
 */
168
template <typename PACKING_CONTEXT, typename T>
169
size_t initializeOffsets(PACKING_CONTEXT& context, size_t bitPosition, T value);
170
171
/**
172
 * Reads an enum item.
173
 *
174
 * \param in Bit stream reader.
175
 *
176
 * \return Enum item read from the bit stream.
177
 */
178
template <typename T>
179
T read(BitStreamReader& in);
180
181
/**
182
 * Reads an enum item which is inside a packed array.
183
 *
184
 * \param context Packing context.
185
 * \param in Bit stream reader.
186
 *
187
 * \return Enum item read from the bit stream.
188
 */
189
template <typename T, typename PACKING_CONTEXT>
190
T read(PACKING_CONTEXT& context, BitStreamReader& in);
191
192
/**
193
 * Writes the enum item to the given bit stream.
194
 *
195
 * \param out Bit stream writer.
196
 * \param value Enum item to write.
197
 */
198
template <typename T>
199
void write(BitStreamWriter& out, T value);
200
201
/**
202
 * Writes the enum item which is inside a packed array to the given bit stream.
203
 *
204
 * \param context Packing context.
205
 * \param out Bit stream writer.
206
 * \param value Enum item to write.
207
 */
208
template <typename PACKING_CONTEXT, typename T>
209
void write(PACKING_CONTEXT& context, BitStreamWriter& out, T value);
210
211
/**
212
 * Appends any enumeration value to the exception's description.
213
 *
214
 * \param exception Exception to modify.
215
 * \param value Enumeration value to append.
216
 *
217
 * \return Reference to the exception to allow operator chaining.
218
 */
219
template <typename T, typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
220
CppRuntimeException& operator<<(CppRuntimeException& exception, T value)
221
1
{
222
1
    exception << enumToString(value);
223
1
    return exception;
224
1
}
225
226
} // namespace zserio
227
228
#endif // ZSERIO_ENUMS_H_INC