mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 10:46:18 +08:00
Made sure we support zero-copy replace
This commit is contained in:
@@ -26,7 +26,7 @@ struct Field {
|
||||
|
||||
Field(Type&& _value) noexcept : value_(std::move(_value)) {}
|
||||
|
||||
Field(Field<_name, Type>&& _field) noexcept
|
||||
Field(Field<_name, _Type>&& _field) noexcept
|
||||
: value_(std::move(_field.value_)) {}
|
||||
|
||||
Field(const Field<_name, Type>& _field) : value_(_field.get()) {}
|
||||
@@ -98,7 +98,7 @@ struct Field {
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline void operator=(Field<_name, Type>&& _field) {
|
||||
inline void operator=(Field<_name, Type>&& _field) noexcept {
|
||||
value_ = _field.get();
|
||||
}
|
||||
|
||||
|
||||
+52
-15
@@ -25,6 +25,21 @@ class NamedTuple {
|
||||
using Values = std::tuple<typename std::decay<FieldTypes>::type::Type...>;
|
||||
|
||||
public:
|
||||
/// Construct from the values.
|
||||
NamedTuple(typename std::decay<FieldTypes>::type::Type&&... _values)
|
||||
: values_(std::forward<typename std::decay<FieldTypes>::type::Type>(
|
||||
_values)...) {
|
||||
static_assert(no_duplicate_field_names(),
|
||||
"Duplicate field names are not allowed");
|
||||
}
|
||||
|
||||
/// Construct from the values.
|
||||
NamedTuple(const typename std::decay<FieldTypes>::type::Type&... _values)
|
||||
: values_(std::make_tuple(_values...)) {
|
||||
static_assert(no_duplicate_field_names(),
|
||||
"Duplicate field names are not allowed");
|
||||
}
|
||||
|
||||
/// Construct from the fields.
|
||||
NamedTuple(FieldTypes&&... _fields)
|
||||
: values_(std::make_tuple(std::move(_fields.value_)...)) {
|
||||
@@ -201,6 +216,20 @@ class NamedTuple {
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Replaces one or several fields, returning a new version
|
||||
/// with the non-replaced fields left unchanged.
|
||||
template <class RField, class... OtherRFields>
|
||||
NamedTuple<FieldTypes...> replace(RField&& _field,
|
||||
OtherRFields&&... _other_fields) {
|
||||
constexpr auto num_other_fields = sizeof...(OtherRFields);
|
||||
if constexpr (num_other_fields == 0) {
|
||||
return replace_value<RField>(_field.value_);
|
||||
} else {
|
||||
return replace_value<RField>(_field.value_)
|
||||
.replace(std::forward<OtherRFields>(_other_fields)...);
|
||||
}
|
||||
}
|
||||
|
||||
/// Replaces one or several fields, returning a new version
|
||||
/// with the non-replaced fields left unchanged.
|
||||
template <class RField, class... OtherRFields>
|
||||
@@ -300,8 +329,8 @@ class NamedTuple {
|
||||
}
|
||||
|
||||
/// Generates a new named tuple with one value replaced with a new value.
|
||||
template <int _index, class T, class... Args>
|
||||
auto make_replaced(T&& _val, Args&&... _args) const {
|
||||
template <int _index, class V, class T, class... Args>
|
||||
auto make_replaced(V&& _values, T&& _val, Args&&... _args) const {
|
||||
constexpr auto size = sizeof...(Args);
|
||||
|
||||
constexpr bool retrieved_all_fields = size == std::tuple_size<Fields>{};
|
||||
@@ -312,13 +341,16 @@ class NamedTuple {
|
||||
using FieldType = typename std::tuple_element<size, Fields>::type;
|
||||
|
||||
if constexpr (size == _index) {
|
||||
return make_replaced<_index, T>(std::forward<T>(_val),
|
||||
std::forward<Args>(_args)...,
|
||||
FieldType(_val));
|
||||
return make_replaced<_index, V, T>(
|
||||
std::forward<V>(_values), std::forward<T>(_val),
|
||||
std::forward<Args>(_args)...,
|
||||
FieldType(std::forward<T>(_val)));
|
||||
} else {
|
||||
return make_replaced<_index, T>(
|
||||
std::forward<T>(_val), std::forward<Args>(_args)...,
|
||||
FieldType(std::get<size>(values_)));
|
||||
using U = typename FieldType::Type;
|
||||
return make_replaced<_index, V, T>(
|
||||
std::forward<V>(_values), std::forward<T>(_val),
|
||||
std::forward<Args>(_args)...,
|
||||
FieldType(std::forward<U>(std::get<size>(_values))));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -328,18 +360,23 @@ class NamedTuple {
|
||||
return internal::no_duplicate_field_names<Fields>();
|
||||
}
|
||||
|
||||
/// Replaced the field signified by the field type.
|
||||
template <class Field, class T>
|
||||
NamedTuple<FieldTypes...> replace_value(T&& _val) {
|
||||
using FieldType = std::decay_t<Field>;
|
||||
constexpr auto index = internal::find_index<FieldType::name_, Fields>();
|
||||
return make_replaced<index, Values, T>(std::forward<Values>(values_),
|
||||
std::forward<T>(_val));
|
||||
}
|
||||
|
||||
/// Replaced the field signified by the field type.
|
||||
template <class Field, class T>
|
||||
NamedTuple<FieldTypes...> replace_value(T&& _val) const {
|
||||
using FieldType = std::decay_t<Field>;
|
||||
constexpr auto index = internal::find_index<FieldType::name_, Fields>();
|
||||
static_assert(
|
||||
std::is_same<typename std::tuple_element<index, Fields>::type::Type,
|
||||
typename FieldType::Type>(),
|
||||
"If two fields have the same name, "
|
||||
"their type must be the same as "
|
||||
"well.");
|
||||
return make_replaced<index, T>(_val);
|
||||
auto values = values_;
|
||||
return make_replaced<index, Values, T>(std::move(values),
|
||||
std::forward<T>(_val));
|
||||
}
|
||||
|
||||
/// Adds the elements of a tuple to a newly created named tuple,
|
||||
|
||||
@@ -1,95 +1,27 @@
|
||||
#ifndef RFL_FROM_NAMED_TUPLE_HPP_
|
||||
#define RFL_FROM_NAMED_TUPLE_HPP_
|
||||
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/internal/has_flatten_fields.hpp"
|
||||
#include "rfl/internal/is_named_tuple.hpp"
|
||||
#include "rfl/internal/ptr_field_tuple_t.hpp"
|
||||
#include "rfl/named_tuple_t.hpp"
|
||||
#include "internal/copy_from_named_tuple.hpp"
|
||||
#include "internal/move_from_named_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <class FieldTupleType, class NamedTupleType, class... Args>
|
||||
FieldTupleType concat_named_tuple(NamedTupleType&& _n, Args&&... _args);
|
||||
|
||||
/// Creates a struct of type T from a named tuple.
|
||||
/// All fields of the struct must be an rfl::Field.
|
||||
/// Generates the struct T from a named tuple.
|
||||
template <class T, class NamedTupleType>
|
||||
T from_named_tuple(NamedTupleType&& _n) {
|
||||
using RequiredType = std::decay_t<named_tuple_t<T>>;
|
||||
|
||||
if constexpr (internal::is_named_tuple_v<std::decay_t<T>>) {
|
||||
return std::forward<NamedTupleType>(_n);
|
||||
} else if constexpr (std::is_same<std::decay_t<NamedTupleType>,
|
||||
RequiredType>()) {
|
||||
const auto make = []<class... ValueTypes>(ValueTypes&&... _values) {
|
||||
return T{std::forward<ValueTypes>(_values)...};
|
||||
};
|
||||
|
||||
using PtrFieldTupleType = internal::ptr_field_tuple_t<std::decay_t<T>>;
|
||||
|
||||
if constexpr (!internal::has_flatten_fields<PtrFieldTupleType>()) {
|
||||
return std::apply(
|
||||
make,
|
||||
std::forward<typename NamedTupleType::Values>(_n.values()));
|
||||
} else {
|
||||
auto fields = concat_named_tuple<PtrFieldTupleType>(_n);
|
||||
using FieldsType = std::decay_t<decltype(fields)>;
|
||||
return std::apply(make, std::forward<FieldsType>(fields));
|
||||
}
|
||||
|
||||
if constexpr (std::is_lvalue_reference<NamedTupleType>{}) {
|
||||
return internal::copy_from_named_tuple<T>(_n);
|
||||
} else {
|
||||
return from_named_tuple<T, RequiredType>(
|
||||
RequiredType(std::forward<NamedTupleType>(_n)));
|
||||
return internal::move_from_named_tuple<T>(_n);
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a struct of type T from a named tuple.
|
||||
/// All fields of the struct must be an rfl::Field.
|
||||
/*template <class T, class NamedTupleType>
|
||||
/// Generates the struct T from a named tuple.
|
||||
template <class T, class NamedTupleType>
|
||||
T from_named_tuple(const NamedTupleType& _n) {
|
||||
using RequiredType = std::decay_t<named_tuple_t<T>>;
|
||||
if constexpr (internal::is_named_tuple_v<std::decay_t<T>>) {
|
||||
return _n;
|
||||
|
||||
} else if constexpr (std::is_same<std::decay_t<NamedTupleType>,
|
||||
RequiredType>()) {
|
||||
const auto make = [](const auto&... _fields) { return T{_fields...};
|
||||
};
|
||||
|
||||
using FieldTupleType = internal::field_tuple_t<T>;
|
||||
|
||||
if constexpr (!internal::has_flatten_fields<FieldTupleType>()) {
|
||||
return std::apply(make, _n.fields());
|
||||
} else {
|
||||
const auto fields = concat_named_tuple<FieldTupleType>(_n);
|
||||
return std::apply(make, fields);
|
||||
}
|
||||
|
||||
} else {
|
||||
return from_named_tuple<T, RequiredType>(RequiredType(_n));
|
||||
}
|
||||
}*/
|
||||
|
||||
template <class FieldTupleType, class NamedTupleType, class... Args>
|
||||
FieldTupleType concat_named_tuple(NamedTupleType&& _n, Args&&... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<FieldTupleType>>) {
|
||||
return std::make_tuple(std::forward<Args>(_args)...);
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, FieldTupleType>;
|
||||
if constexpr (internal::is_flatten_field<T>::value) {
|
||||
return concat_named_tuple<FieldTupleType>(
|
||||
_n, std::move(_args)...,
|
||||
from_named_tuple<typename T::Type>(_n));
|
||||
} else {
|
||||
return concat_named_tuple<FieldTupleType>(
|
||||
_n, std::forward<Args>(_args)...,
|
||||
std::move(_n.template get_field<T::name_>()));
|
||||
}
|
||||
}
|
||||
return internal::copy_from_named_tuple<T>(_n);
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
#ifndef RFL_INTERNAL_COPY_FROM_NAMED_TUPLE_HPP_
|
||||
#define RFL_INTERNAL_COPY_FROM_NAMED_TUPLE_HPP_
|
||||
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/internal/has_flatten_fields.hpp"
|
||||
#include "rfl/internal/is_named_tuple.hpp"
|
||||
#include "rfl/internal/move_from_named_tuple.hpp"
|
||||
#include "rfl/internal/ptr_field_tuple_t.hpp"
|
||||
#include "rfl/named_tuple_t.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class FieldTupleType, class NamedTupleType, class... Args>
|
||||
FieldTupleType concat_named_tuple(const NamedTupleType& _n, Args&&... _args);
|
||||
|
||||
/// Creates a struct of type T from a named tuple.
|
||||
/// All fields of the struct must be an rfl::Field.
|
||||
template <class T, class NamedTupleType>
|
||||
T copy_from_named_tuple(const NamedTupleType& _n) {
|
||||
using RequiredType = std::decay_t<named_tuple_t<T>>;
|
||||
|
||||
if constexpr (internal::is_named_tuple_v<std::decay_t<T>>) {
|
||||
return _n;
|
||||
} else if constexpr (std::is_same<std::decay_t<NamedTupleType>,
|
||||
RequiredType>()) {
|
||||
const auto make = []<class... ValueTypes>(ValueTypes&&... _values) {
|
||||
return T{std::move(_values)...};
|
||||
};
|
||||
|
||||
using PtrFieldTupleType = internal::ptr_field_tuple_t<std::decay_t<T>>;
|
||||
|
||||
if constexpr (!internal::has_flatten_fields<PtrFieldTupleType>()) {
|
||||
auto values = _n.values();
|
||||
return std::apply(make, std::move(values));
|
||||
} else {
|
||||
auto fields = concat_named_tuple<PtrFieldTupleType>(_n);
|
||||
return std::apply(make, std::move(fields));
|
||||
}
|
||||
|
||||
} else {
|
||||
return move_from_named_tuple<T, RequiredType>(RequiredType(_n));
|
||||
}
|
||||
}
|
||||
|
||||
template <class FieldTupleType, class NamedTupleType, class... Args>
|
||||
FieldTupleType concat_named_tuple(const NamedTupleType& _n, Args&&... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<FieldTupleType>>) {
|
||||
return std::make_tuple(std::forward<Args>(_args)...);
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, FieldTupleType>;
|
||||
if constexpr (internal::is_flatten_field<T>::value) {
|
||||
return concat_named_tuple<FieldTupleType>(
|
||||
_n, std::move(_args)...,
|
||||
copy_from_named_tuple<typename T::Type>(_n));
|
||||
} else {
|
||||
return concat_named_tuple<FieldTupleType>(
|
||||
_n, std::move(_args)..., _n.template get_field<T::name_>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
+3
-3
@@ -1,5 +1,5 @@
|
||||
#ifndef RFL_INTERNAL_TO_FIELD_TUPLE_HPP_
|
||||
#define RFL_INTERNAL_TO_FIELD_TUPLE_HPP_
|
||||
#ifndef RFL_INTERNAL_COPY_TO_FIELD_TUPLE_HPP_
|
||||
#define RFL_INTERNAL_COPY_TO_FIELD_TUPLE_HPP_
|
||||
|
||||
#include <iostream>
|
||||
#include <tuple>
|
||||
@@ -41,7 +41,7 @@ with open("generated_code2.cpp", "w", encoding="utf-8") as codefile:
|
||||
*/
|
||||
|
||||
template <class T>
|
||||
auto to_field_tuple(const T& _t) {
|
||||
auto copy_to_field_tuple(const T& _t) {
|
||||
if constexpr (internal::is_named_tuple_v<T>) {
|
||||
return _t.fields();
|
||||
} else if constexpr (internal::has_n_fields<T, 0>) {
|
||||
@@ -5,14 +5,14 @@
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/internal/to_field_tuple.hpp"
|
||||
#include "rfl/internal/copy_to_field_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
using field_tuple_t =
|
||||
typename std::invoke_result<decltype(to_field_tuple<T>), T>::type;
|
||||
typename std::invoke_result<decltype(copy_to_field_tuple<T>), T>::type;
|
||||
|
||||
}
|
||||
} // namespace rfl
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef RFL_INTERNAL_FLATTEN_FIELD_TUPLE_HPP_
|
||||
#define RFL_INTERNAL_FLATTEN_FIELD_TUPLE_HPP_
|
||||
|
||||
#include <tuple>
|
||||
|
||||
#include "rfl/internal/copy_to_field_tuple.hpp"
|
||||
#include "rfl/internal/is_flatten_field.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class FieldTuple, class... Args>
|
||||
auto flatten_field_tuple(FieldTuple&& _t, Args&&... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<FieldTuple>>) {
|
||||
return std::tuple_cat(std::forward<Args>(_args)...);
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, std::decay_t<FieldTuple>>;
|
||||
if constexpr (is_flatten_field<T>::value) {
|
||||
return flatten_field_tuple(
|
||||
std::forward<FieldTuple>(_t), std::forward<Args>(_args)...,
|
||||
flatten_field_tuple(copy_to_field_tuple( // TODO: move_to_...
|
||||
std::get<i>(_t).get())));
|
||||
} else {
|
||||
using T =
|
||||
std::decay_t<std::tuple_element_t<i, std::decay_t<FieldTuple>>>;
|
||||
return flatten_field_tuple(
|
||||
std::forward<FieldTuple>(_t), std::forward<Args>(_args)...,
|
||||
std::make_tuple(std::forward<T>(std::get<i>(_t))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -15,7 +15,7 @@ constexpr bool has_flatten_fields() {
|
||||
if constexpr (_i == std::tuple_size_v<TupleType>) {
|
||||
return false;
|
||||
} else {
|
||||
using T = std::tuple_element_t<_i, TupleType>;
|
||||
using T = std::decay_t<std::tuple_element_t<_i, TupleType>>;
|
||||
return is_flatten_field<T>::value ||
|
||||
has_flatten_fields<TupleType, _i + 1>();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef RFL_INTERNAL_MOVE_AND_FLATTEN_FIELD_TUPLE_HPP_
|
||||
#define RFL_INTERNAL_MOVE_AND_FLATTEN_FIELD_TUPLE_HPP_
|
||||
|
||||
#include <tuple>
|
||||
|
||||
#include "rfl/internal/copy_to_field_tuple.hpp"
|
||||
#include "rfl/internal/is_flatten_field.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class FieldTuple, class... Args>
|
||||
auto move_and_flatten_field_tuple(FieldTuple&& _t, Args&&... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<FieldTuple>>) {
|
||||
return std::tuple_cat(std::move(_args)...);
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, std::decay_t<FieldTuple>>;
|
||||
if constexpr (is_flatten_field<T>::value) {
|
||||
return move_and_flatten_field_tuple(
|
||||
std::move(_t), std::move(_args)...,
|
||||
move_and_flatten_field_tuple(
|
||||
copy_to_field_tuple( // TODO: move_to_...
|
||||
std::get<i>(_t).get())));
|
||||
} else {
|
||||
return move_and_flatten_field_tuple(
|
||||
std::move(_t), std::move(_args)...,
|
||||
std::make_tuple(std::move(std::get<i>(_t))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef RFL_INTERNAL_MOVE_FIELD_TUPLE_TO_NAMED_TUPLE_HPP_
|
||||
#define RFL_INTERNAL_MOVE_FIELD_TUPLE_TO_NAMED_TUPLE_HPP_
|
||||
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/Field.hpp"
|
||||
#include "rfl/internal/has_flatten_fields.hpp"
|
||||
#include "rfl/internal/move_and_flatten_field_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class FieldTuple>
|
||||
auto move_field_tuple_to_named_tuple(FieldTuple&& _field_tuple) {
|
||||
const auto ft_to_nt = []<class... Fields>(Fields&&... _fields) {
|
||||
return make_named_tuple(std::move(_fields)...);
|
||||
};
|
||||
|
||||
if constexpr (!has_flatten_fields<std::decay_t<FieldTuple>>()) {
|
||||
return std::apply(ft_to_nt, std::move(_field_tuple));
|
||||
} else {
|
||||
auto flattened_tuple =
|
||||
move_and_flatten_field_tuple(std::move(_field_tuple));
|
||||
return std::apply(ft_to_nt, std::move(flattened_tuple));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -76,7 +76,7 @@ auto move_from_pointers(Pointers& _ptrs, Args&&... _args) {
|
||||
/// Creates a struct of type T from a named tuple by moving the underlying
|
||||
/// fields.
|
||||
template <class T, class NamedTupleType>
|
||||
T move_from_named_tuple(NamedTupleType& _n) {
|
||||
T move_from_named_tuple(NamedTupleType&& _n) {
|
||||
using RequiredType = std::decay_t<named_tuple_t<T>>;
|
||||
|
||||
if constexpr (is_named_tuple_v<std::decay_t<T>>) {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -22,10 +22,10 @@ auto flatten_ptr_field_tuple(const PtrFieldTuple& _t, Args&&... _args) {
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, std::decay_t<PtrFieldTuple>>;
|
||||
if constexpr (internal::is_flatten_field<T>::value) {
|
||||
return flatten_ptr_field_tuple(
|
||||
_t, std::forward<Args>(_args)...,
|
||||
flatten_ptr_field_tuple(
|
||||
internal::to_ptr_field_tuple(*std::get<i>(_t).get())));
|
||||
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)));
|
||||
@@ -33,6 +33,20 @@ auto flatten_ptr_field_tuple(const PtrFieldTuple& _t, Args&&... _args) {
|
||||
}
|
||||
}
|
||||
|
||||
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...);
|
||||
};
|
||||
|
||||
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>
|
||||
@@ -41,20 +55,7 @@ auto to_ptr_named_tuple(const T& _t) {
|
||||
return nt_to_ptr_named_tuple(_t);
|
||||
} else {
|
||||
const auto ptr_field_tuple = to_ptr_field_tuple(_t);
|
||||
|
||||
const auto ft_to_nt = []<class... Fields>(const Fields&... _fields) {
|
||||
return make_named_tuple(_fields...);
|
||||
};
|
||||
|
||||
using PtrFieldTuple = std::decay_t<decltype(ptr_field_tuple)>;
|
||||
|
||||
if constexpr (!has_flatten_fields<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);
|
||||
}
|
||||
return field_tuple_to_named_tuple(ptr_field_tuple);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,6 @@
|
||||
#include "rfl/internal/all_fields.hpp"
|
||||
#include "rfl/internal/has_reflection_method_v.hpp"
|
||||
#include "rfl/internal/has_reflection_type_v.hpp"
|
||||
#include "rfl/internal/move_from_named_tuple.hpp"
|
||||
#include "rfl/internal/no_duplicate_field_names.hpp"
|
||||
#include "rfl/internal/to_ptr_named_tuple.hpp"
|
||||
#include "rfl/named_tuple_t.hpp"
|
||||
@@ -76,7 +75,7 @@ struct Parser {
|
||||
using NamedTupleType = named_tuple_t<T>;
|
||||
const auto to_struct = [](NamedTupleType&& _n) -> Result<T> {
|
||||
try {
|
||||
return internal::move_from_named_tuple<T>(_n);
|
||||
return from_named_tuple<T>(std::move(_n));
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
|
||||
+14
-2
@@ -1,12 +1,24 @@
|
||||
#ifndef RFL_REPLACE_HPP_
|
||||
#define RFL_REPLACE_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/from_named_tuple.hpp"
|
||||
#include "rfl/internal/is_named_tuple.hpp"
|
||||
#include "rfl/to_named_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Replaces one or several fields, returning a new version
|
||||
/// with the non-replaced fields left unchanged.
|
||||
template <class T, class RField, class... OtherRFields>
|
||||
auto replace(T&& _t, RField&& _field, OtherRFields&&... _other_fields) {
|
||||
if constexpr (internal::is_named_tuple_v<T>) {
|
||||
return std::forward<T>(_t).replace(_field, _other_fields...);
|
||||
} else {
|
||||
return from_named_tuple<T>(to_named_tuple(std::forward<T>(_t))
|
||||
.replace(_field, _other_fields...));
|
||||
}
|
||||
}
|
||||
|
||||
/// Replaces one or several fields, returning a new version
|
||||
/// with the non-replaced fields left unchanged.
|
||||
template <class T, class RField, class... OtherRFields>
|
||||
|
||||
@@ -3,32 +3,37 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/always_false.hpp"
|
||||
#include "rfl/internal/copy_to_field_tuple.hpp"
|
||||
#include "rfl/internal/flatten_field_tuple.hpp"
|
||||
#include "rfl/internal/has_flatten_fields.hpp"
|
||||
#include "rfl/internal/is_field.hpp"
|
||||
#include "rfl/internal/is_named_tuple.hpp"
|
||||
#include "rfl/internal/to_field_tuple.hpp"
|
||||
#include "rfl/internal/move_field_tuple_to_named_tuple.hpp"
|
||||
#include "rfl/internal/move_to_field_tuple.hpp"
|
||||
#include "rfl/make_named_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <class FieldTuple, class... Args>
|
||||
auto flatten_field_tuple(FieldTuple&& _t, Args&&... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<FieldTuple>>) {
|
||||
return std::tuple_cat(std::forward<Args>(_args)...);
|
||||
/// Generates the named tuple that is equivalent to the struct _t.
|
||||
/// If _t already is a named tuple, then _t will be returned.
|
||||
/// All fields of the struct must be an rfl::Field.
|
||||
template <class T>
|
||||
auto to_named_tuple(T&& _t) {
|
||||
if constexpr (internal::is_named_tuple_v<std::decay_t<T>>) {
|
||||
return _t;
|
||||
} else if constexpr (internal::is_field_v<std::decay_t<T>>) {
|
||||
return make_named_tuple(std::forward<T>(_t));
|
||||
} else if constexpr (std::is_lvalue_reference<T>{}) {
|
||||
auto field_tuple = internal::copy_to_field_tuple(_t);
|
||||
return internal::move_field_tuple_to_named_tuple(
|
||||
std::move(field_tuple));
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, std::decay_t<FieldTuple>>;
|
||||
if constexpr (internal::is_flatten_field<T>::value) {
|
||||
return flatten_field_tuple(
|
||||
_t, std::forward<Args>(_args)...,
|
||||
flatten_field_tuple(internal::to_field_tuple(
|
||||
std::move(std::get<i>(_t).get()))));
|
||||
} else {
|
||||
return flatten_field_tuple(_t, std::forward<Args>(_args)...,
|
||||
std::make_tuple(std::get<i>(_t)));
|
||||
}
|
||||
auto field_tuple = internal::move_to_field_tuple(_t);
|
||||
return internal::move_field_tuple_to_named_tuple(
|
||||
std::move(field_tuple));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,25 +42,14 @@ auto flatten_field_tuple(FieldTuple&& _t, Args&&... _args) {
|
||||
/// All fields of the struct must be an rfl::Field.
|
||||
template <class T>
|
||||
auto to_named_tuple(const T& _t) {
|
||||
if constexpr (internal::is_named_tuple_v<T>) {
|
||||
if constexpr (internal::is_named_tuple_v<std::decay_t<T>>) {
|
||||
return _t;
|
||||
} else if constexpr (internal::is_field_v<T>) {
|
||||
} else if constexpr (internal::is_field_v<std::decay_t<T>>) {
|
||||
return make_named_tuple(_t);
|
||||
} else {
|
||||
auto field_tuple = internal::to_field_tuple(_t);
|
||||
|
||||
using FieldTuple = std::decay_t<decltype(field_tuple)>;
|
||||
|
||||
const auto ft_to_nt = []<class... Fields>(Fields&&... _fields) {
|
||||
return make_named_tuple(_fields...);
|
||||
};
|
||||
|
||||
if constexpr (!internal::has_flatten_fields<FieldTuple>()) {
|
||||
return std::apply(ft_to_nt, std::move(field_tuple));
|
||||
} else {
|
||||
auto flattened_tuple = flatten_field_tuple(std::move(field_tuple));
|
||||
return std::apply(ft_to_nt, std::move(flattened_tuple));
|
||||
}
|
||||
auto field_tuple = internal::copy_to_field_tuple(_t);
|
||||
return internal::move_field_tuple_to_named_tuple(
|
||||
std::move(field_tuple));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_move_replace() {
|
||||
std::cout << "test_move_replace" << std::endl;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"children", rfl::Box<std::vector<Person>>> children;
|
||||
};
|
||||
|
||||
const auto lisa = Person{.first_name = "Lisa",
|
||||
.last_name = "Simpson",
|
||||
.children = rfl::make_box<std::vector<Person>>()};
|
||||
|
||||
const auto maggie =
|
||||
rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")));
|
||||
|
||||
write_and_read(
|
||||
maggie, R"({"firstName":"Maggie","lastName":"Simpson","children":[]})");
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_replace() {
|
||||
std::cout << "test_replace" << std::endl;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"children", rfl::Box<std::vector<Person>>> children;
|
||||
};
|
||||
|
||||
auto lisa = Person{.first_name = "Lisa",
|
||||
.last_name = "Simpson",
|
||||
.children = rfl::make_box<std::vector<Person>>()};
|
||||
|
||||
const auto maggie = rfl::replace(
|
||||
std::move(lisa), rfl::make_field<"firstName">(std::string("Maggie")));
|
||||
|
||||
write_and_read(
|
||||
maggie, R"({"firstName":"Maggie","lastName":"Simpson","children":[]})");
|
||||
}
|
||||
+8
-4
@@ -11,6 +11,7 @@
|
||||
#include "test_optional_fields.hpp"
|
||||
#include "test_readme_example.hpp"
|
||||
#include "test_ref.hpp"
|
||||
#include "test_replace.hpp"
|
||||
#include "test_tagged_union.hpp"
|
||||
#include "test_timestamp.hpp"
|
||||
#include "test_unique_ptr.hpp"
|
||||
@@ -29,13 +30,16 @@ int main() {
|
||||
test_field_variant();
|
||||
test_ref();
|
||||
test_box();
|
||||
test_custom_class1();
|
||||
test_custom_class2();
|
||||
test_custom_class3();
|
||||
test_custom_class4();
|
||||
test_array();
|
||||
test_timestamp();
|
||||
test_flatten();
|
||||
|
||||
test_custom_class1();
|
||||
test_custom_class2();
|
||||
test_custom_class3();
|
||||
test_custom_class4();
|
||||
|
||||
test_replace();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user