gmock-more-actions.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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, \
  125. name1) kind0 name0, kind1 name1
  126. #define GMOCK_INTERNAL_DECL_HAS_3_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  127. kind2, name2) kind0 name0, kind1 name1, kind2 name2
  128. #define GMOCK_INTERNAL_DECL_HAS_4_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  129. kind2, name2, kind3, name3) kind0 name0, kind1 name1, kind2 name2, \
  130. kind3 name3
  131. #define GMOCK_INTERNAL_DECL_HAS_5_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  132. kind2, name2, kind3, name3, kind4, name4) kind0 name0, kind1 name1, \
  133. kind2 name2, kind3 name3, kind4 name4
  134. #define GMOCK_INTERNAL_DECL_HAS_6_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  135. kind2, name2, kind3, name3, kind4, name4, kind5, name5) kind0 name0, \
  136. kind1 name1, kind2 name2, kind3 name3, kind4 name4, kind5 name5
  137. #define GMOCK_INTERNAL_DECL_HAS_7_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  138. kind2, name2, kind3, name3, kind4, name4, kind5, name5, kind6, \
  139. name6) kind0 name0, kind1 name1, kind2 name2, kind3 name3, kind4 name4, \
  140. kind5 name5, kind6 name6
  141. #define GMOCK_INTERNAL_DECL_HAS_8_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  142. kind2, name2, kind3, name3, kind4, name4, kind5, name5, kind6, name6, \
  143. kind7, name7) kind0 name0, kind1 name1, kind2 name2, kind3 name3, \
  144. kind4 name4, kind5 name5, kind6 name6, kind7 name7
  145. #define GMOCK_INTERNAL_DECL_HAS_9_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  146. kind2, name2, kind3, name3, kind4, name4, kind5, name5, kind6, name6, \
  147. kind7, name7, kind8, name8) kind0 name0, kind1 name1, kind2 name2, \
  148. kind3 name3, kind4 name4, kind5 name5, kind6 name6, kind7 name7, \
  149. kind8 name8
  150. #define GMOCK_INTERNAL_DECL_HAS_10_TEMPLATE_PARAMS(kind0, name0, kind1, \
  151. name1, kind2, name2, kind3, name3, kind4, name4, kind5, name5, kind6, \
  152. name6, kind7, name7, kind8, name8, kind9, name9) kind0 name0, \
  153. kind1 name1, kind2 name2, kind3 name3, kind4 name4, kind5 name5, \
  154. kind6 name6, kind7 name7, kind8 name8, kind9 name9
  155. // Lists the template parameters.
  156. #define GMOCK_INTERNAL_LIST_HAS_1_TEMPLATE_PARAMS(kind0, name0) name0
  157. #define GMOCK_INTERNAL_LIST_HAS_2_TEMPLATE_PARAMS(kind0, name0, kind1, \
  158. name1) name0, name1
  159. #define GMOCK_INTERNAL_LIST_HAS_3_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  160. kind2, name2) name0, name1, name2
  161. #define GMOCK_INTERNAL_LIST_HAS_4_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  162. kind2, name2, kind3, name3) name0, name1, name2, name3
  163. #define GMOCK_INTERNAL_LIST_HAS_5_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  164. kind2, name2, kind3, name3, kind4, name4) name0, name1, name2, name3, \
  165. name4
  166. #define GMOCK_INTERNAL_LIST_HAS_6_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  167. kind2, name2, kind3, name3, kind4, name4, kind5, name5) name0, name1, \
  168. name2, name3, name4, name5
  169. #define GMOCK_INTERNAL_LIST_HAS_7_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  170. kind2, name2, kind3, name3, kind4, name4, kind5, name5, kind6, \
  171. name6) name0, name1, name2, name3, name4, name5, name6
  172. #define GMOCK_INTERNAL_LIST_HAS_8_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  173. kind2, name2, kind3, name3, kind4, name4, kind5, name5, kind6, name6, \
  174. kind7, name7) name0, name1, name2, name3, name4, name5, name6, name7
  175. #define GMOCK_INTERNAL_LIST_HAS_9_TEMPLATE_PARAMS(kind0, name0, kind1, name1, \
  176. kind2, name2, kind3, name3, kind4, name4, kind5, name5, kind6, name6, \
  177. kind7, name7, kind8, name8) name0, name1, name2, name3, name4, name5, \
  178. name6, name7, name8
  179. #define GMOCK_INTERNAL_LIST_HAS_10_TEMPLATE_PARAMS(kind0, name0, kind1, \
  180. name1, kind2, name2, kind3, name3, kind4, name4, kind5, name5, kind6, \
  181. name6, kind7, name7, kind8, name8, kind9, name9) name0, name1, name2, \
  182. name3, name4, name5, name6, name7, name8, name9
  183. // Declares the types of value parameters.
  184. #define GMOCK_INTERNAL_DECL_TYPE_AND_0_VALUE_PARAMS()
  185. #define GMOCK_INTERNAL_DECL_TYPE_AND_1_VALUE_PARAMS(p0) , typename p0##_type
  186. #define GMOCK_INTERNAL_DECL_TYPE_AND_2_VALUE_PARAMS(p0, p1) , \
  187. typename p0##_type, typename p1##_type
  188. #define GMOCK_INTERNAL_DECL_TYPE_AND_3_VALUE_PARAMS(p0, p1, p2) , \
  189. typename p0##_type, typename p1##_type, typename p2##_type
  190. #define GMOCK_INTERNAL_DECL_TYPE_AND_4_VALUE_PARAMS(p0, p1, p2, p3) , \
  191. typename p0##_type, typename p1##_type, typename p2##_type, \
  192. typename p3##_type
  193. #define GMOCK_INTERNAL_DECL_TYPE_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) , \
  194. typename p0##_type, typename p1##_type, typename p2##_type, \
  195. typename p3##_type, typename p4##_type
  196. #define GMOCK_INTERNAL_DECL_TYPE_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) , \
  197. typename p0##_type, typename p1##_type, typename p2##_type, \
  198. typename p3##_type, typename p4##_type, typename p5##_type
  199. #define GMOCK_INTERNAL_DECL_TYPE_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  200. p6) , typename p0##_type, typename p1##_type, typename p2##_type, \
  201. typename p3##_type, typename p4##_type, typename p5##_type, \
  202. typename p6##_type
  203. #define GMOCK_INTERNAL_DECL_TYPE_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  204. p6, p7) , typename p0##_type, typename p1##_type, typename p2##_type, \
  205. typename p3##_type, typename p4##_type, typename p5##_type, \
  206. typename p6##_type, typename p7##_type
  207. #define GMOCK_INTERNAL_DECL_TYPE_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  208. p6, p7, p8) , typename p0##_type, typename p1##_type, typename p2##_type, \
  209. typename p3##_type, typename p4##_type, typename p5##_type, \
  210. typename p6##_type, typename p7##_type, typename p8##_type
  211. #define GMOCK_INTERNAL_DECL_TYPE_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  212. p6, p7, p8, p9) , typename p0##_type, typename p1##_type, \
  213. typename p2##_type, typename p3##_type, typename p4##_type, \
  214. typename p5##_type, typename p6##_type, typename p7##_type, \
  215. typename p8##_type, typename p9##_type
  216. // Initializes the value parameters.
  217. #define GMOCK_INTERNAL_INIT_AND_0_VALUE_PARAMS()\
  218. ()
  219. #define GMOCK_INTERNAL_INIT_AND_1_VALUE_PARAMS(p0)\
  220. (p0##_type gmock_p0) : p0(::std::move(gmock_p0))
  221. #define GMOCK_INTERNAL_INIT_AND_2_VALUE_PARAMS(p0, p1)\
  222. (p0##_type gmock_p0, p1##_type gmock_p1) : p0(::std::move(gmock_p0)), \
  223. p1(::std::move(gmock_p1))
  224. #define GMOCK_INTERNAL_INIT_AND_3_VALUE_PARAMS(p0, p1, p2)\
  225. (p0##_type gmock_p0, p1##_type gmock_p1, \
  226. p2##_type gmock_p2) : p0(::std::move(gmock_p0)), \
  227. p1(::std::move(gmock_p1)), p2(::std::move(gmock_p2))
  228. #define GMOCK_INTERNAL_INIT_AND_4_VALUE_PARAMS(p0, p1, p2, p3)\
  229. (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
  230. p3##_type gmock_p3) : p0(::std::move(gmock_p0)), \
  231. p1(::std::move(gmock_p1)), p2(::std::move(gmock_p2)), \
  232. p3(::std::move(gmock_p3))
  233. #define GMOCK_INTERNAL_INIT_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4)\
  234. (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
  235. p3##_type gmock_p3, p4##_type gmock_p4) : p0(::std::move(gmock_p0)), \
  236. p1(::std::move(gmock_p1)), p2(::std::move(gmock_p2)), \
  237. p3(::std::move(gmock_p3)), p4(::std::move(gmock_p4))
  238. #define GMOCK_INTERNAL_INIT_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5)\
  239. (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
  240. p3##_type gmock_p3, p4##_type gmock_p4, \
  241. p5##_type gmock_p5) : p0(::std::move(gmock_p0)), \
  242. p1(::std::move(gmock_p1)), p2(::std::move(gmock_p2)), \
  243. p3(::std::move(gmock_p3)), p4(::std::move(gmock_p4)), \
  244. p5(::std::move(gmock_p5))
  245. #define GMOCK_INTERNAL_INIT_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6)\
  246. (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
  247. p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5, \
  248. p6##_type gmock_p6) : p0(::std::move(gmock_p0)), \
  249. p1(::std::move(gmock_p1)), p2(::std::move(gmock_p2)), \
  250. p3(::std::move(gmock_p3)), p4(::std::move(gmock_p4)), \
  251. p5(::std::move(gmock_p5)), p6(::std::move(gmock_p6))
  252. #define GMOCK_INTERNAL_INIT_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, p7)\
  253. (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
  254. p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5, \
  255. p6##_type gmock_p6, p7##_type gmock_p7) : p0(::std::move(gmock_p0)), \
  256. p1(::std::move(gmock_p1)), p2(::std::move(gmock_p2)), \
  257. p3(::std::move(gmock_p3)), p4(::std::move(gmock_p4)), \
  258. p5(::std::move(gmock_p5)), p6(::std::move(gmock_p6)), \
  259. p7(::std::move(gmock_p7))
  260. #define GMOCK_INTERNAL_INIT_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  261. p7, p8)\
  262. (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
  263. p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5, \
  264. p6##_type gmock_p6, p7##_type gmock_p7, \
  265. p8##_type gmock_p8) : p0(::std::move(gmock_p0)), \
  266. p1(::std::move(gmock_p1)), p2(::std::move(gmock_p2)), \
  267. p3(::std::move(gmock_p3)), p4(::std::move(gmock_p4)), \
  268. p5(::std::move(gmock_p5)), p6(::std::move(gmock_p6)), \
  269. p7(::std::move(gmock_p7)), p8(::std::move(gmock_p8))
  270. #define GMOCK_INTERNAL_INIT_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  271. p7, p8, p9)\
  272. (p0##_type gmock_p0, p1##_type gmock_p1, p2##_type gmock_p2, \
  273. p3##_type gmock_p3, p4##_type gmock_p4, p5##_type gmock_p5, \
  274. p6##_type gmock_p6, p7##_type gmock_p7, p8##_type gmock_p8, \
  275. p9##_type gmock_p9) : p0(::std::move(gmock_p0)), \
  276. p1(::std::move(gmock_p1)), p2(::std::move(gmock_p2)), \
  277. p3(::std::move(gmock_p3)), p4(::std::move(gmock_p4)), \
  278. p5(::std::move(gmock_p5)), p6(::std::move(gmock_p6)), \
  279. p7(::std::move(gmock_p7)), p8(::std::move(gmock_p8)), \
  280. p9(::std::move(gmock_p9))
  281. // Defines the copy constructor
  282. #define GMOCK_INTERNAL_DEFN_COPY_AND_0_VALUE_PARAMS() \
  283. {} // Avoid https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82134
  284. #define GMOCK_INTERNAL_DEFN_COPY_AND_1_VALUE_PARAMS(...) = default;
  285. #define GMOCK_INTERNAL_DEFN_COPY_AND_2_VALUE_PARAMS(...) = default;
  286. #define GMOCK_INTERNAL_DEFN_COPY_AND_3_VALUE_PARAMS(...) = default;
  287. #define GMOCK_INTERNAL_DEFN_COPY_AND_4_VALUE_PARAMS(...) = default;
  288. #define GMOCK_INTERNAL_DEFN_COPY_AND_5_VALUE_PARAMS(...) = default;
  289. #define GMOCK_INTERNAL_DEFN_COPY_AND_6_VALUE_PARAMS(...) = default;
  290. #define GMOCK_INTERNAL_DEFN_COPY_AND_7_VALUE_PARAMS(...) = default;
  291. #define GMOCK_INTERNAL_DEFN_COPY_AND_8_VALUE_PARAMS(...) = default;
  292. #define GMOCK_INTERNAL_DEFN_COPY_AND_9_VALUE_PARAMS(...) = default;
  293. #define GMOCK_INTERNAL_DEFN_COPY_AND_10_VALUE_PARAMS(...) = default;
  294. // Declares the fields for storing the value parameters.
  295. #define GMOCK_INTERNAL_DEFN_AND_0_VALUE_PARAMS()
  296. #define GMOCK_INTERNAL_DEFN_AND_1_VALUE_PARAMS(p0) p0##_type p0;
  297. #define GMOCK_INTERNAL_DEFN_AND_2_VALUE_PARAMS(p0, p1) p0##_type p0; \
  298. p1##_type p1;
  299. #define GMOCK_INTERNAL_DEFN_AND_3_VALUE_PARAMS(p0, p1, p2) p0##_type p0; \
  300. p1##_type p1; p2##_type p2;
  301. #define GMOCK_INTERNAL_DEFN_AND_4_VALUE_PARAMS(p0, p1, p2, p3) p0##_type p0; \
  302. p1##_type p1; p2##_type p2; p3##_type p3;
  303. #define GMOCK_INTERNAL_DEFN_AND_5_VALUE_PARAMS(p0, p1, p2, p3, \
  304. p4) p0##_type p0; p1##_type p1; p2##_type p2; p3##_type p3; p4##_type p4;
  305. #define GMOCK_INTERNAL_DEFN_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, \
  306. p5) p0##_type p0; p1##_type p1; p2##_type p2; p3##_type p3; p4##_type p4; \
  307. p5##_type p5;
  308. #define GMOCK_INTERNAL_DEFN_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  309. p6) p0##_type p0; p1##_type p1; p2##_type p2; p3##_type p3; p4##_type p4; \
  310. p5##_type p5; p6##_type p6;
  311. #define GMOCK_INTERNAL_DEFN_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  312. p7) p0##_type p0; p1##_type p1; p2##_type p2; p3##_type p3; p4##_type p4; \
  313. p5##_type p5; p6##_type p6; p7##_type p7;
  314. #define GMOCK_INTERNAL_DEFN_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  315. p7, p8) p0##_type p0; p1##_type p1; p2##_type p2; p3##_type p3; \
  316. p4##_type p4; p5##_type p5; p6##_type p6; p7##_type p7; p8##_type p8;
  317. #define GMOCK_INTERNAL_DEFN_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  318. p7, p8, p9) p0##_type p0; p1##_type p1; p2##_type p2; p3##_type p3; \
  319. p4##_type p4; p5##_type p5; p6##_type p6; p7##_type p7; p8##_type p8; \
  320. p9##_type p9;
  321. // Lists the value parameters.
  322. #define GMOCK_INTERNAL_LIST_AND_0_VALUE_PARAMS()
  323. #define GMOCK_INTERNAL_LIST_AND_1_VALUE_PARAMS(p0) p0
  324. #define GMOCK_INTERNAL_LIST_AND_2_VALUE_PARAMS(p0, p1) p0, p1
  325. #define GMOCK_INTERNAL_LIST_AND_3_VALUE_PARAMS(p0, p1, p2) p0, p1, p2
  326. #define GMOCK_INTERNAL_LIST_AND_4_VALUE_PARAMS(p0, p1, p2, p3) p0, p1, p2, p3
  327. #define GMOCK_INTERNAL_LIST_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) p0, p1, \
  328. p2, p3, p4
  329. #define GMOCK_INTERNAL_LIST_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) p0, \
  330. p1, p2, p3, p4, p5
  331. #define GMOCK_INTERNAL_LIST_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  332. p6) p0, p1, p2, p3, p4, p5, p6
  333. #define GMOCK_INTERNAL_LIST_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  334. p7) p0, p1, p2, p3, p4, p5, p6, p7
  335. #define GMOCK_INTERNAL_LIST_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  336. p7, p8) p0, p1, p2, p3, p4, p5, p6, p7, p8
  337. #define GMOCK_INTERNAL_LIST_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  338. p7, p8, p9) p0, p1, p2, p3, p4, p5, p6, p7, p8, p9
  339. // Lists the value parameter types.
  340. #define GMOCK_INTERNAL_LIST_TYPE_AND_0_VALUE_PARAMS()
  341. #define GMOCK_INTERNAL_LIST_TYPE_AND_1_VALUE_PARAMS(p0) , p0##_type
  342. #define GMOCK_INTERNAL_LIST_TYPE_AND_2_VALUE_PARAMS(p0, p1) , p0##_type, \
  343. p1##_type
  344. #define GMOCK_INTERNAL_LIST_TYPE_AND_3_VALUE_PARAMS(p0, p1, p2) , p0##_type, \
  345. p1##_type, p2##_type
  346. #define GMOCK_INTERNAL_LIST_TYPE_AND_4_VALUE_PARAMS(p0, p1, p2, p3) , \
  347. p0##_type, p1##_type, p2##_type, p3##_type
  348. #define GMOCK_INTERNAL_LIST_TYPE_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) , \
  349. p0##_type, p1##_type, p2##_type, p3##_type, p4##_type
  350. #define GMOCK_INTERNAL_LIST_TYPE_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) , \
  351. p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, p5##_type
  352. #define GMOCK_INTERNAL_LIST_TYPE_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  353. p6) , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, p5##_type, \
  354. p6##_type
  355. #define GMOCK_INTERNAL_LIST_TYPE_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  356. p6, p7) , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, \
  357. p5##_type, p6##_type, p7##_type
  358. #define GMOCK_INTERNAL_LIST_TYPE_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  359. p6, p7, p8) , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, \
  360. p5##_type, p6##_type, p7##_type, p8##_type
  361. #define GMOCK_INTERNAL_LIST_TYPE_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  362. p6, p7, p8, p9) , p0##_type, p1##_type, p2##_type, p3##_type, p4##_type, \
  363. p5##_type, p6##_type, p7##_type, p8##_type, p9##_type
  364. // Declares the value parameters.
  365. #define GMOCK_INTERNAL_DECL_AND_0_VALUE_PARAMS()
  366. #define GMOCK_INTERNAL_DECL_AND_1_VALUE_PARAMS(p0) p0##_type p0
  367. #define GMOCK_INTERNAL_DECL_AND_2_VALUE_PARAMS(p0, p1) p0##_type p0, \
  368. p1##_type p1
  369. #define GMOCK_INTERNAL_DECL_AND_3_VALUE_PARAMS(p0, p1, p2) p0##_type p0, \
  370. p1##_type p1, p2##_type p2
  371. #define GMOCK_INTERNAL_DECL_AND_4_VALUE_PARAMS(p0, p1, p2, p3) p0##_type p0, \
  372. p1##_type p1, p2##_type p2, p3##_type p3
  373. #define GMOCK_INTERNAL_DECL_AND_5_VALUE_PARAMS(p0, p1, p2, p3, \
  374. p4) p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4
  375. #define GMOCK_INTERNAL_DECL_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, \
  376. p5) p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4, \
  377. p5##_type p5
  378. #define GMOCK_INTERNAL_DECL_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, \
  379. p6) p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4, \
  380. p5##_type p5, p6##_type p6
  381. #define GMOCK_INTERNAL_DECL_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  382. p7) p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, p4##_type p4, \
  383. p5##_type p5, p6##_type p6, p7##_type p7
  384. #define GMOCK_INTERNAL_DECL_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  385. p7, p8) p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, \
  386. p4##_type p4, p5##_type p5, p6##_type p6, p7##_type p7, p8##_type p8
  387. #define GMOCK_INTERNAL_DECL_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  388. p7, p8, p9) p0##_type p0, p1##_type p1, p2##_type p2, p3##_type p3, \
  389. p4##_type p4, p5##_type p5, p6##_type p6, p7##_type p7, p8##_type p8, \
  390. p9##_type p9
  391. // The suffix of the class template implementing the action template.
  392. #define GMOCK_INTERNAL_COUNT_AND_0_VALUE_PARAMS()
  393. #define GMOCK_INTERNAL_COUNT_AND_1_VALUE_PARAMS(p0) P
  394. #define GMOCK_INTERNAL_COUNT_AND_2_VALUE_PARAMS(p0, p1) P2
  395. #define GMOCK_INTERNAL_COUNT_AND_3_VALUE_PARAMS(p0, p1, p2) P3
  396. #define GMOCK_INTERNAL_COUNT_AND_4_VALUE_PARAMS(p0, p1, p2, p3) P4
  397. #define GMOCK_INTERNAL_COUNT_AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4) P5
  398. #define GMOCK_INTERNAL_COUNT_AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5) P6
  399. #define GMOCK_INTERNAL_COUNT_AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6) P7
  400. #define GMOCK_INTERNAL_COUNT_AND_8_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  401. p7) P8
  402. #define GMOCK_INTERNAL_COUNT_AND_9_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  403. p7, p8) P9
  404. #define GMOCK_INTERNAL_COUNT_AND_10_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6, \
  405. p7, p8, p9) P10
  406. // The name of the class template implementing the action template.
  407. #define GMOCK_ACTION_CLASS_(name, value_params)\
  408. GTEST_CONCAT_TOKEN_(name##Action, GMOCK_INTERNAL_COUNT_##value_params)
  409. #define ACTION_TEMPLATE(name, template_params, value_params) \
  410. template <GMOCK_INTERNAL_DECL_##template_params \
  411. GMOCK_INTERNAL_DECL_TYPE_##value_params> \
  412. class GMOCK_ACTION_CLASS_(name, value_params) { \
  413. public: \
  414. explicit GMOCK_ACTION_CLASS_(name, value_params)( \
  415. GMOCK_INTERNAL_DECL_##value_params) \
  416. GMOCK_PP_IF(GMOCK_PP_IS_EMPTY(GMOCK_INTERNAL_COUNT_##value_params), \
  417. = default; , \
  418. : impl_(std::make_shared<gmock_Impl>( \
  419. GMOCK_INTERNAL_LIST_##value_params)) { }) \
  420. GMOCK_ACTION_CLASS_(name, value_params)( \
  421. const GMOCK_ACTION_CLASS_(name, value_params)&) noexcept \
  422. GMOCK_INTERNAL_DEFN_COPY_##value_params \
  423. GMOCK_ACTION_CLASS_(name, value_params)( \
  424. GMOCK_ACTION_CLASS_(name, value_params)&&) noexcept \
  425. GMOCK_INTERNAL_DEFN_COPY_##value_params \
  426. template <typename F> \
  427. operator ::testing::Action<F>() const { \
  428. return GMOCK_PP_IF( \
  429. GMOCK_PP_IS_EMPTY(GMOCK_INTERNAL_COUNT_##value_params), \
  430. (::testing::internal::MakeAction<F, gmock_Impl>()), \
  431. (::testing::internal::MakeAction<F>(impl_))); \
  432. } \
  433. private: \
  434. class gmock_Impl { \
  435. public: \
  436. explicit gmock_Impl GMOCK_INTERNAL_INIT_##value_params {} \
  437. template <typename function_type, typename return_type, \
  438. typename args_type, GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
  439. return_type gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_) const; \
  440. GMOCK_INTERNAL_DEFN_##value_params \
  441. }; \
  442. GMOCK_PP_IF(GMOCK_PP_IS_EMPTY(GMOCK_INTERNAL_COUNT_##value_params), \
  443. , std::shared_ptr<const gmock_Impl> impl_;) \
  444. }; \
  445. template <GMOCK_INTERNAL_DECL_##template_params \
  446. GMOCK_INTERNAL_DECL_TYPE_##value_params> \
  447. GMOCK_ACTION_CLASS_(name, value_params)< \
  448. GMOCK_INTERNAL_LIST_##template_params \
  449. GMOCK_INTERNAL_LIST_TYPE_##value_params> name( \
  450. GMOCK_INTERNAL_DECL_##value_params) GTEST_MUST_USE_RESULT_; \
  451. template <GMOCK_INTERNAL_DECL_##template_params \
  452. GMOCK_INTERNAL_DECL_TYPE_##value_params> \
  453. inline GMOCK_ACTION_CLASS_(name, value_params)< \
  454. GMOCK_INTERNAL_LIST_##template_params \
  455. GMOCK_INTERNAL_LIST_TYPE_##value_params> name( \
  456. GMOCK_INTERNAL_DECL_##value_params) { \
  457. return GMOCK_ACTION_CLASS_(name, value_params)< \
  458. GMOCK_INTERNAL_LIST_##template_params \
  459. GMOCK_INTERNAL_LIST_TYPE_##value_params>( \
  460. GMOCK_INTERNAL_LIST_##value_params); \
  461. } \
  462. template <GMOCK_INTERNAL_DECL_##template_params \
  463. GMOCK_INTERNAL_DECL_TYPE_##value_params> \
  464. template <typename function_type, typename return_type, typename args_type, \
  465. GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
  466. return_type GMOCK_ACTION_CLASS_(name, value_params)< \
  467. GMOCK_INTERNAL_LIST_##template_params \
  468. GMOCK_INTERNAL_LIST_TYPE_##value_params>::gmock_Impl::gmock_PerformImpl( \
  469. GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
  470. namespace testing {
  471. // The ACTION*() macros trigger warning C4100 (unreferenced formal
  472. // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
  473. // the macro definition, as the warnings are generated when the macro
  474. // is expanded and macro expansion cannot contain #pragma. Therefore
  475. // we suppress them here.
  476. #ifdef _MSC_VER
  477. # pragma warning(push)
  478. # pragma warning(disable:4100)
  479. #endif
  480. namespace internal {
  481. // internal::InvokeArgument - a helper for InvokeArgument action.
  482. // The basic overloads are provided here for generic functors.
  483. // Overloads for other custom-callables are provided in the
  484. // internal/custom/gmock-generated-actions.h header.
  485. template <typename F, typename... Args>
  486. auto InvokeArgument(F f, Args... args) -> decltype(f(args...)) {
  487. return f(args...);
  488. }
  489. template <std::size_t index, typename... Params>
  490. struct InvokeArgumentAction {
  491. template <typename... Args>
  492. auto operator()(Args&&... args) const -> decltype(internal::InvokeArgument(
  493. std::get<index>(std::forward_as_tuple(std::forward<Args>(args)...)),
  494. std::declval<const Params&>()...)) {
  495. internal::FlatTuple<Args&&...> args_tuple(FlatTupleConstructTag{},
  496. std::forward<Args>(args)...);
  497. return params.Apply([&](const Params&... unpacked_params) {
  498. auto&& callable = args_tuple.template Get<index>();
  499. return internal::InvokeArgument(
  500. std::forward<decltype(callable)>(callable), unpacked_params...);
  501. });
  502. }
  503. internal::FlatTuple<Params...> params;
  504. };
  505. } // namespace internal
  506. // The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th
  507. // (0-based) argument, which must be a k-ary callable, of the mock
  508. // function, with arguments a1, a2, ..., a_k.
  509. //
  510. // Notes:
  511. //
  512. // 1. The arguments are passed by value by default. If you need to
  513. // pass an argument by reference, wrap it inside std::ref(). For
  514. // example,
  515. //
  516. // InvokeArgument<1>(5, string("Hello"), std::ref(foo))
  517. //
  518. // passes 5 and string("Hello") by value, and passes foo by
  519. // reference.
  520. //
  521. // 2. If the callable takes an argument by reference but std::ref() is
  522. // not used, it will receive the reference to a copy of the value,
  523. // instead of the original value. For example, when the 0-th
  524. // argument of the mock function takes a const string&, the action
  525. //
  526. // InvokeArgument<0>(string("Hello"))
  527. //
  528. // makes a copy of the temporary string("Hello") object and passes a
  529. // reference of the copy, instead of the original temporary object,
  530. // to the callable. This makes it easy for a user to define an
  531. // InvokeArgument action from temporary values and have it performed
  532. // later.
  533. template <std::size_t index, typename... Params>
  534. internal::InvokeArgumentAction<index, typename std::decay<Params>::type...>
  535. InvokeArgument(Params&&... params) {
  536. return {internal::FlatTuple<typename std::decay<Params>::type...>(
  537. internal::FlatTupleConstructTag{}, std::forward<Params>(params)...)};
  538. }
  539. #ifdef _MSC_VER
  540. # pragma warning(pop)
  541. #endif
  542. } // namespace testing
  543. #endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_