mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-07 03:06:18 +08:00
Added more features to the automated field name retrieval
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include "rfl/Pattern.hpp"
|
||||
#include "rfl/PatternValidator.hpp"
|
||||
#include "rfl/Ref.hpp"
|
||||
#include "rfl/Rename.hpp"
|
||||
#include "rfl/Size.hpp"
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
#include "rfl/Timestamp.hpp"
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
#ifndef RFL_RENAME_HPP_
|
||||
#define RFL_RENAME_HPP_
|
||||
|
||||
#include <algorithm>
|
||||
#include <string_view>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "rfl/Literal.hpp"
|
||||
#include "rfl/default.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Used to define a field in the NamedTuple.
|
||||
template <internal::StringLiteral _name, class T>
|
||||
struct Rename {
|
||||
/// The underlying type.
|
||||
using Type = T;
|
||||
|
||||
/// The name of the field.
|
||||
using Name = rfl::Literal<_name>;
|
||||
|
||||
Rename() : value_(Type()) {}
|
||||
|
||||
Rename(const Type& _value) : value_(_value) {}
|
||||
|
||||
Rename(Type&& _value) noexcept : value_(std::move(_value)) {}
|
||||
|
||||
Rename(Rename<_name, T>&& _field) noexcept = default;
|
||||
|
||||
Rename(const Rename<_name, Type>& _field) = default;
|
||||
|
||||
template <class U>
|
||||
Rename(const Rename<_name, U>& _field) : value_(_field.get()) {}
|
||||
|
||||
template <class U>
|
||||
Rename(Rename<_name, U>&& _field) : value_(_field.get()) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
|
||||
bool>::type = true>
|
||||
Rename(const U& _value) : value_(_value) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
|
||||
bool>::type = true>
|
||||
Rename(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
|
||||
bool>::type = true>
|
||||
Rename(const Rename<_name, U>& _field) : value_(_field.value()) {}
|
||||
|
||||
/// Assigns the underlying object to its default value.
|
||||
template <class U = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<U>,
|
||||
bool>::type = true>
|
||||
Rename(const Default& _default) : value_(Type()) {}
|
||||
|
||||
~Rename() = default;
|
||||
|
||||
/// The name of the field, for internal use.
|
||||
constexpr static const internal::StringLiteral name_ = _name;
|
||||
|
||||
/// Returns the underlying object.
|
||||
const Type& get() const { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
Type& operator()() { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
const Type& operator()() const { return value_; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
auto& operator=(const Type& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
auto& operator=(Type&& _value) noexcept {
|
||||
value_ = std::move(_value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
|
||||
bool>::type = true>
|
||||
auto& operator=(const U& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object to its default value.
|
||||
template <class U = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<U>,
|
||||
bool>::type = true>
|
||||
auto& operator=(const Default& _default) {
|
||||
value_ = Type();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
Rename<_name, T>& operator=(const Rename<_name, T>& _field) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
Rename<_name, T>& operator=(Rename<_name, T>&& _field) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U>
|
||||
auto& operator=(const Rename<_name, U>& _field) {
|
||||
value_ = _field.get();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U>
|
||||
auto& operator=(Rename<_name, U>&& _field) {
|
||||
value_ = std::forward<T>(_field.value_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
void set(const Type& _value) { value_ = _value; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
void set(Type&& _value) { value_ = std::move(_value); }
|
||||
|
||||
/// Returns the underlying object.
|
||||
Type& value() { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
const Type& value() const { return value_; }
|
||||
|
||||
/// The underlying value.
|
||||
Type value_;
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef RFL_INTERNAL_FIELD_TUPLE_T_HPP_
|
||||
#define RFL_INTERNAL_FIELD_TUPLE_T_HPP_
|
||||
|
||||
#include <functional>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/internal/copy_to_field_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
using field_tuple_t =
|
||||
typename std::invoke_result<decltype(copy_to_field_tuple<T>), T>::type;
|
||||
|
||||
}
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef RFL_INTERNAL_FIELDS_HPP_
|
||||
#define RFL_INTERNAL_FIELDS_HPP_
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <int N>
|
||||
struct Fields {
|
||||
std::array<std::string, N> names_;
|
||||
|
||||
std::unordered_map<std::string_view, std::int16_t> indices_;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
+2685
-2657
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,35 @@
|
||||
#ifndef RFL_INTERNAL_ISRENAME_HPP_
|
||||
#define RFL_INTERNAL_ISRENAME_HPP_
|
||||
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "rfl/Rename.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
class is_rename;
|
||||
|
||||
template <class T>
|
||||
class is_rename : public std::false_type {};
|
||||
|
||||
template <StringLiteral _name, class Type>
|
||||
class is_rename<Rename<_name, Type>> : public std::true_type {};
|
||||
|
||||
template <StringLiteral _name, class Type>
|
||||
class is_rename<Rename<_name, Type>*> : public std::true_type {};
|
||||
|
||||
template <StringLiteral _name, class Type>
|
||||
class is_rename<const Rename<_name, Type>*> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_rename_v = is_rename<T>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
+2287
-2365
File diff suppressed because it is too large
Load Diff
@@ -16,47 +16,49 @@ namespace internal {
|
||||
|
||||
template <class PtrFieldTuple, class... Args>
|
||||
auto flatten_ptr_field_tuple(const PtrFieldTuple& _t, Args&&... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<PtrFieldTuple>>) {
|
||||
return std::tuple_cat(std::forward<Args>(_args)...);
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<PtrFieldTuple>>) {
|
||||
return std::tuple_cat(std::forward<Args>(_args)...);
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, std::decay_t<PtrFieldTuple>>;
|
||||
if constexpr (internal::is_flatten_field<T>::value) {
|
||||
const auto subtuple =
|
||||
internal::to_ptr_field_tuple(*std::get<i>(_t).get());
|
||||
return flatten_ptr_field_tuple(_t, std::forward<Args>(_args)...,
|
||||
flatten_ptr_field_tuple(subtuple));
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, std::decay_t<PtrFieldTuple>>;
|
||||
if constexpr (internal::is_flatten_field<T>::value) {
|
||||
const auto subtuple =
|
||||
internal::to_ptr_field_tuple(*std::get<i>(_t).get());
|
||||
return flatten_ptr_field_tuple(_t, std::forward<Args>(_args)...,
|
||||
flatten_ptr_field_tuple(subtuple));
|
||||
} else {
|
||||
return flatten_ptr_field_tuple(_t, std::forward<Args>(_args)...,
|
||||
std::make_tuple(std::get<i>(_t)));
|
||||
}
|
||||
return flatten_ptr_field_tuple(_t, std::forward<Args>(_args)...,
|
||||
std::make_tuple(std::get<i>(_t)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <class PtrFieldTuple>
|
||||
auto field_tuple_to_named_tuple(const PtrFieldTuple& _ptr_field_tuple) {
|
||||
const auto ft_to_nt = []<class... Fields>(const Fields&... _fields) {
|
||||
return make_named_tuple(_fields...);
|
||||
};
|
||||
const auto ft_to_nt = []<class... Fields>(const Fields&... _fields) {
|
||||
return make_named_tuple(_fields...);
|
||||
};
|
||||
|
||||
if constexpr (!has_flatten_fields<std::decay_t<PtrFieldTuple>>()) {
|
||||
return std::apply(ft_to_nt, std::move(_ptr_field_tuple));
|
||||
} else {
|
||||
const auto flattened_tuple = flatten_ptr_field_tuple(_ptr_field_tuple);
|
||||
return std::apply(ft_to_nt, flattened_tuple);
|
||||
}
|
||||
if constexpr (!has_flatten_fields<std::decay_t<PtrFieldTuple>>()) {
|
||||
return std::apply(ft_to_nt, std::move(_ptr_field_tuple));
|
||||
} else {
|
||||
const auto flattened_tuple = flatten_ptr_field_tuple(_ptr_field_tuple);
|
||||
return std::apply(ft_to_nt, flattened_tuple);
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates a named tuple that contains pointers to the original values in
|
||||
/// the struct.
|
||||
template <class T>
|
||||
auto to_ptr_named_tuple(const T& _t) {
|
||||
if constexpr (is_named_tuple_v<std::decay_t<T>>) {
|
||||
return nt_to_ptr_named_tuple(_t);
|
||||
} else {
|
||||
const auto ptr_field_tuple = to_ptr_field_tuple(_t);
|
||||
return field_tuple_to_named_tuple(ptr_field_tuple);
|
||||
}
|
||||
if constexpr (std::is_pointer_v<std::decay_t<T>>) {
|
||||
return to_ptr_named_tuple(*_t);
|
||||
} else if constexpr (is_named_tuple_v<std::decay_t<T>>) {
|
||||
return nt_to_ptr_named_tuple(_t);
|
||||
} else {
|
||||
const auto ptr_field_tuple = to_ptr_field_tuple(_t);
|
||||
return field_tuple_to_named_tuple(ptr_field_tuple);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
@@ -38,7 +38,9 @@ with open("generated_code4.cpp", "w", encoding="utf-8") as codefile:
|
||||
|
||||
template <class T>
|
||||
auto to_ptr_tuple(const T& _t) {
|
||||
if constexpr (has_n_fields<T, 0>) {
|
||||
if constexpr (std::is_pointer_v<std::decay_t<T>>) {
|
||||
return to_ptr_tuple(*_t);
|
||||
} else if constexpr (has_n_fields<T, 0>) {
|
||||
return std::make_tuple();
|
||||
} else if constexpr (has_n_fields<T, 1>) {
|
||||
auto& [f1] = _t;
|
||||
|
||||
+55
-56
@@ -19,77 +19,76 @@ namespace rfl {
|
||||
namespace json {
|
||||
|
||||
struct Writer {
|
||||
struct YYJSONOutputArray {
|
||||
YYJSONOutputArray(yyjson_mut_val* _val) : val_(_val) {}
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
struct YYJSONOutputArray {
|
||||
YYJSONOutputArray(yyjson_mut_val* _val) : val_(_val) {}
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
|
||||
struct YYJSONOutputObject {
|
||||
YYJSONOutputObject(yyjson_mut_val* _val) : val_(_val) {}
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
struct YYJSONOutputObject {
|
||||
YYJSONOutputObject(yyjson_mut_val* _val) : val_(_val) {}
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
|
||||
struct YYJSONOutputVar {
|
||||
YYJSONOutputVar(yyjson_mut_val* _val) : val_(_val) {}
|
||||
struct YYJSONOutputVar {
|
||||
YYJSONOutputVar(yyjson_mut_val* _val) : val_(_val) {}
|
||||
|
||||
YYJSONOutputVar(YYJSONOutputArray _arr) : val_(_arr.val_) {}
|
||||
YYJSONOutputVar(YYJSONOutputArray _arr) : val_(_arr.val_) {}
|
||||
|
||||
YYJSONOutputVar(YYJSONOutputObject _obj) : val_(_obj.val_) {}
|
||||
YYJSONOutputVar(YYJSONOutputObject _obj) : val_(_obj.val_) {}
|
||||
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
|
||||
using OutputArrayType = YYJSONOutputArray;
|
||||
using OutputObjectType = YYJSONOutputObject;
|
||||
using OutputVarType = YYJSONOutputVar;
|
||||
using OutputArrayType = YYJSONOutputArray;
|
||||
using OutputObjectType = YYJSONOutputObject;
|
||||
using OutputVarType = YYJSONOutputVar;
|
||||
|
||||
Writer(yyjson_mut_doc* _doc) : doc_(_doc) {}
|
||||
Writer(yyjson_mut_doc* _doc) : doc_(_doc) {}
|
||||
|
||||
~Writer() = default;
|
||||
~Writer() = default;
|
||||
|
||||
void add(const OutputVarType _var, OutputArrayType* _arr) const noexcept {
|
||||
yyjson_mut_arr_add_val(_arr->val_, _var.val_);
|
||||
void add(const OutputVarType _var, OutputArrayType* _arr) const noexcept {
|
||||
yyjson_mut_arr_add_val(_arr->val_, _var.val_);
|
||||
}
|
||||
|
||||
OutputVarType empty_var() const noexcept {
|
||||
return OutputVarType(yyjson_mut_null(doc_));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType from_basic_type(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
return OutputVarType(yyjson_mut_strcpy(doc_, _var.c_str()));
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
return OutputVarType(yyjson_mut_bool(doc_, _var));
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
return OutputVarType(yyjson_mut_real(doc_, static_cast<double>(_var)));
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
return OutputVarType(yyjson_mut_int(doc_, static_cast<int>(_var)));
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
|
||||
OutputVarType empty_var() const noexcept {
|
||||
return OutputVarType(yyjson_mut_null(doc_));
|
||||
}
|
||||
OutputArrayType new_array() const noexcept {
|
||||
return OutputArrayType(yyjson_mut_arr(doc_));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType from_basic_type(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
return OutputVarType(yyjson_mut_strcpy(doc_, _var.c_str()));
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
return OutputVarType(yyjson_mut_bool(doc_, _var));
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
return OutputVarType(
|
||||
yyjson_mut_real(doc_, static_cast<double>(_var)));
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
return OutputVarType(yyjson_mut_int(doc_, static_cast<int>(_var)));
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
OutputObjectType new_object() const noexcept {
|
||||
return OutputObjectType(yyjson_mut_obj(doc_));
|
||||
}
|
||||
|
||||
OutputArrayType new_array() const noexcept {
|
||||
return OutputArrayType(yyjson_mut_arr(doc_));
|
||||
}
|
||||
bool is_empty(const OutputVarType& _var) const noexcept {
|
||||
return yyjson_mut_is_null(_var.val_);
|
||||
}
|
||||
|
||||
OutputObjectType new_object() const noexcept {
|
||||
return OutputObjectType(yyjson_mut_obj(doc_));
|
||||
}
|
||||
void set_field(const std::string& _name, const OutputVarType& _var,
|
||||
OutputObjectType* _obj) const noexcept {
|
||||
yyjson_mut_obj_add(_obj->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
|
||||
_var.val_);
|
||||
}
|
||||
|
||||
bool is_empty(const OutputVarType& _var) const noexcept {
|
||||
return yyjson_mut_is_null(_var.val_);
|
||||
}
|
||||
|
||||
void set_field(const std::string& _name, const OutputVarType& _var,
|
||||
OutputObjectType* _obj) const noexcept {
|
||||
yyjson_mut_obj_add(_obj->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
|
||||
_var.val_);
|
||||
}
|
||||
|
||||
yyjson_mut_doc* doc_;
|
||||
yyjson_mut_doc* doc_;
|
||||
};
|
||||
|
||||
} // namespace json
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef RFL_PARSING_PARSER_HPP_
|
||||
#define RFL_PARSING_PARSER_HPP_
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
@@ -29,10 +31,13 @@
|
||||
#include "rfl/always_false.hpp"
|
||||
#include "rfl/field_type.hpp"
|
||||
#include "rfl/from_named_tuple.hpp"
|
||||
#include "rfl/internal/Fields.hpp"
|
||||
#include "rfl/internal/Memoization.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
#include "rfl/internal/all_fields.hpp"
|
||||
#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/has_fields.hpp"
|
||||
#include "rfl/internal/has_reflection_method_v.hpp"
|
||||
#include "rfl/internal/has_reflection_type_v.hpp"
|
||||
@@ -44,6 +49,7 @@
|
||||
#include "rfl/internal/tuple_t.hpp"
|
||||
#include "rfl/named_tuple_t.hpp"
|
||||
#include "rfl/parsing/AreReaderAndWriter.hpp"
|
||||
#include "rfl/parsing/TaggedUnionWrapper.hpp"
|
||||
#include "rfl/parsing/is_forward_list.hpp"
|
||||
#include "rfl/parsing/is_map_like.hpp"
|
||||
#include "rfl/parsing/is_map_like_not_multimap.hpp"
|
||||
@@ -71,7 +77,7 @@ struct StructParser {
|
||||
using OutputObjectType = typename W::OutputObjectType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using PtrTupleType = internal::ptr_tuple_t<T>;
|
||||
using PtrTupleType = internal::flattened_ptr_tuple_t<T>;
|
||||
using TupleType = internal::flattened_tuple_t<T>;
|
||||
|
||||
static constexpr auto tuple_size = std::tuple_size_v<TupleType>;
|
||||
@@ -90,7 +96,7 @@ struct StructParser {
|
||||
return _r.template to_fields_array<tuple_size>(fct, _obj);
|
||||
};
|
||||
|
||||
const auto build_tuple = [&](auto _fields_vec) -> TupleType {
|
||||
const auto build_tuple = [&](auto _fields_vec) -> Result<TupleType> {
|
||||
return build_tuple_recursively(_r, _fields_vec);
|
||||
};
|
||||
|
||||
@@ -218,7 +224,7 @@ struct StructParser {
|
||||
using ValueType =
|
||||
std::decay_t<typename std::tuple_element<_i, PtrTupleType>::type>;
|
||||
|
||||
auto value = Parser<R, W, ValueType>::write(_w, rfl::get<_i>(_tup));
|
||||
auto value = Parser<R, W, ValueType>::write(_w, std::get<_i>(_tup));
|
||||
|
||||
const auto& key = std::get<_i>(field_names());
|
||||
|
||||
@@ -238,16 +244,14 @@ struct StructParser {
|
||||
/// There are some objects that we are likely to parse many times,
|
||||
/// so we only calculate these indices once.
|
||||
static const auto& field_indices() noexcept {
|
||||
// return field_indices_.value(set_field_indices<0>);
|
||||
return field_indices_;
|
||||
return fields_.value(make_fields).indices_;
|
||||
}
|
||||
|
||||
/// Uses a memoization pattern to retrieve the field names.
|
||||
/// There are some objects that we are likely to parse many times,
|
||||
/// so we only calculate these indices once.
|
||||
static const auto& field_names() noexcept {
|
||||
// return field_indices_.value(set_field_indices<0>);
|
||||
return field_names_;
|
||||
return fields_.value(make_fields).names_;
|
||||
}
|
||||
|
||||
/// Retrieves the value from the object. This is mainly needed to
|
||||
@@ -262,31 +266,17 @@ struct StructParser {
|
||||
}
|
||||
|
||||
/// Builds the object field by field.
|
||||
/*template <size_t _i = 0>
|
||||
static void set_field_indices(
|
||||
std::unordered_map<std::string_view, std::int16_t>*
|
||||
_field_indices) noexcept {
|
||||
if constexpr (_i >= sizeof...(FieldTypes)) {
|
||||
return;
|
||||
} else {
|
||||
using FieldType =
|
||||
typename std::tuple_element<_i, std::tuple<FieldTypes...>>::type;
|
||||
const auto name = FieldType::name_.string_view();
|
||||
(*_field_indices)[name] = static_cast<std::int16_t>(_i);
|
||||
set_field_indices<_i + 1>(_field_indices);
|
||||
static void make_fields(internal::Fields<tuple_size>* _fields) noexcept {
|
||||
_fields->names_ = internal::get_field_names<T>();
|
||||
for (size_t i = 0; i < tuple_size; ++i) {
|
||||
_fields->indices_[_fields->names_[i]] = static_cast<std::int16_t>(i);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
private:
|
||||
/// Maps each of the field names to an index signifying their order.
|
||||
/*static inline internal::Memoization<
|
||||
std::unordered_map<std::string_view, std::int16_t>>
|
||||
field_indices_;*/
|
||||
|
||||
static inline std::array<std::string, tuple_size> field_names_;
|
||||
|
||||
static inline std::unordered_map<std::string_view, std::int16_t>
|
||||
field_indices_;
|
||||
/// Maps each of the field names to an index signifying their order and
|
||||
/// vice-versa.
|
||||
static inline internal::Memoization<internal::Fields<tuple_size>> fields_;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -326,11 +316,7 @@ struct Parser {
|
||||
return Parser<R, W, NamedTupleType>::read(_r, _var).and_then(
|
||||
to_struct);
|
||||
} else {
|
||||
using TupleType = internal::flattened_tuple_t<T>;
|
||||
const auto to_struct = [](TupleType&& _t) -> T {
|
||||
return internal::move_from_tuple<T, TupleType>(std::move(_t));
|
||||
};
|
||||
return Parser<R, W, TupleType>::read(_r, _var).transform(to_struct);
|
||||
return StructParser<R, W, T>::read(_r, _var);
|
||||
}
|
||||
} else if constexpr (internal::is_basic_type_v<T>) {
|
||||
return _r.template to_basic_type<std::decay_t<T>>(_var);
|
||||
@@ -360,9 +346,7 @@ struct Parser {
|
||||
using PtrNamedTupleType = std::decay_t<decltype(ptr_named_tuple)>;
|
||||
return Parser<R, W, PtrNamedTupleType>::write(_w, ptr_named_tuple);
|
||||
} else {
|
||||
const auto ptr_tuple = internal::to_flattened_ptr_tuple(_var);
|
||||
using PtrTupleType = std::decay_t<decltype(ptr_tuple)>;
|
||||
return Parser<R, W, PtrTupleType>::write(_w, ptr_tuple);
|
||||
return StructParser<R, W, T>::write(_w, _var);
|
||||
}
|
||||
} else if constexpr (internal::is_basic_type_v<T>) {
|
||||
return _w.from_basic_type(_var);
|
||||
@@ -862,6 +846,28 @@ struct Parser<R, W, Ref<T>> {
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class R, class W, class T, internal::StringLiteral _name>
|
||||
requires AreReaderAndWriter<R, W, Rename<_name, T>>
|
||||
struct Parser<R, W, Rename<_name, T>> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
static Result<Rename<_name, T>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
const auto to_rename = [](auto&& _t) {
|
||||
return Rename<_name, T>(std::move(_t));
|
||||
};
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_rename);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w,
|
||||
const Rename<_name, T>& _rename) noexcept {
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, _rename.value());
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class R, class W, class T>
|
||||
requires AreReaderAndWriter<R, W, Result<T>>
|
||||
struct Parser<R, W, Result<T>> {
|
||||
@@ -997,9 +1003,10 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
static OutputVarType write(
|
||||
const W& _w, const TaggedUnion<_discriminator, AlternativeTypes...>&
|
||||
_tagged_union) noexcept {
|
||||
using VariantType =
|
||||
typename TaggedUnion<_discriminator, AlternativeTypes...>::VariantType;
|
||||
return Parser<R, W, VariantType>::write(_w, _tagged_union.variant_);
|
||||
const auto handle = [&](const auto& _val) -> OutputVarType {
|
||||
return write_wrapped(_w, _val);
|
||||
};
|
||||
return std::visit(handle, _tagged_union.variant_);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -1061,14 +1068,42 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
static inline bool contains_disc_value(
|
||||
const std::string& _disc_value) noexcept {
|
||||
if constexpr (!internal::has_reflection_type_v<T>) {
|
||||
using LiteralType = field_type_t<_discriminator, T>;
|
||||
using LiteralType = typename T::Tag;
|
||||
return LiteralType::contains(_disc_value);
|
||||
} else {
|
||||
using LiteralType =
|
||||
field_type_t<_discriminator, typename T::ReflectionType>;
|
||||
using LiteralType = typename T::ReflectionType::Tag;
|
||||
return LiteralType::contains(_disc_value);
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static inline auto make_tag() noexcept {
|
||||
if constexpr (!internal::has_reflection_type_v<T>) {
|
||||
using LiteralType = typename T::Tag;
|
||||
return LiteralType::template name_of<0>();
|
||||
} else {
|
||||
using LiteralType = typename T::ReflectionType::Tag;
|
||||
return LiteralType::template name_of<0>();
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes a wrapped version of the original object, which contains the tag.
|
||||
template <class T>
|
||||
static OutputVarType write_wrapped(const W& _w, const T& _val) noexcept {
|
||||
const auto tag = make_tag<T>();
|
||||
using TagType = std::decay_t<decltype(tag)>;
|
||||
if constexpr (internal::has_fields<std::decay_t<T>>()) {
|
||||
using WrapperType =
|
||||
TaggedUnionWrapperWithFields<T, TagType, _discriminator>;
|
||||
const auto wrapper = WrapperType{.tag = tag, .fields = &_val};
|
||||
return Parser<R, W, WrapperType>::write(_w, wrapper);
|
||||
} else {
|
||||
using WrapperType =
|
||||
TaggedUnionWrapperNoFields<T, TagType, _discriminator>;
|
||||
const auto wrapper = WrapperType{.tag = tag, .fields = &_val};
|
||||
return Parser<R, W, WrapperType>::write(_w, wrapper);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef RFL_PARSING_TAGGEDUNIONWRAPPER_HPP_
|
||||
#define RFL_PARSING_TAGGEDUNIONWRAPPER_HPP_
|
||||
|
||||
#include <concepts>
|
||||
|
||||
#include "rfl/Flatten.hpp"
|
||||
#include "rfl/Rename.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class T, class LiteralType, internal::StringLiteral _discriminator>
|
||||
struct TaggedUnionWrapperNoFields {
|
||||
rfl::Rename<_discriminator, LiteralType> tag;
|
||||
rfl::Flatten<const T*> fields;
|
||||
};
|
||||
|
||||
template <class T, class LiteralType, internal::StringLiteral _discriminator>
|
||||
struct TaggedUnionWrapperWithFields {
|
||||
rfl::Field<_discriminator, LiteralType> tag;
|
||||
rfl::Flatten<const T*> fields;
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user