ORM C++
Loading...
Searching...
No Matches
field_metadata.cppm
Go to the documentation of this file.
1module;
2
3#include <array>
4#include <cstddef>
5#include <string_view>
6#include <tuple>
7#include <utility>
8
10
11import :fixed_string;
12import :compiler_signatures;
13import :storage;
14import :type_names;
15import :value_names;
16import :aggregate;
17
18namespace orm::reflection
19{
20export
22 struct FieldDescriptor
23 {
24 std::size_t index;
25 std::string_view name;
26 std::string_view typeName;
28 constexpr auto operator==(const FieldDescriptor&) const noexcept -> bool = default;
29 };
30}
31
32namespace detail
33{
34#if defined(__clang__)
35#pragma clang diagnostic push
36#pragma clang diagnostic ignored "-Wundefined-var-template"
37#endif
38
39template <typename T, std::size_t Index>
40[[nodiscard]] consteval auto makeFieldName() noexcept
41{
42 static_assert(Index < fieldCount<T>, "Field index is outside the reflected aggregate.");
43 constexpr auto pointers = fieldPointers(fakeObject<T>());
44 constexpr auto wrapped = wrapPointer(std::get<Index>(pointers));
45 constexpr auto name = pointedFieldNameView<wrapped>();
46 return makeFixedString<name.size()>(name);
47}
48
49template <typename T, std::size_t Index>
50inline constexpr auto fieldNameStorage = makeFieldName<T, Index>();
51
52#if defined(__clang__)
53#pragma clang diagnostic pop
54#endif
55} // namespace detail
56
57export
59 template <typename T, std::size_t Index>
60 [[nodiscard]] consteval auto fieldName() noexcept
61 {
62 using Type = detail::ReflectedType<T>;
63 return detail::fieldNameStorage<Type, Index>;
64 }
65}
66
67namespace detail
68{
69static_assert(fieldName<NameSignatureSentinel, 0>() == "member",
70 "ORM_REFLECTION_FIELD_SIGNATURE_FORMAT: unsupported compiler field-signature format");
71} // namespace detail
72
73namespace detail
74{
75template <typename T, std::size_t... Indices>
76[[nodiscard]] consteval auto makeFieldNames(std::index_sequence<Indices...>) noexcept
77{
78 return std::array<std::string_view, sizeof...(Indices)>{fieldNameStorage<T, Indices>.view()...};
79}
80
81template <typename T>
82inline constexpr auto fieldNamesStorage = makeFieldNames<T>(std::make_index_sequence<fieldCount<T>>{});
83
84template <typename T, std::size_t... Indices>
85[[nodiscard]] consteval auto makeFieldDescriptors(std::index_sequence<Indices...>) noexcept
86{
87 return std::array<FieldDescriptor, sizeof...(Indices)>{FieldDescriptor{
88 Indices, fieldNameStorage<T, Indices>.view(), typeNameStorage<field_type_t<T, Indices>>.view()}...};
89}
90
91template <typename T>
92inline constexpr auto fieldDescriptorStorage = makeFieldDescriptors<T>(std::make_index_sequence<fieldCount<T>>{});
93} // namespace detail
94
95export
96{
97 template <typename T>
98 [[nodiscard]] consteval auto fieldNames() noexcept
99 {
100 using Type = detail::ReflectedType<T>;
101 return detail::fieldNamesStorage<Type>;
103
104 template <typename T>
105 [[nodiscard]] consteval auto fields() noexcept
106 {
107 using Type = detail::ReflectedType<T>;
108 return detail::fieldDescriptorStorage<Type>;
109 }
110}
111} // namespace orm::reflection
constexpr auto fieldPointers(T &value) noexcept
consteval auto fieldNames() noexcept
consteval auto fields() noexcept
constexpr std::size_t fieldCount
consteval auto fieldName() noexcept
constexpr auto typeNameStorage
constexpr auto operator==(const FieldDescriptor &) const noexcept -> bool=default