From 385f6434c5133cfb0968945cc7e22ce9c8426b10 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Thu, 7 Dec 2023 23:18:06 +0100 Subject: [PATCH] Make sure that we can retrieve the type names and tags at compile time --- include/rfl/TaggedUnion.hpp | 127 +++++++++++---------- include/rfl/internal/get_struct_name.hpp | 52 --------- include/rfl/internal/get_type_name.hpp | 44 +++++++ include/rfl/internal/make_tag.hpp | 29 +++++ include/rfl/internal/remove_namespaces.hpp | 30 +++++ include/rfl/internal/tag_t.hpp | 17 +++ include/rfl/parsing/Parser.hpp | 28 +---- include/rfl/parsing/TaggedUnionWrapper.hpp | 8 +- include/rfl/type_name_t.hpp | 17 +++ 9 files changed, 210 insertions(+), 142 deletions(-) delete mode 100644 include/rfl/internal/get_struct_name.hpp create mode 100644 include/rfl/internal/get_type_name.hpp create mode 100644 include/rfl/internal/make_tag.hpp create mode 100644 include/rfl/internal/remove_namespaces.hpp create mode 100644 include/rfl/internal/tag_t.hpp create mode 100644 include/rfl/type_name_t.hpp diff --git a/include/rfl/TaggedUnion.hpp b/include/rfl/TaggedUnion.hpp index 7d3a9d8b..d6d3e421 100644 --- a/include/rfl/TaggedUnion.hpp +++ b/include/rfl/TaggedUnion.hpp @@ -3,92 +3,93 @@ #include +#include "rfl/define_literal.hpp" #include "rfl/internal/StringLiteral.hpp" +#include "rfl/internal/tag_t.hpp" namespace rfl { // https://serde.rs/enum-representations.html -template +template struct TaggedUnion { - static constexpr internal::StringLiteral discrimininator_ = _discriminator; + static constexpr internal::StringLiteral discrimininator_ = _discriminator; - /// The type of the underlying variant. - using VariantType = std::variant; + /// The type of the underlying variant. + using VariantType = std::variant; - TaggedUnion(const VariantType& _variant) : variant_(_variant) {} + TaggedUnion(const VariantType& _variant) : variant_(_variant) {} - TaggedUnion(VariantType&& _variant) noexcept - : variant_(std::move(_variant)) {} + TaggedUnion(VariantType&& _variant) noexcept + : variant_(std::move(_variant)) {} - TaggedUnion(const TaggedUnion<_discriminator, NamedTupleTypes...>& - _tagged_union) = default; + TaggedUnion(const TaggedUnion<_discriminator, Ts...>& _tagged_union) = + default; - TaggedUnion(TaggedUnion<_discriminator, NamedTupleTypes...>&& - _tagged_union) noexcept = default; + TaggedUnion(TaggedUnion<_discriminator, Ts...>&& _tagged_union) noexcept = + default; - template , - bool>::type = true> - TaggedUnion(const T& _t) : variant_(_t) {} + template , + bool>::type = true> + TaggedUnion(const T& _t) : variant_(_t) {} - template , - bool>::type = true> - TaggedUnion(T&& _t) noexcept : variant_(std::forward(_t)) {} + template , + bool>::type = true> + TaggedUnion(T&& _t) noexcept : variant_(std::forward(_t)) {} - ~TaggedUnion() = default; + ~TaggedUnion() = default; - /// Assigns the underlying object. - TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( - const VariantType& _variant) { - variant_ = _variant; - return *this; - } + /// Assigns the underlying object. + TaggedUnion<_discriminator, Ts...>& operator=(const VariantType& _variant) { + variant_ = _variant; + return *this; + } - /// Assigns the underlying object. - TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( - VariantType&& _variant) { - variant_ = std::move(_variant); - return *this; - } + /// Assigns the underlying object. + TaggedUnion<_discriminator, Ts...>& operator=(VariantType&& _variant) { + variant_ = std::move(_variant); + return *this; + } - /// Assigns the underlying object. - template , - bool>::type = true> - TaggedUnion<_discriminator, NamedTupleTypes...>& operator=(T&& _variant) { - variant_ = std::forward(_variant); - return *this; - } + /// Assigns the underlying object. + template , + bool>::type = true> + TaggedUnion<_discriminator, Ts...>& operator=(T&& _variant) { + variant_ = std::forward(_variant); + return *this; + } - /// Assigns the underlying object. - template , - bool>::type = true> - TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( - const T& _variant) { - variant_ = _variant; - return *this; - } + /// Assigns the underlying object. + template , + bool>::type = true> + TaggedUnion<_discriminator, Ts...>& operator=(const T& _variant) { + variant_ = _variant; + return *this; + } - /// Assigns the underlying object. - TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( - const TaggedUnion<_discriminator, NamedTupleTypes...>& _other) = - default; + /// Assigns the underlying object. + TaggedUnion<_discriminator, Ts...>& operator=( + const TaggedUnion<_discriminator, Ts...>& _other) = default; - /// Assigns the underlying object. - TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( - TaggedUnion<_discriminator, NamedTupleTypes...>&& _other) = default; + /// Assigns the underlying object. + TaggedUnion<_discriminator, Ts...>& operator=( + TaggedUnion<_discriminator, Ts...>&& _other) = default; - /// Returns the underlying variant. - VariantType& variant() { return variant_; } + /// Returns the underlying variant. + VariantType& variant() { return variant_; } - /// Returns the underlying variant. - const VariantType& variant() const { return variant_; } + /// Returns the underlying variant. + const VariantType& variant() const { return variant_; } - /// The underlying variant - a TaggedUnion is a thin wrapper - /// around a variant that is mainly used for parsing. - VariantType variant_; + static_assert(!define_literal_t...>::has_duplicates(), + "Duplicate tags are not allowed inside tagged unions."); + + /// The underlying variant - a TaggedUnion is a thin wrapper + /// around a variant that is mainly used for parsing. + VariantType variant_; }; } // namespace rfl diff --git a/include/rfl/internal/get_struct_name.hpp b/include/rfl/internal/get_struct_name.hpp deleted file mode 100644 index 2b457163..00000000 --- a/include/rfl/internal/get_struct_name.hpp +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef RFL_INTERNAL_GETSTRUCTNAME_HPP_ -#define RFL_INTERNAL_GETSTRUCTNAME_HPP_ - -#include -#include -#include - -namespace rfl { -namespace internal { - -inline 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); -} - -inline size_t find_begin(const std::string& _func_name) { - const size_t pos1 = _func_name.find("T = "); - if (pos1 != std::string::npos) { - // clang and gcc - return pos1 + 4; - } - // MSVC - const size_t pos2 = _func_name.find_last_of(" "); - return (pos2 != std::string::npos) ? pos2 + 1 : pos2; -} - -template -std::string get_struct_name_impl() { - const std::string func_name = std::source_location::current().function_name(); - const size_t begin = find_begin(func_name); - 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/get_type_name.hpp b/include/rfl/internal/get_type_name.hpp new file mode 100644 index 00000000..b0147994 --- /dev/null +++ b/include/rfl/internal/get_type_name.hpp @@ -0,0 +1,44 @@ +#ifndef RFL_INTERNAL_GETTYPENAME_HPP_ +#define RFL_INTERNAL_GETTYPENAME_HPP_ + +#include +#include +#include + +namespace rfl { +namespace internal { + +template +consteval auto get_type_name_str_view() { + const auto func_name = + std::string_view{std::source_location::current().function_name()}; +#if defined(__clang__) + const auto split = func_name.substr(0, func_name.size() - 1); + return split.substr(split.find("T = ") + 4); +#elif defined(__GNUC__) + const auto split = func_name.substr(0, func_name.size() - 1); + return split.substr(split.find("T = ") + 4); +#elif defined(_MSC_VER) + const auto split = func_name.substr(0, func_name.size() - 7); + return split.substr(split.find("get_type_name_str_view<") + 23); +#else + static_assert( + false, + "You are using an unsupported compiler. Please use GCC, Clang " + "or MSVC or explicitly tag your structs using 'Tag' or 'Name'."); +#endif +} + +template +consteval auto get_type_name() { + constexpr auto name = get_type_name_str_view(); + const auto to_str_lit = [&](std::index_sequence) { + return StringLiteral{name[Ns]...}; + }; + return to_str_lit(std::make_index_sequence{}); +} + +} // namespace internal +} // namespace rfl + +#endif diff --git a/include/rfl/internal/make_tag.hpp b/include/rfl/internal/make_tag.hpp new file mode 100644 index 00000000..4854824f --- /dev/null +++ b/include/rfl/internal/make_tag.hpp @@ -0,0 +1,29 @@ +#ifndef RFL_INTERNAL_MAKE_TAG_HPP_ +#define RFL_INTERNAL_MAKE_TAG_HPP_ + +#include "rfl/Literal.hpp" +#include "rfl/internal/get_type_name.hpp" +#include "rfl/internal/has_reflection_type_v.hpp" +#include "rfl/internal/has_tag_v.hpp" +#include "rfl/internal/remove_namespaces.hpp" + +namespace rfl { +namespace internal { + +template +static inline auto 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>(); + } else { + return rfl::Literal< + internal::remove_namespaces()>()>(); + } +} + +} // namespace internal +} // namespace rfl + +#endif diff --git a/include/rfl/internal/remove_namespaces.hpp b/include/rfl/internal/remove_namespaces.hpp new file mode 100644 index 00000000..835d4d27 --- /dev/null +++ b/include/rfl/internal/remove_namespaces.hpp @@ -0,0 +1,30 @@ +#ifndef RFL_INTERNAL_REMOVE_NAMESPACES_HPP_ +#define RFL_INTERNAL_REMOVE_NAMESPACES_HPP_ + +#include +#include +#include + +#include "rfl/internal/StringLiteral.hpp" + +namespace rfl { +namespace internal { + +template +consteval auto remove_namespaces() { + constexpr auto name = _name.string_view(); + constexpr size_t pos = name.find_last_of(":"); + if constexpr (pos == std::string_view::npos) { + return _name; + } + constexpr auto substr = name.substr(pos + 1); + const auto to_str_lit = [&](std::index_sequence) { + return StringLiteral{substr[Ns]...}; + }; + return to_str_lit(std::make_index_sequence{}); +} + +} // namespace internal +} // namespace rfl + +#endif diff --git a/include/rfl/internal/tag_t.hpp b/include/rfl/internal/tag_t.hpp new file mode 100644 index 00000000..2b96ceb2 --- /dev/null +++ b/include/rfl/internal/tag_t.hpp @@ -0,0 +1,17 @@ +#ifndef RFL_INTERNAL_TAG_T_HPP_ +#define RFL_INTERNAL_TAG_T_HPP_ + +#include + +#include "rfl/internal/make_tag.hpp" + +namespace rfl { +namespace internal { + +template +using tag_t = typename std::invoke_result)>::type; + +} // namespace internal +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index 7157eef3..e1ca4130 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -38,14 +38,16 @@ #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/get_type_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/make_tag.hpp" #include "rfl/internal/move_from_tuple.hpp" #include "rfl/internal/no_duplicate_field_names.hpp" +#include "rfl/internal/tag_t.hpp" #include "rfl/internal/to_flattened_ptr_tuple.hpp" #include "rfl/internal/to_ptr_named_tuple.hpp" #include "rfl/internal/tuple_t.hpp" @@ -847,33 +849,13 @@ struct Parser> { template static inline bool contains_disc_value( const std::string& _disc_value) noexcept { - 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); - return LiteralType::contains(_disc_value); - } else { - return _disc_value == internal::get_struct_name(); - } - } - - template - 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>().str(); - } else { - return internal::get_struct_name(); - } + return internal::tag_t::contains(_disc_value); } /// Writes a wrapped version of the original object, which contains the tag. template static OutputVarType write_wrapped(const W& _w, const T& _val) noexcept { - const auto tag = make_tag(); + const auto tag = internal::make_tag(); using TagType = std::decay_t; if constexpr (internal::has_fields>()) { using WrapperType = diff --git a/include/rfl/parsing/TaggedUnionWrapper.hpp b/include/rfl/parsing/TaggedUnionWrapper.hpp index 064ef107..bc7c1ff4 100644 --- a/include/rfl/parsing/TaggedUnionWrapper.hpp +++ b/include/rfl/parsing/TaggedUnionWrapper.hpp @@ -10,15 +10,15 @@ namespace rfl { namespace parsing { -template +template struct TaggedUnionWrapperNoFields { - rfl::Rename<_discriminator, std::string> tag; + rfl::Rename<_discriminator, TagType> tag; rfl::Flatten fields; }; -template +template struct TaggedUnionWrapperWithFields { - rfl::Field<_discriminator, std::string> tag; + rfl::Field<_discriminator, TagType> tag; rfl::Flatten fields; }; diff --git a/include/rfl/type_name_t.hpp b/include/rfl/type_name_t.hpp new file mode 100644 index 00000000..57fd9bad --- /dev/null +++ b/include/rfl/type_name_t.hpp @@ -0,0 +1,17 @@ +#ifndef RFL_TYPE_NAME_T_HPP_ +#define RFL_TYPE_NAME_T_HPP_ + +#include + +#include "rfl/Literal.hpp" +#include "rfl/internal/get_type_name.hpp" + +namespace rfl { + +/// Returns a rfl::Literal containing the type name of T. +template +using type_name_t = rfl::Literal()>; + +} // namespace rfl + +#endif