-
Notifications
You must be signed in to change notification settings - Fork 456
/
fun_test.hpp
122 lines (104 loc) · 2.92 KB
/
fun_test.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <cstddef>
#include <string>
#include <utility>
#include <iostream>
#include <sstream>
template<typename T, T mPtr>
struct MemberBinding
{
constexpr static T value = mPtr;
};
template<typename... Args>
struct Pack
{
constexpr static const std::size_t value = sizeof...(Args);
};
template<typename...> using void_t = void;
template<typename> struct Members {};
struct SubAg
{
double value;
};
template<>
struct Members<SubAg>
{
using type = Pack<MemberBinding<decltype(&SubAg::value), &SubAg::value>>;
constexpr static const size_t value = type::value;
constexpr static const char *name = "SubAg";
static const char *const *names()
{
static const char *rv[] = { "value" };
return rv;
}
};
struct Aggregate
{
int member1;
SubAg sa;
std::string member2;
};
template<>
struct Members<Aggregate>
{
using type = Pack<
MemberBinding<decltype(&Aggregate::member1), &Aggregate::member1>,
MemberBinding<decltype(&Aggregate::sa), &Aggregate::sa>,
MemberBinding<decltype(&Aggregate::member2), &Aggregate::member2>
>;
constexpr static const size_t value = type::value;
constexpr static const char *name = "Aggregate";
static const char *const *names()
{
static const char *rv[] = { "member1", "sa", "member2" };
return rv;
}
};
/// Overload for T where there is a Members<T> specialization
template<typename T>
void to_str_impl(std::ostream &out, const T &v, void_t<typename Members<T>::type> *);
/// Less specific overload
template<typename T>
void to_str_impl(std::ostream &out, const T &v, ...)
{
out << v;
}
/// General case
template<typename A, typename MT, MT MPTR, typename... Rest,std::size_t I0, std::size_t... I>
void printPack(std::ostream &out, const A &v, Pack<MemberBinding<MT, MPTR>, Rest...> *, std::index_sequence<I0, I...>)
{
using M = Members<A>;
out << ' ' << I0 << ':' << M::names()[I0] << ':';
to_str_impl(out, v.*MPTR, (void *)nullptr);
printPack(out, v, (Pack<Rest...> *)nullptr, std::index_sequence<I...>{});
}
template<typename A>
void printPack(std::ostream &out, const A &v, Pack<> *,std::index_sequence<>)
{
}
template<typename T>
void to_str_impl(std::ostream &out, const T &v, void_t<typename Members<T>::type> *)
{
using M = Members<T>;
out << '(' << M::name;
printPack(out, v, (typename M::type *)nullptr, std::make_index_sequence<Members<T>::value>{});
out << ')';
}
template<typename T>
std::string to_str(const T &v)
{
std::ostringstream prv;
to_str_impl(prv, v, (void *)nullptr);
return prv.str();
}
std::string fun(Aggregate &a) { return to_str(a); }
//get field by index, name;get field name and method name; invoke by method name;
template <size_t I, typename T>
decltype(auto) get(const T &v)
{
}
int main(int argc, const char *argv[])
{
Aggregate a{ 5, { 3.14 }, "Bellevue" };
std::cout << to_str(a) << std::endl;
return 0;
}