codex
Loading...
Searching...
No Matches
soa.hpp
Go to the documentation of this file.
1#ifndef CODEX_REFLECTION_SOA_DETAIL_HPP
2#define CODEX_REFLECTION_SOA_DETAIL_HPP
3
4#include <cstddef>
5#include <tuple>
6#include <type_traits>
7#include <vector>
8
9#include "../fields.hpp"
10#include "utils.hpp"
11
12namespace codex::refl::detail {
13
20template<typename S, std::size_t I, typename T>
21class field_storage : public S::for_field<I, T> {
22protected:
23 static constexpr auto index = I;
24};
25
26template<typename S, typename Is, typename Ts> struct storage_impl;
27
35template<typename S, std::size_t ...Is, typename ...Ts>
36class storage_impl<S, std::index_sequence<Is...>, std::tuple<Ts...>>
37 : public field_storage<S, Is, std::decay_t<Ts>>...
38{
39private:
41 using tuple_type = std::tuple<std::decay_t<Ts>...>;
43 template<std::size_t I>
44 using value_type = std::tuple_element_t<I, tuple_type>;
50 template<std::size_t I>
52public:
54 template<std::size_t I>
55 auto field(void) -> field_storage_type<I>& { return *this; }
56 template<std::size_t I>
57 auto field(void) const -> const field_storage_type<I>& { return *this; }
65 void for_each(auto &&f) { (..., CODEX_FWD(f)(this->field<Is>())); }
66 void for_each(auto &&f) const { (..., CODEX_FWD(f)(this->field<Is>())); }
74 void for_each_i(std::size_t i, auto &&f);
75 void for_each_i(std::size_t i, auto &&f) const;
76};
77
78template<typename S, std::size_t ...Is, typename ...Ts>
79void storage_impl<
80 S, std::index_sequence<Is...>, std::tuple<Ts...>
81>::for_each_i(std::size_t i, auto &&f) {
82 return this->for_each([i, &f]<typename V>(V &v) {
84 });
85}
86
87template<typename S, std::size_t ...Is, typename ...Ts>
88void storage_impl<
89 S, std::index_sequence<Is...>, std::tuple<Ts...>
90>::for_each_i(std::size_t i, auto &&f) const {
91 return this->for_each([i, &f]<typename V>(V &v) {
93 });
94}
95
101template<typename T, typename S>
102using storage = storage_impl<
103 S, std::make_index_sequence<refl::field_count<T>()>,
105
108 template<std::size_t, typename T> using for_field = std::vector<T>;
109};
110
111}
112
113#endif
#define f(x)
Definition 5.c:2
Ultimate class for the storage of each field.
Definition soa.hpp:21
static constexpr auto index
Definition soa.hpp:23
void for_each(auto &&f)
Applies f to each field storage.
Definition soa.hpp:65
auto field(void) -> field_storage_type< I > &
Storage for field with index I.
Definition soa.hpp:55
void for_each_i(std::size_t i, auto &&f)
Applies f to each field of the element with index i.
std::tuple_element_t< I, tuple_type > value_type
(Value/decayed) type of field with index I.
Definition soa.hpp:44
auto field(void) const -> const field_storage_type< I > &
Definition soa.hpp:57
std::tuple< std::decay_t< Ts >... > tuple_type
Tuple with (value/decayed) types of each field.
Definition soa.hpp:41
constexpr codex::vec3 v
Definition rotate_test.cpp:12
Definition fields.hpp:8
storage_impl< S, std::make_index_sequence< refl::field_count< T >()>, refl::field_tuple_t< T > > storage
Storage for all fields of a structure.
Definition soa.hpp:102
std::integral_constant< std::size_t, I > index_constant
Alias for an integral_constant of size_t.
Definition utils.hpp:14
decltype(field_tuple(std::declval< T >())) field_tuple_t
Alias of the tuple type returned by field_tuple for type T.
Definition fields.hpp:42
Definition common.hpp:9
#define CODEX_FWD(x)
Definition utils.hpp:8
Definition reflexpr.cpp:168
Default storage descriptor.
Definition soa.hpp:107
std::vector< T > for_field
Definition soa.hpp:108