Debian Codegraph @

tests/src/addr_pri.h

raw symbols SCIP
1 #ifndef __ADDR_PRI_H__
2 #define __ADDR_PRI_H__
3
4 #include <utility>
5 #include <type_traits>
6
7 //base on C++11
8
9 /**********************************************************
10 access private function
11 **********************************************************/
12
13 namespace std {
14 template<bool B, class T = void>
15 using enable_if_t = typename enable_if<B, T>::type;
16 template<class T>
17 using remove_reference_t = typename remove_reference<T>::type;
18 } // namespace std
19
20 // Unnamed namespace is used to avoid duplicate symbols if the macros are used
21 namespace {
22 namespace private_access_detail {
23
24 // @tparam TagType, used to declare different "get" funciton overloads for
25 // different members/statics
26 template<typename PtrType, PtrType PtrValue, typename TagType>
27 struct private_access {
28 // Normal lookup cannot find in-class defined (inline) friend functions.
29 friend PtrType get(TagType) { return PtrValue; }
30 };
31
32 } // namespace private_access_detail
33 } // namespace
34
35 // Used macro naming conventions:
36 // The "namespace" of this macro library is PRIVATE_ACCESS, i.e. all
37 // macro here has this prefix.
38 // All implementation macro, which are not meant to be used directly have the
39 // PRIVATE_ACCESS_DETAIL prefix.
40 // Some macros have the ABCD_IMPL form, which means they contain the
41 // implementation details for the specific ABCD macro.
42
43 #define PRIVATE_ACCESS_DETAIL_CONCATENATE_IMPL(x, y) x##y
44 #define PRIVATE_ACCESS_DETAIL_CONCATENATE(x, y) \
45 PRIVATE_ACCESS_DETAIL_CONCATENATE_IMPL(x, y)
46
47 // @param PtrTypeKind E.g if we have "class A", then it can be "A::*" in case of
48 // members, or it can be "*" in case of statics.
49 #define PRIVATE_ACCESS_DETAIL_ACCESS_PRIVATE(Tag, Class, Type, Name, \
50 PtrTypeKind) \
51 namespace { \
52 namespace private_access_detail { \
53 /* Tag type, used to declare different get funcitons for different \
54 * members \
55 */ \
56 struct Tag { \
57 }; \
58 /* Explicit instantiation */ \
59 template struct private_access<decltype(&Class::Name), &Class::Name, \
60 Tag>; \
61 /* We can build the PtrType only with two aliases */ \
62 /* E.g. using PtrType = int(int) *; would be illformed */ \
63 using PRIVATE_ACCESS_DETAIL_CONCATENATE(Alias_, Tag) = Type; \
64 using PRIVATE_ACCESS_DETAIL_CONCATENATE(PtrType_, Tag) = \
65 PRIVATE_ACCESS_DETAIL_CONCATENATE(Alias_, Tag) PtrTypeKind; \
66 /* Declare the friend function, now it is visible in namespace scope. \
67 * Note, \
68 * we could declare it inside the Tag type too, in that case ADL would \
69 * find \
70 * the declaration. By choosing to declare it here, the Tag type remains \
71 * a \
72 * simple tag type, it has no other responsibilities. */ \
73 PRIVATE_ACCESS_DETAIL_CONCATENATE(PtrType_, Tag) \
74 get(Tag); \
75 } \
76 }
77
78 #define PRIVATE_ACCESS_DETAIL_ACCESS_PRIVATE_FIELD(Tag, Class, Type, Name) \
79 PRIVATE_ACCESS_DETAIL_ACCESS_PRIVATE(Tag, Class, Type, Name, Class::*) \
80 namespace { \
81 namespace access_private_field { \
82 Type &Class##Name(Class &&t) { return t.*get(private_access_detail::Tag {}); } \
83 Type &Class##Name(Class &t) { return t.*get(private_access_detail::Tag {}); } \
84 /* The following usings are here to avoid duplicate const qualifier \
85 * warnings \
86 */ \
87 using PRIVATE_ACCESS_DETAIL_CONCATENATE(X, Tag) = Type; \
88 using PRIVATE_ACCESS_DETAIL_CONCATENATE(Y, Tag) = \
89 const PRIVATE_ACCESS_DETAIL_CONCATENATE(X, Tag); \
90 PRIVATE_ACCESS_DETAIL_CONCATENATE(Y, Tag) & Class##Name(const Class &t) \
91 { \
92 return t.*get(private_access_detail::Tag {}); \
93 } \
94 } \
95 }
96
97 #define PRIVATE_ACCESS_DETAIL_ACCESS_PRIVATE_FUN(Tag, Class, Type, Name) \
98 PRIVATE_ACCESS_DETAIL_ACCESS_PRIVATE(Tag, Class, Type, Name, Class::*) \
99 namespace { \
100 namespace call_private_fun { \
101 /* We do perfect forwarding, but we want to restrict the overload set \
102 * only for objects which have the type Class. */ \
103 template<typename Obj, \
104 std::enable_if_t<std::is_same<std::remove_reference_t<Obj>, \
105 Class>::value> * = nullptr, \
106 typename... Args> \
107 auto Class##Name(Obj &&o, Args &&... args) -> decltype( \
108 (std::forward<Obj>(o).*get(private_access_detail::Tag {}))(std::forward<Args>(args)...)) \
109 { \
110 return (std::forward<Obj>(o).*get(private_access_detail::Tag {}))( \
111 std::forward<Args>(args)...); \
112 } \
113 } \
114 namespace get_private_fun { \
115 auto Class##Name() -> decltype( \
116 get(private_access_detail::Tag {})) \
117 { \
118 return (get(private_access_detail::Tag {})); \
119 } \
120 } \
121 }
122
123 #define PRIVATE_ACCESS_DETAIL_ACCESS_PRIVATE_STATIC_FIELD(Tag, Class, Type, \
124 Name) \
125 PRIVATE_ACCESS_DETAIL_ACCESS_PRIVATE(Tag, Class, Type, Name, *) \
126 namespace { \
127 namespace access_private_static_field { \
128 namespace Class { \
129 Type &Class##Name() { return *get(private_access_detail::Tag {}); } \
130 } \
131 } \
132 }
133
134 #define PRIVATE_ACCESS_DETAIL_ACCESS_PRIVATE_STATIC_FUN(Tag, Class, Type, \
135 Name) \
136 PRIVATE_ACCESS_DETAIL_ACCESS_PRIVATE(Tag, Class, Type, Name, *) \
137 namespace { \
138 namespace call_private_static_fun { \
139 namespace Class { \
140 template<typename... Args> \
141 auto Class##Name(Args &&... args) -> decltype( \
142 get(private_access_detail::Tag {})(std::forward<Args>(args)...)) \
143 { \
144 return get(private_access_detail::Tag {})( \
145 std::forward<Args>(args)...); \
146 } \
147 } \
148 } \
149 namespace get_private_static_fun { \
150 namespace Class { \
151 auto Class##Name() -> decltype(get(private_access_detail::Tag {})) \
152 { \
153 return get(private_access_detail::Tag {}); \
154 } \
155 } \
156 } \
157 }
158
159 #define PRIVATE_ACCESS_DETAIL_UNIQUE_TAG \
160 PRIVATE_ACCESS_DETAIL_CONCATENATE(PrivateAccessTag, __COUNTER__)
161
162 #define ACCESS_PRIVATE_FIELD(Class, Type, Name) \
163 PRIVATE_ACCESS_DETAIL_ACCESS_PRIVATE_FIELD(PRIVATE_ACCESS_DETAIL_UNIQUE_TAG, \
164 Class, Type, Name)
165
166 #define ACCESS_PRIVATE_FUN(Class, Type, Name) \
167 PRIVATE_ACCESS_DETAIL_ACCESS_PRIVATE_FUN(PRIVATE_ACCESS_DETAIL_UNIQUE_TAG, \
168 Class, Type, Name)
169
170 #define ACCESS_PRIVATE_STATIC_FIELD(Class, Type, Name) \
171 Type Class::Name; \
172 PRIVATE_ACCESS_DETAIL_ACCESS_PRIVATE_STATIC_FIELD( \
173 PRIVATE_ACCESS_DETAIL_UNIQUE_TAG, Class, Type, Name)
174
175 #define ACCESS_PRIVATE_STATIC_FUN(Class, Type, Name) \
176 PRIVATE_ACCESS_DETAIL_ACCESS_PRIVATE_STATIC_FUN( \
177 PRIVATE_ACCESS_DETAIL_UNIQUE_TAG, Class, Type, Name)
178
179 #endif