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