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
|
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <functional>
// INVOKE (f, t1, t2, ..., tN)
//------------------------------------------------------------------------------
// TESTING INVOKE(f, t1, t2, ..., tN)
// - Bullet 7 -- f(t2, ..., tN)
//
// Overview:
// Bullet 7 handles the cases where the first argument is not a member
// function.
//
// Concerns:
// 1) Different types of callable objects are supported. Including
// 1a) Free Function pointers and references.
// 1b) Classes which provide a call operator
// 1c) lambdas
// 2) The callable objects are perfect forwarded.
// 3) The arguments are perfect forwarded.
// 4) Signatures which include varargs are supported.
// 5) In C++03 3 extra arguments should be allowed.
//
// Plan:
// 1) Define a set of free functions, 'SF', and class types with call
// operators, 'SC', that address concerns 4 and 5. The free functions should
// return 'FunctionID::setUncheckedCall()' and the call operators should
// return 'MethodID::setUncheckedCall()'.
//
// 2) For each function 'f' in 'SF' and 'SC' attempt to call 'f'
// using the correct number of arguments and cv-ref qualifiers. Check that
// 'f' has been called using 'FunctionID::checkCall()' if 'f' is a free
// function and 'MethodID::checkCall()' otherwise.
#include <functional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
#include "invoke_helpers.h"
//==============================================================================
// freeFunction03 - A C++03 free function.
void*& freeFunction03() {
return FunctionPtrID<void*&(), freeFunction03>::setUncheckedCall();
}
void*& freeFunction03(...) {
return FunctionPtrID<void*&(...), freeFunction03>::setUncheckedCall();
}
template <class A0>
void*& freeFunction03(A0&) {
return FunctionPtrID<void*&(A0&), freeFunction03>::setUncheckedCall();
}
template <class A0>
void*& freeFunction03(A0&, ...) {
return FunctionPtrID<void*&(A0&, ...), freeFunction03>::setUncheckedCall();
}
template <class A0, class A1>
void*& freeFunction03(A0&, A1&) {
return FunctionPtrID<void*&(A0&, A1&), freeFunction03>::setUncheckedCall();
}
template <class A0, class A1>
void*& freeFunction03(A0&, A1&, ...) {
return FunctionPtrID<void*&(A0&, A1&, ...), freeFunction03>::setUncheckedCall();
}
template <class A0, class A1, class A2>
void*& freeFunction03(A0&, A1&, A2&) {
return FunctionPtrID<void*&(A0&, A1&, A2&), freeFunction03>::setUncheckedCall();
}
template <class A0, class A1, class A2>
void*& freeFunction03(A0&, A1&, A2&, ...) {
return FunctionPtrID<void*&(A0&, A1&, A2&, ...), freeFunction03>::setUncheckedCall();
}
//==============================================================================
// Functor03 - C++03 compatible functor object
struct Functor03 {
typedef void*& R;
typedef Functor03 C;
#define F(Args, ...) \
__VA_ARGS__ R operator() Args { return MethodID<R(C::*) Args>::setUncheckedCall(); } \
__VA_ARGS__ R operator() Args const { return MethodID<R(C::*) Args const>::setUncheckedCall(); } \
__VA_ARGS__ R operator() Args volatile { return MethodID<R(C::*) Args volatile>::setUncheckedCall(); } \
__VA_ARGS__ R operator() Args const volatile { return MethodID<R(C::*) Args const volatile>::setUncheckedCall(); }
#
F(())
F((A0&), template <class A0>)
F((A0&, A1&), template <class A0, class A1>)
F((A0&, A1&, A2&), template <class A0, class A1, class A2>)
#undef F
public:
Functor03() {}
private:
Functor03(Functor03 const&);
Functor03& operator=(Functor03 const&);
};
#if TEST_STD_VER >= 11
//==============================================================================
// freeFunction11 - A C++11 free function.
template <class ...Args>
void*& freeFunction11(Args&&...) {
return FunctionPtrID<void*&(Args&&...), freeFunction11>::setUncheckedCall();
}
template <class ...Args>
void*& freeFunction11(Args&&...,...) {
return FunctionPtrID<void*&(Args&&...,...), freeFunction11>::setUncheckedCall();
}
//==============================================================================
// Functor11 - C++11 reference qualified test member functions.
struct Functor11 {
typedef void*& R;
typedef Functor11 C;
#define F(CV) \
template <class ...Args> \
R operator()(Args&&...) CV { return MethodID<R(C::*)(Args&&...) CV>::setUncheckedCall(); }
#
F(&)
F(const &)
F(volatile &)
F(const volatile &)
F(&&)
F(const &&)
F(volatile &&)
F(const volatile &&)
#undef F
public:
Functor11() {}
private:
Functor11(Functor11 const&);
Functor11& operator=(Functor11 const&);
};
#endif // TEST_STD_VER >= 11
//==============================================================================
// TestCaseFunctorImp - A test case for an operator() class method.
// ClassType - The type of the call object.
// CallSig - The function signature of the call operator being tested.
// Arity - the arity of 'CallSig'
// ObjCaster - Transformation function applied to call object.
// ArgCaster - Transformation function applied to the extra arguments.
template <class ClassType, class CallSig, int Arity,
class ObjCaster, class ArgCaster = LValueCaster>
struct TestCaseFunctorImp {
public:
static void run() {
typedef MethodID<CallSig ClassType::*> MID;
BasicTest<MID, Arity, ObjCaster, ArgCaster> t;
typedef ClassType T;
typedef DerivedFromType<T> D;
T obj;
D der;
t.runTest(obj);
t.runTest(der);
}
};
//==============================================================================
// TestCaseFreeFunction - A test case for a free function.
// CallSig - The function signature of the free function being tested.
// FnPtr - The function being tested.
// Arity - the arity of 'CallSig'
// ArgCaster - Transformation function to be applied to the extra arguments.
template <class CallSig, CallSig* FnPtr, int Arity, class ArgCaster>
struct TestCaseFreeFunction {
public:
static void run() {
typedef FunctionPtrID<CallSig, FnPtr> FID;
BasicTest<FID, Arity, LValueCaster, ArgCaster> t;
DerefToType<CallSig*> deref_to(FnPtr);
DerefToType<CallSig&> deref_to_ref(*FnPtr);
t.runTest(FnPtr);
t.runTest(*FnPtr);
t.runTest(deref_to);
t.runTest(deref_to_ref);
}
};
//==============================================================================
// runTest Helpers
//==============================================================================
#if TEST_STD_VER >= 11
template <class Sig, int Arity, class ArgCaster>
void runFunctionTestCase11() {
TestCaseFreeFunction<Sig, freeFunction11, Arity, ArgCaster>();
}
#endif
template <class Sig, int Arity, class ArgCaster>
void runFunctionTestCase() {
TestCaseFreeFunction<Sig, freeFunction03, Arity, ArgCaster>();
#if TEST_STD_VER >= 11
runFunctionTestCase11<Sig, Arity, ArgCaster>();
#endif
}
template <class Sig, int Arity, class ObjCaster, class ArgCaster>
void runFunctorTestCase() {
TestCaseFunctorImp<Functor03, Sig, Arity, ObjCaster, ArgCaster>::run();
}
template <class Sig, int Arity, class ObjCaster>
void runFunctorTestCase() {
TestCaseFunctorImp<Functor03, Sig, Arity, ObjCaster>::run();
}
#if TEST_STD_VER >= 11
// runTestCase - Run a test case for C++11 class functor types
template <class Sig, int Arity, class ObjCaster, class ArgCaster = LValueCaster>
void runFunctorTestCase11() {
TestCaseFunctorImp<Functor11, Sig, Arity, ObjCaster, ArgCaster>::run();
}
#endif
// runTestCase - Run a test case for both function and functor types.
template <class Sig, int Arity, class ArgCaster>
void runTestCase() {
runFunctionTestCase<Sig, Arity, ArgCaster>();
runFunctorTestCase <Sig, Arity, LValueCaster, ArgCaster>();
};
int main() {
typedef void*& R;
typedef ArgType A;
typedef A const CA;
runTestCase< R(), 0, LValueCaster >();
runTestCase< R(A&), 1, LValueCaster >();
runTestCase< R(A&, A&), 2, LValueCaster >();
runTestCase< R(A&, A&, A&), 3, LValueCaster >();
runTestCase< R(CA&), 1, ConstCaster >();
runTestCase< R(CA&, CA&), 2, ConstCaster >();
runTestCase< R(CA&, CA&, CA&), 3, ConstCaster >();
runFunctionTestCase<R(...), 0, LValueCaster >();
runFunctionTestCase<R(A&, ...), 1, LValueCaster >();
runFunctionTestCase<R(A&, A&, ...), 2, LValueCaster >();
runFunctionTestCase<R(A&, A&, A&, ...), 3, LValueCaster >();
#if TEST_STD_VER >= 11
runFunctionTestCase11<R(A&&), 1, MoveCaster >();
runFunctionTestCase11<R(A&&, ...), 1, MoveCaster >();
#endif
runFunctorTestCase<R(), 0, LValueCaster >();
runFunctorTestCase<R() const, 0, ConstCaster >();
runFunctorTestCase<R() volatile, 0, VolatileCaster >();
runFunctorTestCase<R() const volatile, 0, CVCaster >();
runFunctorTestCase<R(A&), 1, LValueCaster >();
runFunctorTestCase<R(A&) const, 1, ConstCaster >();
runFunctorTestCase<R(A&) volatile, 1, VolatileCaster >();
runFunctorTestCase<R(A&) const volatile, 1, CVCaster >();
runFunctorTestCase<R(A&, A&), 2, LValueCaster >();
runFunctorTestCase<R(A&, A&) const, 2, ConstCaster >();
runFunctorTestCase<R(A&, A&) volatile, 2, VolatileCaster >();
runFunctorTestCase<R(A&, A&) const volatile, 2, CVCaster >();
runFunctorTestCase<R(A&, A&, A&), 3, LValueCaster >();
runFunctorTestCase<R(A&, A&, A&) const, 3, ConstCaster >();
runFunctorTestCase<R(A&, A&, A&) volatile, 3, VolatileCaster >();
runFunctorTestCase<R(A&, A&, A&) const volatile, 3, CVCaster >();
{
typedef ConstCaster CC;
runFunctorTestCase<R(CA&), 1, LValueCaster, CC>();
runFunctorTestCase<R(CA&) const, 1, ConstCaster, CC>();
runFunctorTestCase<R(CA&) volatile, 1, VolatileCaster, CC>();
runFunctorTestCase<R(CA&) const volatile, 1, CVCaster, CC>();
runFunctorTestCase<R(CA&, CA&), 2, LValueCaster, CC>();
runFunctorTestCase<R(CA&, CA&) const, 2, ConstCaster, CC>();
runFunctorTestCase<R(CA&, CA&) volatile, 2, VolatileCaster, CC>();
runFunctorTestCase<R(CA&, CA&) const volatile, 2, CVCaster, CC>();
runFunctorTestCase<R(CA&, CA&, CA&), 3, LValueCaster, CC>();
runFunctorTestCase<R(CA&, CA&, CA&) const, 3, ConstCaster, CC>();
runFunctorTestCase<R(CA&, CA&, CA&) volatile, 3, VolatileCaster, CC>();
runFunctorTestCase<R(CA&, CA&, CA&) const volatile, 3, CVCaster, CC>();
}
#if TEST_STD_VER >= 11
runFunctorTestCase11<R() &, 0, LValueCaster >();
runFunctorTestCase11<R() const &, 0, ConstCaster >();
runFunctorTestCase11<R() volatile &, 0, VolatileCaster >();
runFunctorTestCase11<R() const volatile &, 0, CVCaster >();
runFunctorTestCase11<R() &&, 0, MoveCaster >();
runFunctorTestCase11<R() const &&, 0, MoveConstCaster >();
runFunctorTestCase11<R() volatile &&, 0, MoveVolatileCaster >();
runFunctorTestCase11<R() const volatile &&, 0, MoveCVCaster >();
{
typedef MoveCaster MC;
runFunctorTestCase11<R(A&&) &, 1, LValueCaster, MC>();
runFunctorTestCase11<R(A&&) const &, 1, ConstCaster, MC>();
runFunctorTestCase11<R(A&&) volatile &, 1, VolatileCaster, MC>();
runFunctorTestCase11<R(A&&) const volatile &, 1, CVCaster, MC>();
runFunctorTestCase11<R(A&&) &&, 1, MoveCaster, MC>();
runFunctorTestCase11<R(A&&) const &&, 1, MoveConstCaster, MC>();
runFunctorTestCase11<R(A&&) volatile &&, 1, MoveVolatileCaster, MC>();
runFunctorTestCase11<R(A&&) const volatile &&, 1, MoveCVCaster, MC>();
}
#endif
}
|