blob: 001fd7d01b96f0e6e355447fd3a6fbac2f64f716 [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]]]]
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_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000231
232 GTEST_DISALLOW_ASSIGN_(WithArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000233};
234
shiqian326aa562009-01-09 21:43:57 +0000235// A macro from the ACTION* family (defined later in this file)
236// defines an action that can be used in a mock function. Typically,
237// these actions only care about a subset of the arguments of the mock
238// function. For example, if such an action only uses the second
239// argument, it can be used in any mock function that takes >= 2
240// arguments where the type of the second argument is compatible.
241//
242// Therefore, the action implementation must be prepared to take more
243// arguments than it needs. The ExcessiveArg type is used to
244// represent those excessive arguments. In order to keep the compiler
245// error messages tractable, we define it in the testing namespace
246// instead of testing::internal. However, this is an INTERNAL TYPE
247// and subject to change without notice, so a user MUST NOT USE THIS
248// TYPE DIRECTLY.
249struct ExcessiveArg {};
250
251// A helper class needed for implementing the ACTION* macros.
252template <typename Result, class Impl>
253class ActionHelper {
254 public:
255$range i 0..n
256$for i
257
258[[
259$var template = [[$if i==0 [[]] $else [[
260$range j 0..i-1
261 template <$for j, [[typename A$j]]>
262]]]]
263$range j 0..i-1
264$var As = [[$for j, [[A$j]]]]
265$var as = [[$for j, [[get<$j>(args)]]]]
266$range k 1..n-i
267$var eas = [[$for k, [[ExcessiveArg()]]]]
268$var arg_list = [[$if (i==0) | (i==n) [[$as$eas]] $else [[$as, $eas]]]]
269$template
270 static Result Perform(Impl* impl, const ::std::tr1::tuple<$As>& args) {
271 using ::std::tr1::get;
zhanyong.wan7f4c2c02009-02-19 22:38:27 +0000272 return impl->template gmock_PerformImpl<$As>(args, $arg_list);
shiqian326aa562009-01-09 21:43:57 +0000273 }
274
275]]
276};
277
shiqiane35fdd92008-12-10 05:08:54 +0000278} // namespace internal
279
280// Various overloads for Invoke().
281
shiqiane35fdd92008-12-10 05:08:54 +0000282// WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes
283// the selected arguments of the mock function to an_action and
284// performs it. It serves as an adaptor between actions with
285// different argument lists. C++ doesn't support default arguments for
286// function templates, so we have to overload it.
287
288$range i 1..n
289$for i [[
290$range j 1..i
291template <$for j [[int k$j, ]]typename InnerAction>
292inline internal::WithArgsAction<InnerAction$for j [[, k$j]]>
293WithArgs(const InnerAction& action) {
294 return internal::WithArgsAction<InnerAction$for j [[, k$j]]>(action);
295}
296
297
298]]
299// Creates an action that does actions a1, a2, ..., sequentially in
300// each invocation.
301$range i 2..n
302$for i [[
303$range j 2..i
304$var types = [[$for j, [[typename Action$j]]]]
305$var Aas = [[$for j [[, Action$j a$j]]]]
306
307template <typename Action1, $types>
308$range k 1..i-1
309
310inline $for k [[internal::DoBothAction<Action$k, ]]Action$i$for k [[>]]
311
312DoAll(Action1 a1$Aas) {
313$if i==2 [[
314
315 return internal::DoBothAction<Action1, Action2>(a1, a2);
316]] $else [[
317$range j2 2..i
318
319 return DoAll(a1, DoAll($for j2, [[a$j2]]));
320]]
321
322}
323
324]]
325
326} // namespace testing
327
shiqian326aa562009-01-09 21:43:57 +0000328// The ACTION* family of macros can be used in a namespace scope to
329// define custom actions easily. The syntax:
330//
331// ACTION(name) { statements; }
332//
333// will define an action with the given name that executes the
334// statements. The value returned by the statements will be used as
335// the return value of the action. Inside the statements, you can
336// refer to the K-th (0-based) argument of the mock function by
337// 'argK', and refer to its type by 'argK_type'. For example:
338//
339// ACTION(IncrementArg1) {
340// arg1_type temp = arg1;
341// return ++(*temp);
342// }
343//
344// allows you to write
345//
346// ...WillOnce(IncrementArg1());
347//
348// You can also refer to the entire argument tuple and its type by
349// 'args' and 'args_type', and refer to the mock function type and its
350// return type by 'function_type' and 'return_type'.
351//
352// Note that you don't need to specify the types of the mock function
353// arguments. However rest assured that your code is still type-safe:
354// you'll get a compiler error if *arg1 doesn't support the ++
355// operator, or if the type of ++(*arg1) isn't compatible with the
356// mock function's return type, for example.
357//
358// Sometimes you'll want to parameterize the action. For that you can use
359// another macro:
360//
361// ACTION_P(name, param_name) { statements; }
362//
363// For example:
364//
365// ACTION_P(Add, n) { return arg0 + n; }
366//
367// will allow you to write:
368//
369// ...WillOnce(Add(5));
370//
371// Note that you don't need to provide the type of the parameter
372// either. If you need to reference the type of a parameter named
373// 'foo', you can write 'foo_type'. For example, in the body of
374// ACTION_P(Add, n) above, you can write 'n_type' to refer to the type
375// of 'n'.
376//
377// We also provide ACTION_P2, ACTION_P3, ..., up to ACTION_P$n to support
378// multi-parameter actions.
379//
380// For the purpose of typing, you can view
381//
382// ACTION_Pk(Foo, p1, ..., pk) { ... }
383//
384// as shorthand for
385//
386// template <typename p1_type, ..., typename pk_type>
387// FooActionPk<p1_type, ..., pk_type> Foo(p1_type p1, ..., pk_type pk) { ... }
388//
389// In particular, you can provide the template type arguments
390// explicitly when invoking Foo(), as in Foo<long, bool>(5, false);
391// although usually you can rely on the compiler to infer the types
392// for you automatically. You can assign the result of expression
393// Foo(p1, ..., pk) to a variable of type FooActionPk<p1_type, ...,
394// pk_type>. This can be useful when composing actions.
395//
396// You can also overload actions with different numbers of parameters:
397//
398// ACTION_P(Plus, a) { ... }
399// ACTION_P2(Plus, a, b) { ... }
400//
401// While it's tempting to always use the ACTION* macros when defining
402// a new action, you should also consider implementing ActionInterface
403// or using MakePolymorphicAction() instead, especially if you need to
404// use the action a lot. While these approaches require more work,
405// they give you more control on the types of the mock function
406// arguments and the action parameters, which in general leads to
407// better compiler error messages that pay off in the long run. They
408// also allow overloading actions based on parameter types (as opposed
409// to just based on the number of parameters).
410//
411// CAVEAT:
412//
413// ACTION*() can only be used in a namespace scope. The reason is
414// that C++ doesn't yet allow function-local types to be used to
415// instantiate templates. The up-coming C++0x standard will fix this.
416// Once that's done, we'll consider supporting using ACTION*() inside
417// a function.
418//
419// MORE INFORMATION:
420//
421// To learn more about using these macros, please search for 'ACTION'
422// on http://code.google.com/p/googlemock/wiki/CookBook.
423
424$range i 0..n
zhanyong.wan33c0af02009-04-03 00:10:12 +0000425$range k 0..n-1
426
427// An internal macro needed for implementing ACTION*().
428#define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_\
429 const args_type& args GTEST_ATTRIBUTE_UNUSED_
430$for k [[,\
431 arg$k[[]]_type arg$k GTEST_ATTRIBUTE_UNUSED_]]
432
433
zhanyong.wan18490652009-05-11 18:54:08 +0000434// Sometimes you want to give an action explicit template parameters
435// that cannot be inferred from its value parameters. ACTION() and
436// ACTION_P*() don't support that. ACTION_TEMPLATE() remedies that
437// and can be viewed as an extension to ACTION() and ACTION_P*().
438//
439// The syntax:
440//
441// ACTION_TEMPLATE(ActionName,
442// HAS_m_TEMPLATE_PARAMS(kind1, name1, ..., kind_m, name_m),
443// AND_n_VALUE_PARAMS(p1, ..., p_n)) { statements; }
444//
445// defines an action template that takes m explicit template
446// parameters and n value parameters. name_i is the name of the i-th
447// template parameter, and kind_i specifies whether it's a typename,
448// an integral constant, or a template. p_i is the name of the i-th
449// value parameter.
450//
451// Example:
452//
453// // DuplicateArg<k, T>(output) converts the k-th argument of the mock
454// // function to type T and copies it to *output.
455// ACTION_TEMPLATE(DuplicateArg,
456// HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
457// AND_1_VALUE_PARAMS(output)) {
458// *output = T(std::tr1::get<k>(args));
459// }
460// ...
461// int n;
462// EXPECT_CALL(mock, Foo(_, _))
463// .WillOnce(DuplicateArg<1, unsigned char>(&n));
464//
465// To create an instance of an action template, write:
466//
467// ActionName<t1, ..., t_m>(v1, ..., v_n)
468//
469// where the ts are the template arguments and the vs are the value
470// arguments. The value argument types are inferred by the compiler.
471// If you want to explicitly specify the value argument types, you can
472// provide additional template arguments:
473//
474// ActionName<t1, ..., t_m, u1, ..., u_k>(v1, ..., v_n)
475//
476// where u_i is the desired type of v_i.
477//
478// ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded on the
479// number of value parameters, but not on the number of template
480// parameters. Without the restriction, the meaning of the following
481// is unclear:
482//
483// OverloadedAction<int, bool>(x);
484//
485// Are we using a single-template-parameter action where 'bool' refers
486// to the type of x, or are we using a two-template-parameter action
487// where the compiler is asked to infer the type of x?
488//
489// Implementation notes:
490//
491// GMOCK_INTERNAL_*_HAS_m_TEMPLATE_PARAMS and
492// GMOCK_INTERNAL_*_AND_n_VALUE_PARAMS are internal macros for
493// implementing ACTION_TEMPLATE. The main trick we use is to create
494// new macro invocations when expanding a macro. For example, we have
495//
496// #define ACTION_TEMPLATE(name, template_params, value_params)
497// ... GMOCK_INTERNAL_DECL_##template_params ...
498//
499// which causes ACTION_TEMPLATE(..., HAS_1_TEMPLATE_PARAMS(typename, T), ...)
500// to expand to
501//
502// ... GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(typename, T) ...
503//
504// Since GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS is a macro, the
505// preprocessor will continue to expand it to
506//
507// ... typename T ...
508//
509// This technique conforms to the C++ standard and is portable. It
510// allows us to implement action templates using O(N) code, where N is
511// the maximum number of template/value parameters supported. Without
512// using it, we'd have to devote O(N^2) amount of code to implement all
513// combinations of m and n.
514
515// Declares the template parameters.
516
517$range j 1..n
518$for j [[
519$range m 0..j-1
520#define GMOCK_INTERNAL_DECL_HAS_$j[[]]
521_TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[kind$m name$m]]
522
523
524]]
525
526// Lists the template parameters.
527
528$for j [[
529$range m 0..j-1
530#define GMOCK_INTERNAL_LIST_HAS_$j[[]]
531_TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[name$m]]
532
533
534]]
535
536// Declares the types of value parameters.
537
538$for i [[
539$range j 0..i-1
540#define GMOCK_INTERNAL_DECL_TYPE_AND_$i[[]]
541_VALUE_PARAMS($for j, [[p$j]]) $for j [[, typename p$j##_type]]
542
543
544]]
545
546// Initializes the value parameters.
547
548$for i [[
549$range j 0..i-1
550#define GMOCK_INTERNAL_INIT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])\
551 ($for j, [[p$j##_type gmock_p$j]])$if i>0 [[ : ]]$for j, [[p$j(gmock_p$j)]]
552
553
554]]
555
556// Declares the fields for storing the value parameters.
557
558$for i [[
559$range j 0..i-1
560#define GMOCK_INTERNAL_DEFN_AND_$i[[]]
561_VALUE_PARAMS($for j, [[p$j]]) $for j [[p$j##_type p$j; ]]
562
563
564]]
565
566// Lists the value parameters.
567
568$for i [[
569$range j 0..i-1
570#define GMOCK_INTERNAL_LIST_AND_$i[[]]
571_VALUE_PARAMS($for j, [[p$j]]) $for j, [[p$j]]
572
573
574]]
575
576// Lists the value parameter types.
577
578$for i [[
579$range j 0..i-1
580#define GMOCK_INTERNAL_LIST_TYPE_AND_$i[[]]
581_VALUE_PARAMS($for j, [[p$j]]) $for j [[, p$j##_type]]
582
583
584]]
585
586// Declares the value parameters.
587
588$for i [[
589$range j 0..i-1
590#define GMOCK_INTERNAL_DECL_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
591$for j, [[p$j##_type p$j]]
592
593
594]]
595
596// The suffix of the class template implementing the action template.
597$for i [[
598
599
600$range j 0..i-1
601#define GMOCK_INTERNAL_COUNT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
602$if i==1 [[P]] $elif i>=2 [[P$i]]
603]]
604
605
606// The name of the class template implementing the action template.
607#define GMOCK_ACTION_CLASS_(name, value_params)\
zhanyong.wanccedc1c2010-08-09 22:46:12 +0000608 GTEST_CONCAT_TOKEN_(name##Action, GMOCK_INTERNAL_COUNT_##value_params)
zhanyong.wan18490652009-05-11 18:54:08 +0000609
610$range k 0..n-1
611
612#define ACTION_TEMPLATE(name, template_params, value_params)\
613 template <GMOCK_INTERNAL_DECL_##template_params\
614 GMOCK_INTERNAL_DECL_TYPE_##value_params>\
615 class GMOCK_ACTION_CLASS_(name, value_params) {\
616 public:\
617 GMOCK_ACTION_CLASS_(name, value_params)\
618 GMOCK_INTERNAL_INIT_##value_params {}\
619 template <typename F>\
620 class gmock_Impl : public ::testing::ActionInterface<F> {\
621 public:\
622 typedef F function_type;\
623 typedef typename ::testing::internal::Function<F>::Result return_type;\
624 typedef typename ::testing::internal::Function<F>::ArgumentTuple\
625 args_type;\
626 explicit gmock_Impl GMOCK_INTERNAL_INIT_##value_params {}\
627 virtual return_type Perform(const args_type& args) {\
628 return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
629 Perform(this, args);\
630 }\
631 template <$for k, [[typename arg$k[[]]_type]]>\
632 return_type gmock_PerformImpl(const args_type& args[[]]
633$for k [[, arg$k[[]]_type arg$k]]) const;\
634 GMOCK_INTERNAL_DEFN_##value_params\
zhanyong.wan32de5f52009-12-23 00:13:23 +0000635 private:\
636 GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
zhanyong.wan18490652009-05-11 18:54:08 +0000637 };\
638 template <typename F> operator ::testing::Action<F>() const {\
639 return ::testing::Action<F>(\
640 new gmock_Impl<F>(GMOCK_INTERNAL_LIST_##value_params));\
641 }\
642 GMOCK_INTERNAL_DEFN_##value_params\
zhanyong.wan32de5f52009-12-23 00:13:23 +0000643 private:\
644 GTEST_DISALLOW_ASSIGN_(GMOCK_ACTION_CLASS_(name, value_params));\
zhanyong.wan18490652009-05-11 18:54:08 +0000645 };\
646 template <GMOCK_INTERNAL_DECL_##template_params\
647 GMOCK_INTERNAL_DECL_TYPE_##value_params>\
648 inline GMOCK_ACTION_CLASS_(name, value_params)<\
649 GMOCK_INTERNAL_LIST_##template_params\
650 GMOCK_INTERNAL_LIST_TYPE_##value_params> name(\
651 GMOCK_INTERNAL_DECL_##value_params) {\
652 return GMOCK_ACTION_CLASS_(name, value_params)<\
653 GMOCK_INTERNAL_LIST_##template_params\
654 GMOCK_INTERNAL_LIST_TYPE_##value_params>(\
655 GMOCK_INTERNAL_LIST_##value_params);\
656 }\
657 template <GMOCK_INTERNAL_DECL_##template_params\
658 GMOCK_INTERNAL_DECL_TYPE_##value_params>\
659 template <typename F>\
660 template <typename arg0_type, typename arg1_type, typename arg2_type,\
661 typename arg3_type, typename arg4_type, typename arg5_type,\
662 typename arg6_type, typename arg7_type, typename arg8_type,\
663 typename arg9_type>\
664 typename ::testing::internal::Function<F>::Result\
665 GMOCK_ACTION_CLASS_(name, value_params)<\
666 GMOCK_INTERNAL_LIST_##template_params\
667 GMOCK_INTERNAL_LIST_TYPE_##value_params>::gmock_Impl<F>::\
668 gmock_PerformImpl(\
669 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
670
shiqian326aa562009-01-09 21:43:57 +0000671$for i
672
673[[
674$var template = [[$if i==0 [[]] $else [[
675$range j 0..i-1
676
677 template <$for j, [[typename p$j##_type]]>\
678]]]]
679$var class_name = [[name##Action[[$if i==0 [[]] $elif i==1 [[P]]
680 $else [[P$i]]]]]]
681$range j 0..i-1
682$var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
683$var param_types_and_names = [[$for j, [[p$j##_type p$j]]]]
684$var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]]
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000685$var param_field_decls = [[$for j
shiqian326aa562009-01-09 21:43:57 +0000686[[
687
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000688 p$j##_type p$j;\
shiqian326aa562009-01-09 21:43:57 +0000689]]]]
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000690$var param_field_decls2 = [[$for j
shiqian326aa562009-01-09 21:43:57 +0000691[[
692
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000693 p$j##_type p$j;\
shiqian326aa562009-01-09 21:43:57 +0000694]]]]
695$var params = [[$for j, [[p$j]]]]
696$var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]]
shiqian326aa562009-01-09 21:43:57 +0000697$var typename_arg_types = [[$for k, [[typename arg$k[[]]_type]]]]
698$var arg_types_and_names = [[$for k, [[arg$k[[]]_type arg$k]]]]
699$var macro_name = [[$if i==0 [[ACTION]] $elif i==1 [[ACTION_P]]
700 $else [[ACTION_P$i]]]]
701
702#define $macro_name(name$for j [[, p$j]])\$template
703 class $class_name {\
704 public:\
705 $class_name($ctor_param_list)$inits {}\
706 template <typename F>\
707 class gmock_Impl : public ::testing::ActionInterface<F> {\
708 public:\
709 typedef F function_type;\
710 typedef typename ::testing::internal::Function<F>::Result return_type;\
711 typedef typename ::testing::internal::Function<F>::ArgumentTuple\
712 args_type;\
713 [[$if i==1 [[explicit ]]]]gmock_Impl($ctor_param_list)$inits {}\
714 virtual return_type Perform(const args_type& args) {\
715 return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
716 Perform(this, args);\
717 }\
718 template <$typename_arg_types>\
719 return_type gmock_PerformImpl(const args_type& args, [[]]
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000720$arg_types_and_names) const;\$param_field_decls
zhanyong.wan32de5f52009-12-23 00:13:23 +0000721 private:\
722 GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
shiqian326aa562009-01-09 21:43:57 +0000723 };\
724 template <typename F> operator ::testing::Action<F>() const {\
725 return ::testing::Action<F>(new gmock_Impl<F>($params));\
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000726 }\$param_field_decls2
zhanyong.wan32de5f52009-12-23 00:13:23 +0000727 private:\
728 GTEST_DISALLOW_ASSIGN_($class_name);\
shiqian326aa562009-01-09 21:43:57 +0000729 };\$template
730 inline $class_name$param_types name($param_types_and_names) {\
731 return $class_name$param_types($params);\
732 }\$template
733 template <typename F>\
734 template <$typename_arg_types>\
735 typename ::testing::internal::Function<F>::Result\
zhanyong.wan33c0af02009-04-03 00:10:12 +0000736 $class_name$param_types::gmock_Impl<F>::gmock_PerformImpl(\
737 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
shiqian326aa562009-01-09 21:43:57 +0000738]]
zhanyong.wan7f4c2c02009-02-19 22:38:27 +0000739$$ } // This meta comment fixes auto-indentation in Emacs. It won't
740$$ // show up in the generated code.
shiqian326aa562009-01-09 21:43:57 +0000741
742
zhanyong.wan7f4c2c02009-02-19 22:38:27 +0000743// TODO(wan@google.com): move the following to a different .h file
744// such that we don't have to run 'pump' every time the code is
745// updated.
zhanyong.wane1cdce52009-02-06 01:09:43 +0000746namespace testing {
747
zhanyong.wan32de5f52009-12-23 00:13:23 +0000748// The ACTION*() macros trigger warning C4100 (unreferenced formal
749// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
750// the macro definition, as the warnings are generated when the macro
751// is expanded and macro expansion cannot contain #pragma. Therefore
752// we suppress them here.
753#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000754# pragma warning(push)
755# pragma warning(disable:4100)
zhanyong.wan32de5f52009-12-23 00:13:23 +0000756#endif
757
zhanyong.wan16cf4732009-05-14 20:55:30 +0000758// Various overloads for InvokeArgument<N>().
759//
760// The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th
761// (0-based) argument, which must be a k-ary callable, of the mock
762// function, with arguments a1, a2, ..., a_k.
763//
764// Notes:
765//
766// 1. The arguments are passed by value by default. If you need to
767// pass an argument by reference, wrap it inside ByRef(). For
768// example,
769//
770// InvokeArgument<1>(5, string("Hello"), ByRef(foo))
771//
772// passes 5 and string("Hello") by value, and passes foo by
773// reference.
774//
775// 2. If the callable takes an argument by reference but ByRef() is
776// not used, it will receive the reference to a copy of the value,
777// instead of the original value. For example, when the 0-th
778// argument of the mock function takes a const string&, the action
779//
780// InvokeArgument<0>(string("Hello"))
781//
782// makes a copy of the temporary string("Hello") object and passes a
783// reference of the copy, instead of the original temporary object,
784// to the callable. This makes it easy for a user to define an
785// InvokeArgument action from temporary values and have it performed
786// later.
zhanyong.wan7f4c2c02009-02-19 22:38:27 +0000787
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000788$range i 0..n
789$for i [[
zhanyong.wan16cf4732009-05-14 20:55:30 +0000790$range j 0..i-1
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000791
zhanyong.wan16cf4732009-05-14 20:55:30 +0000792ACTION_TEMPLATE(InvokeArgument,
793 HAS_1_TEMPLATE_PARAMS(int, k),
794 AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])) {
795 return internal::CallableHelper<return_type>::Call(
796 ::std::tr1::get<k>(args)$for j [[, p$j]]);
797}
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000798
799]]
800
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000801// Various overloads for ReturnNew<T>().
802//
803// The ReturnNew<T>(a1, a2, ..., a_k) action returns a pointer to a new
804// instance of type T, constructed on the heap with constructor arguments
805// a1, a2, ..., and a_k. The caller assumes ownership of the returned value.
806$range i 0..n
807$for i [[
zhanyong.wan16cf4732009-05-14 20:55:30 +0000808$range j 0..i-1
809$var ps = [[$for j, [[p$j]]]]
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000810
zhanyong.wan16cf4732009-05-14 20:55:30 +0000811ACTION_TEMPLATE(ReturnNew,
812 HAS_1_TEMPLATE_PARAMS(typename, T),
813 AND_$i[[]]_VALUE_PARAMS($ps)) {
814 return new T($ps);
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000815}
816
817]]
818
zhanyong.wan32de5f52009-12-23 00:13:23 +0000819#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000820# pragma warning(pop)
zhanyong.wan32de5f52009-12-23 00:13:23 +0000821#endif
822
zhanyong.wane1cdce52009-02-06 01:09:43 +0000823} // namespace testing
824
shiqiane35fdd92008-12-10 05:08:54 +0000825#endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_