From 456fc336133cf8ca9cc140d8b715f347a781e253 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Tue, 28 Nov 2023 19:09:14 +0100 Subject: [PATCH] Added support for tagged unions without explicitly tagging the structs --- include/rfl/internal/get_struct_name.hpp | 41 ++++++++++++++++++++++ include/rfl/internal/has_tag_v.hpp | 31 ++++++++++++++++ include/rfl/parsing/Parser.hpp | 21 ++++++----- include/rfl/parsing/TaggedUnionWrapper.hpp | 6 ++-- tests/json/test_tagged_union.hpp | 11 +++--- tests/json/test_tagged_union2.hpp | 37 +++++++++++++++++++ tests/json/tests.cpp | 2 ++ 7 files changed, 131 insertions(+), 18 deletions(-) create mode 100644 include/rfl/internal/get_struct_name.hpp create mode 100644 include/rfl/internal/has_tag_v.hpp create mode 100644 tests/json/test_tagged_union2.hpp diff --git a/include/rfl/internal/get_struct_name.hpp b/include/rfl/internal/get_struct_name.hpp new file mode 100644 index 00000000..64c7d0bb --- /dev/null +++ b/include/rfl/internal/get_struct_name.hpp @@ -0,0 +1,41 @@ +#ifndef RFL_INTERNAL_GETSTRUCTNAME_HPP_ +#define RFL_INTERNAL_GETSTRUCTNAME_HPP_ + +#include +#include +#include + +namespace rfl { +namespace internal { + +std::string remove_namespaces(const std::string& _name) { + const size_t pos = _name.find_last_of(":"); + if (pos == std::string::npos) { + return _name; + } + return _name.substr(pos + 1); +} + +template +std::string get_struct_name_impl() { + const std::string func_name = std::source_location::current().function_name(); + const size_t begin = func_name.find("T = ") + 4; + if (begin == std::string::npos) { + return "AnonymousStruct"; + } + const size_t end = func_name.find_first_of("])};", begin); + if (end == std::string::npos) { + return "AnonymousStruct"; + } + return remove_namespaces(func_name.substr(begin, end - begin)); +} + +template +std::string get_struct_name() { + return get_struct_name_impl>>(); +} + +} // namespace internal +} // namespace rfl + +#endif diff --git a/include/rfl/internal/has_tag_v.hpp b/include/rfl/internal/has_tag_v.hpp new file mode 100644 index 00000000..29d20f5d --- /dev/null +++ b/include/rfl/internal/has_tag_v.hpp @@ -0,0 +1,31 @@ +#ifndef RFL_HASTAGV_HPP_ +#define RFL_HASTAGV_HPP_ + +#include +#include + +namespace rfl { +namespace internal { + +template +class HasTag { + private: + template + static std::int64_t foo(...); + + template + static std::int32_t foo(typename U::Tag*); + + public: + static constexpr bool value = + sizeof(foo(nullptr)) == sizeof(std::int32_t); +}; + +/// Used for tagged unions - determines whether a struct as a Tag. +template +constexpr bool has_tag_v = HasTag::value; + +} // namespace internal +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index 818f81b9..5c82785f 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -38,9 +38,11 @@ #include "rfl/internal/flattened_ptr_tuple_t.hpp" #include "rfl/internal/flattened_tuple_t.hpp" #include "rfl/internal/get_field_names.hpp" +#include "rfl/internal/get_struct_name.hpp" #include "rfl/internal/has_fields.hpp" #include "rfl/internal/has_reflection_method_v.hpp" #include "rfl/internal/has_reflection_type_v.hpp" +#include "rfl/internal/has_tag_v.hpp" #include "rfl/internal/is_basic_type.hpp" #include "rfl/internal/move_from_tuple.hpp" #include "rfl/internal/no_duplicate_field_names.hpp" @@ -1067,23 +1069,26 @@ struct Parser> { template static inline bool contains_disc_value( const std::string& _disc_value) noexcept { - if constexpr (!internal::has_reflection_type_v) { + if constexpr (internal::has_reflection_type_v) { + return contains_disc_value(_disc_value); + } else if constexpr (internal::has_tag_v) { using LiteralType = typename T::Tag; return LiteralType::contains(_disc_value); - } else { - using LiteralType = typename T::ReflectionType::Tag; return LiteralType::contains(_disc_value); + } else { + return _disc_value == internal::get_struct_name(); } } template - static inline auto make_tag() noexcept { - if constexpr (!internal::has_reflection_type_v) { + static inline std::string make_tag() noexcept { + if constexpr (internal::has_reflection_type_v) { + return make_tag(); + } else if constexpr (internal::has_tag_v) { using LiteralType = typename T::Tag; - return LiteralType::template name_of<0>(); + return LiteralType::template name_of<0>().str(); } else { - using LiteralType = typename T::ReflectionType::Tag; - return LiteralType::template name_of<0>(); + return internal::get_struct_name(); } } diff --git a/include/rfl/parsing/TaggedUnionWrapper.hpp b/include/rfl/parsing/TaggedUnionWrapper.hpp index 474c8abc..064ef107 100644 --- a/include/rfl/parsing/TaggedUnionWrapper.hpp +++ b/include/rfl/parsing/TaggedUnionWrapper.hpp @@ -1,7 +1,7 @@ #ifndef RFL_PARSING_TAGGEDUNIONWRAPPER_HPP_ #define RFL_PARSING_TAGGEDUNIONWRAPPER_HPP_ -#include +#include #include "rfl/Flatten.hpp" #include "rfl/Rename.hpp" @@ -12,13 +12,13 @@ namespace parsing { template struct TaggedUnionWrapperNoFields { - rfl::Rename<_discriminator, LiteralType> tag; + rfl::Rename<_discriminator, std::string> tag; rfl::Flatten fields; }; template struct TaggedUnionWrapperWithFields { - rfl::Field<_discriminator, LiteralType> tag; + rfl::Field<_discriminator, std::string> tag; rfl::Flatten fields; }; diff --git a/tests/json/test_tagged_union.hpp b/tests/json/test_tagged_union.hpp index 17875e2b..0246ee55 100644 --- a/tests/json/test_tagged_union.hpp +++ b/tests/json/test_tagged_union.hpp @@ -10,19 +10,16 @@ namespace test_tagged_union { struct Circle { - using Tag = rfl::Literal<"Circle">; - rfl::Field<"radius", double> radius; + double radius; }; struct Rectangle { - using Tag = rfl::Literal<"Rectangle">; - rfl::Field<"height", double> height; - rfl::Field<"width", double> width; + double height; + double width; }; struct Square { - using Tag = rfl::Literal<"Square">; - rfl::Field<"width", double> width; + double width; }; using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; diff --git a/tests/json/test_tagged_union2.hpp b/tests/json/test_tagged_union2.hpp new file mode 100644 index 00000000..03c89fd8 --- /dev/null +++ b/tests/json/test_tagged_union2.hpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_tagged_union2 { + +struct Circle { + using Tag = rfl::Literal<"Circle">; + rfl::Field<"radius", double> radius; +}; + +struct Rectangle { + using Tag = rfl::Literal<"Rectangle">; + rfl::Field<"height", double> height; + rfl::Field<"width", double> width; +}; + +struct Square { + using Tag = rfl::Literal<"Square">; + rfl::Field<"width", double> width; +}; + +using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; + +void test() { + std::cout << "test_tagged_union2" << std::endl; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r, R"({"shape":"Rectangle","height":10.0,"width":5.0})"); +} +} // namespace test_tagged_union2 diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index f28403c3..a9c8784e 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -44,6 +44,7 @@ #include "test_string_map.hpp" #include "test_string_unordered_map.hpp" #include "test_tagged_union.hpp" +#include "test_tagged_union2.hpp" #include "test_timestamp.hpp" #include "test_unique_ptr.hpp" #include "test_unique_ptr2.hpp" @@ -62,6 +63,7 @@ int main() { test_literal::test(); test_variant::test(); test_tagged_union::test(); + test_tagged_union2::test(); test_field_variant::test(); test_ref::test(); test_box::test();