blob: 9197e8733f93af3e4625286f9faf8149119b38bf [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001$$ -*- mode: c++; -*-
2$$ This is a Pump source file. Please use Pump to convert it to
zhanyong.wan1afe1c72009-07-21 23:26:31 +00003$$ gmock-generated-actions.h.
shiqiane35fdd92008-12-10 05:08:54 +00004$$
5$var n = 10 $$ The maximum arity we support.
zhanyong.wan18490652009-05-11 18:54:08 +00006$$}} This meta comment fixes auto-indentation in editors.
shiqiane35fdd92008-12-10 05:08:54 +00007// 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
38// Google Mock - a framework for writing C++ mock classes.
39//
40// This file implements some commonly used variadic actions.
41
42#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_
43#define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_
44
zhanyong.wan53e08c42010-09-14 05:38:21 +000045#include "gmock/gmock-actions.h"
46#include "gmock/internal/gmock-port.h"
shiqiane35fdd92008-12-10 05:08:54 +000047
48namespace testing {
49namespace internal {
50
51// InvokeHelper<F> knows how to unpack an N-tuple and invoke an N-ary
52// function or method with the unpacked values, where F is a function
53// type that takes N arguments.
54template <typename Result, typename ArgumentTuple>
55class InvokeHelper;
56
57
58$range i 0..n
59$for i [[
60$range j 1..i
61$var types = [[$for j [[, typename A$j]]]]
62$var as = [[$for j, [[A$j]]]]
63$var args = [[$if i==0 [[]] $else [[ args]]]]
shiqiane35fdd92008-12-10 05:08:54 +000064$var gets = [[$for j, [[get<$(j - 1)>(args)]]]]
65template <typename R$types>
kosakbd018832014-04-02 20:30:00 +000066class InvokeHelper<R, ::testing::tuple<$as> > {
shiqiane35fdd92008-12-10 05:08:54 +000067 public:
68 template <typename Function>
kosakbd018832014-04-02 20:30:00 +000069 static R Invoke(Function function, const ::testing::tuple<$as>&$args) {
70 return function($gets);
shiqiane35fdd92008-12-10 05:08:54 +000071 }
72
73 template <class Class, typename MethodPtr>
74 static R InvokeMethod(Class* obj_ptr,
75 MethodPtr method_ptr,
kosakbd018832014-04-02 20:30:00 +000076 const ::testing::tuple<$as>&$args) {
77 return (obj_ptr->*method_ptr)($gets);
shiqiane35fdd92008-12-10 05:08:54 +000078 }
79};
80
81
82]]
shiqiane35fdd92008-12-10 05:08:54 +000083// CallableHelper has static methods for invoking "callables",
84// i.e. function pointers and functors. It uses overloading to
85// provide a uniform interface for invoking different kinds of
86// callables. In particular, you can use:
87//
88// CallableHelper<R>::Call(callable, a1, a2, ..., an)
89//
90// to invoke an n-ary callable, where R is its return type. If an
91// argument, say a2, needs to be passed by reference, you should write
92// ByRef(a2) instead of a2 in the above expression.
93template <typename R>
94class CallableHelper {
95 public:
96 // Calls a nullary callable.
97 template <typename Function>
98 static R Call(Function function) { return function(); }
99
100 // Calls a unary callable.
101
102 // We deliberately pass a1 by value instead of const reference here
103 // in case it is a C-string literal. If we had declared the
104 // parameter as 'const A1& a1' and write Call(function, "Hi"), the
105 // compiler would've thought A1 is 'char[3]', which causes trouble
106 // when you need to copy a value of type A1. By declaring the
107 // parameter as 'A1 a1', the compiler will correctly infer that A1
108 // is 'const char*' when it sees Call(function, "Hi").
109 //
110 // Since this function is defined inline, the compiler can get rid
111 // of the copying of the arguments. Therefore the performance won't
112 // be hurt.
113 template <typename Function, typename A1>
114 static R Call(Function function, A1 a1) { return function(a1); }
115
116$range i 2..n
117$for i
118[[
119$var arity = [[$if i==2 [[binary]] $elif i==3 [[ternary]] $else [[$i-ary]]]]
120
121 // Calls a $arity callable.
122
123$range j 1..i
124$var typename_As = [[$for j, [[typename A$j]]]]
125$var Aas = [[$for j, [[A$j a$j]]]]
126$var as = [[$for j, [[a$j]]]]
127$var typename_Ts = [[$for j, [[typename T$j]]]]
128$var Ts = [[$for j, [[T$j]]]]
129 template <typename Function, $typename_As>
130 static R Call(Function function, $Aas) {
131 return function($as);
132 }
133
134]]
shiqiane35fdd92008-12-10 05:08:54 +0000135}; // class CallableHelper
136
shiqiane35fdd92008-12-10 05:08:54 +0000137// An INTERNAL macro for extracting the type of a tuple field. It's
138// subject to change without notice - DO NOT USE IN USER CODE!
zhanyong.wane0d051e2009-02-19 00:33:37 +0000139#define GMOCK_FIELD_(Tuple, N) \
kosakbd018832014-04-02 20:30:00 +0000140 typename ::testing::tuple_element<N, Tuple>::type
shiqiane35fdd92008-12-10 05:08:54 +0000141
142$range i 1..n
143
144// SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::type is the
145// type of an n-ary function whose i-th (1-based) argument type is the
146// k{i}-th (0-based) field of ArgumentTuple, which must be a tuple
147// type, and whose return type is Result. For example,
kosakbd018832014-04-02 20:30:00 +0000148// SelectArgs<int, ::testing::tuple<bool, char, double, long>, 0, 3>::type
shiqiane35fdd92008-12-10 05:08:54 +0000149// is int(bool, long).
150//
151// SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::Select(args)
152// returns the selected fields (k1, k2, ..., k_n) of args as a tuple.
153// For example,
kosakbd018832014-04-02 20:30:00 +0000154// SelectArgs<int, tuple<bool, char, double>, 2, 0>::Select(
155// ::testing::make_tuple(true, 'a', 2.5))
156// returns tuple (2.5, true).
shiqiane35fdd92008-12-10 05:08:54 +0000157//
158// The numbers in list k1, k2, ..., k_n must be >= 0, where n can be
159// in the range [0, $n]. Duplicates are allowed and they don't have
160// to be in an ascending or descending order.
161
162template <typename Result, typename ArgumentTuple, $for i, [[int k$i]]>
163class SelectArgs {
164 public:
zhanyong.wane0d051e2009-02-19 00:33:37 +0000165 typedef Result type($for i, [[GMOCK_FIELD_(ArgumentTuple, k$i)]]);
shiqiane35fdd92008-12-10 05:08:54 +0000166 typedef typename Function<type>::ArgumentTuple SelectedArgs;
167 static SelectedArgs Select(const ArgumentTuple& args) {
shiqiane35fdd92008-12-10 05:08:54 +0000168 return SelectedArgs($for i, [[get<k$i>(args)]]);
169 }
170};
171
172
173$for i [[
174$range j 1..n
175$range j1 1..i-1
176template <typename Result, typename ArgumentTuple$for j1[[, int k$j1]]>
177class SelectArgs<Result, ArgumentTuple,
178 $for j, [[$if j <= i-1 [[k$j]] $else [[-1]]]]> {
179 public:
zhanyong.wane0d051e2009-02-19 00:33:37 +0000180 typedef Result type($for j1, [[GMOCK_FIELD_(ArgumentTuple, k$j1)]]);
shiqiane35fdd92008-12-10 05:08:54 +0000181 typedef typename Function<type>::ArgumentTuple SelectedArgs;
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000182 static SelectedArgs Select(const ArgumentTuple& [[]]
183$if i == 1 [[/* args */]] $else [[args]]) {
shiqiane35fdd92008-12-10 05:08:54 +0000184 return SelectedArgs($for j1, [[get<k$j1>(args)]]);
185 }
186};
187
188
189]]
zhanyong.wane0d051e2009-02-19 00:33:37 +0000190#undef GMOCK_FIELD_
shiqiane35fdd92008-12-10 05:08:54 +0000191
192$var ks = [[$for i, [[k$i]]]]
193
194// Implements the WithArgs action.
195template <typename InnerAction, $for i, [[int k$i = -1]]>
196class WithArgsAction {
197 public:
198 explicit WithArgsAction(const InnerAction& action) : action_(action) {}
199
200 template <typename F>
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000201 operator Action<F>() const { return MakeAction(new Impl<F>(action_)); }
202
203 private:
204 template <typename F>
205 class Impl : public ActionInterface<F> {
206 public:
shiqiane35fdd92008-12-10 05:08:54 +0000207 typedef typename Function<F>::Result Result;
208 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000209
210 explicit Impl(const InnerAction& action) : action_(action) {}
211
212 virtual Result Perform(const ArgumentTuple& args) {
213 return action_.Perform(SelectArgs<Result, ArgumentTuple, $ks>::Select(args));
214 }
215
216 private:
shiqiane35fdd92008-12-10 05:08:54 +0000217 typedef typename SelectArgs<Result, ArgumentTuple,
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000218 $ks>::type InnerFunctionType;
shiqiane35fdd92008-12-10 05:08:54 +0000219
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000220 Action<InnerFunctionType> action_;
221 };
shiqiane35fdd92008-12-10 05:08:54 +0000222
shiqiane35fdd92008-12-10 05:08:54 +0000223 const InnerAction action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000224
225 GTEST_DISALLOW_ASSIGN_(WithArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000226};
227
shiqian326aa562009-01-09 21:43:57 +0000228// A macro from the ACTION* family (defined later in this file)
229// defines an action that can be used in a mock function. Typically,
230// these actions only care about a subset of the arguments of the mock
231// function. For example, if such an action only uses the second
232// argument, it can be used in any mock function that takes >= 2
233// arguments where the type of the second argument is compatible.
234//
235// Therefore, the action implementation must be prepared to take more
236// arguments than it needs. The ExcessiveArg type is used to
237// represent those excessive arguments. In order to keep the compiler
238// error messages tractable, we define it in the testing namespace
239// instead of testing::internal. However, this is an INTERNAL TYPE
240// and subject to change without notice, so a user MUST NOT USE THIS
241// TYPE DIRECTLY.
242struct ExcessiveArg {};
243
244// A helper class needed for implementing the ACTION* macros.
245template <typename Result, class Impl>
246class ActionHelper {
247 public:
248$range i 0..n
249$for i
250
251[[
252$var template = [[$if i==0 [[]] $else [[
253$range j 0..i-1
254 template <$for j, [[typename A$j]]>
255]]]]
256$range j 0..i-1
257$var As = [[$for j, [[A$j]]]]
258$var as = [[$for j, [[get<$j>(args)]]]]
259$range k 1..n-i
260$var eas = [[$for k, [[ExcessiveArg()]]]]
261$var arg_list = [[$if (i==0) | (i==n) [[$as$eas]] $else [[$as, $eas]]]]
262$template
kosakbd018832014-04-02 20:30:00 +0000263 static Result Perform(Impl* impl, const ::testing::tuple<$As>& args) {
zhanyong.wan7f4c2c02009-02-19 22:38:27 +0000264 return impl->template gmock_PerformImpl<$As>(args, $arg_list);
shiqian326aa562009-01-09 21:43:57 +0000265 }
266
267]]
268};
269
shiqiane35fdd92008-12-10 05:08:54 +0000270} // namespace internal
271
272// Various overloads for Invoke().
273
shiqiane35fdd92008-12-10 05:08:54 +0000274// WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes
275// the selected arguments of the mock function to an_action and
276// performs it. It serves as an adaptor between actions with
277// different argument lists. C++ doesn't support default arguments for
278// function templates, so we have to overload it.
279
280$range i 1..n
281$for i [[
282$range j 1..i
283template <$for j [[int k$j, ]]typename InnerAction>
284inline internal::WithArgsAction<InnerAction$for j [[, k$j]]>
285WithArgs(const InnerAction& action) {
286 return internal::WithArgsAction<InnerAction$for j [[, k$j]]>(action);
287}
288
289
290]]
291// Creates an action that does actions a1, a2, ..., sequentially in
292// each invocation.
293$range i 2..n
294$for i [[
295$range j 2..i
296$var types = [[$for j, [[typename Action$j]]]]
297$var Aas = [[$for j [[, Action$j a$j]]]]
298
299template <typename Action1, $types>
300$range k 1..i-1
301
302inline $for k [[internal::DoBothAction<Action$k, ]]Action$i$for k [[>]]
303
304DoAll(Action1 a1$Aas) {
305$if i==2 [[
306
307 return internal::DoBothAction<Action1, Action2>(a1, a2);
308]] $else [[
309$range j2 2..i
310
311 return DoAll(a1, DoAll($for j2, [[a$j2]]));
312]]
313
314}
315
316]]
317
318} // namespace testing
319
shiqian326aa562009-01-09 21:43:57 +0000320// The ACTION* family of macros can be used in a namespace scope to
321// define custom actions easily. The syntax:
322//
323// ACTION(name) { statements; }
324//
325// will define an action with the given name that executes the
326// statements. The value returned by the statements will be used as
327// the return value of the action. Inside the statements, you can
328// refer to the K-th (0-based) argument of the mock function by
329// 'argK', and refer to its type by 'argK_type'. For example:
330//
331// ACTION(IncrementArg1) {
332// arg1_type temp = arg1;
333// return ++(*temp);
334// }
335//
336// allows you to write
337//
338// ...WillOnce(IncrementArg1());
339//
340// You can also refer to the entire argument tuple and its type by
341// 'args' and 'args_type', and refer to the mock function type and its
342// return type by 'function_type' and 'return_type'.
343//
344// Note that you don't need to specify the types of the mock function
345// arguments. However rest assured that your code is still type-safe:
346// you'll get a compiler error if *arg1 doesn't support the ++
347// operator, or if the type of ++(*arg1) isn't compatible with the
348// mock function's return type, for example.
349//
350// Sometimes you'll want to parameterize the action. For that you can use
351// another macro:
352//
353// ACTION_P(name, param_name) { statements; }
354//
355// For example:
356//
357// ACTION_P(Add, n) { return arg0 + n; }
358//
359// will allow you to write:
360//
361// ...WillOnce(Add(5));
362//
363// Note that you don't need to provide the type of the parameter
364// either. If you need to reference the type of a parameter named
365// 'foo', you can write 'foo_type'. For example, in the body of
366// ACTION_P(Add, n) above, you can write 'n_type' to refer to the type
367// of 'n'.
368//
369// We also provide ACTION_P2, ACTION_P3, ..., up to ACTION_P$n to support
370// multi-parameter actions.
371//
372// For the purpose of typing, you can view
373//
374// ACTION_Pk(Foo, p1, ..., pk) { ... }
375//
376// as shorthand for
377//
378// template <typename p1_type, ..., typename pk_type>
379// FooActionPk<p1_type, ..., pk_type> Foo(p1_type p1, ..., pk_type pk) { ... }
380//
381// In particular, you can provide the template type arguments
382// explicitly when invoking Foo(), as in Foo<long, bool>(5, false);
383// although usually you can rely on the compiler to infer the types
384// for you automatically. You can assign the result of expression
385// Foo(p1, ..., pk) to a variable of type FooActionPk<p1_type, ...,
386// pk_type>. This can be useful when composing actions.
387//
388// You can also overload actions with different numbers of parameters:
389//
390// ACTION_P(Plus, a) { ... }
391// ACTION_P2(Plus, a, b) { ... }
392//
393// While it's tempting to always use the ACTION* macros when defining
394// a new action, you should also consider implementing ActionInterface
395// or using MakePolymorphicAction() instead, especially if you need to
396// use the action a lot. While these approaches require more work,
397// they give you more control on the types of the mock function
398// arguments and the action parameters, which in general leads to
399// better compiler error messages that pay off in the long run. They
400// also allow overloading actions based on parameter types (as opposed
401// to just based on the number of parameters).
402//
403// CAVEAT:
404//
405// ACTION*() can only be used in a namespace scope. The reason is
406// that C++ doesn't yet allow function-local types to be used to
407// instantiate templates. The up-coming C++0x standard will fix this.
408// Once that's done, we'll consider supporting using ACTION*() inside
409// a function.
410//
411// MORE INFORMATION:
412//
413// To learn more about using these macros, please search for 'ACTION'
414// on http://code.google.com/p/googlemock/wiki/CookBook.
415
416$range i 0..n
zhanyong.wan33c0af02009-04-03 00:10:12 +0000417$range k 0..n-1
418
419// An internal macro needed for implementing ACTION*().
420#define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_\
421 const args_type& args GTEST_ATTRIBUTE_UNUSED_
jgm79a367e2012-04-10 16:02:11 +0000422$for k [[, \
zhanyong.wan33c0af02009-04-03 00:10:12 +0000423 arg$k[[]]_type arg$k GTEST_ATTRIBUTE_UNUSED_]]
424
425
zhanyong.wan18490652009-05-11 18:54:08 +0000426// Sometimes you want to give an action explicit template parameters
427// that cannot be inferred from its value parameters. ACTION() and
428// ACTION_P*() don't support that. ACTION_TEMPLATE() remedies that
429// and can be viewed as an extension to ACTION() and ACTION_P*().
430//
431// The syntax:
432//
433// ACTION_TEMPLATE(ActionName,
434// HAS_m_TEMPLATE_PARAMS(kind1, name1, ..., kind_m, name_m),
435// AND_n_VALUE_PARAMS(p1, ..., p_n)) { statements; }
436//
437// defines an action template that takes m explicit template
438// parameters and n value parameters. name_i is the name of the i-th
439// template parameter, and kind_i specifies whether it's a typename,
440// an integral constant, or a template. p_i is the name of the i-th
441// value parameter.
442//
443// Example:
444//
445// // DuplicateArg<k, T>(output) converts the k-th argument of the mock
446// // function to type T and copies it to *output.
447// ACTION_TEMPLATE(DuplicateArg,
448// HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
449// AND_1_VALUE_PARAMS(output)) {
kosakbd018832014-04-02 20:30:00 +0000450// *output = T(::testing::get<k>(args));
zhanyong.wan18490652009-05-11 18:54:08 +0000451// }
452// ...
453// int n;
454// EXPECT_CALL(mock, Foo(_, _))
455// .WillOnce(DuplicateArg<1, unsigned char>(&n));
456//
457// To create an instance of an action template, write:
458//
459// ActionName<t1, ..., t_m>(v1, ..., v_n)
460//
461// where the ts are the template arguments and the vs are the value
462// arguments. The value argument types are inferred by the compiler.
463// If you want to explicitly specify the value argument types, you can
464// provide additional template arguments:
465//
466// ActionName<t1, ..., t_m, u1, ..., u_k>(v1, ..., v_n)
467//
468// where u_i is the desired type of v_i.
469//
470// ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded on the
471// number of value parameters, but not on the number of template
472// parameters. Without the restriction, the meaning of the following
473// is unclear:
474//
475// OverloadedAction<int, bool>(x);
476//
477// Are we using a single-template-parameter action where 'bool' refers
478// to the type of x, or are we using a two-template-parameter action
479// where the compiler is asked to infer the type of x?
480//
481// Implementation notes:
482//
483// GMOCK_INTERNAL_*_HAS_m_TEMPLATE_PARAMS and
484// GMOCK_INTERNAL_*_AND_n_VALUE_PARAMS are internal macros for
485// implementing ACTION_TEMPLATE. The main trick we use is to create
486// new macro invocations when expanding a macro. For example, we have
487//
488// #define ACTION_TEMPLATE(name, template_params, value_params)
489// ... GMOCK_INTERNAL_DECL_##template_params ...
490//
491// which causes ACTION_TEMPLATE(..., HAS_1_TEMPLATE_PARAMS(typename, T), ...)
492// to expand to
493//
494// ... GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(typename, T) ...
495//
496// Since GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS is a macro, the
497// preprocessor will continue to expand it to
498//
499// ... typename T ...
500//
501// This technique conforms to the C++ standard and is portable. It
502// allows us to implement action templates using O(N) code, where N is
503// the maximum number of template/value parameters supported. Without
504// using it, we'd have to devote O(N^2) amount of code to implement all
505// combinations of m and n.
506
507// Declares the template parameters.
508
509$range j 1..n
510$for j [[
511$range m 0..j-1
512#define GMOCK_INTERNAL_DECL_HAS_$j[[]]
513_TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[kind$m name$m]]
514
515
516]]
517
518// Lists the template parameters.
519
520$for j [[
521$range m 0..j-1
522#define GMOCK_INTERNAL_LIST_HAS_$j[[]]
523_TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[name$m]]
524
525
526]]
527
528// Declares the types of value parameters.
529
530$for i [[
531$range j 0..i-1
532#define GMOCK_INTERNAL_DECL_TYPE_AND_$i[[]]
533_VALUE_PARAMS($for j, [[p$j]]) $for j [[, typename p$j##_type]]
534
535
536]]
537
538// Initializes the value parameters.
539
540$for i [[
541$range j 0..i-1
542#define GMOCK_INTERNAL_INIT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])\
543 ($for j, [[p$j##_type gmock_p$j]])$if i>0 [[ : ]]$for j, [[p$j(gmock_p$j)]]
544
545
546]]
547
548// Declares the fields for storing the value parameters.
549
550$for i [[
551$range j 0..i-1
552#define GMOCK_INTERNAL_DEFN_AND_$i[[]]
553_VALUE_PARAMS($for j, [[p$j]]) $for j [[p$j##_type p$j; ]]
554
555
556]]
557
558// Lists the value parameters.
559
560$for i [[
561$range j 0..i-1
562#define GMOCK_INTERNAL_LIST_AND_$i[[]]
563_VALUE_PARAMS($for j, [[p$j]]) $for j, [[p$j]]
564
565
566]]
567
568// Lists the value parameter types.
569
570$for i [[
571$range j 0..i-1
572#define GMOCK_INTERNAL_LIST_TYPE_AND_$i[[]]
573_VALUE_PARAMS($for j, [[p$j]]) $for j [[, p$j##_type]]
574
575
576]]
577
578// Declares the value parameters.
579
580$for i [[
581$range j 0..i-1
582#define GMOCK_INTERNAL_DECL_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
583$for j, [[p$j##_type p$j]]
584
585
586]]
587
588// The suffix of the class template implementing the action template.
589$for i [[
590
591
592$range j 0..i-1
593#define GMOCK_INTERNAL_COUNT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
594$if i==1 [[P]] $elif i>=2 [[P$i]]
595]]
596
597
598// The name of the class template implementing the action template.
599#define GMOCK_ACTION_CLASS_(name, value_params)\
zhanyong.wanccedc1c2010-08-09 22:46:12 +0000600 GTEST_CONCAT_TOKEN_(name##Action, GMOCK_INTERNAL_COUNT_##value_params)
zhanyong.wan18490652009-05-11 18:54:08 +0000601
602$range k 0..n-1
603
604#define ACTION_TEMPLATE(name, template_params, value_params)\
605 template <GMOCK_INTERNAL_DECL_##template_params\
606 GMOCK_INTERNAL_DECL_TYPE_##value_params>\
607 class GMOCK_ACTION_CLASS_(name, value_params) {\
608 public:\
billydonahue1f5fdea2014-05-19 17:54:51 +0000609 explicit GMOCK_ACTION_CLASS_(name, value_params)\
zhanyong.wan18490652009-05-11 18:54:08 +0000610 GMOCK_INTERNAL_INIT_##value_params {}\
611 template <typename F>\
612 class gmock_Impl : public ::testing::ActionInterface<F> {\
613 public:\
614 typedef F function_type;\
615 typedef typename ::testing::internal::Function<F>::Result return_type;\
616 typedef typename ::testing::internal::Function<F>::ArgumentTuple\
617 args_type;\
618 explicit gmock_Impl GMOCK_INTERNAL_INIT_##value_params {}\
619 virtual return_type Perform(const args_type& args) {\
620 return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
621 Perform(this, args);\
622 }\
623 template <$for k, [[typename arg$k[[]]_type]]>\
624 return_type gmock_PerformImpl(const args_type& args[[]]
625$for k [[, arg$k[[]]_type arg$k]]) const;\
626 GMOCK_INTERNAL_DEFN_##value_params\
zhanyong.wan32de5f52009-12-23 00:13:23 +0000627 private:\
628 GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
zhanyong.wan18490652009-05-11 18:54:08 +0000629 };\
630 template <typename F> operator ::testing::Action<F>() const {\
631 return ::testing::Action<F>(\
632 new gmock_Impl<F>(GMOCK_INTERNAL_LIST_##value_params));\
633 }\
634 GMOCK_INTERNAL_DEFN_##value_params\
zhanyong.wan32de5f52009-12-23 00:13:23 +0000635 private:\
636 GTEST_DISALLOW_ASSIGN_(GMOCK_ACTION_CLASS_(name, value_params));\
zhanyong.wan18490652009-05-11 18:54:08 +0000637 };\
638 template <GMOCK_INTERNAL_DECL_##template_params\
639 GMOCK_INTERNAL_DECL_TYPE_##value_params>\
640 inline GMOCK_ACTION_CLASS_(name, value_params)<\
641 GMOCK_INTERNAL_LIST_##template_params\
642 GMOCK_INTERNAL_LIST_TYPE_##value_params> name(\
643 GMOCK_INTERNAL_DECL_##value_params) {\
644 return GMOCK_ACTION_CLASS_(name, value_params)<\
645 GMOCK_INTERNAL_LIST_##template_params\
646 GMOCK_INTERNAL_LIST_TYPE_##value_params>(\
647 GMOCK_INTERNAL_LIST_##value_params);\
648 }\
649 template <GMOCK_INTERNAL_DECL_##template_params\
650 GMOCK_INTERNAL_DECL_TYPE_##value_params>\
651 template <typename F>\
jgm79a367e2012-04-10 16:02:11 +0000652 template <typename arg0_type, typename arg1_type, typename arg2_type, \
653 typename arg3_type, typename arg4_type, typename arg5_type, \
654 typename arg6_type, typename arg7_type, typename arg8_type, \
zhanyong.wan18490652009-05-11 18:54:08 +0000655 typename arg9_type>\
656 typename ::testing::internal::Function<F>::Result\
657 GMOCK_ACTION_CLASS_(name, value_params)<\
658 GMOCK_INTERNAL_LIST_##template_params\
659 GMOCK_INTERNAL_LIST_TYPE_##value_params>::gmock_Impl<F>::\
660 gmock_PerformImpl(\
661 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
662
shiqian326aa562009-01-09 21:43:57 +0000663$for i
664
665[[
666$var template = [[$if i==0 [[]] $else [[
667$range j 0..i-1
668
669 template <$for j, [[typename p$j##_type]]>\
670]]]]
671$var class_name = [[name##Action[[$if i==0 [[]] $elif i==1 [[P]]
672 $else [[P$i]]]]]]
673$range j 0..i-1
674$var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
675$var param_types_and_names = [[$for j, [[p$j##_type p$j]]]]
676$var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]]
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000677$var param_field_decls = [[$for j
shiqian326aa562009-01-09 21:43:57 +0000678[[
679
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000680 p$j##_type p$j;\
shiqian326aa562009-01-09 21:43:57 +0000681]]]]
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000682$var param_field_decls2 = [[$for j
shiqian326aa562009-01-09 21:43:57 +0000683[[
684
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000685 p$j##_type p$j;\
shiqian326aa562009-01-09 21:43:57 +0000686]]]]
687$var params = [[$for j, [[p$j]]]]
688$var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]]
shiqian326aa562009-01-09 21:43:57 +0000689$var typename_arg_types = [[$for k, [[typename arg$k[[]]_type]]]]
690$var arg_types_and_names = [[$for k, [[arg$k[[]]_type arg$k]]]]
691$var macro_name = [[$if i==0 [[ACTION]] $elif i==1 [[ACTION_P]]
692 $else [[ACTION_P$i]]]]
693
694#define $macro_name(name$for j [[, p$j]])\$template
695 class $class_name {\
696 public:\
billydonahue1f5fdea2014-05-19 17:54:51 +0000697 [[$if i==1 [[explicit ]]]]$class_name($ctor_param_list)$inits {}\
shiqian326aa562009-01-09 21:43:57 +0000698 template <typename F>\
699 class gmock_Impl : public ::testing::ActionInterface<F> {\
700 public:\
701 typedef F function_type;\
702 typedef typename ::testing::internal::Function<F>::Result return_type;\
703 typedef typename ::testing::internal::Function<F>::ArgumentTuple\
704 args_type;\
705 [[$if i==1 [[explicit ]]]]gmock_Impl($ctor_param_list)$inits {}\
706 virtual return_type Perform(const args_type& args) {\
707 return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
708 Perform(this, args);\
709 }\
710 template <$typename_arg_types>\
711 return_type gmock_PerformImpl(const args_type& args, [[]]
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000712$arg_types_and_names) const;\$param_field_decls
zhanyong.wan32de5f52009-12-23 00:13:23 +0000713 private:\
714 GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
shiqian326aa562009-01-09 21:43:57 +0000715 };\
716 template <typename F> operator ::testing::Action<F>() const {\
717 return ::testing::Action<F>(new gmock_Impl<F>($params));\
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000718 }\$param_field_decls2
zhanyong.wan32de5f52009-12-23 00:13:23 +0000719 private:\
720 GTEST_DISALLOW_ASSIGN_($class_name);\
shiqian326aa562009-01-09 21:43:57 +0000721 };\$template
722 inline $class_name$param_types name($param_types_and_names) {\
723 return $class_name$param_types($params);\
724 }\$template
725 template <typename F>\
726 template <$typename_arg_types>\
727 typename ::testing::internal::Function<F>::Result\
zhanyong.wan33c0af02009-04-03 00:10:12 +0000728 $class_name$param_types::gmock_Impl<F>::gmock_PerformImpl(\
729 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
shiqian326aa562009-01-09 21:43:57 +0000730]]
zhanyong.wan7f4c2c02009-02-19 22:38:27 +0000731$$ } // This meta comment fixes auto-indentation in Emacs. It won't
732$$ // show up in the generated code.
shiqian326aa562009-01-09 21:43:57 +0000733
734
zhanyong.wane1cdce52009-02-06 01:09:43 +0000735namespace testing {
736
zhanyong.wan32de5f52009-12-23 00:13:23 +0000737// The ACTION*() macros trigger warning C4100 (unreferenced formal
738// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
739// the macro definition, as the warnings are generated when the macro
740// is expanded and macro expansion cannot contain #pragma. Therefore
741// we suppress them here.
742#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000743# pragma warning(push)
744# pragma warning(disable:4100)
zhanyong.wan32de5f52009-12-23 00:13:23 +0000745#endif
746
zhanyong.wan16cf4732009-05-14 20:55:30 +0000747// Various overloads for InvokeArgument<N>().
748//
749// The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th
750// (0-based) argument, which must be a k-ary callable, of the mock
751// function, with arguments a1, a2, ..., a_k.
752//
753// Notes:
754//
755// 1. The arguments are passed by value by default. If you need to
756// pass an argument by reference, wrap it inside ByRef(). For
757// example,
758//
759// InvokeArgument<1>(5, string("Hello"), ByRef(foo))
760//
761// passes 5 and string("Hello") by value, and passes foo by
762// reference.
763//
764// 2. If the callable takes an argument by reference but ByRef() is
765// not used, it will receive the reference to a copy of the value,
766// instead of the original value. For example, when the 0-th
767// argument of the mock function takes a const string&, the action
768//
769// InvokeArgument<0>(string("Hello"))
770//
771// makes a copy of the temporary string("Hello") object and passes a
772// reference of the copy, instead of the original temporary object,
773// to the callable. This makes it easy for a user to define an
774// InvokeArgument action from temporary values and have it performed
775// later.
zhanyong.wan7f4c2c02009-02-19 22:38:27 +0000776
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000777$range i 0..n
778$for i [[
zhanyong.wan16cf4732009-05-14 20:55:30 +0000779$range j 0..i-1
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000780
zhanyong.wan16cf4732009-05-14 20:55:30 +0000781ACTION_TEMPLATE(InvokeArgument,
782 HAS_1_TEMPLATE_PARAMS(int, k),
783 AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])) {
784 return internal::CallableHelper<return_type>::Call(
kosakbd018832014-04-02 20:30:00 +0000785 ::testing::get<k>(args)$for j [[, p$j]]);
zhanyong.wan16cf4732009-05-14 20:55:30 +0000786}
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000787
788]]
789
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000790// Various overloads for ReturnNew<T>().
791//
792// The ReturnNew<T>(a1, a2, ..., a_k) action returns a pointer to a new
793// instance of type T, constructed on the heap with constructor arguments
794// a1, a2, ..., and a_k. The caller assumes ownership of the returned value.
795$range i 0..n
796$for i [[
zhanyong.wan16cf4732009-05-14 20:55:30 +0000797$range j 0..i-1
798$var ps = [[$for j, [[p$j]]]]
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000799
zhanyong.wan16cf4732009-05-14 20:55:30 +0000800ACTION_TEMPLATE(ReturnNew,
801 HAS_1_TEMPLATE_PARAMS(typename, T),
802 AND_$i[[]]_VALUE_PARAMS($ps)) {
803 return new T($ps);
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000804}
805
806]]
807
zhanyong.wan32de5f52009-12-23 00:13:23 +0000808#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000809# pragma warning(pop)
zhanyong.wan32de5f52009-12-23 00:13:23 +0000810#endif
811
zhanyong.wane1cdce52009-02-06 01:09:43 +0000812} // namespace testing
813
shiqiane35fdd92008-12-10 05:08:54 +0000814#endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_