ORM C++
Loading...
Searching...
No Matches
value_names.cppm
Go to the documentation of this file.
1module;
2
3#include <memory>
4#include <string_view>
5#include <type_traits>
6
8
9import :fixed_string;
10import :compiler_signatures;
11import :storage;
12
13namespace orm::reflection
14{
15namespace detail
16{
17template <typename Class, typename Member>
18auto memberClass(Member Class::*) -> Class;
19
20template <typename T>
21inline constexpr T memberNameObjectStorage{};
22
23template <auto Member>
24[[nodiscard]] constexpr auto storedMemberPointer() noexcept
25{
26 using Class = decltype(memberClass(Member));
27 return std::addressof(memberNameObjectStorage<InactiveStorage<InactiveStorage<Class>>>.object.object.*Member);
28}
29
30[[nodiscard]] consteval auto isIdentifierStart(char character) noexcept -> bool
31{
32 return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || character == '_' ||
33 static_cast<unsigned char>(character) >= 0x80U;
34}
35
36[[nodiscard]] consteval auto isIdentifierContinuation(char character) noexcept -> bool
37{
38 return isIdentifierStart(character) || (character >= '0' && character <= '9');
39}
40
41[[nodiscard]] consteval auto unqualifiedMemberName(std::string_view spelling) noexcept -> std::string_view
42{
43 const auto scope = spelling.rfind("::");
44 auto begin = scope == std::string_view::npos ? 0 : scope + 2;
45 while (begin < spelling.size() && !isIdentifierStart(spelling[begin]))
46 {
47 ++begin;
48 }
49
50 auto end = begin;
51 while (end < spelling.size() && isIdentifierContinuation(spelling[end]))
52 {
53 ++end;
54 }
55 return spelling.substr(begin, end - begin);
56}
57} // namespace detail
58
59export
60{
61 template <auto Value>
62 [[nodiscard]] consteval auto valueName() noexcept
63 {
64 constexpr auto name = detail::valueNameView<Value>();
65 return detail::makeFixedString<name.size()>(name);
66 }
67
68 template <auto Member>
69 [[nodiscard]] consteval auto memberName() noexcept
70 {
71#if defined(_MSC_VER)
72 if constexpr (std::is_member_object_pointer_v<decltype(Member)>)
73 {
74 constexpr auto pointer = detail::wrapPointer(detail::storedMemberPointer<Member>());
75 constexpr auto name = detail::pointedFieldNameView<pointer>();
76 return detail::makeFixedString<name.size()>(name);
77 }
78 else
79#endif
80 {
81 constexpr auto spelling = detail::valueNameView<Member>();
82 constexpr auto name = detail::unqualifiedMemberName(spelling);
83 return detail::makeFixedString<name.size()>(name);
84 }
85 }
86}
87
88namespace detail
89{
90struct NameSignatureSentinel
91{
92 int member;
93};
94
95enum class ValueSignatureSentinel
96{
97 enumerator
98};
99
100inline constexpr auto valueSignatureSentinelName = valueName<ValueSignatureSentinel::enumerator>();
101
102static_assert(memberName<&NameSignatureSentinel::member>() == "member",
103 "ORM_REFLECTION_MEMBER_SIGNATURE_FORMAT: unsupported compiler member-signature format");
104static_assert(valueSignatureSentinelName.view().ends_with("enumerator"),
105 "ORM_REFLECTION_VALUE_SIGNATURE_FORMAT: unsupported compiler value-signature format");
106} // namespace detail
107} // namespace orm::reflection
consteval auto memberName() noexcept
consteval auto valueName() noexcept