Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

4908 lines
146KB

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