repr 0.1
Reconstructable string representations and more
Loading...
Searching...
No Matches
members.h
Go to the documentation of this file.
1#pragma once
2#include <type_traits>
3#include <string_view>
4#include <concepts>
5#include <utility>
6
8#include <librepr/ctvi/ctvi.h>
10
11namespace librepr {
12
13template <typename T>
14struct Accessor;
15
16template <typename C, typename T>
17struct Accessor<T C::*>
18{
19private:
20 constexpr static auto get_type(){
21 if constexpr (std::is_function_v<T>){
23 }
24 else {
25 return std::type_identity<T>{};
26 }
27 }
28public:
29 T C::*value;
30
31 using class_type = C;
32 using type = typename decltype(get_type())::type;
33
34 template <typename Obj>
35 requires (std::same_as<Obj, class_type>)
36 constexpr decltype(auto) operator()(Obj&& obj) noexcept{
37 if constexpr (std::is_function_v<T>){
38 return (std::forward<Obj>(obj).*value)();
39 }
40 else {
41 return std::forward<Obj>(obj).*value;
42 }
43 }
44};
45
46template <typename T, typename C>
47struct Accessor<T(*)(C const&)>{
48 T(*value)(C const&);
49
50 using class_type = C;
51 using type = T;
52
53 template <typename Obj>
54 requires (std::same_as<Obj, class_type>)
55 constexpr decltype(auto) operator()(Obj&& obj) noexcept{
56 return value(std::forward<Obj>(obj));
57 }
58};
59
60template <typename C, typename T>
61Accessor(T C::*) -> Accessor<T C::*>;
62
63template <typename C, typename T>
64Accessor(T (*)(C const&)) -> Accessor<T (*)(C const&)>;
65
66template <typename T, std::size_t N>
67struct Member
68{
70 using type = typename accessor::type;
71 using class_type = typename accessor::class_type;
72
75
76 constexpr explicit Member(T accessor) requires (N == 0) : value(accessor) {}
77 constexpr Member(T accessor, auto name_) : value(accessor), name(name_) {}
78
79 template <typename Obj>
80 requires (std::same_as<std::decay_t<Obj>, class_type>)
81 constexpr decltype(auto) operator()(Obj&& obj) noexcept {
82 return value(std::forward<Obj>(obj));
83 }
84};
85
86
87template <typename T, std::size_t N>
88Member(T, char const (&)[N]) -> Member<T, N - 1>;
89
90template <typename T, std::size_t N>
92
93template <typename T>
95
96template <Member... Ts>
97concept is_homogeneous_members = requires { typename std::common_type_t<typename decltype(Ts)::class_type...>; };
98
99template <Member... Ts>
100struct MemberList : ValueListBase<MemberList, Ts...> {
101 static_assert(is_homogeneous_members<Ts...>, "No common class types in declared members detected.");
102
103 template <typename C>
104 constexpr static bool is_valid_for = std::same_as<std::common_type_t<typename decltype(Ts)::class_type...>, C>;
105};
106}
Definition members.h:97
Definition ctvi/ctvi.h:9
std::string code_for()
Definition repr:39
typename decltype(get_type())::type type
Definition members.h:32
C class_type
Definition members.h:31
constexpr decltype(auto) operator()(Obj &&obj) noexcept
Definition members.h:36
T C::* value
Definition members.h:29
constexpr decltype(auto) operator()(Obj &&obj) noexcept
Definition members.h:55
T type
Definition members.h:51
C class_type
Definition members.h:50
Definition members.h:14
Definition members.h:100
static constexpr bool is_valid_for
Definition members.h:104
Definition members.h:68
constexpr Member(T accessor)
Definition members.h:76
typename accessor::type type
Definition members.h:70
constexpr decltype(auto) operator()(Obj &&obj) noexcept
Definition members.h:81
const_string< N > name
Definition members.h:74
typename accessor::class_type class_type
Definition members.h:71
constexpr Member(T accessor, auto name_)
Definition members.h:77
Accessor< T > value
Definition members.h:73
Definition list.h:67
Definition const_string.h:9