repr 0.1
Reconstructable string representations and more
Loading...
Searching...
No Matches
member.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <string_view>
5#include <utility>
6#include <array>
9#include <librepr/ctvi/ctvi.h>
11
12#include <librepr/util/util.h>
14
18
19namespace librepr {
20namespace detail {
21
22#if USING(LIBREPR_COMPILER_CLANG)
23// clang-format off
24#define LIBREPR_SUBOBJECT_PTR(...) ::librepr::Wrap{__VA_ARGS__}
25// clang-format on
26#else
27#define LIBREPR_SUBOBJECT_PTR(...) __VA_ARGS__
28#endif
29
30template <auto>
32
33template <typename C, typename T, T C::*ptr>
34struct MemberName<ptr> {
35 using type = C;
36
37 constexpr static auto value =
38#if USING(LIBREPR_COMPILER_MSVC)
39
40 // MSVC unfortunately returns pointer to member template arguments into offsets
41 // like `pointer-to-member(0x0)`. Using the aggregate member name reflection trick yields
42 // `&fake_obj<struct Test>->value->member` instead, which is usable here.
43
45#else
46 librepr::ctvi::detail::name_from_member_ptr<ptr>();
47#endif
48};
49
50template <Member info>
52 using type = typename decltype(info)::class_type;
53
54 constexpr static auto member_name() {
55 if constexpr (!info.name.empty()) {
56 return info.name;
57 } else if constexpr (!std::is_member_function_pointer_v<decltype(info.value.value)> &&
58 !std::is_function_v<std::remove_pointer_t<decltype(info.value.value)>>) {
59 // don't extract names of accessor functions - those will probably be incorrect
60 return MemberName<info.value.value>{}.value;
61 } else {
62 return const_string<0>{};
63 }
64 }
65 constexpr static auto value = member_name();
66};
67
69LIBREPR_WARNING_DISABLE(CLANG, "-Wundefined-var-template")
70
71// This technique has been described by @schaumb
72// For explanations see
73// https://github.com/boostorg/pfr/issues/150#issuecomment-1831500004
74// https://github.com/boostorg/pfr/issues/90#issuecomment-1589530490
75
76template <typename T, std::size_t Idx>
77 requires(std::is_aggregate_v<T> && !std::is_array_v<T>)
79 librepr::ctvi::detail::name_from_subobject<T, LIBREPR_SUBOBJECT_PTR(get<Idx>(to_addr_tuple(fake_obj<T>.value)))>();
80
82
84 requires(std::is_aggregate_v<T> && !std::is_array_v<T>)
88
89template <auto Accessor>
91
92template <typename T>
93constexpr auto get_member_names() {
94 // TODO
95
97 if constexpr (has_custom_members<T>) {
98 if constexpr (Settings<T>::members != 0) {
100 []<Member... members>() { return std::array{std::string_view{custom_member_name<members>}...}; });
101 } else {
102 return error{};
103 }
104 } else if constexpr (std::is_aggregate_v<T> && !std::is_array_v<T>) {
105 constexpr auto member_count = arity<T>;
106 if constexpr (member_count != 0 && requires { raw_member_name<T, 0>; }) {
107 return []<std::size_t... Idx>(std::index_sequence<Idx...>) {
110
111 } else {
112 return error{};
113 }
114 } else {
115 return error{};
116 }
117}
118
119} // namespace detail
120
121template <typename T>
122constexpr inline auto member_names = detail::get_member_names<T>();
123
124template <typename T, std::size_t Idx>
125 requires(Idx <= member_names<T>.size())
126constexpr inline auto member_name = member_names<T>[Idx];
127} // namespace librepr
T addressof(T... args)
Definition reflection.h:15
T is_function_v
T is_member_function_pointer_v
#define LIBREPR_SUBOBJECT_PTR(...)
Definition member.h:27
constexpr auto name_from_subobject()
Definition ctvi/ctvi.h:37
LIBREPR_WARNING_POP constexpr auto raw_member_names
Definition member.h:85
const Wrap< T > fake_obj
constexpr auto get_member_names()
Definition member.h:93
LIBREPR_WARNING_PUSH constexpr auto raw_member_name
Definition member.h:78
constexpr auto custom_member_name
Definition member.h:90
constexpr auto to_addr_tuple(T &&object)
Definition to_tuple.h:20
Definition ctvi/ctvi.h:9
std::string code_for()
Definition repr:39
constexpr auto member_names
Definition member.h:122
constexpr auto member_name
Definition member.h:126
constexpr decltype(auto) get(RefTuple< Ts... > const &tuple) noexcept
Definition reftuple.h:59
STL namespace.
Definition members.h:68
Definition customization.h:12
Definition const_string.h:9
typename decltype(info)::class_type type
Definition member.h:52
static constexpr auto member_name()
Definition member.h:54
C type
Definition member.h:35
Definition member.h:31
#define LIBREPR_WARNING_DISABLE(COMPILER, WARNING)
Definition warning.h:37