Made sure that attributes are written properly

This commit is contained in:
Patrick Urbanke
2023-12-21 22:43:32 +01:00
parent 6c3e18afa8
commit be7b1932a5
3 changed files with 45 additions and 9 deletions
+1 -1
View File
@@ -26,7 +26,7 @@ template <class Type>
class is_attribute<const Attribute<Type>*> : public std::true_type {};
template <class T>
constexpr bool is_attribute_v = is_attribute<T>::value;
constexpr bool is_attribute_v = is_attribute<std::decay_t<T>>::value;
} // namespace internal
} // namespace rfl
+22 -8
View File
@@ -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<T>) {
using ReflectionType = std::decay_t<typename T::ReflectionType>;
if constexpr (internal::has_reflection_method_v<T>) {
return Parser<R, W, ReflectionType>::write(_w, _var.reflection());
} else {
const auto& [r] = _var;
return Parser<R, W, ReflectionType>::write(_w, r);
}
if constexpr (supports_attributes<W> && internal::is_attribute_v<T>) {
const auto r = resolve_reflection_type(_var);
return _w.from_basic_type(r, true);
} else if constexpr (internal::has_reflection_type_v<T>) {
const auto r = resolve_reflection_type(_var);
return Parser<R, W, std::decay_t<decltype(r)>>::write(_w, r);
} else if constexpr (std::is_class_v<T> && std::is_aggregate_v<T>) {
const auto ptr_named_tuple = internal::to_ptr_named_tuple(_var);
using PtrNamedTupleType = std::decay_t<decltype(ptr_named_tuple)>;
@@ -93,6 +93,20 @@ struct Parser {
"support for your own classes.");
}
}
template <class V>
static const auto resolve_reflection_type(const V& _var) {
if constexpr (internal::has_reflection_type_v<V>) {
if constexpr (internal::has_reflection_method_v<V>) {
return resolve_reflection_type(_var.reflection());
} else {
const auto& [r] = _var;
return resolve_reflection_type(r);
}
} else {
return _var;
}
}
};
} // namespace parsing
@@ -0,0 +1,22 @@
#ifndef RFL_PARSING_SUPPORTSATTRIBUTES_HPP_
#define RFL_PARSING_SUPPORTSATTRIBUTES_HPP_
#include <concepts>
#include "rfl/Result.hpp"
namespace rfl {
namespace parsing {
/// Determines whether a writer supports attributes.
template <class W>
concept supports_attributes = requires(W w, int val, bool _is_attribute) {
{
w.from_basic_type(val, _is_attribute)
} -> std::same_as<typename W::OutputVarType>;
};
} // namespace parsing
} // namespace rfl
#endif