From be7b1932a5d9a5ed0f8f55fac7c05990d5c745a4 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Thu, 21 Dec 2023 22:43:32 +0100 Subject: [PATCH] Made sure that attributes are written properly --- include/rfl/internal/is_attribute.hpp | 2 +- include/rfl/parsing/Parser_default.hpp | 30 +++++++++++++++------ include/rfl/parsing/supports_attributes.hpp | 22 +++++++++++++++ 3 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 include/rfl/parsing/supports_attributes.hpp diff --git a/include/rfl/internal/is_attribute.hpp b/include/rfl/internal/is_attribute.hpp index 043aaa5d..d048f9fb 100644 --- a/include/rfl/internal/is_attribute.hpp +++ b/include/rfl/internal/is_attribute.hpp @@ -26,7 +26,7 @@ template class is_attribute*> : public std::true_type {}; template -constexpr bool is_attribute_v = is_attribute::value; +constexpr bool is_attribute_v = is_attribute>::value; } // namespace internal } // namespace rfl diff --git a/include/rfl/parsing/Parser_default.hpp b/include/rfl/parsing/Parser_default.hpp index 341b581f..268ba979 100644 --- a/include/rfl/parsing/Parser_default.hpp +++ b/include/rfl/parsing/Parser_default.hpp @@ -10,10 +10,12 @@ #include "rfl/internal/enums/StringConverter.hpp" #include "rfl/internal/has_reflection_method_v.hpp" #include "rfl/internal/has_reflection_type_v.hpp" +#include "rfl/internal/is_attribute.hpp" #include "rfl/internal/is_basic_type.hpp" #include "rfl/internal/to_ptr_named_tuple.hpp" #include "rfl/parsing/AreReaderAndWriter.hpp" #include "rfl/parsing/Parser_base.hpp" +#include "rfl/parsing/supports_attributes.hpp" namespace rfl { namespace parsing { @@ -68,14 +70,12 @@ struct Parser { /// Converts the variable to a JSON type. static auto write(const W& _w, const T& _var) noexcept { - if constexpr (internal::has_reflection_type_v) { - using ReflectionType = std::decay_t; - if constexpr (internal::has_reflection_method_v) { - return Parser::write(_w, _var.reflection()); - } else { - const auto& [r] = _var; - return Parser::write(_w, r); - } + if constexpr (supports_attributes && internal::is_attribute_v) { + const auto r = resolve_reflection_type(_var); + return _w.from_basic_type(r, true); + } else if constexpr (internal::has_reflection_type_v) { + const auto r = resolve_reflection_type(_var); + return Parser>::write(_w, r); } else if constexpr (std::is_class_v && std::is_aggregate_v) { const auto ptr_named_tuple = internal::to_ptr_named_tuple(_var); using PtrNamedTupleType = std::decay_t; @@ -93,6 +93,20 @@ struct Parser { "support for your own classes."); } } + + template + static const auto resolve_reflection_type(const V& _var) { + if constexpr (internal::has_reflection_type_v) { + if constexpr (internal::has_reflection_method_v) { + return resolve_reflection_type(_var.reflection()); + } else { + const auto& [r] = _var; + return resolve_reflection_type(r); + } + } else { + return _var; + } + } }; } // namespace parsing diff --git a/include/rfl/parsing/supports_attributes.hpp b/include/rfl/parsing/supports_attributes.hpp new file mode 100644 index 00000000..68671128 --- /dev/null +++ b/include/rfl/parsing/supports_attributes.hpp @@ -0,0 +1,22 @@ +#ifndef RFL_PARSING_SUPPORTSATTRIBUTES_HPP_ +#define RFL_PARSING_SUPPORTSATTRIBUTES_HPP_ + +#include + +#include "rfl/Result.hpp" + +namespace rfl { +namespace parsing { + +/// Determines whether a writer supports attributes. +template +concept supports_attributes = requires(W w, int val, bool _is_attribute) { + { + w.from_basic_type(val, _is_attribute) + } -> std::same_as; +}; + +} // namespace parsing +} // namespace rfl + +#endif