gmock-generated-actions.h.pump 27 KB

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