Debian Codegraph @

tests/stub-tool/cpp-stub/addr_pri.h

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