gmock-generated-actions.h.pump 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. //
  36. // Author: wan@google.com (Zhanyong Wan)
  37. // Google Mock - a framework for writing C++ mock classes.
  38. //
  39. // This file implements some commonly used variadic actions.
  40. #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_
  41. #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_
  42. #include "gmock/gmock-actions.h"
  43. #include "gmock/internal/gmock-port.h"
  44. namespace testing {
  45. namespace internal {
  46. // InvokeHelper<F> knows how to unpack an N-tuple and invoke an N-ary
  47. // function or method with the unpacked values, where F is a function
  48. // type that takes N arguments.
  49. template <typename Result, typename ArgumentTuple>
  50. class InvokeHelper;
  51. $range i 0..n
  52. $for i [[
  53. $range j 1..i
  54. $var types = [[$for j [[, typename A$j]]]]
  55. $var as = [[$for j, [[A$j]]]]
  56. $var args = [[$if i==0 [[]] $else [[ args]]]]
  57. $var gets = [[$for j, [[get<$(j - 1)>(args)]]]]
  58. template <typename R$types>
  59. class InvokeHelper<R, ::testing::tuple<$as> > {
  60. public:
  61. template <typename Function>
  62. static R Invoke(Function function, const ::testing::tuple<$as>&$args) {
  63. return function($gets);
  64. }
  65. template <class Class, typename MethodPtr>
  66. static R InvokeMethod(Class* obj_ptr,
  67. MethodPtr method_ptr,
  68. const ::testing::tuple<$as>&$args) {
  69. return (obj_ptr->*method_ptr)($gets);
  70. }
  71. };
  72. ]]
  73. // An INTERNAL macro for extracting the type of a tuple field. It's
  74. // subject to change without notice - DO NOT USE IN USER CODE!
  75. #define GMOCK_FIELD_(Tuple, N) \
  76. typename ::testing::tuple_element<N, Tuple>::type
  77. $range i 1..n
  78. // SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::type is the
  79. // type of an n-ary function whose i-th (1-based) argument type is the
  80. // k{i}-th (0-based) field of ArgumentTuple, which must be a tuple
  81. // type, and whose return type is Result. For example,
  82. // SelectArgs<int, ::testing::tuple<bool, char, double, long>, 0, 3>::type
  83. // is int(bool, long).
  84. //
  85. // SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::Select(args)
  86. // returns the selected fields (k1, k2, ..., k_n) of args as a tuple.
  87. // For example,
  88. // SelectArgs<int, tuple<bool, char, double>, 2, 0>::Select(
  89. // ::testing::make_tuple(true, 'a', 2.5))
  90. // returns tuple (2.5, true).
  91. //
  92. // The numbers in list k1, k2, ..., k_n must be >= 0, where n can be
  93. // in the range [0, $n]. Duplicates are allowed and they don't have
  94. // to be in an ascending or descending order.
  95. template <typename Result, typename ArgumentTuple, $for i, [[int k$i]]>
  96. class SelectArgs {
  97. public:
  98. typedef Result type($for i, [[GMOCK_FIELD_(ArgumentTuple, k$i)]]);
  99. typedef typename Function<type>::ArgumentTuple SelectedArgs;
  100. static SelectedArgs Select(const ArgumentTuple& args) {
  101. return SelectedArgs($for i, [[get<k$i>(args)]]);
  102. }
  103. };
  104. $for i [[
  105. $range j 1..n
  106. $range j1 1..i-1
  107. template <typename Result, typename ArgumentTuple$for j1[[, int k$j1]]>
  108. class SelectArgs<Result, ArgumentTuple,
  109. $for j, [[$if j <= i-1 [[k$j]] $else [[-1]]]]> {
  110. public:
  111. typedef Result type($for j1, [[GMOCK_FIELD_(ArgumentTuple, k$j1)]]);
  112. typedef typename Function<type>::ArgumentTuple SelectedArgs;
  113. static SelectedArgs Select(const ArgumentTuple& [[]]
  114. $if i == 1 [[/* args */]] $else [[args]]) {
  115. return SelectedArgs($for j1, [[get<k$j1>(args)]]);
  116. }
  117. };
  118. ]]
  119. #undef GMOCK_FIELD_
  120. $var ks = [[$for i, [[k$i]]]]
  121. // Implements the WithArgs action.
  122. template <typename InnerAction, $for i, [[int k$i = -1]]>
  123. class WithArgsAction {
  124. public:
  125. explicit WithArgsAction(const InnerAction& action) : action_(action) {}
  126. template <typename F>
  127. operator Action<F>() const { return MakeAction(new Impl<F>(action_)); }
  128. private:
  129. template <typename F>
  130. class Impl : public ActionInterface<F> {
  131. public:
  132. typedef typename Function<F>::Result Result;
  133. typedef typename Function<F>::ArgumentTuple ArgumentTuple;
  134. explicit Impl(const InnerAction& action) : action_(action) {}
  135. virtual Result Perform(const ArgumentTuple& args) {
  136. return action_.Perform(SelectArgs<Result, ArgumentTuple, $ks>::Select(args));
  137. }
  138. private:
  139. typedef typename SelectArgs<Result, ArgumentTuple,
  140. $ks>::type InnerFunctionType;
  141. Action<InnerFunctionType> action_;
  142. };
  143. const InnerAction action_;
  144. GTEST_DISALLOW_ASSIGN_(WithArgsAction);
  145. };
  146. // A macro from the ACTION* family (defined later in this file)
  147. // defines an action that can be used in a mock function. Typically,
  148. // these actions only care about a subset of the arguments of the mock
  149. // function. For example, if such an action only uses the second
  150. // argument, it can be used in any mock function that takes >= 2
  151. // arguments where the type of the second argument is compatible.
  152. //
  153. // Therefore, the action implementation must be prepared to take more
  154. // arguments than it needs. The ExcessiveArg type is used to
  155. // represent those excessive arguments. In order to keep the compiler
  156. // error messages tractable, we define it in the testing namespace
  157. // instead of testing::internal. However, this is an INTERNAL TYPE
  158. // and subject to change without notice, so a user MUST NOT USE THIS
  159. // TYPE DIRECTLY.
  160. struct ExcessiveArg {};
  161. // A helper class needed for implementing the ACTION* macros.
  162. template <typename Result, class Impl>
  163. class ActionHelper {
  164. public:
  165. $range i 0..n
  166. $for i
  167. [[
  168. $var template = [[$if i==0 [[]] $else [[
  169. $range j 0..i-1
  170. template <$for j, [[typename A$j]]>
  171. ]]]]
  172. $range j 0..i-1
  173. $var As = [[$for j, [[A$j]]]]
  174. $var as = [[$for j, [[get<$j>(args)]]]]
  175. $range k 1..n-i
  176. $var eas = [[$for k, [[ExcessiveArg()]]]]
  177. $var arg_list = [[$if (i==0) | (i==n) [[$as$eas]] $else [[$as, $eas]]]]
  178. $template
  179. static Result Perform(Impl* impl, const ::testing::tuple<$As>& args) {
  180. return impl->template gmock_PerformImpl<$As>(args, $arg_list);
  181. }
  182. ]]
  183. };
  184. } // namespace internal
  185. // Various overloads for Invoke().
  186. // WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes
  187. // the selected arguments of the mock function to an_action and
  188. // performs it. It serves as an adaptor between actions with
  189. // different argument lists. C++ doesn't support default arguments for
  190. // function templates, so we have to overload it.
  191. $range i 1..n
  192. $for i [[
  193. $range j 1..i
  194. template <$for j [[int k$j, ]]typename InnerAction>
  195. inline internal::WithArgsAction<InnerAction$for j [[, k$j]]>
  196. WithArgs(const InnerAction& action) {
  197. return internal::WithArgsAction<InnerAction$for j [[, k$j]]>(action);
  198. }
  199. ]]
  200. // Creates an action that does actions a1, a2, ..., sequentially in
  201. // each invocation.
  202. $range i 2..n
  203. $for i [[
  204. $range j 2..i
  205. $var types = [[$for j, [[typename Action$j]]]]
  206. $var Aas = [[$for j [[, Action$j a$j]]]]
  207. template <typename Action1, $types>
  208. $range k 1..i-1
  209. inline $for k [[internal::DoBothAction<Action$k, ]]Action$i$for k [[>]]
  210. DoAll(Action1 a1$Aas) {
  211. $if i==2 [[
  212. return internal::DoBothAction<Action1, Action2>(a1, a2);
  213. ]] $else [[
  214. $range j2 2..i
  215. return DoAll(a1, DoAll($for j2, [[a$j2]]));
  216. ]]
  217. }
  218. ]]
  219. } // namespace testing
  220. // The ACTION* family of macros can be used in a namespace scope to
  221. // define custom actions easily. The syntax:
  222. //
  223. // ACTION(name) { statements; }
  224. //
  225. // will define an action with the given name that executes the
  226. // statements. The value returned by the statements will be used as
  227. // the return value of the action. Inside the statements, you can
  228. // refer to the K-th (0-based) argument of the mock function by
  229. // 'argK', and refer to its type by 'argK_type'. For example:
  230. //
  231. // ACTION(IncrementArg1) {
  232. // arg1_type temp = arg1;
  233. // return ++(*temp);
  234. // }
  235. //
  236. // allows you to write
  237. //
  238. // ...WillOnce(IncrementArg1());
  239. //
  240. // You can also refer to the entire argument tuple and its type by
  241. // 'args' and 'args_type', and refer to the mock function type and its
  242. // return type by 'function_type' and 'return_type'.
  243. //
  244. // Note that you don't need to specify the types of the mock function
  245. // arguments. However rest assured that your code is still type-safe:
  246. // you'll get a compiler error if *arg1 doesn't support the ++
  247. // operator, or if the type of ++(*arg1) isn't compatible with the
  248. // mock function's return type, for example.
  249. //
  250. // Sometimes you'll want to parameterize the action. For that you can use
  251. // another macro:
  252. //
  253. // ACTION_P(name, param_name) { statements; }
  254. //
  255. // For example:
  256. //
  257. // ACTION_P(Add, n) { return arg0 + n; }
  258. //
  259. // will allow you to write:
  260. //
  261. // ...WillOnce(Add(5));
  262. //
  263. // Note that you don't need to provide the type of the parameter
  264. // either. If you need to reference the type of a parameter named
  265. // 'foo', you can write 'foo_type'. For example, in the body of
  266. // ACTION_P(Add, n) above, you can write 'n_type' to refer to the type
  267. // of 'n'.
  268. //
  269. // We also provide ACTION_P2, ACTION_P3, ..., up to ACTION_P$n to support
  270. // multi-parameter actions.
  271. //
  272. // For the purpose of typing, you can view
  273. //
  274. // ACTION_Pk(Foo, p1, ..., pk) { ... }
  275. //
  276. // as shorthand for
  277. //
  278. // template <typename p1_type, ..., typename pk_type>
  279. // FooActionPk<p1_type, ..., pk_type> Foo(p1_type p1, ..., pk_type pk) { ... }
  280. //
  281. // In particular, you can provide the template type arguments
  282. // explicitly when invoking Foo(), as in Foo<long, bool>(5, false);
  283. // although usually you can rely on the compiler to infer the types
  284. // for you automatically. You can assign the result of expression
  285. // Foo(p1, ..., pk) to a variable of type FooActionPk<p1_type, ...,
  286. // pk_type>. This can be useful when composing actions.
  287. //
  288. // You can also overload actions with different numbers of parameters:
  289. //
  290. // ACTION_P(Plus, a) { ... }
  291. // ACTION_P2(Plus, a, b) { ... }
  292. //
  293. // While it's tempting to always use the ACTION* macros when defining
  294. // a new action, you should also consider implementing ActionInterface
  295. // or using MakePolymorphicAction() instead, especially if you need to
  296. // use the action a lot. While these approaches require more work,
  297. // they give you more control on the types of the mock function
  298. // arguments and the action parameters, which in general leads to
  299. // better compiler error messages that pay off in the long run. They
  300. // also allow overloading actions based on parameter types (as opposed
  301. // to just based on the number of parameters).
  302. //
  303. // CAVEAT:
  304. //
  305. // ACTION*() can only be used in a namespace scope. The reason is
  306. // that C++ doesn't yet allow function-local types to be used to
  307. // instantiate templates. The up-coming C++0x standard will fix this.
  308. // Once that's done, we'll consider supporting using ACTION*() inside
  309. // a function.
  310. //
  311. // MORE INFORMATION:
  312. //
  313. // To learn more about using these macros, please search for 'ACTION'
  314. // on http://code.google.com/p/googlemock/wiki/CookBook.
  315. $range i 0..n
  316. $range k 0..n-1
  317. // An internal macro needed for implementing ACTION*().
  318. #define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_\
  319. const args_type& args GTEST_ATTRIBUTE_UNUSED_
  320. $for k [[, \
  321. arg$k[[]]_type arg$k GTEST_ATTRIBUTE_UNUSED_]]
  322. // Sometimes you want to give an action explicit template parameters
  323. // that cannot be inferred from its value parameters. ACTION() and
  324. // ACTION_P*() don't support that. ACTION_TEMPLATE() remedies that
  325. // and can be viewed as an extension to ACTION() and ACTION_P*().
  326. //
  327. // The syntax:
  328. //
  329. // ACTION_TEMPLATE(ActionName,
  330. // HAS_m_TEMPLATE_PARAMS(kind1, name1, ..., kind_m, name_m),
  331. // AND_n_VALUE_PARAMS(p1, ..., p_n)) { statements; }
  332. //
  333. // defines an action template that takes m explicit template
  334. // parameters and n value parameters. name_i is the name of the i-th
  335. // template parameter, and kind_i specifies whether it's a typename,
  336. // an integral constant, or a template. p_i is the name of the i-th
  337. // value parameter.
  338. //
  339. // Example:
  340. //
  341. // // DuplicateArg<k, T>(output) converts the k-th argument of the mock
  342. // // function to type T and copies it to *output.
  343. // ACTION_TEMPLATE(DuplicateArg,
  344. // HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
  345. // AND_1_VALUE_PARAMS(output)) {
  346. // *output = T(::testing::get<k>(args));
  347. // }
  348. // ...
  349. // int n;
  350. // EXPECT_CALL(mock, Foo(_, _))
  351. // .WillOnce(DuplicateArg<1, unsigned char>(&n));
  352. //
  353. // To create an instance of an action template, write:
  354. //
  355. // ActionName<t1, ..., t_m>(v1, ..., v_n)
  356. //
  357. // where the ts are the template arguments and the vs are the value
  358. // arguments. The value argument types are inferred by the compiler.
  359. // If you want to explicitly specify the value argument types, you can
  360. // provide additional template arguments:
  361. //
  362. // ActionName<t1, ..., t_m, u1, ..., u_k>(v1, ..., v_n)
  363. //
  364. // where u_i is the desired type of v_i.
  365. //
  366. // ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded on the
  367. // number of value parameters, but not on the number of template
  368. // parameters. Without the restriction, the meaning of the following
  369. // is unclear:
  370. //
  371. // OverloadedAction<int, bool>(x);
  372. //
  373. // Are we using a single-template-parameter action where 'bool' refers
  374. // to the type of x, or are we using a two-template-parameter action
  375. // where the compiler is asked to infer the type of x?
  376. //
  377. // Implementation notes:
  378. //
  379. // GMOCK_INTERNAL_*_HAS_m_TEMPLATE_PARAMS and
  380. // GMOCK_INTERNAL_*_AND_n_VALUE_PARAMS are internal macros for
  381. // implementing ACTION_TEMPLATE. The main trick we use is to create
  382. // new macro invocations when expanding a macro. For example, we have
  383. //
  384. // #define ACTION_TEMPLATE(name, template_params, value_params)
  385. // ... GMOCK_INTERNAL_DECL_##template_params ...
  386. //
  387. // which causes ACTION_TEMPLATE(..., HAS_1_TEMPLATE_PARAMS(typename, T), ...)
  388. // to expand to
  389. //
  390. // ... GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(typename, T) ...
  391. //
  392. // Since GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS is a macro, the
  393. // preprocessor will continue to expand it to
  394. //
  395. // ... typename T ...
  396. //
  397. // This technique conforms to the C++ standard and is portable. It
  398. // allows us to implement action templates using O(N) code, where N is
  399. // the maximum number of template/value parameters supported. Without
  400. // using it, we'd have to devote O(N^2) amount of code to implement all
  401. // combinations of m and n.
  402. // Declares the template parameters.
  403. $range j 1..n
  404. $for j [[
  405. $range m 0..j-1
  406. #define GMOCK_INTERNAL_DECL_HAS_$j[[]]
  407. _TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[kind$m name$m]]
  408. ]]
  409. // Lists the template parameters.
  410. $for j [[
  411. $range m 0..j-1
  412. #define GMOCK_INTERNAL_LIST_HAS_$j[[]]
  413. _TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[name$m]]
  414. ]]
  415. // Declares the types of value parameters.
  416. $for i [[
  417. $range j 0..i-1
  418. #define GMOCK_INTERNAL_DECL_TYPE_AND_$i[[]]
  419. _VALUE_PARAMS($for j, [[p$j]]) $for j [[, typename p$j##_type]]
  420. ]]
  421. // Initializes the value parameters.
  422. $for i [[
  423. $range j 0..i-1
  424. #define GMOCK_INTERNAL_INIT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])\
  425. ($for j, [[p$j##_type gmock_p$j]])$if i>0 [[ : ]]$for j, [[p$j(gmock_p$j)]]
  426. ]]
  427. // Declares the fields for storing the value parameters.
  428. $for i [[
  429. $range j 0..i-1
  430. #define GMOCK_INTERNAL_DEFN_AND_$i[[]]
  431. _VALUE_PARAMS($for j, [[p$j]]) $for j [[p$j##_type p$j; ]]
  432. ]]
  433. // Lists the value parameters.
  434. $for i [[
  435. $range j 0..i-1
  436. #define GMOCK_INTERNAL_LIST_AND_$i[[]]
  437. _VALUE_PARAMS($for j, [[p$j]]) $for j, [[p$j]]
  438. ]]
  439. // Lists the value parameter types.
  440. $for i [[
  441. $range j 0..i-1
  442. #define GMOCK_INTERNAL_LIST_TYPE_AND_$i[[]]
  443. _VALUE_PARAMS($for j, [[p$j]]) $for j [[, p$j##_type]]
  444. ]]
  445. // Declares the value parameters.
  446. $for i [[
  447. $range j 0..i-1
  448. #define GMOCK_INTERNAL_DECL_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
  449. $for j, [[p$j##_type p$j]]
  450. ]]
  451. // The suffix of the class template implementing the action template.
  452. $for i [[
  453. $range j 0..i-1
  454. #define GMOCK_INTERNAL_COUNT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
  455. $if i==1 [[P]] $elif i>=2 [[P$i]]
  456. ]]
  457. // The name of the class template implementing the action template.
  458. #define GMOCK_ACTION_CLASS_(name, value_params)\
  459. GTEST_CONCAT_TOKEN_(name##Action, GMOCK_INTERNAL_COUNT_##value_params)
  460. $range k 0..n-1
  461. #define ACTION_TEMPLATE(name, template_params, value_params)\
  462. template <GMOCK_INTERNAL_DECL_##template_params\
  463. GMOCK_INTERNAL_DECL_TYPE_##value_params>\
  464. class GMOCK_ACTION_CLASS_(name, value_params) {\
  465. public:\
  466. explicit GMOCK_ACTION_CLASS_(name, value_params)\
  467. GMOCK_INTERNAL_INIT_##value_params {}\
  468. template <typename F>\
  469. class gmock_Impl : public ::testing::ActionInterface<F> {\
  470. public:\
  471. typedef F function_type;\
  472. typedef typename ::testing::internal::Function<F>::Result return_type;\
  473. typedef typename ::testing::internal::Function<F>::ArgumentTuple\
  474. args_type;\
  475. explicit gmock_Impl GMOCK_INTERNAL_INIT_##value_params {}\
  476. virtual return_type Perform(const args_type& args) {\
  477. return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
  478. Perform(this, args);\
  479. }\
  480. template <$for k, [[typename arg$k[[]]_type]]>\
  481. return_type gmock_PerformImpl(const args_type& args[[]]
  482. $for k [[, arg$k[[]]_type arg$k]]) const;\
  483. GMOCK_INTERNAL_DEFN_##value_params\
  484. private:\
  485. GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
  486. };\
  487. template <typename F> operator ::testing::Action<F>() const {\
  488. return ::testing::Action<F>(\
  489. new gmock_Impl<F>(GMOCK_INTERNAL_LIST_##value_params));\
  490. }\
  491. GMOCK_INTERNAL_DEFN_##value_params\
  492. private:\
  493. GTEST_DISALLOW_ASSIGN_(GMOCK_ACTION_CLASS_(name, value_params));\
  494. };\
  495. template <GMOCK_INTERNAL_DECL_##template_params\
  496. GMOCK_INTERNAL_DECL_TYPE_##value_params>\
  497. inline GMOCK_ACTION_CLASS_(name, value_params)<\
  498. GMOCK_INTERNAL_LIST_##template_params\
  499. GMOCK_INTERNAL_LIST_TYPE_##value_params> name(\
  500. GMOCK_INTERNAL_DECL_##value_params) {\
  501. return GMOCK_ACTION_CLASS_(name, value_params)<\
  502. GMOCK_INTERNAL_LIST_##template_params\
  503. GMOCK_INTERNAL_LIST_TYPE_##value_params>(\
  504. GMOCK_INTERNAL_LIST_##value_params);\
  505. }\
  506. template <GMOCK_INTERNAL_DECL_##template_params\
  507. GMOCK_INTERNAL_DECL_TYPE_##value_params>\
  508. template <typename F>\
  509. template <typename arg0_type, typename arg1_type, typename arg2_type, \
  510. typename arg3_type, typename arg4_type, typename arg5_type, \
  511. typename arg6_type, typename arg7_type, typename arg8_type, \
  512. typename arg9_type>\
  513. typename ::testing::internal::Function<F>::Result\
  514. GMOCK_ACTION_CLASS_(name, value_params)<\
  515. GMOCK_INTERNAL_LIST_##template_params\
  516. GMOCK_INTERNAL_LIST_TYPE_##value_params>::gmock_Impl<F>::\
  517. gmock_PerformImpl(\
  518. GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
  519. $for i
  520. [[
  521. $var template = [[$if i==0 [[]] $else [[
  522. $range j 0..i-1
  523. template <$for j, [[typename p$j##_type]]>\
  524. ]]]]
  525. $var class_name = [[name##Action[[$if i==0 [[]] $elif i==1 [[P]]
  526. $else [[P$i]]]]]]
  527. $range j 0..i-1
  528. $var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
  529. $var param_types_and_names = [[$for j, [[p$j##_type p$j]]]]
  530. $var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]]
  531. $var param_field_decls = [[$for j
  532. [[
  533. p$j##_type p$j;\
  534. ]]]]
  535. $var param_field_decls2 = [[$for j
  536. [[
  537. p$j##_type p$j;\
  538. ]]]]
  539. $var params = [[$for j, [[p$j]]]]
  540. $var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]]
  541. $var typename_arg_types = [[$for k, [[typename arg$k[[]]_type]]]]
  542. $var arg_types_and_names = [[$for k, [[arg$k[[]]_type arg$k]]]]
  543. $var macro_name = [[$if i==0 [[ACTION]] $elif i==1 [[ACTION_P]]
  544. $else [[ACTION_P$i]]]]
  545. #define $macro_name(name$for j [[, p$j]])\$template
  546. class $class_name {\
  547. public:\
  548. [[$if i==1 [[explicit ]]]]$class_name($ctor_param_list)$inits {}\
  549. template <typename F>\
  550. class gmock_Impl : public ::testing::ActionInterface<F> {\
  551. public:\
  552. typedef F function_type;\
  553. typedef typename ::testing::internal::Function<F>::Result return_type;\
  554. typedef typename ::testing::internal::Function<F>::ArgumentTuple\
  555. args_type;\
  556. [[$if i==1 [[explicit ]]]]gmock_Impl($ctor_param_list)$inits {}\
  557. virtual return_type Perform(const args_type& args) {\
  558. return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
  559. Perform(this, args);\
  560. }\
  561. template <$typename_arg_types>\
  562. return_type gmock_PerformImpl(const args_type& args, [[]]
  563. $arg_types_and_names) const;\$param_field_decls
  564. private:\
  565. GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
  566. };\
  567. template <typename F> operator ::testing::Action<F>() const {\
  568. return ::testing::Action<F>(new gmock_Impl<F>($params));\
  569. }\$param_field_decls2
  570. private:\
  571. GTEST_DISALLOW_ASSIGN_($class_name);\
  572. };\$template
  573. inline $class_name$param_types name($param_types_and_names) {\
  574. return $class_name$param_types($params);\
  575. }\$template
  576. template <typename F>\
  577. template <$typename_arg_types>\
  578. typename ::testing::internal::Function<F>::Result\
  579. $class_name$param_types::gmock_Impl<F>::gmock_PerformImpl(\
  580. GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
  581. ]]
  582. $$ } // This meta comment fixes auto-indentation in Emacs. It won't
  583. $$ // show up in the generated code.
  584. namespace testing {
  585. // The ACTION*() macros trigger warning C4100 (unreferenced formal
  586. // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
  587. // the macro definition, as the warnings are generated when the macro
  588. // is expanded and macro expansion cannot contain #pragma. Therefore
  589. // we suppress them here.
  590. #ifdef _MSC_VER
  591. # pragma warning(push)
  592. # pragma warning(disable:4100)
  593. #endif
  594. // Various overloads for InvokeArgument<N>().
  595. //
  596. // The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th
  597. // (0-based) argument, which must be a k-ary callable, of the mock
  598. // function, with arguments a1, a2, ..., a_k.
  599. //
  600. // Notes:
  601. //
  602. // 1. The arguments are passed by value by default. If you need to
  603. // pass an argument by reference, wrap it inside ByRef(). For
  604. // example,
  605. //
  606. // InvokeArgument<1>(5, string("Hello"), ByRef(foo))
  607. //
  608. // passes 5 and string("Hello") by value, and passes foo by
  609. // reference.
  610. //
  611. // 2. If the callable takes an argument by reference but ByRef() is
  612. // not used, it will receive the reference to a copy of the value,
  613. // instead of the original value. For example, when the 0-th
  614. // argument of the mock function takes a const string&, the action
  615. //
  616. // InvokeArgument<0>(string("Hello"))
  617. //
  618. // makes a copy of the temporary string("Hello") object and passes a
  619. // reference of the copy, instead of the original temporary object,
  620. // to the callable. This makes it easy for a user to define an
  621. // InvokeArgument action from temporary values and have it performed
  622. // later.
  623. namespace internal {
  624. namespace invoke_argument {
  625. // Appears in InvokeArgumentAdl's argument list to help avoid
  626. // accidental calls to user functions of the same name.
  627. struct AdlTag {};
  628. // InvokeArgumentAdl - a helper for InvokeArgument.
  629. // The basic overloads are provided here for generic functors.
  630. // Overloads for other custom-callables are provided in the
  631. // internal/custom/callback-actions.h header.
  632. $range i 0..n
  633. $for i
  634. [[
  635. $range j 1..i
  636. template <typename R, typename F[[$for j [[, typename A$j]]]]>
  637. R InvokeArgumentAdl(AdlTag, F f[[$for j [[, A$j a$j]]]]) {
  638. return f([[$for j, [[a$j]]]]);
  639. }
  640. ]]
  641. } // namespace invoke_argument
  642. } // namespace internal
  643. $range i 0..n
  644. $for i [[
  645. $range j 0..i-1
  646. ACTION_TEMPLATE(InvokeArgument,
  647. HAS_1_TEMPLATE_PARAMS(int, k),
  648. AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])) {
  649. using internal::invoke_argument::InvokeArgumentAdl;
  650. return InvokeArgumentAdl<return_type>(
  651. internal::invoke_argument::AdlTag(),
  652. ::testing::get<k>(args)$for j [[, p$j]]);
  653. }
  654. ]]
  655. // Various overloads for ReturnNew<T>().
  656. //
  657. // The ReturnNew<T>(a1, a2, ..., a_k) action returns a pointer to a new
  658. // instance of type T, constructed on the heap with constructor arguments
  659. // a1, a2, ..., and a_k. The caller assumes ownership of the returned value.
  660. $range i 0..n
  661. $for i [[
  662. $range j 0..i-1
  663. $var ps = [[$for j, [[p$j]]]]
  664. ACTION_TEMPLATE(ReturnNew,
  665. HAS_1_TEMPLATE_PARAMS(typename, T),
  666. AND_$i[[]]_VALUE_PARAMS($ps)) {
  667. return new T($ps);
  668. }
  669. ]]
  670. #ifdef _MSC_VER
  671. # pragma warning(pop)
  672. #endif
  673. } // namespace testing
  674. // Include any custom callback actions added by the local installation.
  675. // We must include this header at the end to make sure it can use the
  676. // declarations from this file.
  677. #include "gmock/internal/custom/gmock-generated-actions.h"
  678. #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_