blob: 712f65d62d9aa8036601d6d056f9d57a9fdc29e0 [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]]]]
shiqiane35fdd92008-12-10 05:08:54 +000064$var gets = [[$for j, [[get<$(j - 1)>(args)]]]]
65template <typename R$types>
kosakbd018832014-04-02 20:30:00 +000066class InvokeHelper<R, ::testing::tuple<$as> > {
shiqiane35fdd92008-12-10 05:08:54 +000067 public:
68 template <typename Function>
kosakbd018832014-04-02 20:30:00 +000069 static R Invoke(Function function, const ::testing::tuple<$as>&$args) {
70 return function($gets);
shiqiane35fdd92008-12-10 05:08:54 +000071 }
72
73 template <class Class, typename MethodPtr>
74 static R InvokeMethod(Class* obj_ptr,
75 MethodPtr method_ptr,
kosakbd018832014-04-02 20:30:00 +000076 const ::testing::tuple<$as>&$args) {
77 return (obj_ptr->*method_ptr)($gets);
shiqiane35fdd92008-12-10 05:08:54 +000078 }
79};
80
81
82]]
shiqiane35fdd92008-12-10 05:08:54 +000083// An INTERNAL macro for extracting the type of a tuple field. It's
84// subject to change without notice - DO NOT USE IN USER CODE!
zhanyong.wane0d051e2009-02-19 00:33:37 +000085#define GMOCK_FIELD_(Tuple, N) \
kosakbd018832014-04-02 20:30:00 +000086 typename ::testing::tuple_element<N, Tuple>::type
shiqiane35fdd92008-12-10 05:08:54 +000087
88$range i 1..n
89
90// SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::type is the
91// type of an n-ary function whose i-th (1-based) argument type is the
92// k{i}-th (0-based) field of ArgumentTuple, which must be a tuple
93// type, and whose return type is Result. For example,
kosakbd018832014-04-02 20:30:00 +000094// SelectArgs<int, ::testing::tuple<bool, char, double, long>, 0, 3>::type
shiqiane35fdd92008-12-10 05:08:54 +000095// is int(bool, long).
96//
97// SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::Select(args)
98// returns the selected fields (k1, k2, ..., k_n) of args as a tuple.
99// For example,
kosakbd018832014-04-02 20:30:00 +0000100// SelectArgs<int, tuple<bool, char, double>, 2, 0>::Select(
101// ::testing::make_tuple(true, 'a', 2.5))
102// returns tuple (2.5, true).
shiqiane35fdd92008-12-10 05:08:54 +0000103//
104// The numbers in list k1, k2, ..., k_n must be >= 0, where n can be
105// in the range [0, $n]. Duplicates are allowed and they don't have
106// to be in an ascending or descending order.
107
108template <typename Result, typename ArgumentTuple, $for i, [[int k$i]]>
109class SelectArgs {
110 public:
zhanyong.wane0d051e2009-02-19 00:33:37 +0000111 typedef Result type($for i, [[GMOCK_FIELD_(ArgumentTuple, k$i)]]);
shiqiane35fdd92008-12-10 05:08:54 +0000112 typedef typename Function<type>::ArgumentTuple SelectedArgs;
113 static SelectedArgs Select(const ArgumentTuple& args) {
shiqiane35fdd92008-12-10 05:08:54 +0000114 return SelectedArgs($for i, [[get<k$i>(args)]]);
115 }
116};
117
118
119$for i [[
120$range j 1..n
121$range j1 1..i-1
122template <typename Result, typename ArgumentTuple$for j1[[, int k$j1]]>
123class SelectArgs<Result, ArgumentTuple,
124 $for j, [[$if j <= i-1 [[k$j]] $else [[-1]]]]> {
125 public:
zhanyong.wane0d051e2009-02-19 00:33:37 +0000126 typedef Result type($for j1, [[GMOCK_FIELD_(ArgumentTuple, k$j1)]]);
shiqiane35fdd92008-12-10 05:08:54 +0000127 typedef typename Function<type>::ArgumentTuple SelectedArgs;
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000128 static SelectedArgs Select(const ArgumentTuple& [[]]
129$if i == 1 [[/* args */]] $else [[args]]) {
shiqiane35fdd92008-12-10 05:08:54 +0000130 return SelectedArgs($for j1, [[get<k$j1>(args)]]);
131 }
132};
133
134
135]]
zhanyong.wane0d051e2009-02-19 00:33:37 +0000136#undef GMOCK_FIELD_
shiqiane35fdd92008-12-10 05:08:54 +0000137
138$var ks = [[$for i, [[k$i]]]]
139
140// Implements the WithArgs action.
141template <typename InnerAction, $for i, [[int k$i = -1]]>
142class WithArgsAction {
143 public:
144 explicit WithArgsAction(const InnerAction& action) : action_(action) {}
145
146 template <typename F>
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000147 operator Action<F>() const { return MakeAction(new Impl<F>(action_)); }
148
149 private:
150 template <typename F>
151 class Impl : public ActionInterface<F> {
152 public:
shiqiane35fdd92008-12-10 05:08:54 +0000153 typedef typename Function<F>::Result Result;
154 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000155
156 explicit Impl(const InnerAction& action) : action_(action) {}
157
158 virtual Result Perform(const ArgumentTuple& args) {
159 return action_.Perform(SelectArgs<Result, ArgumentTuple, $ks>::Select(args));
160 }
161
162 private:
shiqiane35fdd92008-12-10 05:08:54 +0000163 typedef typename SelectArgs<Result, ArgumentTuple,
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000164 $ks>::type InnerFunctionType;
shiqiane35fdd92008-12-10 05:08:54 +0000165
zhanyong.wan38ca64d2009-02-19 22:30:22 +0000166 Action<InnerFunctionType> action_;
167 };
shiqiane35fdd92008-12-10 05:08:54 +0000168
shiqiane35fdd92008-12-10 05:08:54 +0000169 const InnerAction action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000170
171 GTEST_DISALLOW_ASSIGN_(WithArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000172};
173
shiqian326aa562009-01-09 21:43:57 +0000174// A macro from the ACTION* family (defined later in this file)
175// defines an action that can be used in a mock function. Typically,
176// these actions only care about a subset of the arguments of the mock
177// function. For example, if such an action only uses the second
178// argument, it can be used in any mock function that takes >= 2
179// arguments where the type of the second argument is compatible.
180//
181// Therefore, the action implementation must be prepared to take more
182// arguments than it needs. The ExcessiveArg type is used to
183// represent those excessive arguments. In order to keep the compiler
184// error messages tractable, we define it in the testing namespace
185// instead of testing::internal. However, this is an INTERNAL TYPE
186// and subject to change without notice, so a user MUST NOT USE THIS
187// TYPE DIRECTLY.
188struct ExcessiveArg {};
189
190// A helper class needed for implementing the ACTION* macros.
191template <typename Result, class Impl>
192class ActionHelper {
193 public:
194$range i 0..n
195$for i
196
197[[
198$var template = [[$if i==0 [[]] $else [[
199$range j 0..i-1
200 template <$for j, [[typename A$j]]>
201]]]]
202$range j 0..i-1
203$var As = [[$for j, [[A$j]]]]
204$var as = [[$for j, [[get<$j>(args)]]]]
205$range k 1..n-i
206$var eas = [[$for k, [[ExcessiveArg()]]]]
207$var arg_list = [[$if (i==0) | (i==n) [[$as$eas]] $else [[$as, $eas]]]]
208$template
kosakbd018832014-04-02 20:30:00 +0000209 static Result Perform(Impl* impl, const ::testing::tuple<$As>& args) {
zhanyong.wan7f4c2c02009-02-19 22:38:27 +0000210 return impl->template gmock_PerformImpl<$As>(args, $arg_list);
shiqian326aa562009-01-09 21:43:57 +0000211 }
212
213]]
214};
215
shiqiane35fdd92008-12-10 05:08:54 +0000216} // namespace internal
217
218// Various overloads for Invoke().
219
shiqiane35fdd92008-12-10 05:08:54 +0000220// WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes
221// the selected arguments of the mock function to an_action and
222// performs it. It serves as an adaptor between actions with
223// different argument lists. C++ doesn't support default arguments for
224// function templates, so we have to overload it.
225
226$range i 1..n
227$for i [[
228$range j 1..i
229template <$for j [[int k$j, ]]typename InnerAction>
230inline internal::WithArgsAction<InnerAction$for j [[, k$j]]>
231WithArgs(const InnerAction& action) {
232 return internal::WithArgsAction<InnerAction$for j [[, k$j]]>(action);
233}
234
235
236]]
237// Creates an action that does actions a1, a2, ..., sequentially in
238// each invocation.
239$range i 2..n
240$for i [[
241$range j 2..i
242$var types = [[$for j, [[typename Action$j]]]]
243$var Aas = [[$for j [[, Action$j a$j]]]]
244
245template <typename Action1, $types>
246$range k 1..i-1
247
248inline $for k [[internal::DoBothAction<Action$k, ]]Action$i$for k [[>]]
249
250DoAll(Action1 a1$Aas) {
251$if i==2 [[
252
253 return internal::DoBothAction<Action1, Action2>(a1, a2);
254]] $else [[
255$range j2 2..i
256
257 return DoAll(a1, DoAll($for j2, [[a$j2]]));
258]]
259
260}
261
262]]
263
264} // namespace testing
265
shiqian326aa562009-01-09 21:43:57 +0000266// The ACTION* family of macros can be used in a namespace scope to
267// define custom actions easily. The syntax:
268//
269// ACTION(name) { statements; }
270//
271// will define an action with the given name that executes the
272// statements. The value returned by the statements will be used as
273// the return value of the action. Inside the statements, you can
274// refer to the K-th (0-based) argument of the mock function by
275// 'argK', and refer to its type by 'argK_type'. For example:
276//
277// ACTION(IncrementArg1) {
278// arg1_type temp = arg1;
279// return ++(*temp);
280// }
281//
282// allows you to write
283//
284// ...WillOnce(IncrementArg1());
285//
286// You can also refer to the entire argument tuple and its type by
287// 'args' and 'args_type', and refer to the mock function type and its
288// return type by 'function_type' and 'return_type'.
289//
290// Note that you don't need to specify the types of the mock function
291// arguments. However rest assured that your code is still type-safe:
292// you'll get a compiler error if *arg1 doesn't support the ++
293// operator, or if the type of ++(*arg1) isn't compatible with the
294// mock function's return type, for example.
295//
296// Sometimes you'll want to parameterize the action. For that you can use
297// another macro:
298//
299// ACTION_P(name, param_name) { statements; }
300//
301// For example:
302//
303// ACTION_P(Add, n) { return arg0 + n; }
304//
305// will allow you to write:
306//
307// ...WillOnce(Add(5));
308//
309// Note that you don't need to provide the type of the parameter
310// either. If you need to reference the type of a parameter named
311// 'foo', you can write 'foo_type'. For example, in the body of
312// ACTION_P(Add, n) above, you can write 'n_type' to refer to the type
313// of 'n'.
314//
315// We also provide ACTION_P2, ACTION_P3, ..., up to ACTION_P$n to support
316// multi-parameter actions.
317//
318// For the purpose of typing, you can view
319//
320// ACTION_Pk(Foo, p1, ..., pk) { ... }
321//
322// as shorthand for
323//
324// template <typename p1_type, ..., typename pk_type>
325// FooActionPk<p1_type, ..., pk_type> Foo(p1_type p1, ..., pk_type pk) { ... }
326//
327// In particular, you can provide the template type arguments
328// explicitly when invoking Foo(), as in Foo<long, bool>(5, false);
329// although usually you can rely on the compiler to infer the types
330// for you automatically. You can assign the result of expression
331// Foo(p1, ..., pk) to a variable of type FooActionPk<p1_type, ...,
332// pk_type>. This can be useful when composing actions.
333//
334// You can also overload actions with different numbers of parameters:
335//
336// ACTION_P(Plus, a) { ... }
337// ACTION_P2(Plus, a, b) { ... }
338//
339// While it's tempting to always use the ACTION* macros when defining
340// a new action, you should also consider implementing ActionInterface
341// or using MakePolymorphicAction() instead, especially if you need to
342// use the action a lot. While these approaches require more work,
343// they give you more control on the types of the mock function
344// arguments and the action parameters, which in general leads to
345// better compiler error messages that pay off in the long run. They
346// also allow overloading actions based on parameter types (as opposed
347// to just based on the number of parameters).
348//
349// CAVEAT:
350//
351// ACTION*() can only be used in a namespace scope. The reason is
352// that C++ doesn't yet allow function-local types to be used to
353// instantiate templates. The up-coming C++0x standard will fix this.
354// Once that's done, we'll consider supporting using ACTION*() inside
355// a function.
356//
357// MORE INFORMATION:
358//
359// To learn more about using these macros, please search for 'ACTION'
Herbert Thielene033d8c2017-08-31 18:12:17 +0200360// on https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md
shiqian326aa562009-01-09 21:43:57 +0000361
362$range i 0..n
zhanyong.wan33c0af02009-04-03 00:10:12 +0000363$range k 0..n-1
364
365// An internal macro needed for implementing ACTION*().
366#define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_\
367 const args_type& args GTEST_ATTRIBUTE_UNUSED_
jgm79a367e2012-04-10 16:02:11 +0000368$for k [[, \
zhanyong.wan33c0af02009-04-03 00:10:12 +0000369 arg$k[[]]_type arg$k GTEST_ATTRIBUTE_UNUSED_]]
370
371
zhanyong.wan18490652009-05-11 18:54:08 +0000372// Sometimes you want to give an action explicit template parameters
373// that cannot be inferred from its value parameters. ACTION() and
374// ACTION_P*() don't support that. ACTION_TEMPLATE() remedies that
375// and can be viewed as an extension to ACTION() and ACTION_P*().
376//
377// The syntax:
378//
379// ACTION_TEMPLATE(ActionName,
380// HAS_m_TEMPLATE_PARAMS(kind1, name1, ..., kind_m, name_m),
381// AND_n_VALUE_PARAMS(p1, ..., p_n)) { statements; }
382//
383// defines an action template that takes m explicit template
384// parameters and n value parameters. name_i is the name of the i-th
385// template parameter, and kind_i specifies whether it's a typename,
386// an integral constant, or a template. p_i is the name of the i-th
387// value parameter.
388//
389// Example:
390//
391// // DuplicateArg<k, T>(output) converts the k-th argument of the mock
392// // function to type T and copies it to *output.
393// ACTION_TEMPLATE(DuplicateArg,
394// HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
395// AND_1_VALUE_PARAMS(output)) {
kosakbd018832014-04-02 20:30:00 +0000396// *output = T(::testing::get<k>(args));
zhanyong.wan18490652009-05-11 18:54:08 +0000397// }
398// ...
399// int n;
400// EXPECT_CALL(mock, Foo(_, _))
401// .WillOnce(DuplicateArg<1, unsigned char>(&n));
402//
403// To create an instance of an action template, write:
404//
405// ActionName<t1, ..., t_m>(v1, ..., v_n)
406//
407// where the ts are the template arguments and the vs are the value
408// arguments. The value argument types are inferred by the compiler.
409// If you want to explicitly specify the value argument types, you can
410// provide additional template arguments:
411//
412// ActionName<t1, ..., t_m, u1, ..., u_k>(v1, ..., v_n)
413//
414// where u_i is the desired type of v_i.
415//
416// ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded on the
417// number of value parameters, but not on the number of template
418// parameters. Without the restriction, the meaning of the following
419// is unclear:
420//
421// OverloadedAction<int, bool>(x);
422//
423// Are we using a single-template-parameter action where 'bool' refers
424// to the type of x, or are we using a two-template-parameter action
425// where the compiler is asked to infer the type of x?
426//
427// Implementation notes:
428//
429// GMOCK_INTERNAL_*_HAS_m_TEMPLATE_PARAMS and
430// GMOCK_INTERNAL_*_AND_n_VALUE_PARAMS are internal macros for
431// implementing ACTION_TEMPLATE. The main trick we use is to create
432// new macro invocations when expanding a macro. For example, we have
433//
434// #define ACTION_TEMPLATE(name, template_params, value_params)
435// ... GMOCK_INTERNAL_DECL_##template_params ...
436//
437// which causes ACTION_TEMPLATE(..., HAS_1_TEMPLATE_PARAMS(typename, T), ...)
438// to expand to
439//
440// ... GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(typename, T) ...
441//
442// Since GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS is a macro, the
443// preprocessor will continue to expand it to
444//
445// ... typename T ...
446//
447// This technique conforms to the C++ standard and is portable. It
448// allows us to implement action templates using O(N) code, where N is
449// the maximum number of template/value parameters supported. Without
450// using it, we'd have to devote O(N^2) amount of code to implement all
451// combinations of m and n.
452
453// Declares the template parameters.
454
455$range j 1..n
456$for j [[
457$range m 0..j-1
458#define GMOCK_INTERNAL_DECL_HAS_$j[[]]
459_TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[kind$m name$m]]
460
461
462]]
463
464// Lists the template parameters.
465
466$for j [[
467$range m 0..j-1
468#define GMOCK_INTERNAL_LIST_HAS_$j[[]]
469_TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[name$m]]
470
471
472]]
473
474// Declares the types of value parameters.
475
476$for i [[
477$range j 0..i-1
478#define GMOCK_INTERNAL_DECL_TYPE_AND_$i[[]]
479_VALUE_PARAMS($for j, [[p$j]]) $for j [[, typename p$j##_type]]
480
481
482]]
483
484// Initializes the value parameters.
485
486$for i [[
487$range j 0..i-1
488#define GMOCK_INTERNAL_INIT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])\
489 ($for j, [[p$j##_type gmock_p$j]])$if i>0 [[ : ]]$for j, [[p$j(gmock_p$j)]]
490
491
492]]
493
494// Declares the fields for storing the value parameters.
495
496$for i [[
497$range j 0..i-1
498#define GMOCK_INTERNAL_DEFN_AND_$i[[]]
499_VALUE_PARAMS($for j, [[p$j]]) $for j [[p$j##_type p$j; ]]
500
501
502]]
503
504// Lists the value parameters.
505
506$for i [[
507$range j 0..i-1
508#define GMOCK_INTERNAL_LIST_AND_$i[[]]
509_VALUE_PARAMS($for j, [[p$j]]) $for j, [[p$j]]
510
511
512]]
513
514// Lists the value parameter types.
515
516$for i [[
517$range j 0..i-1
518#define GMOCK_INTERNAL_LIST_TYPE_AND_$i[[]]
519_VALUE_PARAMS($for j, [[p$j]]) $for j [[, p$j##_type]]
520
521
522]]
523
524// Declares the value parameters.
525
526$for i [[
527$range j 0..i-1
528#define GMOCK_INTERNAL_DECL_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
529$for j, [[p$j##_type p$j]]
530
531
532]]
533
534// The suffix of the class template implementing the action template.
535$for i [[
536
537
538$range j 0..i-1
539#define GMOCK_INTERNAL_COUNT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
540$if i==1 [[P]] $elif i>=2 [[P$i]]
541]]
542
543
544// The name of the class template implementing the action template.
545#define GMOCK_ACTION_CLASS_(name, value_params)\
zhanyong.wanccedc1c2010-08-09 22:46:12 +0000546 GTEST_CONCAT_TOKEN_(name##Action, GMOCK_INTERNAL_COUNT_##value_params)
zhanyong.wan18490652009-05-11 18:54:08 +0000547
548$range k 0..n-1
549
550#define ACTION_TEMPLATE(name, template_params, value_params)\
551 template <GMOCK_INTERNAL_DECL_##template_params\
552 GMOCK_INTERNAL_DECL_TYPE_##value_params>\
553 class GMOCK_ACTION_CLASS_(name, value_params) {\
554 public:\
billydonahue1f5fdea2014-05-19 17:54:51 +0000555 explicit GMOCK_ACTION_CLASS_(name, value_params)\
zhanyong.wan18490652009-05-11 18:54:08 +0000556 GMOCK_INTERNAL_INIT_##value_params {}\
557 template <typename F>\
558 class gmock_Impl : public ::testing::ActionInterface<F> {\
559 public:\
560 typedef F function_type;\
561 typedef typename ::testing::internal::Function<F>::Result return_type;\
562 typedef typename ::testing::internal::Function<F>::ArgumentTuple\
563 args_type;\
564 explicit gmock_Impl GMOCK_INTERNAL_INIT_##value_params {}\
565 virtual return_type Perform(const args_type& args) {\
566 return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
567 Perform(this, args);\
568 }\
569 template <$for k, [[typename arg$k[[]]_type]]>\
570 return_type gmock_PerformImpl(const args_type& args[[]]
571$for k [[, arg$k[[]]_type arg$k]]) const;\
572 GMOCK_INTERNAL_DEFN_##value_params\
zhanyong.wan32de5f52009-12-23 00:13:23 +0000573 private:\
574 GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
zhanyong.wan18490652009-05-11 18:54:08 +0000575 };\
576 template <typename F> operator ::testing::Action<F>() const {\
577 return ::testing::Action<F>(\
578 new gmock_Impl<F>(GMOCK_INTERNAL_LIST_##value_params));\
579 }\
580 GMOCK_INTERNAL_DEFN_##value_params\
zhanyong.wan32de5f52009-12-23 00:13:23 +0000581 private:\
582 GTEST_DISALLOW_ASSIGN_(GMOCK_ACTION_CLASS_(name, value_params));\
zhanyong.wan18490652009-05-11 18:54:08 +0000583 };\
584 template <GMOCK_INTERNAL_DECL_##template_params\
585 GMOCK_INTERNAL_DECL_TYPE_##value_params>\
586 inline GMOCK_ACTION_CLASS_(name, value_params)<\
587 GMOCK_INTERNAL_LIST_##template_params\
588 GMOCK_INTERNAL_LIST_TYPE_##value_params> name(\
589 GMOCK_INTERNAL_DECL_##value_params) {\
590 return GMOCK_ACTION_CLASS_(name, value_params)<\
591 GMOCK_INTERNAL_LIST_##template_params\
592 GMOCK_INTERNAL_LIST_TYPE_##value_params>(\
593 GMOCK_INTERNAL_LIST_##value_params);\
594 }\
595 template <GMOCK_INTERNAL_DECL_##template_params\
596 GMOCK_INTERNAL_DECL_TYPE_##value_params>\
597 template <typename F>\
jgm79a367e2012-04-10 16:02:11 +0000598 template <typename arg0_type, typename arg1_type, typename arg2_type, \
599 typename arg3_type, typename arg4_type, typename arg5_type, \
600 typename arg6_type, typename arg7_type, typename arg8_type, \
zhanyong.wan18490652009-05-11 18:54:08 +0000601 typename arg9_type>\
602 typename ::testing::internal::Function<F>::Result\
603 GMOCK_ACTION_CLASS_(name, value_params)<\
604 GMOCK_INTERNAL_LIST_##template_params\
605 GMOCK_INTERNAL_LIST_TYPE_##value_params>::gmock_Impl<F>::\
606 gmock_PerformImpl(\
607 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
608
shiqian326aa562009-01-09 21:43:57 +0000609$for i
610
611[[
612$var template = [[$if i==0 [[]] $else [[
613$range j 0..i-1
614
615 template <$for j, [[typename p$j##_type]]>\
616]]]]
617$var class_name = [[name##Action[[$if i==0 [[]] $elif i==1 [[P]]
618 $else [[P$i]]]]]]
619$range j 0..i-1
620$var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
621$var param_types_and_names = [[$for j, [[p$j##_type p$j]]]]
622$var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]]
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000623$var param_field_decls = [[$for j
shiqian326aa562009-01-09 21:43:57 +0000624[[
625
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000626 p$j##_type p$j;\
shiqian326aa562009-01-09 21:43:57 +0000627]]]]
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000628$var param_field_decls2 = [[$for j
shiqian326aa562009-01-09 21:43:57 +0000629[[
630
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000631 p$j##_type p$j;\
shiqian326aa562009-01-09 21:43:57 +0000632]]]]
633$var params = [[$for j, [[p$j]]]]
634$var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]]
shiqian326aa562009-01-09 21:43:57 +0000635$var typename_arg_types = [[$for k, [[typename arg$k[[]]_type]]]]
636$var arg_types_and_names = [[$for k, [[arg$k[[]]_type arg$k]]]]
637$var macro_name = [[$if i==0 [[ACTION]] $elif i==1 [[ACTION_P]]
638 $else [[ACTION_P$i]]]]
639
640#define $macro_name(name$for j [[, p$j]])\$template
641 class $class_name {\
642 public:\
billydonahue1f5fdea2014-05-19 17:54:51 +0000643 [[$if i==1 [[explicit ]]]]$class_name($ctor_param_list)$inits {}\
shiqian326aa562009-01-09 21:43:57 +0000644 template <typename F>\
645 class gmock_Impl : public ::testing::ActionInterface<F> {\
646 public:\
647 typedef F function_type;\
648 typedef typename ::testing::internal::Function<F>::Result return_type;\
649 typedef typename ::testing::internal::Function<F>::ArgumentTuple\
650 args_type;\
651 [[$if i==1 [[explicit ]]]]gmock_Impl($ctor_param_list)$inits {}\
652 virtual return_type Perform(const args_type& args) {\
653 return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
654 Perform(this, args);\
655 }\
656 template <$typename_arg_types>\
657 return_type gmock_PerformImpl(const args_type& args, [[]]
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000658$arg_types_and_names) const;\$param_field_decls
zhanyong.wan32de5f52009-12-23 00:13:23 +0000659 private:\
660 GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
shiqian326aa562009-01-09 21:43:57 +0000661 };\
662 template <typename F> operator ::testing::Action<F>() const {\
663 return ::testing::Action<F>(new gmock_Impl<F>($params));\
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000664 }\$param_field_decls2
zhanyong.wan32de5f52009-12-23 00:13:23 +0000665 private:\
666 GTEST_DISALLOW_ASSIGN_($class_name);\
shiqian326aa562009-01-09 21:43:57 +0000667 };\$template
668 inline $class_name$param_types name($param_types_and_names) {\
669 return $class_name$param_types($params);\
670 }\$template
671 template <typename F>\
672 template <$typename_arg_types>\
673 typename ::testing::internal::Function<F>::Result\
zhanyong.wan33c0af02009-04-03 00:10:12 +0000674 $class_name$param_types::gmock_Impl<F>::gmock_PerformImpl(\
675 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
shiqian326aa562009-01-09 21:43:57 +0000676]]
zhanyong.wan7f4c2c02009-02-19 22:38:27 +0000677$$ } // This meta comment fixes auto-indentation in Emacs. It won't
678$$ // show up in the generated code.
shiqian326aa562009-01-09 21:43:57 +0000679
680
zhanyong.wane1cdce52009-02-06 01:09:43 +0000681namespace testing {
682
kosak67c377d2015-07-19 20:39:47 +0000683
zhanyong.wan32de5f52009-12-23 00:13:23 +0000684// The ACTION*() macros trigger warning C4100 (unreferenced formal
685// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
686// the macro definition, as the warnings are generated when the macro
687// is expanded and macro expansion cannot contain #pragma. Therefore
688// we suppress them here.
689#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000690# pragma warning(push)
691# pragma warning(disable:4100)
zhanyong.wan32de5f52009-12-23 00:13:23 +0000692#endif
693
zhanyong.wan16cf4732009-05-14 20:55:30 +0000694// Various overloads for InvokeArgument<N>().
695//
696// The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th
697// (0-based) argument, which must be a k-ary callable, of the mock
698// function, with arguments a1, a2, ..., a_k.
699//
700// Notes:
701//
702// 1. The arguments are passed by value by default. If you need to
703// pass an argument by reference, wrap it inside ByRef(). For
704// example,
705//
706// InvokeArgument<1>(5, string("Hello"), ByRef(foo))
707//
708// passes 5 and string("Hello") by value, and passes foo by
709// reference.
710//
711// 2. If the callable takes an argument by reference but ByRef() is
712// not used, it will receive the reference to a copy of the value,
713// instead of the original value. For example, when the 0-th
714// argument of the mock function takes a const string&, the action
715//
716// InvokeArgument<0>(string("Hello"))
717//
718// makes a copy of the temporary string("Hello") object and passes a
719// reference of the copy, instead of the original temporary object,
720// to the callable. This makes it easy for a user to define an
721// InvokeArgument action from temporary values and have it performed
722// later.
zhanyong.wan7f4c2c02009-02-19 22:38:27 +0000723
kosak67c377d2015-07-19 20:39:47 +0000724namespace internal {
725namespace invoke_argument {
726
727// Appears in InvokeArgumentAdl's argument list to help avoid
728// accidental calls to user functions of the same name.
729struct AdlTag {};
730
731// InvokeArgumentAdl - a helper for InvokeArgument.
732// The basic overloads are provided here for generic functors.
733// Overloads for other custom-callables are provided in the
734// internal/custom/callback-actions.h header.
735
736$range i 0..n
737$for i
738[[
739$range j 1..i
740
741template <typename R, typename F[[$for j [[, typename A$j]]]]>
742R InvokeArgumentAdl(AdlTag, F f[[$for j [[, A$j a$j]]]]) {
743 return f([[$for j, [[a$j]]]]);
744}
745]]
746
747} // namespace invoke_argument
748} // namespace internal
749
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000750$range i 0..n
751$for i [[
zhanyong.wan16cf4732009-05-14 20:55:30 +0000752$range j 0..i-1
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000753
zhanyong.wan16cf4732009-05-14 20:55:30 +0000754ACTION_TEMPLATE(InvokeArgument,
755 HAS_1_TEMPLATE_PARAMS(int, k),
756 AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])) {
kosak67c377d2015-07-19 20:39:47 +0000757 using internal::invoke_argument::InvokeArgumentAdl;
758 return InvokeArgumentAdl<return_type>(
759 internal::invoke_argument::AdlTag(),
kosakbd018832014-04-02 20:30:00 +0000760 ::testing::get<k>(args)$for j [[, p$j]]);
zhanyong.wan16cf4732009-05-14 20:55:30 +0000761}
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000762
763]]
764
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000765// Various overloads for ReturnNew<T>().
766//
767// The ReturnNew<T>(a1, a2, ..., a_k) action returns a pointer to a new
768// instance of type T, constructed on the heap with constructor arguments
769// a1, a2, ..., and a_k. The caller assumes ownership of the returned value.
770$range i 0..n
771$for i [[
zhanyong.wan16cf4732009-05-14 20:55:30 +0000772$range j 0..i-1
773$var ps = [[$for j, [[p$j]]]]
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000774
zhanyong.wan16cf4732009-05-14 20:55:30 +0000775ACTION_TEMPLATE(ReturnNew,
776 HAS_1_TEMPLATE_PARAMS(typename, T),
777 AND_$i[[]]_VALUE_PARAMS($ps)) {
778 return new T($ps);
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000779}
780
781]]
782
zhanyong.wan32de5f52009-12-23 00:13:23 +0000783#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000784# pragma warning(pop)
zhanyong.wan32de5f52009-12-23 00:13:23 +0000785#endif
786
zhanyong.wane1cdce52009-02-06 01:09:43 +0000787} // namespace testing
788
kosak67c377d2015-07-19 20:39:47 +0000789// Include any custom callback actions added by the local installation.
790// We must include this header at the end to make sure it can use the
791// declarations from this file.
kosak6e108722015-07-28 00:53:13 +0000792#include "gmock/internal/custom/gmock-generated-actions.h"
kosak67c377d2015-07-19 20:39:47 +0000793
shiqiane35fdd92008-12-10 05:08:54 +0000794#endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_