gmock-generated-actions.h.pump 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. $range i 0..n
  46. $range k 0..n-1
  47. // Sometimes you want to give an action explicit template parameters
  48. // that cannot be inferred from its value parameters. ACTION() and
  49. // ACTION_P*() don't support that. ACTION_TEMPLATE() remedies that
  50. // and can be viewed as an extension to ACTION() and ACTION_P*().
  51. //
  52. // The syntax:
  53. //
  54. // ACTION_TEMPLATE(ActionName,
  55. // HAS_m_TEMPLATE_PARAMS(kind1, name1, ..., kind_m, name_m),
  56. // AND_n_VALUE_PARAMS(p1, ..., p_n)) { statements; }
  57. //
  58. // defines an action template that takes m explicit template
  59. // parameters and n value parameters. name_i is the name of the i-th
  60. // template parameter, and kind_i specifies whether it's a typename,
  61. // an integral constant, or a template. p_i is the name of the i-th
  62. // value parameter.
  63. //
  64. // Example:
  65. //
  66. // // DuplicateArg<k, T>(output) converts the k-th argument of the mock
  67. // // function to type T and copies it to *output.
  68. // ACTION_TEMPLATE(DuplicateArg,
  69. // HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
  70. // AND_1_VALUE_PARAMS(output)) {
  71. // *output = T(::std::get<k>(args));
  72. // }
  73. // ...
  74. // int n;
  75. // EXPECT_CALL(mock, Foo(_, _))
  76. // .WillOnce(DuplicateArg<1, unsigned char>(&n));
  77. //
  78. // To create an instance of an action template, write:
  79. //
  80. // ActionName<t1, ..., t_m>(v1, ..., v_n)
  81. //
  82. // where the ts are the template arguments and the vs are the value
  83. // arguments. The value argument types are inferred by the compiler.
  84. // If you want to explicitly specify the value argument types, you can
  85. // provide additional template arguments:
  86. //
  87. // ActionName<t1, ..., t_m, u1, ..., u_k>(v1, ..., v_n)
  88. //
  89. // where u_i is the desired type of v_i.
  90. //
  91. // ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded on the
  92. // number of value parameters, but not on the number of template
  93. // parameters. Without the restriction, the meaning of the following
  94. // is unclear:
  95. //
  96. // OverloadedAction<int, bool>(x);
  97. //
  98. // Are we using a single-template-parameter action where 'bool' refers
  99. // to the type of x, or are we using a two-template-parameter action
  100. // where the compiler is asked to infer the type of x?
  101. //
  102. // Implementation notes:
  103. //
  104. // GMOCK_INTERNAL_*_HAS_m_TEMPLATE_PARAMS and
  105. // GMOCK_INTERNAL_*_AND_n_VALUE_PARAMS are internal macros for
  106. // implementing ACTION_TEMPLATE. The main trick we use is to create
  107. // new macro invocations when expanding a macro. For example, we have
  108. //
  109. // #define ACTION_TEMPLATE(name, template_params, value_params)
  110. // ... GMOCK_INTERNAL_DECL_##template_params ...
  111. //
  112. // which causes ACTION_TEMPLATE(..., HAS_1_TEMPLATE_PARAMS(typename, T), ...)
  113. // to expand to
  114. //
  115. // ... GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(typename, T) ...
  116. //
  117. // Since GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS is a macro, the
  118. // preprocessor will continue to expand it to
  119. //
  120. // ... typename T ...
  121. //
  122. // This technique conforms to the C++ standard and is portable. It
  123. // allows us to implement action templates using O(N) code, where N is
  124. // the maximum number of template/value parameters supported. Without
  125. // using it, we'd have to devote O(N^2) amount of code to implement all
  126. // combinations of m and n.
  127. // Declares the template parameters.
  128. $range j 1..n
  129. $for j [[
  130. $range m 0..j-1
  131. #define GMOCK_INTERNAL_DECL_HAS_$j[[]]
  132. _TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[kind$m name$m]]
  133. ]]
  134. // Lists the template parameters.
  135. $for j [[
  136. $range m 0..j-1
  137. #define GMOCK_INTERNAL_LIST_HAS_$j[[]]
  138. _TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[name$m]]
  139. ]]
  140. // Declares the types of value parameters.
  141. $for i [[
  142. $range j 0..i-1
  143. #define GMOCK_INTERNAL_DECL_TYPE_AND_$i[[]]
  144. _VALUE_PARAMS($for j, [[p$j]]) $for j [[, typename p$j##_type]]
  145. ]]
  146. // Initializes the value parameters.
  147. $for i [[
  148. $range j 0..i-1
  149. #define GMOCK_INTERNAL_INIT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])\
  150. ($for j, [[p$j##_type gmock_p$j]])$if i>0 [[ : ]]$for j, [[p$j(::std::move(gmock_p$j))]]
  151. ]]
  152. // Declares the fields for storing the value parameters.
  153. $for i [[
  154. $range j 0..i-1
  155. #define GMOCK_INTERNAL_DEFN_AND_$i[[]]
  156. _VALUE_PARAMS($for j, [[p$j]]) $for j [[p$j##_type p$j; ]]
  157. ]]
  158. // Lists the value parameters.
  159. $for i [[
  160. $range j 0..i-1
  161. #define GMOCK_INTERNAL_LIST_AND_$i[[]]
  162. _VALUE_PARAMS($for j, [[p$j]]) $for j, [[p$j]]
  163. ]]
  164. // Lists the value parameter types.
  165. $for i [[
  166. $range j 0..i-1
  167. #define GMOCK_INTERNAL_LIST_TYPE_AND_$i[[]]
  168. _VALUE_PARAMS($for j, [[p$j]]) $for j [[, p$j##_type]]
  169. ]]
  170. // Declares the value parameters.
  171. $for i [[
  172. $range j 0..i-1
  173. #define GMOCK_INTERNAL_DECL_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
  174. $for j, [[p$j##_type p$j]]
  175. ]]
  176. // The suffix of the class template implementing the action template.
  177. $for i [[
  178. $range j 0..i-1
  179. #define GMOCK_INTERNAL_COUNT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
  180. $if i==1 [[P]] $elif i>=2 [[P$i]]
  181. ]]
  182. // The name of the class template implementing the action template.
  183. #define GMOCK_ACTION_CLASS_(name, value_params)\
  184. GTEST_CONCAT_TOKEN_(name##Action, GMOCK_INTERNAL_COUNT_##value_params)
  185. $range k 0..n-1
  186. #define ACTION_TEMPLATE(name, template_params, value_params)\
  187. template <GMOCK_INTERNAL_DECL_##template_params\
  188. GMOCK_INTERNAL_DECL_TYPE_##value_params>\
  189. class GMOCK_ACTION_CLASS_(name, value_params) {\
  190. public:\
  191. explicit GMOCK_ACTION_CLASS_(name, value_params)\
  192. GMOCK_INTERNAL_INIT_##value_params {}\
  193. template <typename F>\
  194. class gmock_Impl : public ::testing::ActionInterface<F> {\
  195. public:\
  196. typedef F function_type;\
  197. typedef typename ::testing::internal::Function<F>::Result return_type;\
  198. typedef typename ::testing::internal::Function<F>::ArgumentTuple\
  199. args_type;\
  200. explicit gmock_Impl GMOCK_INTERNAL_INIT_##value_params {}\
  201. return_type Perform(const args_type& args) override {\
  202. return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
  203. Perform(this, args);\
  204. }\
  205. template <GMOCK_ACTION_TEMPLATE_ARGS_NAMES_>\
  206. return_type gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_) const;\
  207. GMOCK_INTERNAL_DEFN_##value_params\
  208. };\
  209. template <typename F> operator ::testing::Action<F>() const {\
  210. return ::testing::Action<F>(\
  211. new gmock_Impl<F>(GMOCK_INTERNAL_LIST_##value_params));\
  212. }\
  213. GMOCK_INTERNAL_DEFN_##value_params\
  214. };\
  215. template <GMOCK_INTERNAL_DECL_##template_params\
  216. GMOCK_INTERNAL_DECL_TYPE_##value_params>\
  217. inline GMOCK_ACTION_CLASS_(name, value_params)<\
  218. GMOCK_INTERNAL_LIST_##template_params\
  219. GMOCK_INTERNAL_LIST_TYPE_##value_params> name(\
  220. GMOCK_INTERNAL_DECL_##value_params) {\
  221. return GMOCK_ACTION_CLASS_(name, value_params)<\
  222. GMOCK_INTERNAL_LIST_##template_params\
  223. GMOCK_INTERNAL_LIST_TYPE_##value_params>(\
  224. GMOCK_INTERNAL_LIST_##value_params);\
  225. }\
  226. template <GMOCK_INTERNAL_DECL_##template_params\
  227. GMOCK_INTERNAL_DECL_TYPE_##value_params>\
  228. template <typename F>\
  229. template <GMOCK_ACTION_TEMPLATE_ARGS_NAMES_>\
  230. typename ::testing::internal::Function<F>::Result\
  231. GMOCK_ACTION_CLASS_(name, value_params)<\
  232. GMOCK_INTERNAL_LIST_##template_params\
  233. GMOCK_INTERNAL_LIST_TYPE_##value_params>::gmock_Impl<F>::\
  234. gmock_PerformImpl(\
  235. GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
  236. namespace testing {
  237. // The ACTION*() macros trigger warning C4100 (unreferenced formal
  238. // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
  239. // the macro definition, as the warnings are generated when the macro
  240. // is expanded and macro expansion cannot contain #pragma. Therefore
  241. // we suppress them here.
  242. #ifdef _MSC_VER
  243. # pragma warning(push)
  244. # pragma warning(disable:4100)
  245. #endif
  246. // Various overloads for InvokeArgument<N>().
  247. //
  248. // The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th
  249. // (0-based) argument, which must be a k-ary callable, of the mock
  250. // function, with arguments a1, a2, ..., a_k.
  251. //
  252. // Notes:
  253. //
  254. // 1. The arguments are passed by value by default. If you need to
  255. // pass an argument by reference, wrap it inside ByRef(). For
  256. // example,
  257. //
  258. // InvokeArgument<1>(5, string("Hello"), ByRef(foo))
  259. //
  260. // passes 5 and string("Hello") by value, and passes foo by
  261. // reference.
  262. //
  263. // 2. If the callable takes an argument by reference but ByRef() is
  264. // not used, it will receive the reference to a copy of the value,
  265. // instead of the original value. For example, when the 0-th
  266. // argument of the mock function takes a const string&, the action
  267. //
  268. // InvokeArgument<0>(string("Hello"))
  269. //
  270. // makes a copy of the temporary string("Hello") object and passes a
  271. // reference of the copy, instead of the original temporary object,
  272. // to the callable. This makes it easy for a user to define an
  273. // InvokeArgument action from temporary values and have it performed
  274. // later.
  275. $range i 0..n
  276. $for i [[
  277. $range j 0..i-1
  278. ACTION_TEMPLATE(InvokeArgument,
  279. HAS_1_TEMPLATE_PARAMS(int, k),
  280. AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])) {
  281. using internal::invoke_argument::InvokeArgumentAdl;
  282. return InvokeArgumentAdl(internal::invoke_argument::AdlTag(),
  283. ::std::get<k>(args)$for j[[, p$j]]);
  284. }
  285. ]]
  286. #ifdef _MSC_VER
  287. # pragma warning(pop)
  288. #endif
  289. } // namespace testing
  290. // Include any custom callback actions added by the local installation.
  291. // We must include this header at the end to make sure it can use the
  292. // declarations from this file.
  293. #include "gmock/internal/custom/gmock-generated-actions.h"
  294. #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_