forked from Harrm/scale-codec
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathenum.hpp
More file actions
284 lines (253 loc) · 10.2 KB
/
enum.hpp
File metadata and controls
284 lines (253 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @brief Implements encoding and validation of enumeration values using SCALE.
*
* This file provides utilities for handling enumerations in SCALE encoding.
* It allows defining constraints on enum values via `enum_traits`
* specializations, ensuring that only valid values are encoded or decoded.
*
* There are two ways to specialize an enumeration type:
* - Define a range using `min_value` and `max_value`.
* - Provide an explicit list using `valid_values`.
*
* Validation guarantees decoded values are within expected bounds,
* reducing risk when handling SCALE-encoded data.
*/
#pragma once
#include <algorithm>
#include <limits>
#include <string_view>
#include <type_traits>
#include <scale/detail/tagged.hpp>
#include <scale/outcome/outcome_throw.hpp>
#include <scale/scale_error.hpp>
namespace scale {
namespace detail::enumerations {
#define ENUM_NAME_PRETTY_FUNCTION __PRETTY_FUNCTION__
#if defined(__clang__)
// clang-format off
// Invalid:
// "std::string_view scale::detail::enumerations::enum_name_impl() [V = (Baz)-128]"
// Valid:
// "std::string_view scale::detail::enumerations::enum_name_impl() [V = Enum_ValidatingByReflection_I8_Test::TestBody()::Baz::A]"
// clang-format on
constexpr std::string_view enum_prefix = "EnumValue = ";
constexpr std::string_view enum_suffix = "]";
#elif defined(__GNUC__)
// clang-format off
// Invalid:
// "std::string_view scale::detail::enumerations::enum_name_impl() [with auto V = (Enum_ValidatingByReflection_I8_Test::TestBody::Baz)-128; std::string_view = std::basic_string_view<char>]"
// Valid:
// "std::string_view scale::detail::enumerations::enum_name_impl() [with auto V = Enum_ValidatingByReflection_I8_Test::TestBody::Baz::A; std::string_view = std::basic_string_view<char>]"
// clang-format on
constexpr std::string_view enum_prefix = "EnumValue = ";
constexpr std::string_view enum_suffix = "; ";
#else
#error Unsupported compiler
#endif
/**
* @brief Extracts enumeration name from the compiler-specific string.
* @tparam V The enum value.
*/
template <auto EnumValue>
consteval std::string_view enum_name_impl() {
constexpr std::string_view func = ENUM_NAME_PRETTY_FUNCTION;
constexpr std::size_t prefix_pos = func.find(enum_prefix);
static_assert(prefix_pos != std::string_view::npos);
constexpr std::size_t start = prefix_pos + enum_prefix.size();
if (func[start] == '(')
return {}; // Invalid: __PRETTY_FUNCTION__ prints invalid value of enum
// as number C-style-casted into enum type, i.e. `(Enum)-1`.
constexpr std::size_t end = func.find(enum_suffix, start);
constexpr std::string_view full = func.substr(start, end - start);
constexpr std::size_t colons = full.rfind("::");
if (colons == std::string_view::npos)
return {}; // Invalid: __PRETTY_FUNCTION__ always prints valid value
// with type, i.e. `Enum::value`
return full.substr(colons + 2);
}
/**
* @brief Concept that checks if a type is an enumeration.
*/
template <typename T>
concept Enumeration = std::is_enum_v<std::remove_cvref_t<T>>;
/**
* @brief Traits struct to be specialized for enumeration validation.
*/
template <typename E>
struct enum_traits; // default not specialized
namespace detail_impl {
template <typename E>
concept HasValidValues = requires {
{ enum_traits<E>::valid_values } -> std::ranges::range;
};
template <typename E>
concept HasMinMax = requires {
{
enum_traits<E>::min_value
} -> std::convertible_to<std::underlying_type_t<E>>;
{
enum_traits<E>::max_value
} -> std::convertible_to<std::underlying_type_t<E>>;
};
template <typename E, typename U = std::underlying_type_t<E>, U... Vs>
constexpr bool is_valid_enum_value_by_reflection_impl(
U value, std::integer_sequence<U, Vs...>) {
return ((enum_name_impl<static_cast<E>(Vs)>().size() > 0
&& static_cast<U>(static_cast<E>(Vs)) == value)
|| ...);
}
template <typename E, int... Is>
constexpr bool call_with_casted_signed_range(
std::underlying_type_t<E> value, std::integer_sequence<int, Is...>) {
using U = std::underlying_type_t<E>;
constexpr int min = -128;
return is_valid_enum_value_by_reflection_impl<E>(
value, std::integer_sequence<U, static_cast<U>(min + Is)...>{});
}
template <typename E, int... Is>
constexpr bool call_with_casted_unsigned_range(
std::underlying_type_t<E> value, std::integer_sequence<int, Is...>) {
using U = std::underlying_type_t<E>;
return is_valid_enum_value_by_reflection_impl<E>(
value, std::integer_sequence<U, static_cast<U>(Is)...>{});
}
/**
* @brief Fallback validation using reflection for enums of size 1 byte.
*/
template <typename E>
requires(sizeof(std::underlying_type_t<E>) == 1)
constexpr bool is_valid_enum_value_by_reflection(
std::underlying_type_t<E> value) {
using U = std::underlying_type_t<E>;
if constexpr (std::is_signed_v<U>) {
constexpr int min = -128;
constexpr int max = 127;
return call_with_casted_signed_range<E>(
value, std::make_integer_sequence<int, max - min + 1>{});
} else {
constexpr int max = 255;
return call_with_casted_unsigned_range<E>(
value, std::make_integer_sequence<int, max + 1>{});
}
}
/**
* @brief Validates enum value using min/max range.
*/
template <typename T>
requires HasMinMax<std::decay_t<T>>
constexpr bool is_valid_enum_value_range(
std::underlying_type_t<std::decay_t<T>> value) noexcept {
using E = std::decay_t<T>;
constexpr auto Min = enum_traits<E>::min_value;
constexpr auto Max = enum_traits<E>::max_value;
return value >= Min && value <= Max;
}
/**
* @brief Validates enum value against list of valid values.
*/
template <typename T>
requires HasValidValues<std::decay_t<T>>
constexpr bool is_valid_enum_value_list(
std::underlying_type_t<std::decay_t<T>> value) noexcept {
using E = std::decay_t<T>;
const auto &valid_values = enum_traits<E>::valid_values;
return std::find(valid_values.begin(),
valid_values.end(),
static_cast<E>(value))
!= valid_values.end();
}
} // namespace detail_impl
template <typename T>
constexpr bool CannotValidateEnum = false;
/**
* @brief Central enum validation entry point.
* @tparam T Enum type.
* @param value The underlying integer value.
* @return true if value is valid.
*/
template <Enumeration T>
constexpr bool is_valid_enum_value(
std::underlying_type_t<std::decay_t<T>> value) noexcept {
using E = std::decay_t<T>;
if constexpr (detail_impl::HasValidValues<E>) {
return detail_impl::is_valid_enum_value_list<T>(value);
} else if constexpr (detail_impl::HasMinMax<E>) {
return detail_impl::is_valid_enum_value_range<T>(value);
} else if constexpr (sizeof(std::underlying_type_t<E>) == 1) {
return detail_impl::is_valid_enum_value_by_reflection<E>(value);
} else {
static_assert(CannotValidateEnum<T>,
"Cannot validate enum: "
"define enum_traits<> with min/max or valid_values.");
return true;
}
}
} // namespace detail::enumerations
using detail::enumerations::enum_traits;
using detail::enumerations::Enumeration;
using detail::enumerations::is_valid_enum_value;
/**
* @brief Encodes an enum value using SCALE encoding.
* @tparam T Enum type.
* @param enumeration Enum value to encode.
* @param encoder Encoder instance.
*/
void encode(const Enumeration auto &enumeration, Encoder &encoder)
requires NoTagged<decltype(enumeration)>
{
using T =
std::underlying_type_t<std::remove_cvref_t<decltype(enumeration)>>;
encode(static_cast<T>(enumeration), encoder);
}
/**
* @brief Decodes an enum value using SCALE decoding.
* @tparam T Enum type.
* @param v Enum variable to store the decoded value.
* @param decoder Decoder instance.
*/
void decode(Enumeration auto &v, Decoder &decoder)
requires NoTagged<decltype(v)>
{
using E = std::decay_t<decltype(v)>;
std::underlying_type_t<E> value;
decoder >> value;
if (is_valid_enum_value<E>(value)) {
v = static_cast<E>(value);
return;
}
raise(DecodeError::INVALID_ENUM_VALUE);
}
} // namespace scale
/**
* @def SCALE_DEFINE_ENUM_VALUE_RANGE
* @brief Defines a valid value range for an enum.
* @param enum_namespace Namespace where enum is defined.
* @param enum_name Enum type name.
* @param min Minimum valid value.
* @param max Maximum valid value.
*/
#define SCALE_DEFINE_ENUM_VALUE_RANGE(enum_namespace, enum_name, min, max) \
template <> \
struct scale::enum_traits<enum_namespace::enum_name> final { \
using underlying = std::underlying_type_t<enum_namespace::enum_name>; \
static constexpr underlying min_value = static_cast<underlying>((min)); \
static constexpr underlying max_value = static_cast<underlying>((max)); \
};
/**
* @def SCALE_DEFINE_ENUM_VALUE_LIST
* @brief Defines an explicit list of valid enum values.
* @param enum_namespace Namespace where enum is defined.
* @param enum_name Enum type name.
* @param ... List of valid values.
*/
#define SCALE_DEFINE_ENUM_VALUE_LIST(enum_namespace, enum_name, ...) \
template <> \
struct scale::enum_traits<enum_namespace::enum_name> final { \
static constexpr bool is_default = false; \
static constexpr std::array valid_values = {__VA_ARGS__}; \
};