Home | History | Annotate | Download | only in gmock
      1 $$ -*- mode: c++; -*-
      2 $$ This is a Pump source file.  Please use Pump to convert it to
      3 $$ gmock-generated-actions.h.
      4 $$
      5 $var n = 10  $$ The maximum arity we support.
      6 $$}} This meta comment fixes auto-indentation in editors.
      7 // 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 (a] 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 
     48 namespace testing {
     49 namespace 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.
     54 template <typename Result, typename ArgumentTuple>
     55 class 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)]]]]
     69 template <typename R$types>
     70 class 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 // CallableHelper has static methods for invoking "callables",
     88 // i.e. function pointers and functors.  It uses overloading to
     89 // provide a uniform interface for invoking different kinds of
     90 // callables.  In particular, you can use:
     91 //
     92 //   CallableHelper<R>::Call(callable, a1, a2, ..., an)
     93 //
     94 // to invoke an n-ary callable, where R is its return type.  If an
     95 // argument, say a2, needs to be passed by reference, you should write
     96 // ByRef(a2) instead of a2 in the above expression.
     97 template <typename R>
     98 class CallableHelper {
     99  public:
    100   // Calls a nullary callable.
    101   template <typename Function>
    102   static R Call(Function function) { return function(); }
    103 
    104   // Calls a unary callable.
    105 
    106   // We deliberately pass a1 by value instead of const reference here
    107   // in case it is a C-string literal.  If we had declared the
    108   // parameter as 'const A1& a1' and write Call(function, "Hi"), the
    109   // compiler would've thought A1 is 'char[3]', which causes trouble
    110   // when you need to copy a value of type A1.  By declaring the
    111   // parameter as 'A1 a1', the compiler will correctly infer that A1
    112   // is 'const char*' when it sees Call(function, "Hi").
    113   //
    114   // Since this function is defined inline, the compiler can get rid
    115   // of the copying of the arguments.  Therefore the performance won't
    116   // be hurt.
    117   template <typename Function, typename A1>
    118   static R Call(Function function, A1 a1) { return function(a1); }
    119 
    120 $range i 2..n
    121 $for i
    122 [[
    123 $var arity = [[$if i==2 [[binary]] $elif i==3 [[ternary]] $else [[$i-ary]]]]
    124 
    125   // Calls a $arity callable.
    126 
    127 $range j 1..i
    128 $var typename_As = [[$for j, [[typename A$j]]]]
    129 $var Aas = [[$for j, [[A$j a$j]]]]
    130 $var as = [[$for j, [[a$j]]]]
    131 $var typename_Ts = [[$for j, [[typename T$j]]]]
    132 $var Ts = [[$for j, [[T$j]]]]
    133   template <typename Function, $typename_As>
    134   static R Call(Function function, $Aas) {
    135     return function($as);
    136   }
    137 
    138 ]]
    139 };  // class CallableHelper
    140 
    141 // An INTERNAL macro for extracting the type of a tuple field.  It's
    142 // subject to change without notice - DO NOT USE IN USER CODE!
    143 #define GMOCK_FIELD_(Tuple, N) \
    144     typename ::std::tr1::tuple_element<N, Tuple>::type
    145 
    146 $range i 1..n
    147 
    148 // SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::type is the
    149 // type of an n-ary function whose i-th (1-based) argument type is the
    150 // k{i}-th (0-based) field of ArgumentTuple, which must be a tuple
    151 // type, and whose return type is Result.  For example,
    152 //   SelectArgs<int, ::std::tr1::tuple<bool, char, double, long>, 0, 3>::type
    153 // is int(bool, long).
    154 //
    155 // SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::Select(args)
    156 // returns the selected fields (k1, k2, ..., k_n) of args as a tuple.
    157 // For example,
    158 //   SelectArgs<int, ::std::tr1::tuple<bool, char, double>, 2, 0>::Select(
    159 //       ::std::tr1::make_tuple(true, 'a', 2.5))
    160 // returns ::std::tr1::tuple (2.5, true).
    161 //
    162 // The numbers in list k1, k2, ..., k_n must be >= 0, where n can be
    163 // in the range [0, $n].  Duplicates are allowed and they don't have
    164 // to be in an ascending or descending order.
    165 
    166 template <typename Result, typename ArgumentTuple, $for i, [[int k$i]]>
    167 class SelectArgs {
    168  public:
    169   typedef Result type($for i, [[GMOCK_FIELD_(ArgumentTuple, k$i)]]);
    170   typedef typename Function<type>::ArgumentTuple SelectedArgs;
    171   static SelectedArgs Select(const ArgumentTuple& args) {
    172     using ::std::tr1::get;
    173     return SelectedArgs($for i, [[get<k$i>(args)]]);
    174   }
    175 };
    176 
    177 
    178 $for i [[
    179 $range j 1..n
    180 $range j1 1..i-1
    181 template <typename Result, typename ArgumentTuple$for j1[[, int k$j1]]>
    182 class SelectArgs<Result, ArgumentTuple,
    183                  $for j, [[$if j <= i-1 [[k$j]] $else [[-1]]]]> {
    184  public:
    185   typedef Result type($for j1, [[GMOCK_FIELD_(ArgumentTuple, k$j1)]]);
    186   typedef typename Function<type>::ArgumentTuple SelectedArgs;
    187   static SelectedArgs Select(const ArgumentTuple& [[]]
    188 $if i == 1 [[/* args */]] $else [[args]]) {
    189     using ::std::tr1::get;
    190     return SelectedArgs($for j1, [[get<k$j1>(args)]]);
    191   }
    192 };
    193 
    194 
    195 ]]
    196 #undef GMOCK_FIELD_
    197 
    198 $var ks = [[$for i, [[k$i]]]]
    199 
    200 // Implements the WithArgs action.
    201 template <typename InnerAction, $for i, [[int k$i = -1]]>
    202 class WithArgsAction {
    203  public:
    204   explicit WithArgsAction(const InnerAction& action) : action_(action) {}
    205 
    206   template <typename F>
    207   operator Action<F>() const { return MakeAction(new Impl<F>(action_)); }
    208 
    209  private:
    210   template <typename F>
    211   class Impl : public ActionInterface<F> {
    212    public:
    213     typedef typename Function<F>::Result Result;
    214     typedef typename Function<F>::ArgumentTuple ArgumentTuple;
    215 
    216     explicit Impl(const InnerAction& action) : action_(action) {}
    217 
    218     virtual Result Perform(const ArgumentTuple& args) {
    219       return action_.Perform(SelectArgs<Result, ArgumentTuple, $ks>::Select(args));
    220     }
    221 
    222    private:
    223     typedef typename SelectArgs<Result, ArgumentTuple,
    224         $ks>::type InnerFunctionType;
    225 
    226     Action<InnerFunctionType> action_;
    227   };
    228 
    229   const InnerAction action_;
    230 
    231   GTEST_DISALLOW_ASSIGN_(WithArgsAction);
    232 };
    233 
    234 // A macro from the ACTION* family (defined later in this file)
    235 // defines an action that can be used in a mock function.  Typically,
    236 // these actions only care about a subset of the arguments of the mock
    237 // function.  For example, if such an action only uses the second
    238 // argument, it can be used in any mock function that takes >= 2
    239 // arguments where the type of the second argument is compatible.
    240 //
    241 // Therefore, the action implementation must be prepared to take more
    242 // arguments than it needs.  The ExcessiveArg type is used to
    243 // represent those excessive arguments.  In order to keep the compiler
    244 // error messages tractable, we define it in the testing namespace
    245 // instead of testing::internal.  However, this is an INTERNAL TYPE
    246 // and subject to change without notice, so a user MUST NOT USE THIS
    247 // TYPE DIRECTLY.
    248 struct ExcessiveArg {};
    249 
    250 // A helper class needed for implementing the ACTION* macros.
    251 template <typename Result, class Impl>
    252 class ActionHelper {
    253  public:
    254 $range i 0..n
    255 $for i
    256 
    257 [[
    258 $var template = [[$if i==0 [[]] $else [[
    259 $range j 0..i-1
    260   template <$for j, [[typename A$j]]>
    261 ]]]]
    262 $range j 0..i-1
    263 $var As = [[$for j, [[A$j]]]]
    264 $var as = [[$for j, [[get<$j>(args)]]]]
    265 $range k 1..n-i
    266 $var eas = [[$for k, [[ExcessiveArg()]]]]
    267 $var arg_list = [[$if (i==0) | (i==n) [[$as$eas]] $else [[$as, $eas]]]]
    268 $template
    269   static Result Perform(Impl* impl, const ::std::tr1::tuple<$As>& args) {
    270     using ::std::tr1::get;
    271     return impl->template gmock_PerformImpl<$As>(args, $arg_list);
    272   }
    273 
    274 ]]
    275 };
    276 
    277 }  // namespace internal
    278 
    279 // Various overloads for Invoke().
    280 
    281 // WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes
    282 // the selected arguments of the mock function to an_action and
    283 // performs it.  It serves as an adaptor between actions with
    284 // different argument lists.  C++ doesn't support default arguments for
    285 // function templates, so we have to overload it.
    286 
    287 $range i 1..n
    288 $for i [[
    289 $range j 1..i
    290 template <$for j [[int k$j, ]]typename InnerAction>
    291 inline internal::WithArgsAction<InnerAction$for j [[, k$j]]>
    292 WithArgs(const InnerAction& action) {
    293   return internal::WithArgsAction<InnerAction$for j [[, k$j]]>(action);
    294 }
    295 
    296 
    297 ]]
    298 // Creates an action that does actions a1, a2, ..., sequentially in
    299 // each invocation.
    300 $range i 2..n
    301 $for i [[
    302 $range j 2..i
    303 $var types = [[$for j, [[typename Action$j]]]]
    304 $var Aas = [[$for j [[, Action$j a$j]]]]
    305 
    306 template <typename Action1, $types>
    307 $range k 1..i-1
    308 
    309 inline $for k [[internal::DoBothAction<Action$k, ]]Action$i$for k  [[>]]
    310 
    311 DoAll(Action1 a1$Aas) {
    312 $if i==2 [[
    313 
    314   return internal::DoBothAction<Action1, Action2>(a1, a2);
    315 ]] $else [[
    316 $range j2 2..i
    317 
    318   return DoAll(a1, DoAll($for j2, [[a$j2]]));
    319 ]]
    320 
    321 }
    322 
    323 ]]
    324 
    325 }  // namespace testing
    326 
    327 // The ACTION* family of macros can be used in a namespace scope to
    328 // define custom actions easily.  The syntax:
    329 //
    330 //   ACTION(name) { statements; }
    331 //
    332 // will define an action with the given name that executes the
    333 // statements.  The value returned by the statements will be used as
    334 // the return value of the action.  Inside the statements, you can
    335 // refer to the K-th (0-based) argument of the mock function by
    336 // 'argK', and refer to its type by 'argK_type'.  For example:
    337 //
    338 //   ACTION(IncrementArg1) {
    339 //     arg1_type temp = arg1;
    340 //     return ++(*temp);
    341 //   }
    342 //
    343 // allows you to write
    344 //
    345 //   ...WillOnce(IncrementArg1());
    346 //
    347 // You can also refer to the entire argument tuple and its type by
    348 // 'args' and 'args_type', and refer to the mock function type and its
    349 // return type by 'function_type' and 'return_type'.
    350 //
    351 // Note that you don't need to specify the types of the mock function
    352 // arguments.  However rest assured that your code is still type-safe:
    353 // you'll get a compiler error if *arg1 doesn't support the ++
    354 // operator, or if the type of ++(*arg1) isn't compatible with the
    355 // mock function's return type, for example.
    356 //
    357 // Sometimes you'll want to parameterize the action.   For that you can use
    358 // another macro:
    359 //
    360 //   ACTION_P(name, param_name) { statements; }
    361 //
    362 // For example:
    363 //
    364 //   ACTION_P(Add, n) { return arg0 + n; }
    365 //
    366 // will allow you to write:
    367 //
    368 //   ...WillOnce(Add(5));
    369 //
    370 // Note that you don't need to provide the type of the parameter
    371 // either.  If you need to reference the type of a parameter named
    372 // 'foo', you can write 'foo_type'.  For example, in the body of
    373 // ACTION_P(Add, n) above, you can write 'n_type' to refer to the type
    374 // of 'n'.
    375 //
    376 // We also provide ACTION_P2, ACTION_P3, ..., up to ACTION_P$n to support
    377 // multi-parameter actions.
    378 //
    379 // For the purpose of typing, you can view
    380 //
    381 //   ACTION_Pk(Foo, p1, ..., pk) { ... }
    382 //
    383 // as shorthand for
    384 //
    385 //   template <typename p1_type, ..., typename pk_type>
    386 //   FooActionPk<p1_type, ..., pk_type> Foo(p1_type p1, ..., pk_type pk) { ... }
    387 //
    388 // In particular, you can provide the template type arguments
    389 // explicitly when invoking Foo(), as in Foo<long, bool>(5, false);
    390 // although usually you can rely on the compiler to infer the types
    391 // for you automatically.  You can assign the result of expression
    392 // Foo(p1, ..., pk) to a variable of type FooActionPk<p1_type, ...,
    393 // pk_type>.  This can be useful when composing actions.
    394 //
    395 // You can also overload actions with different numbers of parameters:
    396 //
    397 //   ACTION_P(Plus, a) { ... }
    398 //   ACTION_P2(Plus, a, b) { ... }
    399 //
    400 // While it's tempting to always use the ACTION* macros when defining
    401 // a new action, you should also consider implementing ActionInterface
    402 // or using MakePolymorphicAction() instead, especially if you need to
    403 // use the action a lot.  While these approaches require more work,
    404 // they give you more control on the types of the mock function
    405 // arguments and the action parameters, which in general leads to
    406 // better compiler error messages that pay off in the long run.  They
    407 // also allow overloading actions based on parameter types (as opposed
    408 // to just based on the number of parameters).
    409 //
    410 // CAVEAT:
    411 //
    412 // ACTION*() can only be used in a namespace scope.  The reason is
    413 // that C++ doesn't yet allow function-local types to be used to
    414 // instantiate templates.  The up-coming C++0x standard will fix this.
    415 // Once that's done, we'll consider supporting using ACTION*() inside
    416 // a function.
    417 //
    418 // MORE INFORMATION:
    419 //
    420 // To learn more about using these macros, please search for 'ACTION'
    421 // on http://code.google.com/p/googlemock/wiki/CookBook.
    422 
    423 $range i 0..n
    424 $range k 0..n-1
    425 
    426 // An internal macro needed for implementing ACTION*().
    427 #define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_\
    428     const args_type& args GTEST_ATTRIBUTE_UNUSED_
    429 $for k [[, \
    430     arg$k[[]]_type arg$k GTEST_ATTRIBUTE_UNUSED_]]
    431 
    432 
    433 // Sometimes you want to give an action explicit template parameters
    434 // that cannot be inferred from its value parameters.  ACTION() and
    435 // ACTION_P*() don't support that.  ACTION_TEMPLATE() remedies that
    436 // and can be viewed as an extension to ACTION() and ACTION_P*().
    437 //
    438 // The syntax:
    439 //
    440 //   ACTION_TEMPLATE(ActionName,
    441 //                   HAS_m_TEMPLATE_PARAMS(kind1, name1, ..., kind_m, name_m),
    442 //                   AND_n_VALUE_PARAMS(p1, ..., p_n)) { statements; }
    443 //
    444 // defines an action template that takes m explicit template
    445 // parameters and n value parameters.  name_i is the name of the i-th
    446 // template parameter, and kind_i specifies whether it's a typename,
    447 // an integral constant, or a template.  p_i is the name of the i-th
    448 // value parameter.
    449 //
    450 // Example:
    451 //
    452 //   // DuplicateArg<k, T>(output) converts the k-th argument of the mock
    453 //   // function to type T and copies it to *output.
    454 //   ACTION_TEMPLATE(DuplicateArg,
    455 //                   HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
    456 //                   AND_1_VALUE_PARAMS(output)) {
    457 //     *output = T(std::tr1::get<k>(args));
    458 //   }
    459 //   ...
    460 //     int n;
    461 //     EXPECT_CALL(mock, Foo(_, _))
    462 //         .WillOnce(DuplicateArg<1, unsigned char>(&n));
    463 //
    464 // To create an instance of an action template, write:
    465 //
    466 //   ActionName<t1, ..., t_m>(v1, ..., v_n)
    467 //
    468 // where the ts are the template arguments and the vs are the value
    469 // arguments.  The value argument types are inferred by the compiler.
    470 // If you want to explicitly specify the value argument types, you can
    471 // provide additional template arguments:
    472 //
    473 //   ActionName<t1, ..., t_m, u1, ..., u_k>(v1, ..., v_n)
    474 //
    475 // where u_i is the desired type of v_i.
    476 //
    477 // ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded on the
    478 // number of value parameters, but not on the number of template
    479 // parameters.  Without the restriction, the meaning of the following
    480 // is unclear:
    481 //
    482 //   OverloadedAction<int, bool>(x);
    483 //
    484 // Are we using a single-template-parameter action where 'bool' refers
    485 // to the type of x, or are we using a two-template-parameter action
    486 // where the compiler is asked to infer the type of x?
    487 //
    488 // Implementation notes:
    489 //
    490 // GMOCK_INTERNAL_*_HAS_m_TEMPLATE_PARAMS and
    491 // GMOCK_INTERNAL_*_AND_n_VALUE_PARAMS are internal macros for
    492 // implementing ACTION_TEMPLATE.  The main trick we use is to create
    493 // new macro invocations when expanding a macro.  For example, we have
    494 //
    495 //   #define ACTION_TEMPLATE(name, template_params, value_params)
    496 //       ... GMOCK_INTERNAL_DECL_##template_params ...
    497 //
    498 // which causes ACTION_TEMPLATE(..., HAS_1_TEMPLATE_PARAMS(typename, T), ...)
    499 // to expand to
    500 //
    501 //       ... GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(typename, T) ...
    502 //
    503 // Since GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS is a macro, the
    504 // preprocessor will continue to expand it to
    505 //
    506 //       ... typename T ...
    507 //
    508 // This technique conforms to the C++ standard and is portable.  It
    509 // allows us to implement action templates using O(N) code, where N is
    510 // the maximum number of template/value parameters supported.  Without
    511 // using it, we'd have to devote O(N^2) amount of code to implement all
    512 // combinations of m and n.
    513 
    514 // Declares the template parameters.
    515 
    516 $range j 1..n
    517 $for j [[
    518 $range m 0..j-1
    519 #define GMOCK_INTERNAL_DECL_HAS_$j[[]]
    520 _TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[kind$m name$m]]
    521 
    522 
    523 ]]
    524 
    525 // Lists the template parameters.
    526 
    527 $for j [[
    528 $range m 0..j-1
    529 #define GMOCK_INTERNAL_LIST_HAS_$j[[]]
    530 _TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[name$m]]
    531 
    532 
    533 ]]
    534 
    535 // Declares the types of value parameters.
    536 
    537 $for i [[
    538 $range j 0..i-1
    539 #define GMOCK_INTERNAL_DECL_TYPE_AND_$i[[]]
    540 _VALUE_PARAMS($for j, [[p$j]]) $for j [[, typename p$j##_type]]
    541 
    542 
    543 ]]
    544 
    545 // Initializes the value parameters.
    546 
    547 $for i [[
    548 $range j 0..i-1
    549 #define GMOCK_INTERNAL_INIT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])\
    550     ($for j, [[p$j##_type gmock_p$j]])$if i>0 [[ : ]]$for j, [[p$j(gmock_p$j)]]
    551 
    552 
    553 ]]
    554 
    555 // Declares the fields for storing the value parameters.
    556 
    557 $for i [[
    558 $range j 0..i-1
    559 #define GMOCK_INTERNAL_DEFN_AND_$i[[]]
    560 _VALUE_PARAMS($for j, [[p$j]]) $for j [[p$j##_type p$j; ]]
    561 
    562 
    563 ]]
    564 
    565 // Lists the value parameters.
    566 
    567 $for i [[
    568 $range j 0..i-1
    569 #define GMOCK_INTERNAL_LIST_AND_$i[[]]
    570 _VALUE_PARAMS($for j, [[p$j]]) $for j, [[p$j]]
    571 
    572 
    573 ]]
    574 
    575 // Lists the value parameter types.
    576 
    577 $for i [[
    578 $range j 0..i-1
    579 #define GMOCK_INTERNAL_LIST_TYPE_AND_$i[[]]
    580 _VALUE_PARAMS($for j, [[p$j]]) $for j [[, p$j##_type]]
    581 
    582 
    583 ]]
    584 
    585 // Declares the value parameters.
    586 
    587 $for i [[
    588 $range j 0..i-1
    589 #define GMOCK_INTERNAL_DECL_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
    590 $for j, [[p$j##_type p$j]]
    591 
    592 
    593 ]]
    594 
    595 // The suffix of the class template implementing the action template.
    596 $for i [[
    597 
    598 
    599 $range j 0..i-1
    600 #define GMOCK_INTERNAL_COUNT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
    601 $if i==1 [[P]] $elif i>=2 [[P$i]]
    602 ]]
    603 
    604 
    605 // The name of the class template implementing the action template.
    606 #define GMOCK_ACTION_CLASS_(name, value_params)\
    607     GTEST_CONCAT_TOKEN_(name##Action, GMOCK_INTERNAL_COUNT_##value_params)
    608 
    609 $range k 0..n-1
    610 
    611 #define ACTION_TEMPLATE(name, template_params, value_params)\
    612   template <GMOCK_INTERNAL_DECL_##template_params\
    613             GMOCK_INTERNAL_DECL_TYPE_##value_params>\
    614   class GMOCK_ACTION_CLASS_(name, value_params) {\
    615    public:\
    616     GMOCK_ACTION_CLASS_(name, value_params)\
    617         GMOCK_INTERNAL_INIT_##value_params {}\
    618     template <typename F>\
    619     class gmock_Impl : public ::testing::ActionInterface<F> {\
    620      public:\
    621       typedef F function_type;\
    622       typedef typename ::testing::internal::Function<F>::Result return_type;\
    623       typedef typename ::testing::internal::Function<F>::ArgumentTuple\
    624           args_type;\
    625       explicit gmock_Impl GMOCK_INTERNAL_INIT_##value_params {}\
    626       virtual return_type Perform(const args_type& args) {\
    627         return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
    628             Perform(this, args);\
    629       }\
    630       template <$for k, [[typename arg$k[[]]_type]]>\
    631       return_type gmock_PerformImpl(const args_type& args[[]]
    632 $for k [[, arg$k[[]]_type arg$k]]) const;\
    633       GMOCK_INTERNAL_DEFN_##value_params\
    634      private:\
    635       GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
    636     };\
    637     template <typename F> operator ::testing::Action<F>() const {\
    638       return ::testing::Action<F>(\
    639           new gmock_Impl<F>(GMOCK_INTERNAL_LIST_##value_params));\
    640     }\
    641     GMOCK_INTERNAL_DEFN_##value_params\
    642    private:\
    643     GTEST_DISALLOW_ASSIGN_(GMOCK_ACTION_CLASS_(name, value_params));\
    644   };\
    645   template <GMOCK_INTERNAL_DECL_##template_params\
    646             GMOCK_INTERNAL_DECL_TYPE_##value_params>\
    647   inline GMOCK_ACTION_CLASS_(name, value_params)<\
    648       GMOCK_INTERNAL_LIST_##template_params\
    649       GMOCK_INTERNAL_LIST_TYPE_##value_params> name(\
    650           GMOCK_INTERNAL_DECL_##value_params) {\
    651     return GMOCK_ACTION_CLASS_(name, value_params)<\
    652         GMOCK_INTERNAL_LIST_##template_params\
    653         GMOCK_INTERNAL_LIST_TYPE_##value_params>(\
    654             GMOCK_INTERNAL_LIST_##value_params);\
    655   }\
    656   template <GMOCK_INTERNAL_DECL_##template_params\
    657             GMOCK_INTERNAL_DECL_TYPE_##value_params>\
    658   template <typename F>\
    659   template <typename arg0_type, typename arg1_type, typename arg2_type, \
    660       typename arg3_type, typename arg4_type, typename arg5_type, \
    661       typename arg6_type, typename arg7_type, typename arg8_type, \
    662       typename arg9_type>\
    663   typename ::testing::internal::Function<F>::Result\
    664       GMOCK_ACTION_CLASS_(name, value_params)<\
    665           GMOCK_INTERNAL_LIST_##template_params\
    666           GMOCK_INTERNAL_LIST_TYPE_##value_params>::gmock_Impl<F>::\
    667               gmock_PerformImpl(\
    668           GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
    669 
    670 $for i
    671 
    672 [[
    673 $var template = [[$if i==0 [[]] $else [[
    674 $range j 0..i-1
    675 
    676   template <$for j, [[typename p$j##_type]]>\
    677 ]]]]
    678 $var class_name = [[name##Action[[$if i==0 [[]] $elif i==1 [[P]]
    679                                                 $else [[P$i]]]]]]
    680 $range j 0..i-1
    681 $var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
    682 $var param_types_and_names = [[$for j, [[p$j##_type p$j]]]]
    683 $var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]]
    684 $var param_field_decls = [[$for j
    685 [[
    686 
    687       p$j##_type p$j;\
    688 ]]]]
    689 $var param_field_decls2 = [[$for j
    690 [[
    691 
    692     p$j##_type p$j;\
    693 ]]]]
    694 $var params = [[$for j, [[p$j]]]]
    695 $var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]]
    696 $var typename_arg_types = [[$for k, [[typename arg$k[[]]_type]]]]
    697 $var arg_types_and_names = [[$for k, [[arg$k[[]]_type arg$k]]]]
    698 $var macro_name = [[$if i==0 [[ACTION]] $elif i==1 [[ACTION_P]]
    699                                         $else [[ACTION_P$i]]]]
    700 
    701 #define $macro_name(name$for j [[, p$j]])\$template
    702   class $class_name {\
    703    public:\
    704     $class_name($ctor_param_list)$inits {}\
    705     template <typename F>\
    706     class gmock_Impl : public ::testing::ActionInterface<F> {\
    707      public:\
    708       typedef F function_type;\
    709       typedef typename ::testing::internal::Function<F>::Result return_type;\
    710       typedef typename ::testing::internal::Function<F>::ArgumentTuple\
    711           args_type;\
    712       [[$if i==1 [[explicit ]]]]gmock_Impl($ctor_param_list)$inits {}\
    713       virtual return_type Perform(const args_type& args) {\
    714         return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
    715             Perform(this, args);\
    716       }\
    717       template <$typename_arg_types>\
    718       return_type gmock_PerformImpl(const args_type& args, [[]]
    719 $arg_types_and_names) const;\$param_field_decls
    720      private:\
    721       GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
    722     };\
    723     template <typename F> operator ::testing::Action<F>() const {\
    724       return ::testing::Action<F>(new gmock_Impl<F>($params));\
    725     }\$param_field_decls2
    726    private:\
    727     GTEST_DISALLOW_ASSIGN_($class_name);\
    728   };\$template
    729   inline $class_name$param_types name($param_types_and_names) {\
    730     return $class_name$param_types($params);\
    731   }\$template
    732   template <typename F>\
    733   template <$typename_arg_types>\
    734   typename ::testing::internal::Function<F>::Result\
    735       $class_name$param_types::gmock_Impl<F>::gmock_PerformImpl(\
    736           GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
    737 ]]
    738 $$ }  // This meta comment fixes auto-indentation in Emacs.  It won't
    739 $$    // show up in the generated code.
    740 
    741 
    742 namespace testing {
    743 
    744 // The ACTION*() macros trigger warning C4100 (unreferenced formal
    745 // parameter) in MSVC with -W4.  Unfortunately they cannot be fixed in
    746 // the macro definition, as the warnings are generated when the macro
    747 // is expanded and macro expansion cannot contain #pragma.  Therefore
    748 // we suppress them here.
    749 #ifdef _MSC_VER
    750 # pragma warning(push)
    751 # pragma warning(disable:4100)
    752 #endif
    753 
    754 // Various overloads for InvokeArgument<N>().
    755 //
    756 // The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th
    757 // (0-based) argument, which must be a k-ary callable, of the mock
    758 // function, with arguments a1, a2, ..., a_k.
    759 //
    760 // Notes:
    761 //
    762 //   1. The arguments are passed by value by default.  If you need to
    763 //   pass an argument by reference, wrap it inside ByRef().  For
    764 //   example,
    765 //
    766 //     InvokeArgument<1>(5, string("Hello"), ByRef(foo))
    767 //
    768 //   passes 5 and string("Hello") by value, and passes foo by
    769 //   reference.
    770 //
    771 //   2. If the callable takes an argument by reference but ByRef() is
    772 //   not used, it will receive the reference to a copy of the value,
    773 //   instead of the original value.  For example, when the 0-th
    774 //   argument of the mock function takes a const string&, the action
    775 //
    776 //     InvokeArgument<0>(string("Hello"))
    777 //
    778 //   makes a copy of the temporary string("Hello") object and passes a
    779 //   reference of the copy, instead of the original temporary object,
    780 //   to the callable.  This makes it easy for a user to define an
    781 //   InvokeArgument action from temporary values and have it performed
    782 //   later.
    783 
    784 $range i 0..n
    785 $for i [[
    786 $range j 0..i-1
    787 
    788 ACTION_TEMPLATE(InvokeArgument,
    789                 HAS_1_TEMPLATE_PARAMS(int, k),
    790                 AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])) {
    791   return internal::CallableHelper<return_type>::Call(
    792       ::std::tr1::get<k>(args)$for j [[, p$j]]);
    793 }
    794 
    795 ]]
    796 
    797 // Various overloads for ReturnNew<T>().
    798 //
    799 // The ReturnNew<T>(a1, a2, ..., a_k) action returns a pointer to a new
    800 // instance of type T, constructed on the heap with constructor arguments
    801 // a1, a2, ..., and a_k. The caller assumes ownership of the returned value.
    802 $range i 0..n
    803 $for i [[
    804 $range j 0..i-1
    805 $var ps = [[$for j, [[p$j]]]]
    806 
    807 ACTION_TEMPLATE(ReturnNew,
    808                 HAS_1_TEMPLATE_PARAMS(typename, T),
    809                 AND_$i[[]]_VALUE_PARAMS($ps)) {
    810   return new T($ps);
    811 }
    812 
    813 ]]
    814 
    815 #ifdef _MSC_VER
    816 # pragma warning(pop)
    817 #endif
    818 
    819 }  // namespace testing
    820 
    821 #endif  // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_
    822