From 63e7bf3d815628a329ba503627ed93d6766af69e Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 31 Mar 2024 14:37:34 +0200 Subject: [PATCH] Allow tagged unions with explicit tags --- include/rfl/TaggedUnion.hpp | 60 +++++++++++++++++- include/rfl/field_type.hpp | 5 +- include/rfl/internal/Getter.hpp | 62 +------------------ .../internal/bind_fake_object_to_tuple.hpp | 1 - include/rfl/internal/get_field_names.hpp | 8 +-- include/rfl/internal/is_empty.hpp | 7 ++- include/rfl/internal/make_tag.hpp | 15 +++-- include/rfl/internal/tag_t.hpp | 12 ++-- include/rfl/parsing/Parser_tagged_union.hpp | 35 ++++++----- tests/json/test_tagged_union3.cpp | 45 ++++++++++++++ tests/json/test_tagged_union3.hpp | 4 ++ tests/json/tests.cpp | 2 + 12 files changed, 156 insertions(+), 100 deletions(-) create mode 100644 tests/json/test_tagged_union3.cpp create mode 100644 tests/json/test_tagged_union3.hpp diff --git a/include/rfl/TaggedUnion.hpp b/include/rfl/TaggedUnion.hpp index a44ee159..aa50600e 100644 --- a/include/rfl/TaggedUnion.hpp +++ b/include/rfl/TaggedUnion.hpp @@ -4,6 +4,7 @@ #include #include "define_literal.hpp" +#include "internal/Getter.hpp" #include "internal/StringLiteral.hpp" #include "internal/tag_t.hpp" @@ -84,7 +85,8 @@ struct TaggedUnion { /// Returns the underlying variant. const VariantType& variant() const { return variant_; } - static_assert(!define_literal_t...>::has_duplicates(), + static_assert(!define_literal_t< + internal::tag_t<_discriminator, Ts>...>::has_duplicates(), "Duplicate tags are not allowed inside tagged unions."); /// The underlying variant - a TaggedUnion is a thin wrapper @@ -92,6 +94,62 @@ struct TaggedUnion { VariantType variant_; }; +namespace internal { + +template +struct Getter> { + public: + /// Retrieves the indicated value from the tuple. + template + static inline auto& get( + TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { + return Getter>::template get<_index>( + _tu.variant_); + } + + /// Gets a field by name. + template + static inline auto& get( + TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { + return Getter>::template get<_field_name>( + _tu.variant_); + } + + /// Gets a field by the field type. + template + static inline auto& get( + TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { + return Getter>::template get( + _tu.variant_); + } + + /// Retrieves the indicated value from the tuple. + template + static inline const auto& get_const( + const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { + return Getter>::template get_const<_index>( + _tu.variant_); + } + + /// Gets a field by name. + template + static inline const auto& get_const( + const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { + return Getter>::template get_const< + _field_name>(_tu.variant_); + } + + /// Gets a field by the field type. + template + static inline const auto& get_const( + const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { + return Getter>::template get_const( + _tu.variant_); + } +}; + +} // namespace internal + } // namespace rfl #endif // RFL_TAGGEDUNION_HPP_ diff --git a/include/rfl/field_type.hpp b/include/rfl/field_type.hpp index 1e97fca1..9d79778c 100644 --- a/include/rfl/field_type.hpp +++ b/include/rfl/field_type.hpp @@ -10,9 +10,8 @@ namespace rfl { -template -using field_type_t = - typename internal::FieldType<_field_name, NamedTupleType>::Type; +template +using field_type_t = typename internal::FieldType<_field_name, T>::Type; } // namespace rfl diff --git a/include/rfl/internal/Getter.hpp b/include/rfl/internal/Getter.hpp index cee1742a..03824624 100644 --- a/include/rfl/internal/Getter.hpp +++ b/include/rfl/internal/Getter.hpp @@ -4,12 +4,10 @@ #include #include -#include "../TaggedUnion.hpp" #include "StringLiteral.hpp" #include "find_index.hpp" -namespace rfl { -namespace internal { +namespace rfl::internal { // ---------------------------------------------------------------------------- @@ -153,62 +151,6 @@ struct Getter> { // ---------------------------------------------------------------------------- -/// For handling std::variant. -template -struct Getter> { - public: - /// Retrieves the indicated value from the tuple. - template - static inline auto& get( - TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { - return Getter>::template get<_index>( - _tu.variant_); - } - - /// Gets a field by name. - template - static inline auto& get( - TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { - return Getter>::template get<_field_name>( - _tu.variant_); - } - - /// Gets a field by the field type. - template - static inline auto& get( - TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { - return Getter>::template get( - _tu.variant_); - } - - /// Retrieves the indicated value from the tuple. - template - static inline const auto& get_const( - const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { - return Getter>::template get_const<_index>( - _tu.variant_); - } - - /// Gets a field by name. - template - static inline const auto& get_const( - const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { - return Getter>::template get_const< - _field_name>(_tu.variant_); - } - - /// Gets a field by the field type. - template - static inline const auto& get_const( - const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { - return Getter>::template get_const( - _tu.variant_); - } -}; - -// ---------------------------------------------------------------------------- - -} // namespace internal -} // namespace rfl +} // namespace rfl::internal #endif diff --git a/include/rfl/internal/bind_fake_object_to_tuple.hpp b/include/rfl/internal/bind_fake_object_to_tuple.hpp index 26d1652d..2929306c 100644 --- a/include/rfl/internal/bind_fake_object_to_tuple.hpp +++ b/include/rfl/internal/bind_fake_object_to_tuple.hpp @@ -9,7 +9,6 @@ #include "../always_false.hpp" #include "get_fake_object.hpp" -#include "is_named_tuple.hpp" #include "num_fields.hpp" namespace rfl { diff --git a/include/rfl/internal/get_field_names.hpp b/include/rfl/internal/get_field_names.hpp index 01e743e2..8f5bd3c8 100644 --- a/include/rfl/internal/get_field_names.hpp +++ b/include/rfl/internal/get_field_names.hpp @@ -23,8 +23,7 @@ #endif #endif -namespace rfl { -namespace internal { +namespace rfl::internal { template struct Wrapper { @@ -112,6 +111,8 @@ auto concat_literals(const Head& _head, const Tail&... _tail) { } } +inline auto concat_literals() { return rfl::Literal<>(); } + #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wundefined-var-template" @@ -150,7 +151,6 @@ auto get_field_names() { #pragma clang diagnostic pop #endif -} // namespace internal -} // namespace rfl +} // namespace rfl::internal #endif diff --git a/include/rfl/internal/is_empty.hpp b/include/rfl/internal/is_empty.hpp index 5c721d01..a671ef00 100644 --- a/include/rfl/internal/is_empty.hpp +++ b/include/rfl/internal/is_empty.hpp @@ -10,10 +10,11 @@ namespace rfl::internal { template constexpr bool is_empty() { - if constexpr (is_named_tuple_v) { - return std::remove_cvref_t>::size() == 0; + using U = std::remove_cvref_t>; + if constexpr (is_named_tuple_v) { + return U::size() == 0; } else { - using TupleType = ptr_tuple_t; + using TupleType = ptr_tuple_t; return std::tuple_size_v == 0; } } diff --git a/include/rfl/internal/make_tag.hpp b/include/rfl/internal/make_tag.hpp index ce747c19..4004a3dd 100644 --- a/include/rfl/internal/make_tag.hpp +++ b/include/rfl/internal/make_tag.hpp @@ -2,18 +2,22 @@ #define RFL_INTERNAL_MAKE_TAG_HPP_ #include "../Literal.hpp" +#include "../field_names_t.hpp" +#include "../to_view.hpp" +#include "StringLiteral.hpp" #include "get_type_name.hpp" #include "has_reflection_type_v.hpp" #include "has_tag_v.hpp" #include "remove_namespaces.hpp" -namespace rfl { -namespace internal { +namespace rfl::internal { -template -static inline auto make_tag() noexcept { +template +static inline auto make_tag(const T& _t) noexcept { if constexpr (internal::has_reflection_type_v) { return make_tag(); + } else if constexpr (field_names_t::template contains<_discriminator>()) { + return *to_view(_t).template get<_discriminator>(); } else if constexpr (internal::has_tag_v) { using LiteralType = typename T::Tag; return LiteralType::template name_of<0>(); @@ -23,7 +27,6 @@ static inline auto make_tag() noexcept { } } -} // namespace internal -} // namespace rfl +} // namespace rfl::internal #endif diff --git a/include/rfl/internal/tag_t.hpp b/include/rfl/internal/tag_t.hpp index 927b0743..02278c82 100644 --- a/include/rfl/internal/tag_t.hpp +++ b/include/rfl/internal/tag_t.hpp @@ -3,15 +3,15 @@ #include +#include "StringLiteral.hpp" #include "make_tag.hpp" -namespace rfl { -namespace internal { +namespace rfl::internal { -template -using tag_t = typename std::invoke_result)>::type; +template +using tag_t = + typename std::invoke_result), T>::type; -} // namespace internal -} // namespace rfl +} // namespace rfl::internal #endif diff --git a/include/rfl/parsing/Parser_tagged_union.hpp b/include/rfl/parsing/Parser_tagged_union.hpp index fbc490aa..4efcfbb3 100644 --- a/include/rfl/parsing/Parser_tagged_union.hpp +++ b/include/rfl/parsing/Parser_tagged_union.hpp @@ -53,9 +53,8 @@ struct Parser> { static schema::Type to_schema( std::map* _definitions) { - using VariantType = - std::variant), - AlternativeTypes>...>; + using VariantType = std::variant), AlternativeTypes>...>; return Parser::to_schema(_definitions); } @@ -117,32 +116,36 @@ struct Parser> { template static inline bool contains_disc_value( const std::string& _disc_value) noexcept { - return internal::tag_t::contains(_disc_value); + return internal::tag_t<_discriminator, T>::contains(_disc_value); } /// Writes a wrapped version of the original object, which contains the tag. template static void write_wrapped(const W& _w, const T& _val, const P& _parent) noexcept { - const auto wrapped = wrap(_val); + const auto wrapped = wrap_if_necessary(_val); Parser>::write(_w, wrapped, _parent); } /// Generates a wrapped version of the original object, which contains the - /// tag. + /// tag, if the object doesn't already contain the wrap. template - static auto wrap(const T& _val) noexcept { - const auto tag = internal::make_tag(); - using TagType = std::remove_cvref_t; - if constexpr (internal::has_fields>()) { - using WrapperType = - TaggedUnionWrapperWithFields; - return WrapperType{.tag = tag, .fields = &_val}; + static auto wrap_if_necessary(const T& _val) noexcept { + if constexpr (field_names_t::template contains<_discriminator>()) { + return _val; } else { - using WrapperType = - TaggedUnionWrapperNoFields; - return WrapperType{.tag = tag, .fields = &_val}; + const auto tag = internal::make_tag<_discriminator, T>(_val); + using TagType = std::remove_cvref_t; + if constexpr (internal::has_fields>()) { + using WrapperType = + TaggedUnionWrapperWithFields; + return WrapperType{.tag = tag, .fields = &_val}; + } else { + using WrapperType = + TaggedUnionWrapperNoFields; + return WrapperType{.tag = tag, .fields = &_val}; + } } } }; diff --git a/tests/json/test_tagged_union3.cpp b/tests/json/test_tagged_union3.cpp new file mode 100644 index 00000000..82b14a04 --- /dev/null +++ b/tests/json/test_tagged_union3.cpp @@ -0,0 +1,45 @@ +#include "test_tagged_union3.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_tagged_union3 { + +struct Circle { + rfl::Literal<"circle", "Circle"> shape; + double radius; +}; + +struct Rectangle { + rfl::Literal<"rectangle", "Rectangle", "rect"> shape; + double height; + double width; +}; + +struct Square { + rfl::Literal<"square", "Square"> shape; + double width; +}; + +struct Empty {}; + +using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle, Empty>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = Rectangle{ + .shape = rfl::Literal<"rectangle", "Rectangle", "rect">::make<"rect">(), + .height = 10, + .width = 5}; + + write_and_read(r, R"({"shape":"rect","height":10.0,"width":5.0})"); +} +} // namespace test_tagged_union3 diff --git a/tests/json/test_tagged_union3.hpp b/tests/json/test_tagged_union3.hpp new file mode 100644 index 00000000..c40beebf --- /dev/null +++ b/tests/json/test_tagged_union3.hpp @@ -0,0 +1,4 @@ +namespace test_tagged_union3 { +void test(); +} + diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index ed5ab8de..ebcffe3b 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -73,6 +73,7 @@ #include "test_string_unordered_map.hpp" #include "test_tagged_union.hpp" #include "test_tagged_union2.hpp" +#include "test_tagged_union3.hpp" #include "test_timestamp.hpp" #include "test_transform.hpp" #include "test_unique_ptr.hpp" @@ -97,6 +98,7 @@ int main() { test_variant::test(); test_tagged_union::test(); test_tagged_union2::test(); + test_tagged_union3::test(); test_field_variant::test(); test_ref::test(); test_box::test();