blob: 05bf3db3b325179f4c2850f00e8da6ff19b2da01 [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
45#include <gmock/gmock-actions.h>
46#include <gmock/internal/gmock-port.h>
47
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]]]]
64$var import = [[$if i==0 [[]] $else [[
65 using ::std::tr1::get;
66
67]]]]
68$var gets = [[$for j, [[get<$(j - 1)>(args)]]]]
69template <typename R$types>
70class InvokeHelper<R, ::std::tr1::tuple<$as> > {
71 public:
72 template <typename Function>
73 static R Invoke(Function function, const ::std::tr1::tuple<$as>&$args) {
74$import return function($gets);
75 }
76
77 template <class Class, typename MethodPtr>
78 static R InvokeMethod(Class* obj_ptr,
79 MethodPtr method_ptr,
80 const ::std::tr1::tuple<$as>&$args) {
81$import return (obj_ptr->*method_ptr)($gets);
82 }
83};
84
85
86]]
shiqiane35fdd92008-12-10 05:08:54 +000087// CallableHelper has static methods for invoking "callables",
88// i.e. function pointers and functors. It uses overloading to
89// provide a uniform interface for invoking different kinds of
90// callables. In particular, you can use:
91//
92// CallableHelper<R>::Call(callable, a1, a2, ..., an)
93//
94// to invoke an n-ary callable, where R is its return type. If an
95// argument, say a2, needs to be passed by reference, you should write
96// ByRef(a2) instead of a2 in the above expression.
97template <typename R>
98class CallableHelper {
99 public:
100 // Calls a nullary callable.
101 template <typename Function>
102 static R Call(Function function) { return function(); }
103
104 // Calls a unary callable.
105
106 // We deliberately pass a1 by value instead of const reference here
107 // in case it is a C-string literal. If we had declared the
108 // parameter as 'const A1& a1' and write Call(function, "Hi"), the
109 // compiler would've thought A1 is 'char[3]', which causes trouble
110 // when you need to copy a value of type A1. By declaring the
111 // parameter as 'A1 a1', the compiler will correctly infer that A1
112 // is 'const char*' when it sees Call(function, "Hi").
113 //
114 // Since this function is defined inline, the compiler can get rid
115 // of the copying of the arguments. Therefore the performance won't
116 // be hurt.
117 template <typename Function, typename A1>
118 static R Call(Function function, A1 a1) { return function(a1); }
119
120$range i 2..n
121$for i
122[[
123$var arity = [[$if i==2 [[binary]] $elif i==3 [[ternary]] $else [[$i-ary]]]]
124
125 // Calls a $arity callable.
126
127$range j 1..i
128$var typename_As = [[$for j, [[typename A$j]]]]
129$var Aas = [[$for j, [[A$j a$j]]]]
130$var as = [[$for j, [[a$j]]]]
131$var typename_Ts = [[$for j, [[typename T$j]]]]
132$var Ts = [[$for j, [[T$j]]]]
133 template <typename Function, $typename_As>
134 static R Call(Function function, $Aas) {
135 return function($as);
136 }
137
138]]
139
140}; // class CallableHelper
141
shiqiane35fdd92008-12-10 05:08:54 +0000142// An INTERNAL macro for extracting the type of a tuple field. It's
143// subject to change without notice - DO NOT USE IN USER CODE!
zhanyong.wane0d051e2009-02-19 00:33:37 +0000144#define GMOCK_FIELD_(Tuple, N) \
shiqiane35fdd92008-12-10 05:08:54 +0000145 typename ::std::tr1::tuple_element<N, Tuple>::type
146
147$range i 1..n
148
149// SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::type is the
150// type of an n-ary function whose i-th (1-based) argument type is the
151// k{i}-th (0-based) field of ArgumentTuple, which must be a tuple
152// type, and whose return type is Result. For example,
153// SelectArgs<int, ::std::tr1::tuple<bool, char, double, long>, 0, 3>::type
154// is int(bool, long).
155//
156// SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::Select(args)
157// returns the selected fields (k1, k2, ..., k_n) of args as a tuple.
158// For example,
159// SelectArgs<int, ::std::tr1::tuple<bool, char, double>, 2, 0>::Select(
160// ::std::tr1::make_tuple(true, 'a', 2.5))
161// returns ::std::tr1::tuple (2.5, true).
162//
163// The numbers in list k1, k2, ..., k_n must be >= 0, where n can be
164// in the range [0, $n]. Duplicates are allowed and they don't have
165// to be in an ascending or descending order.
166
167template <typename Result, typename ArgumentTuple, $for i, [[int k$i]]>
168class SelectArgs {
169 public:
zhanyong.wane0d051e2009-02-19 00:33:37 +0000170 typedef Result type($for i, [[GMOCK_FIELD_(ArgumentTuple, k$i)]]);
shiqiane35fdd92008-12-10 05:08:54 +0000171 typedef typename Function<type>::ArgumentTuple SelectedArgs;
172 static SelectedArgs Select(const ArgumentTuple& args) {
173 using ::std::tr1::get;
174 return SelectedArgs($for i, [[get<k$i>(args)]]);
175 }
176};
177
178
179$for i [[
180$range j 1..n
181$range j1 1..i-1
182template <typename Result, typename ArgumentTuple$for j1[[, int k$j1]]>
183class SelectArgs<Result, ArgumentTuple,
184 $for j, [[$if j <= i-1 [[k$j]] $else [[-1]]]]> {
185 public:
zhanyong.wane0d051e2009-02-19 00:33:37 +0000186 typedef Result type($for j1, [[GMOCK_FIELD_(ArgumentTuple, k$j1)]]);
shiqiane35fdd92008-12-10 05:08:54 +0000187 typedef typename Function<type>::ArgumentTuple SelectedArgs;
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000188 static SelectedArgs Select(const ArgumentTuple& [[]]
189$if i == 1 [[/* args */]] $else [[args]]) {
shiqiane35fdd92008-12-10 05:08:54 +0000190 using ::std::tr1::get;
191 return SelectedArgs($for j1, [[get<k$j1>(args)]]);
192 }
193};
194
195
196]]
zhanyong.wane0d051e2009-02-19 00:33:37 +0000197#undef GMOCK_FIELD_
shiqiane35fdd92008-12-10 05:08:54 +0000198
199$var ks = [[$for i, [[k$i]]]]
200
201// Implements the WithArgs action.
202template <typename InnerAction, $for i, [[int k$i = -1]]>
203class WithArgsAction {
204 public:
205 explicit WithArgsAction(const InnerAction& action) : action_(action) {}
206
207 template <typename F>
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000208 operator Action<F>() const { return MakeAction(new Impl<F>(action_)); }
209
210 private:
211 template <typename F>
212 class Impl : public ActionInterface<F> {
213 public:
shiqiane35fdd92008-12-10 05:08:54 +0000214 typedef typename Function<F>::Result Result;
215 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000216
217 explicit Impl(const InnerAction& action) : action_(action) {}
218
219 virtual Result Perform(const ArgumentTuple& args) {
220 return action_.Perform(SelectArgs<Result, ArgumentTuple, $ks>::Select(args));
221 }
222
223 private:
shiqiane35fdd92008-12-10 05:08:54 +0000224 typedef typename SelectArgs<Result, ArgumentTuple,
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000225 $ks>::type InnerFunctionType;
shiqiane35fdd92008-12-10 05:08:54 +0000226
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000227 Action<InnerFunctionType> action_;
228 };
shiqiane35fdd92008-12-10 05:08:54 +0000229
shiqiane35fdd92008-12-10 05:08:54 +0000230 const InnerAction action_;
231};
232
shiqian326aa562009-01-09 21:43:57 +0000233// A macro from the ACTION* family (defined later in this file)
234// defines an action that can be used in a mock function. Typically,
235// these actions only care about a subset of the arguments of the mock
236// function. For example, if such an action only uses the second
237// argument, it can be used in any mock function that takes >= 2
238// arguments where the type of the second argument is compatible.
239//
240// Therefore, the action implementation must be prepared to take more
241// arguments than it needs. The ExcessiveArg type is used to
242// represent those excessive arguments. In order to keep the compiler
243// error messages tractable, we define it in the testing namespace
244// instead of testing::internal. However, this is an INTERNAL TYPE
245// and subject to change without notice, so a user MUST NOT USE THIS
246// TYPE DIRECTLY.
247struct ExcessiveArg {};
248
249// A helper class needed for implementing the ACTION* macros.
250template <typename Result, class Impl>
251class ActionHelper {
252 public:
253$range i 0..n
254$for i
255
256[[
257$var template = [[$if i==0 [[]] $else [[
258$range j 0..i-1
259 template <$for j, [[typename A$j]]>
260]]]]
261$range j 0..i-1
262$var As = [[$for j, [[A$j]]]]
263$var as = [[$for j, [[get<$j>(args)]]]]
264$range k 1..n-i
265$var eas = [[$for k, [[ExcessiveArg()]]]]
266$var arg_list = [[$if (i==0) | (i==n) [[$as$eas]] $else [[$as, $eas]]]]
267$template
268 static Result Perform(Impl* impl, const ::std::tr1::tuple<$As>& args) {
269 using ::std::tr1::get;
zhanyong.wan7f4c2c02009-02-19 22:38:27 +0000270 return impl->template gmock_PerformImpl<$As>(args, $arg_list);
shiqian326aa562009-01-09 21:43:57 +0000271 }
272
273]]
274};
275
shiqiane35fdd92008-12-10 05:08:54 +0000276} // namespace internal
277
278// Various overloads for Invoke().
279
shiqiane35fdd92008-12-10 05:08:54 +0000280// WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes
281// the selected arguments of the mock function to an_action and
282// performs it. It serves as an adaptor between actions with
283// different argument lists. C++ doesn't support default arguments for
284// function templates, so we have to overload it.
285
286$range i 1..n
287$for i [[
288$range j 1..i
289template <$for j [[int k$j, ]]typename InnerAction>
290inline internal::WithArgsAction<InnerAction$for j [[, k$j]]>
291WithArgs(const InnerAction& action) {
292 return internal::WithArgsAction<InnerAction$for j [[, k$j]]>(action);
293}
294
295
296]]
297// Creates an action that does actions a1, a2, ..., sequentially in
298// each invocation.
299$range i 2..n
300$for i [[
301$range j 2..i
302$var types = [[$for j, [[typename Action$j]]]]
303$var Aas = [[$for j [[, Action$j a$j]]]]
304
305template <typename Action1, $types>
306$range k 1..i-1
307
308inline $for k [[internal::DoBothAction<Action$k, ]]Action$i$for k [[>]]
309
310DoAll(Action1 a1$Aas) {
311$if i==2 [[
312
313 return internal::DoBothAction<Action1, Action2>(a1, a2);
314]] $else [[
315$range j2 2..i
316
317 return DoAll(a1, DoAll($for j2, [[a$j2]]));
318]]
319
320}
321
322]]
323
324} // namespace testing
325
shiqian326aa562009-01-09 21:43:57 +0000326// The ACTION* family of macros can be used in a namespace scope to
327// define custom actions easily. The syntax:
328//
329// ACTION(name) { statements; }
330//
331// will define an action with the given name that executes the
332// statements. The value returned by the statements will be used as
333// the return value of the action. Inside the statements, you can
334// refer to the K-th (0-based) argument of the mock function by
335// 'argK', and refer to its type by 'argK_type'. For example:
336//
337// ACTION(IncrementArg1) {
338// arg1_type temp = arg1;
339// return ++(*temp);
340// }
341//
342// allows you to write
343//
344// ...WillOnce(IncrementArg1());
345//
346// You can also refer to the entire argument tuple and its type by
347// 'args' and 'args_type', and refer to the mock function type and its
348// return type by 'function_type' and 'return_type'.
349//
350// Note that you don't need to specify the types of the mock function
351// arguments. However rest assured that your code is still type-safe:
352// you'll get a compiler error if *arg1 doesn't support the ++
353// operator, or if the type of ++(*arg1) isn't compatible with the
354// mock function's return type, for example.
355//
356// Sometimes you'll want to parameterize the action. For that you can use
357// another macro:
358//
359// ACTION_P(name, param_name) { statements; }
360//
361// For example:
362//
363// ACTION_P(Add, n) { return arg0 + n; }
364//
365// will allow you to write:
366//
367// ...WillOnce(Add(5));
368//
369// Note that you don't need to provide the type of the parameter
370// either. If you need to reference the type of a parameter named
371// 'foo', you can write 'foo_type'. For example, in the body of
372// ACTION_P(Add, n) above, you can write 'n_type' to refer to the type
373// of 'n'.
374//
375// We also provide ACTION_P2, ACTION_P3, ..., up to ACTION_P$n to support
376// multi-parameter actions.
377//
378// For the purpose of typing, you can view
379//
380// ACTION_Pk(Foo, p1, ..., pk) { ... }
381//
382// as shorthand for
383//
384// template <typename p1_type, ..., typename pk_type>
385// FooActionPk<p1_type, ..., pk_type> Foo(p1_type p1, ..., pk_type pk) { ... }
386//
387// In particular, you can provide the template type arguments
388// explicitly when invoking Foo(), as in Foo<long, bool>(5, false);
389// although usually you can rely on the compiler to infer the types
390// for you automatically. You can assign the result of expression
391// Foo(p1, ..., pk) to a variable of type FooActionPk<p1_type, ...,
392// pk_type>. This can be useful when composing actions.
393//
394// You can also overload actions with different numbers of parameters:
395//
396// ACTION_P(Plus, a) { ... }
397// ACTION_P2(Plus, a, b) { ... }
398//
399// While it's tempting to always use the ACTION* macros when defining
400// a new action, you should also consider implementing ActionInterface
401// or using MakePolymorphicAction() instead, especially if you need to
402// use the action a lot. While these approaches require more work,
403// they give you more control on the types of the mock function
404// arguments and the action parameters, which in general leads to
405// better compiler error messages that pay off in the long run. They
406// also allow overloading actions based on parameter types (as opposed
407// to just based on the number of parameters).
408//
409// CAVEAT:
410//
411// ACTION*() can only be used in a namespace scope. The reason is
412// that C++ doesn't yet allow function-local types to be used to
413// instantiate templates. The up-coming C++0x standard will fix this.
414// Once that's done, we'll consider supporting using ACTION*() inside
415// a function.
416//
417// MORE INFORMATION:
418//
419// To learn more about using these macros, please search for 'ACTION'
420// on http://code.google.com/p/googlemock/wiki/CookBook.
421
422$range i 0..n
zhanyong.wan33c0af02009-04-03 00:10:12 +0000423$range k 0..n-1
424
425// An internal macro needed for implementing ACTION*().
426#define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_\
427 const args_type& args GTEST_ATTRIBUTE_UNUSED_
428$for k [[,\
429 arg$k[[]]_type arg$k GTEST_ATTRIBUTE_UNUSED_]]
430
431
zhanyong.wan18490652009-05-11 18:54:08 +0000432// Sometimes you want to give an action explicit template parameters
433// that cannot be inferred from its value parameters. ACTION() and
434// ACTION_P*() don't support that. ACTION_TEMPLATE() remedies that
435// and can be viewed as an extension to ACTION() and ACTION_P*().
436//
437// The syntax:
438//
439// ACTION_TEMPLATE(ActionName,
440// HAS_m_TEMPLATE_PARAMS(kind1, name1, ..., kind_m, name_m),
441// AND_n_VALUE_PARAMS(p1, ..., p_n)) { statements; }
442//
443// defines an action template that takes m explicit template
444// parameters and n value parameters. name_i is the name of the i-th
445// template parameter, and kind_i specifies whether it's a typename,
446// an integral constant, or a template. p_i is the name of the i-th
447// value parameter.
448//
449// Example:
450//
451// // DuplicateArg<k, T>(output) converts the k-th argument of the mock
452// // function to type T and copies it to *output.
453// ACTION_TEMPLATE(DuplicateArg,
454// HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
455// AND_1_VALUE_PARAMS(output)) {
456// *output = T(std::tr1::get<k>(args));
457// }
458// ...
459// int n;
460// EXPECT_CALL(mock, Foo(_, _))
461// .WillOnce(DuplicateArg<1, unsigned char>(&n));
462//
463// To create an instance of an action template, write:
464//
465// ActionName<t1, ..., t_m>(v1, ..., v_n)
466//
467// where the ts are the template arguments and the vs are the value
468// arguments. The value argument types are inferred by the compiler.
469// If you want to explicitly specify the value argument types, you can
470// provide additional template arguments:
471//
472// ActionName<t1, ..., t_m, u1, ..., u_k>(v1, ..., v_n)
473//
474// where u_i is the desired type of v_i.
475//
476// ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded on the
477// number of value parameters, but not on the number of template
478// parameters. Without the restriction, the meaning of the following
479// is unclear:
480//
481// OverloadedAction<int, bool>(x);
482//
483// Are we using a single-template-parameter action where 'bool' refers
484// to the type of x, or are we using a two-template-parameter action
485// where the compiler is asked to infer the type of x?
486//
487// Implementation notes:
488//
489// GMOCK_INTERNAL_*_HAS_m_TEMPLATE_PARAMS and
490// GMOCK_INTERNAL_*_AND_n_VALUE_PARAMS are internal macros for
491// implementing ACTION_TEMPLATE. The main trick we use is to create
492// new macro invocations when expanding a macro. For example, we have
493//
494// #define ACTION_TEMPLATE(name, template_params, value_params)
495// ... GMOCK_INTERNAL_DECL_##template_params ...
496//
497// which causes ACTION_TEMPLATE(..., HAS_1_TEMPLATE_PARAMS(typename, T), ...)
498// to expand to
499//
500// ... GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(typename, T) ...
501//
502// Since GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS is a macro, the
503// preprocessor will continue to expand it to
504//
505// ... typename T ...
506//
507// This technique conforms to the C++ standard and is portable. It
508// allows us to implement action templates using O(N) code, where N is
509// the maximum number of template/value parameters supported. Without
510// using it, we'd have to devote O(N^2) amount of code to implement all
511// combinations of m and n.
512
513// Declares the template parameters.
514
515$range j 1..n
516$for j [[
517$range m 0..j-1
518#define GMOCK_INTERNAL_DECL_HAS_$j[[]]
519_TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[kind$m name$m]]
520
521
522]]
523
524// Lists the template parameters.
525
526$for j [[
527$range m 0..j-1
528#define GMOCK_INTERNAL_LIST_HAS_$j[[]]
529_TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[name$m]]
530
531
532]]
533
534// Declares the types of value parameters.
535
536$for i [[
537$range j 0..i-1
538#define GMOCK_INTERNAL_DECL_TYPE_AND_$i[[]]
539_VALUE_PARAMS($for j, [[p$j]]) $for j [[, typename p$j##_type]]
540
541
542]]
543
544// Initializes the value parameters.
545
546$for i [[
547$range j 0..i-1
548#define GMOCK_INTERNAL_INIT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])\
549 ($for j, [[p$j##_type gmock_p$j]])$if i>0 [[ : ]]$for j, [[p$j(gmock_p$j)]]
550
551
552]]
553
554// Declares the fields for storing the value parameters.
555
556$for i [[
557$range j 0..i-1
558#define GMOCK_INTERNAL_DEFN_AND_$i[[]]
559_VALUE_PARAMS($for j, [[p$j]]) $for j [[p$j##_type p$j; ]]
560
561
562]]
563
564// Lists the value parameters.
565
566$for i [[
567$range j 0..i-1
568#define GMOCK_INTERNAL_LIST_AND_$i[[]]
569_VALUE_PARAMS($for j, [[p$j]]) $for j, [[p$j]]
570
571
572]]
573
574// Lists the value parameter types.
575
576$for i [[
577$range j 0..i-1
578#define GMOCK_INTERNAL_LIST_TYPE_AND_$i[[]]
579_VALUE_PARAMS($for j, [[p$j]]) $for j [[, p$j##_type]]
580
581
582]]
583
584// Declares the value parameters.
585
586$for i [[
587$range j 0..i-1
588#define GMOCK_INTERNAL_DECL_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
589$for j, [[p$j##_type p$j]]
590
591
592]]
593
594// The suffix of the class template implementing the action template.
595$for i [[
596
597
598$range j 0..i-1
599#define GMOCK_INTERNAL_COUNT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
600$if i==1 [[P]] $elif i>=2 [[P$i]]
601]]
602
603
604// The name of the class template implementing the action template.
605#define GMOCK_ACTION_CLASS_(name, value_params)\
606 GMOCK_CONCAT_TOKEN_(name##Action, GMOCK_INTERNAL_COUNT_##value_params)
607
608$range k 0..n-1
609
610#define ACTION_TEMPLATE(name, template_params, value_params)\
611 template <GMOCK_INTERNAL_DECL_##template_params\
612 GMOCK_INTERNAL_DECL_TYPE_##value_params>\
613 class GMOCK_ACTION_CLASS_(name, value_params) {\
614 public:\
615 GMOCK_ACTION_CLASS_(name, value_params)\
616 GMOCK_INTERNAL_INIT_##value_params {}\
617 template <typename F>\
618 class gmock_Impl : public ::testing::ActionInterface<F> {\
619 public:\
620 typedef F function_type;\
621 typedef typename ::testing::internal::Function<F>::Result return_type;\
622 typedef typename ::testing::internal::Function<F>::ArgumentTuple\
623 args_type;\
624 explicit gmock_Impl GMOCK_INTERNAL_INIT_##value_params {}\
625 virtual return_type Perform(const args_type& args) {\
626 return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
627 Perform(this, args);\
628 }\
629 template <$for k, [[typename arg$k[[]]_type]]>\
630 return_type gmock_PerformImpl(const args_type& args[[]]
631$for k [[, arg$k[[]]_type arg$k]]) const;\
632 GMOCK_INTERNAL_DEFN_##value_params\
633 };\
634 template <typename F> operator ::testing::Action<F>() const {\
635 return ::testing::Action<F>(\
636 new gmock_Impl<F>(GMOCK_INTERNAL_LIST_##value_params));\
637 }\
638 GMOCK_INTERNAL_DEFN_##value_params\
639 };\
640 template <GMOCK_INTERNAL_DECL_##template_params\
641 GMOCK_INTERNAL_DECL_TYPE_##value_params>\
642 inline GMOCK_ACTION_CLASS_(name, value_params)<\
643 GMOCK_INTERNAL_LIST_##template_params\
644 GMOCK_INTERNAL_LIST_TYPE_##value_params> name(\
645 GMOCK_INTERNAL_DECL_##value_params) {\
646 return GMOCK_ACTION_CLASS_(name, value_params)<\
647 GMOCK_INTERNAL_LIST_##template_params\
648 GMOCK_INTERNAL_LIST_TYPE_##value_params>(\
649 GMOCK_INTERNAL_LIST_##value_params);\
650 }\
651 template <GMOCK_INTERNAL_DECL_##template_params\
652 GMOCK_INTERNAL_DECL_TYPE_##value_params>\
653 template <typename F>\
654 template <typename arg0_type, typename arg1_type, typename arg2_type,\
655 typename arg3_type, typename arg4_type, typename arg5_type,\
656 typename arg6_type, typename arg7_type, typename arg8_type,\
657 typename arg9_type>\
658 typename ::testing::internal::Function<F>::Result\
659 GMOCK_ACTION_CLASS_(name, value_params)<\
660 GMOCK_INTERNAL_LIST_##template_params\
661 GMOCK_INTERNAL_LIST_TYPE_##value_params>::gmock_Impl<F>::\
662 gmock_PerformImpl(\
663 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
664
shiqian326aa562009-01-09 21:43:57 +0000665$for i
666
667[[
668$var template = [[$if i==0 [[]] $else [[
669$range j 0..i-1
670
671 template <$for j, [[typename p$j##_type]]>\
672]]]]
673$var class_name = [[name##Action[[$if i==0 [[]] $elif i==1 [[P]]
674 $else [[P$i]]]]]]
675$range j 0..i-1
676$var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
677$var param_types_and_names = [[$for j, [[p$j##_type p$j]]]]
678$var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]]
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000679$var param_field_decls = [[$for j
shiqian326aa562009-01-09 21:43:57 +0000680[[
681
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000682 p$j##_type p$j;\
shiqian326aa562009-01-09 21:43:57 +0000683]]]]
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000684$var param_field_decls2 = [[$for j
shiqian326aa562009-01-09 21:43:57 +0000685[[
686
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000687 p$j##_type p$j;\
shiqian326aa562009-01-09 21:43:57 +0000688]]]]
689$var params = [[$for j, [[p$j]]]]
690$var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]]
shiqian326aa562009-01-09 21:43:57 +0000691$var typename_arg_types = [[$for k, [[typename arg$k[[]]_type]]]]
692$var arg_types_and_names = [[$for k, [[arg$k[[]]_type arg$k]]]]
693$var macro_name = [[$if i==0 [[ACTION]] $elif i==1 [[ACTION_P]]
694 $else [[ACTION_P$i]]]]
695
696#define $macro_name(name$for j [[, p$j]])\$template
697 class $class_name {\
698 public:\
699 $class_name($ctor_param_list)$inits {}\
700 template <typename F>\
701 class gmock_Impl : public ::testing::ActionInterface<F> {\
702 public:\
703 typedef F function_type;\
704 typedef typename ::testing::internal::Function<F>::Result return_type;\
705 typedef typename ::testing::internal::Function<F>::ArgumentTuple\
706 args_type;\
707 [[$if i==1 [[explicit ]]]]gmock_Impl($ctor_param_list)$inits {}\
708 virtual return_type Perform(const args_type& args) {\
709 return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
710 Perform(this, args);\
711 }\
712 template <$typename_arg_types>\
713 return_type gmock_PerformImpl(const args_type& args, [[]]
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000714$arg_types_and_names) const;\$param_field_decls
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
shiqian326aa562009-01-09 21:43:57 +0000719 };\$template
720 inline $class_name$param_types name($param_types_and_names) {\
721 return $class_name$param_types($params);\
722 }\$template
723 template <typename F>\
724 template <$typename_arg_types>\
725 typename ::testing::internal::Function<F>::Result\
zhanyong.wan33c0af02009-04-03 00:10:12 +0000726 $class_name$param_types::gmock_Impl<F>::gmock_PerformImpl(\
727 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
shiqian326aa562009-01-09 21:43:57 +0000728]]
zhanyong.wan7f4c2c02009-02-19 22:38:27 +0000729$$ } // This meta comment fixes auto-indentation in Emacs. It won't
730$$ // show up in the generated code.
shiqian326aa562009-01-09 21:43:57 +0000731
732
zhanyong.wan7f4c2c02009-02-19 22:38:27 +0000733// TODO(wan@google.com): move the following to a different .h file
734// such that we don't have to run 'pump' every time the code is
735// updated.
zhanyong.wane1cdce52009-02-06 01:09:43 +0000736namespace testing {
737
zhanyong.wan16cf4732009-05-14 20:55:30 +0000738// Various overloads for InvokeArgument<N>().
739//
740// The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th
741// (0-based) argument, which must be a k-ary callable, of the mock
742// function, with arguments a1, a2, ..., a_k.
743//
744// Notes:
745//
746// 1. The arguments are passed by value by default. If you need to
747// pass an argument by reference, wrap it inside ByRef(). For
748// example,
749//
750// InvokeArgument<1>(5, string("Hello"), ByRef(foo))
751//
752// passes 5 and string("Hello") by value, and passes foo by
753// reference.
754//
755// 2. If the callable takes an argument by reference but ByRef() is
756// not used, it will receive the reference to a copy of the value,
757// instead of the original value. For example, when the 0-th
758// argument of the mock function takes a const string&, the action
759//
760// InvokeArgument<0>(string("Hello"))
761//
762// makes a copy of the temporary string("Hello") object and passes a
763// reference of the copy, instead of the original temporary object,
764// to the callable. This makes it easy for a user to define an
765// InvokeArgument action from temporary values and have it performed
766// later.
zhanyong.wan7f4c2c02009-02-19 22:38:27 +0000767
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000768$range i 0..n
769$for i [[
zhanyong.wan16cf4732009-05-14 20:55:30 +0000770$range j 0..i-1
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000771
zhanyong.wan16cf4732009-05-14 20:55:30 +0000772ACTION_TEMPLATE(InvokeArgument,
773 HAS_1_TEMPLATE_PARAMS(int, k),
774 AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])) {
775 return internal::CallableHelper<return_type>::Call(
776 ::std::tr1::get<k>(args)$for j [[, p$j]]);
777}
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000778
779]]
780
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000781// Various overloads for ReturnNew<T>().
782//
783// The ReturnNew<T>(a1, a2, ..., a_k) action returns a pointer to a new
784// instance of type T, constructed on the heap with constructor arguments
785// a1, a2, ..., and a_k. The caller assumes ownership of the returned value.
786$range i 0..n
787$for i [[
zhanyong.wan16cf4732009-05-14 20:55:30 +0000788$range j 0..i-1
789$var ps = [[$for j, [[p$j]]]]
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000790
zhanyong.wan16cf4732009-05-14 20:55:30 +0000791ACTION_TEMPLATE(ReturnNew,
792 HAS_1_TEMPLATE_PARAMS(typename, T),
793 AND_$i[[]]_VALUE_PARAMS($ps)) {
794 return new T($ps);
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000795}
796
797]]
798
zhanyong.wane1cdce52009-02-06 01:09:43 +0000799} // namespace testing
800
shiqiane35fdd92008-12-10 05:08:54 +0000801#endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_