-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
access
299 lines (237 loc) · 21.2 KB
/
access
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_DEBUG_ACCESS_HPP
#define GTL_DEBUG_ACCESS_HPP
// Summary: Classes and macros for accessing class private members.
#if defined(_MSC_VER)
# pragma warning(push, 0)
#endif
#include <functional>
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
namespace gtl {
namespace access {
/// @brief The connection template which when explicitly specialised routes a private member pointer to the return of a public friend function.
template <typename member_type, member_type member, typename connection_id>
class connection final {
friend member_type get_member(connection_id) {
return member;
}
};
/// @brief The target template provides helper types to handle conversion between the private members and new public members.
template <typename class_type, typename return_type, typename... arguments>
struct target;
/// @brief Specialisation of the target template for member variables.
template <typename class_type, typename return_type>
struct target<class_type, return_type> final {
using member_type = return_type class_type::*;
using connection_type = return_type&;
};
/// @brief Specialisation of the target template for static member variables.
template <typename return_type>
struct target<void, return_type> final {
using member_type = return_type*;
using connection_type = return_type&;
};
/// @brief Specialisation of the target template for member functions.
template <typename class_type, typename return_type, typename... arguments>
struct target<class_type, return_type(arguments...)> final {
using member_type = return_type(class_type::*)(arguments...);
using connection_type = std::function<return_type(arguments...)>;
};
/// @brief Specialisation of the target template for const member functions.
template <typename class_type, typename return_type, typename... arguments>
struct target<class_type, return_type(arguments...) const> final {
using member_type = return_type(class_type::*)(arguments...) const;
using connection_type = std::function<return_type(arguments...)>;
};
/// @brief Specialisation of the target template for volatile member functions.
template <typename class_type, typename return_type, typename... arguments>
struct target<class_type, return_type(arguments...) volatile> final {
using member_type = return_type(class_type::*)(arguments...) volatile;
using connection_type = std::function<return_type(arguments...)>;
};
/// @brief Specialisation of the target template for volatile const member functions.
template <typename class_type, typename return_type, typename... arguments>
struct target<class_type, return_type(arguments...) volatile const> final {
using member_type = return_type(class_type::*)(arguments...) volatile const;
using connection_type = std::function<return_type(arguments...)>;
};
/// @brief Specialisation of the target template for static member functions.
template <typename return_type, typename... arguments>
struct target<void, return_type(arguments...)> final {
using member_type = return_type(*)(arguments...);
using connection_type = std::function<return_type(arguments...)>;
};
// Expansion macros to expand arguments 1024 times.
#define GTL_ACCESS_EXPAND(...) GTL_ACCESS_EXPAND4(GTL_ACCESS_EXPAND4(GTL_ACCESS_EXPAND4(GTL_ACCESS_EXPAND4(__VA_ARGS__))))
#define GTL_ACCESS_EXPAND4(...) GTL_ACCESS_EXPAND3(GTL_ACCESS_EXPAND3(GTL_ACCESS_EXPAND3(GTL_ACCESS_EXPAND3(__VA_ARGS__))))
#define GTL_ACCESS_EXPAND3(...) GTL_ACCESS_EXPAND2(GTL_ACCESS_EXPAND2(GTL_ACCESS_EXPAND2(GTL_ACCESS_EXPAND2(__VA_ARGS__))))
#define GTL_ACCESS_EXPAND2(...) GTL_ACCESS_EXPAND1(GTL_ACCESS_EXPAND1(GTL_ACCESS_EXPAND1(GTL_ACCESS_EXPAND1(__VA_ARGS__))))
#define GTL_ACCESS_EXPAND1(...) GTL_ACCESS_EXPAND0(GTL_ACCESS_EXPAND0(GTL_ACCESS_EXPAND0(GTL_ACCESS_EXPAND0(__VA_ARGS__))))
#define GTL_ACCESS_EXPAND0(...) __VA_ARGS__
#define GTL_ACCESS_FOR_EACH_END(...)
#define GTL_ACCESS_FOR_EACH_OUT
#define GTL_ACCESS_EMPTY()
#define GTL_ACCESS_DEFER(EXPRESSION) EXPRESSION GTL_ACCESS_EMPTY()
#define GTL_ACCESS_FOR_EACH_GET_END(...) GTL_ACCESS_FOR_EACH_GET_END_EXPAND
#define GTL_ACCESS_FOR_EACH_GET_END_EXPAND(...) GTL_ACCESS_FOR_EACH_GET_END_EXPANDED
#define GTL_ACCESS_FOR_EACH_GET_END_EXPANDED() 0, GTL_ACCESS_FOR_EACH_END
#define GTL_ACCESS_FOR_EACH_NEXT(TEST, SEPARATOR, NEXT) GTL_ACCESS_FOR_EACH_NEXT_EXPAND(GTL_ACCESS_FOR_EACH_GET_END TEST, SEPARATOR, NEXT)
#define GTL_ACCESS_FOR_EACH_NEXT_EXPAND(TEST, SEPARATOR, NEXT) GTL_ACCESS_DEFER(GTL_ACCESS_FOR_EACH_NEXT_EXPANDED(TEST, GTL_ACCESS_SEPARATOR_##SEPARATOR NEXT, 0))
#define GTL_ACCESS_FOR_EACH_NEXT_EXPANDED(TEST, NEXT, ...) NEXT GTL_ACCESS_FOR_EACH_OUT
#define GTL_ACCESS_FOR_EACH_PONG(FUNCTION, SEPARATOR, CLASS_NAME, ACCESS_TYPE, ACCESS_STORAGE, MEMBER_TYPE, MEMBER, NEXT, ...) \
FUNCTION##_##ACCESS_TYPE##_##ACCESS_STORAGE(CLASS_NAME, MEMBER_TYPE, MEMBER) \
GTL_ACCESS_FOR_EACH_NEXT(NEXT, SEPARATOR, GTL_ACCESS_FOR_EACH_PING)(FUNCTION, SEPARATOR, CLASS_NAME, NEXT, __VA_ARGS__)
#define GTL_ACCESS_FOR_EACH_PING(FUNCTION, SEPARATOR, CLASS_NAME, ACCESS_TYPE, ACCESS_STORAGE, MEMBER_TYPE, MEMBER, NEXT, ...) \
FUNCTION##_##ACCESS_TYPE##_##ACCESS_STORAGE(CLASS_NAME, MEMBER_TYPE, MEMBER) \
GTL_ACCESS_FOR_EACH_NEXT(NEXT, SEPARATOR, GTL_ACCESS_FOR_EACH_PONG)(FUNCTION, SEPARATOR, CLASS_NAME, NEXT, __VA_ARGS__)
// Macro to apply another macro to each set of parameters adding a provided separator between outputs.
#define GTL_ACCESS_FOR_EACH(FUNCTION, SEPARATOR, CLASS_NAME, ...) \
GTL_ACCESS_EXPAND(GTL_ACCESS_FOR_EACH_PING(FUNCTION, SEPARATOR, CLASS_NAME, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
// Available separator symbols.
#define GTL_ACCESS_SEPARATOR_GTL_ACCESS_COMMA ,
#define GTL_ACCESS_SEPARATOR_GTL_ACCESS_NOTHING
// Symbols for selecting the separator. Defined to enable syntax highlighting, and protect these symbols from being defined elsewhere.
#define GTL_ACCESS_COMMA GTL_ACCESS_COMMA
#define GTL_ACCESS_NOTHING GTL_ACCESS_NOTHING
// Symbols for defining inputs to the GTL_ACCESS_GENERATE macro. Defined to enable syntax highlighting, and protect these symbols from being defined elsewhere.
#define GTL_ACCESS_TYPE GTL_ACCESS_TYPE
#define GTL_ACCESS_FUNC GTL_ACCESS_FUNCTION
#define GTL_ACCESS_FUNCTION GTL_ACCESS_FUNCTION
// Generation of connection_id type names using the class name, member name and line number.
#define GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME) GTL_ACCESS_CONNECTION_ID_EXPAND(CLASS_NAME, MEMBER_NAME, __LINE__)
#define GTL_ACCESS_CONNECTION_ID_EXPAND(CLASS_NAME, MEMBER_NAME, LINE) GTL_ACCESS_CONNECTION_ID_EXPANDED(CLASS_NAME, MEMBER_NAME, LINE)
#define GTL_ACCESS_CONNECTION_ID_EXPANDED(CLASS_NAME, MEMBER_NAME, LINE) connection_##CLASS_NAME##_##MEMBER_NAME##LINE
// There are three stages of construction of the wrapping class which are performed for each private member.
// 1) CONNECT:
// - Define the unique connection_id for accessing this private member.
// - Declare the member accessor friend function using the unique connection_id.
// - Explicitly specialise the connection template with the private member.
// 2) GENERATE:
// - Define a public member reference that will reference the private member.
// 3) INITIALISE:
// - Point the public member reference at the private member.
// There are three storage classes allowed for members of a class which must each be handled:
// The storage classes allowed in classes are: automatic, static, and mutable.
// Connecting automatic member variables.
#define GTL_ACCESS_CONNECT_GTL_ACCESS_TYPE_auto(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
struct GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME) final {}; \
target<CLASS_NAME, MEMBER_TYPE>::member_type get_member(GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME)); \
template class connection<target<CLASS_NAME, MEMBER_TYPE>::member_type, &CLASS_NAME::MEMBER_NAME, GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME)>;
// Connecting static member variables.
#define GTL_ACCESS_CONNECT_GTL_ACCESS_TYPE_static(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
struct GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME) final {}; \
target<void, MEMBER_TYPE>::member_type get_member(GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME)); \
template class connection<target<void, MEMBER_TYPE>::member_type, &CLASS_NAME::MEMBER_NAME, GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME)>;
// Connecting mutable member variables.
#define GTL_ACCESS_CONNECT_GTL_ACCESS_TYPE_mutable(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
struct GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME) final {}; \
target<CLASS_NAME, MEMBER_TYPE>::member_type get_member(GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME)); \
template class connection<target<CLASS_NAME, MEMBER_TYPE>::member_type, &CLASS_NAME::MEMBER_NAME, GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME)>;
// Default to automatic if no storage class is given.
#define GTL_ACCESS_CONNECT_GTL_ACCESS_TYPE_(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
GTL_ACCESS_CONNECT_GTL_ACCESS_TYPE_auto(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME)
// Connecting automatic member functions.
#define GTL_ACCESS_CONNECT_GTL_ACCESS_FUNCTION_auto(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
struct GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME) final {}; \
target<CLASS_NAME, MEMBER_TYPE>::member_type get_member(GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME)); \
template class connection<target<CLASS_NAME, MEMBER_TYPE>::member_type, &CLASS_NAME::MEMBER_NAME, GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME)>;
// Connecting static member functions.
#define GTL_ACCESS_CONNECT_GTL_ACCESS_FUNCTION_static(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
struct GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME) final {}; \
target<void, MEMBER_TYPE>::member_type get_member(GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME)); \
template class connection<target<void, MEMBER_TYPE>::member_type, &CLASS_NAME::MEMBER_NAME, GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME)>;
// Connecting mutable member functions.
#define GTL_ACCESS_CONNECT_GTL_ACCESS_FUNCTION_mutable(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
struct GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME) final {}; \
target<CLASS_NAME, MEMBER_TYPE>::member_type get_member(GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME)); \
template class connection<target<CLASS_NAME, MEMBER_TYPE>::member_type, &CLASS_NAME::MEMBER_NAME, GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME)>;
// Default to automatic if no storage class is given.
#define GTL_ACCESS_CONNECT_GTL_ACCESS_FUNCTION_(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
GTL_ACCESS_CONNECT_GTL_ACCESS_FUNCTION_auto(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME)
// Generating automatic member variables.
#define GTL_ACCESS_GENERATE_GTL_ACCESS_TYPE_auto(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
gtl::access::target<CLASS_NAME, MEMBER_TYPE>::connection_type MEMBER_NAME;
// Generating static member variables.
#define GTL_ACCESS_GENERATE_GTL_ACCESS_TYPE_static(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
gtl::access::target<void, MEMBER_TYPE>::connection_type MEMBER_NAME;
// Generating mutable member variables.
#define GTL_ACCESS_GENERATE_GTL_ACCESS_TYPE_mutable(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
gtl::access::target<CLASS_NAME, MEMBER_TYPE>::connection_type MEMBER_NAME;
// Default to automatic if no storage class is given.
#define GTL_ACCESS_GENERATE_GTL_ACCESS_TYPE_(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
GTL_ACCESS_GENERATE_GTL_ACCESS_TYPE_auto(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME)
// Generating automatic member functions.
#define GTL_ACCESS_GENERATE_GTL_ACCESS_FUNCTION_auto(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
gtl::access::target<CLASS_NAME, MEMBER_TYPE>::connection_type MEMBER_NAME;
// Generating static member functions.
#define GTL_ACCESS_GENERATE_GTL_ACCESS_FUNCTION_static(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
gtl::access::target<void, MEMBER_TYPE>::connection_type MEMBER_NAME;
// Generating mutable member functions.
#define GTL_ACCESS_GENERATE_GTL_ACCESS_FUNCTION_mutable(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
gtl::access::target<CLASS_NAME, MEMBER_TYPE>::connection_type MEMBER_NAME;
// Default to automatic if no storage class is given.
#define GTL_ACCESS_GENERATE_GTL_ACCESS_FUNCTION_(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
GTL_ACCESS_GENERATE_GTL_ACCESS_FUNCTION_auto(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME)
// Initialising automatic member variables.
#define GTL_ACCESS_INITIALISE_GTL_ACCESS_TYPE_auto(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
MEMBER_NAME(instance->*get_member(gtl::access::GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME) {}))
// Initialising static member variables.
#define GTL_ACCESS_INITIALISE_GTL_ACCESS_TYPE_static(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
MEMBER_NAME(*get_member(gtl::access::GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME) {}))
// Initialising mutable member variables.
#define GTL_ACCESS_INITIALISE_GTL_ACCESS_TYPE_mutable(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
MEMBER_NAME(instance->*get_member(gtl::access::GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME) {}))
// Default to automatic if no storage class is given.
#define GTL_ACCESS_INITIALISE_GTL_ACCESS_TYPE_(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
GTL_ACCESS_INITIALISE_GTL_ACCESS_TYPE_auto(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME)
// Initialising automatic member functions.
#define GTL_ACCESS_INITIALISE_GTL_ACCESS_FUNCTION_auto(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
MEMBER_NAME([instance](auto&&... params) { \
return (instance->*get_member(gtl::access::GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME) {}))(std::forward<decltype(params)>(params)...); \
})
// Initialising static member functions.
#define GTL_ACCESS_INITIALISE_GTL_ACCESS_FUNCTION_static(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
MEMBER_NAME([/*instance*/](auto&&... params) { \
return (*get_member(gtl::access::GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME) {}))(std::forward<decltype(params)>(params)...); \
})
// Initialising mutable member functions.
#define GTL_ACCESS_INITIALISE_GTL_ACCESS_FUNCTION_mutable(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
MEMBER_NAME([instance](auto&&... params) { \
return (instance->*get_member(gtl::access::GTL_ACCESS_CONNECTION_ID(CLASS_NAME, MEMBER_NAME) {}))(std::forward<decltype(params)>(params)...); \
})
// Default to automatic if no storage class is given.
#define GTL_ACCESS_INITIALISE_GTL_ACCESS_FUNCTION_(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME) \
GTL_ACCESS_INITIALISE_GTL_ACCESS_FUNCTION_auto(CLASS_NAME, MEMBER_TYPE, MEMBER_NAME)
// Generate an access class for a given class name and members.
#define GTL_ACCESS_GENERATE(ACCESS_CLASS_NAME, CLASS_NAME, ...) \
namespace gtl { \
namespace access { \
GTL_ACCESS_FOR_EACH(GTL_ACCESS_CONNECT, GTL_ACCESS_NOTHING, CLASS_NAME, __VA_ARGS__) \
}} \
class ACCESS_CLASS_NAME final { \
public: \
GTL_ACCESS_FOR_EACH(GTL_ACCESS_GENERATE, GTL_ACCESS_NOTHING, CLASS_NAME, __VA_ARGS__) \
public: \
explicit ACCESS_CLASS_NAME(CLASS_NAME& instance) \
: ACCESS_CLASS_NAME(&instance) { \
} \
explicit ACCESS_CLASS_NAME(CLASS_NAME* instance) \
: GTL_ACCESS_FOR_EACH(GTL_ACCESS_INITIALISE, GTL_ACCESS_COMMA, CLASS_NAME, __VA_ARGS__) { \
} \
}
}}
#endif // GTL_DEBUG_ACCESS_HPP