blob: 121c2c1bf5cc831e4940382047b3d5690860453d [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
3$$ gmock-generated-variadic-actions.h.
4$$
5$var n = 10 $$ The maximum arity we support.
6// Copyright 2007, Google Inc.
7// All rights reserved.
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13// * Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15// * Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following disclaimer
17// in the documentation and/or other materials provided with the
18// distribution.
19// * Neither the name of Google Inc. nor the names of its
20// contributors may be used to endorse or promote products derived from
21// this software without specific prior written permission.
22//
23// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34//
35// Author: wan@google.com (Zhanyong Wan)
36
37// Google Mock - a framework for writing C++ mock classes.
38//
39// This file implements some commonly used variadic actions.
40
41#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_
42#define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_
43
44#include <gmock/gmock-actions.h>
45#include <gmock/internal/gmock-port.h>
46
47namespace testing {
48namespace internal {
49
50// InvokeHelper<F> knows how to unpack an N-tuple and invoke an N-ary
51// function or method with the unpacked values, where F is a function
52// type that takes N arguments.
53template <typename Result, typename ArgumentTuple>
54class InvokeHelper;
55
56
57$range i 0..n
58$for i [[
59$range j 1..i
60$var types = [[$for j [[, typename A$j]]]]
61$var as = [[$for j, [[A$j]]]]
62$var args = [[$if i==0 [[]] $else [[ args]]]]
63$var import = [[$if i==0 [[]] $else [[
64 using ::std::tr1::get;
65
66]]]]
67$var gets = [[$for j, [[get<$(j - 1)>(args)]]]]
68template <typename R$types>
69class InvokeHelper<R, ::std::tr1::tuple<$as> > {
70 public:
71 template <typename Function>
72 static R Invoke(Function function, const ::std::tr1::tuple<$as>&$args) {
73$import return function($gets);
74 }
75
76 template <class Class, typename MethodPtr>
77 static R InvokeMethod(Class* obj_ptr,
78 MethodPtr method_ptr,
79 const ::std::tr1::tuple<$as>&$args) {
80$import return (obj_ptr->*method_ptr)($gets);
81 }
82};
83
84
85]]
86
87// Implements the Invoke(f) action. The template argument
88// FunctionImpl is the implementation type of f, which can be either a
89// function pointer or a functor. Invoke(f) can be used as an
90// Action<F> as long as f's type is compatible with F (i.e. f can be
91// assigned to a tr1::function<F>).
92template <typename FunctionImpl>
93class InvokeAction {
94 public:
95 // The c'tor makes a copy of function_impl (either a function
96 // pointer or a functor).
97 explicit InvokeAction(FunctionImpl function_impl)
98 : function_impl_(function_impl) {}
99
100 template <typename Result, typename ArgumentTuple>
101 Result Perform(const ArgumentTuple& args) {
102 return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args);
103 }
104 private:
105 FunctionImpl function_impl_;
106};
107
108// Implements the Invoke(object_ptr, &Class::Method) action.
109template <class Class, typename MethodPtr>
110class InvokeMethodAction {
111 public:
112 InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
113 : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
114
115 template <typename Result, typename ArgumentTuple>
116 Result Perform(const ArgumentTuple& args) const {
117 return InvokeHelper<Result, ArgumentTuple>::InvokeMethod(
118 obj_ptr_, method_ptr_, args);
119 }
120 private:
121 Class* const obj_ptr_;
122 const MethodPtr method_ptr_;
123};
124
125// A ReferenceWrapper<T> object represents a reference to type T,
126// which can be either const or not. It can be explicitly converted
127// from, and implicitly converted to, a T&. Unlike a reference,
128// ReferenceWrapper<T> can be copied and can survive template type
129// inference. This is used to support by-reference arguments in the
130// InvokeArgument<N>(...) action. The idea was from "reference
131// wrappers" in tr1, which we don't have in our source tree yet.
132template <typename T>
133class ReferenceWrapper {
134 public:
135 // Constructs a ReferenceWrapper<T> object from a T&.
136 explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT
137
138 // Allows a ReferenceWrapper<T> object to be implicitly converted to
139 // a T&.
140 operator T&() const { return *pointer_; }
141 private:
142 T* pointer_;
143};
144
145// CallableHelper has static methods for invoking "callables",
146// i.e. function pointers and functors. It uses overloading to
147// provide a uniform interface for invoking different kinds of
148// callables. In particular, you can use:
149//
150// CallableHelper<R>::Call(callable, a1, a2, ..., an)
151//
152// to invoke an n-ary callable, where R is its return type. If an
153// argument, say a2, needs to be passed by reference, you should write
154// ByRef(a2) instead of a2 in the above expression.
155template <typename R>
156class CallableHelper {
157 public:
158 // Calls a nullary callable.
159 template <typename Function>
160 static R Call(Function function) { return function(); }
161
162 // Calls a unary callable.
163
164 // We deliberately pass a1 by value instead of const reference here
165 // in case it is a C-string literal. If we had declared the
166 // parameter as 'const A1& a1' and write Call(function, "Hi"), the
167 // compiler would've thought A1 is 'char[3]', which causes trouble
168 // when you need to copy a value of type A1. By declaring the
169 // parameter as 'A1 a1', the compiler will correctly infer that A1
170 // is 'const char*' when it sees Call(function, "Hi").
171 //
172 // Since this function is defined inline, the compiler can get rid
173 // of the copying of the arguments. Therefore the performance won't
174 // be hurt.
175 template <typename Function, typename A1>
176 static R Call(Function function, A1 a1) { return function(a1); }
177
178$range i 2..n
179$for i
180[[
181$var arity = [[$if i==2 [[binary]] $elif i==3 [[ternary]] $else [[$i-ary]]]]
182
183 // Calls a $arity callable.
184
185$range j 1..i
186$var typename_As = [[$for j, [[typename A$j]]]]
187$var Aas = [[$for j, [[A$j a$j]]]]
188$var as = [[$for j, [[a$j]]]]
189$var typename_Ts = [[$for j, [[typename T$j]]]]
190$var Ts = [[$for j, [[T$j]]]]
191 template <typename Function, $typename_As>
192 static R Call(Function function, $Aas) {
193 return function($as);
194 }
195
196]]
197
198}; // class CallableHelper
199
200// Invokes a nullary callable argument.
201template <size_t N>
202class InvokeArgumentAction0 {
203 public:
204 template <typename Result, typename ArgumentTuple>
205 static Result Perform(const ArgumentTuple& args) {
206 return CallableHelper<Result>::Call(::std::tr1::get<N>(args));
207 }
208};
209
210// Invokes a unary callable argument with the given argument.
211template <size_t N, typename A1>
212class InvokeArgumentAction1 {
213 public:
214 // We deliberately pass a1 by value instead of const reference here
215 // in case it is a C-string literal.
216 //
217 // Since this function is defined inline, the compiler can get rid
218 // of the copying of the arguments. Therefore the performance won't
219 // be hurt.
220 explicit InvokeArgumentAction1(A1 a1) : arg1_(a1) {}
221
222 template <typename Result, typename ArgumentTuple>
223 Result Perform(const ArgumentTuple& args) {
224 return CallableHelper<Result>::Call(::std::tr1::get<N>(args), arg1_);
225 }
226 private:
227 const A1 arg1_;
228};
229
230$range i 2..n
231$for i [[
232$var arity = [[$if i==2 [[binary]] $elif i==3 [[ternary]] $else [[$i-ary]]]]
233$range j 1..i
234$var typename_As = [[$for j, [[typename A$j]]]]
235$var args_ = [[$for j, [[arg$j[[]]_]]]]
236
237// Invokes a $arity callable argument with the given arguments.
238template <size_t N, $typename_As>
239class InvokeArgumentAction$i {
240 public:
241 InvokeArgumentAction$i($for j, [[A$j a$j]]) :
242 $for j, [[arg$j[[]]_(a$j)]] {}
243
244 template <typename Result, typename ArgumentTuple>
245 Result Perform(const ArgumentTuple& args) {
246$if i <= 4 [[
247
248 return CallableHelper<Result>::Call(::std::tr1::get<N>(args), $args_);
249
250]] $else [[
251
252 // We extract the callable to a variable before invoking it, in
253 // case it is a functor passed by value and its operator() is not
254 // const.
255 typename ::std::tr1::tuple_element<N, ArgumentTuple>::type function =
256 ::std::tr1::get<N>(args);
257 return function($args_);
258
259]]
260 }
261 private:
262$for j [[
263
264 const A$j arg$j[[]]_;
265]]
266
267};
268
269]]
270
271// An INTERNAL macro for extracting the type of a tuple field. It's
272// subject to change without notice - DO NOT USE IN USER CODE!
273#define GMOCK_FIELD(Tuple, N) \
274 typename ::std::tr1::tuple_element<N, Tuple>::type
275
276$range i 1..n
277
278// SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::type is the
279// type of an n-ary function whose i-th (1-based) argument type is the
280// k{i}-th (0-based) field of ArgumentTuple, which must be a tuple
281// type, and whose return type is Result. For example,
282// SelectArgs<int, ::std::tr1::tuple<bool, char, double, long>, 0, 3>::type
283// is int(bool, long).
284//
285// SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::Select(args)
286// returns the selected fields (k1, k2, ..., k_n) of args as a tuple.
287// For example,
288// SelectArgs<int, ::std::tr1::tuple<bool, char, double>, 2, 0>::Select(
289// ::std::tr1::make_tuple(true, 'a', 2.5))
290// returns ::std::tr1::tuple (2.5, true).
291//
292// The numbers in list k1, k2, ..., k_n must be >= 0, where n can be
293// in the range [0, $n]. Duplicates are allowed and they don't have
294// to be in an ascending or descending order.
295
296template <typename Result, typename ArgumentTuple, $for i, [[int k$i]]>
297class SelectArgs {
298 public:
299 typedef Result type($for i, [[GMOCK_FIELD(ArgumentTuple, k$i)]]);
300 typedef typename Function<type>::ArgumentTuple SelectedArgs;
301 static SelectedArgs Select(const ArgumentTuple& args) {
302 using ::std::tr1::get;
303 return SelectedArgs($for i, [[get<k$i>(args)]]);
304 }
305};
306
307
308$for i [[
309$range j 1..n
310$range j1 1..i-1
311template <typename Result, typename ArgumentTuple$for j1[[, int k$j1]]>
312class SelectArgs<Result, ArgumentTuple,
313 $for j, [[$if j <= i-1 [[k$j]] $else [[-1]]]]> {
314 public:
315 typedef Result type($for j1, [[GMOCK_FIELD(ArgumentTuple, k$j1)]]);
316 typedef typename Function<type>::ArgumentTuple SelectedArgs;
317 static SelectedArgs Select(const ArgumentTuple& args) {
318 using ::std::tr1::get;
319 return SelectedArgs($for j1, [[get<k$j1>(args)]]);
320 }
321};
322
323
324]]
325#undef GMOCK_FIELD
326
327$var ks = [[$for i, [[k$i]]]]
328
329// Implements the WithArgs action.
330template <typename InnerAction, $for i, [[int k$i = -1]]>
331class WithArgsAction {
332 public:
333 explicit WithArgsAction(const InnerAction& action) : action_(action) {}
334
335 template <typename F>
336 operator Action<F>() const {
337 typedef typename Function<F>::Result Result;
338 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
339 typedef typename SelectArgs<Result, ArgumentTuple,
340 $ks>::type
341 InnerFunctionType;
342
343 class Impl : public ActionInterface<F> {
344 public:
345 explicit Impl(const InnerAction& action) : action_(action) {}
346
347 virtual Result Perform(const ArgumentTuple& args) {
348 return action_.Perform(SelectArgs<Result, ArgumentTuple, $ks>::Select(args));
349 }
350 private:
351 Action<InnerFunctionType> action_;
352 };
353
354 return MakeAction(new Impl(action_));
355 }
356 private:
357 const InnerAction action_;
358};
359
360// Does two actions sequentially. Used for implementing the DoAll(a1,
361// a2, ...) action.
362template <typename Action1, typename Action2>
363class DoBothAction {
364 public:
365 DoBothAction(Action1 action1, Action2 action2)
366 : action1_(action1), action2_(action2) {}
367
368 // This template type conversion operator allows DoAll(a1, ..., a_n)
369 // to be used in ANY function of compatible type.
370 template <typename F>
371 operator Action<F>() const {
372 typedef typename Function<F>::Result Result;
373 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
374 typedef typename Function<F>::MakeResultVoid VoidResult;
375
376 // Implements the DoAll(...) action for a particular function type F.
377 class Impl : public ActionInterface<F> {
378 public:
379 Impl(const Action<VoidResult>& action1, const Action<F>& action2)
380 : action1_(action1), action2_(action2) {}
381
382 virtual Result Perform(const ArgumentTuple& args) {
383 action1_.Perform(args);
384 return action2_.Perform(args);
385 }
386 private:
387 const Action<VoidResult> action1_;
388 const Action<F> action2_;
389 };
390
391 return Action<F>(new Impl(action1_, action2_));
392 }
393 private:
394 Action1 action1_;
395 Action2 action2_;
396};
397
shiqian326aa562009-01-09 21:43:57 +0000398// A macro from the ACTION* family (defined later in this file)
399// defines an action that can be used in a mock function. Typically,
400// these actions only care about a subset of the arguments of the mock
401// function. For example, if such an action only uses the second
402// argument, it can be used in any mock function that takes >= 2
403// arguments where the type of the second argument is compatible.
404//
405// Therefore, the action implementation must be prepared to take more
406// arguments than it needs. The ExcessiveArg type is used to
407// represent those excessive arguments. In order to keep the compiler
408// error messages tractable, we define it in the testing namespace
409// instead of testing::internal. However, this is an INTERNAL TYPE
410// and subject to change without notice, so a user MUST NOT USE THIS
411// TYPE DIRECTLY.
412struct ExcessiveArg {};
413
414// A helper class needed for implementing the ACTION* macros.
415template <typename Result, class Impl>
416class ActionHelper {
417 public:
418$range i 0..n
419$for i
420
421[[
422$var template = [[$if i==0 [[]] $else [[
423$range j 0..i-1
424 template <$for j, [[typename A$j]]>
425]]]]
426$range j 0..i-1
427$var As = [[$for j, [[A$j]]]]
428$var as = [[$for j, [[get<$j>(args)]]]]
429$range k 1..n-i
430$var eas = [[$for k, [[ExcessiveArg()]]]]
431$var arg_list = [[$if (i==0) | (i==n) [[$as$eas]] $else [[$as, $eas]]]]
432$template
433 static Result Perform(Impl* impl, const ::std::tr1::tuple<$As>& args) {
434 using ::std::tr1::get;
435 return impl->gmock_PerformImpl(args, $arg_list);
436 }
437
438]]
439};
440
shiqiane35fdd92008-12-10 05:08:54 +0000441} // namespace internal
442
443// Various overloads for Invoke().
444
445// Creates an action that invokes 'function_impl' with the mock
446// function's arguments.
447template <typename FunctionImpl>
448PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke(
449 FunctionImpl function_impl) {
450 return MakePolymorphicAction(
451 internal::InvokeAction<FunctionImpl>(function_impl));
452}
453
454// Creates an action that invokes the given method on the given object
455// with the mock function's arguments.
456template <class Class, typename MethodPtr>
457PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke(
458 Class* obj_ptr, MethodPtr method_ptr) {
459 return MakePolymorphicAction(
460 internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr));
461}
462
463// Creates a reference wrapper for the given L-value. If necessary,
464// you can explicitly specify the type of the reference. For example,
465// suppose 'derived' is an object of type Derived, ByRef(derived)
466// would wrap a Derived&. If you want to wrap a const Base& instead,
467// where Base is a base class of Derived, just write:
468//
469// ByRef<const Base>(derived)
470template <typename T>
471inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT
472 return internal::ReferenceWrapper<T>(l_value);
473}
474
475// Various overloads for InvokeArgument<N>().
476//
477// The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th
478// (0-based) argument, which must be a k-ary callable, of the mock
479// function, with arguments a1, a2, ..., a_k.
480//
481// Notes:
482//
483// 1. The arguments are passed by value by default. If you need to
484// pass an argument by reference, wrap it inside ByRef(). For
485// example,
486//
487// InvokeArgument<1>(5, string("Hello"), ByRef(foo))
488//
489// passes 5 and string("Hello") by value, and passes foo by
490// reference.
491//
492// 2. If the callable takes an argument by reference but ByRef() is
493// not used, it will receive the reference to a copy of the value,
494// instead of the original value. For example, when the 0-th
495// argument of the mock function takes a const string&, the action
496//
497// InvokeArgument<0>(string("Hello"))
498//
499// makes a copy of the temporary string("Hello") object and passes a
500// reference of the copy, instead of the original temporary object,
501// to the callable. This makes it easy for a user to define an
502// InvokeArgument action from temporary values and have it performed
503// later.
504template <size_t N>
505inline PolymorphicAction<internal::InvokeArgumentAction0<N> > InvokeArgument() {
506 return MakePolymorphicAction(internal::InvokeArgumentAction0<N>());
507}
508
509// We deliberately pass a1 by value instead of const reference here in
510// case it is a C-string literal. If we had declared the parameter as
511// 'const A1& a1' and write InvokeArgument<0>("Hi"), the compiler
512// would've thought A1 is 'char[3]', which causes trouble as the
513// implementation needs to copy a value of type A1. By declaring the
514// parameter as 'A1 a1', the compiler will correctly infer that A1 is
515// 'const char*' when it sees InvokeArgument<0>("Hi").
516//
517// Since this function is defined inline, the compiler can get rid of
518// the copying of the arguments. Therefore the performance won't be
519// hurt.
520template <size_t N, typename A1>
521inline PolymorphicAction<internal::InvokeArgumentAction1<N, A1> >
522InvokeArgument(A1 a1) {
523 return MakePolymorphicAction(internal::InvokeArgumentAction1<N, A1>(a1));
524}
525
526$range i 2..n
527$for i [[
528$range j 1..i
529$var typename_As = [[$for j, [[typename A$j]]]]
530$var As = [[$for j, [[A$j]]]]
531$var Aas = [[$for j, [[A$j a$j]]]]
532$var as = [[$for j, [[a$j]]]]
533
534template <size_t N, $typename_As>
535inline PolymorphicAction<internal::InvokeArgumentAction$i<N, $As> >
536InvokeArgument($Aas) {
537 return MakePolymorphicAction(
538 internal::InvokeArgumentAction$i<N, $As>($as));
539}
540
541]]
542
543// WithoutArgs(inner_action) can be used in a mock function with a
544// non-empty argument list to perform inner_action, which takes no
545// argument. In other words, it adapts an action accepting no
546// argument to one that accepts (and ignores) arguments.
547template <typename InnerAction>
548inline internal::WithArgsAction<InnerAction>
549WithoutArgs(const InnerAction& action) {
550 return internal::WithArgsAction<InnerAction>(action);
551}
552
553// WithArg<k>(an_action) creates an action that passes the k-th
554// (0-based) argument of the mock function to an_action and performs
555// it. It adapts an action accepting one argument to one that accepts
556// multiple arguments. For convenience, we also provide
557// WithArgs<k>(an_action) (defined below) as a synonym.
558template <int k, typename InnerAction>
559inline internal::WithArgsAction<InnerAction, k>
560WithArg(const InnerAction& action) {
561 return internal::WithArgsAction<InnerAction, k>(action);
562}
563
564// WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes
565// the selected arguments of the mock function to an_action and
566// performs it. It serves as an adaptor between actions with
567// different argument lists. C++ doesn't support default arguments for
568// function templates, so we have to overload it.
569
570$range i 1..n
571$for i [[
572$range j 1..i
573template <$for j [[int k$j, ]]typename InnerAction>
574inline internal::WithArgsAction<InnerAction$for j [[, k$j]]>
575WithArgs(const InnerAction& action) {
576 return internal::WithArgsAction<InnerAction$for j [[, k$j]]>(action);
577}
578
579
580]]
581// Creates an action that does actions a1, a2, ..., sequentially in
582// each invocation.
583$range i 2..n
584$for i [[
585$range j 2..i
586$var types = [[$for j, [[typename Action$j]]]]
587$var Aas = [[$for j [[, Action$j a$j]]]]
588
589template <typename Action1, $types>
590$range k 1..i-1
591
592inline $for k [[internal::DoBothAction<Action$k, ]]Action$i$for k [[>]]
593
594DoAll(Action1 a1$Aas) {
595$if i==2 [[
596
597 return internal::DoBothAction<Action1, Action2>(a1, a2);
598]] $else [[
599$range j2 2..i
600
601 return DoAll(a1, DoAll($for j2, [[a$j2]]));
602]]
603
604}
605
606]]
607
608} // namespace testing
609
shiqian326aa562009-01-09 21:43:57 +0000610// The ACTION* family of macros can be used in a namespace scope to
611// define custom actions easily. The syntax:
612//
613// ACTION(name) { statements; }
614//
615// will define an action with the given name that executes the
616// statements. The value returned by the statements will be used as
617// the return value of the action. Inside the statements, you can
618// refer to the K-th (0-based) argument of the mock function by
619// 'argK', and refer to its type by 'argK_type'. For example:
620//
621// ACTION(IncrementArg1) {
622// arg1_type temp = arg1;
623// return ++(*temp);
624// }
625//
626// allows you to write
627//
628// ...WillOnce(IncrementArg1());
629//
630// You can also refer to the entire argument tuple and its type by
631// 'args' and 'args_type', and refer to the mock function type and its
632// return type by 'function_type' and 'return_type'.
633//
634// Note that you don't need to specify the types of the mock function
635// arguments. However rest assured that your code is still type-safe:
636// you'll get a compiler error if *arg1 doesn't support the ++
637// operator, or if the type of ++(*arg1) isn't compatible with the
638// mock function's return type, for example.
639//
640// Sometimes you'll want to parameterize the action. For that you can use
641// another macro:
642//
643// ACTION_P(name, param_name) { statements; }
644//
645// For example:
646//
647// ACTION_P(Add, n) { return arg0 + n; }
648//
649// will allow you to write:
650//
651// ...WillOnce(Add(5));
652//
653// Note that you don't need to provide the type of the parameter
654// either. If you need to reference the type of a parameter named
655// 'foo', you can write 'foo_type'. For example, in the body of
656// ACTION_P(Add, n) above, you can write 'n_type' to refer to the type
657// of 'n'.
658//
659// We also provide ACTION_P2, ACTION_P3, ..., up to ACTION_P$n to support
660// multi-parameter actions.
661//
662// For the purpose of typing, you can view
663//
664// ACTION_Pk(Foo, p1, ..., pk) { ... }
665//
666// as shorthand for
667//
668// template <typename p1_type, ..., typename pk_type>
669// FooActionPk<p1_type, ..., pk_type> Foo(p1_type p1, ..., pk_type pk) { ... }
670//
671// In particular, you can provide the template type arguments
672// explicitly when invoking Foo(), as in Foo<long, bool>(5, false);
673// although usually you can rely on the compiler to infer the types
674// for you automatically. You can assign the result of expression
675// Foo(p1, ..., pk) to a variable of type FooActionPk<p1_type, ...,
676// pk_type>. This can be useful when composing actions.
677//
678// You can also overload actions with different numbers of parameters:
679//
680// ACTION_P(Plus, a) { ... }
681// ACTION_P2(Plus, a, b) { ... }
682//
683// While it's tempting to always use the ACTION* macros when defining
684// a new action, you should also consider implementing ActionInterface
685// or using MakePolymorphicAction() instead, especially if you need to
686// use the action a lot. While these approaches require more work,
687// they give you more control on the types of the mock function
688// arguments and the action parameters, which in general leads to
689// better compiler error messages that pay off in the long run. They
690// also allow overloading actions based on parameter types (as opposed
691// to just based on the number of parameters).
692//
693// CAVEAT:
694//
695// ACTION*() can only be used in a namespace scope. The reason is
696// that C++ doesn't yet allow function-local types to be used to
697// instantiate templates. The up-coming C++0x standard will fix this.
698// Once that's done, we'll consider supporting using ACTION*() inside
699// a function.
700//
701// MORE INFORMATION:
702//
703// To learn more about using these macros, please search for 'ACTION'
704// on http://code.google.com/p/googlemock/wiki/CookBook.
705
706$range i 0..n
707$for i
708
709[[
710$var template = [[$if i==0 [[]] $else [[
711$range j 0..i-1
712
713 template <$for j, [[typename p$j##_type]]>\
714]]]]
715$var class_name = [[name##Action[[$if i==0 [[]] $elif i==1 [[P]]
716 $else [[P$i]]]]]]
717$range j 0..i-1
718$var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
719$var param_types_and_names = [[$for j, [[p$j##_type p$j]]]]
720$var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]]
721$var const_param_field_decls = [[$for j
722[[
723
724 const p$j##_type p$j;\
725]]]]
726$var const_param_field_decls2 = [[$for j
727[[
728
729 const p$j##_type p$j;\
730]]]]
731$var params = [[$for j, [[p$j]]]]
732$var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]]
733$range k 0..n-1
734$var typename_arg_types = [[$for k, [[typename arg$k[[]]_type]]]]
735$var arg_types_and_names = [[$for k, [[arg$k[[]]_type arg$k]]]]
736$var macro_name = [[$if i==0 [[ACTION]] $elif i==1 [[ACTION_P]]
737 $else [[ACTION_P$i]]]]
738
739#define $macro_name(name$for j [[, p$j]])\$template
740 class $class_name {\
741 public:\
742 $class_name($ctor_param_list)$inits {}\
743 template <typename F>\
744 class gmock_Impl : public ::testing::ActionInterface<F> {\
745 public:\
746 typedef F function_type;\
747 typedef typename ::testing::internal::Function<F>::Result return_type;\
748 typedef typename ::testing::internal::Function<F>::ArgumentTuple\
749 args_type;\
750 [[$if i==1 [[explicit ]]]]gmock_Impl($ctor_param_list)$inits {}\
751 virtual return_type Perform(const args_type& args) {\
752 return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
753 Perform(this, args);\
754 }\
755 template <$typename_arg_types>\
756 return_type gmock_PerformImpl(const args_type& args, [[]]
757$arg_types_and_names) const;\$const_param_field_decls
758 };\
759 template <typename F> operator ::testing::Action<F>() const {\
760 return ::testing::Action<F>(new gmock_Impl<F>($params));\
761 }\$const_param_field_decls2
762 };\$template
763 inline $class_name$param_types name($param_types_and_names) {\
764 return $class_name$param_types($params);\
765 }\$template
766 template <typename F>\
767 template <$typename_arg_types>\
768 typename ::testing::internal::Function<F>::Result\
769 $class_name$param_types::\
770 gmock_Impl<F>::gmock_PerformImpl(const args_type& args, [[]]
771$arg_types_and_names) const
772]]
773
774
shiqiane35fdd92008-12-10 05:08:54 +0000775#endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_