ORM C++
Loading...
Searching...
No Matches
aggregate.cppm
Go to the documentation of this file.
1module;
2
3#include <cstddef>
4#include <memory>
5#include <tuple>
6#include <type_traits>
7#include <utility>
8
9export module orm.reflection:aggregate;
10
11import :generated;
12import :storage;
13
15{
16export
18 inline constexpr std::size_t maxFieldCount = 128;
19}
20
21namespace detail
22{
23struct Any
24{
25 template <typename T>
26 operator T() const noexcept;
27};
28
29template <typename T, std::size_t... Indices>
30[[nodiscard]] consteval auto isAggregateInitializable(std::index_sequence<Indices...>) noexcept -> bool
31{
32 return requires { T{(static_cast<void>(Indices), Any{})...}; };
33}
34
35template <typename T, std::size_t N>
36inline constexpr bool isAggregateInitializableWith = isAggregateInitializable<T>(std::make_index_sequence<N>{});
37
38template <typename T, std::size_t N = 0>
39[[nodiscard]] consteval auto aggregateFieldCount() noexcept -> std::size_t
40{
41 if constexpr (N == maxFieldCount)
42 {
43 static_assert(!isAggregateInitializableWith<T, maxFieldCount + 1>,
44 "orm::reflection supports aggregates with at most 128 fields.");
45 return maxFieldCount;
46 }
47 else if constexpr (isAggregateInitializableWith<T, N> && !isAggregateInitializableWith<T, N + 1>)
48 {
49 return N;
50 }
51 else
52 {
53 return aggregateFieldCount<T, N + 1>();
54 }
55}
56
57template <typename T>
58inline constexpr bool supportedAggregateCategory = std::is_aggregate_v<T> && !std::is_union_v<T> && !std::is_array_v<T>;
59
60template <typename T, bool = supportedAggregateCategory<T>>
61struct ReflectionTraits;
62
63template <typename T>
64struct ReflectionTraits<T, false>
65{
66 static_assert(std::is_aggregate_v<T>, "ORM_REFLECTION_AGGREGATE: orm::reflection requires an aggregate type.");
67 static_assert(!std::is_union_v<T>, "orm::reflection does not support unions.");
68 static_assert(!std::is_array_v<T>, "orm::reflection does not support raw C arrays.");
69
70 static constexpr std::size_t count = 0;
71 using Tuple = std::tuple<>;
72};
73
74template <typename T>
75struct ReflectionTraits<T, true>
76{
77 static constexpr std::size_t count = aggregateFieldCount<T>();
78 using Binding = decltype(bindingTraitsImpl(std::declval<T&>(), std::integral_constant<std::size_t, count>{}));
79 using Tuple = typename Binding::Tuple;
80
81private:
82 template <std::size_t... Indices>
83 [[nodiscard]] static consteval auto containsRawArray(std::index_sequence<Indices...>) noexcept -> bool
84 {
85 return (std::is_array_v<std::remove_reference_t<std::tuple_element_t<Indices, Tuple>>> || ... || false);
86 }
87
88public:
89 static_assert(Binding::fieldsAreAddressable,
90 "ORM_REFLECTION_BIT_FIELD: orm::reflection does not support bit-fields.");
91 static_assert(std::tuple_size_v<Tuple> == count,
92 "orm::reflection cannot decompose this aggregate; inheritance is not supported.");
93 static_assert(!containsRawArray(std::make_index_sequence<count>{}),
94 "orm::reflection does not support raw C-array fields; use std::array instead.");
95};
96
97template <typename T>
98using ReflectedType = std::remove_cv_t<std::remove_reference_t<T>>;
99
100template <typename T>
101inline constexpr InactiveStorage<InactiveStorage<T>> fakeObjectStorage{};
102
103template <typename T>
104[[nodiscard]] consteval auto fakeObject() noexcept -> const T&
105{
106 return fakeObjectStorage<T>.object.object;
107}
108} // namespace detail
109
110export
112 template <typename T>
113 inline constexpr std::size_t fieldCount = detail::ReflectionTraits<detail::ReflectedType<T>>::count;
115 template <typename T>
116 [[nodiscard]] constexpr auto tieFields(T & value) noexcept
117// requires(!std::is_const_v<T>)
118 {
119 using Type = detail::ReflectedType<T>;
120 return detail::tieFieldsImpl(value, std::integral_constant<std::size_t, fieldCount<Type>>{});
121 }
123 template <typename T>
124 [[nodiscard]] constexpr auto tieFields(const T& value) noexcept
125 {
126 using Type = detail::ReflectedType<T>;
127 return detail::tieFieldsImpl(value, std::integral_constant<std::size_t, fieldCount<Type>>{});
128 }
130 template <typename T>
131 auto tieFields(T&&)
132// requires(!std::is_lvalue_reference_v<T>)
133 = delete;
135 template <typename T>
136 [[nodiscard]] constexpr auto fieldPointers(T & value) noexcept
137// requires(!std::is_const_v<T>)
138 {
139 return std::apply([]<typename... Fields>(Fields&... fields) { return std::tuple{std::addressof(fields)...}; },
140 tieFields(value));
141 }
143 template <typename T>
144 [[nodiscard]] constexpr auto fieldPointers(const T& value) noexcept
145 {
146 return std::apply([]<typename... Fields>(const Fields&... fields)
147 { return std::tuple{std::addressof(fields)...}; }, tieFields(value));
148 }
150 template <typename T>
151 auto fieldPointers(T&&)
152// requires(!std::is_lvalue_reference_v<T>)
153 = delete;
154}
155
156export
157{
158 template <typename T, std::size_t Index>
159 using field_type_t = std::remove_reference_t<
160 std::tuple_element_t<Index, typename detail::ReflectionTraits<detail::ReflectedType<T>>::Tuple>>;
161}
162} // namespace orm::reflection
constexpr auto fieldPointers(T &value) noexcept
constexpr std::size_t maxFieldCount
std::remove_reference_t< std::tuple_element_t< Index, typename detail::ReflectionTraits< detail::ReflectedType< T > >::Tuple > > field_type_t
consteval auto fields() noexcept
constexpr auto tieFields(T &value) noexcept
constexpr std::size_t fieldCount