mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-04 01:06:19 +08:00
Added support for rfl::to_view; #32
This commit is contained in:
@@ -46,6 +46,7 @@
|
||||
#include "rfl/remove_fields.hpp"
|
||||
#include "rfl/replace.hpp"
|
||||
#include "rfl/to_named_tuple.hpp"
|
||||
#include "rfl/to_view.hpp"
|
||||
#include "rfl/type_name_t.hpp"
|
||||
#include "rfl/visit.hpp"
|
||||
|
||||
|
||||
@@ -18,11 +18,12 @@ template <class T>
|
||||
constexpr auto tuple_view(T&);
|
||||
|
||||
template <class T, typename F>
|
||||
constexpr auto bind_to_tuple(T& _t, F&& f) {
|
||||
constexpr auto bind_to_tuple(T& _t, const F& _f) {
|
||||
auto view = tuple_view(_t);
|
||||
return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
|
||||
return std::make_tuple(std::forward<F>(f)(std::get<Is>(view))...);
|
||||
}(std::make_index_sequence<std::tuple_size_v<decltype(view)>>());
|
||||
return std::make_tuple(_f(std::get<Is>(view))...);
|
||||
}
|
||||
(std::make_index_sequence<std::tuple_size_v<decltype(view)>>());
|
||||
}
|
||||
|
||||
/*The following boilerplate code was generated using a Python script:
|
||||
|
||||
@@ -32,7 +32,7 @@ auto flatten_ptr_tuple(PtrTuple&& _t, Args... _args) {
|
||||
}
|
||||
|
||||
template <class T>
|
||||
auto to_flattened_ptr_tuple(const T& _t) {
|
||||
auto to_flattened_ptr_tuple(T&& _t) {
|
||||
return flatten_ptr_tuple(to_ptr_tuple(_t));
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class PtrFieldTuple, class... Args>
|
||||
auto flatten_ptr_field_tuple(const PtrFieldTuple& _t, Args&&... _args) {
|
||||
auto flatten_ptr_field_tuple(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)...);
|
||||
@@ -38,7 +38,7 @@ auto flatten_ptr_field_tuple(const PtrFieldTuple& _t, Args&&... _args) {
|
||||
}
|
||||
|
||||
template <class PtrFieldTuple>
|
||||
auto field_tuple_to_named_tuple(const PtrFieldTuple& _ptr_field_tuple) {
|
||||
auto field_tuple_to_named_tuple(PtrFieldTuple& _ptr_field_tuple) {
|
||||
const auto ft_to_nt = []<class... Fields>(const Fields&... _fields) {
|
||||
return make_named_tuple(_fields...);
|
||||
};
|
||||
@@ -54,19 +54,19 @@ auto field_tuple_to_named_tuple(const PtrFieldTuple& _ptr_field_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) {
|
||||
auto to_ptr_named_tuple(T&& _t) {
|
||||
if constexpr (has_fields<std::decay_t<T>>()) {
|
||||
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);
|
||||
auto ptr_field_tuple = to_ptr_field_tuple(_t);
|
||||
return field_tuple_to_named_tuple(ptr_field_tuple);
|
||||
}
|
||||
} else {
|
||||
using FieldNames = rfl::field_names_t<T>;
|
||||
const auto flattened_ptr_tuple = to_flattened_ptr_tuple(_t);
|
||||
auto flattened_ptr_tuple = to_flattened_ptr_tuple(_t);
|
||||
return copy_flattened_tuple_to_named_tuple<FieldNames>(flattened_ptr_tuple);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ auto wrap_in_fields(auto&& _tuple, Fields&&... _fields) {
|
||||
return std::make_tuple(std::move(_fields)...);
|
||||
} else {
|
||||
auto value = std::move(std::get<i>(_tuple));
|
||||
using Type = std::decay_t<decltype(value)>;
|
||||
using Type = std::decay_t<std::remove_pointer_t<decltype(value)>>;
|
||||
if constexpr (is_flatten_field_v<Type>) {
|
||||
// The problem here is that the FieldNames are already flattened, but this
|
||||
// is not, so we need to determine how many field names to skip.
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef RFL_TO_VIEW_HPP_
|
||||
#define RFL_TO_VIEW_HPP_
|
||||
|
||||
#include <iostream>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/internal/to_ptr_named_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <class T>
|
||||
auto to_view(T& _t) {
|
||||
return internal::to_ptr_named_tuple(_t);
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user