blob: 39f80804d802b6a0f31e6b6cb7e216864c719ee7 [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.
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]]
87
88// Implements the Invoke(f) action. The template argument
89// FunctionImpl is the implementation type of f, which can be either a
90// function pointer or a functor. Invoke(f) can be used as an
91// Action<F> as long as f's type is compatible with F (i.e. f can be
92// assigned to a tr1::function<F>).
93template <typename FunctionImpl>
94class InvokeAction {
95 public:
96 // The c'tor makes a copy of function_impl (either a function
97 // pointer or a functor).
98 explicit InvokeAction(FunctionImpl function_impl)
99 : function_impl_(function_impl) {}
100
101 template <typename Result, typename ArgumentTuple>
102 Result Perform(const ArgumentTuple& args) {
103 return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args);
104 }
105 private:
106 FunctionImpl function_impl_;
107};
108
109// Implements the Invoke(object_ptr, &Class::Method) action.
110template <class Class, typename MethodPtr>
111class InvokeMethodAction {
112 public:
113 InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
114 : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
115
116 template <typename Result, typename ArgumentTuple>
117 Result Perform(const ArgumentTuple& args) const {
118 return InvokeHelper<Result, ArgumentTuple>::InvokeMethod(
119 obj_ptr_, method_ptr_, args);
120 }
121 private:
122 Class* const obj_ptr_;
123 const MethodPtr method_ptr_;
124};
125
126// A ReferenceWrapper<T> object represents a reference to type T,
127// which can be either const or not. It can be explicitly converted
128// from, and implicitly converted to, a T&. Unlike a reference,
129// ReferenceWrapper<T> can be copied and can survive template type
130// inference. This is used to support by-reference arguments in the
131// InvokeArgument<N>(...) action. The idea was from "reference
132// wrappers" in tr1, which we don't have in our source tree yet.
133template <typename T>
134class ReferenceWrapper {
135 public:
136 // Constructs a ReferenceWrapper<T> object from a T&.
137 explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT
138
139 // Allows a ReferenceWrapper<T> object to be implicitly converted to
140 // a T&.
141 operator T&() const { return *pointer_; }
142 private:
143 T* pointer_;
144};
145
146// CallableHelper has static methods for invoking "callables",
147// i.e. function pointers and functors. It uses overloading to
148// provide a uniform interface for invoking different kinds of
149// callables. In particular, you can use:
150//
151// CallableHelper<R>::Call(callable, a1, a2, ..., an)
152//
153// to invoke an n-ary callable, where R is its return type. If an
154// argument, say a2, needs to be passed by reference, you should write
155// ByRef(a2) instead of a2 in the above expression.
156template <typename R>
157class CallableHelper {
158 public:
159 // Calls a nullary callable.
160 template <typename Function>
161 static R Call(Function function) { return function(); }
162
163 // Calls a unary callable.
164
165 // We deliberately pass a1 by value instead of const reference here
166 // in case it is a C-string literal. If we had declared the
167 // parameter as 'const A1& a1' and write Call(function, "Hi"), the
168 // compiler would've thought A1 is 'char[3]', which causes trouble
169 // when you need to copy a value of type A1. By declaring the
170 // parameter as 'A1 a1', the compiler will correctly infer that A1
171 // is 'const char*' when it sees Call(function, "Hi").
172 //
173 // Since this function is defined inline, the compiler can get rid
174 // of the copying of the arguments. Therefore the performance won't
175 // be hurt.
176 template <typename Function, typename A1>
177 static R Call(Function function, A1 a1) { return function(a1); }
178
179$range i 2..n
180$for i
181[[
182$var arity = [[$if i==2 [[binary]] $elif i==3 [[ternary]] $else [[$i-ary]]]]
183
184 // Calls a $arity callable.
185
186$range j 1..i
187$var typename_As = [[$for j, [[typename A$j]]]]
188$var Aas = [[$for j, [[A$j a$j]]]]
189$var as = [[$for j, [[a$j]]]]
190$var typename_Ts = [[$for j, [[typename T$j]]]]
191$var Ts = [[$for j, [[T$j]]]]
192 template <typename Function, $typename_As>
193 static R Call(Function function, $Aas) {
194 return function($as);
195 }
196
197]]
198
199}; // class CallableHelper
200
201// Invokes a nullary callable argument.
202template <size_t N>
203class InvokeArgumentAction0 {
204 public:
205 template <typename Result, typename ArgumentTuple>
206 static Result Perform(const ArgumentTuple& args) {
207 return CallableHelper<Result>::Call(::std::tr1::get<N>(args));
208 }
209};
210
211// Invokes a unary callable argument with the given argument.
212template <size_t N, typename A1>
213class InvokeArgumentAction1 {
214 public:
215 // We deliberately pass a1 by value instead of const reference here
216 // in case it is a C-string literal.
217 //
218 // Since this function is defined inline, the compiler can get rid
219 // of the copying of the arguments. Therefore the performance won't
220 // be hurt.
221 explicit InvokeArgumentAction1(A1 a1) : arg1_(a1) {}
222
223 template <typename Result, typename ArgumentTuple>
224 Result Perform(const ArgumentTuple& args) {
225 return CallableHelper<Result>::Call(::std::tr1::get<N>(args), arg1_);
226 }
227 private:
228 const A1 arg1_;
229};
230
231$range i 2..n
232$for i [[
233$var arity = [[$if i==2 [[binary]] $elif i==3 [[ternary]] $else [[$i-ary]]]]
234$range j 1..i
235$var typename_As = [[$for j, [[typename A$j]]]]
236$var args_ = [[$for j, [[arg$j[[]]_]]]]
237
238// Invokes a $arity callable argument with the given arguments.
239template <size_t N, $typename_As>
240class InvokeArgumentAction$i {
241 public:
242 InvokeArgumentAction$i($for j, [[A$j a$j]]) :
243 $for j, [[arg$j[[]]_(a$j)]] {}
244
245 template <typename Result, typename ArgumentTuple>
246 Result Perform(const ArgumentTuple& args) {
247$if i <= 4 [[
248
249 return CallableHelper<Result>::Call(::std::tr1::get<N>(args), $args_);
250
251]] $else [[
252
253 // We extract the callable to a variable before invoking it, in
254 // case it is a functor passed by value and its operator() is not
255 // const.
256 typename ::std::tr1::tuple_element<N, ArgumentTuple>::type function =
257 ::std::tr1::get<N>(args);
258 return function($args_);
259
260]]
261 }
262 private:
263$for j [[
264
265 const A$j arg$j[[]]_;
266]]
267
268};
269
270]]
271
272// An INTERNAL macro for extracting the type of a tuple field. It's
273// subject to change without notice - DO NOT USE IN USER CODE!
zhanyong.wane0d051e2009-02-19 00:33:37 +0000274#define GMOCK_FIELD_(Tuple, N) \
shiqiane35fdd92008-12-10 05:08:54 +0000275 typename ::std::tr1::tuple_element<N, Tuple>::type
276
277$range i 1..n
278
279// SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::type is the
280// type of an n-ary function whose i-th (1-based) argument type is the
281// k{i}-th (0-based) field of ArgumentTuple, which must be a tuple
282// type, and whose return type is Result. For example,
283// SelectArgs<int, ::std::tr1::tuple<bool, char, double, long>, 0, 3>::type
284// is int(bool, long).
285//
286// SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::Select(args)
287// returns the selected fields (k1, k2, ..., k_n) of args as a tuple.
288// For example,
289// SelectArgs<int, ::std::tr1::tuple<bool, char, double>, 2, 0>::Select(
290// ::std::tr1::make_tuple(true, 'a', 2.5))
291// returns ::std::tr1::tuple (2.5, true).
292//
293// The numbers in list k1, k2, ..., k_n must be >= 0, where n can be
294// in the range [0, $n]. Duplicates are allowed and they don't have
295// to be in an ascending or descending order.
296
297template <typename Result, typename ArgumentTuple, $for i, [[int k$i]]>
298class SelectArgs {
299 public:
zhanyong.wane0d051e2009-02-19 00:33:37 +0000300 typedef Result type($for i, [[GMOCK_FIELD_(ArgumentTuple, k$i)]]);
shiqiane35fdd92008-12-10 05:08:54 +0000301 typedef typename Function<type>::ArgumentTuple SelectedArgs;
302 static SelectedArgs Select(const ArgumentTuple& args) {
303 using ::std::tr1::get;
304 return SelectedArgs($for i, [[get<k$i>(args)]]);
305 }
306};
307
308
309$for i [[
310$range j 1..n
311$range j1 1..i-1
312template <typename Result, typename ArgumentTuple$for j1[[, int k$j1]]>
313class SelectArgs<Result, ArgumentTuple,
314 $for j, [[$if j <= i-1 [[k$j]] $else [[-1]]]]> {
315 public:
zhanyong.wane0d051e2009-02-19 00:33:37 +0000316 typedef Result type($for j1, [[GMOCK_FIELD_(ArgumentTuple, k$j1)]]);
shiqiane35fdd92008-12-10 05:08:54 +0000317 typedef typename Function<type>::ArgumentTuple SelectedArgs;
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000318 static SelectedArgs Select(const ArgumentTuple& [[]]
319$if i == 1 [[/* args */]] $else [[args]]) {
shiqiane35fdd92008-12-10 05:08:54 +0000320 using ::std::tr1::get;
321 return SelectedArgs($for j1, [[get<k$j1>(args)]]);
322 }
323};
324
325
326]]
zhanyong.wane0d051e2009-02-19 00:33:37 +0000327#undef GMOCK_FIELD_
shiqiane35fdd92008-12-10 05:08:54 +0000328
329$var ks = [[$for i, [[k$i]]]]
330
331// Implements the WithArgs action.
332template <typename InnerAction, $for i, [[int k$i = -1]]>
333class WithArgsAction {
334 public:
335 explicit WithArgsAction(const InnerAction& action) : action_(action) {}
336
337 template <typename F>
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000338 operator Action<F>() const { return MakeAction(new Impl<F>(action_)); }
339
340 private:
341 template <typename F>
342 class Impl : public ActionInterface<F> {
343 public:
shiqiane35fdd92008-12-10 05:08:54 +0000344 typedef typename Function<F>::Result Result;
345 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000346
347 explicit Impl(const InnerAction& action) : action_(action) {}
348
349 virtual Result Perform(const ArgumentTuple& args) {
350 return action_.Perform(SelectArgs<Result, ArgumentTuple, $ks>::Select(args));
351 }
352
353 private:
shiqiane35fdd92008-12-10 05:08:54 +0000354 typedef typename SelectArgs<Result, ArgumentTuple,
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000355 $ks>::type InnerFunctionType;
shiqiane35fdd92008-12-10 05:08:54 +0000356
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000357 Action<InnerFunctionType> action_;
358 };
shiqiane35fdd92008-12-10 05:08:54 +0000359
shiqiane35fdd92008-12-10 05:08:54 +0000360 const InnerAction action_;
361};
362
363// Does two actions sequentially. Used for implementing the DoAll(a1,
364// a2, ...) action.
365template <typename Action1, typename Action2>
366class DoBothAction {
367 public:
368 DoBothAction(Action1 action1, Action2 action2)
369 : action1_(action1), action2_(action2) {}
370
371 // This template type conversion operator allows DoAll(a1, ..., a_n)
372 // to be used in ANY function of compatible type.
373 template <typename F>
374 operator Action<F>() const {
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000375 return Action<F>(new Impl<F>(action1_, action2_));
376 }
377
378 private:
379 // Implements the DoAll(...) action for a particular function type F.
380 template <typename F>
381 class Impl : public ActionInterface<F> {
382 public:
shiqiane35fdd92008-12-10 05:08:54 +0000383 typedef typename Function<F>::Result Result;
384 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
385 typedef typename Function<F>::MakeResultVoid VoidResult;
386
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000387 Impl(const Action<VoidResult>& action1, const Action<F>& action2)
388 : action1_(action1), action2_(action2) {}
shiqiane35fdd92008-12-10 05:08:54 +0000389
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000390 virtual Result Perform(const ArgumentTuple& args) {
391 action1_.Perform(args);
392 return action2_.Perform(args);
393 }
shiqiane35fdd92008-12-10 05:08:54 +0000394
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000395 private:
396 const Action<VoidResult> action1_;
397 const Action<F> action2_;
398 };
399
shiqiane35fdd92008-12-10 05:08:54 +0000400 Action1 action1_;
401 Action2 action2_;
402};
403
shiqian326aa562009-01-09 21:43:57 +0000404// A macro from the ACTION* family (defined later in this file)
405// defines an action that can be used in a mock function. Typically,
406// these actions only care about a subset of the arguments of the mock
407// function. For example, if such an action only uses the second
408// argument, it can be used in any mock function that takes >= 2
409// arguments where the type of the second argument is compatible.
410//
411// Therefore, the action implementation must be prepared to take more
412// arguments than it needs. The ExcessiveArg type is used to
413// represent those excessive arguments. In order to keep the compiler
414// error messages tractable, we define it in the testing namespace
415// instead of testing::internal. However, this is an INTERNAL TYPE
416// and subject to change without notice, so a user MUST NOT USE THIS
417// TYPE DIRECTLY.
418struct ExcessiveArg {};
419
420// A helper class needed for implementing the ACTION* macros.
421template <typename Result, class Impl>
422class ActionHelper {
423 public:
424$range i 0..n
425$for i
426
427[[
428$var template = [[$if i==0 [[]] $else [[
429$range j 0..i-1
430 template <$for j, [[typename A$j]]>
431]]]]
432$range j 0..i-1
433$var As = [[$for j, [[A$j]]]]
434$var as = [[$for j, [[get<$j>(args)]]]]
435$range k 1..n-i
436$var eas = [[$for k, [[ExcessiveArg()]]]]
437$var arg_list = [[$if (i==0) | (i==n) [[$as$eas]] $else [[$as, $eas]]]]
438$template
439 static Result Perform(Impl* impl, const ::std::tr1::tuple<$As>& args) {
440 using ::std::tr1::get;
zhanyong.wan7f4c2c02009-02-19 22:38:27 +0000441 return impl->template gmock_PerformImpl<$As>(args, $arg_list);
shiqian326aa562009-01-09 21:43:57 +0000442 }
443
444]]
445};
446
shiqiane35fdd92008-12-10 05:08:54 +0000447} // namespace internal
448
449// Various overloads for Invoke().
450
451// Creates an action that invokes 'function_impl' with the mock
452// function's arguments.
453template <typename FunctionImpl>
454PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke(
455 FunctionImpl function_impl) {
456 return MakePolymorphicAction(
457 internal::InvokeAction<FunctionImpl>(function_impl));
458}
459
460// Creates an action that invokes the given method on the given object
461// with the mock function's arguments.
462template <class Class, typename MethodPtr>
463PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke(
464 Class* obj_ptr, MethodPtr method_ptr) {
465 return MakePolymorphicAction(
466 internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr));
467}
468
469// Creates a reference wrapper for the given L-value. If necessary,
470// you can explicitly specify the type of the reference. For example,
471// suppose 'derived' is an object of type Derived, ByRef(derived)
472// would wrap a Derived&. If you want to wrap a const Base& instead,
473// where Base is a base class of Derived, just write:
474//
475// ByRef<const Base>(derived)
476template <typename T>
477inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT
478 return internal::ReferenceWrapper<T>(l_value);
479}
480
481// Various overloads for InvokeArgument<N>().
482//
483// The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th
484// (0-based) argument, which must be a k-ary callable, of the mock
485// function, with arguments a1, a2, ..., a_k.
486//
487// Notes:
488//
489// 1. The arguments are passed by value by default. If you need to
490// pass an argument by reference, wrap it inside ByRef(). For
491// example,
492//
493// InvokeArgument<1>(5, string("Hello"), ByRef(foo))
494//
495// passes 5 and string("Hello") by value, and passes foo by
496// reference.
497//
498// 2. If the callable takes an argument by reference but ByRef() is
499// not used, it will receive the reference to a copy of the value,
500// instead of the original value. For example, when the 0-th
501// argument of the mock function takes a const string&, the action
502//
503// InvokeArgument<0>(string("Hello"))
504//
505// makes a copy of the temporary string("Hello") object and passes a
506// reference of the copy, instead of the original temporary object,
507// to the callable. This makes it easy for a user to define an
508// InvokeArgument action from temporary values and have it performed
509// later.
510template <size_t N>
511inline PolymorphicAction<internal::InvokeArgumentAction0<N> > InvokeArgument() {
512 return MakePolymorphicAction(internal::InvokeArgumentAction0<N>());
513}
514
515// We deliberately pass a1 by value instead of const reference here in
516// case it is a C-string literal. If we had declared the parameter as
517// 'const A1& a1' and write InvokeArgument<0>("Hi"), the compiler
518// would've thought A1 is 'char[3]', which causes trouble as the
519// implementation needs to copy a value of type A1. By declaring the
520// parameter as 'A1 a1', the compiler will correctly infer that A1 is
521// 'const char*' when it sees InvokeArgument<0>("Hi").
522//
523// Since this function is defined inline, the compiler can get rid of
524// the copying of the arguments. Therefore the performance won't be
525// hurt.
526template <size_t N, typename A1>
527inline PolymorphicAction<internal::InvokeArgumentAction1<N, A1> >
528InvokeArgument(A1 a1) {
529 return MakePolymorphicAction(internal::InvokeArgumentAction1<N, A1>(a1));
530}
531
532$range i 2..n
533$for i [[
534$range j 1..i
535$var typename_As = [[$for j, [[typename A$j]]]]
536$var As = [[$for j, [[A$j]]]]
537$var Aas = [[$for j, [[A$j a$j]]]]
538$var as = [[$for j, [[a$j]]]]
539
540template <size_t N, $typename_As>
541inline PolymorphicAction<internal::InvokeArgumentAction$i<N, $As> >
542InvokeArgument($Aas) {
543 return MakePolymorphicAction(
544 internal::InvokeArgumentAction$i<N, $As>($as));
545}
546
547]]
548
549// WithoutArgs(inner_action) can be used in a mock function with a
550// non-empty argument list to perform inner_action, which takes no
551// argument. In other words, it adapts an action accepting no
552// argument to one that accepts (and ignores) arguments.
553template <typename InnerAction>
554inline internal::WithArgsAction<InnerAction>
555WithoutArgs(const InnerAction& action) {
556 return internal::WithArgsAction<InnerAction>(action);
557}
558
559// WithArg<k>(an_action) creates an action that passes the k-th
560// (0-based) argument of the mock function to an_action and performs
561// it. It adapts an action accepting one argument to one that accepts
562// multiple arguments. For convenience, we also provide
563// WithArgs<k>(an_action) (defined below) as a synonym.
564template <int k, typename InnerAction>
565inline internal::WithArgsAction<InnerAction, k>
566WithArg(const InnerAction& action) {
567 return internal::WithArgsAction<InnerAction, k>(action);
568}
569
570// WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes
571// the selected arguments of the mock function to an_action and
572// performs it. It serves as an adaptor between actions with
573// different argument lists. C++ doesn't support default arguments for
574// function templates, so we have to overload it.
575
576$range i 1..n
577$for i [[
578$range j 1..i
579template <$for j [[int k$j, ]]typename InnerAction>
580inline internal::WithArgsAction<InnerAction$for j [[, k$j]]>
581WithArgs(const InnerAction& action) {
582 return internal::WithArgsAction<InnerAction$for j [[, k$j]]>(action);
583}
584
585
586]]
587// Creates an action that does actions a1, a2, ..., sequentially in
588// each invocation.
589$range i 2..n
590$for i [[
591$range j 2..i
592$var types = [[$for j, [[typename Action$j]]]]
593$var Aas = [[$for j [[, Action$j a$j]]]]
594
595template <typename Action1, $types>
596$range k 1..i-1
597
598inline $for k [[internal::DoBothAction<Action$k, ]]Action$i$for k [[>]]
599
600DoAll(Action1 a1$Aas) {
601$if i==2 [[
602
603 return internal::DoBothAction<Action1, Action2>(a1, a2);
604]] $else [[
605$range j2 2..i
606
607 return DoAll(a1, DoAll($for j2, [[a$j2]]));
608]]
609
610}
611
612]]
613
614} // namespace testing
615
shiqian326aa562009-01-09 21:43:57 +0000616// The ACTION* family of macros can be used in a namespace scope to
617// define custom actions easily. The syntax:
618//
619// ACTION(name) { statements; }
620//
621// will define an action with the given name that executes the
622// statements. The value returned by the statements will be used as
623// the return value of the action. Inside the statements, you can
624// refer to the K-th (0-based) argument of the mock function by
625// 'argK', and refer to its type by 'argK_type'. For example:
626//
627// ACTION(IncrementArg1) {
628// arg1_type temp = arg1;
629// return ++(*temp);
630// }
631//
632// allows you to write
633//
634// ...WillOnce(IncrementArg1());
635//
636// You can also refer to the entire argument tuple and its type by
637// 'args' and 'args_type', and refer to the mock function type and its
638// return type by 'function_type' and 'return_type'.
639//
640// Note that you don't need to specify the types of the mock function
641// arguments. However rest assured that your code is still type-safe:
642// you'll get a compiler error if *arg1 doesn't support the ++
643// operator, or if the type of ++(*arg1) isn't compatible with the
644// mock function's return type, for example.
645//
646// Sometimes you'll want to parameterize the action. For that you can use
647// another macro:
648//
649// ACTION_P(name, param_name) { statements; }
650//
651// For example:
652//
653// ACTION_P(Add, n) { return arg0 + n; }
654//
655// will allow you to write:
656//
657// ...WillOnce(Add(5));
658//
659// Note that you don't need to provide the type of the parameter
660// either. If you need to reference the type of a parameter named
661// 'foo', you can write 'foo_type'. For example, in the body of
662// ACTION_P(Add, n) above, you can write 'n_type' to refer to the type
663// of 'n'.
664//
665// We also provide ACTION_P2, ACTION_P3, ..., up to ACTION_P$n to support
666// multi-parameter actions.
667//
668// For the purpose of typing, you can view
669//
670// ACTION_Pk(Foo, p1, ..., pk) { ... }
671//
672// as shorthand for
673//
674// template <typename p1_type, ..., typename pk_type>
675// FooActionPk<p1_type, ..., pk_type> Foo(p1_type p1, ..., pk_type pk) { ... }
676//
677// In particular, you can provide the template type arguments
678// explicitly when invoking Foo(), as in Foo<long, bool>(5, false);
679// although usually you can rely on the compiler to infer the types
680// for you automatically. You can assign the result of expression
681// Foo(p1, ..., pk) to a variable of type FooActionPk<p1_type, ...,
682// pk_type>. This can be useful when composing actions.
683//
684// You can also overload actions with different numbers of parameters:
685//
686// ACTION_P(Plus, a) { ... }
687// ACTION_P2(Plus, a, b) { ... }
688//
689// While it's tempting to always use the ACTION* macros when defining
690// a new action, you should also consider implementing ActionInterface
691// or using MakePolymorphicAction() instead, especially if you need to
692// use the action a lot. While these approaches require more work,
693// they give you more control on the types of the mock function
694// arguments and the action parameters, which in general leads to
695// better compiler error messages that pay off in the long run. They
696// also allow overloading actions based on parameter types (as opposed
697// to just based on the number of parameters).
698//
699// CAVEAT:
700//
701// ACTION*() can only be used in a namespace scope. The reason is
702// that C++ doesn't yet allow function-local types to be used to
703// instantiate templates. The up-coming C++0x standard will fix this.
704// Once that's done, we'll consider supporting using ACTION*() inside
705// a function.
706//
707// MORE INFORMATION:
708//
709// To learn more about using these macros, please search for 'ACTION'
710// on http://code.google.com/p/googlemock/wiki/CookBook.
711
712$range i 0..n
zhanyong.wan33c0af02009-04-03 00:10:12 +0000713$range k 0..n-1
714
715// An internal macro needed for implementing ACTION*().
716#define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_\
717 const args_type& args GTEST_ATTRIBUTE_UNUSED_
718$for k [[,\
719 arg$k[[]]_type arg$k GTEST_ATTRIBUTE_UNUSED_]]
720
721
zhanyong.wan18490652009-05-11 18:54:08 +0000722// Sometimes you want to give an action explicit template parameters
723// that cannot be inferred from its value parameters. ACTION() and
724// ACTION_P*() don't support that. ACTION_TEMPLATE() remedies that
725// and can be viewed as an extension to ACTION() and ACTION_P*().
726//
727// The syntax:
728//
729// ACTION_TEMPLATE(ActionName,
730// HAS_m_TEMPLATE_PARAMS(kind1, name1, ..., kind_m, name_m),
731// AND_n_VALUE_PARAMS(p1, ..., p_n)) { statements; }
732//
733// defines an action template that takes m explicit template
734// parameters and n value parameters. name_i is the name of the i-th
735// template parameter, and kind_i specifies whether it's a typename,
736// an integral constant, or a template. p_i is the name of the i-th
737// value parameter.
738//
739// Example:
740//
741// // DuplicateArg<k, T>(output) converts the k-th argument of the mock
742// // function to type T and copies it to *output.
743// ACTION_TEMPLATE(DuplicateArg,
744// HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
745// AND_1_VALUE_PARAMS(output)) {
746// *output = T(std::tr1::get<k>(args));
747// }
748// ...
749// int n;
750// EXPECT_CALL(mock, Foo(_, _))
751// .WillOnce(DuplicateArg<1, unsigned char>(&n));
752//
753// To create an instance of an action template, write:
754//
755// ActionName<t1, ..., t_m>(v1, ..., v_n)
756//
757// where the ts are the template arguments and the vs are the value
758// arguments. The value argument types are inferred by the compiler.
759// If you want to explicitly specify the value argument types, you can
760// provide additional template arguments:
761//
762// ActionName<t1, ..., t_m, u1, ..., u_k>(v1, ..., v_n)
763//
764// where u_i is the desired type of v_i.
765//
766// ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded on the
767// number of value parameters, but not on the number of template
768// parameters. Without the restriction, the meaning of the following
769// is unclear:
770//
771// OverloadedAction<int, bool>(x);
772//
773// Are we using a single-template-parameter action where 'bool' refers
774// to the type of x, or are we using a two-template-parameter action
775// where the compiler is asked to infer the type of x?
776//
777// Implementation notes:
778//
779// GMOCK_INTERNAL_*_HAS_m_TEMPLATE_PARAMS and
780// GMOCK_INTERNAL_*_AND_n_VALUE_PARAMS are internal macros for
781// implementing ACTION_TEMPLATE. The main trick we use is to create
782// new macro invocations when expanding a macro. For example, we have
783//
784// #define ACTION_TEMPLATE(name, template_params, value_params)
785// ... GMOCK_INTERNAL_DECL_##template_params ...
786//
787// which causes ACTION_TEMPLATE(..., HAS_1_TEMPLATE_PARAMS(typename, T), ...)
788// to expand to
789//
790// ... GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(typename, T) ...
791//
792// Since GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS is a macro, the
793// preprocessor will continue to expand it to
794//
795// ... typename T ...
796//
797// This technique conforms to the C++ standard and is portable. It
798// allows us to implement action templates using O(N) code, where N is
799// the maximum number of template/value parameters supported. Without
800// using it, we'd have to devote O(N^2) amount of code to implement all
801// combinations of m and n.
802
803// Declares the template parameters.
804
805$range j 1..n
806$for j [[
807$range m 0..j-1
808#define GMOCK_INTERNAL_DECL_HAS_$j[[]]
809_TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[kind$m name$m]]
810
811
812]]
813
814// Lists the template parameters.
815
816$for j [[
817$range m 0..j-1
818#define GMOCK_INTERNAL_LIST_HAS_$j[[]]
819_TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[name$m]]
820
821
822]]
823
824// Declares the types of value parameters.
825
826$for i [[
827$range j 0..i-1
828#define GMOCK_INTERNAL_DECL_TYPE_AND_$i[[]]
829_VALUE_PARAMS($for j, [[p$j]]) $for j [[, typename p$j##_type]]
830
831
832]]
833
834// Initializes the value parameters.
835
836$for i [[
837$range j 0..i-1
838#define GMOCK_INTERNAL_INIT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])\
839 ($for j, [[p$j##_type gmock_p$j]])$if i>0 [[ : ]]$for j, [[p$j(gmock_p$j)]]
840
841
842]]
843
844// Declares the fields for storing the value parameters.
845
846$for i [[
847$range j 0..i-1
848#define GMOCK_INTERNAL_DEFN_AND_$i[[]]
849_VALUE_PARAMS($for j, [[p$j]]) $for j [[p$j##_type p$j; ]]
850
851
852]]
853
854// Lists the value parameters.
855
856$for i [[
857$range j 0..i-1
858#define GMOCK_INTERNAL_LIST_AND_$i[[]]
859_VALUE_PARAMS($for j, [[p$j]]) $for j, [[p$j]]
860
861
862]]
863
864// Lists the value parameter types.
865
866$for i [[
867$range j 0..i-1
868#define GMOCK_INTERNAL_LIST_TYPE_AND_$i[[]]
869_VALUE_PARAMS($for j, [[p$j]]) $for j [[, p$j##_type]]
870
871
872]]
873
874// Declares the value parameters.
875
876$for i [[
877$range j 0..i-1
878#define GMOCK_INTERNAL_DECL_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
879$for j, [[p$j##_type p$j]]
880
881
882]]
883
884// The suffix of the class template implementing the action template.
885$for i [[
886
887
888$range j 0..i-1
889#define GMOCK_INTERNAL_COUNT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
890$if i==1 [[P]] $elif i>=2 [[P$i]]
891]]
892
893
894// The name of the class template implementing the action template.
895#define GMOCK_ACTION_CLASS_(name, value_params)\
896 GMOCK_CONCAT_TOKEN_(name##Action, GMOCK_INTERNAL_COUNT_##value_params)
897
898$range k 0..n-1
899
900#define ACTION_TEMPLATE(name, template_params, value_params)\
901 template <GMOCK_INTERNAL_DECL_##template_params\
902 GMOCK_INTERNAL_DECL_TYPE_##value_params>\
903 class GMOCK_ACTION_CLASS_(name, value_params) {\
904 public:\
905 GMOCK_ACTION_CLASS_(name, value_params)\
906 GMOCK_INTERNAL_INIT_##value_params {}\
907 template <typename F>\
908 class gmock_Impl : public ::testing::ActionInterface<F> {\
909 public:\
910 typedef F function_type;\
911 typedef typename ::testing::internal::Function<F>::Result return_type;\
912 typedef typename ::testing::internal::Function<F>::ArgumentTuple\
913 args_type;\
914 explicit gmock_Impl GMOCK_INTERNAL_INIT_##value_params {}\
915 virtual return_type Perform(const args_type& args) {\
916 return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
917 Perform(this, args);\
918 }\
919 template <$for k, [[typename arg$k[[]]_type]]>\
920 return_type gmock_PerformImpl(const args_type& args[[]]
921$for k [[, arg$k[[]]_type arg$k]]) const;\
922 GMOCK_INTERNAL_DEFN_##value_params\
923 };\
924 template <typename F> operator ::testing::Action<F>() const {\
925 return ::testing::Action<F>(\
926 new gmock_Impl<F>(GMOCK_INTERNAL_LIST_##value_params));\
927 }\
928 GMOCK_INTERNAL_DEFN_##value_params\
929 };\
930 template <GMOCK_INTERNAL_DECL_##template_params\
931 GMOCK_INTERNAL_DECL_TYPE_##value_params>\
932 inline GMOCK_ACTION_CLASS_(name, value_params)<\
933 GMOCK_INTERNAL_LIST_##template_params\
934 GMOCK_INTERNAL_LIST_TYPE_##value_params> name(\
935 GMOCK_INTERNAL_DECL_##value_params) {\
936 return GMOCK_ACTION_CLASS_(name, value_params)<\
937 GMOCK_INTERNAL_LIST_##template_params\
938 GMOCK_INTERNAL_LIST_TYPE_##value_params>(\
939 GMOCK_INTERNAL_LIST_##value_params);\
940 }\
941 template <GMOCK_INTERNAL_DECL_##template_params\
942 GMOCK_INTERNAL_DECL_TYPE_##value_params>\
943 template <typename F>\
944 template <typename arg0_type, typename arg1_type, typename arg2_type,\
945 typename arg3_type, typename arg4_type, typename arg5_type,\
946 typename arg6_type, typename arg7_type, typename arg8_type,\
947 typename arg9_type>\
948 typename ::testing::internal::Function<F>::Result\
949 GMOCK_ACTION_CLASS_(name, value_params)<\
950 GMOCK_INTERNAL_LIST_##template_params\
951 GMOCK_INTERNAL_LIST_TYPE_##value_params>::gmock_Impl<F>::\
952 gmock_PerformImpl(\
953 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
954
shiqian326aa562009-01-09 21:43:57 +0000955$for i
956
957[[
958$var template = [[$if i==0 [[]] $else [[
959$range j 0..i-1
960
961 template <$for j, [[typename p$j##_type]]>\
962]]]]
963$var class_name = [[name##Action[[$if i==0 [[]] $elif i==1 [[P]]
964 $else [[P$i]]]]]]
965$range j 0..i-1
966$var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
967$var param_types_and_names = [[$for j, [[p$j##_type p$j]]]]
968$var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]]
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000969$var param_field_decls = [[$for j
shiqian326aa562009-01-09 21:43:57 +0000970[[
971
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000972 p$j##_type p$j;\
shiqian326aa562009-01-09 21:43:57 +0000973]]]]
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000974$var param_field_decls2 = [[$for j
shiqian326aa562009-01-09 21:43:57 +0000975[[
976
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000977 p$j##_type p$j;\
shiqian326aa562009-01-09 21:43:57 +0000978]]]]
979$var params = [[$for j, [[p$j]]]]
980$var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]]
shiqian326aa562009-01-09 21:43:57 +0000981$var typename_arg_types = [[$for k, [[typename arg$k[[]]_type]]]]
982$var arg_types_and_names = [[$for k, [[arg$k[[]]_type arg$k]]]]
983$var macro_name = [[$if i==0 [[ACTION]] $elif i==1 [[ACTION_P]]
984 $else [[ACTION_P$i]]]]
985
986#define $macro_name(name$for j [[, p$j]])\$template
987 class $class_name {\
988 public:\
989 $class_name($ctor_param_list)$inits {}\
990 template <typename F>\
991 class gmock_Impl : public ::testing::ActionInterface<F> {\
992 public:\
993 typedef F function_type;\
994 typedef typename ::testing::internal::Function<F>::Result return_type;\
995 typedef typename ::testing::internal::Function<F>::ArgumentTuple\
996 args_type;\
997 [[$if i==1 [[explicit ]]]]gmock_Impl($ctor_param_list)$inits {}\
998 virtual return_type Perform(const args_type& args) {\
999 return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
1000 Perform(this, args);\
1001 }\
1002 template <$typename_arg_types>\
1003 return_type gmock_PerformImpl(const args_type& args, [[]]
zhanyong.wanc069d7f2009-02-02 20:51:53 +00001004$arg_types_and_names) const;\$param_field_decls
shiqian326aa562009-01-09 21:43:57 +00001005 };\
1006 template <typename F> operator ::testing::Action<F>() const {\
1007 return ::testing::Action<F>(new gmock_Impl<F>($params));\
zhanyong.wanc069d7f2009-02-02 20:51:53 +00001008 }\$param_field_decls2
shiqian326aa562009-01-09 21:43:57 +00001009 };\$template
1010 inline $class_name$param_types name($param_types_and_names) {\
1011 return $class_name$param_types($params);\
1012 }\$template
1013 template <typename F>\
1014 template <$typename_arg_types>\
1015 typename ::testing::internal::Function<F>::Result\
zhanyong.wan33c0af02009-04-03 00:10:12 +00001016 $class_name$param_types::gmock_Impl<F>::gmock_PerformImpl(\
1017 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
shiqian326aa562009-01-09 21:43:57 +00001018]]
zhanyong.wan7f4c2c02009-02-19 22:38:27 +00001019$$ } // This meta comment fixes auto-indentation in Emacs. It won't
1020$$ // show up in the generated code.
shiqian326aa562009-01-09 21:43:57 +00001021
1022
zhanyong.wan7f4c2c02009-02-19 22:38:27 +00001023// TODO(wan@google.com): move the following to a different .h file
1024// such that we don't have to run 'pump' every time the code is
1025// updated.
zhanyong.wane1cdce52009-02-06 01:09:43 +00001026namespace testing {
1027
zhanyong.wan7f4c2c02009-02-19 22:38:27 +00001028namespace internal {
1029
1030// Saves argument #0 to where the pointer points.
1031ACTION_P(SaveArg0, pointer) { *pointer = arg0; }
1032
1033// Assigns 'value' to the variable referenced by argument #0.
1034ACTION_P(SetArg0Referee, value) {
1035 // Ensures that argument #0 is a reference. If you get a compiler
1036 // error on the next line, you are using SetArgReferee<k>(value) in
1037 // a mock function whose k-th (0-based) argument is not a reference.
1038 GMOCK_COMPILE_ASSERT_(internal::is_reference<arg0_type>::value,
1039 SetArgReferee_must_be_used_with_a_reference_argument);
1040 arg0 = value;
1041}
1042
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +00001043// ReturnNewAction<T> creates and returns a new instance of an object each time
1044// it is performed. It is overloaded to work with constructors that take
1045// different numbers of arguments.
1046$range i 0..n
1047$for i [[
1048$var arity = [[ $if i==0 [[nullary]]
1049 $elif i==1 [[unary]]
1050 $elif i==2 [[binary]]
1051 $elif i==3 [[ternary]]
1052 $else [[$i-ary]]]]
1053$range j 1..i
1054$var typename_As = [[$for j [[, typename A$j]]]]
1055$var args_ = [[$for j, [[arg$j[[]]_]]]]
1056
1057// Returns a new instance of T using a $arity constructor with the given
1058// arguments.
1059template <typename T$typename_As>
1060class ReturnNewAction$i {
1061 public:
1062 $if i==1 [[explicit ]]ReturnNewAction$i($for j, [[A$j a$j]])$if i>0 [[ : ]]
1063$for j, [[arg$j[[]]_(a$j)]] {}
1064
1065 template <typename Result, typename ArgumentTuple>
1066 Result Perform(const ArgumentTuple& /* args */) {
1067 return new T($args_);
1068 }
1069 private:
1070$for j [[
1071
1072 const A$j arg$j[[]]_;
1073]]
1074
1075};
1076
1077]]
1078
1079// Deletes the object pointed to by argument #0.
1080ACTION(DeleteArg0) { delete arg0; }
1081
zhanyong.wan7f4c2c02009-02-19 22:38:27 +00001082} // namespace internal
1083
1084// Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
1085// mock function to *pointer.
1086template <int k, typename Pointer>
1087inline internal::WithArgsAction<internal::SaveArg0ActionP<Pointer>, k>
1088SaveArg(const Pointer& pointer) {
1089 return WithArg<k>(internal::SaveArg0(pointer));
1090}
1091
1092// Action SetArgReferee<k>(value) assigns 'value' to the variable
1093// referenced by the k-th (0-based) argument of the mock function.
1094template <int k, typename Value>
1095inline internal::WithArgsAction<internal::SetArg0RefereeActionP<Value>, k>
1096SetArgReferee(const Value& value) {
1097 return WithArg<k>(internal::SetArg0Referee(value));
1098}
1099
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +00001100// Various overloads for ReturnNew<T>().
1101//
1102// The ReturnNew<T>(a1, a2, ..., a_k) action returns a pointer to a new
1103// instance of type T, constructed on the heap with constructor arguments
1104// a1, a2, ..., and a_k. The caller assumes ownership of the returned value.
1105$range i 0..n
1106$for i [[
1107$range j 1..i
1108$var typename_As = [[$for j [[, typename A$j]]]]
1109$var As = [[$for j [[, A$j]]]]
1110$var Aas = [[$for j, [[A$j a$j]]]]
1111$var as = [[$for j, [[a$j]]]]
1112
1113template <typename T$typename_As>
1114inline PolymorphicAction<internal::ReturnNewAction$i<T$As> >
1115ReturnNew($Aas) {
1116 return MakePolymorphicAction(
1117 internal::ReturnNewAction$i<T$As>($as));
1118}
1119
1120]]
1121
1122// Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
1123// function.
1124template <int k>
1125inline internal::WithArgsAction<internal::DeleteArg0Action, k>
1126DeleteArg() {
1127 return WithArg<k>(internal::DeleteArg0());
1128}
1129
zhanyong.wane1cdce52009-02-06 01:09:43 +00001130// Action Throw(exception) can be used in a mock function of any type
1131// to throw the given exception. Any copyable value can be thrown.
1132#if GTEST_HAS_EXCEPTIONS
1133ACTION_P(Throw, exception) { throw exception; }
1134#endif // GTEST_HAS_EXCEPTIONS
1135
1136} // namespace testing
1137
shiqiane35fdd92008-12-10 05:08:54 +00001138#endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_