Debian Codegraph @

plugins/core/Standard/qMPlane/external/trompeloeil/trompeloeil.hpp

raw
1 /*
2 * Trompeloeil C++ mocking framework
3 *
4 * Copyright Björn Fahller 2014-2019
5 * Copyright (C) 2017, 2018 Andrew Paxie
6 * Copyright Tore Martin Hagen 2019
7 *
8 * Use, modification and distribution is subject to the
9 * Boost Software License, Version 1.0. (See accompanying
10 * file LICENSE_1_0.txt or copy at
11 * http://www.boost.org/LICENSE_1_0.txt)
12 *
13 * Project home: https://github.com/rollbear/trompeloeil
14 */
15
16 #ifndef TROMPELOEIL_HPP_
17 #define TROMPELOEIL_HPP_
18
19
20 // trompe l'oeil noun (Concise Encyclopedia)
21 // Style of representation in which a painted object is intended
22 // to deceive the viewer into believing it is the object itself...
23
24 // project home: https://github.com/rollbear/trompeloeil
25
26
27 // Deficiencies and missing features
28 // * Mocking function templates is not supported
29 // * If a macro kills a kitten, this threatens extinction of all felines!
30
31 #if defined(_MSC_VER)
32
33 # define TROMPELOEIL_NORETURN __declspec(noreturn)
34
35 # if (!defined(__cplusplus) || _MSC_VER < 1900)
36 # error requires C++ in Visual Studio 2015 RC or later
37 # endif
38
39 #else
40
41 # define TROMPELOEIL_NORETURN [[noreturn]]
42
43 # if (!defined(__cplusplus) || __cplusplus < 201103L)
44 # error requires C++11 or higher
45 # endif
46
47 #endif
48
49 #if defined(__clang__)
50
51 # define TROMPELOEIL_CLANG 1
52 # define TROMPELOEIL_GCC 0
53 # define TROMPELOEIL_MSVC 0
54
55 # define TROMPELOEIL_CLANG_VERSION \
56 (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
57
58 # define TROMPELOEIL_GCC_VERSION 0
59
60 # define TROMPELOEIL_CPLUSPLUS __cplusplus
61
62 # define TROMPELOEIL_NOT_IMPLEMENTED(...) \
63 _Pragma("clang diagnostic push") \
64 _Pragma("clang diagnostic ignored \"-Wunneeded-member-function\"") \
65 __VA_ARGS__ \
66 _Pragma("clang diagnostic pop")
67
68 #elif defined(__GNUC__)
69
70 # define TROMPELOEIL_CLANG 0
71 # define TROMPELOEIL_GCC 1
72 # define TROMPELOEIL_MSVC 0
73
74 # define TROMPELOEIL_CLANG_VERSION 0
75 # define TROMPELOEIL_GCC_VERSION \
76 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
77
78 # define TROMPELOEIL_CPLUSPLUS __cplusplus
79
80 #elif defined(_MSC_VER)
81
82 # define TROMPELOEIL_CLANG 0
83 # define TROMPELOEIL_GCC 0
84 # define TROMPELOEIL_MSVC 1
85
86 # define TROMPELOEIL_CLANG_VERSION 0
87 # define TROMPELOEIL_GCC_VERSION 0
88
89 # if defined(_MSVC_LANG)
90
91 // Compiler is at least Microsoft Visual Studio 2015 Update 3.
92 # define TROMPELOEIL_CPLUSPLUS _MSVC_LANG
93
94 # else /* defined(_MSVC_LANG) */
95
96 /*
97 * This version of Microsoft Visual C++ is released
98 * in a version of Microsoft Visual Studio between
99 * 2015 RC and less than 2015 Update 3.
100 *
101 * It is an amalgam of C++ versions, with no provision
102 * to specify C++11 mode.
103 *
104 * It also has a __cplusplus macro stuck at 199711L with
105 * no way to change it, such as /Zc:__cplusplus.
106 *
107 * Assume the C++14 code path, but don't promise that it is a
108 * fully conforming implementation of C++14 either.
109 * Hence a value of 201401L, which less than 201402L,
110 * the standards conforming value of __cplusplus.
111 */
112 # define TROMPELOEIL_CPLUSPLUS 201401L
113
114 # endif /* !defined(_MSVC_LANG) */
115
116 #endif
117
118
119 #ifndef TROMPELOEIL_NOT_IMPLEMENTED
120 #define TROMPELOEIL_NOT_IMPLEMENTED(...) __VA_ARGS__
121 #endif
122
123 #include <tuple>
124 #include <iomanip>
125 #include <sstream>
126 #include <exception>
127 #include <functional>
128 #include <memory>
129 #include <cstring>
130 #include <regex>
131 #include <mutex>
132 #include <atomic>
133 #include <initializer_list>
134 #include <type_traits>
135 #include <utility>
136
137
138 #ifdef TROMPELOEIL_SANITY_CHECKS
139 #include <cassert>
140 #define TROMPELOEIL_ASSERT(x) assert(x)
141 #else
142 #define TROMPELOEIL_ASSERT(x) do {} while (false)
143 #endif
144
145 #define TROMPELOEIL_IDENTITY(...) __VA_ARGS__ // work around stupid MS VS2015 RC bug
146
147 #define TROMPELOEIL_ARG16(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15, ...) _15
148
149 #define TROMPELOEIL_COUNT(...) \
150 TROMPELOEIL_IDENTITY(TROMPELOEIL_ARG16(__VA_ARGS__, \
151 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0))
152
153 #if defined(_MSC_VER)
154
155 #define TROMPELOEIL_CONCAT_(x, y, ...) x ## y __VA_ARGS__
156 #define TROMPELOEIL_CONCAT(x, ...) TROMPELOEIL_CONCAT_(x, __VA_ARGS__)
157
158 #else /* defined(_MSC_VER) */
159
160 #define TROMPELOEIL_CONCAT_(x, ...) x ## __VA_ARGS__
161 #define TROMPELOEIL_CONCAT(x, ...) TROMPELOEIL_CONCAT_(x, __VA_ARGS__)
162
163 #endif /* !defined(_MSC_VER) */
164
165 #define TROMPELOEIL_SEPARATE1(p1) p1
166 #define TROMPELOEIL_SEPARATE2(p1,p2) p1 p2
167 #define TROMPELOEIL_SEPARATE3(p1,...) p1 TROMPELOEIL_SEPARATE2(__VA_ARGS__)
168 #define TROMPELOEIL_SEPARATE4(p1,...) p1 TROMPELOEIL_SEPARATE3(__VA_ARGS__)
169 #define TROMPELOEIL_SEPARATE5(p1,...) p1 TROMPELOEIL_SEPARATE4(__VA_ARGS__)
170 #define TROMPELOEIL_SEPARATE6(p1,...) p1 TROMPELOEIL_SEPARATE5(__VA_ARGS__)
171 #define TROMPELOEIL_SEPARATE7(p1,...) p1 TROMPELOEIL_SEPARATE6(__VA_ARGS__)
172 #define TROMPELOEIL_SEPARATE8(p1,...) p1 TROMPELOEIL_SEPARATE7(__VA_ARGS__)
173 #define TROMPELOEIL_SEPARATE9(p1,...) p1 TROMPELOEIL_SEPARATE8(__VA_ARGS__)
174 #define TROMPELOEIL_SEPARATE(...) \
175 TROMPELOEIL_CONCAT(TROMPELOEIL_SEPARATE,\
176 TROMPELOEIL_COUNT(__VA_ARGS__))(__VA_ARGS__)
177
178
179 #define TROMPELOEIL_REMOVE_PAREN(...) TROMPELOEIL_CONCAT(TROMPELOEIL_CLEAR_, \
180 TROMPELOEIL_REMOVE_PAREN_INTERNAL __VA_ARGS__)
181 #define TROMPELOEIL_REMOVE_PAREN_INTERNAL(...) \
182 TROMPELOEIL_REMOVE_PAREN_INTERNAL __VA_ARGS__
183 #define TROMPELOEIL_CLEAR_TROMPELOEIL_REMOVE_PAREN_INTERNAL
184
185 #define TROMPELOEIL_INIT_WITH_STR15(base, x, ...) \
186 base{#x, x}, TROMPELOEIL_INIT_WITH_STR14(base, __VA_ARGS__)
187
188 #define TROMPELOEIL_INIT_WITH_STR14(base, x, ...) \
189 base{#x, x}, TROMPELOEIL_INIT_WITH_STR13(base, __VA_ARGS__)
190
191 #define TROMPELOEIL_INIT_WITH_STR13(base, x, ...) \
192 base{#x, x}, TROMPELOEIL_INIT_WITH_STR12(base, __VA_ARGS__)
193
194 #define TROMPELOEIL_INIT_WITH_STR12(base, x, ...) \
195 base{#x, x}, TROMPELOEIL_INIT_WITH_STR11(base, __VA_ARGS__)
196
197 #define TROMPELOEIL_INIT_WITH_STR11(base, x, ...) \
198 base{#x, x}, TROMPELOEIL_INIT_WITH_STR10(base, __VA_ARGS__)
199
200 #define TROMPELOEIL_INIT_WITH_STR10(base, x, ...) \
201 base{#x, x}, TROMPELOEIL_INIT_WITH_STR9(base, __VA_ARGS__)
202
203 #define TROMPELOEIL_INIT_WITH_STR9(base, x, ...) \
204 base{#x, x}, TROMPELOEIL_INIT_WITH_STR8(base, __VA_ARGS__)
205
206 #define TROMPELOEIL_INIT_WITH_STR8(base, x, ...) \
207 base{#x, x}, TROMPELOEIL_INIT_WITH_STR7(base, __VA_ARGS__)
208
209 #define TROMPELOEIL_INIT_WITH_STR7(base, x, ...) \
210 base{#x, x}, TROMPELOEIL_INIT_WITH_STR6(base, __VA_ARGS__)
211
212 #define TROMPELOEIL_INIT_WITH_STR6(base, x, ...) \
213 base{#x, x}, TROMPELOEIL_INIT_WITH_STR5(base, __VA_ARGS__)
214
215 #define TROMPELOEIL_INIT_WITH_STR5(base, x, ...) \
216 base{#x, x}, TROMPELOEIL_INIT_WITH_STR4(base, __VA_ARGS__)
217
218 #define TROMPELOEIL_INIT_WITH_STR4(base, x, ...) \
219 base{#x, x}, TROMPELOEIL_INIT_WITH_STR3(base, __VA_ARGS__)
220
221 #define TROMPELOEIL_INIT_WITH_STR3(base, x, ...) \
222 base{#x, x}, TROMPELOEIL_INIT_WITH_STR2(base, __VA_ARGS__)
223
224 #define TROMPELOEIL_INIT_WITH_STR2(base, x, ...) \
225 base{#x, x}, TROMPELOEIL_INIT_WITH_STR1(base, __VA_ARGS__)
226
227 #define TROMPELOEIL_INIT_WITH_STR1(base, x) \
228 base{#x, x}
229
230 #define TROMPELOEIL_INIT_WITH_STR0(base)
231
232 #define TROMPELOEIL_INIT_WITH_STR(base, ...) \
233 TROMPELOEIL_CONCAT(TROMPELOEIL_INIT_WITH_STR, \
234 TROMPELOEIL_COUNT(__VA_ARGS__))(base, __VA_ARGS__)
235
236 #define TROMPELOEIL_PARAM_LIST15(...) \
237 TROMPELOEIL_PARAM_LIST14(__VA_ARGS__), \
238 ::trompeloeil::param_list_t<__VA_ARGS__, 14> p15
239
240 #define TROMPELOEIL_PARAM_LIST14(...) \
241 TROMPELOEIL_PARAM_LIST13(__VA_ARGS__), \
242 ::trompeloeil::param_list_t<__VA_ARGS__, 13> p14
243
244 #define TROMPELOEIL_PARAM_LIST13(...) \
245 TROMPELOEIL_PARAM_LIST12(__VA_ARGS__), \
246 ::trompeloeil::param_list_t<__VA_ARGS__, 12> p13
247
248 #define TROMPELOEIL_PARAM_LIST12(...) \
249 TROMPELOEIL_PARAM_LIST11(__VA_ARGS__), \
250 ::trompeloeil::param_list_t<__VA_ARGS__, 11> p12
251
252 #define TROMPELOEIL_PARAM_LIST11(...) \
253 TROMPELOEIL_PARAM_LIST10(__VA_ARGS__), \
254 ::trompeloeil::param_list_t<__VA_ARGS__, 10> p11
255
256 #define TROMPELOEIL_PARAM_LIST10(...) \
257 TROMPELOEIL_PARAM_LIST9(__VA_ARGS__), \
258 ::trompeloeil::param_list_t<__VA_ARGS__, 9> p10
259
260 #define TROMPELOEIL_PARAM_LIST9(...) \
261 TROMPELOEIL_PARAM_LIST8(__VA_ARGS__), \
262 ::trompeloeil::param_list_t<__VA_ARGS__, 8> p9
263
264 #define TROMPELOEIL_PARAM_LIST8(...) \
265 TROMPELOEIL_PARAM_LIST7(__VA_ARGS__), \
266 ::trompeloeil::param_list_t<__VA_ARGS__, 7> p8
267
268 #define TROMPELOEIL_PARAM_LIST7(...) \
269 TROMPELOEIL_PARAM_LIST6(__VA_ARGS__), \
270 ::trompeloeil::param_list_t<__VA_ARGS__, 6> p7
271
272 #define TROMPELOEIL_PARAM_LIST6(...) \
273 TROMPELOEIL_PARAM_LIST5(__VA_ARGS__), \
274 ::trompeloeil::param_list_t<__VA_ARGS__, 5> p6
275
276 #define TROMPELOEIL_PARAM_LIST5(...) \
277 TROMPELOEIL_PARAM_LIST4(__VA_ARGS__), \
278 ::trompeloeil::param_list_t<__VA_ARGS__, 4> p5
279
280 #define TROMPELOEIL_PARAM_LIST4(...) \
281 TROMPELOEIL_PARAM_LIST3(__VA_ARGS__), \
282 ::trompeloeil::param_list_t<__VA_ARGS__, 3> p4
283
284 #define TROMPELOEIL_PARAM_LIST3(...) \
285 TROMPELOEIL_PARAM_LIST2(__VA_ARGS__), \
286 ::trompeloeil::param_list_t<__VA_ARGS__, 2> p3
287
288 #define TROMPELOEIL_PARAM_LIST2(...) \
289 TROMPELOEIL_PARAM_LIST1(__VA_ARGS__), \
290 ::trompeloeil::param_list_t<__VA_ARGS__, 1> p2
291
292 #define TROMPELOEIL_PARAM_LIST1(...) \
293 ::trompeloeil::param_list_t<__VA_ARGS__, 0> p1
294
295 #define TROMPELOEIL_PARAM_LIST0(func_type)
296
297 #define TROMPELOEIL_PARAM_LIST(num, func_type) \
298 TROMPELOEIL_CONCAT(TROMPELOEIL_PARAM_LIST, num) \
299 (TROMPELOEIL_REMOVE_PAREN(func_type))
300
301
302 #define TROMPELOEIL_PARAMS15 TROMPELOEIL_PARAMS14, p15
303 #define TROMPELOEIL_PARAMS14 TROMPELOEIL_PARAMS13, p14
304 #define TROMPELOEIL_PARAMS13 TROMPELOEIL_PARAMS12, p13
305 #define TROMPELOEIL_PARAMS12 TROMPELOEIL_PARAMS11, p12
306 #define TROMPELOEIL_PARAMS11 TROMPELOEIL_PARAMS10, p11
307 #define TROMPELOEIL_PARAMS10 TROMPELOEIL_PARAMS9, p10
308 #define TROMPELOEIL_PARAMS9 TROMPELOEIL_PARAMS8, p9
309 #define TROMPELOEIL_PARAMS8 TROMPELOEIL_PARAMS7, p8
310 #define TROMPELOEIL_PARAMS7 TROMPELOEIL_PARAMS6, p7
311 #define TROMPELOEIL_PARAMS6 TROMPELOEIL_PARAMS5, p6
312 #define TROMPELOEIL_PARAMS5 TROMPELOEIL_PARAMS4, p5
313 #define TROMPELOEIL_PARAMS4 TROMPELOEIL_PARAMS3, p4
314 #define TROMPELOEIL_PARAMS3 TROMPELOEIL_PARAMS2, p3
315 #define TROMPELOEIL_PARAMS2 TROMPELOEIL_PARAMS1, p2
316 #define TROMPELOEIL_PARAMS1 , p1
317 #define TROMPELOEIL_PARAMS0
318
319 #define TROMPELOEIL_PARAMS(num) TROMPELOEIL_CONCAT(TROMPELOEIL_PARAMS, num)
320
321 #if defined(__cxx_rtti) || defined(__GXX_RTTI) || defined(_CPPRTTI)
322 # define TROMPELOEIL_TYPE_ID_NAME(x) typeid(x).name()
323 #else
324 # define TROMPELOEIL_TYPE_ID_NAME(x) "object"
325 #endif
326
327 #if (TROMPELOEIL_CPLUSPLUS == 201103L)
328
329 #define TROMPELOEIL_DECLTYPE_AUTO \
330 auto
331
332 #define TROMPELOEIL_TRAILING_RETURN_TYPE(return_type) \
333 -> return_type
334
335 #else /* (TROMPELOEIL_CPLUSPLUS == 201103L) */
336
337 #define TROMPELOEIL_DECLTYPE_AUTO \
338 decltype(auto)
339
340 #define TROMPELOEIL_TRAILING_RETURN_TYPE(return_type) \
341 /**/
342
343 #endif /* !(TROMPELOEIL_CPLUSPLUS == 201103L) */
344
345 static constexpr bool trompeloeil_movable_mock = false;
346
347 namespace trompeloeil
348 {
349 template <typename T>
350 struct identity_type
351 {
352 using type = T;
353 };
354
355 template <typename R, typename C, typename ... Args>
356 identity_type<R(Args...)>
357 nonconst_member_signature(R (C::*)(Args...))
358 {
359 return {};
360 }
361
362 template <typename R, typename C, typename ... Args>
363 identity_type<R(Args...)>
364 const_member_signature(R (C::*)(Args...) const)
365 {
366 return {};
367 }
368
369 template <typename ...>
370 struct void_t_
371 {
372 using type = void;
373 };
374
375 template <typename ... T>
376 using void_t = typename void_t_<T...>::type;
377
378 template <template <typename ...> class, typename, typename ...>
379 struct is_detected_{
380 using type = std::false_type;
381 };
382
383 template <template <typename ...> class D, typename ... Ts>
384 struct is_detected_<D, void_t<D<Ts...>>, Ts...>
385 {
386 using type = std::true_type;
387 };
388
389 template <template <typename ...> class D, typename ... Ts>
390 using is_detected = typename is_detected_<D, void, Ts...>::type;
391
392 # if (TROMPELOEIL_CPLUSPLUS == 201103L)
393
394 namespace detail
395 {
396 /* Implement C++14 features using only C++11 entities. */
397
398 /* <memory> */
399
400 /* Implementation of make_unique is from
401 *
402 * Stephan T. Lavavej, "make_unique (Revision 1),"
403 * ISO/IEC JTC1 SC22 WG21 N3656, 18 April 2013.
404 * Available: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3656.htm
405 * Accessed: 14 June 2017
406 */
407 template <class T>
408 struct _Unique_if
409 {
410 typedef std::unique_ptr<T> _Single_object;
411 };
412
413 template <class T>
414 struct _Unique_if<T[]>
415 {
416 typedef std::unique_ptr<T[]> _Unknown_bound;
417 };
418
419 template <class T, size_t N>
420 struct _Unique_if<T[N]>
421 {
422 typedef void _Known_bound;
423 };
424
425 template <class T, class... Args>
426 typename _Unique_if<T>::_Single_object
427 make_unique(Args&&... args)
428 {
429 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
430 }
431
432 template <class T>
433 typename _Unique_if<T>::_Unknown_bound
434 make_unique(size_t n)
435 {
436 typedef typename std::remove_extent<T>::type U;
437 return std::unique_ptr<T>(new U[n]());
438 }
439
440 template <class T, class... Args>
441 typename _Unique_if<T>::_Known_bound
442 make_unique(Args&&...) = delete;
443
444 /* <type_traits> */
445
446 /* The implementation of these is from
447 *
448 * Walter E. Brown, "TransformationTraits Redux, v2,"
449 * ISO/IEC JTC1 SC22 WG21 N3655, 18 April 2013.
450 * Available: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3655.pdf
451 * Accessed: 2 November 2017
452 *
453 * Minor changes to capitalize template parameter `bool B` has been made.
454 *
455 * See also:
456 * http://en.cppreference.com/w/cpp/types/conditional
457 * http://en.cppreference.com/w/cpp/types/decay
458 * http://en.cppreference.com/w/cpp/types/enable_if
459 * http://en.cppreference.com/w/cpp/types/remove_pointer
460 * http://en.cppreference.com/w/cpp/types/remove_reference
461 * Accessed: 17 May 2017
462 */
463 template <bool B, typename T, typename F>
464 using conditional_t = typename std::conditional<B, T, F>::type;
465
466 template <typename T>
467 using decay_t = typename std::decay<T>::type;
468
469 template <bool B, typename T = void>
470 using enable_if_t = typename std::enable_if<B, T>::type;
471
472 template <typename T>
473 using remove_pointer_t = typename std::remove_pointer<T>::type;
474
475 template <typename T>
476 using remove_reference_t = typename std::remove_reference<T>::type;
477
478 /* <utility> */
479
480 /* This implementation of exchange is from
481 *
482 * Jeffrey Yasskin, "exchange() utility function, revision 3,"
483 * ISO/IEC JTC1 SC22 WG21 N3688, 19 April 2013.
484 * Available: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3668.html
485 * Accessed: 2 November 2017
486 *
487 * See also:
488 * http://en.cppreference.com/w/cpp/utility/exchange
489 * Accessed: 17 May 2017
490 */
491 template<class T, class U = T>
492 inline
493 T
494 exchange(
495 T& obj,
496 U&& new_value)
497 {
498 T old_value = std::move(obj);
499 obj = std::forward<U>(new_value);
500 return old_value;
501 }
502
503 /* integer_sequence and index_sequence implemenations are from
504 *
505 * Jonathan Wakely, "Compile-time integer sequences,"
506 * ISO/IEC JTC1 SC22 WG21 N3658, 18 April 2013.
507 * Available: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3658.html
508 * Accessed: 2 November 2017
509 *
510 * See also:
511 * http://en.cppreference.com/w/cpp/utility/integer_sequence
512 * Accessed: 17 May 2017
513 */
514 template <typename T, T... I>
515 struct integer_sequence
516 {
517 // Replaces a typedef used in the definition found in N3658.
518 using value_type = T;
519
520 static constexpr size_t size() noexcept
521 {
522 return sizeof...(I);
523 }
524 };
525
526 template <size_t... I>
527 using index_sequence = integer_sequence<size_t, I...>;
528
529 /* This implementation of make_integer_sequence is from boost/mp11,
530 *
531 * Copyright 2015, 2017 Peter Dimov
532 *
533 * Distributed under the Boost Software License, Version 1.0.
534 *
535 * Implemented here:
536 *
537 * https://github.com/pdimov/mp11/blob/master/include/boost/
538 * integer_sequence.hpp
539 * Accessed: 17 May 2017
540 *
541 * (now missing) and here:
542 *
543 * https://github.com/boostorg/mp11/blob/develop/include/boost/
544 * mp11/integer_sequence.hpp
545 * Accessed: 13 August 2017
546 */
547 namespace impl
548 {
549 // iseq_if_c
550 template <bool C, class T, class E>
551 struct iseq_if_c_impl;
552
553 template <class T, class E>
554 struct iseq_if_c_impl<true, T, E>
555 {
556 using type = T;
557 };
558
559 template <class T, class E>
560 struct iseq_if_c_impl<false, T, E>
561 {
562 using type = E;
563 };
564
565 template <bool C, class T, class E>
566 using iseq_if_c = typename iseq_if_c_impl<C, T, E>::type;
567
568 // iseq_identity
569 template <class T>
570 struct iseq_identity
571 {
572 using type = T;
573 };
574
575 template <class S1, class S2>
576 struct append_integer_sequence;
577
578 template <class T, T... I, T... J>
579 struct append_integer_sequence<integer_sequence<T, I...>, integer_sequence<T, J...>>
580 {
581 using type = integer_sequence<T, I..., ( J + sizeof...(I) )...>;
582 };
583
584 template <class T, T N>
585 struct make_integer_sequence_impl;
586
587 template <class T, T N>
588 struct make_integer_sequence_impl_
589 {
590 private:
591
592 static_assert( N >= 0, "make_integer_sequence<T, N>: N must not be negative" );
593
594 static T const M = N / 2;
595 static T const R = N % 2;
596
597 using S1 = typename make_integer_sequence_impl<T, M>::type;
598 using S2 = typename append_integer_sequence<S1, S1>::type;
599 using S3 = typename make_integer_sequence_impl<T, R>::type;
600 using S4 = typename append_integer_sequence<S2, S3>::type;
601
602 public:
603
604 using type = S4;
605 };
606
607 template <class T, T N>
608 struct make_integer_sequence_impl:
609 iseq_if_c<N == 0,
610 iseq_identity<integer_sequence<T>>,
611 iseq_if_c<N == 1,
612 iseq_identity<integer_sequence<T, 0>>,
613 make_integer_sequence_impl_<T, N>>>
614 {
615 };
616 }
617
618 template<class T, T N>
619 using make_integer_sequence = typename impl::make_integer_sequence_impl<T, N>::type;
620
621 template <size_t N>
622 using make_index_sequence = make_integer_sequence<size_t, N>;
623
624 template <typename... T>
625 using index_sequence_for = make_index_sequence<sizeof...(T)>;
626
627 } /* namespace detail */
628
629 # else /* (TROMPELOEIL_CPLUSPLUS == 201103L) */
630
631 /* Only these entities really need to
632 * be available in namespace detail, but
633 * VS 2015 has a problem with this approach.
634 *
635 * namespace detail {
636 *
637 * using std::make_unique;
638 *
639 * using std::conditional_t;
640 * using std::decay_t;
641 * using std::enable_if_t;
642 * using std::remove_pointer_t;
643 * using std::remove_reference_t;
644 *
645 * using std::exchange;
646 * using std::index_sequence;
647 * using std::index_sequence_for;
648 * using std::integer_sequence;
649 * using std::make_index_sequence;
650 *
651 * }
652 *
653 * Instead, use a namespace alias.
654 */
655 namespace detail = std;
656
657 # endif /* !(TROMPELOEIL_CPLUSPLUS == 201103L) */
658
659 # if TROMPELOEIL_CPLUSPLUS >= 201703L
660 # if TROMPELOEIL_CLANG && TROMPELOEIL_CLANG_VERSION >= 60000
661
662 // these are mostly added to work around clang++ bugs
663 // https://bugs.llvm.org/show_bug.cgi?id=38033
664 // https://bugs.llvm.org/show_bug.cgi?id=38010
665
666 template <typename T>
667 using move_construct_type = decltype(T(std::declval<T&&>()));
668
669 template <typename T>
670 using copy_construct_type = decltype(T(std::declval<const T&>()));
671
672 template <typename T>
673 using can_move_construct = is_detected<move_construct_type, detail::decay_t<T>>;
674
675 template <typename T>
676 using can_copy_construct = is_detected<copy_construct_type, detail::decay_t<T>>;
677
678 # else
679 template <typename T>
680 using can_move_construct = std::is_move_constructible<T>;
681
682 template <typename T>
683 using can_copy_construct = std::is_copy_constructible<T>;
684 # endif
685
686 template <typename F, typename ... A>
687 using invoke_result_type = decltype(std::declval<F&>()(std::declval<A>()...));
688
689 # else
690
691 template <typename T>
692 using can_move_construct = std::is_move_constructible<T>;
693
694 template <typename T>
695 using can_copy_construct = std::is_copy_constructible<T>;
696
697 template <typename F, typename ... A>
698 using invoke_result_type = decltype(std::declval<F&>()(std::declval<A>()...));
699
700 # endif
701
702 class specialized;
703
704 template <typename T>
705 using aligned_storage_for =
706 typename std::aligned_storage<sizeof(T), alignof(T)>::type;
707
708 template <typename T = void>
709 std::unique_lock<std::recursive_mutex> get_lock()
710 {
711 // Ugly hack for lifetime of mutex. The statically allocated
712 // recursive_mutex is intentionally leaked, to ensure that the
713 // mutex is available and valid even if the last use is from
714 // the destructor of a global object in a translation unit
715 // without #include <trompeloeil.hpp>
716
717 static aligned_storage_for<std::recursive_mutex> buffer;
718 static auto mutex = new (&buffer) std::recursive_mutex;
719 return std::unique_lock<std::recursive_mutex>{*mutex};
720 }
721
722 template <size_t N, typename T>
723 using conditional_tuple_element
724 = detail::conditional_t<(N < std::tuple_size<T>::value),
725 typename std::tuple_element<N, T>::type,
726 int>;
727
728 template <typename T>
729 struct param_list;
730
731 template <typename R, typename ... P>
732 struct param_list<R(P...)>
733 {
734 static size_t constexpr const size = sizeof...(P);
735 template <size_t N>
736 using type = conditional_tuple_element<N, std::tuple<P...>>;
737 };
738
739 template <typename Sig, size_t N>
740 using param_list_t = typename param_list<Sig>::template type<N>;
741
742 class expectation_violation : public std::logic_error
743 {
744 public:
745 using std::logic_error::logic_error;
746 };
747
748 struct location
749 {
750 location()
751 noexcept
752 : file("")
753 , line(0U)
754 {}
755
756 location(
757 char const* file_,
758 unsigned long line_
759 )
760 noexcept
761 : file{file_}
762 , line{line_}
763 {}
764
765 char const *file;
766 unsigned long line;
767 };
768
769 inline
770 std::ostream&
771 operator<<(
772 std::ostream& os,
773 const location& loc)
774 {
775 if (loc.line != 0U) os << loc.file << ':' << loc.line;
776 return os;
777 }
778
779 enum class severity { fatal, nonfatal };
780
781 using reporter_func = std::function<void(severity,
782 char const *file,
783 unsigned long line,
784 std::string const &msg)>;
785
786 using ok_reporter_func = std::function<void(char const *msg)>;
787
788 inline
789 void
790 default_reporter(
791 severity,
792 char const *file,
793 unsigned long line,
794 std::string const &msg)
795 {
796 if (!std::current_exception())
797 {
798 std::stringstream os;
799 os << location{ file, line } << "\n" << msg;
800 throw expectation_violation(os.str());
801 }
802 }
803
804 inline
805 void
806 default_ok_reporter(char const* /*msg*/)
807 {
808 /* OK reporter defaults to doing nothing. */
809 }
810
811 inline
812 reporter_func&
813 reporter_obj()
814 {
815 static reporter_func obj = default_reporter;
816 return obj;
817 }
818
819 inline
820 ok_reporter_func&
821 ok_reporter_obj()
822 {
823 static ok_reporter_func obj = default_ok_reporter;
824 return obj;
825 }
826
827 inline
828 reporter_func
829 set_reporter(
830 reporter_func f)
831 {
832 return detail::exchange(reporter_obj(), std::move(f));
833 }
834
835 inline
836 std::pair<reporter_func, ok_reporter_func>
837 set_reporter(
838 reporter_func rf,
839 ok_reporter_func orf)
840 {
841 return {
842 set_reporter(rf),
843 detail::exchange(ok_reporter_obj(), std::move(orf))
844 };
845 }
846
847 class tracer;
848
849 inline
850 tracer*&
851 tracer_obj()
852 noexcept
853 {
854 static tracer* ptr = nullptr;
855 return ptr;
856 }
857
858 inline
859 tracer*
860 set_tracer(
861 tracer* obj)
862 noexcept
863 {
864 // std::exchange would be sane here, but it costs compilation time
865 auto& ptr = tracer_obj();
866 auto rv = ptr;
867 ptr = obj;
868 return rv;
869 }
870
871 class tracer
872 {
873 public:
874 virtual
875 void
876 trace(
877 char const *file,
878 unsigned long line,
879 std::string const &call) = 0;
880 protected:
881 tracer() = default;
882 tracer(tracer const&) = delete;
883 tracer& operator=(tracer const&) = delete;
884 virtual
885 ~tracer()
886 {
887 set_tracer(previous);
888 }
889 private:
890 tracer* previous = set_tracer(this);
891 };
892
893 class stream_tracer : public tracer
894 {
895 public:
896 stream_tracer(
897 std::ostream& stream_)
898 : stream(stream_) {}
899 void
900 trace(
901 char const *file,
902 unsigned long line,
903 std::string const &call)
904 override
905 {
906 stream << location{file, line} << '\n' << call << '\n';
907 }
908 private:
909 std::ostream& stream;
910 };
911
912 class trace_agent;
913
914 template <typename T>
915 struct reporter;
916
917 template <typename T>
918 void
919 send_report(
920 severity s,
921 location loc,
922 std::string const &msg)
923 {
924 reporter<T>::send(s, loc.file, loc.line, msg.c_str());
925 }
926
927 template <typename T>
928 void
929 send_ok_report(
930 std::string const &msg)
931 {
932 reporter<T>::sendOk(msg.c_str());
933 }
934
935 template <typename T>
936 struct reporter
937 {
938 static
939 void
940 send(
941 severity s,
942 char const *file,
943 unsigned long line,
944 char const *msg);
945
946 static
947 void
948 sendOk(
949 char const *msg);
950
951 };
952
953 template <typename T>
954 void reporter<T>::
955 send(
956 severity s,
957 char const *file,
958 unsigned long line,
959 char const *msg)
960 {
961 reporter_obj()(s, file, line, msg);
962 }
963
964 template <typename T>
965 void reporter<T>::
966 sendOk(char const* msg)
967 {
968 ok_reporter_obj()(msg);
969 }
970
971 template <typename ... T>
972 inline
973 constexpr
974 bool
975 ignore(
976 T const& ...)
977 noexcept
978 {
979 return true;
980 }
981
982 struct illegal_argument
983 {
984 template <bool b = false>
985 constexpr
986 illegal_argument const& operator&() const
987 {
988 static_assert(b, "illegal argument");
989 return *this;
990 }
991
992 template <bool b = false>
993 constexpr
994 illegal_argument const& operator*() const
995 {
996 static_assert(b, "illegal argument");
997 return *this;
998 }
999
1000 template <typename T, bool b = false>
1001 constexpr
1002 illegal_argument const& operator=(T const&) const
1003 {
1004 static_assert(b, "illegal argument");
1005 return *this;
1006 }
1007
1008 template <typename T, bool b = false>
1009 constexpr
1010 operator T() const
1011 {
1012 static_assert(b, "illegal argument");
1013 return {};
1014 }
1015 };
1016
1017 struct matcher { };
1018
1019 struct wildcard : public matcher
1020 {
1021 template <typename T>
1022 operator T&&()
1023 const;
1024
1025 template <typename T>
1026 operator T&()
1027 const volatile; // less preferred than T&& above
1028
1029 template <typename T>
1030 constexpr
1031 bool
1032 matches(
1033 T const&)
1034 const
1035 noexcept
1036 {
1037 return true;
1038 }
1039
1040 friend
1041 std::ostream&
1042 operator<<(
1043 std::ostream& os,
1044 wildcard const&)
1045 noexcept
1046 {
1047 return os << " matching _";
1048 }
1049 };
1050
1051 static constexpr wildcard const _{};
1052
1053
1054 template <typename T>
1055 using matcher_access = decltype(static_cast<matcher*>(std::declval<typename std::add_pointer<T>::type>()));
1056
1057 template <typename T>
1058 using is_matcher = typename is_detected<matcher_access, T>::type;
1059
1060 template <typename T>
1061 struct typed_matcher : matcher
1062 {
1063 operator T() const;
1064 };
1065
1066 template <>
1067 struct typed_matcher<std::nullptr_t> : matcher
1068 {
1069 template <typename T, typename = decltype(std::declval<T>() == nullptr)>
1070 operator T&&() const;
1071
1072 template <typename T,
1073 typename = decltype(std::declval<T>() == nullptr)>
1074 operator T&()const volatile;
1075
1076 template <typename T, typename C>
1077 operator T C::*() const;
1078 };
1079
1080 template <typename Pred, typename ... T>
1081 class duck_typed_matcher : public matcher
1082 {
1083 public:
1084 #if (!TROMPELOEIL_GCC) || \
1085 (TROMPELOEIL_GCC && TROMPELOEIL_GCC_VERSION >= 40900)
1086
1087 // g++ 4.8 gives a "conversion from <T> to <U> is ambiguous" error
1088 // if this operator is defined.
1089 template <typename V,
1090 typename = detail::enable_if_t<!is_matcher<V>{}>,
1091 typename = invoke_result_type<Pred, V&&, T...>>
1092 operator V&&() const;
1093
1094 #endif
1095
1096 template <typename V,
1097 typename = detail::enable_if_t<!is_matcher<V>{}>,
1098 typename = invoke_result_type<Pred, V&, T...>>
1099 operator V&() const volatile;
1100 };
1101
1102 template <typename T>
1103 using ostream_insertion = decltype(std::declval<std::ostream&>() << std::declval<T>());
1104
1105 template <typename T>
1106 using is_output_streamable = std::integral_constant<bool, is_detected<ostream_insertion, T>::value && !std::is_array<T>::value>;
1107
1108 struct stream_sentry
1109 {
1110 stream_sentry(
1111 std::ostream& os_)
1112 : os(os_)
1113 , width(os.width(0))
1114 , flags(os.flags(std::ios_base::dec | std::ios_base::left))
1115 , fill(os.fill(' '))
1116 { }
1117 ~stream_sentry()
1118 {
1119 os.flags(flags);
1120 os.fill(fill);
1121 os.width(width);
1122 }
1123 private:
1124 std::ostream& os;
1125 std::streamsize width;
1126 std::ios_base::fmtflags flags;
1127 char fill;
1128 };
1129
1130 struct indirect_null {
1131 #if !TROMPELOEIL_CLANG
1132 template <
1133 typename T,
1134 typename = detail::enable_if_t<!std::is_constructible<T, std::nullptr_t>::value>
1135 >
1136 operator T&&() const = delete;
1137 #endif
1138 #if TROMPELOEIL_GCC
1139
1140 template <typename T, typename C, typename ... As>
1141 using memfunptr = T (C::*)(As...);
1142
1143 template <typename T>
1144 operator T*() const;
1145 template <typename T, typename C>
1146 operator T C::*() const;
1147 template <typename T, typename C, typename ... As>
1148 operator memfunptr<T,C,As...>() const;
1149 #endif /* TROMPELOEIL_GCC */
1150 operator std::nullptr_t() const;
1151 };
1152
1153 template <typename T, typename U>
1154 using equality_comparison = decltype((std::declval<T const&>() == std::declval<U const&>())
1155 ? true
1156 : false);
1157
1158 template <typename T, typename U>
1159 using is_equal_comparable = is_detected<equality_comparison, T, U>;
1160
1161 #if defined(_MSC_VER) && (_MSC_VER < 1910)
1162 template <typename T>
1163 using is_null_comparable = is_equal_comparable<T, std::nullptr_t>;
1164 #else
1165 template <typename T>
1166 using is_null_comparable = is_equal_comparable<T, indirect_null>;
1167 #endif
1168
1169 template <typename T>
1170 inline
1171 constexpr
1172 auto
1173 is_null(
1174 T const &t,
1175 std::true_type)
1176 noexcept(noexcept(std::declval<T const&>() == nullptr))
1177 -> decltype(t == nullptr)
1178 {
1179 return t == nullptr;
1180 }
1181
1182 template <typename T>
1183 inline
1184 constexpr
1185 bool
1186 is_null(
1187 T const &,
1188 std::false_type)
1189 noexcept
1190 {
1191 return false;
1192 }
1193
1194 template <typename T>
1195 inline
1196 constexpr
1197 bool
1198 is_null(
1199 T const &t)
1200 {
1201 // g++-4.9 uses C++11 rules for constexpr function, so can't
1202 // break this up into smaller bits
1203 using tag = std::integral_constant<bool, is_null_comparable<T>::value
1204 && !is_matcher<T>::value
1205 && !std::is_array<T>::value>;
1206
1207 return ::trompeloeil::is_null(t, tag{});
1208 }
1209
1210 template <typename T>
1211 void
1212 print(
1213 std::ostream& os,
1214 T const &t);
1215
1216 template <typename T>
1217 using iterable = decltype(std::begin(std::declval<T&>()) == std::end(std::declval<T&>()));
1218
1219 template <typename T>
1220 using is_collection = is_detected<iterable, T>;
1221
1222 template <typename T,
1223 bool = is_output_streamable<T>::value,
1224 bool = is_collection<detail::remove_reference_t<T>>::value>
1225 struct streamer
1226 {
1227 static
1228 void
1229 print(
1230 std::ostream& os,
1231 T const &t)
1232 {
1233 stream_sentry s(os);
1234 os << t;
1235 }
1236 };
1237
1238 template <typename ... T>
1239 struct streamer<std::tuple<T...>, false, false>
1240 {
1241 static
1242 void
1243 print(
1244 std::ostream& os,
1245 std::tuple<T...> const& t)
1246 {
1247 print_tuple(os, t, detail::index_sequence_for<T...>{});
1248 }
1249 template <size_t ... I>
1250 static
1251 void
1252 print_tuple(
1253 std::ostream& os,
1254 std::tuple<T...> const& t,
1255 detail::index_sequence<I...>)
1256 {
1257 os << "{ ";
1258 const char* sep = "";
1259 std::initializer_list<const char*> v{((os << sep),
1260 ::trompeloeil::print(os, std::get<I>(t)),
1261 (sep = ", "))...};
1262 ignore(v);
1263 os << " }";
1264 }
1265 };
1266
1267 template <typename T, typename U>
1268 struct streamer<std::pair<T, U>, false, false>
1269 {
1270 static
1271 void
1272 print(
1273 std::ostream& os,
1274 std::pair<T, U> const& t)
1275 {
1276 os << "{ ";
1277 ::trompeloeil::print(os, t.first);
1278 os << ", ";
1279 ::trompeloeil::print(os, t.second);
1280 os << " }";
1281 }
1282 };
1283
1284 template <typename T>
1285 struct streamer<T, false, true>
1286 {
1287 static
1288 void
1289 print(
1290 std::ostream& os,
1291 T const& t)
1292 {
1293 os << "{ ";
1294 const char* sep = "";
1295 auto const end = std::end(t);
1296 for (auto i = std::begin(t); i != end; ++i)
1297 {
1298 os << sep;
1299 ::trompeloeil::print(os, *i);
1300 sep = ", ";
1301 }
1302 os << " }";
1303 }
1304 };
1305
1306 template <typename T>
1307 struct streamer<T, false, false>
1308 {
1309 static
1310 void
1311 print(
1312 std::ostream& os,
1313 T const &t)
1314 {
1315 stream_sentry s(os);
1316 static const char *linebreak = "\n";
1317 os << sizeof(T) << "-byte object={";
1318 os << (linebreak + (sizeof(T) <= 8)); // stupid construction silences VS2015 warning
1319 os << std::setfill('0') << std::hex;
1320 auto p = reinterpret_cast<uint8_t const*>(&t);
1321 for (size_t i = 0; i < sizeof(T); ++i)
1322 {
1323 os << " 0x" << std::setw(2) << unsigned(p[i]);
1324 if ((i & 0xf) == 0xf) os << '\n';
1325 }
1326 os << " }";
1327 }
1328 };
1329
1330 template <typename T>
1331 void
1332 print(
1333 std::ostream& os,
1334 T const &t)
1335 {
1336 if (is_null(t))
1337 {
1338 os << "nullptr";
1339 }
1340 else
1341 {
1342 streamer<T>::print(os, t);
1343 }
1344 }
1345
1346 inline
1347 void
1348 print(
1349 std::ostream& os,
1350 std::nullptr_t)
1351 {
1352 os << "nullptr";
1353 }
1354
1355 inline
1356 constexpr
1357 auto
1358 param_compare_operator(
1359 ...)
1360 TROMPELOEIL_TRAILING_RETURN_TYPE(const char*)
1361 {
1362 return " == ";
1363 }
1364
1365 inline
1366 constexpr
1367 auto
1368 param_compare_operator(
1369 matcher const*)
1370 TROMPELOEIL_TRAILING_RETURN_TYPE(const char*)
1371 {
1372 return "";
1373 }
1374
1375 template <typename T>
1376 void
1377 print_expectation(
1378 std::ostream& os,
1379 T const& t)
1380 {
1381 os << param_compare_operator(&t);
1382 print(os, t);
1383 os << '\n';
1384 }
1385
1386 template <typename T>
1387 class list_elem
1388 {
1389 public:
1390 list_elem(
1391 const list_elem&)
1392 = delete;
1393
1394 list_elem(
1395 list_elem &&r)
1396 noexcept
1397 {
1398 *this = std::move(r);
1399 }
1400
1401 list_elem&
1402 operator=(
1403 list_elem &&r)
1404 noexcept
1405 {
1406 if (this != &r)
1407 {
1408 next = r.next;
1409 prev = &r;
1410 r.invariant_check();
1411
1412 next->prev = this;
1413 r.next = this;
1414
1415 TROMPELOEIL_ASSERT(next->prev == this);
1416 TROMPELOEIL_ASSERT(prev->next == this);
1417
1418 r.unlink();
1419
1420 TROMPELOEIL_ASSERT(!r.is_linked());
1421 invariant_check();
1422 }
1423 return *this;
1424 }
1425
1426 list_elem&
1427 operator=(
1428 const list_elem&)
1429 = delete;
1430
1431 virtual
1432 ~list_elem()
1433 {
1434 unlink();
1435 }
1436
1437 void
1438 unlink()
1439 noexcept
1440 {
1441 invariant_check();
1442 auto n = next;
1443 auto p = prev;
1444 n->prev = p;
1445 p->next = n;
1446 next = this;
1447 prev = this;
1448 invariant_check();
1449 }
1450
1451 void
1452 invariant_check()
1453 const
1454 noexcept
1455 {
1456 #ifdef TROMPELOEIL_SANITY_CHECKS
1457 TROMPELOEIL_ASSERT(next->prev == this);
1458 TROMPELOEIL_ASSERT(prev->next == this);
1459 TROMPELOEIL_ASSERT((next == this) == (prev == this));
1460 TROMPELOEIL_ASSERT((prev->next == next) == (next == this));
1461 TROMPELOEIL_ASSERT((next->prev == prev) == (prev == this));
1462 auto pp = prev;
1463 auto nn = next;
1464 do {
1465 TROMPELOEIL_ASSERT((nn == this) == (pp == this));
1466 TROMPELOEIL_ASSERT(nn->next->prev == nn);
1467 TROMPELOEIL_ASSERT(nn->prev->next == nn);
1468 TROMPELOEIL_ASSERT(pp->next->prev == pp);
1469 TROMPELOEIL_ASSERT(pp->prev->next == pp);
1470 TROMPELOEIL_ASSERT((nn->next == nn) == (nn == this));
1471 TROMPELOEIL_ASSERT((pp->prev == pp) == (pp == this));
1472 nn = nn->next;
1473 pp = pp->prev;
1474 } while (nn != this);
1475 #endif
1476 }
1477
1478 bool
1479 is_linked()
1480 const
1481 noexcept
1482 {
1483 invariant_check();
1484 return next != this;
1485 }
1486 protected:
1487 list_elem() noexcept = default;
1488 public:
1489 list_elem* next = this;
1490 list_elem* prev = this;
1491 };
1492
1493 class ignore_disposer
1494 {
1495 protected:
1496 template <typename T>
1497 TROMPELOEIL_NORETURN
1498 void
1499 dispose(
1500 T*)
1501 const
1502 noexcept
1503 {
1504 std::abort(); // must never be called
1505 }
1506 };
1507
1508 class delete_disposer
1509 {
1510 protected:
1511 template <typename T>
1512 void
1513 dispose(
1514 T* t)
1515 const
1516 {
1517 delete t;
1518 }
1519 };
1520
1521 template <typename T, typename Disposer = ignore_disposer>
1522 class list : private list_elem<T>, private Disposer
1523 {
1524 public:
1525 list() noexcept;
1526 list(list&&) noexcept;
1527 list(const list&) = delete;
1528 list& operator=(list&&) noexcept;
1529 list& operator=(const list&) = delete;
1530 ~list();
1531 class iterator;
1532 iterator begin() const noexcept;
1533 iterator end() const noexcept;
1534 iterator push_front(T* t) noexcept;
1535 iterator push_back(T* t) noexcept;
1536 bool empty() const noexcept { return begin() == end(); }
1537 private:
1538 using list_elem<T>::invariant_check;
1539 using list_elem<T>::next;
1540 using list_elem<T>::prev;
1541 };
1542
1543 template <typename T, typename Disposer>
1544 class list<T, Disposer>::iterator
1545 {
1546 friend class list<T, Disposer>;
1547 typedef std::bidirectional_iterator_tag iterator_category;
1548 typedef T value_type;
1549 typedef std::ptrdiff_t difference_type;
1550 typedef T* pointer;
1551 typedef T& reference;
1552 public:
1553 iterator()
1554 noexcept
1555 : p{nullptr}
1556 {}
1557
1558 friend
1559 bool
1560 operator==(
1561 iterator const &lh,
1562 iterator const &rh)
1563 noexcept
1564 {
1565 return lh.p == rh.p;
1566 }
1567
1568 friend
1569 bool
1570 operator!=(
1571 iterator const &lh,
1572 iterator const &rh)
1573 noexcept
1574 {
1575 return !(lh == rh);
1576 }
1577
1578 iterator&
1579 operator++()
1580 noexcept
1581 {
1582 p = p->next;
1583 return *this;
1584 }
1585
1586 iterator
1587 operator++(int)
1588 noexcept
1589 {
1590 auto rv = *this;
1591 operator++();
1592 return rv;
1593 }
1594
1595 T&
1596 operator*()
1597 noexcept
1598 {
1599 return static_cast<T&>(*p);
1600 }
1601
1602 T*
1603 operator->()
1604 noexcept
1605 {
1606 return static_cast<T*>(p);
1607 }
1608
1609 private:
1610 iterator(
1611 list_elem<T> const *t)
1612 noexcept
1613 : p{const_cast<list_elem<T>*>(t)}
1614 {}
1615
1616 list_elem<T>* p;
1617 };
1618
1619 template <typename T, typename Disposer>
1620 list<T, Disposer>::list() noexcept = default;
1621
1622 template <typename T, typename Disposer>
1623 list<T, Disposer>::list(list&&) noexcept = default;
1624
1625 template <typename T, typename Disposer>
1626 list<T, Disposer>& list<T, Disposer>::operator=(list&&) noexcept = default;
1627
1628 template <typename T, typename Disposer>
1629 list<T, Disposer>::~list()
1630 {
1631 auto i = this->begin();
1632 while (i != this->end())
1633 {
1634 auto p = i++;
1635 Disposer::dispose(&*p);
1636 }
1637 }
1638
1639 template <typename T, typename Disposer>
1640 auto
1641 list<T, Disposer>::begin()
1642 const
1643 noexcept
1644 -> iterator
1645 {
1646 return {next};
1647 }
1648
1649 template <typename T, typename Disposer>
1650 auto
1651 list<T, Disposer>::end()
1652 const
1653 noexcept
1654 -> iterator
1655 {
1656 return {this};
1657 }
1658
1659 template <typename T, typename Disposer>
1660 auto
1661 list<T, Disposer>::push_front(
1662 T* t)
1663 noexcept
1664 -> iterator
1665 {
1666 invariant_check();
1667 t->next = next;
1668 t->prev = this;
1669 next->prev = t;
1670 next = t;
1671 invariant_check();
1672 return {t};
1673 }
1674
1675 template <typename T, typename Disposer>
1676 auto
1677 list<T, Disposer>::push_back(
1678 T* t)
1679 noexcept
1680 -> iterator
1681 {
1682 invariant_check();
1683 t->prev = prev;
1684 t->next = this;
1685 prev->next = t;
1686 prev = t;
1687 invariant_check();
1688 return {t};
1689 }
1690
1691 class sequence_matcher;
1692
1693 class sequence_type
1694 {
1695 public:
1696 sequence_type() noexcept = default;
1697 sequence_type(sequence_type&&) noexcept = delete;
1698 sequence_type(const sequence_type&) = delete;
1699 sequence_type& operator=(sequence_type&&) noexcept = delete;
1700 sequence_type& operator=(const sequence_type&) = delete;
1701 ~sequence_type();
1702
1703 bool
1704 is_completed()
1705 const
1706 noexcept;
1707
1708 bool
1709 is_first(
1710 sequence_matcher const *m)
1711 const
1712 noexcept;
1713
1714 void
1715 add_last(
1716 sequence_matcher *m)
1717 noexcept;
1718
1719 void
1720 validate_match(
1721 severity s,
1722 sequence_matcher const *matcher,
1723 char const *seq_name,
1724 char const *match_name,
1725 location loc)
1726 const;
1727
1728 private:
1729 list<sequence_matcher> matchers;
1730 };
1731
1732 class sequence
1733 {
1734 public:
1735 sequence() : obj(new sequence_type) {}
1736 sequence_type& operator*() { return *obj; }
1737 bool is_completed() const { return obj->is_completed(); }
1738 private:
1739 std::unique_ptr<sequence_type> obj;
1740 };
1741
1742 class sequence_matcher : public list_elem<sequence_matcher>
1743 {
1744 public:
1745 using init_type = std::pair<char const*, sequence&>;
1746 sequence_matcher(
1747 char const *exp,
1748 location loc,
1749 init_type i)
1750 noexcept
1751 : seq_name(i.first)
1752 , exp_name(exp)
1753 , exp_loc(loc)
1754 , seq(*i.second)
1755 {
1756 auto lock = get_lock();
1757 seq.add_last(this);
1758 }
1759
1760 void
1761 validate_match(
1762 severity s,
1763 char const *match_name,
1764 location loc)
1765 const
1766 {
1767 seq.validate_match(s, this, seq_name, match_name, loc);
1768 }
1769
1770 bool
1771 is_first()
1772 const
1773 noexcept
1774 {
1775 return seq.is_first(this);
1776 }
1777
1778 void
1779 retire()
1780 noexcept
1781 {
1782 this->unlink();
1783 }
1784
1785 void
1786 print_expectation(std::ostream& os)
1787 const
1788 {
1789 os << exp_name << " at " << exp_loc;
1790 }
1791
1792 char const*
1793 sequence_name()
1794 noexcept
1795 {
1796 return seq_name;
1797 }
1798 private:
1799 char const *seq_name;
1800 char const *exp_name;
1801 location exp_loc;
1802 sequence_type& seq;
1803 };
1804
1805 inline
1806 bool
1807 sequence_type::is_completed()
1808 const
1809 noexcept
1810 {
1811 return matchers.empty();
1812 }
1813
1814 inline
1815 bool
1816 sequence_type::is_first(
1817 sequence_matcher const *m)
1818 const
1819 noexcept
1820 {
1821 return !matchers.empty() && &*matchers.begin() == m;
1822 }
1823
1824 inline
1825 void
1826 sequence_type::validate_match(
1827 severity s,
1828 sequence_matcher const *matcher,
1829 char const* seq_name,
1830 char const* match_name,
1831 location loc)
1832 const
1833 {
1834 if (is_first(matcher)) return;
1835 for (auto& m : matchers)
1836 {
1837 std::ostringstream os;
1838 os << "Sequence mismatch for sequence \"" << seq_name
1839 << "\" with matching call of " << match_name
1840 << " at " << loc
1841 << ". Sequence \"" << seq_name << "\" has ";
1842 m.print_expectation(os);
1843 os << " first in line\n";
1844 send_report<specialized>(s, loc, os.str());
1845 }
1846 }
1847
1848 inline
1849 sequence_type::~sequence_type()
1850 {
1851 bool touched = false;
1852 std::ostringstream os;
1853 while (!matchers.empty())
1854 {
1855 auto m = matchers.begin();
1856 if (!touched)
1857 {
1858 os << "Sequence expectations not met at destruction of sequence object \""
1859 << m->sequence_name() << "\":";
1860 touched = true;
1861 }
1862 os << "\n missing ";
1863 m->print_expectation(os);
1864 m->unlink();
1865 }
1866 if (touched)
1867 {
1868 os << "\n";
1869 send_report<specialized>(severity::nonfatal, location{}, os.str());
1870 }
1871 }
1872
1873 inline
1874 void
1875 sequence_type::add_last(
1876 sequence_matcher *m)
1877 noexcept
1878 {
1879 matchers.push_back(m);
1880 }
1881
1882 template <typename T>
1883 void can_match_parameter(T&);
1884
1885 template <typename T>
1886 void can_match_parameter(T&&);
1887
1888 template <typename M>
1889 class ptr_deref : public matcher
1890 {
1891 public:
1892 template <typename U,
1893 typename = decltype(can_match_parameter<detail::remove_reference_t<decltype(*std::declval<U>())>>(std::declval<M>()))>
1894 operator U() const;
1895
1896 template <typename U>
1897 explicit
1898 ptr_deref(
1899 U&& m_)
1900 : m( std::forward<U>(m_) )
1901 {}
1902
1903 template <typename U>
1904 bool
1905 matches(
1906 const U& u)
1907 const
1908 noexcept(noexcept(std::declval<M>().matches(*u)))
1909 {
1910 return (u != nullptr) && m.matches(*u);
1911 }
1912
1913 friend
1914 std::ostream&
1915 operator<<(
1916 std::ostream& os,
1917 ptr_deref<M> const& p)
1918 {
1919 return os << p.m;
1920 }
1921 private:
1922 M m;
1923 };
1924
1925 template <typename M>
1926 class neg_matcher : public matcher
1927 {
1928 public:
1929 template <typename U,
1930 typename = decltype(can_match_parameter<detail::remove_reference_t<decltype(std::declval<U>())>>(std::declval<M>()))>
1931 operator U() const;
1932
1933 template <typename U>
1934 explicit
1935 neg_matcher(
1936 U&& m_)
1937 : m( std::forward<U>(m_) )
1938 {}
1939
1940 template <typename U>
1941 bool
1942 matches(
1943 const U& u)
1944 const
1945 noexcept(noexcept(!std::declval<M>().matches(u)))
1946 {
1947 return !m.matches(u);
1948 }
1949
1950 friend
1951 std::ostream&
1952 operator<<(
1953 std::ostream& os,
1954 neg_matcher<M> const& p)
1955 {
1956 return os << p.m;
1957 }
1958 private:
1959 M m;
1960 };
1961
1962 template <typename MatchType, typename Predicate, typename ... ActualType>
1963 struct matcher_kind
1964 {
1965 using type = typed_matcher<MatchType>;
1966 };
1967
1968 template <typename Predicate, typename ... ActualType>
1969 struct matcher_kind<wildcard, Predicate, ActualType...>
1970 {
1971 using type = duck_typed_matcher<Predicate, ActualType...>;
1972 };
1973
1974 template <typename MatchType, typename Predicate, typename ... ActualType>
1975 using matcher_kind_t =
1976 typename matcher_kind<MatchType, Predicate, ActualType...>::type;
1977
1978 template <typename Predicate, typename Printer, typename MatcherType, typename ... T>
1979 class predicate_matcher
1980 : private Predicate
1981 , private Printer
1982 , public MatcherType
1983 {
1984 public:
1985 template <typename ... U>
1986 constexpr
1987 predicate_matcher(
1988 Predicate&& pred,
1989 Printer&& printer,
1990 U&& ... v)
1991 noexcept(noexcept(std::tuple<T...>(std::declval<U>()...)) && noexcept(Predicate(std::declval<Predicate&&>())) && noexcept(Printer(std::declval<Printer&&>())))
1992 : Predicate(std::move(pred))
1993 , Printer(std::move(printer))
1994 , value(std::forward<U>(v)...)
1995 {}
1996
1997 template <typename V>
1998 constexpr
1999 bool
2000 matches(
2001 V&& v)
2002 const
2003 noexcept(noexcept(std::declval<Predicate const&>()(std::declval<V&&>(), std::declval<const T&>()...)))
2004 {
2005 return matches_(std::forward<V>(v), detail::make_index_sequence<sizeof...(T)>{});
2006 }
2007
2008 friend
2009 std::ostream&
2010 operator<<(
2011 std::ostream& os,
2012 predicate_matcher const& v)
2013 {
2014 return v.print_(os, detail::make_index_sequence<sizeof...(T)>{});
2015 }
2016 private:
2017 // The below function call operator must be declared to
2018 // work around gcc bug 78446
2019 //
2020 // For some reason microsoft compiler from VS2015 update 3
2021 // requires the function call operator to be private to avoid
2022 // ambiguities.
2023 template <typename ... U>
2024 void operator()(U&&...) const = delete;
2025
2026 template <typename V, size_t ... I>
2027 bool matches_(V&& v, detail::index_sequence<I...>) const
2028 {
2029 return Predicate::operator()(std::forward<V>(v), std::get<I>(value)...);
2030 }
2031
2032 template <size_t ... I>
2033 std::ostream& print_(std::ostream& os_, detail::index_sequence<I...>) const
2034 {
2035 Printer::operator()(os_, std::get<I>(value)...);
2036 return os_;
2037 }
2038
2039 std::tuple<T...> value;
2040 };
2041
2042 template <typename MatchType, typename Predicate, typename Printer, typename ... T>
2043 using make_matcher_return =
2044 predicate_matcher<Predicate,
2045 Printer,
2046 matcher_kind_t<MatchType, Predicate, detail::decay_t<T>...>,
2047 detail::decay_t<T>...>;
2048
2049 namespace lambdas {
2050
2051 struct any_predicate
2052 {
2053 template <typename T>
2054 bool
2055 operator()(
2056 T&&)
2057 const
2058 {
2059 return true;
2060 }
2061 };
2062
2063 // The below must be classes/structs to work with VS 2015 update 3
2064 // since it doesn't respect the trailing return type declaration on
2065 // the lambdas of template deduction context
2066
2067 #define TROMPELOEIL_MK_PRED_BINOP(name, op) \
2068 struct name { \
2069 template <typename X, typename Y> \
2070 auto operator()(X const& x, Y const& y) const -> decltype(x op y) \
2071 { \
2072 ::trompeloeil::ignore(x,y); \
2073 return x op y; \
2074 } \
2075 }
2076 TROMPELOEIL_MK_PRED_BINOP(equal, ==);
2077 TROMPELOEIL_MK_PRED_BINOP(not_equal, !=);
2078 TROMPELOEIL_MK_PRED_BINOP(less, <);
2079 TROMPELOEIL_MK_PRED_BINOP(less_equal, <=);
2080 TROMPELOEIL_MK_PRED_BINOP(greater, >);
2081 TROMPELOEIL_MK_PRED_BINOP(greater_equal, >=);
2082 #undef TROMPELOEIL_MK_PRED_BINOP
2083
2084 // Define `struct` with `operator()` to replace generic lambdas.
2085
2086 struct any_printer
2087 {
2088 explicit
2089 any_printer(
2090 char const* type_name_)
2091 : type_name(type_name_)
2092 {}
2093
2094 void
2095 operator()(
2096 std::ostream& os)
2097 const
2098 {
2099 os << " matching ANY(" << type_name << ")";
2100 }
2101
2102 private:
2103 char const* type_name;
2104 };
2105
2106 // These structures replace the `op` printer lambdas.
2107
2108 #define TROMPELOEIL_MK_OP_PRINTER(name, op_string) \
2109 struct name ## _printer \
2110 { \
2111 template <typename T> \
2112 void \
2113 operator()( \
2114 std::ostream& os, \
2115 T const& value) \
2116 const \
2117 { \
2118 os << op_string; \
2119 ::trompeloeil::print(os, value); \
2120 } \
2121 }
2122 TROMPELOEIL_MK_OP_PRINTER(equal, " == ");
2123 TROMPELOEIL_MK_OP_PRINTER(not_equal, " != ");
2124 TROMPELOEIL_MK_OP_PRINTER(less, " < ");
2125 TROMPELOEIL_MK_OP_PRINTER(less_equal, " <= ");
2126 TROMPELOEIL_MK_OP_PRINTER(greater, " > ");
2127 TROMPELOEIL_MK_OP_PRINTER(greater_equal, " >= ");
2128 #undef TROMPELOEIL_MK_OP_PRINTER
2129
2130 }
2131
2132 template <typename MatchType, typename Predicate, typename Printer, typename ... T>
2133 inline
2134 make_matcher_return<MatchType, Predicate, Printer, T...>
2135 make_matcher(Predicate pred, Printer print, T&& ... t)
2136 {
2137 return {std::move(pred), std::move(print), std::forward<T>(t)...};
2138 }
2139
2140
2141 template <
2142 typename T,
2143 typename R = make_matcher_return<T, lambdas::any_predicate, lambdas::any_printer>>
2144 inline
2145 auto
2146 any_matcher_impl(char const* type_name, std::false_type)
2147 TROMPELOEIL_TRAILING_RETURN_TYPE(R)
2148 {
2149 return make_matcher<T>(lambdas::any_predicate(), lambdas::any_printer(type_name));
2150 }
2151
2152 template <typename T>
2153 wildcard
2154 any_matcher_impl(char const*, std::true_type);
2155
2156 template <typename T>
2157 inline
2158 auto
2159 any_matcher(char const* name)
2160 TROMPELOEIL_TRAILING_RETURN_TYPE(decltype(any_matcher_impl<T>(name, std::is_array<T>{})))
2161 {
2162 static_assert(!std::is_array<T>::value,
2163 "array parameter type decays to pointer type for ANY()"
2164 " matcher. Please rephrase as pointer instead");
2165 return any_matcher_impl<T>(name, std::is_array<T>{});
2166 }
2167 template <
2168 typename T = wildcard,
2169 typename V,
2170 typename R = make_matcher_return<T, lambdas::equal, lambdas::equal_printer, V>>
2171 inline
2172 auto
2173 eq(
2174 V&& v)
2175 TROMPELOEIL_TRAILING_RETURN_TYPE(R)
2176 {
2177 return make_matcher<T>(lambdas::equal(),
2178 lambdas::equal_printer(),
2179 std::forward<V>(v));
2180 }
2181
2182 template <
2183 typename T = wildcard,
2184 typename V,
2185 typename R = make_matcher_return<T, lambdas::not_equal, lambdas::not_equal_printer, V>>
2186 inline
2187 auto
2188 ne(
2189 V&& v)
2190 TROMPELOEIL_TRAILING_RETURN_TYPE(R)
2191 {
2192 return make_matcher<T>(lambdas::not_equal(),
2193 lambdas::not_equal_printer(),
2194 std::forward<V>(v));
2195 }
2196
2197 template <
2198 typename T = wildcard,
2199 typename V,
2200 typename R = make_matcher_return<T, lambdas::greater_equal, lambdas::greater_equal_printer, V>>
2201 inline
2202 auto
2203 ge(
2204 V&& v)
2205 TROMPELOEIL_TRAILING_RETURN_TYPE(R)
2206 {
2207 return make_matcher<T>(lambdas::greater_equal(),
2208 lambdas::greater_equal_printer(),
2209 std::forward<V>(v));
2210 }
2211
2212 template <
2213 typename T = wildcard,
2214 typename V,
2215 typename R = make_matcher_return<T, lambdas::greater, lambdas::greater_printer, V>>
2216 inline
2217 auto
2218 gt(
2219 V&& v)
2220 TROMPELOEIL_TRAILING_RETURN_TYPE(R)
2221 {
2222 return make_matcher<T>(lambdas::greater(),
2223 lambdas::greater_printer(),
2224 std::forward<V>(v));
2225 }
2226
2227 template <
2228 typename T = wildcard,
2229 typename V,
2230 typename R = make_matcher_return<T, lambdas::less, lambdas::less_printer, V>>
2231 inline
2232 auto
2233 lt(
2234 V&& v)
2235 TROMPELOEIL_TRAILING_RETURN_TYPE(R)
2236 {
2237 return make_matcher<T>(lambdas::less(),
2238 lambdas::less_printer(),
2239 std::forward<V>(v));
2240 }
2241
2242 template <
2243 typename T = wildcard,
2244 typename V,
2245 typename R = make_matcher_return<T, lambdas::less_equal, lambdas::less_equal_printer, V>>
2246 inline
2247 auto
2248 le(
2249 V&& v)
2250 TROMPELOEIL_TRAILING_RETURN_TYPE(R)
2251 {
2252 return make_matcher<T>(lambdas::less_equal(),
2253 lambdas::less_equal_printer(),
2254 std::forward<V>(v));
2255 }
2256
2257 namespace lambdas {
2258
2259 struct regex_check
2260 {
2261
2262 class string_helper // a vastly simplified string_view type of class
2263 {
2264 public:
2265 string_helper(
2266 std::string const& s)
2267 noexcept
2268 : str(s.c_str())
2269 {}
2270
2271 constexpr
2272 string_helper(
2273 char const* s)
2274 noexcept
2275 : str(s)
2276 {}
2277
2278 char const*
2279 c_str()
2280 const
2281 noexcept
2282 {
2283 return str;
2284 }
2285 private:
2286 char const* str;
2287 };
2288
2289 regex_check(
2290 std::regex&& re_,
2291 std::regex_constants::match_flag_type match_type_)
2292 : re(std::move(re_)),
2293 match_type(match_type_)
2294 {}
2295
2296 template <typename T>
2297 bool
2298 operator()(
2299 string_helper str,
2300 T const&)
2301 const
2302 {
2303 return str.c_str()
2304 && std::regex_search(str.c_str(), re, match_type);
2305 }
2306
2307 private:
2308 std::regex re;
2309 std::regex_constants::match_flag_type match_type;
2310 };
2311
2312 struct regex_printer
2313 {
2314 template <typename T>
2315 void
2316 operator()(
2317 std::ostream& os,
2318 T const& str)
2319 const
2320 {
2321 os << " matching regular expression /" << str << "/";
2322 }
2323 };
2324 }
2325
2326 template <
2327 typename Kind = wildcard,
2328 typename R = make_matcher_return<Kind, lambdas::regex_check, lambdas::regex_printer, std::string&&>>
2329 auto
2330 re(
2331 std::string s,
2332 std::regex_constants::syntax_option_type opt = std::regex_constants::ECMAScript,
2333 std::regex_constants::match_flag_type match_type = std::regex_constants::match_default)
2334 TROMPELOEIL_TRAILING_RETURN_TYPE(R)
2335 {
2336 return make_matcher<Kind>(lambdas::regex_check(std::regex(s, opt),
2337 match_type),
2338 lambdas::regex_printer(),
2339 std::move(s));
2340 }
2341
2342 template <
2343 typename Kind = wildcard,
2344 typename R = make_matcher_return<Kind, lambdas::regex_check, lambdas::regex_printer, std::string&&>>
2345 auto
2346 re(
2347 std::string s,
2348 std::regex_constants::match_flag_type match_type)
2349 TROMPELOEIL_TRAILING_RETURN_TYPE(R)
2350 {
2351 return make_matcher<Kind>(lambdas::regex_check(std::regex(s), match_type),
2352 lambdas::regex_printer(),
2353 std::move(s));
2354 }
2355
2356 inline
2357 std::string
2358 param_name_prefix(
2359 ...)
2360 {
2361 return "";
2362 }
2363
2364 template <typename M>
2365 std::string
2366 param_name_prefix(
2367 const ptr_deref<M>*)
2368 {
2369 return "*" + ::trompeloeil::param_name_prefix(static_cast<M*>(nullptr));
2370 }
2371
2372 template <typename M>
2373 std::string
2374 param_name_prefix(
2375 const neg_matcher<M>*)
2376 {
2377 return "not " + ::trompeloeil::param_name_prefix(static_cast<M*>(nullptr));
2378 }
2379
2380 template <typename T>
2381 struct null_on_move
2382 {
2383 public:
2384 null_on_move()
2385 noexcept
2386 : p{nullptr}
2387 {}
2388
2389 null_on_move(
2390 T* p_)
2391 noexcept
2392 : p{p_}
2393 {}
2394
2395 null_on_move(
2396 null_on_move&&)
2397 noexcept
2398 : p{nullptr}
2399 {}
2400
2401 null_on_move(
2402 null_on_move const&)
2403 noexcept
2404 : p{nullptr}
2405 {}
2406
2407 null_on_move&
2408 operator=(
2409 const null_on_move&)
2410 noexcept
2411 {
2412 p = nullptr;
2413 return *this;
2414 }
2415
2416 null_on_move&
2417 operator=(
2418 null_on_move&&)
2419 noexcept
2420 {
2421 p = nullptr;
2422 return *this;
2423 }
2424
2425 null_on_move&
2426 operator=(
2427 T* t)
2428 noexcept
2429 {
2430 p = t;
2431 return *this;
2432 }
2433
2434 T*&
2435 leak()
2436 noexcept
2437 {
2438 return p;
2439 }
2440
2441 T&
2442 operator*()
2443 const
2444 noexcept
2445 {
2446 return *p;
2447 }
2448
2449 T*
2450 operator->()
2451 const
2452 noexcept
2453 {
2454 return p;
2455 }
2456
2457 explicit
2458 operator bool()
2459 const
2460 noexcept
2461 {
2462 return p != nullptr;
2463 }
2464 private:
2465 T* p;
2466 };
2467
2468 struct sequence_handler_base
2469 {
2470 virtual
2471 ~sequence_handler_base()
2472 noexcept = default;
2473
2474 virtual
2475 void
2476 validate(severity s, char const *, location) = 0;
2477
2478 virtual
2479 bool
2480 is_first()
2481 const
2482 noexcept = 0;
2483
2484 virtual
2485 void
2486 retire()
2487 noexcept = 0;
2488 };
2489
2490 template <size_t N>
2491 struct sequence_handler : public sequence_handler_base
2492 {
2493 public:
2494 template <typename ... S>
2495 sequence_handler(
2496 char const *name,
2497 location loc,
2498 S&& ... s)
2499 noexcept
2500 : matchers{{name, loc, std::forward<S>(s)}...}
2501 {
2502 }
2503
2504 void
2505 validate(
2506 severity s,
2507 char const *match_name,
2508 location loc)
2509 override
2510 {
2511 for (auto& e : matchers)
2512 {
2513 e.validate_match(s, match_name, loc);
2514 }
2515 }
2516
2517 bool
2518 is_first()
2519 const
2520 noexcept
2521 override
2522 {
2523 // std::all_of() is almost always preferable. The only reason
2524 // for using a hand rolled loop is because it cuts compilation
2525 // times quite noticeably (almost 10% with g++5.1)
2526 for (auto& m : matchers)
2527 {
2528 if (!m.is_first()) return false;
2529 }
2530 return true;
2531 }
2532
2533 void
2534 retire()
2535 noexcept
2536 override
2537 {
2538 for (auto& e : matchers)
2539 {
2540 e.retire();
2541 }
2542 }
2543 private:
2544 sequence_matcher matchers[N];
2545 };
2546
2547 struct lifetime_monitor;
2548
2549 template <typename T>
2550 class deathwatched : public T
2551 {
2552 static_assert(std::has_virtual_destructor<T>::value,
2553 "virtual destructor is a necessity for deathwatched to work");
2554 public:
2555 template <typename ... U,
2556 typename = detail::enable_if_t<std::is_constructible<T,U...>::value>>
2557 deathwatched(
2558 U&& ...u)
2559 noexcept(noexcept(T(std::declval<U>()...)))
2560 : T(std::forward<U>(u)...)
2561 {}
2562
2563 ~deathwatched();
2564
2565 trompeloeil::lifetime_monitor*&
2566 trompeloeil_expect_death(
2567 trompeloeil::lifetime_monitor* monitor)
2568 const
2569 noexcept
2570 {
2571 auto lock = get_lock();
2572 trompeloeil_lifetime_monitor = monitor;
2573 return trompeloeil_lifetime_monitor.leak();
2574 }
2575 private:
2576 mutable null_on_move<trompeloeil::lifetime_monitor> trompeloeil_lifetime_monitor;
2577 };
2578
2579 struct expectation {
2580 virtual ~expectation() = default;
2581 virtual bool is_satisfied() const noexcept = 0;
2582 virtual bool is_saturated() const noexcept = 0;
2583 };
2584
2585 struct lifetime_monitor : public expectation
2586 {
2587 template <typename T>
2588 lifetime_monitor(
2589 ::trompeloeil::deathwatched<T> const &obj,
2590 char const* obj_name_,
2591 char const* invocation_name_,
2592 char const* call_name_,
2593 location loc_)
2594 noexcept
2595 : object_monitor(obj.trompeloeil_expect_death(this))
2596 , loc(loc_)
2597 , object_name(obj_name_)
2598 , invocation_name(invocation_name_)
2599 , call_name(call_name_)
2600 {
2601 }
2602
2603 bool is_satisfied() const noexcept override
2604 {
2605 return died;
2606 }
2607
2608 bool is_saturated() const noexcept override
2609 {
2610 return died;
2611 }
2612
2613 lifetime_monitor(lifetime_monitor const&) = delete;
2614
2615 ~lifetime_monitor() override
2616 {
2617 auto lock = get_lock();
2618 if (!died)
2619 {
2620 std::ostringstream os;
2621 os << "Object " << object_name << " is still alive";
2622 send_report<specialized>(severity::nonfatal, loc, os.str());
2623 object_monitor = nullptr; // prevent its death poking this cadaver
2624 }
2625 }
2626
2627 void
2628 notify()
2629 noexcept
2630 {
2631 died = true;
2632 if (sequences) sequences->validate(severity::nonfatal, call_name, loc);
2633 }
2634
2635 template <typename ... T>
2636 void
2637 set_sequence(
2638 T&& ... t)
2639 {
2640 auto seq = new sequence_handler<sizeof...(T)>(invocation_name,
2641 loc,
2642 std::forward<T>(t)...);
2643 sequences.reset(seq);
2644 }
2645 private:
2646 std::atomic<bool> died{false};
2647 lifetime_monitor *&object_monitor;
2648 location loc;
2649 char const *object_name;
2650 char const *invocation_name;
2651 char const *call_name;
2652 std::unique_ptr<sequence_handler_base> sequences;
2653 };
2654
2655 template <typename T>
2656 deathwatched<T>::~deathwatched()
2657 {
2658 auto lock = get_lock();
2659 if (trompeloeil_lifetime_monitor)
2660 {
2661 trompeloeil_lifetime_monitor->notify();
2662 return;
2663 }
2664 std::ostringstream os;
2665 os << "Unexpected destruction of "
2666 << TROMPELOEIL_TYPE_ID_NAME(T) << "@" << this << '\n';
2667 send_report<specialized>(severity::nonfatal,
2668 location{},
2669 os.str());
2670 }
2671
2672 template <typename T>
2673 struct return_of;
2674
2675 template <typename R, typename ... A>
2676 struct return_of<R(A...)>
2677 {
2678 using type = R;
2679 };
2680
2681 template <typename T>
2682 using return_of_t = typename return_of<T>::type;
2683
2684 template <typename T>
2685 struct call_params_type;
2686
2687 template <typename R, typename ... T>
2688 struct call_params_type<R(T...)>
2689 {
2690 using type = std::tuple<typename std::add_lvalue_reference<T>::type...>;
2691 };
2692
2693 template <typename T>
2694 using call_params_type_t = typename call_params_type<T>::type;
2695
2696
2697 template <typename R>
2698 struct default_return_t
2699 {
2700 TROMPELOEIL_NORETURN static R value()
2701 {
2702 std::abort(); // must never be called
2703 }
2704 };
2705
2706 template <typename R>
2707 inline
2708 R
2709 default_return()
2710 {
2711 /* Work around VS 2017 15.7.x C4702 warning by
2712 * enclosing the operation in an otherwise
2713 * unnecessary try/catch block.
2714 */
2715 try
2716 {
2717 return default_return_t<R>::value();
2718 }
2719 catch (...)
2720 {
2721 throw;
2722 }
2723 }
2724
2725
2726
2727 template <>
2728 inline
2729 void
2730 default_return<void>()
2731 {
2732 }
2733
2734 template <typename Sig>
2735 struct call_matcher_base;
2736
2737 template <typename Sig>
2738 struct call_matcher_list : public list<call_matcher_base<Sig>>
2739 {
2740 void decommission()
2741 {
2742 auto lock = get_lock();
2743 auto iter = this->begin();
2744 auto const e = this->end();
2745 while (iter != e)
2746 {
2747 auto i = iter++;
2748 auto &m = *i;
2749 m.mock_destroyed();
2750 m.unlink();
2751 }
2752 }
2753 };
2754
2755 template <typename Sig>
2756 struct call_matcher_base : public list_elem<call_matcher_base<Sig>>
2757 {
2758 call_matcher_base(
2759 location loc_,
2760 char const* name_)
2761 : loc{loc_}
2762 , name{name_}
2763 {
2764 }
2765
2766 call_matcher_base(call_matcher_base&&) = delete;
2767
2768 virtual
2769 ~call_matcher_base() = default;
2770
2771 virtual
2772 void
2773 mock_destroyed() = 0;
2774
2775 virtual
2776 bool
2777 matches(
2778 call_params_type_t<Sig> const&)
2779 const = 0;
2780
2781 virtual
2782 bool
2783 first_in_sequence()
2784 const
2785 noexcept = 0;
2786
2787 virtual
2788 void
2789 run_actions(
2790 call_params_type_t<Sig> &,
2791 call_matcher_list<Sig> &saturated_list
2792 ) = 0;
2793
2794 virtual
2795 std::ostream&
2796 report_signature(
2797 std::ostream&)
2798 const = 0;
2799
2800 TROMPELOEIL_NORETURN
2801 virtual
2802 std::ostream&
2803 report_mismatch(
2804 std::ostream&,
2805 call_params_type_t<Sig> const &) = 0;
2806
2807 virtual
2808 return_of_t<Sig>
2809 return_value(
2810 trace_agent&,
2811 call_params_type_t<Sig>& p) = 0;
2812
2813 location loc;
2814 char const *name;
2815 };
2816
2817 template <typename T, typename U>
2818 bool
2819 param_matches_impl(
2820 T const& t,
2821 U const& u,
2822 matcher const*)
2823 noexcept(noexcept(t.matches(u)))
2824 {
2825 return t.matches(u);
2826 }
2827
2828 template <typename T,
2829 typename U,
2830 typename = detail::enable_if_t<is_equal_comparable<T, U>::value>>
2831 inline
2832 U&
2833 identity(
2834 U& t)
2835 noexcept
2836 {
2837 return t;
2838 }
2839
2840 template <typename T,
2841 typename U,
2842 typename = detail::enable_if_t<!is_equal_comparable<T, U>::value>>
2843 inline
2844 T
2845 identity(
2846 const U& u)
2847 noexcept(noexcept(T(u)))
2848 {
2849 return u;
2850 }
2851
2852 template <typename T, typename U>
2853 bool
2854 param_matches_impl(
2855 T const& t,
2856 U const& u,
2857 ...)
2858 noexcept(noexcept(::trompeloeil::identity<U>(t) == u))
2859 {
2860 return ::trompeloeil::identity<U>(t) == u;
2861 }
2862
2863 template <typename T, typename U>
2864 bool
2865 param_matches(
2866 T const& t,
2867 U const& u)
2868 noexcept(noexcept(param_matches_impl(t, u, &t)))
2869 {
2870 return ::trompeloeil::param_matches_impl(t, u, &t);
2871 }
2872
2873 template <size_t ... I, typename T, typename U>
2874 bool
2875 match_parameters(
2876 detail::index_sequence<I...>,
2877 T const& t,
2878 U const& u)
2879 noexcept(noexcept(std::initializer_list<bool>{trompeloeil::param_matches(std::get<I>(t),std::get<I>(u))...}))
2880 {
2881 bool all_true = true;
2882 ::trompeloeil::ignore(t, u); // Kills unmotivated VS2015 warning in the empty case
2883 ::trompeloeil::ignore(std::initializer_list<bool>{all_true = all_true && ::trompeloeil::param_matches(std::get<I>(t), std::get<I>(u))...});
2884 return all_true;
2885 }
2886
2887 template <typename ... T, typename ... U>
2888 bool
2889 match_parameters(
2890 std::tuple<T...> const& t,
2891 std::tuple<U...> const& u)
2892 noexcept(noexcept(match_parameters(detail::make_index_sequence<sizeof...(T)>{}, t, u)))
2893 {
2894 return ::trompeloeil::match_parameters(detail::make_index_sequence<sizeof...(T)>{}, t, u);
2895 }
2896
2897 template <typename V, typename P>
2898 void print_mismatch(
2899 std::ostream& os,
2900 size_t num,
2901 V const& v,
2902 P const& p)
2903 {
2904 if (!::trompeloeil::param_matches(v, p))
2905 {
2906 auto prefix = ::trompeloeil::param_name_prefix(&v) + "_";
2907 os << " Expected " << std::setw((num < 9) + 1) << prefix << num+1;
2908 ::trompeloeil::print_expectation(os, v);
2909 }
2910 }
2911
2912 template <typename ... V, typename ... P, size_t ... I>
2913 void print_mismatch(
2914 std::ostream& os,
2915 detail::index_sequence<I...>,
2916 std::tuple<V...> const& v,
2917 std::tuple<P...> const& p)
2918 {
2919 ::trompeloeil::ignore(os, v, p); // Kills unmotivated VS2015 warning in the empty case
2920 ::trompeloeil::ignore(std::initializer_list<int>{(print_mismatch(os, I, std::get<I>(v), std::get<I>(p)),0)...});
2921 }
2922
2923 template <typename ... V, typename ... P>
2924 void print_mismatch(
2925 std::ostream& os,
2926 std::tuple<V...> const& v,
2927 std::tuple<P...> const& p)
2928 {
2929 print_mismatch(os, detail::make_index_sequence<sizeof...(V)>{}, v, p);
2930 }
2931
2932 template <typename T>
2933 void missed_value(
2934 std::ostream& os,
2935 int i,
2936 T const& t)
2937 {
2938 auto prefix = ::trompeloeil::param_name_prefix(&t) + "_";
2939 os << " param " << std::setw((i < 9) + 1) << prefix << i + 1
2940 << ::trompeloeil::param_compare_operator(&t);
2941 ::trompeloeil::print(os, t);
2942 os << '\n';
2943 }
2944
2945 template <size_t ... I, typename ... T>
2946 void stream_params(
2947 std::ostream &os,
2948 detail::index_sequence<I...>,
2949 std::tuple<T...> const &t)
2950 {
2951 ::trompeloeil::ignore(os, t); // Kills unmotivated VS2015 warning in the empty case
2952 ::trompeloeil::ignore(std::initializer_list<int>{(missed_value(os, I, std::get<I>(t)),0)...});
2953 }
2954
2955 template <typename ... T>
2956 void
2957 stream_params(
2958 std::ostream &os,
2959 std::tuple<T...> const &t)
2960 {
2961 stream_params(os, detail::make_index_sequence<sizeof...(T)>{}, t);
2962 }
2963
2964 template <typename ... T>
2965 std::string
2966 params_string(
2967 std::tuple<T...> const& t)
2968 {
2969 std::ostringstream os;
2970 stream_params(os, t);
2971 return os.str();
2972 }
2973
2974 class trace_agent
2975 {
2976 public:
2977 trace_agent(
2978 location loc_,
2979 char const* name_,
2980 tracer* t_)
2981 : loc{loc_}
2982 , t{t_}
2983 {
2984 if (t)
2985 {
2986 os << name_ << " with.\n";
2987 }
2988 }
2989
2990 trace_agent(trace_agent const&) = delete;
2991
2992 trace_agent(trace_agent &&) = delete;
2993
2994 ~trace_agent()
2995 {
2996 if (t)
2997 {
2998 t->trace(loc.file, loc.line, os.str());
2999 }
3000 }
3001
3002 trace_agent&
3003 operator=(trace_agent const&) = delete;
3004
3005 trace_agent&
3006 operator=(trace_agent &&) = delete;
3007
3008 template <typename ... T>
3009 void
3010 trace_params(
3011 std::tuple<T...> const& params)
3012 {
3013 if (t)
3014 {
3015 stream_params(os, params);
3016 }
3017 }
3018
3019 template <typename T>
3020 auto
3021 trace_return(
3022 T&& rv)
3023 -> T
3024 {
3025 if (t)
3026 {
3027 os << " -> ";
3028 print(os, rv);
3029 os << '\n';
3030 }
3031 return std::forward<T>(rv);
3032 }
3033
3034 void
3035 trace_exception()
3036 {
3037 if (t)
3038 {
3039 try {
3040 throw;
3041 }
3042 catch (std::exception& e)
3043 {
3044 os << "threw exception: what() = " << e.what() << '\n';
3045 }
3046 catch (...)
3047 {
3048 os << "threw unknown exception\n";
3049 }
3050 }
3051 }
3052 private:
3053 location loc;
3054 tracer* t;
3055 std::ostringstream os;
3056 };
3057
3058 template <typename Sig>
3059 call_matcher_base <Sig> *
3060 find(
3061 call_matcher_list <Sig> &list,
3062 call_params_type_t <Sig> const &p)
3063 noexcept
3064 {
3065 call_matcher_base<Sig>* first_match = nullptr;
3066 for (auto& i : list)
3067 {
3068 if (i.matches(p))
3069 {
3070 if (i.first_in_sequence())
3071 {
3072 return &i;
3073 }
3074 if (!first_match)
3075 {
3076 first_match = &i;
3077 }
3078 }
3079 }
3080 return first_match;
3081 }
3082
3083 template <typename Sig>
3084 TROMPELOEIL_NORETURN
3085 void
3086 report_mismatch(
3087 call_matcher_list <Sig> &matcher_list,
3088 call_matcher_list <Sig> &saturated_list,
3089 std::string const &name,
3090 call_params_type_t <Sig> const &p)
3091 {
3092 std::ostringstream os;
3093 os << "No match for call of " << name << " with.\n";
3094 stream_params(os, p);
3095 bool saturated_match = false;
3096 for (auto& m : saturated_list)
3097 {
3098 if (m.matches(p))
3099 {
3100 if (!saturated_match)
3101 {
3102 os << "\nMatches saturated call requirement\n";
3103 saturated_match = true;
3104 }
3105 os << " ";
3106 m.report_signature(os) << '\n';
3107 }
3108 }
3109 if (!saturated_match)
3110 {
3111 for (auto& m : matcher_list)
3112 {
3113 os << "\nTried ";
3114 m.report_mismatch(os, p);
3115 }
3116 }
3117 send_report<specialized>(severity::fatal, location{}, os.str());
3118 std::abort(); // must never get here.
3119 }
3120
3121 template <typename Sig>
3122 void
3123 report_match(
3124 call_matcher_list <Sig> &matcher_list)
3125 {
3126 if(! matcher_list.empty())
3127 {
3128 send_ok_report<specialized>((matcher_list.begin())->name);
3129 }
3130 }
3131
3132 template <typename Sig>
3133 class return_handler
3134 {
3135 public:
3136 virtual
3137 ~return_handler() = default;
3138
3139 virtual
3140 return_of_t<Sig>
3141 call(
3142 trace_agent&,
3143 call_params_type_t<Sig>& params) = 0;
3144 };
3145
3146
3147 template <typename Ret, typename F, typename P, typename = detail::enable_if_t<std::is_void<Ret>::value>>
3148 void
3149 trace_return(
3150 trace_agent&,
3151 F& func,
3152 P& params)
3153 {
3154 func(params);
3155 }
3156
3157 template <typename Ret, typename F, typename P, typename = detail::enable_if_t<!std::is_void<Ret>::value>>
3158 Ret
3159 trace_return(
3160 trace_agent& agent,
3161 F& func,
3162 P& params)
3163 {
3164 /* Work around VS 2017 15.7.x C4702 warning by
3165 * enclosing the operation in an otherwise
3166 * unnecessary try/catch block.
3167 */
3168 try
3169 {
3170 return agent.trace_return(func(params));
3171 }
3172 catch (...)
3173 {
3174 throw;
3175 }
3176 }
3177
3178 template <typename Sig, typename T>
3179 class return_handler_t : public return_handler<Sig>
3180 {
3181 public:
3182 template <typename U>
3183 return_handler_t(
3184 U&& u)
3185 : func(std::forward<U>(u))
3186 {}
3187
3188 return_of_t<Sig>
3189 call(
3190 trace_agent& agent,
3191 call_params_type_t<Sig>& params)
3192 override
3193 {
3194 return trace_return<return_of_t<Sig>>(agent, func, params);
3195 }
3196 private:
3197 T func;
3198 };
3199
3200 template <typename Sig>
3201 class condition_base : public list_elem<condition_base<Sig>>
3202 {
3203 public:
3204 condition_base(
3205 char const *n)
3206 noexcept
3207 : id(n)
3208 {}
3209
3210 virtual
3211 ~condition_base() = default;
3212
3213 virtual
3214 bool
3215 check(
3216 call_params_type_t<Sig> const&)
3217 const = 0;
3218
3219 virtual
3220 char const*
3221 name()
3222 const
3223 noexcept
3224 {
3225 return id;
3226 }
3227 private:
3228 char const *id;
3229 };
3230
3231 template <typename Sig>
3232 using condition_list = list<condition_base<Sig>, delete_disposer>;
3233
3234 template <typename Sig, typename Cond>
3235 struct condition : public condition_base<Sig>
3236 {
3237 condition(
3238 char const *str_,
3239 Cond c_)
3240 : condition_base<Sig>(str_)
3241 , c(c_) {}
3242
3243 bool
3244 check(
3245 call_params_type_t<Sig> const & t)
3246 const
3247 override
3248 {
3249 return c(t);
3250 }
3251
3252 private:
3253 Cond c;
3254 };
3255
3256 template <typename Sig>
3257 struct side_effect_base : public list_elem<side_effect_base<Sig>>
3258 {
3259 virtual
3260 ~side_effect_base() = default;
3261
3262 virtual
3263 void
3264 action(
3265 call_params_type_t<Sig> &)
3266 const = 0;
3267 };
3268
3269 template <typename Sig>
3270 using side_effect_list = list<side_effect_base<Sig>, delete_disposer>;
3271
3272 template <typename Sig, typename Action>
3273 struct side_effect : public side_effect_base<Sig>
3274 {
3275 template <typename A>
3276 side_effect(
3277 A&& a_)
3278 : a(std::forward<A>(a_))
3279 {}
3280
3281 void
3282 action(
3283 call_params_type_t<Sig> &t)
3284 const
3285 override
3286 {
3287 a(t);
3288 }
3289 private:
3290 Action a;
3291 };
3292
3293 template <unsigned long long L, unsigned long long H = L>
3294 struct multiplicity { };
3295
3296 template <typename R, typename Parent>
3297 struct return_injector : Parent
3298 {
3299 using return_type = R;
3300 };
3301
3302 template <typename Parent>
3303 struct throw_injector : Parent
3304 {
3305 static bool const throws = true;
3306 };
3307
3308 template <typename Parent>
3309 struct sideeffect_injector : Parent
3310 {
3311 static bool const side_effects = true;
3312 };
3313
3314 template <typename Parent, unsigned long long H>
3315 struct call_limit_injector : Parent
3316 {
3317 static bool const call_limit_set = true;
3318 static unsigned long long const upper_call_limit = H;
3319 };
3320
3321 template <typename Parent>
3322 struct call_limit_injector<Parent, 0ULL> : Parent
3323 {
3324 static bool const call_limit_set = true;
3325 static unsigned long long const upper_call_limit = 0ULL;
3326 };
3327
3328 template <typename Parent>
3329 struct sequence_injector : Parent
3330 {
3331 static bool const sequence_set = true;
3332 };
3333
3334 template <typename Matcher, typename modifier_tag, typename Parent>
3335 struct call_modifier : public Parent
3336 {
3337 using typename Parent::signature;
3338 using typename Parent::return_type;
3339 using Parent::call_limit_set;
3340 using Parent::upper_call_limit;
3341 using Parent::sequence_set;
3342 using Parent::throws;
3343 using Parent::side_effects;
3344
3345 call_modifier(
3346 Matcher* m)
3347 noexcept
3348 : matcher{m}
3349 {}
3350
3351 template <typename D>
3352 call_modifier&&
3353 with(
3354 char const* str,
3355 D&& d)
3356 &&
3357 {
3358 matcher->add_condition(str, std::forward<D>(d));
3359 return std::move(*this);
3360 }
3361
3362 template <typename A>
3363 call_modifier<Matcher, modifier_tag, sideeffect_injector<Parent>>
3364 sideeffect(
3365 A&& a)
3366 {
3367 constexpr bool forbidden = upper_call_limit == 0U;
3368 static_assert(!forbidden,
3369 "SIDE_EFFECT for forbidden call does not make sense");
3370 matcher->add_side_effect(std::forward<A>(a));
3371 return {std::move(matcher)};
3372 }
3373
3374 template <typename H>
3375 call_modifier<Matcher, modifier_tag, return_injector<return_of_t<signature>, Parent >>
3376 handle_return(
3377 H&& h)
3378 {
3379 using params_type = call_params_type_t<signature>&;
3380 using sigret = return_of_t<signature>;
3381 using ret = decltype(std::declval<H>()(std::declval<params_type>()));
3382 // don't know why MS VS 2015 RC doesn't like std::result_of
3383
3384 constexpr bool is_illegal_type = std::is_same<detail::decay_t<ret>, illegal_argument>::value;
3385 constexpr bool is_first_return = std::is_same<return_type, void>::value;
3386 constexpr bool void_signature = std::is_same<sigret, void>::value;
3387 constexpr bool is_pointer_sigret = std::is_pointer<sigret>::value;
3388 constexpr bool is_pointer_ret = std::is_pointer<detail::decay_t<ret>>::value;
3389 constexpr bool ptr_const_mismatch =
3390 is_pointer_ret &&
3391 is_pointer_sigret &&
3392 !std::is_const<detail::remove_pointer_t<sigret>>{} &&
3393 std::is_const<detail::remove_pointer_t<detail::decay_t<ret>>>{};
3394 constexpr bool is_ref_sigret = std::is_reference<sigret>::value;
3395 constexpr bool is_ref_ret = std::is_reference<ret>::value;
3396 constexpr bool ref_const_mismatch=
3397 is_ref_ret &&
3398 is_ref_sigret &&
3399 !std::is_const<detail::remove_reference_t<sigret>>::value &&
3400 std::is_const<detail::remove_reference_t<ret>>::value;
3401 constexpr bool matching_ret_type = std::is_constructible<sigret, ret>::value;
3402 constexpr bool ref_value_mismatch = !is_ref_ret && is_ref_sigret;
3403
3404 static_assert(matching_ret_type || !void_signature,
3405 "RETURN does not make sense for void-function");
3406 static_assert(!is_illegal_type,
3407 "RETURN illegal argument");
3408 static_assert(!ptr_const_mismatch,
3409 "RETURN const* from function returning pointer to non-const");
3410 static_assert(!ref_value_mismatch || matching_ret_type,
3411 "RETURN non-reference from function returning reference");
3412 static_assert(ref_value_mismatch || !ref_const_mismatch,
3413 "RETURN const& from function returning non-const reference");
3414
3415 static_assert(ptr_const_mismatch || ref_const_mismatch || is_illegal_type || matching_ret_type || void_signature,
3416 "RETURN value is not convertible to the return type of the function");
3417 static_assert(is_first_return,
3418 "Multiple RETURN does not make sense");
3419 static_assert(!throws || upper_call_limit == 0,
3420 "THROW and RETURN does not make sense");
3421 static_assert(upper_call_limit > 0ULL,
3422 "RETURN for forbidden call does not make sense");
3423
3424 constexpr bool valid = !is_illegal_type && matching_ret_type && is_first_return && !throws && upper_call_limit > 0ULL;
3425 using tag = std::integral_constant<bool, valid>;
3426 matcher->set_return(tag{}, std::forward<H>(h));
3427 return {matcher};
3428 }
3429
3430 call_modifier&&
3431 null_modifier()
3432 {
3433 return std::move(*this);
3434 }
3435
3436 private:
3437 template <typename H>
3438 struct throw_handler_t
3439 {
3440 using R = decltype(default_return<return_of_t<signature>>());
3441
3442 throw_handler_t(H&& h_)
3443 : h(std::forward<H>(h_))
3444 {}
3445
3446 template <typename T>
3447 R operator()(T& p)
3448 {
3449 /* Work around VS 2017 15.7.x C4702 warning by
3450 * enclosing the operation in an otherwise
3451 * unnecessary try/catch block.
3452 */
3453 try
3454 {
3455 h(p);
3456 abort();
3457 }
3458 catch (...)
3459 {
3460 throw;
3461 }
3462 return default_return<R>(); // unreachable code
3463 }
3464
3465 private:
3466 H h;
3467 };
3468
3469 public:
3470 template <typename H>
3471 call_modifier<Matcher, modifier_tag, throw_injector<Parent>>
3472 handle_throw(
3473 H&& h)
3474 {
3475 static_assert(!throws,
3476 "Multiple THROW does not make sense");
3477 constexpr bool has_return = !std::is_same<return_type, void>::value;
3478 static_assert(!has_return,
3479 "THROW and RETURN does not make sense");
3480
3481 constexpr bool forbidden = upper_call_limit == 0U;
3482
3483 static_assert(!forbidden,
3484 "THROW for forbidden call does not make sense");
3485
3486 constexpr bool valid = !throws && !has_return;// && !forbidden;
3487 using tag = std::integral_constant<bool, valid>;
3488 auto handler = throw_handler_t<H>(std::forward<H>(h));
3489 matcher->set_return(tag{}, std::move(handler));
3490 return {matcher};
3491 }
3492
3493 template <unsigned long long L,
3494 unsigned long long H,
3495 bool times_set = call_limit_set>
3496 call_modifier<Matcher, modifier_tag, call_limit_injector<Parent, H>>
3497 times(
3498 multiplicity<L, H>)
3499 {
3500 static_assert(!times_set,
3501 "Only one TIMES call limit is allowed, but it can express an interval");
3502
3503 static_assert(H >= L,
3504 "In TIMES the first value must not exceed the second");
3505
3506 static_assert(H > 0 || !throws,
3507 "THROW and TIMES(0) does not make sense");
3508
3509 static_assert(H > 0 || std::is_same<return_type, void>::value,
3510 "RETURN and TIMES(0) does not make sense");
3511
3512 static_assert(H > 0 || !side_effects,
3513 "SIDE_EFFECT and TIMES(0) does not make sense");
3514
3515 static_assert(H > 0 || !sequence_set,
3516 "IN_SEQUENCE and TIMES(0) does not make sense");
3517
3518 matcher->min_calls = L;
3519 matcher->max_calls = H;
3520 return {matcher};
3521 }
3522
3523 template <typename ... T,
3524 bool b = sequence_set>
3525 call_modifier<Matcher, modifier_tag, sequence_injector<Parent>>
3526 in_sequence(
3527 T&& ... t)
3528 {
3529 static_assert(!b,
3530 "Multiple IN_SEQUENCE does not make sense."
3531 " You can list several sequence objects at once");
3532
3533 static_assert(upper_call_limit > 0ULL,
3534 "IN_SEQUENCE for forbidden call does not make sense");
3535
3536 matcher->set_sequence(std::forward<T>(t)...);
3537 return {matcher};
3538 }
3539 Matcher* matcher;
3540 };
3541
3542 inline
3543 void
3544 report_unfulfilled(
3545 const char* reason,
3546 char const *name,
3547 std::string const &values,
3548 unsigned long long min_calls,
3549 unsigned long long call_count,
3550 location loc)
3551 {
3552 std::ostringstream os;
3553 os << reason
3554 << ":\nExpected " << name << " to be called ";
3555 if (min_calls == 1)
3556 os << "once";
3557 else
3558 os << min_calls << " times";
3559 os << ", actually ";
3560 switch (call_count)
3561 {
3562 case 0:
3563 os << "never called\n"; break;
3564 case 1:
3565 os << "called once\n"; break;
3566 default:
3567 os << "called " << call_count << " times\n";
3568 }
3569 os << values;
3570 send_report<specialized>(severity::nonfatal, loc, os.str());
3571 }
3572
3573 inline
3574 void
3575 report_forbidden_call(
3576 char const *name,
3577 location loc,
3578 std::string const& values)
3579 {
3580 std::ostringstream os;
3581 os << "Match of forbidden call of " << name
3582 << " at " << loc << '\n' << values;
3583 send_report<specialized>(severity::fatal, loc, os.str());
3584 }
3585
3586 template <typename Sig>
3587 struct matcher_info
3588 {
3589 using signature = Sig;
3590 using return_type = void;
3591 static unsigned long long const upper_call_limit = 1;
3592 static bool const throws = false;
3593 static bool const call_limit_set = false;
3594 static bool const sequence_set = false;
3595 static bool const side_effects = false;
3596 };
3597
3598
3599 template <typename Sig, typename Value>
3600 struct call_matcher : public call_matcher_base<Sig>, expectation
3601 {
3602 using call_matcher_base<Sig>::name;
3603 using call_matcher_base<Sig>::loc;
3604
3605 template <typename ... U>
3606 call_matcher(
3607 char const *file,
3608 unsigned long line,
3609 char const *call_string,
3610 U &&... u)
3611 : call_matcher_base<Sig>(location{file, line}, call_string)
3612 , val(std::forward<U>(u)...)
3613 {}
3614
3615 call_matcher(call_matcher &&r) = delete;
3616
3617 ~call_matcher() override
3618 {
3619 auto lock = get_lock();
3620 if (is_unfulfilled())
3621 {
3622 report_missed("Unfulfilled expectation");
3623 }
3624 this->unlink();
3625 }
3626
3627 bool
3628 is_satisfied()
3629 const
3630 noexcept
3631 override
3632 {
3633 auto lock = get_lock();
3634 return call_count >= min_calls;
3635 }
3636
3637 bool
3638 is_saturated()
3639 const
3640 noexcept
3641 override
3642 {
3643 auto lock = get_lock();
3644 return call_count >= max_calls;
3645 }
3646 bool
3647 is_unfulfilled()
3648 const
3649 noexcept
3650 {
3651 return !reported && this->is_linked() && call_count < min_calls;
3652 }
3653
3654 void
3655 mock_destroyed()
3656 override
3657 {
3658 if (is_unfulfilled())
3659 {
3660 report_missed("Pending expectation on destroyed mock object");
3661 }
3662 }
3663
3664 call_matcher*
3665 hook_last(
3666 call_matcher_list<Sig> &list)
3667 noexcept
3668 {
3669 list.push_front(this);
3670 return this;
3671 }
3672
3673 bool
3674 matches(
3675 call_params_type_t<Sig> const& params)
3676 const
3677 override
3678 {
3679 return match_parameters(val, params) && match_conditions(params);
3680 }
3681
3682 bool
3683 match_conditions(
3684 call_params_type_t<Sig> const & params)
3685 const
3686 {
3687 // std::all_of() is almost always preferable. The only reason
3688 // for using a hand rolled loop is because it cuts compilation
3689 // times quite noticeably (almost 10% with g++5.1)
3690 for (auto& c : conditions)
3691 {
3692 if (!c.check(params)) return false;
3693 }
3694 return true;
3695 }
3696
3697 bool
3698 first_in_sequence()
3699 const
3700 noexcept
3701 override
3702 {
3703 auto saturated = call_count >= min_calls;
3704 return saturated || !sequences || sequences->is_first();
3705 }
3706
3707 return_of_t<Sig>
3708 return_value(
3709 trace_agent& agent,
3710 call_params_type_t<Sig>& params)
3711 override
3712 {
3713 if (!return_handler_obj) return default_return<return_of_t<Sig>>();
3714 return return_handler_obj->call(agent, params);
3715 }
3716
3717 void
3718 run_actions(
3719 call_params_type_t<Sig>& params,
3720 call_matcher_list<Sig> &saturated_list)
3721 override
3722 {
3723 if (max_calls == 0)
3724 {
3725 reported = true;
3726 report_forbidden_call(name, loc, params_string(params));
3727 }
3728 auto lock = get_lock();
3729 {
3730 if (call_count < min_calls && sequences)
3731 {
3732 sequences->validate(severity::fatal, name, loc);
3733 }
3734 if (++call_count == min_calls && sequences)
3735 {
3736 sequences->retire();
3737 }
3738 if (call_count == max_calls)
3739 {
3740 this->unlink();
3741 saturated_list.push_back(this);
3742 }
3743 }
3744 for (auto& a : actions) a.action(params);
3745 }
3746
3747 std::ostream&
3748 report_signature(
3749 std::ostream& os)
3750 const override
3751 {
3752 return os << name << " at " << loc;
3753 }
3754
3755 std::ostream&
3756 report_mismatch(
3757 std::ostream& os,
3758 call_params_type_t<Sig> const & params)
3759 override
3760 {
3761 reported = true;
3762 report_signature(os);
3763 if (match_parameters(val, params))
3764 {
3765 for (auto& cond : conditions)
3766 {
3767 if (!cond.check(params))
3768 {
3769 os << "\n Failed WITH(" << cond.name() << ')';
3770 }
3771 }
3772 }
3773 else
3774 {
3775 os << '\n';
3776 ::trompeloeil::print_mismatch(os, val, params);
3777 }
3778 return os;
3779 }
3780
3781 void
3782 report_missed(
3783 char const *reason)
3784 noexcept
3785 {
3786 reported = true;
3787 report_unfulfilled(
3788 reason,
3789 name,
3790 params_string(val),
3791 min_calls,
3792 call_count,
3793 loc);
3794 }
3795
3796 template <typename C>
3797 void
3798 add_condition(
3799 char const *str,
3800 C&& c)
3801 {
3802 auto cond = new condition<Sig, C>(str, std::forward<C>(c));
3803 conditions.push_back(cond);
3804 }
3805
3806 template <typename S>
3807 void
3808 add_side_effect(
3809 S&& s)
3810 {
3811 auto effect = new side_effect<Sig, S>(std::forward<S>(s));
3812 actions.push_back(effect);
3813 }
3814
3815 template <typename ... T>
3816 void
3817 set_sequence(
3818 T&& ... t)
3819 {
3820 auto seq = new sequence_handler<sizeof...(T)>(name,
3821 loc,
3822 std::forward<T>(t)...);
3823 sequences.reset(seq);
3824 }
3825
3826 template <typename T>
3827 inline
3828 void
3829 set_return(
3830 std::true_type,
3831 T&& h)
3832 {
3833 using basic_t = typename std::remove_reference<T>::type;
3834 using handler = return_handler_t<Sig, basic_t>;
3835 return_handler_obj.reset(new handler(std::forward<T>(h)));
3836 }
3837
3838 template <typename T>
3839 inline // Never called. Used to limit errmsg
3840 static // with RETURN of wrong type and after:
3841 void // FORBIDDEN_CALL
3842 set_return(std::false_type, T&&t)// RETURN
3843 noexcept; // THROW
3844
3845 condition_list<Sig> conditions;
3846 side_effect_list<Sig> actions;
3847 std::unique_ptr<return_handler<Sig>> return_handler_obj;
3848 std::unique_ptr<sequence_handler_base> sequences;
3849 unsigned long long call_count = 0;
3850 std::atomic<unsigned long long> min_calls{1};
3851 std::atomic<unsigned long long> max_calls{1};
3852 Value val;
3853 bool reported = false;
3854 };
3855
3856 /* Clang (all versions) does not like computing the return type R
3857 * before determining if the function overload is the best match.
3858 */
3859 template <
3860 int N,
3861 typename T,
3862 typename = detail::enable_if_t<N <= std::tuple_size<T>::value>,
3863 typename R = decltype(std::get<N-1>(std::declval<T>()))
3864 >
3865 constexpr
3866 TROMPELOEIL_DECLTYPE_AUTO
3867 arg(
3868 T* t,
3869 std::true_type)
3870 TROMPELOEIL_TRAILING_RETURN_TYPE(R)
3871 {
3872 return std::get<N-1>(*t);
3873 }
3874
3875 template <int N>
3876 inline
3877 constexpr
3878 illegal_argument const
3879 arg(
3880 void const*,
3881 std::false_type)
3882 noexcept
3883 {
3884 return {};
3885 }
3886
3887 template <
3888 int N,
3889 typename T,
3890 typename R = decltype(arg<N>(std::declval<T*>(),
3891 std::integral_constant<bool, (N <= std::tuple_size<T>::value)>{}))
3892 >
3893 TROMPELOEIL_DECLTYPE_AUTO
3894 mkarg(
3895 T& t)
3896 noexcept
3897 TROMPELOEIL_TRAILING_RETURN_TYPE(R)
3898 {
3899 return arg<N>(&t, std::integral_constant<bool, (N <= std::tuple_size<T>::value)>{});
3900 }
3901
3902 template <typename Mock>
3903 struct call_validator_t
3904 {
3905 template <typename M, typename Tag, typename Info>
3906 auto
3907 make_expectation(
3908 std::true_type,
3909 call_modifier<M, Tag, Info>&& m)
3910 const
3911 noexcept
3912 TROMPELOEIL_TRAILING_RETURN_TYPE(std::unique_ptr<expectation>)
3913 {
3914 auto lock = get_lock();
3915 m.matcher->hook_last(obj.trompeloeil_matcher_list(static_cast<Tag*>(nullptr)));
3916 if (m.matcher->min_calls == 0 && m.matcher->sequences)
3917 {
3918 m.matcher->sequences->retire();
3919 }
3920
3921 return std::unique_ptr<expectation>(m.matcher);
3922 }
3923
3924 template <typename T>
3925 static // Never called. Used to
3926 std::unique_ptr<expectation> // limit errmsg when RETURN
3927 make_expectation(std::false_type, T&&) noexcept; // is missing in non-void
3928 // function
3929
3930 template <typename M, typename Tag, typename Info>
3931 inline
3932 auto
3933 operator+(
3934 call_modifier<M, Tag, Info>&& t)
3935 const
3936 TROMPELOEIL_TRAILING_RETURN_TYPE(std::unique_ptr<expectation>)
3937 {
3938 using call = call_modifier<M, Tag, Info>;
3939 using sigret = return_of_t<typename call::signature>;
3940 using ret = typename call::return_type;
3941 constexpr bool retmatch = std::is_same<ret, sigret>::value;
3942 constexpr bool forbidden = call::upper_call_limit == 0ULL;
3943 constexpr bool valid_return_type = call::throws || retmatch || forbidden;
3944 static_assert(valid_return_type, "RETURN missing for non-void function");
3945 auto tag = std::integral_constant<bool, valid_return_type>{};
3946 return make_expectation(tag, std::move(t));
3947 }
3948 Mock& obj;
3949 };
3950
3951 template <typename T,
3952 typename = detail::enable_if_t<std::is_lvalue_reference<T&&>::value>>
3953 inline
3954 T&&
3955 decay_return_type(
3956 T&& t)
3957 {
3958 return std::forward<T>(t);
3959 }
3960
3961 template <typename T,
3962 typename = detail::enable_if_t<std::is_rvalue_reference<T&&>::value>>
3963 inline
3964 T
3965 decay_return_type(
3966 T&& t)
3967 {
3968 return std::forward<T>(t);
3969 }
3970
3971 template <typename T, size_t N>
3972 inline
3973 T*
3974 decay_return_type(
3975 T (&t)[N])
3976 {
3977 return t;
3978 }
3979
3980 template <bool sequence_set>
3981 struct lifetime_monitor_modifier
3982 {
3983 operator std::unique_ptr<lifetime_monitor>() { return std::move(monitor);}
3984 template <typename ... T, bool b = sequence_set>
3985 lifetime_monitor_modifier<true>
3986 in_sequence(T&& ... t)
3987 {
3988 static_assert(!b,
3989 "Multiple IN_SEQUENCE does not make sense."
3990 " You can list several sequence objects at once");
3991 monitor->set_sequence(std::forward<T>(t)...);
3992 return { std::move(monitor) };
3993 }
3994 std::unique_ptr<lifetime_monitor> monitor;
3995 };
3996
3997 struct lifetime_monitor_releaser
3998 {
3999 template <bool b>
4000 std::unique_ptr<trompeloeil::lifetime_monitor>
4001 operator+(
4002 lifetime_monitor_modifier<b>&& m)
4003 const
4004 {
4005 return m;
4006 }
4007 };
4008
4009 template <bool movable, typename Sig>
4010 struct expectations
4011 {
4012 expectations() = default;
4013 expectations(expectations&&) = default;
4014 ~expectations() {
4015 active.decommission();
4016 saturated.decommission();
4017 }
4018 call_matcher_list<Sig> active{};
4019 call_matcher_list<Sig> saturated{};
4020 };
4021
4022 template <typename Sig>
4023 struct expectations<false, Sig>
4024 {
4025 expectations() = default;
4026 expectations(expectations&&)
4027 {
4028 static_assert(std::is_same<Sig,void>::value,
4029 "By default, mock objects are not movable. "
4030 "To make a mock object movable, see: "
4031 "https://github.com/rollbear/trompeloeil/blob/master/docs/reference.md#movable_mock");
4032 }
4033 ~expectations() {
4034 active.decommission();
4035 saturated.decommission();
4036 }
4037 call_matcher_list<Sig> active{};
4038 call_matcher_list<Sig> saturated{};
4039 };
4040
4041 template <typename Sig, typename ... P>
4042 return_of_t<Sig> mock_func(std::false_type, P&& ...);
4043
4044
4045 template <bool movable, typename Sig, typename ... P>
4046 return_of_t<Sig>
4047 mock_func(std::true_type,
4048 expectations<movable, Sig>& e,
4049 char const *func_name,
4050 char const *sig_name,
4051 P&& ... p)
4052 {
4053 auto lock = get_lock();
4054
4055 call_params_type_t<void(P...)> param_value(std::forward<P>(p)...);
4056
4057 auto i = find(e.active, param_value);
4058 if (!i)
4059 {
4060 report_mismatch(e.active,
4061 e.saturated,
4062 func_name + std::string(" with signature ") + sig_name,
4063 param_value);
4064 }
4065 else{
4066 report_match(e.active);
4067 }
4068 trace_agent ta{i->loc, i->name, tracer_obj()};
4069 try
4070 {
4071 ta.trace_params(param_value);
4072 i->run_actions(param_value, e.saturated);
4073 return i->return_value(ta, param_value);
4074 }
4075 catch (...)
4076 {
4077 ta.trace_exception();
4078 throw;
4079 }
4080 }
4081
4082 template <typename ... U>
4083 struct param_helper {
4084 using type = decltype(std::make_tuple(std::declval<U>()...));
4085 };
4086
4087 template <typename ... U>
4088 using param_t = typename param_helper<U...>::type;
4089
4090 template <typename sig, typename tag, typename... U>
4091 using modifier_t = call_modifier<call_matcher<sig, param_t<U...>>,
4092 tag,
4093 matcher_info<sig>>;
4094
4095 template <typename M,
4096 typename = detail::enable_if_t<::trompeloeil::is_matcher<M>::value>>
4097 inline
4098 ::trompeloeil::ptr_deref<detail::decay_t<M>>
4099 operator*(
4100 M&& m)
4101 {
4102 return ::trompeloeil::ptr_deref<detail::decay_t<M>>{std::forward<M>(m)};
4103 }
4104
4105 template <typename M,
4106 typename = detail::enable_if_t<::trompeloeil::is_matcher<M>::value>>
4107 inline
4108 ::trompeloeil::neg_matcher<detail::decay_t<M>>
4109 operator!(
4110 M&& m)
4111 {
4112 return ::trompeloeil::neg_matcher<detail::decay_t<M>>{std::forward<M>(m)};
4113 }
4114
4115 /*
4116 * Convert the signature S of a mock function to the signature of
4117 * a member function of class T that takes the same parameters P
4118 * but returns R.
4119 *
4120 * The member function has the same constness as the mock function.
4121 */
4122 template <typename T, typename R, typename S>
4123 struct signature_to_member_function;
4124
4125 template <typename T, typename R, typename R_of_S, typename... P>
4126 struct signature_to_member_function<T, R, R_of_S(P...)>
4127 {
4128 using type = detail::conditional_t<
4129 std::is_const<T>::value,
4130 R (T::*)(P...) const,
4131 R (T::*)(P...)>;
4132 };
4133
4134 template <typename T>
4135 struct mock_interface : public T
4136 {
4137 using trompeloeil_interface_name = T;
4138 using T::T;
4139 };
4140
4141 #if defined(TROMPELOEIL_USER_DEFINED_COMPILE_TIME_REPORTER)
4142 extern template struct reporter<specialized>;
4143 #endif // TROMPELOEIL_USER_DEFINED_COMPILE_TIME_REPORTER
4144 }
4145
4146 #define TROMPELOEIL_LINE_ID(name) \
4147 TROMPELOEIL_CONCAT(trompeloeil_l_ ## name ## _, __LINE__)
4148
4149 #define TROMPELOEIL_COUNT_ID(name) \
4150 TROMPELOEIL_CONCAT(trompeloeil_c_ ## name ## _, __COUNTER__)
4151
4152 #ifdef _MSC_VER
4153 #define TROMPELOEIL_MAKE_MOCK0(name, sig, ...) \
4154 TROMPELOEIL_MAKE_MOCK_(name,,0, sig, __VA_ARGS__,,)
4155 #define TROMPELOEIL_MAKE_MOCK1(name, sig, ...) \
4156 TROMPELOEIL_MAKE_MOCK_(name,,1, sig, __VA_ARGS__,,)
4157 #define TROMPELOEIL_MAKE_MOCK2(name, sig, ...) \
4158 TROMPELOEIL_MAKE_MOCK_(name,,2, sig, __VA_ARGS__,,)
4159 #define TROMPELOEIL_MAKE_MOCK3(name, sig, ...) \
4160 TROMPELOEIL_MAKE_MOCK_(name,,3, sig, __VA_ARGS__,,)
4161 #define TROMPELOEIL_MAKE_MOCK4(name, sig, ...) \
4162 TROMPELOEIL_MAKE_MOCK_(name,,4, sig, __VA_ARGS__,,)
4163 #define TROMPELOEIL_MAKE_MOCK5(name, sig, ...) \
4164 TROMPELOEIL_MAKE_MOCK_(name,,5, sig, __VA_ARGS__,,)
4165 #define TROMPELOEIL_MAKE_MOCK6(name, sig, ...) \
4166 TROMPELOEIL_MAKE_MOCK_(name,,6, sig, __VA_ARGS__,,)
4167 #define TROMPELOEIL_MAKE_MOCK7(name, sig, ...) \
4168 TROMPELOEIL_MAKE_MOCK_(name,,7, sig, __VA_ARGS__,,)
4169 #define TROMPELOEIL_MAKE_MOCK8(name, sig, ...) \
4170 TROMPELOEIL_MAKE_MOCK_(name,,8, sig, __VA_ARGS__,,)
4171 #define TROMPELOEIL_MAKE_MOCK9(name, sig, ...) \
4172 TROMPELOEIL_MAKE_MOCK_(name,,9, sig, __VA_ARGS__,,)
4173 #define TROMPELOEIL_MAKE_MOCK10(name, sig, ...) \
4174 TROMPELOEIL_MAKE_MOCK_(name,,10, sig, __VA_ARGS__,,)
4175 #define TROMPELOEIL_MAKE_MOCK11(name, sig, ...) \
4176 TROMPELOEIL_MAKE_MOCK_(name,,11, sig, __VA_ARGS__,,)
4177 #define TROMPELOEIL_MAKE_MOCK12(name, sig, ...) \
4178 TROMPELOEIL_MAKE_MOCK_(name,,12, sig, __VA_ARGS__,,)
4179 #define TROMPELOEIL_MAKE_MOCK13(name, sig, ...) \
4180 TROMPELOEIL_MAKE_MOCK_(name,,13, sig, __VA_ARGS__,,)
4181 #define TROMPELOEIL_MAKE_MOCK14(name, sig, ...) \
4182 TROMPELOEIL_MAKE_MOCK_(name,,14, sig, __VA_ARGS__,,)
4183 #define TROMPELOEIL_MAKE_MOCK15(name, sig, ...) \
4184 TROMPELOEIL_MAKE_MOCK_(name,,15, sig, __VA_ARGS__,,)
4185
4186 #define TROMPELOEIL_MAKE_CONST_MOCK0(name, sig, ...) \
4187 TROMPELOEIL_MAKE_MOCK_(name,const,0, sig, __VA_ARGS__,,)
4188 #define TROMPELOEIL_MAKE_CONST_MOCK1(name, sig, ...) \
4189 TROMPELOEIL_MAKE_MOCK_(name,const,1, sig, __VA_ARGS__,,)
4190 #define TROMPELOEIL_MAKE_CONST_MOCK2(name, sig, ...) \
4191 TROMPELOEIL_MAKE_MOCK_(name,const,2, sig, __VA_ARGS__,,)
4192 #define TROMPELOEIL_MAKE_CONST_MOCK3(name, sig, ...) \
4193 TROMPELOEIL_MAKE_MOCK_(name,const,3, sig, __VA_ARGS__,,)
4194 #define TROMPELOEIL_MAKE_CONST_MOCK4(name, sig, ...) \
4195 TROMPELOEIL_MAKE_MOCK_(name,const,4, sig, __VA_ARGS__,,)
4196 #define TROMPELOEIL_MAKE_CONST_MOCK5(name, sig, ...) \
4197 TROMPELOEIL_MAKE_MOCK_(name,const,5, sig, __VA_ARGS__,,)
4198 #define TROMPELOEIL_MAKE_CONST_MOCK6(name, sig, ...) \
4199 TROMPELOEIL_MAKE_MOCK_(name,const,6, sig, __VA_ARGS__,,)
4200 #define TROMPELOEIL_MAKE_CONST_MOCK7(name, sig, ...) \
4201 TROMPELOEIL_MAKE_MOCK_(name,const,7, sig, __VA_ARGS__,,)
4202 #define TROMPELOEIL_MAKE_CONST_MOCK8(name, sig, ...) \
4203 TROMPELOEIL_MAKE_MOCK_(name,const,8, sig, __VA_ARGS__,,)
4204 #define TROMPELOEIL_MAKE_CONST_MOCK9(name, sig, ...) \
4205 TROMPELOEIL_MAKE_MOCK_(name,const,9, sig, __VA_ARGS__,,)
4206 #define TROMPELOEIL_MAKE_CONST_MOCK10(name, sig, ...) \
4207 TROMPELOEIL_MAKE_MOCK_(name,const,10, sig, __VA_ARGS__,,)
4208 #define TROMPELOEIL_MAKE_CONST_MOCK11(name, sig, ...) \
4209 TROMPELOEIL_MAKE_MOCK_(name,const,11, sig, __VA_ARGS__,,)
4210 #define TROMPELOEIL_MAKE_CONST_MOCK12(name, sig, ...) \
4211 TROMPELOEIL_MAKE_MOCK_(name,const,12, sig, __VA_ARGS__,,)
4212 #define TROMPELOEIL_MAKE_CONST_MOCK13(name, sig, ...) \
4213 TROMPELOEIL_MAKE_MOCK_(name,const,13, sig, __VA_ARGS__,,)
4214 #define TROMPELOEIL_MAKE_CONST_MOCK14(name, sig, ...) \
4215 TROMPELOEIL_MAKE_MOCK_(name,const,14, sig, __VA_ARGS__,,)
4216 #define TROMPELOEIL_MAKE_CONST_MOCK15(name, sig, ...) \
4217 TROMPELOEIL_MAKE_MOCK_(name,const,15, sig, __VA_ARGS__,,)
4218
4219
4220 #else
4221 // sane standards compliant preprocessor
4222
4223 #define TROMPELOEIL_MAKE_MOCK0(name, ...) \
4224 TROMPELOEIL_MAKE_MOCK_(name,,0, __VA_ARGS__,,)
4225 #define TROMPELOEIL_MAKE_MOCK1(name, ...) \
4226 TROMPELOEIL_MAKE_MOCK_(name,,1, __VA_ARGS__,,)
4227 #define TROMPELOEIL_MAKE_MOCK2(name, ...) \
4228 TROMPELOEIL_MAKE_MOCK_(name,,2, __VA_ARGS__,,)
4229 #define TROMPELOEIL_MAKE_MOCK3(name, ...) \
4230 TROMPELOEIL_MAKE_MOCK_(name,,3, __VA_ARGS__,,)
4231 #define TROMPELOEIL_MAKE_MOCK4(name, ...) \
4232 TROMPELOEIL_MAKE_MOCK_(name,,4, __VA_ARGS__,,)
4233 #define TROMPELOEIL_MAKE_MOCK5(name, ...) \
4234 TROMPELOEIL_MAKE_MOCK_(name,,5, __VA_ARGS__,,)
4235 #define TROMPELOEIL_MAKE_MOCK6(name, ...) \
4236 TROMPELOEIL_MAKE_MOCK_(name,,6, __VA_ARGS__,,)
4237 #define TROMPELOEIL_MAKE_MOCK7(name, ...) \
4238 TROMPELOEIL_MAKE_MOCK_(name,,7, __VA_ARGS__,,)
4239 #define TROMPELOEIL_MAKE_MOCK8(name, ...) \
4240 TROMPELOEIL_MAKE_MOCK_(name,,8, __VA_ARGS__,,)
4241 #define TROMPELOEIL_MAKE_MOCK9(name, ...) \
4242 TROMPELOEIL_MAKE_MOCK_(name,,9, __VA_ARGS__,,)
4243 #define TROMPELOEIL_MAKE_MOCK10(name, ...) \
4244 TROMPELOEIL_MAKE_MOCK_(name,,10, __VA_ARGS__,,)
4245 #define TROMPELOEIL_MAKE_MOCK11(name, ...) \
4246 TROMPELOEIL_MAKE_MOCK_(name,,11, __VA_ARGS__,,)
4247 #define TROMPELOEIL_MAKE_MOCK12(name, ...) \
4248 TROMPELOEIL_MAKE_MOCK_(name,,12, __VA_ARGS__,,)
4249 #define TROMPELOEIL_MAKE_MOCK13(name, ...) \
4250 TROMPELOEIL_MAKE_MOCK_(name,,13, __VA_ARGS__,,)
4251 #define TROMPELOEIL_MAKE_MOCK14(name, ...) \
4252 TROMPELOEIL_MAKE_MOCK_(name,,14,__VA_ARGS__,,)
4253 #define TROMPELOEIL_MAKE_MOCK15(name, ...) \
4254 TROMPELOEIL_MAKE_MOCK_(name,,15, __VA_ARGS__,,)
4255
4256 #define TROMPELOEIL_MAKE_CONST_MOCK0(name, ...) \
4257 TROMPELOEIL_MAKE_MOCK_(name,const,0, __VA_ARGS__,,)
4258 #define TROMPELOEIL_MAKE_CONST_MOCK1(name, ...) \
4259 TROMPELOEIL_MAKE_MOCK_(name,const,1, __VA_ARGS__,,)
4260 #define TROMPELOEIL_MAKE_CONST_MOCK2(name, ...) \
4261 TROMPELOEIL_MAKE_MOCK_(name,const,2, __VA_ARGS__,,)
4262 #define TROMPELOEIL_MAKE_CONST_MOCK3(name, ...) \
4263 TROMPELOEIL_MAKE_MOCK_(name,const,3, __VA_ARGS__,,)
4264 #define TROMPELOEIL_MAKE_CONST_MOCK4(name, ...) \
4265 TROMPELOEIL_MAKE_MOCK_(name,const,4, __VA_ARGS__,,)
4266 #define TROMPELOEIL_MAKE_CONST_MOCK5(name, ...) \
4267 TROMPELOEIL_MAKE_MOCK_(name,const,5, __VA_ARGS__,,)
4268 #define TROMPELOEIL_MAKE_CONST_MOCK6(name, ...) \
4269 TROMPELOEIL_MAKE_MOCK_(name,const,6, __VA_ARGS__,,)
4270 #define TROMPELOEIL_MAKE_CONST_MOCK7(name, ...) \
4271 TROMPELOEIL_MAKE_MOCK_(name,const,7, __VA_ARGS__,,)
4272 #define TROMPELOEIL_MAKE_CONST_MOCK8(name, ...) \
4273 TROMPELOEIL_MAKE_MOCK_(name,const,8, __VA_ARGS__,,)
4274 #define TROMPELOEIL_MAKE_CONST_MOCK9(name, ...) \
4275 TROMPELOEIL_MAKE_MOCK_(name,const,9, __VA_ARGS__,,)
4276 #define TROMPELOEIL_MAKE_CONST_MOCK10(name, ...) \
4277 TROMPELOEIL_MAKE_MOCK_(name,const,10, __VA_ARGS__,,)
4278 #define TROMPELOEIL_MAKE_CONST_MOCK11(name, ...) \
4279 TROMPELOEIL_MAKE_MOCK_(name,const,11, __VA_ARGS__,,)
4280 #define TROMPELOEIL_MAKE_CONST_MOCK12(name, ...) \
4281 TROMPELOEIL_MAKE_MOCK_(name,const,12, __VA_ARGS__,,)
4282 #define TROMPELOEIL_MAKE_CONST_MOCK13(name, ...) \
4283 TROMPELOEIL_MAKE_MOCK_(name,const,13, __VA_ARGS__,,)
4284 #define TROMPELOEIL_MAKE_CONST_MOCK14(name, ...) \
4285 TROMPELOEIL_MAKE_MOCK_(name,const,14, __VA_ARGS__,,)
4286 #define TROMPELOEIL_MAKE_CONST_MOCK15(name, ...) \
4287 TROMPELOEIL_MAKE_MOCK_(name,const,15, __VA_ARGS__,,)
4288
4289 #endif
4290
4291 #define TROMPELOEIL_IMPLEMENT_MOCK0(...) \
4292 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_MOCK_(0, __VA_ARGS__,override))
4293 #define TROMPELOEIL_IMPLEMENT_MOCK1(...) \
4294 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_MOCK_(1, __VA_ARGS__,override))
4295 #define TROMPELOEIL_IMPLEMENT_MOCK2(...) \
4296 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_MOCK_(2, __VA_ARGS__,override))
4297 #define TROMPELOEIL_IMPLEMENT_MOCK3(...) \
4298 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_MOCK_(3, __VA_ARGS__,override))
4299 #define TROMPELOEIL_IMPLEMENT_MOCK4(...) \
4300 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_MOCK_(4, __VA_ARGS__,override))
4301 #define TROMPELOEIL_IMPLEMENT_MOCK5(...) \
4302 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_MOCK_(5, __VA_ARGS__,override))
4303 #define TROMPELOEIL_IMPLEMENT_MOCK6(...) \
4304 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_MOCK_(6, __VA_ARGS__,override))
4305 #define TROMPELOEIL_IMPLEMENT_MOCK7(...) \
4306 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_MOCK_(7, __VA_ARGS__,override))
4307 #define TROMPELOEIL_IMPLEMENT_MOCK8(...) \
4308 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_MOCK_(8, __VA_ARGS__,override))
4309 #define TROMPELOEIL_IMPLEMENT_MOCK9(...) \
4310 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_MOCK_(9, __VA_ARGS__,override))
4311 #define TROMPELOEIL_IMPLEMENT_MOCK10(...) \
4312 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_MOCK_(10, __VA_ARGS__,override))
4313 #define TROMPELOEIL_IMPLEMENT_MOCK11(...) \
4314 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_MOCK_(11, __VA_ARGS__,override))
4315 #define TROMPELOEIL_IMPLEMENT_MOCK12(...) \
4316 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_MOCK_(12, __VA_ARGS__,override))
4317 #define TROMPELOEIL_IMPLEMENT_MOCK13(...) \
4318 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_MOCK_(13, __VA_ARGS__,override))
4319 #define TROMPELOEIL_IMPLEMENT_MOCK14(...) \
4320 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_MOCK_(14, __VA_ARGS__,override))
4321 #define TROMPELOEIL_IMPLEMENT_MOCK15(...) \
4322 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_MOCK_(15, __VA_ARGS__,override))
4323
4324 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK0(...) \
4325 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_CONST_MOCK_(0, __VA_ARGS__,override))
4326 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK1(...) \
4327 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_CONST_MOCK_(1, __VA_ARGS__,override))
4328 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK2(...) \
4329 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_CONST_MOCK_(2, __VA_ARGS__,override))
4330 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK3(...) \
4331 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_CONST_MOCK_(3, __VA_ARGS__,override))
4332 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK4(...) \
4333 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_CONST_MOCK_(4, __VA_ARGS__,override))
4334 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK5(...) \
4335 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_CONST_MOCK_(5, __VA_ARGS__,override))
4336 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK6(...) \
4337 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_CONST_MOCK_(6, __VA_ARGS__,override))
4338 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK7(...) \
4339 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_CONST_MOCK_(7, __VA_ARGS__,override))
4340 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK8(...) \
4341 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_CONST_MOCK_(8, __VA_ARGS__,override))
4342 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK9(...) \
4343 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_CONST_MOCK_(9, __VA_ARGS__,override))
4344 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK10(...) \
4345 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_CONST_MOCK_(10, __VA_ARGS__,override))
4346 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK11(...) \
4347 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_CONST_MOCK_(11, __VA_ARGS__,override))
4348 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK12(...) \
4349 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_CONST_MOCK_(12, __VA_ARGS__,override))
4350 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK13(...) \
4351 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_CONST_MOCK_(13, __VA_ARGS__,override))
4352 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK14(...) \
4353 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_CONST_MOCK_(14, __VA_ARGS__,override))
4354 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK15(...) \
4355 TROMPELOEIL_IDENTITY(TROMPELOEIL_IMPLEMENT_CONST_MOCK_(15, __VA_ARGS__,override))
4356
4357
4358 #define TROMPELOEIL_IMPLEMENT_MOCK_(num, name, ...) \
4359 TROMPELOEIL_MAKE_MOCK_(name,\
4360 ,\
4361 num,\
4362 decltype(::trompeloeil::nonconst_member_signature(&trompeloeil_interface_name::name))::type,\
4363 TROMPELOEIL_SEPARATE(__VA_ARGS__),)
4364 #define TROMPELOEIL_IMPLEMENT_CONST_MOCK_(num, name, ...) \
4365 TROMPELOEIL_MAKE_MOCK_(name,\
4366 const,\
4367 num,\
4368 decltype(::trompeloeil::const_member_signature(&trompeloeil_interface_name::name))::type,\
4369 TROMPELOEIL_SEPARATE(__VA_ARGS__),)
4370
4371 #define TROMPELOEIL_MAKE_MOCK_(name, constness, num, sig, spec, ...) \
4372 private: \
4373 using TROMPELOEIL_LINE_ID(cardinality_match) = \
4374 std::integral_constant<bool, num == \
4375 ::trompeloeil::param_list<TROMPELOEIL_REMOVE_PAREN(sig)>::size>; \
4376 static_assert(TROMPELOEIL_LINE_ID(cardinality_match)::value, \
4377 "Function signature does not have " #num " parameters"); \
4378 using TROMPELOEIL_LINE_ID(matcher_list_t) = \
4379 ::trompeloeil::call_matcher_list<TROMPELOEIL_REMOVE_PAREN(sig)>; \
4380 using TROMPELOEIL_LINE_ID(expectation_list_t) = \
4381 ::trompeloeil::expectations<trompeloeil_movable_mock, \
4382 TROMPELOEIL_REMOVE_PAREN(sig)>; \
4383 \
4384 struct TROMPELOEIL_LINE_ID(tag_type_trompeloeil) \
4385 { \
4386 const char* trompeloeil_expectation_file; \
4387 unsigned long trompeloeil_expectation_line; \
4388 const char *trompeloeil_expectation_string; \
4389 \
4390 /* Work around parsing bug in VS 2015 when a "complex" */ \
4391 /* decltype() appears in a trailing return type. */ \
4392 /* Further, work around C2066 defect in VS 2017 15.7.1. */ \
4393 using trompeloeil_sig_t = typename \
4394 ::trompeloeil::identity_type<TROMPELOEIL_REMOVE_PAREN(sig)>::type; \
4395 \
4396 using trompeloeil_call_params_type_t = \
4397 ::trompeloeil::call_params_type_t<TROMPELOEIL_REMOVE_PAREN(sig)>; \
4398 \
4399 using trompeloeil_return_of_t = \
4400 ::trompeloeil::return_of_t<TROMPELOEIL_REMOVE_PAREN(sig)>; \
4401 \
4402 template <typename ... trompeloeil_param_type> \
4403 auto name( \
4404 trompeloeil_param_type&& ... trompeloeil_param) \
4405 -> ::trompeloeil::modifier_t<trompeloeil_sig_t, \
4406 TROMPELOEIL_LINE_ID(tag_type_trompeloeil), \
4407 trompeloeil_param_type...> \
4408 { \
4409 using matcher = ::trompeloeil::call_matcher< \
4410 TROMPELOEIL_REMOVE_PAREN(sig), \
4411 ::trompeloeil::param_t<trompeloeil_param_type...>>; \
4412 return { \
4413 new matcher { \
4414 trompeloeil_expectation_file, \
4415 trompeloeil_expectation_line, \
4416 trompeloeil_expectation_string, \
4417 std::forward<trompeloeil_param_type>(trompeloeil_param)... \
4418 } \
4419 }; \
4420 } \
4421 }; \
4422 \
4423 public: \
4424 TROMPELOEIL_LINE_ID(matcher_list_t)& \
4425 trompeloeil_matcher_list( \
4426 TROMPELOEIL_LINE_ID(tag_type_trompeloeil)*) \
4427 constness \
4428 noexcept \
4429 { \
4430 return TROMPELOEIL_LINE_ID(expectations).active; \
4431 } \
4432 \
4433 ::trompeloeil::return_of_t<TROMPELOEIL_REMOVE_PAREN(sig)> \
4434 name(TROMPELOEIL_PARAM_LIST(num, sig)) \
4435 constness \
4436 spec \
4437 { \
4438 return ::trompeloeil::mock_func<trompeloeil_movable_mock, TROMPELOEIL_REMOVE_PAREN(sig)>( \
4439 TROMPELOEIL_LINE_ID(cardinality_match){}, \
4440 TROMPELOEIL_LINE_ID(expectations), \
4441 #name, \
4442 #sig \
4443 TROMPELOEIL_PARAMS(num)); \
4444 } \
4445 \
4446 TROMPELOEIL_NOT_IMPLEMENTED( \
4447 auto \
4448 trompeloeil_self_ ## name(TROMPELOEIL_PARAM_LIST(num, sig)) constness \
4449 -> decltype(*this)); \
4450 \
4451 TROMPELOEIL_NOT_IMPLEMENTED(TROMPELOEIL_LINE_ID(tag_type_trompeloeil) \
4452 trompeloeil_tag_ ## name(TROMPELOEIL_PARAM_LIST(num, sig)) constness); \
4453 \
4454 private: \
4455 mutable \
4456 TROMPELOEIL_LINE_ID(expectation_list_t) TROMPELOEIL_LINE_ID(expectations){}; \
4457 \
4458 public: \
4459 /* An unused alias declaration to make trailing semicolon acceptable. */ \
4460 using TROMPELOEIL_LINE_ID(unused_alias) = void
4461
4462
4463 #define TROMPELOEIL_LPAREN (
4464
4465 #define TROMPELOEIL_MORE_THAN_TWO_ARGS(...) \
4466 TROMPELOEIL_IDENTITY( \
4467 TROMPELOEIL_ARG16(__VA_ARGS__, \
4468 T, T, T, T, T, T, T, T, T, T, T, T, T, F, F, F))
4469
4470
4471 #define TROMPELOEIL_REQUIRE_CALL_V(...) \
4472 TROMPELOEIL_IDENTITY(TROMPELOEIL_REQUIRE_CALL_IMPL TROMPELOEIL_LPAREN \
4473 TROMPELOEIL_MORE_THAN_TWO_ARGS(__VA_ARGS__), __VA_ARGS__))
4474
4475 // Dispatch to _F (0, 1, or 2 arguments) or _T (> 2 arguments) macro
4476 #define TROMPELOEIL_REQUIRE_CALL_IMPL(N, ...) \
4477 TROMPELOEIL_IDENTITY( \
4478 TROMPELOEIL_REQUIRE_CALL_ ## N TROMPELOEIL_LPAREN __VA_ARGS__))
4479
4480 // Accept only two arguments
4481 #define TROMPELOEIL_REQUIRE_CALL_F(obj, func) \
4482 auto TROMPELOEIL_COUNT_ID(call_obj) = \
4483 TROMPELOEIL_REQUIRE_CALL_V_LAMBDA(obj, func, #obj, #func, .null_modifier())
4484
4485 // Accept three or more arguments.
4486 #define TROMPELOEIL_REQUIRE_CALL_T(obj, func, ...) \
4487 auto TROMPELOEIL_COUNT_ID(call_obj) = \
4488 TROMPELOEIL_REQUIRE_CALL_V_LAMBDA(obj, func, #obj, #func, __VA_ARGS__)
4489
4490
4491 #define TROMPELOEIL_REQUIRE_CALL_V_LAMBDA(obj, func, obj_s, func_s, ...) \
4492 [&] \
4493 { \
4494 using trompeloeil_s_t = decltype((obj) \
4495 .TROMPELOEIL_CONCAT(trompeloeil_self_, func)); \
4496 using trompeloeil_e_t = decltype((obj) \
4497 .TROMPELOEIL_CONCAT(trompeloeil_tag_,func)); \
4498 \
4499 return TROMPELOEIL_REQUIRE_CALL_LAMBDA_OBJ(obj, func, obj_s, func_s) \
4500 __VA_ARGS__ \
4501 ; \
4502 }()
4503
4504
4505 #define TROMPELOEIL_REQUIRE_CALL_LAMBDA_OBJ(obj, func, obj_s, func_s) \
4506 ::trompeloeil::call_validator_t<trompeloeil_s_t>{(obj)} + \
4507 ::trompeloeil::detail::conditional_t<false, \
4508 decltype((obj).func), \
4509 trompeloeil_e_t> \
4510 {__FILE__, static_cast<unsigned long>(__LINE__), obj_s "." func_s}.func
4511
4512
4513 #define TROMPELOEIL_NAMED_REQUIRE_CALL_V(...) \
4514 TROMPELOEIL_IDENTITY(TROMPELOEIL_NAMED_REQUIRE_CALL_IMPL TROMPELOEIL_LPAREN \
4515 TROMPELOEIL_MORE_THAN_TWO_ARGS(__VA_ARGS__), __VA_ARGS__))
4516
4517 // Dispatch to _F (0, 1, or 2 arguments) or _T (> 2 arguments) macro
4518 #define TROMPELOEIL_NAMED_REQUIRE_CALL_IMPL(N, ...) \
4519 TROMPELOEIL_IDENTITY( \
4520 TROMPELOEIL_NAMED_REQUIRE_CALL_ ## N TROMPELOEIL_LPAREN __VA_ARGS__))
4521
4522 // Accept only two arguments
4523 #define TROMPELOEIL_NAMED_REQUIRE_CALL_F(obj, func) \
4524 TROMPELOEIL_REQUIRE_CALL_V_LAMBDA(obj, func, #obj, #func, .null_modifier())
4525
4526 // Accept three or more arguments.
4527 #define TROMPELOEIL_NAMED_REQUIRE_CALL_T(obj, func, ...) \
4528 TROMPELOEIL_REQUIRE_CALL_V_LAMBDA(obj, func, #obj, #func, __VA_ARGS__)
4529
4530
4531 #define TROMPELOEIL_ALLOW_CALL_V(...) \
4532 TROMPELOEIL_IDENTITY(TROMPELOEIL_ALLOW_CALL_IMPL TROMPELOEIL_LPAREN \
4533 TROMPELOEIL_MORE_THAN_TWO_ARGS(__VA_ARGS__), __VA_ARGS__))
4534
4535 // Dispatch to _F (0, 1, or 2 arguments) or _T (> 2 arguments) macro
4536 #define TROMPELOEIL_ALLOW_CALL_IMPL(N, ...) \
4537 TROMPELOEIL_IDENTITY( \
4538 TROMPELOEIL_ALLOW_CALL_ ## N TROMPELOEIL_LPAREN __VA_ARGS__))
4539
4540 // Accept only two arguments
4541 #define TROMPELOEIL_ALLOW_CALL_F(obj, func) \
4542 TROMPELOEIL_REQUIRE_CALL_T(obj, func, .TROMPELOEIL_TIMES(0, ~0ULL))
4543
4544 // Accept three or more arguments.
4545 #define TROMPELOEIL_ALLOW_CALL_T(obj, func, ...) \
4546 TROMPELOEIL_REQUIRE_CALL_T(obj, \
4547 func, \
4548 .TROMPELOEIL_TIMES(0, ~0ULL) __VA_ARGS__)
4549
4550
4551 #define TROMPELOEIL_NAMED_ALLOW_CALL_V(...) \
4552 TROMPELOEIL_IDENTITY(TROMPELOEIL_NAMED_ALLOW_CALL_IMPL TROMPELOEIL_LPAREN \
4553 TROMPELOEIL_MORE_THAN_TWO_ARGS(__VA_ARGS__), __VA_ARGS__))
4554
4555 // Dispatch to _F (0, 1, or 2 arguments) or _T (> 2 arguments) macro
4556 #define TROMPELOEIL_NAMED_ALLOW_CALL_IMPL(N, ...) \
4557 TROMPELOEIL_IDENTITY( \
4558 TROMPELOEIL_NAMED_ALLOW_CALL_ ## N TROMPELOEIL_LPAREN __VA_ARGS__))
4559
4560 // Accept only two arguments
4561 #define TROMPELOEIL_NAMED_ALLOW_CALL_F(obj, func) \
4562 TROMPELOEIL_NAMED_REQUIRE_CALL_T(obj, func, .TROMPELOEIL_TIMES(0, ~0ULL))
4563
4564 // Accept three or more arguments.
4565 #define TROMPELOEIL_NAMED_ALLOW_CALL_T(obj, func, ...) \
4566 TROMPELOEIL_NAMED_REQUIRE_CALL_T(obj, \
4567 func, \
4568 .TROMPELOEIL_TIMES(0, ~0ULL) __VA_ARGS__)
4569
4570
4571 #define TROMPELOEIL_FORBID_CALL_V(...) \
4572 TROMPELOEIL_IDENTITY(TROMPELOEIL_FORBID_CALL_IMPL TROMPELOEIL_LPAREN \
4573 TROMPELOEIL_MORE_THAN_TWO_ARGS(__VA_ARGS__), __VA_ARGS__))
4574
4575 // Dispatch to _F (0, 1, or 2 arguments) or _T (> 2 arguments) macro
4576 #define TROMPELOEIL_FORBID_CALL_IMPL(N, ...) \
4577 TROMPELOEIL_IDENTITY( \
4578 TROMPELOEIL_FORBID_CALL_ ## N TROMPELOEIL_LPAREN __VA_ARGS__))
4579
4580 // Accept only two arguments
4581 #define TROMPELOEIL_FORBID_CALL_F(obj, func) \
4582 TROMPELOEIL_REQUIRE_CALL_T(obj, func, .TROMPELOEIL_TIMES(0))
4583
4584 // Accept three or more arguments.
4585 #define TROMPELOEIL_FORBID_CALL_T(obj, func, ...) \
4586 TROMPELOEIL_REQUIRE_CALL_T(obj, \
4587 func, \
4588 .TROMPELOEIL_TIMES(0) __VA_ARGS__)
4589
4590
4591 #define TROMPELOEIL_NAMED_FORBID_CALL_V(...) \
4592 TROMPELOEIL_IDENTITY(TROMPELOEIL_NAMED_FORBID_CALL_IMPL TROMPELOEIL_LPAREN \
4593 TROMPELOEIL_MORE_THAN_TWO_ARGS(__VA_ARGS__), __VA_ARGS__))
4594
4595 // Dispatch to _F (0, 1, or 2 arguments) or _T (> 2 arguments) macro
4596 #define TROMPELOEIL_NAMED_FORBID_CALL_IMPL(N, ...) \
4597 TROMPELOEIL_IDENTITY( \
4598 TROMPELOEIL_NAMED_FORBID_CALL_ ## N TROMPELOEIL_LPAREN __VA_ARGS__))
4599
4600 // Accept only two arguments
4601 #define TROMPELOEIL_NAMED_FORBID_CALL_F(obj, func) \
4602 TROMPELOEIL_NAMED_REQUIRE_CALL_T(obj, func, .TROMPELOEIL_TIMES(0))
4603
4604 // Accept three or more arguments.
4605 #define TROMPELOEIL_NAMED_FORBID_CALL_T(obj, func, ...) \
4606 TROMPELOEIL_NAMED_REQUIRE_CALL_T(obj, \
4607 func, \
4608 .TROMPELOEIL_TIMES(0) __VA_ARGS__)
4609
4610
4611 #if (TROMPELOEIL_CPLUSPLUS > 201103L)
4612
4613 #define TROMPELOEIL_REQUIRE_CALL(obj, func) \
4614 TROMPELOEIL_REQUIRE_CALL_(obj, func, #obj, #func)
4615
4616 #define TROMPELOEIL_REQUIRE_CALL_(obj, func, obj_s, func_s) \
4617 auto TROMPELOEIL_COUNT_ID(call_obj) = TROMPELOEIL_REQUIRE_CALL_OBJ(obj, func,\
4618 obj_s, func_s)
4619
4620 #define TROMPELOEIL_NAMED_REQUIRE_CALL(obj, func) \
4621 TROMPELOEIL_NAMED_REQUIRE_CALL_(obj, func, #obj, #func)
4622
4623 #define TROMPELOEIL_NAMED_REQUIRE_CALL_(obj, func, obj_s, func_s) \
4624 TROMPELOEIL_REQUIRE_CALL_OBJ(obj, func, obj_s, func_s)
4625
4626
4627 #define TROMPELOEIL_REQUIRE_CALL_OBJ(obj, func, obj_s, func_s) \
4628 ::trompeloeil::call_validator_t<decltype((obj).TROMPELOEIL_CONCAT(trompeloeil_self_, func))>{(obj)} + \
4629 ::trompeloeil::detail::conditional_t<false, \
4630 decltype((obj).func), \
4631 decltype((obj).TROMPELOEIL_CONCAT(trompeloeil_tag_,func))>\
4632 {__FILE__, static_cast<unsigned long>(__LINE__), obj_s "." func_s}.func
4633
4634
4635 #define TROMPELOEIL_ALLOW_CALL(obj, func) \
4636 TROMPELOEIL_ALLOW_CALL_(obj, func, #obj, #func)
4637
4638 #define TROMPELOEIL_ALLOW_CALL_(obj, func, obj_s, func_s) \
4639 TROMPELOEIL_REQUIRE_CALL_(obj, func, obj_s, func_s) \
4640 .TROMPELOEIL_TIMES(0, ~0ULL)
4641
4642
4643 #define TROMPELOEIL_NAMED_ALLOW_CALL(obj, func) \
4644 TROMPELOEIL_NAMED_ALLOW_CALL_(obj, func, #obj, #func)
4645
4646 #define TROMPELOEIL_NAMED_ALLOW_CALL_(obj, func, obj_s, func_s) \
4647 TROMPELOEIL_NAMED_REQUIRE_CALL_(obj, func, obj_s, func_s) \
4648 .TROMPELOEIL_TIMES(0, ~0ULL)
4649
4650
4651 #define TROMPELOEIL_FORBID_CALL(obj, func) \
4652 TROMPELOEIL_FORBID_CALL_(obj, func, #obj, #func)
4653
4654 #define TROMPELOEIL_FORBID_CALL_(obj, func, obj_s, func_s) \
4655 TROMPELOEIL_REQUIRE_CALL_(obj, func, obj_s, func_s) \
4656 .TROMPELOEIL_TIMES(0)
4657
4658
4659 #define TROMPELOEIL_NAMED_FORBID_CALL(obj, func) \
4660 TROMPELOEIL_NAMED_FORBID_CALL_(obj, func, #obj, #func)
4661
4662 #define TROMPELOEIL_NAMED_FORBID_CALL_(obj, func, obj_s, func_s) \
4663 TROMPELOEIL_NAMED_REQUIRE_CALL_(obj, func, obj_s, func_s) \
4664 .TROMPELOEIL_TIMES(0)
4665
4666 #endif /* (TROMPELOEIL_CPLUSPLUS > 201103L) */
4667
4668
4669 #define TROMPELOEIL_WITH(...) TROMPELOEIL_WITH_(=,#__VA_ARGS__, __VA_ARGS__)
4670 #define TROMPELOEIL_LR_WITH(...) TROMPELOEIL_WITH_(&,#__VA_ARGS__, __VA_ARGS__)
4671
4672
4673 #if (TROMPELOEIL_CPLUSPLUS == 201103L)
4674
4675 #define TROMPELOEIL_WITH_(capture, arg_s, ...) \
4676 with( \
4677 arg_s, \
4678 [capture] \
4679 (trompeloeil_e_t::trompeloeil_call_params_type_t const& trompeloeil_x) \
4680 { \
4681 auto& _1 = ::trompeloeil::mkarg<1>(trompeloeil_x); \
4682 auto& _2 = ::trompeloeil::mkarg<2>(trompeloeil_x); \
4683 auto& _3 = ::trompeloeil::mkarg<3>(trompeloeil_x); \
4684 auto& _4 = ::trompeloeil::mkarg<4>(trompeloeil_x); \
4685 auto& _5 = ::trompeloeil::mkarg<5>(trompeloeil_x); \
4686 auto& _6 = ::trompeloeil::mkarg<6>(trompeloeil_x); \
4687 auto& _7 = ::trompeloeil::mkarg<7>(trompeloeil_x); \
4688 auto& _8 = ::trompeloeil::mkarg<8>(trompeloeil_x); \
4689 auto& _9 = ::trompeloeil::mkarg<9>(trompeloeil_x); \
4690 auto&_10 = ::trompeloeil::mkarg<10>(trompeloeil_x); \
4691 auto&_11 = ::trompeloeil::mkarg<11>(trompeloeil_x); \
4692 auto&_12 = ::trompeloeil::mkarg<12>(trompeloeil_x); \
4693 auto&_13 = ::trompeloeil::mkarg<13>(trompeloeil_x); \
4694 auto&_14 = ::trompeloeil::mkarg<14>(trompeloeil_x); \
4695 auto&_15 = ::trompeloeil::mkarg<15>(trompeloeil_x); \
4696 ::trompeloeil::ignore( \
4697 _1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15); \
4698 return __VA_ARGS__; \
4699 })
4700
4701 #else /* (TROMPELOEIL_CPLUSPLUS == 201103L) */
4702
4703 #define TROMPELOEIL_WITH_(capture, arg_s, ...) \
4704 with(arg_s, [capture](auto const& trompeloeil_x) { \
4705 auto& _1 = ::trompeloeil::mkarg<1>(trompeloeil_x); \
4706 auto& _2 = ::trompeloeil::mkarg<2>(trompeloeil_x); \
4707 auto& _3 = ::trompeloeil::mkarg<3>(trompeloeil_x); \
4708 auto& _4 = ::trompeloeil::mkarg<4>(trompeloeil_x); \
4709 auto& _5 = ::trompeloeil::mkarg<5>(trompeloeil_x); \
4710 auto& _6 = ::trompeloeil::mkarg<6>(trompeloeil_x); \
4711 auto& _7 = ::trompeloeil::mkarg<7>(trompeloeil_x); \
4712 auto& _8 = ::trompeloeil::mkarg<8>(trompeloeil_x); \
4713 auto& _9 = ::trompeloeil::mkarg<9>(trompeloeil_x); \
4714 auto&_10 = ::trompeloeil::mkarg<10>(trompeloeil_x); \
4715 auto&_11 = ::trompeloeil::mkarg<11>(trompeloeil_x); \
4716 auto&_12 = ::trompeloeil::mkarg<12>(trompeloeil_x); \
4717 auto&_13 = ::trompeloeil::mkarg<13>(trompeloeil_x); \
4718 auto&_14 = ::trompeloeil::mkarg<14>(trompeloeil_x); \
4719 auto&_15 = ::trompeloeil::mkarg<15>(trompeloeil_x); \
4720 ::trompeloeil::ignore(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15); \
4721 return __VA_ARGS__; \
4722 })
4723
4724 #endif /* !(TROMPELOEIL_CPLUSPLUS == 201103L) */
4725
4726
4727 #define TROMPELOEIL_SIDE_EFFECT(...) TROMPELOEIL_SIDE_EFFECT_(=, __VA_ARGS__)
4728 #define TROMPELOEIL_LR_SIDE_EFFECT(...) TROMPELOEIL_SIDE_EFFECT_(&, __VA_ARGS__)
4729
4730
4731 #if (TROMPELOEIL_CPLUSPLUS == 201103L)
4732
4733 #define TROMPELOEIL_SIDE_EFFECT_(capture, ...) \
4734 sideeffect( \
4735 [capture](trompeloeil_e_t::trompeloeil_call_params_type_t& trompeloeil_x) {\
4736 auto& _1 = ::trompeloeil::mkarg<1>(trompeloeil_x); \
4737 auto& _2 = ::trompeloeil::mkarg<2>(trompeloeil_x); \
4738 auto& _3 = ::trompeloeil::mkarg<3>(trompeloeil_x); \
4739 auto& _4 = ::trompeloeil::mkarg<4>(trompeloeil_x); \
4740 auto& _5 = ::trompeloeil::mkarg<5>(trompeloeil_x); \
4741 auto& _6 = ::trompeloeil::mkarg<6>(trompeloeil_x); \
4742 auto& _7 = ::trompeloeil::mkarg<7>(trompeloeil_x); \
4743 auto& _8 = ::trompeloeil::mkarg<8>(trompeloeil_x); \
4744 auto& _9 = ::trompeloeil::mkarg<9>(trompeloeil_x); \
4745 auto&_10 = ::trompeloeil::mkarg<10>(trompeloeil_x); \
4746 auto&_11 = ::trompeloeil::mkarg<11>(trompeloeil_x); \
4747 auto&_12 = ::trompeloeil::mkarg<12>(trompeloeil_x); \
4748 auto&_13 = ::trompeloeil::mkarg<13>(trompeloeil_x); \
4749 auto&_14 = ::trompeloeil::mkarg<14>(trompeloeil_x); \
4750 auto&_15 = ::trompeloeil::mkarg<15>(trompeloeil_x); \
4751 ::trompeloeil::ignore( \
4752 _1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15); \
4753 __VA_ARGS__; \
4754 })
4755
4756 #else /* (TROMPELOEIL_CPLUSPLUS == 201103L) */
4757
4758 #define TROMPELOEIL_SIDE_EFFECT_(capture, ...) \
4759 sideeffect([capture](auto& trompeloeil_x) { \
4760 auto& _1 = ::trompeloeil::mkarg<1>(trompeloeil_x); \
4761 auto& _2 = ::trompeloeil::mkarg<2>(trompeloeil_x); \
4762 auto& _3 = ::trompeloeil::mkarg<3>(trompeloeil_x); \
4763 auto& _4 = ::trompeloeil::mkarg<4>(trompeloeil_x); \
4764 auto& _5 = ::trompeloeil::mkarg<5>(trompeloeil_x); \
4765 auto& _6 = ::trompeloeil::mkarg<6>(trompeloeil_x); \
4766 auto& _7 = ::trompeloeil::mkarg<7>(trompeloeil_x); \
4767 auto& _8 = ::trompeloeil::mkarg<8>(trompeloeil_x); \
4768 auto& _9 = ::trompeloeil::mkarg<9>(trompeloeil_x); \
4769 auto&_10 = ::trompeloeil::mkarg<10>(trompeloeil_x); \
4770 auto&_11 = ::trompeloeil::mkarg<11>(trompeloeil_x); \
4771 auto&_12 = ::trompeloeil::mkarg<12>(trompeloeil_x); \
4772 auto&_13 = ::trompeloeil::mkarg<13>(trompeloeil_x); \
4773 auto&_14 = ::trompeloeil::mkarg<14>(trompeloeil_x); \
4774 auto&_15 = ::trompeloeil::mkarg<15>(trompeloeil_x); \
4775 ::trompeloeil::ignore(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15); \
4776 __VA_ARGS__; \
4777 })
4778
4779 #endif /* !(TROMPELOEIL_CPLUSPLUS == 201103L) */
4780
4781
4782 #define TROMPELOEIL_RETURN(...) TROMPELOEIL_RETURN_(=, __VA_ARGS__)
4783 #define TROMPELOEIL_LR_RETURN(...) TROMPELOEIL_RETURN_(&, __VA_ARGS__)
4784
4785
4786 #if (TROMPELOEIL_CPLUSPLUS == 201103L)
4787
4788 #define TROMPELOEIL_RETURN_(capture, ...) \
4789 handle_return( \
4790 [capture](trompeloeil_e_t::trompeloeil_call_params_type_t& trompeloeil_x) \
4791 -> trompeloeil_e_t::trompeloeil_return_of_t \
4792 { \
4793 auto& _1 = ::trompeloeil::mkarg<1>(trompeloeil_x); \
4794 auto& _2 = ::trompeloeil::mkarg<2>(trompeloeil_x); \
4795 auto& _3 = ::trompeloeil::mkarg<3>(trompeloeil_x); \
4796 auto& _4 = ::trompeloeil::mkarg<4>(trompeloeil_x); \
4797 auto& _5 = ::trompeloeil::mkarg<5>(trompeloeil_x); \
4798 auto& _6 = ::trompeloeil::mkarg<6>(trompeloeil_x); \
4799 auto& _7 = ::trompeloeil::mkarg<7>(trompeloeil_x); \
4800 auto& _8 = ::trompeloeil::mkarg<8>(trompeloeil_x); \
4801 auto& _9 = ::trompeloeil::mkarg<9>(trompeloeil_x); \
4802 auto&_10 = ::trompeloeil::mkarg<10>(trompeloeil_x); \
4803 auto&_11 = ::trompeloeil::mkarg<11>(trompeloeil_x); \
4804 auto&_12 = ::trompeloeil::mkarg<12>(trompeloeil_x); \
4805 auto&_13 = ::trompeloeil::mkarg<13>(trompeloeil_x); \
4806 auto&_14 = ::trompeloeil::mkarg<14>(trompeloeil_x); \
4807 auto&_15 = ::trompeloeil::mkarg<15>(trompeloeil_x); \
4808 ::trompeloeil::ignore( \
4809 _1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15); \
4810 return ::trompeloeil::decay_return_type(__VA_ARGS__); \
4811 })
4812
4813 #else /* (TROMPELOEIL_CPLUSPLUS == 201103L) */
4814
4815 #define TROMPELOEIL_RETURN_(capture, ...) \
4816 handle_return([capture](auto& trompeloeil_x) -> decltype(auto) { \
4817 auto& _1 = ::trompeloeil::mkarg<1>(trompeloeil_x); \
4818 auto& _2 = ::trompeloeil::mkarg<2>(trompeloeil_x); \
4819 auto& _3 = ::trompeloeil::mkarg<3>(trompeloeil_x); \
4820 auto& _4 = ::trompeloeil::mkarg<4>(trompeloeil_x); \
4821 auto& _5 = ::trompeloeil::mkarg<5>(trompeloeil_x); \
4822 auto& _6 = ::trompeloeil::mkarg<6>(trompeloeil_x); \
4823 auto& _7 = ::trompeloeil::mkarg<7>(trompeloeil_x); \
4824 auto& _8 = ::trompeloeil::mkarg<8>(trompeloeil_x); \
4825 auto& _9 = ::trompeloeil::mkarg<9>(trompeloeil_x); \
4826 auto&_10 = ::trompeloeil::mkarg<10>(trompeloeil_x); \
4827 auto&_11 = ::trompeloeil::mkarg<11>(trompeloeil_x); \
4828 auto&_12 = ::trompeloeil::mkarg<12>(trompeloeil_x); \
4829 auto&_13 = ::trompeloeil::mkarg<13>(trompeloeil_x); \
4830 auto&_14 = ::trompeloeil::mkarg<14>(trompeloeil_x); \
4831 auto&_15 = ::trompeloeil::mkarg<15>(trompeloeil_x); \
4832 ::trompeloeil::ignore(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15); \
4833 return ::trompeloeil::decay_return_type(__VA_ARGS__); \
4834 })
4835
4836 #endif /* !(TROMPELOEIL_CPLUSPLUS == 201103L) */
4837
4838
4839 #define TROMPELOEIL_THROW(...) TROMPELOEIL_THROW_(=, __VA_ARGS__)
4840 #define TROMPELOEIL_LR_THROW(...) TROMPELOEIL_THROW_(&, __VA_ARGS__)
4841
4842
4843 #if (TROMPELOEIL_CPLUSPLUS == 201103L)
4844
4845 #define TROMPELOEIL_THROW_(capture, ...) \
4846 handle_throw( \
4847 [capture](trompeloeil_e_t::trompeloeil_call_params_type_t& trompeloeil_x) {\
4848 auto& _1 = ::trompeloeil::mkarg<1>(trompeloeil_x); \
4849 auto& _2 = ::trompeloeil::mkarg<2>(trompeloeil_x); \
4850 auto& _3 = ::trompeloeil::mkarg<3>(trompeloeil_x); \
4851 auto& _4 = ::trompeloeil::mkarg<4>(trompeloeil_x); \
4852 auto& _5 = ::trompeloeil::mkarg<5>(trompeloeil_x); \
4853 auto& _6 = ::trompeloeil::mkarg<6>(trompeloeil_x); \
4854 auto& _7 = ::trompeloeil::mkarg<7>(trompeloeil_x); \
4855 auto& _8 = ::trompeloeil::mkarg<8>(trompeloeil_x); \
4856 auto& _9 = ::trompeloeil::mkarg<9>(trompeloeil_x); \
4857 auto&_10 = ::trompeloeil::mkarg<10>(trompeloeil_x); \
4858 auto&_11 = ::trompeloeil::mkarg<11>(trompeloeil_x); \
4859 auto&_12 = ::trompeloeil::mkarg<12>(trompeloeil_x); \
4860 auto&_13 = ::trompeloeil::mkarg<13>(trompeloeil_x); \
4861 auto&_14 = ::trompeloeil::mkarg<14>(trompeloeil_x); \
4862 auto&_15 = ::trompeloeil::mkarg<15>(trompeloeil_x); \
4863 ::trompeloeil::ignore( \
4864 _1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15); \
4865 throw __VA_ARGS__; \
4866 })
4867
4868 #else /* (TROMPELOEIL_CPLUSPLUS == 201103L) */
4869
4870 #define TROMPELOEIL_THROW_(capture, ...) \
4871 handle_throw([capture](auto& trompeloeil_x) { \
4872 auto& _1 = ::trompeloeil::mkarg<1>(trompeloeil_x); \
4873 auto& _2 = ::trompeloeil::mkarg<2>(trompeloeil_x); \
4874 auto& _3 = ::trompeloeil::mkarg<3>(trompeloeil_x); \
4875 auto& _4 = ::trompeloeil::mkarg<4>(trompeloeil_x); \
4876 auto& _5 = ::trompeloeil::mkarg<5>(trompeloeil_x); \
4877 auto& _6 = ::trompeloeil::mkarg<6>(trompeloeil_x); \
4878 auto& _7 = ::trompeloeil::mkarg<7>(trompeloeil_x); \
4879 auto& _8 = ::trompeloeil::mkarg<8>(trompeloeil_x); \
4880 auto& _9 = ::trompeloeil::mkarg<9>(trompeloeil_x); \
4881 auto&_10 = ::trompeloeil::mkarg<10>(trompeloeil_x); \
4882 auto&_11 = ::trompeloeil::mkarg<11>(trompeloeil_x); \
4883 auto&_12 = ::trompeloeil::mkarg<12>(trompeloeil_x); \
4884 auto&_13 = ::trompeloeil::mkarg<13>(trompeloeil_x); \
4885 auto&_14 = ::trompeloeil::mkarg<14>(trompeloeil_x); \
4886 auto&_15 = ::trompeloeil::mkarg<15>(trompeloeil_x); \
4887 ::trompeloeil::ignore(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15); \
4888 throw __VA_ARGS__; \
4889 })
4890
4891 #endif /* !(TROMPELOEIL_CPLUSPLUS == 201103L) */
4892
4893
4894 #define TROMPELOEIL_TIMES(...) times(::trompeloeil::multiplicity<__VA_ARGS__>{})
4895
4896 #define TROMPELOEIL_IN_SEQUENCE(...) \
4897 in_sequence(TROMPELOEIL_INIT_WITH_STR(::trompeloeil::sequence_matcher::init_type, __VA_ARGS__))
4898
4899 #define TROMPELOEIL_ANY(type) ::trompeloeil::any_matcher<type>(#type)
4900
4901 #define TROMPELOEIL_AT_LEAST(num) num, ~0ULL
4902 #define TROMPELOEIL_AT_MOST(num) 0, num
4903
4904 #define TROMPELOEIL_REQUIRE_DESTRUCTION(obj) \
4905 TROMPELOEIL_REQUIRE_DESTRUCTION_(obj, #obj)
4906
4907 #define TROMPELOEIL_REQUIRE_DESTRUCTION_(obj, obj_s) \
4908 std::unique_ptr<trompeloeil::expectation> \
4909 TROMPELOEIL_CONCAT(trompeloeil_death_monitor_, __LINE__) \
4910 = TROMPELOEIL_NAMED_REQUIRE_DESTRUCTION_(,obj, obj_s)
4911
4912 #define TROMPELOEIL_NAMED_REQUIRE_DESTRUCTION(obj) \
4913 TROMPELOEIL_NAMED_REQUIRE_DESTRUCTION_("NAMED_", obj, #obj)
4914
4915 #define TROMPELOEIL_NAMED_REQUIRE_DESTRUCTION_(prefix, obj, obj_s) \
4916 trompeloeil::lifetime_monitor_releaser{} + \
4917 trompeloeil::lifetime_monitor_modifier<false>{ \
4918 ::trompeloeil::detail::make_unique<trompeloeil::lifetime_monitor>( \
4919 obj, \
4920 obj_s, \
4921 prefix "REQUIRE_DESTRUCTION(" obj_s ")", \
4922 "destructor for " obj_s, \
4923 ::trompeloeil::location{__FILE__, \
4924 static_cast<unsigned long>(__LINE__)}) \
4925 }
4926
4927 #ifndef TROMPELOEIL_LONG_MACROS
4928 #define MAKE_MOCK0 TROMPELOEIL_MAKE_MOCK0
4929 #define MAKE_MOCK1 TROMPELOEIL_MAKE_MOCK1
4930 #define MAKE_MOCK2 TROMPELOEIL_MAKE_MOCK2
4931 #define MAKE_MOCK3 TROMPELOEIL_MAKE_MOCK3
4932 #define MAKE_MOCK4 TROMPELOEIL_MAKE_MOCK4
4933 #define MAKE_MOCK5 TROMPELOEIL_MAKE_MOCK5
4934 #define MAKE_MOCK6 TROMPELOEIL_MAKE_MOCK6
4935 #define MAKE_MOCK7 TROMPELOEIL_MAKE_MOCK7
4936 #define MAKE_MOCK8 TROMPELOEIL_MAKE_MOCK8
4937 #define MAKE_MOCK9 TROMPELOEIL_MAKE_MOCK9
4938 #define MAKE_MOCK10 TROMPELOEIL_MAKE_MOCK10
4939 #define MAKE_MOCK11 TROMPELOEIL_MAKE_MOCK11
4940 #define MAKE_MOCK12 TROMPELOEIL_MAKE_MOCK12
4941 #define MAKE_MOCK13 TROMPELOEIL_MAKE_MOCK13
4942 #define MAKE_MOCK14 TROMPELOEIL_MAKE_MOCK14
4943 #define MAKE_MOCK15 TROMPELOEIL_MAKE_MOCK15
4944
4945 #define MAKE_CONST_MOCK0 TROMPELOEIL_MAKE_CONST_MOCK0
4946 #define MAKE_CONST_MOCK1 TROMPELOEIL_MAKE_CONST_MOCK1
4947 #define MAKE_CONST_MOCK2 TROMPELOEIL_MAKE_CONST_MOCK2
4948 #define MAKE_CONST_MOCK3 TROMPELOEIL_MAKE_CONST_MOCK3
4949 #define MAKE_CONST_MOCK4 TROMPELOEIL_MAKE_CONST_MOCK4
4950 #define MAKE_CONST_MOCK5 TROMPELOEIL_MAKE_CONST_MOCK5
4951 #define MAKE_CONST_MOCK6 TROMPELOEIL_MAKE_CONST_MOCK6
4952 #define MAKE_CONST_MOCK7 TROMPELOEIL_MAKE_CONST_MOCK7
4953 #define MAKE_CONST_MOCK8 TROMPELOEIL_MAKE_CONST_MOCK8
4954 #define MAKE_CONST_MOCK9 TROMPELOEIL_MAKE_CONST_MOCK9
4955 #define MAKE_CONST_MOCK10 TROMPELOEIL_MAKE_CONST_MOCK10
4956 #define MAKE_CONST_MOCK11 TROMPELOEIL_MAKE_CONST_MOCK11
4957 #define MAKE_CONST_MOCK12 TROMPELOEIL_MAKE_CONST_MOCK12
4958 #define MAKE_CONST_MOCK13 TROMPELOEIL_MAKE_CONST_MOCK13
4959 #define MAKE_CONST_MOCK14 TROMPELOEIL_MAKE_CONST_MOCK14
4960 #define MAKE_CONST_MOCK15 TROMPELOEIL_MAKE_CONST_MOCK15
4961
4962 #define IMPLEMENT_MOCK0 TROMPELOEIL_IMPLEMENT_MOCK0
4963 #define IMPLEMENT_MOCK1 TROMPELOEIL_IMPLEMENT_MOCK1
4964 #define IMPLEMENT_MOCK2 TROMPELOEIL_IMPLEMENT_MOCK2
4965 #define IMPLEMENT_MOCK3 TROMPELOEIL_IMPLEMENT_MOCK3
4966 #define IMPLEMENT_MOCK4 TROMPELOEIL_IMPLEMENT_MOCK4
4967 #define IMPLEMENT_MOCK5 TROMPELOEIL_IMPLEMENT_MOCK5
4968 #define IMPLEMENT_MOCK6 TROMPELOEIL_IMPLEMENT_MOCK6
4969 #define IMPLEMENT_MOCK7 TROMPELOEIL_IMPLEMENT_MOCK7
4970 #define IMPLEMENT_MOCK8 TROMPELOEIL_IMPLEMENT_MOCK8
4971 #define IMPLEMENT_MOCK9 TROMPELOEIL_IMPLEMENT_MOCK9
4972 #define IMPLEMENT_MOCK10 TROMPELOEIL_IMPLEMENT_MOCK10
4973 #define IMPLEMENT_MOCK11 TROMPELOEIL_IMPLEMENT_MOCK11
4974 #define IMPLEMENT_MOCK12 TROMPELOEIL_IMPLEMENT_MOCK12
4975 #define IMPLEMENT_MOCK13 TROMPELOEIL_IMPLEMENT_MOCK13
4976 #define IMPLEMENT_MOCK14 TROMPELOEIL_IMPLEMENT_MOCK14
4977 #define IMPLEMENT_MOCK15 TROMPELOEIL_IMPLEMENT_MOCK15
4978
4979 #define IMPLEMENT_CONST_MOCK0 TROMPELOEIL_IMPLEMENT_CONST_MOCK0
4980 #define IMPLEMENT_CONST_MOCK1 TROMPELOEIL_IMPLEMENT_CONST_MOCK1
4981 #define IMPLEMENT_CONST_MOCK2 TROMPELOEIL_IMPLEMENT_CONST_MOCK2
4982 #define IMPLEMENT_CONST_MOCK3 TROMPELOEIL_IMPLEMENT_CONST_MOCK3
4983 #define IMPLEMENT_CONST_MOCK4 TROMPELOEIL_IMPLEMENT_CONST_MOCK4
4984 #define IMPLEMENT_CONST_MOCK5 TROMPELOEIL_IMPLEMENT_CONST_MOCK5
4985 #define IMPLEMENT_CONST_MOCK6 TROMPELOEIL_IMPLEMENT_CONST_MOCK6
4986 #define IMPLEMENT_CONST_MOCK7 TROMPELOEIL_IMPLEMENT_CONST_MOCK7
4987 #define IMPLEMENT_CONST_MOCK8 TROMPELOEIL_IMPLEMENT_CONST_MOCK8
4988 #define IMPLEMENT_CONST_MOCK9 TROMPELOEIL_IMPLEMENT_CONST_MOCK9
4989 #define IMPLEMENT_CONST_MOCK10 TROMPELOEIL_IMPLEMENT_CONST_MOCK10
4990 #define IMPLEMENT_CONST_MOCK11 TROMPELOEIL_IMPLEMENT_CONST_MOCK11
4991 #define IMPLEMENT_CONST_MOCK12 TROMPELOEIL_IMPLEMENT_CONST_MOCK12
4992 #define IMPLEMENT_CONST_MOCK13 TROMPELOEIL_IMPLEMENT_CONST_MOCK13
4993 #define IMPLEMENT_CONST_MOCK14 TROMPELOEIL_IMPLEMENT_CONST_MOCK14
4994 #define IMPLEMENT_CONST_MOCK15 TROMPELOEIL_IMPLEMENT_CONST_MOCK15
4995
4996 #define REQUIRE_CALL_V TROMPELOEIL_REQUIRE_CALL_V
4997 #define NAMED_REQUIRE_CALL_V TROMPELOEIL_NAMED_REQUIRE_CALL_V
4998 #define ALLOW_CALL_V TROMPELOEIL_ALLOW_CALL_V
4999 #define NAMED_ALLOW_CALL_V TROMPELOEIL_NAMED_ALLOW_CALL_V
5000 #define FORBID_CALL_V TROMPELOEIL_FORBID_CALL_V
5001 #define NAMED_FORBID_CALL_V TROMPELOEIL_NAMED_FORBID_CALL_V
5002
5003 #if (TROMPELOEIL_CPLUSPLUS > 201103L)
5004
5005 #define REQUIRE_CALL TROMPELOEIL_REQUIRE_CALL
5006 #define NAMED_REQUIRE_CALL TROMPELOEIL_NAMED_REQUIRE_CALL
5007 #define ALLOW_CALL TROMPELOEIL_ALLOW_CALL
5008 #define NAMED_ALLOW_CALL TROMPELOEIL_NAMED_ALLOW_CALL
5009 #define FORBID_CALL TROMPELOEIL_FORBID_CALL
5010 #define NAMED_FORBID_CALL TROMPELOEIL_NAMED_FORBID_CALL
5011
5012 #endif /* (TROMPELOEIL_CPLUSPLUS > 201103L) */
5013
5014 #define WITH TROMPELOEIL_WITH
5015 #define LR_WITH TROMPELOEIL_LR_WITH
5016 #define SIDE_EFFECT TROMPELOEIL_SIDE_EFFECT
5017 #define LR_SIDE_EFFECT TROMPELOEIL_LR_SIDE_EFFECT
5018 #define RETURN TROMPELOEIL_RETURN
5019 #define LR_RETURN TROMPELOEIL_LR_RETURN
5020 #define THROW TROMPELOEIL_THROW
5021 #define LR_THROW TROMPELOEIL_LR_THROW
5022
5023 #define TIMES TROMPELOEIL_TIMES
5024 #define IN_SEQUENCE TROMPELOEIL_IN_SEQUENCE
5025 #define ANY TROMPELOEIL_ANY
5026 #define AT_LEAST TROMPELOEIL_AT_LEAST
5027 #define AT_MOST TROMPELOEIL_AT_MOST
5028 #define REQUIRE_DESTRUCTION TROMPELOEIL_REQUIRE_DESTRUCTION
5029 #define NAMED_REQUIRE_DESTRUCTION TROMPELOEIL_NAMED_REQUIRE_DESTRUCTION
5030
5031 #endif // TROMPELOEIL_LONG_MACROS
5032
5033 #endif // include guard