From dfc24ad93251f8532dd3a73800a0ffb14dd4cb6f Mon Sep 17 00:00:00 2001 From: AnsiV01 <129291956+AnsiV01@users.noreply.github.com> Date: Thu, 12 Sep 2024 09:37:31 +0200 Subject: [PATCH] `rfl::UnderlyingEnums` (#186) * First part * Plural * Update rfl.hpp * Documentation * Update rfl.hpp * Update processors.md * underlying_enums test * Update Parser_default.hpp * Fix typo in test --- docs/processors.md | 25 ++++++++++++++++ include/rfl.hpp | 1 + include/rfl/Processors.hpp | 6 ++++ include/rfl/UnderlyingEnums.hpp | 18 ++++++++++++ .../rfl/internal/is_underlying_enums_v.hpp | 29 +++++++++++++++++++ include/rfl/parsing/Parser_default.hpp | 27 ++++++++++++----- tests/json/test_underlying_enums.cpp | 25 ++++++++++++++++ 7 files changed, 124 insertions(+), 7 deletions(-) create mode 100644 include/rfl/UnderlyingEnums.hpp create mode 100644 include/rfl/internal/is_underlying_enums_v.hpp create mode 100644 tests/json/test_underlying_enums.cpp diff --git a/docs/processors.md b/docs/processors.md index 9f6447e8..8d721db7 100644 --- a/docs/processors.md +++ b/docs/processors.md @@ -34,7 +34,9 @@ The resulting JSON string looks like this: reflect-cpp currently supports the following processors: - `rfl::AddStructName` +- `rfl::NoFieldNames` - `rfl::NoOptionals` +- `rfl::UnderlyingEnums` - `rfl::SnakeCaseToCamelCase` - `rfl::SnakeCaseToPascalCase` @@ -117,6 +119,29 @@ leaving out the field `town`. However, if you want to require the field rfl::json::read(json_string); ``` +### `rfl::UnderlyingEnums` + +By passing the processor `rfl::UnderlyingEnums`, fields of the enum type will be written and read as integers + +```cpp +enum class Color { red, green, blue, yellow }; + +struct Circle { + float radius; + Color color; +}; + +const auto circle = Circle{.radius = 2.0, .color = Color::green}; + +rfl::json::write(circle); +``` + +The resulting JSON string looks like this: + +```json +{"radius":2.0,"color":1} +``` + ### `rfl::SnakeCaseToCamelCase` Please refer to the example above. diff --git a/include/rfl.hpp b/include/rfl.hpp index d6ad0745..087d52a4 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -38,6 +38,7 @@ #include "rfl/SnakeCaseToPascalCase.hpp" #include "rfl/TaggedUnion.hpp" #include "rfl/Timestamp.hpp" +#include "rfl/UnderlyingEnums.hpp" #include "rfl/Validator.hpp" #include "rfl/Variant.hpp" #include "rfl/always_false.hpp" diff --git a/include/rfl/Processors.hpp b/include/rfl/Processors.hpp index 302e0c24..318635c2 100644 --- a/include/rfl/Processors.hpp +++ b/include/rfl/Processors.hpp @@ -5,6 +5,7 @@ #include "internal/is_no_field_names_v.hpp" #include "internal/is_no_optionals_v.hpp" +#include "internal/is_underlying_enums_v.hpp" namespace rfl { @@ -15,6 +16,7 @@ template <> struct Processors<> { static constexpr bool all_required_ = false; static constexpr bool no_field_names_ = false; + static constexpr bool underlying_enums_ = false; template static auto process(NamedTupleType&& _named_tuple) { @@ -32,6 +34,10 @@ struct Processors { std::disjunction_v, internal::is_no_field_names...>; + static constexpr bool underlying_enums_ = + std::disjunction_v, + internal::is_underlying_enums...>; + template static auto process(NamedTupleType&& _named_tuple) { return Processors::template process( diff --git a/include/rfl/UnderlyingEnums.hpp b/include/rfl/UnderlyingEnums.hpp new file mode 100644 index 00000000..87f3249d --- /dev/null +++ b/include/rfl/UnderlyingEnums.hpp @@ -0,0 +1,18 @@ +#ifndef RFL_UNDERLYINGENUMS_HPP_ +#define RFL_UNDERLYINGENUMS_HPP_ + +namespace rfl { + +/// This is a ‘fake’ processor - it doesn't do much by itself, but its +/// its inclusion instructs parsers not to convert enum types to strings, but to integers +struct UnderlyingEnums { + public: + template + static auto process(auto&& _named_tuple) { + return _named_tuple; + } +}; + +} // namespace rfl + +#endif diff --git a/include/rfl/internal/is_underlying_enums_v.hpp b/include/rfl/internal/is_underlying_enums_v.hpp new file mode 100644 index 00000000..1a9479ca --- /dev/null +++ b/include/rfl/internal/is_underlying_enums_v.hpp @@ -0,0 +1,29 @@ +#ifndef RFL_INTERNAL_ISUNDERLYINGENUMS_HPP_ +#define RFL_INTERNAL_ISUNDERLYINGENUMS_HPP_ + +#include +#include +#include + +#include "../UnderlyingEnums.hpp" + +namespace rfl { +namespace internal { + +template +class is_underlying_enums; + +template +class is_underlying_enums : public std::false_type {}; + +template <> +class is_underlying_enums : public std::true_type {}; + +template +constexpr bool is_underlying_enums_v = + is_underlying_enums>>::value; + +} // namespace internal +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/Parser_default.hpp b/include/rfl/parsing/Parser_default.hpp index 52c73573..0f4d6b77 100644 --- a/include/rfl/parsing/Parser_default.hpp +++ b/include/rfl/parsing/Parser_default.hpp @@ -15,6 +15,7 @@ #include "../internal/is_basic_type.hpp" #include "../internal/is_description.hpp" #include "../internal/is_literal.hpp" +#include "../internal/is_underlying_enums_v.hpp" #include "../internal/is_validator.hpp" #include "../internal/processed_t.hpp" #include "../internal/to_ptr_named_tuple.hpp" @@ -69,9 +70,13 @@ struct Parser { } else if constexpr (std::is_class_v && std::is_aggregate_v) { return read_struct(_r, _var); } else if constexpr (std::is_enum_v) { - using StringConverter = internal::enums::StringConverter; - return _r.template to_basic_type(_var).and_then( - StringConverter::string_to_enum); + if constexpr (ProcessorsType::underlying_enums_) { + return static_cast(*_r.template to_basic_type>(_var)); + } else { + using StringConverter = internal::enums::StringConverter; + return _r.template to_basic_type(_var).and_then( + StringConverter::string_to_enum); + } } else { return _r.template to_basic_type>(_var); } @@ -99,9 +104,14 @@ struct Parser { Parser::write( _w, ptr_named_tuple, _parent); } else if constexpr (std::is_enum_v) { - using StringConverter = internal::enums::StringConverter; - const auto str = StringConverter::enum_to_string(_var); - ParentType::add_value(_w, str, _parent); + if constexpr (ProcessorsType::underlying_enums_) { + const auto val = static_cast>(_var); + ParentType::add_value(_w, val, _parent); + } else { + using StringConverter = internal::enums::StringConverter; + const auto str = StringConverter::enum_to_string(_var); + ParentType::add_value(_w, str, _parent); + } } else { ParentType::add_value(_w, _var, _parent); } @@ -179,7 +189,10 @@ struct Parser { std::map* _definitions) { using Type = schema::Type; using S = internal::enums::StringConverter; - if constexpr (S::is_flag_enum_) { + if constexpr (ProcessorsType::underlying_enums_) { + return Type{Type::Integer{}}; + } + else if constexpr (S::is_flag_enum_) { return Type{Type::String{}}; } else { return Parser::to_schema( diff --git a/tests/json/test_underlying_enums.cpp b/tests/json/test_underlying_enums.cpp new file mode 100644 index 00000000..c2f58ea7 --- /dev/null +++ b/tests/json/test_underlying_enums.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_underlying_enums { + +enum class Color { red, green, blue, yellow }; + +struct Circle { + float radius; + Color color; +}; + +TEST(json, test_underlying_enums) { + const auto circle = Circle{.radius = 2.0, .color = Color::green}; + + write_and_read(circle, R"({"radius":2.0,"color":1})"); +} + +} // namespace test_enum1