blob: d3dfac09239aa16d01a2a00e20f4e2d4e4ebde08 [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
398} // namespace internal
399
400// Various overloads for Invoke().
401
402// Creates an action that invokes 'function_impl' with the mock
403// function's arguments.
404template <typename FunctionImpl>
405PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke(
406 FunctionImpl function_impl) {
407 return MakePolymorphicAction(
408 internal::InvokeAction<FunctionImpl>(function_impl));
409}
410
411// Creates an action that invokes the given method on the given object
412// with the mock function's arguments.
413template <class Class, typename MethodPtr>
414PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke(
415 Class* obj_ptr, MethodPtr method_ptr) {
416 return MakePolymorphicAction(
417 internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr));
418}
419
420// Creates a reference wrapper for the given L-value. If necessary,
421// you can explicitly specify the type of the reference. For example,
422// suppose 'derived' is an object of type Derived, ByRef(derived)
423// would wrap a Derived&. If you want to wrap a const Base& instead,
424// where Base is a base class of Derived, just write:
425//
426// ByRef<const Base>(derived)
427template <typename T>
428inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT
429 return internal::ReferenceWrapper<T>(l_value);
430}
431
432// Various overloads for InvokeArgument<N>().
433//
434// The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th
435// (0-based) argument, which must be a k-ary callable, of the mock
436// function, with arguments a1, a2, ..., a_k.
437//
438// Notes:
439//
440// 1. The arguments are passed by value by default. If you need to
441// pass an argument by reference, wrap it inside ByRef(). For
442// example,
443//
444// InvokeArgument<1>(5, string("Hello"), ByRef(foo))
445//
446// passes 5 and string("Hello") by value, and passes foo by
447// reference.
448//
449// 2. If the callable takes an argument by reference but ByRef() is
450// not used, it will receive the reference to a copy of the value,
451// instead of the original value. For example, when the 0-th
452// argument of the mock function takes a const string&, the action
453//
454// InvokeArgument<0>(string("Hello"))
455//
456// makes a copy of the temporary string("Hello") object and passes a
457// reference of the copy, instead of the original temporary object,
458// to the callable. This makes it easy for a user to define an
459// InvokeArgument action from temporary values and have it performed
460// later.
461template <size_t N>
462inline PolymorphicAction<internal::InvokeArgumentAction0<N> > InvokeArgument() {
463 return MakePolymorphicAction(internal::InvokeArgumentAction0<N>());
464}
465
466// We deliberately pass a1 by value instead of const reference here in
467// case it is a C-string literal. If we had declared the parameter as
468// 'const A1& a1' and write InvokeArgument<0>("Hi"), the compiler
469// would've thought A1 is 'char[3]', which causes trouble as the
470// implementation needs to copy a value of type A1. By declaring the
471// parameter as 'A1 a1', the compiler will correctly infer that A1 is
472// 'const char*' when it sees InvokeArgument<0>("Hi").
473//
474// Since this function is defined inline, the compiler can get rid of
475// the copying of the arguments. Therefore the performance won't be
476// hurt.
477template <size_t N, typename A1>
478inline PolymorphicAction<internal::InvokeArgumentAction1<N, A1> >
479InvokeArgument(A1 a1) {
480 return MakePolymorphicAction(internal::InvokeArgumentAction1<N, A1>(a1));
481}
482
483$range i 2..n
484$for i [[
485$range j 1..i
486$var typename_As = [[$for j, [[typename A$j]]]]
487$var As = [[$for j, [[A$j]]]]
488$var Aas = [[$for j, [[A$j a$j]]]]
489$var as = [[$for j, [[a$j]]]]
490
491template <size_t N, $typename_As>
492inline PolymorphicAction<internal::InvokeArgumentAction$i<N, $As> >
493InvokeArgument($Aas) {
494 return MakePolymorphicAction(
495 internal::InvokeArgumentAction$i<N, $As>($as));
496}
497
498]]
499
500// WithoutArgs(inner_action) can be used in a mock function with a
501// non-empty argument list to perform inner_action, which takes no
502// argument. In other words, it adapts an action accepting no
503// argument to one that accepts (and ignores) arguments.
504template <typename InnerAction>
505inline internal::WithArgsAction<InnerAction>
506WithoutArgs(const InnerAction& action) {
507 return internal::WithArgsAction<InnerAction>(action);
508}
509
510// WithArg<k>(an_action) creates an action that passes the k-th
511// (0-based) argument of the mock function to an_action and performs
512// it. It adapts an action accepting one argument to one that accepts
513// multiple arguments. For convenience, we also provide
514// WithArgs<k>(an_action) (defined below) as a synonym.
515template <int k, typename InnerAction>
516inline internal::WithArgsAction<InnerAction, k>
517WithArg(const InnerAction& action) {
518 return internal::WithArgsAction<InnerAction, k>(action);
519}
520
521// WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes
522// the selected arguments of the mock function to an_action and
523// performs it. It serves as an adaptor between actions with
524// different argument lists. C++ doesn't support default arguments for
525// function templates, so we have to overload it.
526
527$range i 1..n
528$for i [[
529$range j 1..i
530template <$for j [[int k$j, ]]typename InnerAction>
531inline internal::WithArgsAction<InnerAction$for j [[, k$j]]>
532WithArgs(const InnerAction& action) {
533 return internal::WithArgsAction<InnerAction$for j [[, k$j]]>(action);
534}
535
536
537]]
538// Creates an action that does actions a1, a2, ..., sequentially in
539// each invocation.
540$range i 2..n
541$for i [[
542$range j 2..i
543$var types = [[$for j, [[typename Action$j]]]]
544$var Aas = [[$for j [[, Action$j a$j]]]]
545
546template <typename Action1, $types>
547$range k 1..i-1
548
549inline $for k [[internal::DoBothAction<Action$k, ]]Action$i$for k [[>]]
550
551DoAll(Action1 a1$Aas) {
552$if i==2 [[
553
554 return internal::DoBothAction<Action1, Action2>(a1, a2);
555]] $else [[
556$range j2 2..i
557
558 return DoAll(a1, DoAll($for j2, [[a$j2]]));
559]]
560
561}
562
563]]
564
565} // namespace testing
566
567#endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_