gmock-more-actions.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. // Copyright 2007, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // Google Mock - a framework for writing C++ mock classes.
  30. //
  31. // This file implements some commonly used variadic actions.
  32. // IWYU pragma: private, include "gmock/gmock.h"
  33. // IWYU pragma: friend gmock/.*
  34. #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
  35. #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
  36. #include <memory>
  37. #include <utility>
  38. #include "gmock/gmock-actions.h"
  39. #include "gmock/internal/gmock-port.h"
  40. // Include any custom callback actions added by the local installation.
  41. #include "gmock/internal/custom/gmock-generated-actions.h"
  42. // Sometimes you want to give an action explicit template parameters
  43. // that cannot be inferred from its value parameters. ACTION() and
  44. // ACTION_P*() don't support that. ACTION_TEMPLATE() remedies that
  45. // and can be viewed as an extension to ACTION() and ACTION_P*().
  46. //
  47. // The syntax:
  48. //
  49. // ACTION_TEMPLATE(ActionName,
  50. // HAS_m_TEMPLATE_PARAMS(kind1, name1, ..., kind_m, name_m),
  51. // AND_n_VALUE_PARAMS(p1, ..., p_n)) { statements; }
  52. //
  53. // defines an action template that takes m explicit template
  54. // parameters and n value parameters. name_i is the name of the i-th
  55. // template parameter, and kind_i specifies whether it's a typename,
  56. // an integral constant, or a template. p_i is the name of the i-th
  57. // value parameter.
  58. //
  59. // Example:
  60. //
  61. // // DuplicateArg<k, T>(output) converts the k-th argument of the mock
  62. // // function to type T and copies it to *output.
  63. // ACTION_TEMPLATE(DuplicateArg,
  64. // HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
  65. // AND_1_VALUE_PARAMS(output)) {
  66. // *output = T(::std::get<k>(args));
  67. // }
  68. // ...
  69. // int n;
  70. // EXPECT_CALL(mock, Foo(_, _))
  71. // .WillOnce(DuplicateArg<1, unsigned char>(&n));
  72. //
  73. // To create an instance of an action template, write:
  74. //
  75. // ActionName<t1, ..., t_m>(v1, ..., v_n)
  76. //
  77. // where the ts are the template arguments and the vs are the value
  78. // arguments. The value argument types are inferred by the compiler.
  79. // If you want to explicitly specify the value argument types, you can
  80. // provide additional template arguments:
  81. //
  82. // ActionName<t1, ..., t_m, u1, ..., u_k>(v1, ..., v_n)
  83. //
  84. // where u_i is the desired type of v_i.
  85. //
  86. // ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded on the
  87. // number of value parameters, but not on the number of template
  88. // parameters. Without the restriction, the meaning of the following
  89. // is unclear:
  90. //
  91. // OverloadedAction<int, bool>(x);
  92. //
  93. // Are we using a single-template-parameter action where 'bool' refers
  94. // to the type of x, or are we using a two-template-parameter action
  95. // where the compiler is asked to infer the type of x?
  96. //
  97. // Implementation notes:
  98. //
  99. // GMOCK_INTERNAL_*_HAS_m_TEMPLATE_PARAMS and
  100. // GMOCK_INTERNAL_*_AND_n_VALUE_PARAMS are internal macros for
  101. // implementing ACTION_TEMPLATE. The main trick we use is to create
  102. // new macro invocations when expanding a macro. For example, we have
  103. //
  104. // #define ACTION_TEMPLATE(name, template_params, value_params)
  105. // ... GMOCK_INTERNAL_DECL_##template_params ...
  106. //
  107. // which causes ACTION_TEMPLATE(..., HAS_1_TEMPLATE_PARAMS(typename, T), ...)
  108. // to expand to
  109. //
  110. // ... GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(typename, T) ...
  111. //
  112. // Since GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS is a macro, the
  113. // preprocessor will continue to expand it to
  114. //
  115. // ... typename T ...
  116. //
  117. // This technique conforms to the C++ standard and is portable. It
  118. // allows us to implement action templates using O(N) code, where N is
  119. // the maximum number of template/value parameters supported. Without
  120. // using it, we'd have to devote O(N^2) amount of code to implement all
  121. // combinations of m and n.
  122. // Declares the template parameters.
  123. #define GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(kind0, name0) kind0 name0
  124. #define GMOCK_INTERNAL_DECL_HAS_2_TEMPLATE_PARAMS(kind0, name0, kind1, name1) \
  125. kind0 name0, kind1 name1
  126. #define GMOCK_INTERNAL_DECL_HAS_3_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  127. kind2, name2) \
  128. kind0 name0, kind1 name1, kind2 name2
  129. #define GMOCK_INTERNAL_DECL_HAS_4_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  130. kind2, name2, kind3, name3) \
  131. kind0 name0, kind1 name1, kind2 name2, kind3 name3
  132. #define GMOCK_INTERNAL_DECL_HAS_5_TEMPLATE_PARAMS( \
  133. kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4) \
  134. kind0 name0, kind1 name1, kind2 name2, kind3 name3, kind4 name4
  135. #define GMOCK_INTERNAL_DECL_HAS_6_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  136. kind2, name2, kind3, name3, \
  137. kind4, name4, kind5, name5) \
  138. kind0 name0, kind1 name1, kind2 name2, kind3 name3, kind4 name4, kind5 name5
  139. #define GMOCK_INTERNAL_DECL_HAS_7_TEMPLATE_PARAMS( \
  140. kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4, \
  141. kind5, name5, kind6, name6) \
  142. kind0 name0, kind1 name1, kind2 name2, kind3 name3, kind4 name4, \
  143. kind5 name5, kind6 name6
  144. #define GMOCK_INTERNAL_DECL_HAS_8_TEMPLATE_PARAMS( \
  145. kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4, \
  146. kind5, name5, kind6, name6, kind7, name7) \
  147. kind0 name0, kind1 name1, kind2 name2, kind3 name3, kind4 name4, \
  148. kind5 name5, kind6 name6, kind7 name7
  149. #define GMOCK_INTERNAL_DECL_HAS_9_TEMPLATE_PARAMS( \
  150. kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4, \
  151. kind5, name5, kind6, name6, kind7, name7, kind8, name8) \
  152. kind0 name0, kind1 name1, kind2 name2, kind3 name3, kind4 name4, \
  153. kind5 name5, kind6 name6, kind7 name7, kind8 name8
  154. #define GMOCK_INTERNAL_DECL_HAS_10_TEMPLATE_PARAMS( \
  155. kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4, \
  156. kind5, name5, kind6, name6, kind7, name7, kind8, name8, kind9, name9) \
  157. kind0 name0, kind1 name1, kind2 name2, kind3 name3, kind4 name4, \
  158. kind5 name5, kind6 name6, kind7 name7, kind8 name8, kind9 name9
  159. // Lists the template parameters.
  160. #define GMOCK_INTERNAL_LIST_HAS_1_TEMPLATE_PARAMS(kind0, name0) name0
  161. #define GMOCK_INTERNAL_LIST_HAS_2_TEMPLATE_PARAMS(kind0, name0, kind1, name1) \
  162. name0, name1
  163. #define GMOCK_INTERNAL_LIST_HAS_3_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  164. kind2, name2) \
  165. name0, name1, name2
  166. #define GMOCK_INTERNAL_LIST_HAS_4_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  167. kind2, name2, kind3, name3) \
  168. name0, name1, name2, name3
  169. #define GMOCK_INTERNAL_LIST_HAS_5_TEMPLATE_PARAMS( \
  170. kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4) \
  171. name0, name1, name2, name3, name4
  172. #define GMOCK_INTERNAL_LIST_HAS_6_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  173. kind2, name2, kind3, name3, \
  174. kind4, name4, kind5, name5) \
  175. name0, name1, name2, name3, name4, name5
  176. #define GMOCK_INTERNAL_LIST_HAS_7_TEMPLATE_PARAMS( \
  177. kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4, \
  178. kind5, name5, kind6, name6) \
  179. name0, name1, name2, name3, name4, name5, name6
  180. #define GMOCK_INTERNAL_LIST_HAS_8_TEMPLATE_PARAMS( \
  181. kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4, \
  182. kind5, name5, kind6, name6, kind7, name7) \
  183. name0, name1, name2, name3, name4, name5, name6, name7
  184. #define GMOCK_INTERNAL_LIST_HAS_9_TEMPLATE_PARAMS( \
  185. kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4, \
  186. kind5, name5, kind6, name6, kind7, name7, kind8, name8) \
  187. name0, name1, name2, name3, name4, name5, name6, name7, name8
  188. #define GMOCK_INTERNAL_LIST_HAS_10_TEMPLATE_PARAMS( \
  189. kind0, name0, kind1, name1, kind2, name2, kind3, name3, kind4, name4, \
  190. kind5, name5, kind6, name6, kind7, name7, kind8, name8, kind9, name9) \
  191. name0, name1, name2, name3, name4, name5, name6, name7, name8, name9
  192. // Declares the types of value parameters.
  193. #define GMOCK_INTERNAL_DECL_TYPE_AND_0_VALUE_PARAMS()
  194. #define GMOCK_INTERNAL_DECL_TYPE_AND_1_VALUE_PARAMS(p0) , typename p0##_type
  195. #define GMOCK_INTERNAL_DECL_TYPE_AND_2_VALUE_PARAMS(p0, p1) \
  196. , typename p0##_type, typename p1##_type
  197. #define GMOCK_INTERNAL_DECL_TYPE_AND_3_VALUE_PARAMS(p0, p1, p2) \
  198. , typename p0##_type, typename p1##_type, typename p2##_type
  199. #define GMOCK_INTERNAL_DECL_TYPE_AND_4_VALUE_PARAMS(p0, p1, p2, p3) \
  200. , typename p0##_type, typename p1##_type, typename p2##_type, \
  201. typename p3##_type
  202. #define GMOCK_INTERNAL_DECL_TYPE_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) \
  203. , typename p0##_type, typename p1##_type, typename p2##_type, \
  204. typename p3##_type, typename p4##_type
  205. #define GMOCK_INTERNAL_DECL_TYPE_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) \
  206. , typename p0##_type, typename p1##_type, typename p2##_type, \
  207. typename p3##_type, typename p4##_type, typename p5##_type
  208. #define GMOCK_INTERNAL_DECL_TYPE_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  209. p6) \
  210. , typename p0##_type, typename p1##_type, typename p2##_type, \
  211. typename p3##_type, typename p4##_type, typename p5##_type, \
  212. typename p6##_type
  213. #define GMOCK_INTERNAL_DECL_TYPE_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  214. p6, p7) \
  215. , typename p0##_type, typename p1##_type, typename p2##_type, \
  216. typename p3##_type, typename p4##_type, typename p5##_type, \
  217. typename p6##_type, typename p7##_type
  218. #define GMOCK_INTERNAL_DECL_TYPE_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  219. p6, p7, p8) \
  220. , typename p0##_type, typename p1##_type, typename p2##_type, \
  221. typename p3##_type, typename p4##_type, typename p5##_type, \
  222. typename p6##_type, typename p7##_type, typename p8##_type
  223. #define GMOCK_INTERNAL_DECL_TYPE_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  224. p6, p7, p8, p9) \
  225. , typename p0##_type, typename p1##_type, typename p2##_type, \
  226. typename p3##_type, typename p4##_type, typename p5##_type, \
  227. typename p6##_type, typename p7##_type, typename p8##_type, \
  228. typename p9##_type
  229. // Initializes the value parameters.
  230. #define GMOCK_INTERNAL_INIT_AND_0_VALUE_PARAMS() ()
  231. #define GMOCK_INTERNAL_INIT_AND_1_VALUE_PARAMS(p0) \
  232. (p0##_type gmock_p0) : p0(::std::move(gmock_p0))
  233. #define GMOCK_INTERNAL_INIT_AND_2_VALUE_PARAMS(p0, p1) \
  234. (p0##_type gmock_p0, p1##_type gmock_p1) \
  235. : p0(::std::move(gmock_p0)), p1(::std::move(gmock_p1))
  236. #define GMOCK_INTERNAL_INIT_AND_3_VALUE_PARAMS(p0, p1, p2) \
  237. (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2) \
  238. : p0(::std::move(gmock_p0)), \
  239. p1(::std::move(gmock_p1)), \
  240. p2(::std::move(gmock_p2))
  241. #define GMOCK_INTERNAL_INIT_AND_4_VALUE_PARAMS(p0, p1, p2, p3) \
  242. (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
  243. p3##_type gmock_p3) \
  244. : p0(::std::move(gmock_p0)), \
  245. p1(::std::move(gmock_p1)), \
  246. p2(::std::move(gmock_p2)), \
  247. p3(::std::move(gmock_p3))
  248. #define GMOCK_INTERNAL_INIT_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) \
  249. (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
  250. p3##_type gmock_p3, p4##_type gmock_p4) \
  251. : p0(::std::move(gmock_p0)), \
  252. p1(::std::move(gmock_p1)), \
  253. p2(::std::move(gmock_p2)), \
  254. p3(::std::move(gmock_p3)), \
  255. p4(::std::move(gmock_p4))
  256. #define GMOCK_INTERNAL_INIT_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) \
  257. (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
  258. p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5) \
  259. : p0(::std::move(gmock_p0)), \
  260. p1(::std::move(gmock_p1)), \
  261. p2(::std::move(gmock_p2)), \
  262. p3(::std::move(gmock_p3)), \
  263. p4(::std::move(gmock_p4)), \
  264. p5(::std::move(gmock_p5))
  265. #define GMOCK_INTERNAL_INIT_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6) \
  266. (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
  267. p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5, \
  268. p6##_type gmock_p6) \
  269. : p0(::std::move(gmock_p0)), \
  270. p1(::std::move(gmock_p1)), \
  271. p2(::std::move(gmock_p2)), \
  272. p3(::std::move(gmock_p3)), \
  273. p4(::std::move(gmock_p4)), \
  274. p5(::std::move(gmock_p5)), \
  275. p6(::std::move(gmock_p6))
  276. #define GMOCK_INTERNAL_INIT_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7) \
  277. (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
  278. p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5, \
  279. p6##_type gmock_p6, p7##_type gmock_p7) \
  280. : p0(::std::move(gmock_p0)), \
  281. p1(::std::move(gmock_p1)), \
  282. p2(::std::move(gmock_p2)), \
  283. p3(::std::move(gmock_p3)), \
  284. p4(::std::move(gmock_p4)), \
  285. p5(::std::move(gmock_p5)), \
  286. p6(::std::move(gmock_p6)), \
  287. p7(::std::move(gmock_p7))
  288. #define GMOCK_INTERNAL_INIT_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7, \
  289. p8) \
  290. (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
  291. p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5, \
  292. p6##_type gmock_p6, p7##_type gmock_p7, p8##_type gmock_p8) \
  293. : p0(::std::move(gmock_p0)), \
  294. p1(::std::move(gmock_p1)), \
  295. p2(::std::move(gmock_p2)), \
  296. p3(::std::move(gmock_p3)), \
  297. p4(::std::move(gmock_p4)), \
  298. p5(::std::move(gmock_p5)), \
  299. p6(::std::move(gmock_p6)), \
  300. p7(::std::move(gmock_p7)), \
  301. p8(::std::move(gmock_p8))
  302. #define GMOCK_INTERNAL_INIT_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  303. p7, p8, p9) \
  304. (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
  305. p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5, \
  306. p6##_type gmock_p6, p7##_type gmock_p7, p8##_type gmock_p8, \
  307. p9##_type gmock_p9) \
  308. : p0(::std::move(gmock_p0)), \
  309. p1(::std::move(gmock_p1)), \
  310. p2(::std::move(gmock_p2)), \
  311. p3(::std::move(gmock_p3)), \
  312. p4(::std::move(gmock_p4)), \
  313. p5(::std::move(gmock_p5)), \
  314. p6(::std::move(gmock_p6)), \
  315. p7(::std::move(gmock_p7)), \
  316. p8(::std::move(gmock_p8)), \
  317. p9(::std::move(gmock_p9))
  318. // Defines the copy constructor
  319. #define GMOCK_INTERNAL_DEFN_COPY_AND_0_VALUE_PARAMS() \
  320. {} // Avoid https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82134
  321. #define GMOCK_INTERNAL_DEFN_COPY_AND_1_VALUE_PARAMS(...) = default;
  322. #define GMOCK_INTERNAL_DEFN_COPY_AND_2_VALUE_PARAMS(...) = default;
  323. #define GMOCK_INTERNAL_DEFN_COPY_AND_3_VALUE_PARAMS(...) = default;
  324. #define GMOCK_INTERNAL_DEFN_COPY_AND_4_VALUE_PARAMS(...) = default;
  325. #define GMOCK_INTERNAL_DEFN_COPY_AND_5_VALUE_PARAMS(...) = default;
  326. #define GMOCK_INTERNAL_DEFN_COPY_AND_6_VALUE_PARAMS(...) = default;
  327. #define GMOCK_INTERNAL_DEFN_COPY_AND_7_VALUE_PARAMS(...) = default;
  328. #define GMOCK_INTERNAL_DEFN_COPY_AND_8_VALUE_PARAMS(...) = default;
  329. #define GMOCK_INTERNAL_DEFN_COPY_AND_9_VALUE_PARAMS(...) = default;
  330. #define GMOCK_INTERNAL_DEFN_COPY_AND_10_VALUE_PARAMS(...) = default;
  331. // Declares the fields for storing the value parameters.
  332. #define GMOCK_INTERNAL_DEFN_AND_0_VALUE_PARAMS()
  333. #define GMOCK_INTERNAL_DEFN_AND_1_VALUE_PARAMS(p0) p0##_type p0;
  334. #define GMOCK_INTERNAL_DEFN_AND_2_VALUE_PARAMS(p0, p1) \
  335. p0##_type p0; \
  336. p1##_type p1;
  337. #define GMOCK_INTERNAL_DEFN_AND_3_VALUE_PARAMS(p0, p1, p2) \
  338. p0##_type p0; \
  339. p1##_type p1; \
  340. p2##_type p2;
  341. #define GMOCK_INTERNAL_DEFN_AND_4_VALUE_PARAMS(p0, p1, p2, p3) \
  342. p0##_type p0; \
  343. p1##_type p1; \
  344. p2##_type p2; \
  345. p3##_type p3;
  346. #define GMOCK_INTERNAL_DEFN_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) \
  347. p0##_type p0; \
  348. p1##_type p1; \
  349. p2##_type p2; \
  350. p3##_type p3; \
  351. p4##_type p4;
  352. #define GMOCK_INTERNAL_DEFN_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) \
  353. p0##_type p0; \
  354. p1##_type p1; \
  355. p2##_type p2; \
  356. p3##_type p3; \
  357. p4##_type p4; \
  358. p5##_type p5;
  359. #define GMOCK_INTERNAL_DEFN_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6) \
  360. p0##_type p0; \
  361. p1##_type p1; \
  362. p2##_type p2; \
  363. p3##_type p3; \
  364. p4##_type p4; \
  365. p5##_type p5; \
  366. p6##_type p6;
  367. #define GMOCK_INTERNAL_DEFN_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7) \
  368. p0##_type p0; \
  369. p1##_type p1; \
  370. p2##_type p2; \
  371. p3##_type p3; \
  372. p4##_type p4; \
  373. p5##_type p5; \
  374. p6##_type p6; \
  375. p7##_type p7;
  376. #define GMOCK_INTERNAL_DEFN_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7, \
  377. p8) \
  378. p0##_type p0; \
  379. p1##_type p1; \
  380. p2##_type p2; \
  381. p3##_type p3; \
  382. p4##_type p4; \
  383. p5##_type p5; \
  384. p6##_type p6; \
  385. p7##_type p7; \
  386. p8##_type p8;
  387. #define GMOCK_INTERNAL_DEFN_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  388. p7, p8, p9) \
  389. p0##_type p0; \
  390. p1##_type p1; \
  391. p2##_type p2; \
  392. p3##_type p3; \
  393. p4##_type p4; \
  394. p5##_type p5; \
  395. p6##_type p6; \
  396. p7##_type p7; \
  397. p8##_type p8; \
  398. p9##_type p9;
  399. // Lists the value parameters.
  400. #define GMOCK_INTERNAL_LIST_AND_0_VALUE_PARAMS()
  401. #define GMOCK_INTERNAL_LIST_AND_1_VALUE_PARAMS(p0) p0
  402. #define GMOCK_INTERNAL_LIST_AND_2_VALUE_PARAMS(p0, p1) p0, p1
  403. #define GMOCK_INTERNAL_LIST_AND_3_VALUE_PARAMS(p0, p1, p2) p0, p1, p2
  404. #define GMOCK_INTERNAL_LIST_AND_4_VALUE_PARAMS(p0, p1, p2, p3) p0, p1, p2, p3
  405. #define GMOCK_INTERNAL_LIST_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) \
  406. p0, p1, p2, p3, p4
  407. #define GMOCK_INTERNAL_LIST_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) \
  408. p0, p1, p2, p3, p4, p5
  409. #define GMOCK_INTERNAL_LIST_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6) \
  410. p0, p1, p2, p3, p4, p5, p6
  411. #define GMOCK_INTERNAL_LIST_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7) \
  412. p0, p1, p2, p3, p4, p5, p6, p7
  413. #define GMOCK_INTERNAL_LIST_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7, \
  414. p8) \
  415. p0, p1, p2, p3, p4, p5, p6, p7, p8
  416. #define GMOCK_INTERNAL_LIST_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  417. p7, p8, p9) \
  418. p0, p1, p2, p3, p4, p5, p6, p7, p8, p9
  419. // Lists the value parameter types.
  420. #define GMOCK_INTERNAL_LIST_TYPE_AND_0_VALUE_PARAMS()
  421. #define GMOCK_INTERNAL_LIST_TYPE_AND_1_VALUE_PARAMS(p0) , p0##_type
  422. #define GMOCK_INTERNAL_LIST_TYPE_AND_2_VALUE_PARAMS(p0, p1) \
  423. , p0##_type, p1##_type
  424. #define GMOCK_INTERNAL_LIST_TYPE_AND_3_VALUE_PARAMS(p0, p1, p2) \
  425. , p0##_type, p1##_type, p2##_type
  426. #define GMOCK_INTERNAL_LIST_TYPE_AND_4_VALUE_PARAMS(p0, p1, p2, p3) \
  427. , p0##_type, p1##_type, p2##_type, p3##_type
  428. #define GMOCK_INTERNAL_LIST_TYPE_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) \
  429. , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type
  430. #define GMOCK_INTERNAL_LIST_TYPE_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) \
  431. , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, p5##_type
  432. #define GMOCK_INTERNAL_LIST_TYPE_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  433. p6) \
  434. , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, p5##_type, p6##_type
  435. #define GMOCK_INTERNAL_LIST_TYPE_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  436. p6, p7) \
  437. , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, p5##_type, \
  438. p6##_type, p7##_type
  439. #define GMOCK_INTERNAL_LIST_TYPE_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  440. p6, p7, p8) \
  441. , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, p5##_type, \
  442. p6##_type, p7##_type, p8##_type
  443. #define GMOCK_INTERNAL_LIST_TYPE_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  444. p6, p7, p8, p9) \
  445. , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, p5##_type, \
  446. p6##_type, p7##_type, p8##_type, p9##_type
  447. // Declares the value parameters.
  448. #define GMOCK_INTERNAL_DECL_AND_0_VALUE_PARAMS()
  449. #define GMOCK_INTERNAL_DECL_AND_1_VALUE_PARAMS(p0) p0##_type p0
  450. #define GMOCK_INTERNAL_DECL_AND_2_VALUE_PARAMS(p0, p1) \
  451. p0##_type p0, p1##_type p1
  452. #define GMOCK_INTERNAL_DECL_AND_3_VALUE_PARAMS(p0, p1, p2) \
  453. p0##_type p0, p1##_type p1, p2##_type p2
  454. #define GMOCK_INTERNAL_DECL_AND_4_VALUE_PARAMS(p0, p1, p2, p3) \
  455. p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3
  456. #define GMOCK_INTERNAL_DECL_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) \
  457. p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4
  458. #define GMOCK_INTERNAL_DECL_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) \
  459. p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4, \
  460. p5##_type p5
  461. #define GMOCK_INTERNAL_DECL_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6) \
  462. p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4, \
  463. p5##_type p5, p6##_type p6
  464. #define GMOCK_INTERNAL_DECL_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7) \
  465. p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4, \
  466. p5##_type p5, p6##_type p6, p7##_type p7
  467. #define GMOCK_INTERNAL_DECL_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7, \
  468. p8) \
  469. p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4, \
  470. p5##_type p5, p6##_type p6, p7##_type p7, p8##_type p8
  471. #define GMOCK_INTERNAL_DECL_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  472. p7, p8, p9) \
  473. p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4, \
  474. p5##_type p5, p6##_type p6, p7##_type p7, p8##_type p8, p9##_type p9
  475. // The suffix of the class template implementing the action template.
  476. #define GMOCK_INTERNAL_COUNT_AND_0_VALUE_PARAMS()
  477. #define GMOCK_INTERNAL_COUNT_AND_1_VALUE_PARAMS(p0) P
  478. #define GMOCK_INTERNAL_COUNT_AND_2_VALUE_PARAMS(p0, p1) P2
  479. #define GMOCK_INTERNAL_COUNT_AND_3_VALUE_PARAMS(p0, p1, p2) P3
  480. #define GMOCK_INTERNAL_COUNT_AND_4_VALUE_PARAMS(p0, p1, p2, p3) P4
  481. #define GMOCK_INTERNAL_COUNT_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) P5
  482. #define GMOCK_INTERNAL_COUNT_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) P6
  483. #define GMOCK_INTERNAL_COUNT_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6) P7
  484. #define GMOCK_INTERNAL_COUNT_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  485. p7) \
  486. P8
  487. #define GMOCK_INTERNAL_COUNT_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  488. p7, p8) \
  489. P9
  490. #define GMOCK_INTERNAL_COUNT_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  491. p7, p8, p9) \
  492. P10
  493. // The name of the class template implementing the action template.
  494. #define GMOCK_ACTION_CLASS_(name, value_params) \
  495. GTEST_CONCAT_TOKEN_(name##Action, GMOCK_INTERNAL_COUNT_##value_params)
  496. #define ACTION_TEMPLATE(name, template_params, value_params) \
  497. template <GMOCK_INTERNAL_DECL_##template_params \
  498. GMOCK_INTERNAL_DECL_TYPE_##value_params> \
  499. class GMOCK_ACTION_CLASS_(name, value_params) { \
  500. public: \
  501. explicit GMOCK_ACTION_CLASS_(name, value_params)( \
  502. GMOCK_INTERNAL_DECL_##value_params) \
  503. GMOCK_PP_IF(GMOCK_PP_IS_EMPTY(GMOCK_INTERNAL_COUNT_##value_params), \
  504. = default; \
  505. , \
  506. : impl_(std::make_shared<gmock_Impl>( \
  507. GMOCK_INTERNAL_LIST_##value_params)){}) \
  508. GMOCK_ACTION_CLASS_(name, value_params)(const GMOCK_ACTION_CLASS_( \
  509. name, value_params) &) noexcept GMOCK_INTERNAL_DEFN_COPY_ \
  510. ##value_params \
  511. GMOCK_ACTION_CLASS_(name, value_params)(GMOCK_ACTION_CLASS_( \
  512. name, value_params) &&) noexcept GMOCK_INTERNAL_DEFN_COPY_ \
  513. ##value_params template <typename F> \
  514. operator ::testing::Action<F>() const { \
  515. return GMOCK_PP_IF( \
  516. GMOCK_PP_IS_EMPTY(GMOCK_INTERNAL_COUNT_##value_params), \
  517. (::testing::internal::MakeAction<F, gmock_Impl>()), \
  518. (::testing::internal::MakeAction<F>(impl_))); \
  519. } \
  520. \
  521. private: \
  522. class gmock_Impl { \
  523. public: \
  524. explicit gmock_Impl GMOCK_INTERNAL_INIT_##value_params {} \
  525. template <typename function_type, typename return_type, \
  526. typename args_type, GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
  527. return_type gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_) const; \
  528. GMOCK_INTERNAL_DEFN_##value_params \
  529. }; \
  530. GMOCK_PP_IF(GMOCK_PP_IS_EMPTY(GMOCK_INTERNAL_COUNT_##value_params), , \
  531. std::shared_ptr<const gmock_Impl> impl_;) \
  532. }; \
  533. template <GMOCK_INTERNAL_DECL_##template_params \
  534. GMOCK_INTERNAL_DECL_TYPE_##value_params> \
  535. GMOCK_ACTION_CLASS_( \
  536. name, value_params)<GMOCK_INTERNAL_LIST_##template_params \
  537. GMOCK_INTERNAL_LIST_TYPE_##value_params> \
  538. name(GMOCK_INTERNAL_DECL_##value_params) GTEST_MUST_USE_RESULT_; \
  539. template <GMOCK_INTERNAL_DECL_##template_params \
  540. GMOCK_INTERNAL_DECL_TYPE_##value_params> \
  541. inline GMOCK_ACTION_CLASS_( \
  542. name, value_params)<GMOCK_INTERNAL_LIST_##template_params \
  543. GMOCK_INTERNAL_LIST_TYPE_##value_params> \
  544. name(GMOCK_INTERNAL_DECL_##value_params) { \
  545. return GMOCK_ACTION_CLASS_( \
  546. name, value_params)<GMOCK_INTERNAL_LIST_##template_params \
  547. GMOCK_INTERNAL_LIST_TYPE_##value_params>( \
  548. GMOCK_INTERNAL_LIST_##value_params); \
  549. } \
  550. template <GMOCK_INTERNAL_DECL_##template_params \
  551. GMOCK_INTERNAL_DECL_TYPE_##value_params> \
  552. template <typename function_type, typename return_type, typename args_type, \
  553. GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
  554. return_type GMOCK_ACTION_CLASS_( \
  555. name, value_params)<GMOCK_INTERNAL_LIST_##template_params \
  556. GMOCK_INTERNAL_LIST_TYPE_##value_params>:: \
  557. gmock_Impl::gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) \
  558. const
  559. namespace testing {
  560. // The ACTION*() macros trigger warning C4100 (unreferenced formal
  561. // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
  562. // the macro definition, as the warnings are generated when the macro
  563. // is expanded and macro expansion cannot contain #pragma. Therefore
  564. // we suppress them here.
  565. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
  566. namespace internal {
  567. // internal::InvokeArgument - a helper for InvokeArgument action.
  568. // The basic overloads are provided here for generic functors.
  569. // Overloads for other custom-callables are provided in the
  570. // internal/custom/gmock-generated-actions.h header.
  571. template <typename F, typename... Args>
  572. auto InvokeArgument(F &&f,
  573. Args... args) -> decltype(std::forward<F>(f)(args...)) {
  574. return std::forward<F>(f)(args...);
  575. }
  576. template <std::size_t index, typename... Params>
  577. struct InvokeArgumentAction {
  578. template <typename... Args,
  579. typename = typename std::enable_if<(index < sizeof...(Args))>::type>
  580. auto operator()(Args &&...args) const -> decltype(internal::InvokeArgument(
  581. std::get<index>(std::forward_as_tuple(std::forward<Args>(args)...)),
  582. std::declval<const Params &>()...)) {
  583. internal::FlatTuple<Args &&...> args_tuple(FlatTupleConstructTag{},
  584. std::forward<Args>(args)...);
  585. return params.Apply([&](const Params &...unpacked_params) {
  586. auto &&callable = std::move(args_tuple.template Get<index>());
  587. return internal::InvokeArgument(
  588. std::forward<decltype(callable)>(callable), unpacked_params...);
  589. });
  590. }
  591. internal::FlatTuple<Params...> params;
  592. };
  593. } // namespace internal
  594. // The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th
  595. // (0-based) argument, which must be a k-ary callable, of the mock
  596. // function, with arguments a1, a2, ..., a_k.
  597. //
  598. // Notes:
  599. //
  600. // 1. The arguments are passed by value by default. If you need to
  601. // pass an argument by reference, wrap it inside std::ref(). For
  602. // example,
  603. //
  604. // InvokeArgument<1>(5, string("Hello"), std::ref(foo))
  605. //
  606. // passes 5 and string("Hello") by value, and passes foo by
  607. // reference.
  608. //
  609. // 2. If the callable takes an argument by reference but std::ref() is
  610. // not used, it will receive the reference to a copy of the value,
  611. // instead of the original value. For example, when the 0-th
  612. // argument of the mock function takes a const string&, the action
  613. //
  614. // InvokeArgument<0>(string("Hello"))
  615. //
  616. // makes a copy of the temporary string("Hello") object and passes a
  617. // reference of the copy, instead of the original temporary object,
  618. // to the callable. This makes it easy for a user to define an
  619. // InvokeArgument action from temporary values and have it performed
  620. // later.
  621. template <std::size_t index, typename... Params>
  622. internal::InvokeArgumentAction<index, typename std::decay<Params>::type...>
  623. InvokeArgument(Params &&...params) {
  624. return {internal::FlatTuple<typename std::decay<Params>::type...>(
  625. internal::FlatTupleConstructTag{}, std::forward<Params>(params)...)};
  626. }
  627. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100
  628. } // namespace testing
  629. #endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_