aboutsummaryrefslogtreecommitdiff
path: root/test/libcxx/utilities/function.objects/func.require/invoke_helpers.h
blob: 7e7a5fd24a6275fd3066e863a9100e539bc8317e (plain) (blame)
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//===----------------------------------------------------------------------===//
//
//                     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.
//
//===----------------------------------------------------------------------===//

#ifndef INVOKE_HELPERS_H
#define INVOKE_HELPERS_H

#include <type_traits>
#include <cassert>
#include <functional>

#include "test_macros.h"

template <int I>
struct Int : public std::integral_constant<int, I> {};

template <bool P>
struct Bool : public std::integral_constant<bool, P> {};

struct Q_None {
    template <class T>
    struct apply { typedef T type; };
};

struct Q_Const {
    template <class T>
    struct apply { typedef T const type; };
};

struct Q_Volatile {
    template <class T>
    struct apply { typedef T volatile type; };
};

struct Q_CV {
    template <class T>
    struct apply { typedef T const volatile type; };
};

// Caster - A functor object that performs cv-qualifier and value category
//   conversions.
//   QualTag - A metafunction type that applies cv-qualifiers to its argument.
//   RValue - True if the resulting object should be an RValue reference.
//            False otherwise.
template <class QualTag, bool RValue = false>
struct Caster {
    template <class T>
    struct apply {
        typedef typename std::remove_reference<T>::type RawType;
        typedef typename QualTag::template apply<RawType>::type CVType;
#if TEST_STD_VER >= 11
        typedef typename std::conditional<RValue,
            CVType&&, CVType&
        >::type type;
#else
        typedef CVType& type;
#endif
    };

    template <class T>
    typename apply<T>::type
    operator()(T& obj) const {
        typedef typename apply<T>::type OutType;
        return static_cast<OutType>(obj);
    }
};

typedef Caster<Q_None>           LValueCaster;
typedef Caster<Q_Const>          ConstCaster;
typedef Caster<Q_Volatile>       VolatileCaster;
typedef Caster<Q_CV>             CVCaster;
typedef Caster<Q_None,     true> MoveCaster;
typedef Caster<Q_Const,    true> MoveConstCaster;
typedef Caster<Q_Volatile, true> MoveVolatileCaster;
typedef Caster<Q_CV,       true> MoveCVCaster;


template <class Tp>
Tp const& makeConst(Tp& ref) { return ref; }

template <class Tp>
Tp const* makeConst(Tp* ptr) { return ptr; }

template <class Tp>
std::reference_wrapper<const Tp> makeConst(std::reference_wrapper<Tp>& ref) {
    return std::reference_wrapper<const Tp>(ref.get());
}

template <class Tp>
Tp volatile& makeVolatile(Tp& ref) { return ref; }

template <class Tp>
Tp volatile* makeVolatile(Tp* ptr) { return ptr; }

template <class Tp>
std::reference_wrapper<volatile Tp> makeVolatile(std::reference_wrapper<Tp>& ref) {
    return std::reference_wrapper<volatile Tp>(ref.get());
}

template <class Tp>
Tp const volatile& makeCV(Tp& ref) { return ref; }

template <class Tp>
Tp const volatile* makeCV(Tp* ptr) { return ptr; }

template <class Tp>
std::reference_wrapper<const volatile Tp> makeCV(std::reference_wrapper<Tp>& ref) {
    return std::reference_wrapper<const volatile Tp>(ref.get());
}

// A shorter name for 'static_cast'
template <class QualType, class Tp>
QualType C_(Tp& v) { return static_cast<QualType>(v); };

//==============================================================================
// ArgType - A non-copyable type intended to be used as a dummy argument type
//   to test functions.
struct ArgType {
    int value;
    explicit ArgType(int val = 0) : value(val) {}
private:
    ArgType(ArgType const&);
    ArgType& operator=(ArgType const&);
};

//==============================================================================
// DerivedFromBase - A type that derives from it's template argument 'Base'
template <class Base>
struct DerivedFromType : public Base {
    DerivedFromType() : Base() {}
    template <class Tp>
    explicit DerivedFromType(Tp const& t) : Base(t) {}
};

//==============================================================================
// DerefToType - A type that dereferences to it's template argument 'To'.
//   The cv-ref qualifiers of the 'DerefToType' object do not propagate
//   to the resulting 'To' object.
template <class To>
struct DerefToType {
    To object;

    DerefToType() {}

    template <class Up>
    explicit DerefToType(Up const& val) : object(val) {}

    To& operator*() const volatile { return const_cast<To&>(object); }
};

//==============================================================================
// DerefPropToType - A type that dereferences to it's template argument 'To'.
//   The cv-ref qualifiers of the 'DerefPropToType' object propagate
//   to the resulting 'To' object.
template <class To>
struct DerefPropType {
    To object;

    DerefPropType() {}

    template <class Up>
    explicit DerefPropType(Up const& val) : object(val) {}

#if TEST_STD_VER < 11
    To& operator*() { return object; }
    To const& operator*() const { return object; }
    To volatile& operator*() volatile  { return object; }
    To const volatile& operator*() const volatile { return object; }
#else
    To& operator*() & { return object; }
    To const& operator*() const & { return object; }
    To volatile& operator*() volatile  & { return object; }
    To const volatile& operator*() const volatile & { return object; }
    To&& operator*() && { return static_cast<To &&>(object); }
    To const&& operator*() const && { return static_cast<To const&&>(object); }
    To volatile&& operator*() volatile  && { return static_cast<To volatile&&>(object); }
    To const volatile&& operator*() const volatile && { return static_cast<To const volatile&&>(object); }
#endif
};

//==============================================================================
// MethodID - A type that uniquely identifies a member function for a class.
//   This type is used to communicate between the member functions being tested
//   and the tests invoking them.
// - Test methods should call 'setUncheckedCall()' whenever they are invoked.
// - Tests consume the unchecked call using checkCall(<return-value>)` to assert
//   that the method has been called and that the return value of `__invoke`
//   matches what the method actually returned.
template <class T>
struct MethodID {
    typedef void* IDType;

    static int dummy; // A dummy memory location.
    static void* id; // The "ID" is the value of this pointer.
    static bool unchecked_call; // Has a call happened that has not been checked.

    static void*& setUncheckedCall() {
        assert(unchecked_call == false);
        unchecked_call = true;
        return id;
    }

    static bool checkCalled(void*& return_value) {
        bool old = unchecked_call;
        unchecked_call = false;
        return old && id == return_value && &id == &return_value;
    }
};

template <class T> int   MethodID<T>::dummy = 0;
template <class T> void* MethodID<T>::id = (void*)&MethodID<T>::dummy;
template <class T> bool  MethodID<T>::unchecked_call = false;


//==============================================================================
// FunctionPtrID - Like MethodID but for free function pointers.
template <class T, T*>
struct FunctionPtrID {
    static int dummy; // A dummy memory location.
    static void* id; // The "ID" is the value of this pointer.
    static bool unchecked_call; // Has a call happened that has not been checked.

    static void*& setUncheckedCall() {
        assert(unchecked_call == false);
        unchecked_call = true;
        return id;
    }

    static bool checkCalled(void*& return_value) {
        bool old = unchecked_call;
        unchecked_call = false;
        return old && id == return_value && &id == &return_value;
    }
};

template <class T, T* Ptr> int   FunctionPtrID<T, Ptr>::dummy = 0;
template <class T, T* Ptr> void* FunctionPtrID<T, Ptr>::id = (void*)&FunctionPtrID<T, Ptr>::dummy;
template <class T, T* Ptr> bool  FunctionPtrID<T, Ptr>::unchecked_call = false;

//==============================================================================
// BasicTest - The basic test structure for everything except
// member object pointers.
// ID - The "Function Identifier" type used either MethodID or FunctionPtrID.
// Arity - The Arity of the call signature.
// ObjectCaster - The object transformation functor type.
// ArgCaster - The extra argument transformation functor type.
template <class ID, int Arity, class ObjectCaster = LValueCaster,
                               class ArgCaster    = LValueCaster>
struct BasicTest {
    template <class ObjectT>
    void runTest(ObjectT& object) {
        Int<Arity> A;
        runTestImp(A, object);
    }

    template <class MethodPtr, class ObjectT>
    void runTest(MethodPtr ptr, ObjectT& object) {
        Int<Arity> A;
        runTestImp(A, ptr, object);
    }

private:
    typedef void*& CallRet;
    ObjectCaster object_cast;
    ArgCaster arg_cast;
    ArgType a0, a1, a2;

    //==========================================================================
    //                       BULLET 1, 2 AND 3 TEST METHODS
    //==========================================================================
    template <class MethodPtr, class ObjectT>
    void runTestImp(Int<0>, MethodPtr ptr, ObjectT& object) {
        {
            static_assert((std::is_same<
                decltype(std::__invoke(ptr, object_cast(object)))
              , CallRet>::value), "");
            assert(ID::unchecked_call == false);
            CallRet ret = std::__invoke(ptr, object_cast(object));
            assert(ID::checkCalled(ret));
        }
#if TEST_STD_VER >= 11
        {
            static_assert((std::is_same<
                decltype(std::__invoke_constexpr(ptr, object_cast(object)))
              , CallRet>::value), "");
            assert(ID::unchecked_call == false);
            CallRet ret = std::__invoke_constexpr(ptr, object_cast(object));
            assert(ID::checkCalled(ret));
        }
#endif
    }

    template <class MethodPtr, class ObjectT>
    void runTestImp(Int<1>, MethodPtr ptr, ObjectT& object) {
        {
            static_assert((std::is_same<
                decltype(std::__invoke(ptr, object_cast(object), arg_cast(a0)))
              , CallRet>::value), "");
            assert(ID::unchecked_call == false);
            CallRet ret = std::__invoke(ptr, object_cast(object), arg_cast(a0));
            assert(ID::checkCalled(ret));
        }
#if TEST_STD_VER >= 11
        {
            static_assert((std::is_same<
                decltype(std::__invoke_constexpr(ptr, object_cast(object), arg_cast(a0)))
              , CallRet>::value), "");
            assert(ID::unchecked_call == false);
            CallRet ret = std::__invoke_constexpr(ptr, object_cast(object), arg_cast(a0));
            assert(ID::checkCalled(ret));
        }
#endif
    }

    template <class MethodPtr, class ObjectT>
    void runTestImp(Int<2>, MethodPtr ptr, ObjectT& object) {
        {
            static_assert((std::is_same<
                decltype(std::__invoke(ptr, object_cast(object), arg_cast(a0), arg_cast(a1)))
              , CallRet>::value), "");
            assert(ID::unchecked_call == false);
            CallRet ret = std::__invoke(ptr, object_cast(object), arg_cast(a0), arg_cast(a1));
            assert(ID::checkCalled(ret));
        }
#if TEST_STD_VER >= 11
        {
            static_assert((std::is_same<
                decltype(std::__invoke_constexpr(ptr, object_cast(object), arg_cast(a0), arg_cast(a1)))
              , CallRet>::value), "");
            assert(ID::unchecked_call == false);
            CallRet ret = std::__invoke_constexpr(ptr, object_cast(object), arg_cast(a0), arg_cast(a1));
            assert(ID::checkCalled(ret));
        }
#endif
    }

    template <class MethodPtr, class ObjectT>
    void runTestImp(Int<3>, MethodPtr ptr, ObjectT& object) {
        {
            static_assert((std::is_same<
                decltype(std::__invoke(ptr, object_cast(object), arg_cast(a0), arg_cast(a1), arg_cast(a2)))
              , CallRet>::value), "");
            assert(ID::unchecked_call == false);
            CallRet ret = std::__invoke(ptr, object_cast(object), arg_cast(a0), arg_cast(a1), arg_cast(a2));
            assert(ID::checkCalled(ret));
        }
#if TEST_STD_VER >= 11
        {
            static_assert((std::is_same<
                decltype(std::__invoke_constexpr(ptr, object_cast(object), arg_cast(a0), arg_cast(a1), arg_cast(a2)))
              , CallRet>::value), "");
            assert(ID::unchecked_call == false);
            CallRet ret = std::__invoke_constexpr(ptr, object_cast(object), arg_cast(a0), arg_cast(a1), arg_cast(a2));
            assert(ID::checkCalled(ret));
        }
#endif
    }

    //==========================================================================
    //                       BULLET 7 TEST METHODS
    //==========================================================================
    template <class ObjectT>
    void runTestImp(Int<0>, ObjectT& object) {
        {
            static_assert((std::is_same<
                decltype(std::__invoke(object_cast(object)))
              , CallRet>::value), "");
            assert(ID::unchecked_call == false);
            CallRet ret = std::__invoke(object_cast(object));
            assert(ID::checkCalled(ret));
        }
#if TEST_STD_VER >= 11
        {
            static_assert((std::is_same<
                decltype(std::__invoke_constexpr(object_cast(object)))
              , CallRet>::value), "");
            assert(ID::unchecked_call == false);
            CallRet ret = std::__invoke_constexpr(object_cast(object));
            assert(ID::checkCalled(ret));
        }
#endif
    }

    template <class ObjectT>
    void runTestImp(Int<1>, ObjectT& object) {
        {
            static_assert((std::is_same<
                decltype(std::__invoke(object_cast(object), arg_cast(a0)))
              , CallRet>::value), "");
            assert(ID::unchecked_call == false);
            CallRet ret = std::__invoke(object_cast(object), arg_cast(a0));
            assert(ID::checkCalled(ret));
        }
#if TEST_STD_VER >= 11
        {
            static_assert((std::is_same<
                decltype(std::__invoke_constexpr(object_cast(object), arg_cast(a0)))
              , CallRet>::value), "");
            assert(ID::unchecked_call == false);
            CallRet ret = std::__invoke_constexpr(object_cast(object), arg_cast(a0));
            assert(ID::checkCalled(ret));
        }
#endif
    }

    template <class ObjectT>
    void runTestImp(Int<2>, ObjectT& object) {
        {
            static_assert((std::is_same<
                decltype(std::__invoke(object_cast(object), arg_cast(a0), arg_cast(a1)))
              , CallRet>::value), "");
            assert(ID::unchecked_call == false);
            CallRet ret = std::__invoke(object_cast(object), arg_cast(a0), arg_cast(a1));
            assert(ID::checkCalled(ret));
        }
#if TEST_STD_VER >= 11
        {
            static_assert((std::is_same<
                decltype(std::__invoke_constexpr(object_cast(object), arg_cast(a0), arg_cast(a1)))
              , CallRet>::value), "");
            assert(ID::unchecked_call == false);
            CallRet ret = std::__invoke_constexpr(object_cast(object), arg_cast(a0), arg_cast(a1));
            assert(ID::checkCalled(ret));
        }
#endif
    }

    template <class ObjectT>
    void runTestImp(Int<3>, ObjectT& object) {
        {
            static_assert((std::is_same<
                decltype(std::__invoke(object_cast(object), arg_cast(a0), arg_cast(a1), arg_cast(a2)))
              , CallRet>::value), "");
            assert(ID::unchecked_call == false);
            CallRet ret = std::__invoke(object_cast(object), arg_cast(a0), arg_cast(a1), arg_cast(a2));
            assert(ID::checkCalled(ret));
        }
#if TEST_STD_VER >= 11
        {
            static_assert((std::is_same<
                decltype(std::__invoke_constexpr(object_cast(object), arg_cast(a0), arg_cast(a1), arg_cast(a2)))
              , CallRet>::value), "");
            assert(ID::unchecked_call == false);
            CallRet ret = std::__invoke_constexpr(object_cast(object), arg_cast(a0), arg_cast(a1), arg_cast(a2));
            assert(ID::checkCalled(ret));
        }
#endif
    }
};

#endif // INVOKE_HELPERS_H