-
Notifications
You must be signed in to change notification settings - Fork 88
/
mock.hpp
4084 lines (3527 loc) · 121 KB
/
mock.hpp
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Trompeloeil C++ mocking framework
*
* Copyright (C) Björn Fahller
* Copyright (C) 2017, 2018 Andrew Paxie
* Copyright Tore Martin Hagen 2019
*
* Use, modification and distribution is subject to the
* Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy atl
* http://www.boost.org/LICENSE_1_0.txt)
*
* Project home: https://github.com/rollbear/trompeloeil
*/
#ifndef TROMPELOEIL_MOCK_HPP_
#define TROMPELOEIL_MOCK_HPP_
// trompe l'oeil noun (Concise Encyclopedia)
// Style of representation in which a painted object is intended
// to deceive the viewer into believing it is the object itself...
// project home: https://github.com/rollbear/trompeloeil
// Deficiencies and missing features
// * Mocking function templates is not supported
// * If a macro kills a kitten, this threatens extinction of all felines!
#if defined(_MSC_VER)
# define TROMPELOEIL_NORETURN __declspec(noreturn)
# if (!defined(__cplusplus) || _MSC_VER < 1900)
# error requires C++ in Visual Studio 2015 RC or later
# endif
#else
# define TROMPELOEIL_NORETURN [[noreturn]]
# if (!defined(__cplusplus) || __cplusplus < 201103L)
# error requires C++11 or higher
# endif
#endif
#if defined(__clang__)
# define TROMPELOEIL_CLANG 1
# define TROMPELOEIL_GCC 0
# define TROMPELOEIL_MSVC 0
# define TROMPELOEIL_CLANG_VERSION \
(__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
# define TROMPELOEIL_GCC_VERSION 0
# define TROMPELOEIL_CPLUSPLUS __cplusplus
# define TROMPELOEIL_NOT_IMPLEMENTED(...) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wunneeded-member-function\"") \
__VA_ARGS__ \
_Pragma("clang diagnostic pop")
#elif defined(__GNUC__)
# define TROMPELOEIL_CLANG 0
# define TROMPELOEIL_GCC 1
# define TROMPELOEIL_MSVC 0
# define TROMPELOEIL_CLANG_VERSION 0
# define TROMPELOEIL_GCC_VERSION \
(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
# define TROMPELOEIL_CPLUSPLUS __cplusplus
#elif defined(_MSC_VER)
# define TROMPELOEIL_CLANG 0
# define TROMPELOEIL_GCC 0
# define TROMPELOEIL_MSVC 1
# define TROMPELOEIL_CLANG_VERSION 0
# define TROMPELOEIL_GCC_VERSION 0
# if defined(_MSVC_LANG)
// Compiler is at least Microsoft Visual Studio 2015 Update 3.
# define TROMPELOEIL_CPLUSPLUS _MSVC_LANG
# else /* defined(_MSVC_LANG) */
/*
* This version of Microsoft Visual C++ is released
* in a version of Microsoft Visual Studio between
* 2015 RC and less than 2015 Update 3.
*
* It is an amalgam of C++ versions, with no provision
* to specify C++11 mode.
*
* It also has a __cplusplus macro stuck at 199711L with
* no way to change it, such as /Zc:__cplusplus.
*
* Assume the C++14 code path, but don't promise that it is a
* fully conforming implementation of C++14 either.
* Hence a value of 201401L, which less than 201402L,
* the standards conforming value of __cplusplus.
*/
# define TROMPELOEIL_CPLUSPLUS 201401L
# endif /* !defined(_MSVC_LANG) */
#endif
#ifndef TROMPELOEIL_NOT_IMPLEMENTED
#define TROMPELOEIL_NOT_IMPLEMENTED(...) __VA_ARGS__
#endif
#include <algorithm>
#include <tuple>
#include <iomanip>
#include <sstream>
#include <exception>
#include <functional>
#include <memory>
#include <mutex>
#include <array>
#include <initializer_list>
#include <type_traits>
#include <utility>
#ifdef __cpp_impl_coroutine
#include <coroutine>
#endif
#ifndef TROMPELOEIL_CUSTOM_ATOMIC
#include <atomic>
namespace trompeloeil { using std::atomic; }
#else
#include <trompeloeil/custom_atomic.hpp>
#endif
#ifndef TROMPELOEIL_CUSTOM_UNIQUE_LOCK
namespace trompeloeil { using std::unique_lock; }
#else
#include <trompeloeil/custom_unique_lock.hpp>
#endif
#ifdef TROMPELOEIL_SANITY_CHECKS
#include <cassert>
#define TROMPELOEIL_ASSERT(x) assert(x)
#else
#define TROMPELOEIL_ASSERT(x) do {} while (false)
#endif
#define TROMPELOEIL_IDENTITY(...) __VA_ARGS__ // work around stupid MS VS2015 RC bug
#define TROMPELOEIL_ARG16(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15, ...) _15
#define TROMPELOEIL_COUNT(...) \
TROMPELOEIL_IDENTITY(TROMPELOEIL_ARG16(__VA_ARGS__, \
15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0))
#if TROMPELOEIL_MSVC
#define TROMPELOEIL_CONCAT_(x, y, ...) x ## y __VA_ARGS__
#define TROMPELOEIL_CONCAT(x, ...) TROMPELOEIL_CONCAT_(x, __VA_ARGS__)
#else /* TROMPELOEIL_MSVC */
#define TROMPELOEIL_CONCAT_(x, ...) x ## __VA_ARGS__
#define TROMPELOEIL_CONCAT(x, ...) TROMPELOEIL_CONCAT_(x, __VA_ARGS__)
#endif /* !TROMPELOEIL_MSVC */
#define TROMPELOEIL_SEPARATE1(p1) p1
#define TROMPELOEIL_SEPARATE2(p1,p2) p1 p2
#define TROMPELOEIL_SEPARATE3(p1,...) p1 TROMPELOEIL_SEPARATE2(__VA_ARGS__)
#define TROMPELOEIL_SEPARATE4(p1,...) p1 TROMPELOEIL_SEPARATE3(__VA_ARGS__)
#define TROMPELOEIL_SEPARATE5(p1,...) p1 TROMPELOEIL_SEPARATE4(__VA_ARGS__)
#define TROMPELOEIL_SEPARATE6(p1,...) p1 TROMPELOEIL_SEPARATE5(__VA_ARGS__)
#define TROMPELOEIL_SEPARATE7(p1,...) p1 TROMPELOEIL_SEPARATE6(__VA_ARGS__)
#define TROMPELOEIL_SEPARATE8(p1,...) p1 TROMPELOEIL_SEPARATE7(__VA_ARGS__)
#define TROMPELOEIL_SEPARATE9(p1,...) p1 TROMPELOEIL_SEPARATE8(__VA_ARGS__)
#define TROMPELOEIL_SEPARATE(...) \
TROMPELOEIL_CONCAT(TROMPELOEIL_SEPARATE,\
TROMPELOEIL_COUNT(__VA_ARGS__))(__VA_ARGS__)
#define TROMPELOEIL_REMOVE_PAREN(...) TROMPELOEIL_CONCAT(TROMPELOEIL_CLEAR_, \
TROMPELOEIL_REMOVE_PAREN_INTERNAL __VA_ARGS__)
#define TROMPELOEIL_REMOVE_PAREN_INTERNAL(...) \
TROMPELOEIL_REMOVE_PAREN_INTERNAL __VA_ARGS__
#define TROMPELOEIL_CLEAR_TROMPELOEIL_REMOVE_PAREN_INTERNAL
#define TROMPELOEIL_EXPAND(x) x
#define TROMPELOEIL_INIT_WITH_STR15(base, x, ...) \
base{#x, x}, TROMPELOEIL_EXPAND(TROMPELOEIL_INIT_WITH_STR14(base, __VA_ARGS__))
#define TROMPELOEIL_INIT_WITH_STR14(base, x, ...) \
base{#x, x}, TROMPELOEIL_EXPAND(TROMPELOEIL_INIT_WITH_STR13(base, __VA_ARGS__))
#define TROMPELOEIL_INIT_WITH_STR13(base, x, ...) \
base{#x, x}, TROMPELOEIL_EXPAND(TROMPELOEIL_INIT_WITH_STR12(base, __VA_ARGS__))
#define TROMPELOEIL_INIT_WITH_STR12(base, x, ...) \
base{#x, x}, TROMPELOEIL_EXPAND(TROMPELOEIL_INIT_WITH_STR11(base, __VA_ARGS__))
#define TROMPELOEIL_INIT_WITH_STR11(base, x, ...) \
base{#x, x}, TROMPELOEIL_EXPAND(TROMPELOEIL_INIT_WITH_STR10(base, __VA_ARGS__))
#define TROMPELOEIL_INIT_WITH_STR10(base, x, ...) \
base{#x, x}, TROMPELOEIL_EXPAND(TROMPELOEIL_INIT_WITH_STR9(base, __VA_ARGS__))
#define TROMPELOEIL_INIT_WITH_STR9(base, x, ...) \
base{#x, x}, TROMPELOEIL_EXPAND(TROMPELOEIL_INIT_WITH_STR8(base, __VA_ARGS__))
#define TROMPELOEIL_INIT_WITH_STR8(base, x, ...) \
base{#x, x}, TROMPELOEIL_EXPAND(TROMPELOEIL_INIT_WITH_STR7(base, __VA_ARGS__))
#define TROMPELOEIL_INIT_WITH_STR7(base, x, ...) \
base{#x, x}, TROMPELOEIL_EXPAND(TROMPELOEIL_INIT_WITH_STR6(base, __VA_ARGS__))
#define TROMPELOEIL_INIT_WITH_STR6(base, x, ...) \
base{#x, x}, TROMPELOEIL_EXPAND(TROMPELOEIL_INIT_WITH_STR5(base, __VA_ARGS__))
#define TROMPELOEIL_INIT_WITH_STR5(base, x, ...) \
base{#x, x}, TROMPELOEIL_EXPAND(TROMPELOEIL_INIT_WITH_STR4(base, __VA_ARGS__))
#define TROMPELOEIL_INIT_WITH_STR4(base, x, ...) \
base{#x, x}, TROMPELOEIL_EXPAND(TROMPELOEIL_INIT_WITH_STR3(base, __VA_ARGS__))
#define TROMPELOEIL_INIT_WITH_STR3(base, x, ...) \
base{#x, x}, TROMPELOEIL_EXPAND(TROMPELOEIL_INIT_WITH_STR2(base, __VA_ARGS__))
#define TROMPELOEIL_INIT_WITH_STR2(base, x, ...) \
base{#x, x}, TROMPELOEIL_EXPAND(TROMPELOEIL_INIT_WITH_STR1(base, __VA_ARGS__))
#define TROMPELOEIL_INIT_WITH_STR1(base, x) \
base{#x, x}
#define TROMPELOEIL_INIT_WITH_STR0(base)
#define TROMPELOEIL_INIT_WITH_STR(base, ...) \
TROMPELOEIL_CONCAT(TROMPELOEIL_INIT_WITH_STR, \
TROMPELOEIL_COUNT(__VA_ARGS__))(base, __VA_ARGS__)
#define TROMPELOEIL_PARAM_LIST15(...) \
TROMPELOEIL_PARAM_LIST14(__VA_ARGS__), \
::trompeloeil::param_list_t<__VA_ARGS__, 14> p15
#define TROMPELOEIL_PARAM_LIST14(...) \
TROMPELOEIL_PARAM_LIST13(__VA_ARGS__), \
::trompeloeil::param_list_t<__VA_ARGS__, 13> p14
#define TROMPELOEIL_PARAM_LIST13(...) \
TROMPELOEIL_PARAM_LIST12(__VA_ARGS__), \
::trompeloeil::param_list_t<__VA_ARGS__, 12> p13
#define TROMPELOEIL_PARAM_LIST12(...) \
TROMPELOEIL_PARAM_LIST11(__VA_ARGS__), \
::trompeloeil::param_list_t<__VA_ARGS__, 11> p12
#define TROMPELOEIL_PARAM_LIST11(...) \
TROMPELOEIL_PARAM_LIST10(__VA_ARGS__), \
::trompeloeil::param_list_t<__VA_ARGS__, 10> p11
#define TROMPELOEIL_PARAM_LIST10(...) \
TROMPELOEIL_PARAM_LIST9(__VA_ARGS__), \
::trompeloeil::param_list_t<__VA_ARGS__, 9> p10
#define TROMPELOEIL_PARAM_LIST9(...) \
TROMPELOEIL_PARAM_LIST8(__VA_ARGS__), \
::trompeloeil::param_list_t<__VA_ARGS__, 8> p9
#define TROMPELOEIL_PARAM_LIST8(...) \
TROMPELOEIL_PARAM_LIST7(__VA_ARGS__), \
::trompeloeil::param_list_t<__VA_ARGS__, 7> p8
#define TROMPELOEIL_PARAM_LIST7(...) \
TROMPELOEIL_PARAM_LIST6(__VA_ARGS__), \
::trompeloeil::param_list_t<__VA_ARGS__, 6> p7
#define TROMPELOEIL_PARAM_LIST6(...) \
TROMPELOEIL_PARAM_LIST5(__VA_ARGS__), \
::trompeloeil::param_list_t<__VA_ARGS__, 5> p6
#define TROMPELOEIL_PARAM_LIST5(...) \
TROMPELOEIL_PARAM_LIST4(__VA_ARGS__), \
::trompeloeil::param_list_t<__VA_ARGS__, 4> p5
#define TROMPELOEIL_PARAM_LIST4(...) \
TROMPELOEIL_PARAM_LIST3(__VA_ARGS__), \
::trompeloeil::param_list_t<__VA_ARGS__, 3> p4
#define TROMPELOEIL_PARAM_LIST3(...) \
TROMPELOEIL_PARAM_LIST2(__VA_ARGS__), \
::trompeloeil::param_list_t<__VA_ARGS__, 2> p3
#define TROMPELOEIL_PARAM_LIST2(...) \
TROMPELOEIL_PARAM_LIST1(__VA_ARGS__), \
::trompeloeil::param_list_t<__VA_ARGS__, 1> p2
#define TROMPELOEIL_PARAM_LIST1(...) \
::trompeloeil::param_list_t<__VA_ARGS__, 0> p1
#define TROMPELOEIL_PARAM_LIST0(func_type)
#define TROMPELOEIL_PARAM_LIST(num, func_type) \
TROMPELOEIL_CONCAT(TROMPELOEIL_PARAM_LIST, num) \
(TROMPELOEIL_REMOVE_PAREN(func_type))
#define TROMPELOEIL_PARAMS15 TROMPELOEIL_PARAMS14, p15
#define TROMPELOEIL_PARAMS14 TROMPELOEIL_PARAMS13, p14
#define TROMPELOEIL_PARAMS13 TROMPELOEIL_PARAMS12, p13
#define TROMPELOEIL_PARAMS12 TROMPELOEIL_PARAMS11, p12
#define TROMPELOEIL_PARAMS11 TROMPELOEIL_PARAMS10, p11
#define TROMPELOEIL_PARAMS10 TROMPELOEIL_PARAMS9, p10
#define TROMPELOEIL_PARAMS9 TROMPELOEIL_PARAMS8, p9
#define TROMPELOEIL_PARAMS8 TROMPELOEIL_PARAMS7, p8
#define TROMPELOEIL_PARAMS7 TROMPELOEIL_PARAMS6, p7
#define TROMPELOEIL_PARAMS6 TROMPELOEIL_PARAMS5, p6
#define TROMPELOEIL_PARAMS5 TROMPELOEIL_PARAMS4, p5
#define TROMPELOEIL_PARAMS4 TROMPELOEIL_PARAMS3, p4
#define TROMPELOEIL_PARAMS3 TROMPELOEIL_PARAMS2, p3
#define TROMPELOEIL_PARAMS2 TROMPELOEIL_PARAMS1, p2
#define TROMPELOEIL_PARAMS1 , p1
#define TROMPELOEIL_PARAMS0
#define TROMPELOEIL_PARAMS(num) TROMPELOEIL_CONCAT(TROMPELOEIL_PARAMS, num)
#if (TROMPELOEIL_CPLUSPLUS == 201103L)
#define TROMPELOEIL_DECLTYPE_AUTO \
auto
#define TROMPELOEIL_TRAILING_RETURN_TYPE(return_type) \
-> return_type
#else /* (TROMPELOEIL_CPLUSPLUS == 201103L) */
#define TROMPELOEIL_DECLTYPE_AUTO \
decltype(auto)
#define TROMPELOEIL_TRAILING_RETURN_TYPE(return_type) \
/**/
#endif /* !(TROMPELOEIL_CPLUSPLUS == 201103L) */
#if TROMPELOEIL_CPLUSPLUS > 201403L && (!TROMPELOEIL_GCC || TROMPELOEIL_GCC_VERSION >= 70000)
# define TROMPELOEIL_INLINE_VAR [[maybe_unused]] static inline
#else
# define TROMPELOEIL_INLINE_VAR static
#endif
static constexpr bool trompeloeil_movable_mock = false;
namespace trompeloeil
{
template <typename T>
struct identity_type
{
using type = T;
};
template <typename R, typename C, typename ... Args>
identity_type<R(Args...)>
nonconst_member_signature(R (C::*)(Args...));
template <typename R, typename C, typename ... Args>
identity_type<R(Args...)>
const_member_signature(R (C::*)(Args...) const);
template <typename ...>
struct void_t_
{
using type = void;
};
template <typename ... T>
using void_t = typename void_t_<T...>::type;
template <template <typename ...> class, typename, typename ...>
struct is_detected_{
using type = std::false_type;
};
template <template <typename ...> class D, typename ... Ts>
struct is_detected_<D, void_t<D<Ts...>>, Ts...>
{
using type = std::true_type;
};
template <template <typename ...> class D, typename ... Ts>
using is_detected = typename is_detected_<D, void, Ts...>::type;
}
# if (TROMPELOEIL_CPLUSPLUS == 201103L)
#include "cpp11_shenanigans.hpp"
# else /* (TROMPELOEIL_CPLUSPLUS == 201103L) */
namespace trompeloeil {
/* Only these entities really need to
* be available in namespace detail, but
* VS 2015 has a problem with this approach.
*
* namespace detail {
*
* using std::make_unique;
*
* using std::conditional_t;
* using std::decay_t;
* using std::enable_if_t;
* using std::remove_pointer_t;
* using std::remove_reference_t;
*
* using std::exchange;
* using std::index_sequence;
* using std::index_sequence_for;
* using std::integer_sequence;
* using std::make_index_sequence;
*
* }
*
* Instead, use a namespace alias.
*/
namespace detail = std;
}
# endif /* !(TROMPELOEIL_CPLUSPLUS == 201103L) */
namespace trompeloeil {
# if TROMPELOEIL_CPLUSPLUS >= 201703L
# if TROMPELOEIL_CLANG && TROMPELOEIL_CLANG_VERSION >= 60000
// these are mostly added to work around clang++ bugs
// https://bugs.llvm.org/show_bug.cgi?id=38033
// https://bugs.llvm.org/show_bug.cgi?id=38010
template <typename T>
using move_construct_type = decltype(T(std::declval<T&&>()));
template <typename T>
using copy_construct_type = decltype(T(std::declval<const T&>()));
template <typename T>
using can_move_construct = is_detected<move_construct_type, detail::decay_t<T>>;
template <typename T>
using can_copy_construct = is_detected<copy_construct_type, detail::decay_t<T>>;
# else
template <typename T>
using can_move_construct = std::is_move_constructible<T>;
template <typename T>
using can_copy_construct = std::is_copy_constructible<T>;
# endif
template <typename F, typename ... A>
using invoke_result_type = decltype(std::declval<F&>()(std::declval<A>()...));
# else
template <typename T>
using can_move_construct = std::is_move_constructible<T>;
template <typename T>
using can_copy_construct = std::is_copy_constructible<T>;
template <typename F, typename ... A>
using invoke_result_type = decltype(std::declval<F&>()(std::declval<A>()...));
# endif
#if defined(__cpp_impl_coroutine)
template <typename Sig, typename = void>
struct is_coroutine : std::false_type {};
template <typename Sig>
struct is_coroutine<Sig, std::void_t<decltype(&std::coroutine_traits<Sig>::promise_type::initial_suspend)>> : std::true_type {};
#else
template <typename>
using is_coroutine = std::false_type;
#endif
template <typename T>
class mini_span
{
public:
mini_span(T* address, size_t size) noexcept : begin_(address), end_(address + size) {}
T* begin() const noexcept { return begin_; }
T* end() const noexcept { return end_; }
private:
T* begin_;
T* end_;
};
class specialized;
#ifndef TROMPELOEIL_CUSTOM_RECURSIVE_MUTEX
template <typename T = void>
unique_lock<std::recursive_mutex> get_lock()
{
// Ugly hack for lifetime of mutex. The statically allocated
// recursive_mutex is intentionally leaked, to ensure that the
// mutex is available and valid even if the last use is from
// the destructor of a global object in a translation unit
// without #include <trompeloeil.hpp>
alignas(std::recursive_mutex)
static char buffer[sizeof(std::recursive_mutex)];
static auto mutex = new (&buffer) std::recursive_mutex;
return unique_lock<std::recursive_mutex>{*mutex};
}
#else
class custom_recursive_mutex {
public:
virtual ~custom_recursive_mutex() = default;
virtual void lock() = 0;
virtual void unlock() = 0;
};
// User has to provide an own recursive mutex.
std::unique_ptr<custom_recursive_mutex> create_custom_recursive_mutex();
template <typename T = void>
unique_lock<custom_recursive_mutex> get_lock()
{
static std::unique_ptr<custom_recursive_mutex> mtx =
create_custom_recursive_mutex();
return unique_lock<custom_recursive_mutex>{*mtx};
}
#endif
template <size_t N, typename T>
using conditional_tuple_element
= detail::conditional_t<(N < std::tuple_size<T>::value),
typename std::tuple_element<N, T>::type,
int>;
template <typename T>
struct param_list;
template <typename R, typename ... P>
struct param_list<R(P...)>
{
static size_t constexpr const size = sizeof...(P);
template <size_t N>
using type = conditional_tuple_element<N, std::tuple<P...>>;
};
template <typename Sig, size_t N>
using param_list_t = typename param_list<Sig>::template type<N>;
class expectation_violation : public std::logic_error
{
public:
using std::logic_error::logic_error;
};
struct location
{
location()
noexcept
: file("")
, line(0U)
{}
location(
char const* file_,
unsigned long line_
)
noexcept
: file{file_}
, line{line_}
{}
char const *file;
unsigned long line;
friend
std::ostream&
operator<<(
std::ostream& os,
const location& loc)
{
if (loc.line != 0U) os << loc.file << ':' << loc.line;
return os;
}
};
enum class severity { fatal, nonfatal };
using reporter_func = std::function<void(severity,
char const *file,
unsigned long line,
std::string const &msg)>;
using ok_reporter_func = std::function<void(char const *msg)>;
inline
void
default_reporter(
severity,
char const *file,
unsigned long line,
std::string const &msg)
{
if (!std::current_exception())
{
std::stringstream os;
os << location{ file, line } << "\n" << msg;
throw expectation_violation(os.str());
}
}
inline
void
default_ok_reporter(char const* /*msg*/)
{
/* OK reporter defaults to doing nothing. */
}
inline
reporter_func&
reporter_obj()
{
static reporter_func obj = default_reporter;
return obj;
}
inline
ok_reporter_func&
ok_reporter_obj()
{
static ok_reporter_func obj = default_ok_reporter;
return obj;
}
inline
reporter_func
set_reporter(
reporter_func f)
{
return detail::exchange(reporter_obj(), std::move(f));
}
inline
std::pair<reporter_func, ok_reporter_func>
set_reporter(
reporter_func rf,
ok_reporter_func orf)
{
return {
set_reporter(std::move(rf)),
detail::exchange(ok_reporter_obj(), std::move(orf))
};
}
class tracer;
inline
tracer*&
tracer_obj()
noexcept
{
static tracer* ptr = nullptr;
return ptr;
}
inline
tracer*
set_tracer(
tracer* obj)
noexcept
{
// std::exchange would be sane here, but it costs compilation time
auto& ptr = tracer_obj();
auto rv = ptr;
ptr = obj;
return rv;
}
class tracer
{
public:
virtual
void
trace(
char const *file,
unsigned long line,
std::string const &call) = 0;
protected:
tracer() = default;
tracer(tracer const&) = delete;
tracer& operator=(tracer const&) = delete;
virtual
~tracer()
{
set_tracer(previous);
}
private:
tracer* previous = set_tracer(this);
};
class trace_agent;
template <typename T>
struct reporter;
template <typename T>
void
send_report(
severity s,
location loc,
std::string const &msg)
{
reporter<T>::send(s, loc.file, loc.line, msg.c_str());
}
template <typename T>
void
send_ok_report(
std::string const &msg)
{
reporter<T>::sendOk(msg.c_str());
}
template <typename T>
struct reporter
{
static
void
send(
severity s,
char const *file,
unsigned long line,
char const *msg);
static
void
sendOk(
char const *msg);
};
template <typename T>
void reporter<T>::
send(
severity s,
char const *file,
unsigned long line,
char const *msg)
{
reporter_obj()(s, file, line, msg);
}
template <typename T>
void reporter<T>::
sendOk(char const* msg)
{
ok_reporter_obj()(msg);
}
template <typename ... T>
constexpr
bool
ignore(
T const& ...)
noexcept
{
return true;
}
struct illegal_argument
{
template <bool b = false>
constexpr
illegal_argument const& operator&() const
{
static_assert(b, "illegal argument");
return *this;
}
template <bool b = false>
constexpr
illegal_argument const& operator*() const
{
static_assert(b, "illegal argument");
return *this;
}
template <typename T, bool b = false>
constexpr
illegal_argument const& operator=(T const&) const
{
static_assert(b, "illegal argument");
return *this;
}
template <typename T, bool b = false>
constexpr
operator T() const
{
static_assert(b, "illegal argument");
return {};
}
};
struct matcher { };
struct wildcard : public matcher
{
template <typename T>
struct wrapper {
operator T() {
static_assert(std::is_same<T, void>{},
"Getting a value from wildcard is not allowed.\n"
"See https://github.com/rollbear/trompeloeil/issues/270\n"
"and https://github.com/rollbear/trompeloeil/issues/290");
return *this;
}
};
template <typename T, typename std::enable_if<!std::is_convertible<wildcard&, T>{}>::type* = nullptr>
operator T()
{
return wrapper<T>{};
}
template <typename T>
operator T&&() const
{
#if (!TROMPELOEIL_GCC || TROMPELOEIL_GCC_VERSION < 60000 || TROMPELOEIL_GCC_VERSION >= 90000) \
&& (!TROMPELOEIL_CLANG || TROMPELOEIL_CLANG_VERSION >= 40000 || !defined(_LIBCPP_STD_VER) || _LIBCPP_STD_VER != 14)
return wrapper<T &&>{};
#else
return *this;
#endif
}
template <typename T>
operator T&() const volatile
{
return wrapper<T&>{};
}
template <typename T>
constexpr
bool
matches(
T const&)
const
noexcept
{
return true;
}
};
TROMPELOEIL_INLINE_VAR wildcard _{};
template <typename T>
using matcher_access = decltype(static_cast<matcher*>(std::declval<typename std::add_pointer<T>::type>()));
template <typename T>
using is_matcher = typename is_detected<matcher_access, T>::type;
template <typename T>
using ostream_insertion = decltype(std::declval<std::ostream&>() << std::declval<T>());
template <typename T>
using is_output_streamable = std::integral_constant<bool, is_detected<ostream_insertion, T>::value && !std::is_array<T>::value>;
struct stream_sentry
{
explicit
stream_sentry(
std::ostream& os_)
: os(os_)
, width(os.width(0))
, flags(os.flags(std::ios_base::dec | std::ios_base::left))
, fill(os.fill(' '))
{ }
stream_sentry(
stream_sentry const&)
= delete;
~stream_sentry()
{
os.flags(flags);
os.fill(fill);
os.width(width);
}
private:
std::ostream& os;
std::streamsize width;
std::ios_base::fmtflags flags;
char fill;
};
struct indirect_null {
#if !TROMPELOEIL_CLANG
template <
typename T,
typename = detail::enable_if_t<std::is_convertible<std::nullptr_t, T>::value>
>
operator T&&() const = delete;
#endif
#if TROMPELOEIL_GCC || TROMPELOEIL_MSVC
template <typename T, typename C, typename ... As>
using memfunptr = T (C::*)(As...);
template <typename T>
operator T*() const;
template <typename T, typename C>
operator T C::*() const;
template <typename T, typename C, typename ... As>
operator memfunptr<T,C,As...>() const;
#endif /* TROMPELOEIL_GCC || TROMPELOEIL_MSVC */
operator std::nullptr_t() const;
};
template <typename T, typename U>
using equality_comparison = decltype((std::declval<T const&>() == std::declval<U const&>())
? true
: false);
template <typename T, typename U>
using is_equal_comparable = is_detected<equality_comparison, T, U>;
#if defined(_MSC_VER) && (_MSC_VER < 1910)
template <typename T>
using is_null_comparable = is_equal_comparable<T, std::nullptr_t>;
#else
template <typename T>
using is_null_comparable = is_equal_comparable<T, indirect_null>;
#endif
template <typename T, typename = decltype(std::declval<T const&>() == nullptr)>
constexpr
auto
is_null_redirect(
T const &t)
noexcept(noexcept(std::declval<T const&>() == nullptr))
-> decltype(t == nullptr)
{
return t == nullptr;
}
template <typename T>
constexpr
auto
is_null(
T const &t,
std::true_type)
noexcept(noexcept(is_null_redirect(t)))
-> decltype(is_null_redirect(t))
{
// Redirect evaluation to suppress wrong non-null warnings in g++ 9 and 10.
return is_null_redirect(t);
}
template <typename T, typename V>
constexpr
bool
is_null(
T const &,
V)
noexcept
{
return false;
}
template <typename T>
constexpr
bool
is_null(
T const &t)
{
// g++-4.9 uses C++11 rules for constexpr function, so can't
// break this up into smaller bits
using tag = std::integral_constant<bool, is_null_comparable<T>::value
&& !is_matcher<T>::value
&& !std::is_array<T>::value>;
return ::trompeloeil::is_null(t, tag{});
}