forked from martong/access_private
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest.cpp
More file actions
353 lines (298 loc) · 9.19 KB
/
test.cpp
File metadata and controls
353 lines (298 loc) · 9.19 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "../include/access_private.hpp"
#include <cstdio>
#include <cstdlib>
#include <string>
#define ASSERT(CONDITION) \
do \
if (!(CONDITION)) { \
printf("Assertion failure %s:%d ASSERT(%s)\n", __FILE__, __LINE__, \
#CONDITION); \
abort(); \
} \
while (0)
class A {
int m_i = 3;
int m_f(int p) { return 14 * p; }
int m_f2(int&& p) { return 14 * p; }
constexpr int m_cxf(int p) const { return p * m_i; }
static int s_i;
static const int s_ci = 403;
static constexpr const int s_cxi = 42;
static int s_f(int r) { return r + 1; }
constexpr static int s_cxf(int r) { return r * 2; }
public:
const int &get_m_i() const { return m_i; }
static const int &get_s_i() { return s_i; }
};
int A::s_i = 404;
// Because we are using a pointer in the implementation, we need to explicitly
// define the const static variable as well, otherwise we'll have linker error.
const int A::s_ci;
template struct access_private::access<&A::m_i>;
void test_access_private_in_lvalue_expr() {
A a;
auto &i = access_private::accessor<"m_i">(a);
ASSERT(i == 3);
++i;
ASSERT(a.get_m_i() == 4);
}
void test_access_private_in_prvalue_expr() {
auto i = access_private::accessor<"m_i">(A{});
ASSERT(i == 3);
}
void test_access_private_in_xvalue_expr() {
A a;
auto i = access_private::accessor<"m_i">(std::move(a));
ASSERT(i == 3);
}
namespace NS {
class B {
int m_i = 3;
public:
class C {
int m_i = 4;
};
};
} // NS
namespace access_private {
template struct access<&NS::B::m_i>;
template struct access<&NS::B::C::m_i>;
}
void test_access_private_in_class_in_namespace() {
NS::B b;
auto &i = access_private::accessor<"m_i">(b);
ASSERT(i == 3);
}
void test_access_private_in_nested_class() {
NS::B::C c;
auto &i = access_private::accessor<"m_i">(c);
ASSERT(i == 4);
}
class C {
const int m_i = 3;
};
namespace access_private {
template struct access<&C::m_i>;
}
void test_access_private_const_member() {
C c;
auto &i = access_private::accessor<"m_i">(c);
// should not deduce to int&
static_assert(std::is_same<const int &, decltype(i)>::value, "");
ASSERT(i == 3);
}
class CA {
int m_i = 3;
public:
CA() {}
};
namespace access_private {
template struct access<&CA::m_i>;
}
void test_access_private_const_object() {
const CA ca;
auto &i = access_private::accessor<"m_i">(ca);
// should not deduce to int&
static_assert(std::is_same<const int &, decltype(i)>::value, "");
ASSERT(i == 3);
}
template <typename T>
class TemplateA {
T m_i = 3;
};
namespace access_private {
template struct access<&TemplateA<int>::m_i>;
}
void test_access_private_template_field() {
TemplateA<int> a;
auto &i = access_private::accessor<"m_i">(a);
ASSERT(i == 3);
}
void test_access_private_constexpr() {
constexpr A a;
static_assert(access_private::accessor<"m_i">(a) == 3, "");
}
namespace access_private {
template struct access<&A::m_f>;
// this will makes to work with lvalue int argument too
constexpr decltype(auto) call(accessor_t<"m_f">, A&, int);
}
void test_call_private_in_lvalue_expr() {
A a;
int p = 3;
auto res = access_private::accessor<"m_f">(a, p);
ASSERT(res == 42);
}
void test_call_private_in_prvalue_expr() {
auto res = access_private::accessor<"m_f">(A{}, 3);
ASSERT(res == 42);
}
void test_call_private_in_xvalue_expr() {
A a;
auto res = access_private::accessor<"m_f">(std::move(a), 3);
ASSERT(res == 42);
}
namespace access_private {
template struct access<&A::m_f2>;
}
void test_call_private_xvalue_parameter_through_explicit_parameters() {
A a;
auto res = access_private::accessor<"m_f2", A&, int&&>(a, 3);
ASSERT(res == 42);
}
namespace access_private {
template struct access<&A::m_cxf>;
}
void test_call_private_constexpr() {
constexpr A a;
static_assert(access_private::accessor<"m_cxf">(a, 5) == 15, "");
}
namespace access_private {
template struct access<&A::s_i, A>;
}
void test_access_private_static() {
auto &i = access_private::accessor<"s_i", A>();
ASSERT(i == 404);
++i;
ASSERT(A::get_s_i() == 405);
}
namespace access_private {
template struct access<&A::s_ci, A>;
}
void test_access_private_static_const() {
auto &i = access_private::accessor<"s_ci", A>();
static_assert(std::is_same<const int &, decltype(i)>::value, "");
ASSERT(i == 403);
}
namespace access_private {
template struct access<&A::s_cxi, A>;
}
void test_access_private_static_constexpr() {
static_assert(access_private::accessor<"s_cxi", A>() == 42, "");
}
namespace access_private {
// older GCC will not work the "expected" static function call
// for this reason we need to use the call_static_function_with
#if not defined(__clang__) and defined (__GNUC__)
call_static_function_with(A, s_f, int);
#else
template struct access_private::access<&A::s_f, A>;
#endif
}
void test_call_private_static() {
auto l = access_private::accessor<"s_f", A>(4);
ASSERT(l == 5);
}
namespace access_private {
// older GCC will not work the "expected" static function call
// for this reason we need to use the call_static_function_with
#if not defined(__clang__) and defined (__GNUC__)
call_static_function_with(A, s_cxf, int);
#else
template struct access<&A::s_cxf, A>;
#endif
}
void test_call_private_static_constexpr() {
static_assert(access_private::accessor<"s_cxf", A>(5) == 10, "");
}
class A3 {
int m_i = 3;
int m_f(int x) { return x + m_i; }
int m_f(int x, int y) { return x + y * m_i; }
template <typename T>
constexpr auto m_cxf(T x) const -> decltype(x + m_i) {
return x + m_i;
}
template <typename T, typename U>
static auto s_f(T t, U u) -> decltype(t + u) {
return t + u;
}
constexpr static const char nums[] = "0123456789";
constexpr static char s_cxf(int a) {
return nums[a];
}
constexpr static const char* s_cxf(float f) {
return nums + static_cast<int>(f * sizeof(nums) / sizeof(nums[0]));
}
};
#include "../include/overload.hpp"
namespace access_private {
template struct access<overload<int>(&A3::m_f)>;
template struct access<overload<int, int>(&A3::m_f)>;
}
void test_call_private_overloaded() {
auto res = access_private::accessor<"m_f">(A3(), 1);
ASSERT(res == 4);
res = access_private::accessor<"m_f">(A3(), 1, 2);
ASSERT(res == 7);
}
using const_a3 = const A3;
namespace access_private {
template struct access<&A3::m_cxf<int>>;
template struct access<&A3::m_cxf<const char*>>;
}
void test_call_private_overloaded_constexpr() {
constexpr A3 a3;
static_assert(access_private::accessor<"m_cxf">(a3, 10) == 13, "");
constexpr const char data[] = "hello world";
static_assert(access_private::accessor<"m_cxf">(a3, &*data) == data+3, "");
}
namespace access_private {
// older GCC will not work the "expected" static function call
// for this reason we need to use the call_static_function_with
#if not defined(__clang__) and defined (__GNUC__)
call_static_function_with(A3, s_f, char, int);
call_static_function_with(A3, s_f, const char*, std::string);
#else
template struct access<&A3::s_f<char, int>, A3>;
template struct access<&A3::s_f<const char*, std::string>, A3>;
#endif
}
void test_call_private_overloaded_static() {
auto c = access_private::accessor<"s_f", A3>('A', 25);
ASSERT(c == 'Z');
auto s = access_private::accessor<"s_f", A3>(&*"Hello", std::string{"World"});
ASSERT(s == "HelloWorld");
}
namespace access_private {
// older GCC will not work the "expected" static function call
// for this reason we need to use the call_static_function_with
#if not defined(__clang__) and defined (__GNUC__)
call_static_function_with(A3, s_cxf, int);
call_static_function_with(A3, s_cxf, float);
#else
template struct access<overload<int>(&A3::s_cxf), A3>;
template struct access<overload<float>(&A3::s_cxf), A3>;
#endif
}
void test_call_private_overloaded_static_constexpr() {
static_assert(access_private::accessor<"s_cxf", A3>(3) == '3', "");
static_assert(*access_private::accessor<"s_cxf", A3>(0.5f) == '5', "");
}
int main() {
test_access_private_in_lvalue_expr();
test_access_private_in_prvalue_expr();
test_access_private_in_xvalue_expr();
test_access_private_in_class_in_namespace();
test_access_private_in_nested_class();
test_access_private_const_member();
test_access_private_const_object();
test_access_private_template_field();
test_access_private_constexpr();
test_call_private_in_prvalue_expr();
test_call_private_in_xvalue_expr();
test_call_private_in_lvalue_expr();
test_call_private_xvalue_parameter_through_explicit_parameters();
test_call_private_constexpr();
test_access_private_static();
test_access_private_static_const();
test_access_private_static_constexpr();
test_call_private_static();
test_call_private_static_constexpr();
test_call_private_overloaded();
test_call_private_overloaded_constexpr();
test_call_private_overloaded_static();
test_call_private_overloaded_static_constexpr();
printf("OK\n");
return 0;
}