From 74738ed1316216e5737717ecf2995b7eb34cf9f9 Mon Sep 17 00:00:00 2001 From: Giovanni Claudio <6526050+jokla@users.noreply.github.com> Date: Fri, 28 Mar 2025 07:58:36 +0000 Subject: [PATCH] Add enum range support with customizable min and max values (#392) --- docs/enums.md | 56 +++++++++------- include/rfl/enums.hpp | 10 +++ include/rfl/internal/enums/Names.hpp | 61 +++++++++++++++++- include/rfl/internal/enums/get_enum_names.hpp | 64 +++++++++++++++---- tests/generic/test_enum.cpp | 1 - tests/generic/test_enum_range.cpp | 40 ++++++++++++ tests/generic/test_enum_range_min_max.cpp | 33 ++++++++++ tests/generic/test_enum_range_size.cpp | 37 +++++++++++ 8 files changed, 261 insertions(+), 41 deletions(-) create mode 100644 tests/generic/test_enum_range.cpp create mode 100644 tests/generic/test_enum_range_min_max.cpp create mode 100644 tests/generic/test_enum_range_size.cpp diff --git a/docs/enums.md b/docs/enums.md index 8de33da0..9cdf8734 100644 --- a/docs/enums.md +++ b/docs/enums.md @@ -27,34 +27,42 @@ This results in the following JSON string: However, some limitations apply: -1. They must be be scoped enumerations. +1. They must be scoped enumerations. -```cpp -/// OK - scoped enumeration -enum class Color1 { red, green, blue, yellow }; + ```cpp + /// OK - scoped enumeration + enum class Color1 { red, green, blue, yellow }; + + /// OK - scoped enumeration + enum struct Color2 { red, green, blue, yellow }; + + /// unsupported - unscoped enumerations + enum Color3 { red, green, blue, yellow }; + ``` -/// OK - scoped enumeration -enum struct Color2 { red, green, blue, yellow }; +2. Enum values must be in the range `[RFL_ENUM_RANGE_MIN, RFL_ENUM_RANGE_MAX]`. If the range is not specified, the + default range is `[0, 127]`. -/// unsupported - unscoped enumerations -enum Color3 { red, green, blue, yellow }; -``` + - You can specify a custom range for the all enum values by defining `RFL_ENUM_RANGE_MIN` and `RFL_ENUM_RANGE_MAX` + before including the reflect-cpp header: -2. You cannot have more than 128 values and if you explicitly assign values, they must be between 0 and 127. - -```cpp -/// OK -enum class Color1 { red, green, blue, yellow }; - -/// OK -enum struct Color2 { red, green, blue = 50, yellow }; - -/// unsupported - red is a negative value -enum Color3 { red = -10, green, blue, yellow }; - -/// unsupported - red is greater than 127 -enum Color3 { red = 200, green, blue, yellow }; -``` + ```cpp + #define RFL_ENUM_RANGE_MIN -128 + #define RFL_ENUM_RANGE_MAX 128 + #include + ``` + - You can specify a custom range for a specific enum by defining the specialization `rfl::config::enum_range` for + the enum type : + ```cpp + + enum class Color { yellow = 200, purple = 300, orange = 400 }; + + template <> + struct rfl::config::enum_range { + static constexpr int min = 200; + static constexpr int max = 400; + }; + ``` ## Flag enums diff --git a/include/rfl/enums.hpp b/include/rfl/enums.hpp index 3b1b4eb9..b7a889b3 100644 --- a/include/rfl/enums.hpp +++ b/include/rfl/enums.hpp @@ -59,6 +59,16 @@ constexpr auto get_underlying_enumerator_array() { return internal::enums::names_to_underlying_enumerator_array(names); } +// Returns the range of the given enum type as a pair of the minimum and maximum +template +constexpr auto get_enum_range() { + return std::make_pair( + internal::enums::get_range_min>(), + internal::enums::get_range_max< + EnumType, internal::enums::is_flag_enum>()); +} + } // namespace rfl #endif // RFL_ENUMS_HPP_ diff --git a/include/rfl/internal/enums/Names.hpp b/include/rfl/internal/enums/Names.hpp index 93a2b6f1..332c2ed8 100644 --- a/include/rfl/internal/enums/Names.hpp +++ b/include/rfl/internal/enums/Names.hpp @@ -14,12 +14,58 @@ #include "../StringLiteral.hpp" namespace rfl { + +// Enum values must be greater than or equal to RFL_ENUM_RANGE_MIN. +// By default, RFL_ENUM_RANGE_MIN is set to 0. +// To change the default minimum range for all enum types, redefine the macro +// RFL_ENUM_RANGE_MIN. +#if !defined(RFL_ENUM_RANGE_MIN) +#define RFL_ENUM_RANGE_MIN 0 +#endif + +// Enum values must be less than or equal to RFL_ENUM_RANGE_MAX. +// By default, RFL_ENUM_RANGE_MAX is set to 127. +// To change the default maximum range for all enum types, redefine the macro +// RFL_ENUM_RANGE_MAX. +#if !defined(RFL_ENUM_RANGE_MAX) +#define RFL_ENUM_RANGE_MAX 127 +#endif + +namespace config { + +// To specify a different range for a particular enum type, specialize the +// enum_range template for that enum type. +template +struct enum_range { + static constexpr int min = RFL_ENUM_RANGE_MIN; + static constexpr int max = RFL_ENUM_RANGE_MAX; +}; +} // namespace config + namespace internal { namespace enums { +static_assert(RFL_ENUM_RANGE_MAX > RFL_ENUM_RANGE_MIN, + "RFL_ENUM_RANGE_MAX must be greater than RFL_ENUM_RANGE_MIN."); + +template +struct range_min : std::integral_constant {}; + +template +struct range_min::min)>> + : std::integral_constant::min), + config::enum_range::min> {}; + +template +struct range_max : std::integral_constant {}; + +template +struct range_max::max)>> + : std::integral_constant::max), + config::enum_range::max> {}; + template -struct Names { + auto... _enums>struct Names { /// Contains a collection of enums as compile-time strings. using Literal = LiteralType; @@ -40,6 +86,17 @@ struct Names { _is_flag, _enums..., _new_enum>>; }; + +template +consteval auto operator|( + Names, + Names) { + using CombinedLiteral = define_literal_t; + return Names{}; +} + template auto names_to_enumerator_named_tuple( diff --git a/include/rfl/internal/enums/get_enum_names.hpp b/include/rfl/internal/enums/get_enum_names.hpp index e69971a0..1f66eb95 100644 --- a/include/rfl/internal/enums/get_enum_names.hpp +++ b/include/rfl/internal/enums/get_enum_names.hpp @@ -79,16 +79,25 @@ consteval auto get_enum_name() { } template -consteval T get_max() { +consteval auto get_range_min() { + using U = std::underlying_type_t; + if constexpr (_is_flag) { + return 0; + } else { + return std::max(std::numeric_limits::min(), range_min::value); + } +} +template +consteval auto get_range_max() { + using U = std::underlying_type_t; if constexpr (_is_flag) { if constexpr (std::is_signed_v) { - return (sizeof(T) * 8 - 2); + return (sizeof(U) * 8 - 2); } else { - return (sizeof(T) * 8 - 1); + return (sizeof(U) * 8 - 1); } } else { - return std::numeric_limits::max() > 127 ? static_cast(127) - : std::numeric_limits::max(); + return std::min(std::numeric_limits::max(), range_max::value); } } @@ -117,22 +126,49 @@ consteval auto operator|(Names, } } +template +struct NamesCombiner { + template + static consteval auto combine(std::integer_sequence) { + return (Names{} | ... | + std::integral_constant()); + } +}; + +template +struct CombineNames { + template + static consteval auto apply() { + if constexpr (End - Start < ChunkSize) { + return [](std::integer_sequence) { + return (NamesType{} | ... | std::integral_constant()); + }(std::make_integer_sequence{}); + } else { + constexpr int Mid = Start + (End - Start) / 2; + constexpr auto left = CombineNames::template apply(); + constexpr auto right = CombineNames::template apply(); + return left | right; + } + } +}; + template consteval auto get_enum_names() { static_assert(is_scoped_enum, - "You must use scoped enums (using class or struct) for the " - "parsing to work!"); - + "You must use scoped enums (using class or struct) for the parsing to work!"); static_assert(std::is_integral_v>, - "The underlying type of any Enum must be integral!"); + "The underlying type of any Enum must be integral!"); + + constexpr auto max = get_range_max(); + constexpr auto min = get_range_min(); + constexpr auto range_size = max - min + 1; + + static_assert(range_size > 0, + "enum_range requires a valid range size. Ensure that max is greater than min."); using EmptyNames = Names, 0, _is_flag>; - constexpr auto max = get_max, _is_flag>(); - - return [](std::integer_sequence) { - return (EmptyNames{} | ... | std::integral_constant()); - }(std::make_integer_sequence()); + return CombineNames<>::template apply(); } } // namespace rfl::internal::enums diff --git a/tests/generic/test_enum.cpp b/tests/generic/test_enum.cpp index 60608b51..1d92f4a5 100644 --- a/tests/generic/test_enum.cpp +++ b/tests/generic/test_enum.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include "write_and_read.hpp" diff --git a/tests/generic/test_enum_range.cpp b/tests/generic/test_enum_range.cpp new file mode 100644 index 00000000..32e33403 --- /dev/null +++ b/tests/generic/test_enum_range.cpp @@ -0,0 +1,40 @@ +#include +// Redefine the range of the enums +#define RFL_ENUM_RANGE_MIN -128 +#define RFL_ENUM_RANGE_MAX 128 +#include +#include + +#include "write_and_read.hpp" + +namespace test_enum_range { + +enum class InnerColor { none = -128, red = -50, green = 0, blue = 128 }; +enum class LineColor { yellow = 200, purple = 300, orange = 400 }; + +struct Circle { + float radius; + InnerColor inner_color; + LineColor line_color; +}; + +} // namespace test_enum_range + +// Define the range of the LineColor enum +template <> +struct rfl::config::enum_range { + static constexpr int min = 200; + static constexpr int max = 400; +}; + +namespace test_enum_range { + +TEST(generic, test_enum_range) { + auto circle = Circle{.radius = 2.0, + .inner_color = InnerColor::none, + .line_color = LineColor::yellow}; + + write_and_read(circle); +} + +} // namespace test_enum_range \ No newline at end of file diff --git a/tests/generic/test_enum_range_min_max.cpp b/tests/generic/test_enum_range_min_max.cpp new file mode 100644 index 00000000..08188538 --- /dev/null +++ b/tests/generic/test_enum_range_min_max.cpp @@ -0,0 +1,33 @@ +#include +// Redefine the range of the enums +#define RFL_ENUM_RANGE_MIN -128 +#define RFL_ENUM_RANGE_MAX 128 +#include + +namespace test_enum_range_min_max { + +enum class InnerColor { none = -128, red = -50, green = 0, blue = 128 }; +enum class LineColor { yellow = 200, purple = 300, orange = 400 }; + +} // namespace test_enum_range_min_max + +// Define the range of the LineColor enum +template <> +struct rfl::config::enum_range { + static constexpr int min = 200; + static constexpr int max = 400; +}; + +namespace test_enum_range_min_max { + +TEST(generic, test_enum_range_min_max) { + auto [inner_min, inner_max] = rfl::get_enum_range(); + EXPECT_EQ(inner_min, RFL_ENUM_RANGE_MIN); + EXPECT_EQ(inner_max, RFL_ENUM_RANGE_MAX); + + auto [line_min, line_max] = rfl::get_enum_range(); + EXPECT_EQ(line_min, 200); + EXPECT_EQ(line_max, 400); +} + +} // namespace test_enum_range_min_max \ No newline at end of file diff --git a/tests/generic/test_enum_range_size.cpp b/tests/generic/test_enum_range_size.cpp new file mode 100644 index 00000000..284ce294 --- /dev/null +++ b/tests/generic/test_enum_range_size.cpp @@ -0,0 +1,37 @@ +#include +// Redefine the range of the enums +#define RFL_ENUM_RANGE_MIN -128 +#define RFL_ENUM_RANGE_MAX 128 +#include + +namespace test_enum_range_size { + +enum class InnerColor { none = -128, red = -50, green = 0, blue = 128 }; +enum class LineColor { yellow = 200, purple = 300, orange = 400 }; +enum class OneColor {black}; + +} // namespace test_enum_range_size + +// Define the range of the LineColor enum +template <> +struct rfl::config::enum_range { + static constexpr int min = 0; + static constexpr int max = 400; +}; + +// Define the range of the OneColor enum +template <> +struct rfl::config::enum_range { + static constexpr int min = 0; + static constexpr int max = 0; +}; + +namespace test_enum_range_size { + +TEST(generic, test_enum_range_size) { + EXPECT_EQ(rfl::get_enumerator_array().size(), 4); + EXPECT_EQ(rfl::get_enumerator_array().size(), 3); + EXPECT_EQ(rfl::get_enumerator_array().size(), 1); +} + +} // namespace test_enum_range_size \ No newline at end of file