-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrivateAccessor.hpp
91 lines (76 loc) · 3.23 KB
/
PrivateAccessor.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
#ifndef PRIVATEACCESSOR_HPP
#define PRIVATEACCESSOR_HPP
namespace PrivateAccess
{
template <typename Class_t, typename Member_t, Member_t Class_t::* Member>
struct PropertyAccessor
{
using Flag_t = Member_t*;
friend Member_t& access(Class_t& obj, Flag_t)
{
return obj.*Member;
}
};
template <typename T>
constexpr static T* Flag = nullptr;
#ifdef DECLARE_PROPERTY_ACCESSOR
#error DECLARE_PROPERTY_ACCESSOR macro redefined!
#endif
#define DECLARE_PROPERTY_ACCESSOR(CLASS_TYPE, PROPERTY_TYPE, PROPERTY_NAME) \
namespace PrivateAccess \
{ \
template class PropertyAccessor<CLASS_TYPE, PROPERTY_TYPE, &CLASS_TYPE::PROPERTY_NAME>; \
PROPERTY_TYPE& access(CLASS_TYPE&, PROPERTY_TYPE*); \
}
template <typename Class_t, typename Ret_t, Ret_t(Class_t::*Member)()>
struct FunctionAccessorWith0Parameter
{
using Flag_t = Ret_t*;
friend Ret_t call(Class_t& obj, Flag_t)
{
if constexpr (std::is_same_v<Ret_t, void>)
{
(obj.*Member)();
}
else
{
return (obj.*Member)();
}
}
};
#ifdef DECLARE_FUNCTION_ACCESSOR_With_0_ARGS
#error DECLARE_FUNCTION_ACCESSOR_With_0_ARGS macro redefined!
#endif
#define DECLARE_FUNCTION_ACCESSOR_With_0_ARGS(CLASS_TYPE, RETURN_TYPE, FUNCTION_NAME) \
namespace PrivateAccess \
{ \
template class FunctionAccessorWith0Parameter<CLASS_TYPE, RETURN_TYPE, &CLASS_TYPE::FUNCTION_NAME>; \
RETURN_TYPE call(CLASS_TYPE&, RETURN_TYPE*); \
}
template <typename Class_t, typename Ret_t, typename Parameter_t, Ret_t(Class_t::*Member)(Parameter_t)>
struct FunctionAccessorWith1Parameter
{
using Flag_t = Ret_t*;
friend Ret_t call(Class_t& obj, Parameter_t parameter, Flag_t)
{
if constexpr (std::is_same_v<Ret_t, void>)
{
(obj.*Member)(parameter);
}
else
{
return (obj.*Member)(parameter);
}
}
};
#ifdef DECLARE_FUNCTION_ACCESSOR_With_1_ARGS
#error DECLARE_FUNCTION_ACCESSOR_With_1_ARGS macro redefined!
#endif
#define DECLARE_FUNCTION_ACCESSOR_With_1_ARGS(CLASS_TYPE, RETURN_TYPE, PARAMETER_TYPE, FUNCTION_NAME) \
namespace PrivateAccess \
{ \
template class FunctionAccessorWith1Parameter<CLASS_TYPE, RETURN_TYPE, PARAMETER_TYPE, &CLASS_TYPE::FUNCTION_NAME>; \
RETURN_TYPE call(CLASS_TYPE&, PARAMETER_TYPE, RETURN_TYPE*); \
}
}
#endif