gmock-generated-actions.h.pump 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. $$ -*- mode: c++; -*-
  2. $$ This is a Pump source file. Please use Pump to convert it to
  3. $$ gmock-generated-actions.h.
  4. $$
  5. $var n = 10 $$ The maximum arity we support.
  6. $$}} This meta comment fixes auto-indentation in editors.
  7. // Copyright 2007, Google Inc.
  8. // All rights reserved.
  9. //
  10. // Redistribution and use in source and binary forms, with or without
  11. // modification, are permitted provided that the following conditions are
  12. // met:
  13. //
  14. // * Redistributions of source code must retain the above copyright
  15. // notice, this list of conditions and the following disclaimer.
  16. // * Redistributions in binary form must reproduce the above
  17. // copyright notice, this list of conditions and the following disclaimer
  18. // in the documentation and/or other materials provided with the
  19. // distribution.
  20. // * Neither the name of Google Inc. nor the names of its
  21. // contributors may be used to endorse or promote products derived from
  22. // this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. // Google Mock - a framework for writing C++ mock classes.
  36. //
  37. // This file implements some commonly used variadic actions.
  38. // GOOGLETEST_CM0002 DO NOT DELETE
  39. #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_
  40. #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_
  41. #include <memory>
  42. #include <utility>
  43. #include "gmock/gmock-actions.h"
  44. #include "gmock/internal/gmock-port.h"
  45. namespace testing {
  46. namespace internal {
  47. // A macro from the ACTION* family (defined later in this file)
  48. // defines an action that can be used in a mock function. Typically,
  49. // these actions only care about a subset of the arguments of the mock
  50. // function. For example, if such an action only uses the second
  51. // argument, it can be used in any mock function that takes >= 2
  52. // arguments where the type of the second argument is compatible.
  53. //
  54. // Therefore, the action implementation must be prepared to take more
  55. // arguments than it needs. The ExcessiveArg type is used to
  56. // represent those excessive arguments. In order to keep the compiler
  57. // error messages tractable, we define it in the testing namespace
  58. // instead of testing::internal. However, this is an INTERNAL TYPE
  59. // and subject to change without notice, so a user MUST NOT USE THIS
  60. // TYPE DIRECTLY.
  61. struct ExcessiveArg {};
  62. // A helper class needed for implementing the ACTION* macros.
  63. template <typename Result, class Impl>
  64. class ActionHelper {
  65. public:
  66. $range i 0..n
  67. $for i
  68. [[
  69. $var template = [[$if i==0 [[]] $else [[
  70. $range j 0..i-1
  71. template <$for j, [[typename A$j]]>
  72. ]]]]
  73. $range j 0..i-1
  74. $var As = [[$for j, [[A$j]]]]
  75. $var as = [[$for j, [[std::get<$j>(args)]]]]
  76. $range k 1..n-i
  77. $var eas = [[$for k, [[ExcessiveArg()]]]]
  78. $var arg_list = [[$if (i==0) | (i==n) [[$as$eas]] $else [[$as, $eas]]]]
  79. $template
  80. static Result Perform(Impl* impl, const ::std::tuple<$As>& args) {
  81. return impl->template gmock_PerformImpl<$As>(args, $arg_list);
  82. }
  83. ]]
  84. };
  85. } // namespace internal
  86. } // namespace testing
  87. // The ACTION* family of macros can be used in a namespace scope to
  88. // define custom actions easily. The syntax:
  89. //
  90. // ACTION(name) { statements; }
  91. //
  92. // will define an action with the given name that executes the
  93. // statements. The value returned by the statements will be used as
  94. // the return value of the action. Inside the statements, you can
  95. // refer to the K-th (0-based) argument of the mock function by
  96. // 'argK', and refer to its type by 'argK_type'. For example:
  97. //
  98. // ACTION(IncrementArg1) {
  99. // arg1_type temp = arg1;
  100. // return ++(*temp);
  101. // }
  102. //
  103. // allows you to write
  104. //
  105. // ...WillOnce(IncrementArg1());
  106. //
  107. // You can also refer to the entire argument tuple and its type by
  108. // 'args' and 'args_type', and refer to the mock function type and its
  109. // return type by 'function_type' and 'return_type'.
  110. //
  111. // Note that you don't need to specify the types of the mock function
  112. // arguments. However rest assured that your code is still type-safe:
  113. // you'll get a compiler error if *arg1 doesn't support the ++
  114. // operator, or if the type of ++(*arg1) isn't compatible with the
  115. // mock function's return type, for example.
  116. //
  117. // Sometimes you'll want to parameterize the action. For that you can use
  118. // another macro:
  119. //
  120. // ACTION_P(name, param_name) { statements; }
  121. //
  122. // For example:
  123. //
  124. // ACTION_P(Add, n) { return arg0 + n; }
  125. //
  126. // will allow you to write:
  127. //
  128. // ...WillOnce(Add(5));
  129. //
  130. // Note that you don't need to provide the type of the parameter
  131. // either. If you need to reference the type of a parameter named
  132. // 'foo', you can write 'foo_type'. For example, in the body of
  133. // ACTION_P(Add, n) above, you can write 'n_type' to refer to the type
  134. // of 'n'.
  135. //
  136. // We also provide ACTION_P2, ACTION_P3, ..., up to ACTION_P$n to support
  137. // multi-parameter actions.
  138. //
  139. // For the purpose of typing, you can view
  140. //
  141. // ACTION_Pk(Foo, p1, ..., pk) { ... }
  142. //
  143. // as shorthand for
  144. //
  145. // template <typename p1_type, ..., typename pk_type>
  146. // FooActionPk<p1_type, ..., pk_type> Foo(p1_type p1, ..., pk_type pk) { ... }
  147. //
  148. // In particular, you can provide the template type arguments
  149. // explicitly when invoking Foo(), as in Foo<long, bool>(5, false);
  150. // although usually you can rely on the compiler to infer the types
  151. // for you automatically. You can assign the result of expression
  152. // Foo(p1, ..., pk) to a variable of type FooActionPk<p1_type, ...,
  153. // pk_type>. This can be useful when composing actions.
  154. //
  155. // You can also overload actions with different numbers of parameters:
  156. //
  157. // ACTION_P(Plus, a) { ... }
  158. // ACTION_P2(Plus, a, b) { ... }
  159. //
  160. // While it's tempting to always use the ACTION* macros when defining
  161. // a new action, you should also consider implementing ActionInterface
  162. // or using MakePolymorphicAction() instead, especially if you need to
  163. // use the action a lot. While these approaches require more work,
  164. // they give you more control on the types of the mock function
  165. // arguments and the action parameters, which in general leads to
  166. // better compiler error messages that pay off in the long run. They
  167. // also allow overloading actions based on parameter types (as opposed
  168. // to just based on the number of parameters).
  169. //
  170. // CAVEAT:
  171. //
  172. // ACTION*() can only be used in a namespace scope as templates cannot be
  173. // declared inside of a local class.
  174. // Users can, however, define any local functors (e.g. a lambda) that
  175. // can be used as actions.
  176. //
  177. // MORE INFORMATION:
  178. //
  179. // To learn more about using these macros, please search for 'ACTION' on
  180. // https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md
  181. $range i 0..n
  182. $range k 0..n-1
  183. // An internal macro needed for implementing ACTION*().
  184. #define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_\
  185. const args_type& args GTEST_ATTRIBUTE_UNUSED_
  186. $for k [[, \
  187. const arg$k[[]]_type& arg$k GTEST_ATTRIBUTE_UNUSED_]]
  188. // Sometimes you want to give an action explicit template parameters
  189. // that cannot be inferred from its value parameters. ACTION() and
  190. // ACTION_P*() don't support that. ACTION_TEMPLATE() remedies that
  191. // and can be viewed as an extension to ACTION() and ACTION_P*().
  192. //
  193. // The syntax:
  194. //
  195. // ACTION_TEMPLATE(ActionName,
  196. // HAS_m_TEMPLATE_PARAMS(kind1, name1, ..., kind_m, name_m),
  197. // AND_n_VALUE_PARAMS(p1, ..., p_n)) { statements; }
  198. //
  199. // defines an action template that takes m explicit template
  200. // parameters and n value parameters. name_i is the name of the i-th
  201. // template parameter, and kind_i specifies whether it's a typename,
  202. // an integral constant, or a template. p_i is the name of the i-th
  203. // value parameter.
  204. //
  205. // Example:
  206. //
  207. // // DuplicateArg<k, T>(output) converts the k-th argument of the mock
  208. // // function to type T and copies it to *output.
  209. // ACTION_TEMPLATE(DuplicateArg,
  210. // HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
  211. // AND_1_VALUE_PARAMS(output)) {
  212. // *output = T(::std::get<k>(args));
  213. // }
  214. // ...
  215. // int n;
  216. // EXPECT_CALL(mock, Foo(_, _))
  217. // .WillOnce(DuplicateArg<1, unsigned char>(&n));
  218. //
  219. // To create an instance of an action template, write:
  220. //
  221. // ActionName<t1, ..., t_m>(v1, ..., v_n)
  222. //
  223. // where the ts are the template arguments and the vs are the value
  224. // arguments. The value argument types are inferred by the compiler.
  225. // If you want to explicitly specify the value argument types, you can
  226. // provide additional template arguments:
  227. //
  228. // ActionName<t1, ..., t_m, u1, ..., u_k>(v1, ..., v_n)
  229. //
  230. // where u_i is the desired type of v_i.
  231. //
  232. // ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded on the
  233. // number of value parameters, but not on the number of template
  234. // parameters. Without the restriction, the meaning of the following
  235. // is unclear:
  236. //
  237. // OverloadedAction<int, bool>(x);
  238. //
  239. // Are we using a single-template-parameter action where 'bool' refers
  240. // to the type of x, or are we using a two-template-parameter action
  241. // where the compiler is asked to infer the type of x?
  242. //
  243. // Implementation notes:
  244. //
  245. // GMOCK_INTERNAL_*_HAS_m_TEMPLATE_PARAMS and
  246. // GMOCK_INTERNAL_*_AND_n_VALUE_PARAMS are internal macros for
  247. // implementing ACTION_TEMPLATE. The main trick we use is to create
  248. // new macro invocations when expanding a macro. For example, we have
  249. //
  250. // #define ACTION_TEMPLATE(name, template_params, value_params)
  251. // ... GMOCK_INTERNAL_DECL_##template_params ...
  252. //
  253. // which causes ACTION_TEMPLATE(..., HAS_1_TEMPLATE_PARAMS(typename, T), ...)
  254. // to expand to
  255. //
  256. // ... GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(typename, T) ...
  257. //
  258. // Since GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS is a macro, the
  259. // preprocessor will continue to expand it to
  260. //
  261. // ... typename T ...
  262. //
  263. // This technique conforms to the C++ standard and is portable. It
  264. // allows us to implement action templates using O(N) code, where N is
  265. // the maximum number of template/value parameters supported. Without
  266. // using it, we'd have to devote O(N^2) amount of code to implement all
  267. // combinations of m and n.
  268. // Declares the template parameters.
  269. $range j 1..n
  270. $for j [[
  271. $range m 0..j-1
  272. #define GMOCK_INTERNAL_DECL_HAS_$j[[]]
  273. _TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[kind$m name$m]]
  274. ]]
  275. // Lists the template parameters.
  276. $for j [[
  277. $range m 0..j-1
  278. #define GMOCK_INTERNAL_LIST_HAS_$j[[]]
  279. _TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[name$m]]
  280. ]]
  281. // Declares the types of value parameters.
  282. $for i [[
  283. $range j 0..i-1
  284. #define GMOCK_INTERNAL_DECL_TYPE_AND_$i[[]]
  285. _VALUE_PARAMS($for j, [[p$j]]) $for j [[, typename p$j##_type]]
  286. ]]
  287. // Initializes the value parameters.
  288. $for i [[
  289. $range j 0..i-1
  290. #define GMOCK_INTERNAL_INIT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])\
  291. ($for j, [[p$j##_type gmock_p$j]])$if i>0 [[ : ]]$for j, [[p$j(::std::move(gmock_p$j))]]
  292. ]]
  293. // Declares the fields for storing the value parameters.
  294. $for i [[
  295. $range j 0..i-1
  296. #define GMOCK_INTERNAL_DEFN_AND_$i[[]]
  297. _VALUE_PARAMS($for j, [[p$j]]) $for j [[p$j##_type p$j; ]]
  298. ]]
  299. // Lists the value parameters.
  300. $for i [[
  301. $range j 0..i-1
  302. #define GMOCK_INTERNAL_LIST_AND_$i[[]]
  303. _VALUE_PARAMS($for j, [[p$j]]) $for j, [[p$j]]
  304. ]]
  305. // Lists the value parameter types.
  306. $for i [[
  307. $range j 0..i-1
  308. #define GMOCK_INTERNAL_LIST_TYPE_AND_$i[[]]
  309. _VALUE_PARAMS($for j, [[p$j]]) $for j [[, p$j##_type]]
  310. ]]
  311. // Declares the value parameters.
  312. $for i [[
  313. $range j 0..i-1
  314. #define GMOCK_INTERNAL_DECL_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
  315. $for j, [[p$j##_type p$j]]
  316. ]]
  317. // The suffix of the class template implementing the action template.
  318. $for i [[
  319. $range j 0..i-1
  320. #define GMOCK_INTERNAL_COUNT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
  321. $if i==1 [[P]] $elif i>=2 [[P$i]]
  322. ]]
  323. // The name of the class template implementing the action template.
  324. #define GMOCK_ACTION_CLASS_(name, value_params)\
  325. GTEST_CONCAT_TOKEN_(name##Action, GMOCK_INTERNAL_COUNT_##value_params)
  326. $range k 0..n-1
  327. #define ACTION_TEMPLATE(name, template_params, value_params)\
  328. template <GMOCK_INTERNAL_DECL_##template_params\
  329. GMOCK_INTERNAL_DECL_TYPE_##value_params>\
  330. class GMOCK_ACTION_CLASS_(name, value_params) {\
  331. public:\
  332. explicit GMOCK_ACTION_CLASS_(name, value_params)\
  333. GMOCK_INTERNAL_INIT_##value_params {}\
  334. template <typename F>\
  335. class gmock_Impl : public ::testing::ActionInterface<F> {\
  336. public:\
  337. typedef F function_type;\
  338. typedef typename ::testing::internal::Function<F>::Result return_type;\
  339. typedef typename ::testing::internal::Function<F>::ArgumentTuple\
  340. args_type;\
  341. explicit gmock_Impl GMOCK_INTERNAL_INIT_##value_params {}\
  342. virtual return_type Perform(const args_type& args) {\
  343. return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
  344. Perform(this, args);\
  345. }\
  346. template <$for k, [[typename arg$k[[]]_type]]>\
  347. return_type gmock_PerformImpl(const args_type& args[[]]
  348. $for k [[, const arg$k[[]]_type& arg$k]]) const;\
  349. GMOCK_INTERNAL_DEFN_##value_params\
  350. private:\
  351. GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
  352. };\
  353. template <typename F> operator ::testing::Action<F>() const {\
  354. return ::testing::Action<F>(\
  355. new gmock_Impl<F>(GMOCK_INTERNAL_LIST_##value_params));\
  356. }\
  357. GMOCK_INTERNAL_DEFN_##value_params\
  358. private:\
  359. GTEST_DISALLOW_ASSIGN_(GMOCK_ACTION_CLASS_(name, value_params));\
  360. };\
  361. template <GMOCK_INTERNAL_DECL_##template_params\
  362. GMOCK_INTERNAL_DECL_TYPE_##value_params>\
  363. inline GMOCK_ACTION_CLASS_(name, value_params)<\
  364. GMOCK_INTERNAL_LIST_##template_params\
  365. GMOCK_INTERNAL_LIST_TYPE_##value_params> name(\
  366. GMOCK_INTERNAL_DECL_##value_params) {\
  367. return GMOCK_ACTION_CLASS_(name, value_params)<\
  368. GMOCK_INTERNAL_LIST_##template_params\
  369. GMOCK_INTERNAL_LIST_TYPE_##value_params>(\
  370. GMOCK_INTERNAL_LIST_##value_params);\
  371. }\
  372. template <GMOCK_INTERNAL_DECL_##template_params\
  373. GMOCK_INTERNAL_DECL_TYPE_##value_params>\
  374. template <typename F>\
  375. template <typename arg0_type, typename arg1_type, typename arg2_type, \
  376. typename arg3_type, typename arg4_type, typename arg5_type, \
  377. typename arg6_type, typename arg7_type, typename arg8_type, \
  378. typename arg9_type>\
  379. typename ::testing::internal::Function<F>::Result\
  380. GMOCK_ACTION_CLASS_(name, value_params)<\
  381. GMOCK_INTERNAL_LIST_##template_params\
  382. GMOCK_INTERNAL_LIST_TYPE_##value_params>::gmock_Impl<F>::\
  383. gmock_PerformImpl(\
  384. GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
  385. $for i
  386. [[
  387. $var template = [[$if i==0 [[]] $else [[
  388. $range j 0..i-1
  389. template <$for j, [[typename p$j##_type]]>\
  390. ]]]]
  391. $var class_name = [[name##Action[[$if i==0 [[]] $elif i==1 [[P]]
  392. $else [[P$i]]]]]]
  393. $range j 0..i-1
  394. $var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
  395. $var param_types_and_names = [[$for j, [[p$j##_type p$j]]]]
  396. $var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(::std::forward<p$j##_type>(gmock_p$j))]]]]]]
  397. $var param_field_decls = [[$for j
  398. [[
  399. p$j##_type p$j;\
  400. ]]]]
  401. $var param_field_decls2 = [[$for j
  402. [[
  403. p$j##_type p$j;\
  404. ]]]]
  405. $var params = [[$for j, [[p$j]]]]
  406. $var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]]
  407. $var typename_arg_types = [[$for k, [[typename arg$k[[]]_type]]]]
  408. $var arg_types_and_names = [[$for k, [[const arg$k[[]]_type& arg$k]]]]
  409. $var macro_name = [[$if i==0 [[ACTION]] $elif i==1 [[ACTION_P]]
  410. $else [[ACTION_P$i]]]]
  411. #define $macro_name(name$for j [[, p$j]])\$template
  412. class $class_name {\
  413. public:\
  414. [[$if i==1 [[explicit ]]]]$class_name($ctor_param_list)$inits {}\
  415. template <typename F>\
  416. class gmock_Impl : public ::testing::ActionInterface<F> {\
  417. public:\
  418. typedef F function_type;\
  419. typedef typename ::testing::internal::Function<F>::Result return_type;\
  420. typedef typename ::testing::internal::Function<F>::ArgumentTuple\
  421. args_type;\
  422. [[$if i==1 [[explicit ]]]]gmock_Impl($ctor_param_list)$inits {}\
  423. virtual return_type Perform(const args_type& args) {\
  424. return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
  425. Perform(this, args);\
  426. }\
  427. template <$typename_arg_types>\
  428. return_type gmock_PerformImpl(const args_type& args, [[]]
  429. $arg_types_and_names) const;\$param_field_decls
  430. private:\
  431. GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
  432. };\
  433. template <typename F> operator ::testing::Action<F>() const {\
  434. return ::testing::Action<F>(new gmock_Impl<F>($params));\
  435. }\$param_field_decls2
  436. private:\
  437. GTEST_DISALLOW_ASSIGN_($class_name);\
  438. };\$template
  439. inline $class_name$param_types name($param_types_and_names) {\
  440. return $class_name$param_types($params);\
  441. }\$template
  442. template <typename F>\
  443. template <$typename_arg_types>\
  444. typename ::testing::internal::Function<F>::Result\
  445. $class_name$param_types::gmock_Impl<F>::gmock_PerformImpl(\
  446. GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
  447. ]]
  448. $$ } // This meta comment fixes auto-indentation in Emacs. It won't
  449. $$ // show up in the generated code.
  450. namespace testing {
  451. // The ACTION*() macros trigger warning C4100 (unreferenced formal
  452. // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
  453. // the macro definition, as the warnings are generated when the macro
  454. // is expanded and macro expansion cannot contain #pragma. Therefore
  455. // we suppress them here.
  456. #ifdef _MSC_VER
  457. # pragma warning(push)
  458. # pragma warning(disable:4100)
  459. #endif
  460. // Various overloads for InvokeArgument<N>().
  461. //
  462. // The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th
  463. // (0-based) argument, which must be a k-ary callable, of the mock
  464. // function, with arguments a1, a2, ..., a_k.
  465. //
  466. // Notes:
  467. //
  468. // 1. The arguments are passed by value by default. If you need to
  469. // pass an argument by reference, wrap it inside ByRef(). For
  470. // example,
  471. //
  472. // InvokeArgument<1>(5, string("Hello"), ByRef(foo))
  473. //
  474. // passes 5 and string("Hello") by value, and passes foo by
  475. // reference.
  476. //
  477. // 2. If the callable takes an argument by reference but ByRef() is
  478. // not used, it will receive the reference to a copy of the value,
  479. // instead of the original value. For example, when the 0-th
  480. // argument of the mock function takes a const string&, the action
  481. //
  482. // InvokeArgument<0>(string("Hello"))
  483. //
  484. // makes a copy of the temporary string("Hello") object and passes a
  485. // reference of the copy, instead of the original temporary object,
  486. // to the callable. This makes it easy for a user to define an
  487. // InvokeArgument action from temporary values and have it performed
  488. // later.
  489. namespace internal {
  490. namespace invoke_argument {
  491. // Appears in InvokeArgumentAdl's argument list to help avoid
  492. // accidental calls to user functions of the same name.
  493. struct AdlTag {};
  494. // InvokeArgumentAdl - a helper for InvokeArgument.
  495. // The basic overloads are provided here for generic functors.
  496. // Overloads for other custom-callables are provided in the
  497. // internal/custom/callback-actions.h header.
  498. $range i 0..n
  499. $for i
  500. [[
  501. $range j 1..i
  502. template <typename R, typename F[[$for j [[, typename A$j]]]]>
  503. R InvokeArgumentAdl(AdlTag, F f[[$for j [[, A$j a$j]]]]) {
  504. return f([[$for j, [[a$j]]]]);
  505. }
  506. ]]
  507. } // namespace invoke_argument
  508. } // namespace internal
  509. $range i 0..n
  510. $for i [[
  511. $range j 0..i-1
  512. ACTION_TEMPLATE(InvokeArgument,
  513. HAS_1_TEMPLATE_PARAMS(int, k),
  514. AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])) {
  515. using internal::invoke_argument::InvokeArgumentAdl;
  516. return InvokeArgumentAdl<return_type>(
  517. internal::invoke_argument::AdlTag(),
  518. ::std::get<k>(args)$for j [[, p$j]]);
  519. }
  520. ]]
  521. // Various overloads for ReturnNew<T>().
  522. //
  523. // The ReturnNew<T>(a1, a2, ..., a_k) action returns a pointer to a new
  524. // instance of type T, constructed on the heap with constructor arguments
  525. // a1, a2, ..., and a_k. The caller assumes ownership of the returned value.
  526. $range i 0..n
  527. $for i [[
  528. $range j 0..i-1
  529. $var ps = [[$for j, [[p$j]]]]
  530. ACTION_TEMPLATE(ReturnNew,
  531. HAS_1_TEMPLATE_PARAMS(typename, T),
  532. AND_$i[[]]_VALUE_PARAMS($ps)) {
  533. return new T($ps);
  534. }
  535. ]]
  536. #ifdef _MSC_VER
  537. # pragma warning(pop)
  538. #endif
  539. } // namespace testing
  540. // Include any custom callback actions added by the local installation.
  541. // We must include this header at the end to make sure it can use the
  542. // declarations from this file.
  543. #include "gmock/internal/custom/gmock-generated-actions.h"
  544. #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_