Blame googlemock/include/gmock/gmock-generated-actions.h.pump

Packit bd1cd8
$$ -*- mode: c++; -*-
Packit bd1cd8
$$ This is a Pump source file.  Please use Pump to convert it to
Packit bd1cd8
$$ gmock-generated-actions.h.
Packit bd1cd8
$$
Packit bd1cd8
$var n = 10  $$ The maximum arity we support.
Packit bd1cd8
$$}} This meta comment fixes auto-indentation in editors.
Packit bd1cd8
// Copyright 2007, Google Inc.
Packit bd1cd8
// All rights reserved.
Packit bd1cd8
//
Packit bd1cd8
// Redistribution and use in source and binary forms, with or without
Packit bd1cd8
// modification, are permitted provided that the following conditions are
Packit bd1cd8
// met:
Packit bd1cd8
//
Packit bd1cd8
//     * Redistributions of source code must retain the above copyright
Packit bd1cd8
// notice, this list of conditions and the following disclaimer.
Packit bd1cd8
//     * Redistributions in binary form must reproduce the above
Packit bd1cd8
// copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
// in the documentation and/or other materials provided with the
Packit bd1cd8
// distribution.
Packit bd1cd8
//     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
// contributors may be used to endorse or promote products derived from
Packit bd1cd8
// this software without specific prior written permission.
Packit bd1cd8
//
Packit bd1cd8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
//
Packit bd1cd8
// Author: wan@google.com (Zhanyong Wan)
Packit bd1cd8
Packit bd1cd8
// Google Mock - a framework for writing C++ mock classes.
Packit bd1cd8
//
Packit bd1cd8
// This file implements some commonly used variadic actions.
Packit bd1cd8
Packit bd1cd8
#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_
Packit bd1cd8
#define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_
Packit bd1cd8
Packit bd1cd8
#include "gmock/gmock-actions.h"
Packit bd1cd8
#include "gmock/internal/gmock-port.h"
Packit bd1cd8
Packit bd1cd8
namespace testing {
Packit bd1cd8
namespace internal {
Packit bd1cd8
Packit bd1cd8
// InvokeHelper<F> knows how to unpack an N-tuple and invoke an N-ary
Packit bd1cd8
// function or method with the unpacked values, where F is a function
Packit bd1cd8
// type that takes N arguments.
Packit bd1cd8
template <typename Result, typename ArgumentTuple>
Packit bd1cd8
class InvokeHelper;
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
$range i 0..n
Packit bd1cd8
$for i [[
Packit bd1cd8
$range j 1..i
Packit bd1cd8
$var types = [[$for j [[, typename A$j]]]]
Packit bd1cd8
$var as = [[$for j, [[A$j]]]]
Packit bd1cd8
$var args = [[$if i==0 [[]] $else [[ args]]]]
Packit bd1cd8
$var gets = [[$for j, [[get<$(j - 1)>(args)]]]]
Packit bd1cd8
template <typename R$types>
Packit bd1cd8
class InvokeHelper<R, ::testing::tuple<$as> > {
Packit bd1cd8
 public:
Packit bd1cd8
  template <typename Function>
Packit bd1cd8
  static R Invoke(Function function, const ::testing::tuple<$as>&$args) {
Packit bd1cd8
           return function($gets);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  template <class Class, typename MethodPtr>
Packit bd1cd8
  static R InvokeMethod(Class* obj_ptr,
Packit bd1cd8
                        MethodPtr method_ptr,
Packit bd1cd8
                        const ::testing::tuple<$as>&$args) {
Packit bd1cd8
           return (obj_ptr->*method_ptr)($gets);
Packit bd1cd8
  }
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
]]
Packit bd1cd8
// An INTERNAL macro for extracting the type of a tuple field.  It's
Packit bd1cd8
// subject to change without notice - DO NOT USE IN USER CODE!
Packit bd1cd8
#define GMOCK_FIELD_(Tuple, N) \
Packit bd1cd8
    typename ::testing::tuple_element<N, Tuple>::type
Packit bd1cd8
Packit bd1cd8
$range i 1..n
Packit bd1cd8
Packit bd1cd8
// SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::type is the
Packit bd1cd8
// type of an n-ary function whose i-th (1-based) argument type is the
Packit bd1cd8
// k{i}-th (0-based) field of ArgumentTuple, which must be a tuple
Packit bd1cd8
// type, and whose return type is Result.  For example,
Packit bd1cd8
//   SelectArgs<int, ::testing::tuple<bool, char, double, long>, 0, 3>::type
Packit bd1cd8
// is int(bool, long).
Packit bd1cd8
//
Packit bd1cd8
// SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::Select(args)
Packit bd1cd8
// returns the selected fields (k1, k2, ..., k_n) of args as a tuple.
Packit bd1cd8
// For example,
Packit bd1cd8
//   SelectArgs<int, tuple<bool, char, double>, 2, 0>::Select(
Packit bd1cd8
//       ::testing::make_tuple(true, 'a', 2.5))
Packit bd1cd8
// returns tuple (2.5, true).
Packit bd1cd8
//
Packit bd1cd8
// The numbers in list k1, k2, ..., k_n must be >= 0, where n can be
Packit bd1cd8
// in the range [0, $n].  Duplicates are allowed and they don't have
Packit bd1cd8
// to be in an ascending or descending order.
Packit bd1cd8
Packit bd1cd8
template <typename Result, typename ArgumentTuple, $for i, [[int k$i]]>
Packit bd1cd8
class SelectArgs {
Packit bd1cd8
 public:
Packit bd1cd8
  typedef Result type($for i, [[GMOCK_FIELD_(ArgumentTuple, k$i)]]);
Packit bd1cd8
  typedef typename Function<type>::ArgumentTuple SelectedArgs;
Packit bd1cd8
  static SelectedArgs Select(const ArgumentTuple& args) {
Packit bd1cd8
    return SelectedArgs($for i, [[get<k$i>(args)]]);
Packit bd1cd8
  }
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
$for i [[
Packit bd1cd8
$range j 1..n
Packit bd1cd8
$range j1 1..i-1
Packit bd1cd8
template <typename Result, typename ArgumentTuple$for j1[[, int k$j1]]>
Packit bd1cd8
class SelectArgs
Packit bd1cd8
                 $for j, [[$if j <= i-1 [[k$j]] $else [[-1]]]]> {
Packit bd1cd8
 public:
Packit bd1cd8
  typedef Result type($for j1, [[GMOCK_FIELD_(ArgumentTuple, k$j1)]]);
Packit bd1cd8
  typedef typename Function<type>::ArgumentTuple SelectedArgs;
Packit bd1cd8
  static SelectedArgs Select(const ArgumentTuple& [[]]
Packit bd1cd8
$if i == 1 [[/* args */]] $else [[args]]) {
Packit bd1cd8
    return SelectedArgs($for j1, [[get<k$j1>(args)]]);
Packit bd1cd8
  }
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
]]
Packit bd1cd8
#undef GMOCK_FIELD_
Packit bd1cd8
Packit bd1cd8
$var ks = [[$for i, [[k$i]]]]
Packit bd1cd8
Packit bd1cd8
// Implements the WithArgs action.
Packit bd1cd8
template <typename InnerAction, $for i, [[int k$i = -1]]>
Packit bd1cd8
class WithArgsAction {
Packit bd1cd8
 public:
Packit bd1cd8
  explicit WithArgsAction(const InnerAction& action) : action_(action) {}
Packit bd1cd8
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  operator Action<F>() const { return MakeAction(new Impl<F>(action_)); }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  class Impl : public ActionInterface<F> {
Packit bd1cd8
   public:
Packit bd1cd8
    typedef typename Function<F>::Result Result;
Packit bd1cd8
    typedef typename Function<F>::ArgumentTuple ArgumentTuple;
Packit bd1cd8
Packit bd1cd8
    explicit Impl(const InnerAction& action) : action_(action) {}
Packit bd1cd8
Packit bd1cd8
    virtual Result Perform(const ArgumentTuple& args) {
Packit bd1cd8
      return action_.Perform(SelectArgs<Result, ArgumentTuple, $ks>::Select(args));
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
   private:
Packit bd1cd8
    typedef typename SelectArgs
Packit bd1cd8
        $ks>::type InnerFunctionType;
Packit bd1cd8
Packit bd1cd8
    Action<InnerFunctionType> action_;
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  const InnerAction action_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(WithArgsAction);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// A macro from the ACTION* family (defined later in this file)
Packit bd1cd8
// defines an action that can be used in a mock function.  Typically,
Packit bd1cd8
// these actions only care about a subset of the arguments of the mock
Packit bd1cd8
// function.  For example, if such an action only uses the second
Packit bd1cd8
// argument, it can be used in any mock function that takes >= 2
Packit bd1cd8
// arguments where the type of the second argument is compatible.
Packit bd1cd8
//
Packit bd1cd8
// Therefore, the action implementation must be prepared to take more
Packit bd1cd8
// arguments than it needs.  The ExcessiveArg type is used to
Packit bd1cd8
// represent those excessive arguments.  In order to keep the compiler
Packit bd1cd8
// error messages tractable, we define it in the testing namespace
Packit bd1cd8
// instead of testing::internal.  However, this is an INTERNAL TYPE
Packit bd1cd8
// and subject to change without notice, so a user MUST NOT USE THIS
Packit bd1cd8
// TYPE DIRECTLY.
Packit bd1cd8
struct ExcessiveArg {};
Packit bd1cd8
Packit bd1cd8
// A helper class needed for implementing the ACTION* macros.
Packit bd1cd8
template <typename Result, class Impl>
Packit bd1cd8
class ActionHelper {
Packit bd1cd8
 public:
Packit bd1cd8
$range i 0..n
Packit bd1cd8
$for i
Packit bd1cd8
Packit bd1cd8
[[
Packit bd1cd8
$var template = [[$if i==0 [[]] $else [[
Packit bd1cd8
$range j 0..i-1
Packit bd1cd8
  template <$for j, [[typename A$j]]>
Packit bd1cd8
]]]]
Packit bd1cd8
$range j 0..i-1
Packit bd1cd8
$var As = [[$for j, [[A$j]]]]
Packit bd1cd8
$var as = [[$for j, [[get<$j>(args)]]]]
Packit bd1cd8
$range k 1..n-i
Packit bd1cd8
$var eas = [[$for k, [[ExcessiveArg()]]]]
Packit bd1cd8
$var arg_list = [[$if (i==0) | (i==n) [[$as$eas]] $else [[$as, $eas]]]]
Packit bd1cd8
$template
Packit bd1cd8
  static Result Perform(Impl* impl, const ::testing::tuple<$As>& args) {
Packit bd1cd8
    return impl->template gmock_PerformImpl<$As>(args, $arg_list);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
]]
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
}  // namespace internal
Packit bd1cd8
Packit bd1cd8
// Various overloads for Invoke().
Packit bd1cd8
Packit bd1cd8
// WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes
Packit bd1cd8
// the selected arguments of the mock function to an_action and
Packit bd1cd8
// performs it.  It serves as an adaptor between actions with
Packit bd1cd8
// different argument lists.  C++ doesn't support default arguments for
Packit bd1cd8
// function templates, so we have to overload it.
Packit bd1cd8
Packit bd1cd8
$range i 1..n
Packit bd1cd8
$for i [[
Packit bd1cd8
$range j 1..i
Packit bd1cd8
template <$for j [[int k$j, ]]typename InnerAction>
Packit bd1cd8
inline internal::WithArgsAction<InnerAction$for j [[, k$j]]>
Packit bd1cd8
WithArgs(const InnerAction& action) {
Packit bd1cd8
  return internal::WithArgsAction<InnerAction$for j [[, k$j]]>(action);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
]]
Packit bd1cd8
// Creates an action that does actions a1, a2, ..., sequentially in
Packit bd1cd8
// each invocation.
Packit bd1cd8
$range i 2..n
Packit bd1cd8
$for i [[
Packit bd1cd8
$range j 2..i
Packit bd1cd8
$var types = [[$for j, [[typename Action$j]]]]
Packit bd1cd8
$var Aas = [[$for j [[, Action$j a$j]]]]
Packit bd1cd8
Packit bd1cd8
template <typename Action1, $types>
Packit bd1cd8
$range k 1..i-1
Packit bd1cd8
Packit bd1cd8
inline $for k [[internal::DoBothAction<Action$k, ]]Action$i$for k  [[>]]
Packit bd1cd8
Packit bd1cd8
DoAll(Action1 a1$Aas) {
Packit bd1cd8
$if i==2 [[
Packit bd1cd8
Packit bd1cd8
  return internal::DoBothAction<Action1, Action2>(a1, a2);
Packit bd1cd8
]] $else [[
Packit bd1cd8
$range j2 2..i
Packit bd1cd8
Packit bd1cd8
  return DoAll(a1, DoAll($for j2, [[a$j2]]));
Packit bd1cd8
]]
Packit bd1cd8
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
]]
Packit bd1cd8
Packit bd1cd8
}  // namespace testing
Packit bd1cd8
Packit bd1cd8
// The ACTION* family of macros can be used in a namespace scope to
Packit bd1cd8
// define custom actions easily.  The syntax:
Packit bd1cd8
//
Packit bd1cd8
//   ACTION(name) { statements; }
Packit bd1cd8
//
Packit bd1cd8
// will define an action with the given name that executes the
Packit bd1cd8
// statements.  The value returned by the statements will be used as
Packit bd1cd8
// the return value of the action.  Inside the statements, you can
Packit bd1cd8
// refer to the K-th (0-based) argument of the mock function by
Packit bd1cd8
// 'argK', and refer to its type by 'argK_type'.  For example:
Packit bd1cd8
//
Packit bd1cd8
//   ACTION(IncrementArg1) {
Packit bd1cd8
//     arg1_type temp = arg1;
Packit bd1cd8
//     return ++(*temp);
Packit bd1cd8
//   }
Packit bd1cd8
//
Packit bd1cd8
// allows you to write
Packit bd1cd8
//
Packit bd1cd8
//   ...WillOnce(IncrementArg1());
Packit bd1cd8
//
Packit bd1cd8
// You can also refer to the entire argument tuple and its type by
Packit bd1cd8
// 'args' and 'args_type', and refer to the mock function type and its
Packit bd1cd8
// return type by 'function_type' and 'return_type'.
Packit bd1cd8
//
Packit bd1cd8
// Note that you don't need to specify the types of the mock function
Packit bd1cd8
// arguments.  However rest assured that your code is still type-safe:
Packit bd1cd8
// you'll get a compiler error if *arg1 doesn't support the ++
Packit bd1cd8
// operator, or if the type of ++(*arg1) isn't compatible with the
Packit bd1cd8
// mock function's return type, for example.
Packit bd1cd8
//
Packit bd1cd8
// Sometimes you'll want to parameterize the action.   For that you can use
Packit bd1cd8
// another macro:
Packit bd1cd8
//
Packit bd1cd8
//   ACTION_P(name, param_name) { statements; }
Packit bd1cd8
//
Packit bd1cd8
// For example:
Packit bd1cd8
//
Packit bd1cd8
//   ACTION_P(Add, n) { return arg0 + n; }
Packit bd1cd8
//
Packit bd1cd8
// will allow you to write:
Packit bd1cd8
//
Packit bd1cd8
//   ...WillOnce(Add(5));
Packit bd1cd8
//
Packit bd1cd8
// Note that you don't need to provide the type of the parameter
Packit bd1cd8
// either.  If you need to reference the type of a parameter named
Packit bd1cd8
// 'foo', you can write 'foo_type'.  For example, in the body of
Packit bd1cd8
// ACTION_P(Add, n) above, you can write 'n_type' to refer to the type
Packit bd1cd8
// of 'n'.
Packit bd1cd8
//
Packit bd1cd8
// We also provide ACTION_P2, ACTION_P3, ..., up to ACTION_P$n to support
Packit bd1cd8
// multi-parameter actions.
Packit bd1cd8
//
Packit bd1cd8
// For the purpose of typing, you can view
Packit bd1cd8
//
Packit bd1cd8
//   ACTION_Pk(Foo, p1, ..., pk) { ... }
Packit bd1cd8
//
Packit bd1cd8
// as shorthand for
Packit bd1cd8
//
Packit bd1cd8
//   template <typename p1_type, ..., typename pk_type>
Packit bd1cd8
//   FooActionPk<p1_type, ..., pk_type> Foo(p1_type p1, ..., pk_type pk) { ... }
Packit bd1cd8
//
Packit bd1cd8
// In particular, you can provide the template type arguments
Packit bd1cd8
// explicitly when invoking Foo(), as in Foo<long, bool>(5, false);
Packit bd1cd8
// although usually you can rely on the compiler to infer the types
Packit bd1cd8
// for you automatically.  You can assign the result of expression
Packit bd1cd8
// Foo(p1, ..., pk) to a variable of type FooActionPk
Packit bd1cd8
// pk_type>.  This can be useful when composing actions.
Packit bd1cd8
//
Packit bd1cd8
// You can also overload actions with different numbers of parameters:
Packit bd1cd8
//
Packit bd1cd8
//   ACTION_P(Plus, a) { ... }
Packit bd1cd8
//   ACTION_P2(Plus, a, b) { ... }
Packit bd1cd8
//
Packit bd1cd8
// While it's tempting to always use the ACTION* macros when defining
Packit bd1cd8
// a new action, you should also consider implementing ActionInterface
Packit bd1cd8
// or using MakePolymorphicAction() instead, especially if you need to
Packit bd1cd8
// use the action a lot.  While these approaches require more work,
Packit bd1cd8
// they give you more control on the types of the mock function
Packit bd1cd8
// arguments and the action parameters, which in general leads to
Packit bd1cd8
// better compiler error messages that pay off in the long run.  They
Packit bd1cd8
// also allow overloading actions based on parameter types (as opposed
Packit bd1cd8
// to just based on the number of parameters).
Packit bd1cd8
//
Packit bd1cd8
// CAVEAT:
Packit bd1cd8
//
Packit bd1cd8
// ACTION*() can only be used in a namespace scope.  The reason is
Packit bd1cd8
// that C++ doesn't yet allow function-local types to be used to
Packit bd1cd8
// instantiate templates.  The up-coming C++0x standard will fix this.
Packit bd1cd8
// Once that's done, we'll consider supporting using ACTION*() inside
Packit bd1cd8
// a function.
Packit bd1cd8
//
Packit bd1cd8
// MORE INFORMATION:
Packit bd1cd8
//
Packit bd1cd8
// To learn more about using these macros, please search for 'ACTION'
Packit bd1cd8
// on http://code.google.com/p/googlemock/wiki/CookBook.
Packit bd1cd8
Packit bd1cd8
$range i 0..n
Packit bd1cd8
$range k 0..n-1
Packit bd1cd8
Packit bd1cd8
// An internal macro needed for implementing ACTION*().
Packit bd1cd8
#define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_\
Packit bd1cd8
    const args_type& args GTEST_ATTRIBUTE_UNUSED_
Packit bd1cd8
$for k [[, \
Packit bd1cd8
    arg$k[[]]_type arg$k GTEST_ATTRIBUTE_UNUSED_]]
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
// Sometimes you want to give an action explicit template parameters
Packit bd1cd8
// that cannot be inferred from its value parameters.  ACTION() and
Packit bd1cd8
// ACTION_P*() don't support that.  ACTION_TEMPLATE() remedies that
Packit bd1cd8
// and can be viewed as an extension to ACTION() and ACTION_P*().
Packit bd1cd8
//
Packit bd1cd8
// The syntax:
Packit bd1cd8
//
Packit bd1cd8
//   ACTION_TEMPLATE(ActionName,
Packit bd1cd8
//                   HAS_m_TEMPLATE_PARAMS(kind1, name1, ..., kind_m, name_m),
Packit bd1cd8
//                   AND_n_VALUE_PARAMS(p1, ..., p_n)) { statements; }
Packit bd1cd8
//
Packit bd1cd8
// defines an action template that takes m explicit template
Packit bd1cd8
// parameters and n value parameters.  name_i is the name of the i-th
Packit bd1cd8
// template parameter, and kind_i specifies whether it's a typename,
Packit bd1cd8
// an integral constant, or a template.  p_i is the name of the i-th
Packit bd1cd8
// value parameter.
Packit bd1cd8
//
Packit bd1cd8
// Example:
Packit bd1cd8
//
Packit bd1cd8
//   // DuplicateArg<k, T>(output) converts the k-th argument of the mock
Packit bd1cd8
//   // function to type T and copies it to *output.
Packit bd1cd8
//   ACTION_TEMPLATE(DuplicateArg,
Packit bd1cd8
//                   HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
Packit bd1cd8
//                   AND_1_VALUE_PARAMS(output)) {
Packit bd1cd8
//     *output = T(::testing::get<k>(args));
Packit bd1cd8
//   }
Packit bd1cd8
//   ...
Packit bd1cd8
//     int n;
Packit bd1cd8
//     EXPECT_CALL(mock, Foo(_, _))
Packit bd1cd8
//         .WillOnce(DuplicateArg<1, unsigned char>(&n);;
Packit bd1cd8
//
Packit bd1cd8
// To create an instance of an action template, write:
Packit bd1cd8
//
Packit bd1cd8
//   ActionName<t1, ..., t_m>(v1, ..., v_n)
Packit bd1cd8
//
Packit bd1cd8
// where the ts are the template arguments and the vs are the value
Packit bd1cd8
// arguments.  The value argument types are inferred by the compiler.
Packit bd1cd8
// If you want to explicitly specify the value argument types, you can
Packit bd1cd8
// provide additional template arguments:
Packit bd1cd8
//
Packit bd1cd8
//   ActionName<t1, ..., t_m, u1, ..., u_k>(v1, ..., v_n)
Packit bd1cd8
//
Packit bd1cd8
// where u_i is the desired type of v_i.
Packit bd1cd8
//
Packit bd1cd8
// ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded on the
Packit bd1cd8
// number of value parameters, but not on the number of template
Packit bd1cd8
// parameters.  Without the restriction, the meaning of the following
Packit bd1cd8
// is unclear:
Packit bd1cd8
//
Packit bd1cd8
//   OverloadedAction<int, bool>(x);
Packit bd1cd8
//
Packit bd1cd8
// Are we using a single-template-parameter action where 'bool' refers
Packit bd1cd8
// to the type of x, or are we using a two-template-parameter action
Packit bd1cd8
// where the compiler is asked to infer the type of x?
Packit bd1cd8
//
Packit bd1cd8
// Implementation notes:
Packit bd1cd8
//
Packit bd1cd8
// GMOCK_INTERNAL_*_HAS_m_TEMPLATE_PARAMS and
Packit bd1cd8
// GMOCK_INTERNAL_*_AND_n_VALUE_PARAMS are internal macros for
Packit bd1cd8
// implementing ACTION_TEMPLATE.  The main trick we use is to create
Packit bd1cd8
// new macro invocations when expanding a macro.  For example, we have
Packit bd1cd8
//
Packit bd1cd8
//   #define ACTION_TEMPLATE(name, template_params, value_params)
Packit bd1cd8
//       ... GMOCK_INTERNAL_DECL_##template_params ...
Packit bd1cd8
//
Packit bd1cd8
// which causes ACTION_TEMPLATE(..., HAS_1_TEMPLATE_PARAMS(typename, T), ...)
Packit bd1cd8
// to expand to
Packit bd1cd8
//
Packit bd1cd8
//       ... GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(typename, T) ...
Packit bd1cd8
//
Packit bd1cd8
// Since GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS is a macro, the
Packit bd1cd8
// preprocessor will continue to expand it to
Packit bd1cd8
//
Packit bd1cd8
//       ... typename T ...
Packit bd1cd8
//
Packit bd1cd8
// This technique conforms to the C++ standard and is portable.  It
Packit bd1cd8
// allows us to implement action templates using O(N) code, where N is
Packit bd1cd8
// the maximum number of template/value parameters supported.  Without
Packit bd1cd8
// using it, we'd have to devote O(N^2) amount of code to implement all
Packit bd1cd8
// combinations of m and n.
Packit bd1cd8
Packit bd1cd8
// Declares the template parameters.
Packit bd1cd8
Packit bd1cd8
$range j 1..n
Packit bd1cd8
$for j [[
Packit bd1cd8
$range m 0..j-1
Packit bd1cd8
#define GMOCK_INTERNAL_DECL_HAS_$j[[]]
Packit bd1cd8
_TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[kind$m name$m]]
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
]]
Packit bd1cd8
Packit bd1cd8
// Lists the template parameters.
Packit bd1cd8
Packit bd1cd8
$for j [[
Packit bd1cd8
$range m 0..j-1
Packit bd1cd8
#define GMOCK_INTERNAL_LIST_HAS_$j[[]]
Packit bd1cd8
_TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[name$m]]
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
]]
Packit bd1cd8
Packit bd1cd8
// Declares the types of value parameters.
Packit bd1cd8
Packit bd1cd8
$for i [[
Packit bd1cd8
$range j 0..i-1
Packit bd1cd8
#define GMOCK_INTERNAL_DECL_TYPE_AND_$i[[]]
Packit bd1cd8
_VALUE_PARAMS($for j, [[p$j]]) $for j [[, typename p$j##_type]]
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
]]
Packit bd1cd8
Packit bd1cd8
// Initializes the value parameters.
Packit bd1cd8
Packit bd1cd8
$for i [[
Packit bd1cd8
$range j 0..i-1
Packit bd1cd8
#define GMOCK_INTERNAL_INIT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])\
Packit bd1cd8
    ($for j, [[p$j##_type gmock_p$j]])$if i>0 [[ : ]]$for j, [[p$j(gmock_p$j)]]
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
]]
Packit bd1cd8
Packit bd1cd8
// Declares the fields for storing the value parameters.
Packit bd1cd8
Packit bd1cd8
$for i [[
Packit bd1cd8
$range j 0..i-1
Packit bd1cd8
#define GMOCK_INTERNAL_DEFN_AND_$i[[]]
Packit bd1cd8
_VALUE_PARAMS($for j, [[p$j]]) $for j [[p$j##_type p$j; ]]
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
]]
Packit bd1cd8
Packit bd1cd8
// Lists the value parameters.
Packit bd1cd8
Packit bd1cd8
$for i [[
Packit bd1cd8
$range j 0..i-1
Packit bd1cd8
#define GMOCK_INTERNAL_LIST_AND_$i[[]]
Packit bd1cd8
_VALUE_PARAMS($for j, [[p$j]]) $for j, [[p$j]]
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
]]
Packit bd1cd8
Packit bd1cd8
// Lists the value parameter types.
Packit bd1cd8
Packit bd1cd8
$for i [[
Packit bd1cd8
$range j 0..i-1
Packit bd1cd8
#define GMOCK_INTERNAL_LIST_TYPE_AND_$i[[]]
Packit bd1cd8
_VALUE_PARAMS($for j, [[p$j]]) $for j [[, p$j##_type]]
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
]]
Packit bd1cd8
Packit bd1cd8
// Declares the value parameters.
Packit bd1cd8
Packit bd1cd8
$for i [[
Packit bd1cd8
$range j 0..i-1
Packit bd1cd8
#define GMOCK_INTERNAL_DECL_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
Packit bd1cd8
$for j, [[p$j##_type p$j]]
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
]]
Packit bd1cd8
Packit bd1cd8
// The suffix of the class template implementing the action template.
Packit bd1cd8
$for i [[
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
$range j 0..i-1
Packit bd1cd8
#define GMOCK_INTERNAL_COUNT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]]
Packit bd1cd8
$if i==1 [[P]] $elif i>=2 [[P$i]]
Packit bd1cd8
]]
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
// The name of the class template implementing the action template.
Packit bd1cd8
#define GMOCK_ACTION_CLASS_(name, value_params)\
Packit bd1cd8
    GTEST_CONCAT_TOKEN_(name##Action, GMOCK_INTERNAL_COUNT_##value_params)
Packit bd1cd8
Packit bd1cd8
$range k 0..n-1
Packit bd1cd8
Packit bd1cd8
#define ACTION_TEMPLATE(name, template_params, value_params)\
Packit bd1cd8
  template 
Packit bd1cd8
            GMOCK_INTERNAL_DECL_TYPE_##value_params>\
Packit bd1cd8
  class GMOCK_ACTION_CLASS_(name, value_params) {\
Packit bd1cd8
   public:\
Packit bd1cd8
    explicit GMOCK_ACTION_CLASS_(name, value_params)\
Packit bd1cd8
        GMOCK_INTERNAL_INIT_##value_params {}\
Packit bd1cd8
    template <typename F>\
Packit bd1cd8
    class gmock_Impl : public ::testing::ActionInterface<F> {\
Packit bd1cd8
     public:\
Packit bd1cd8
      typedef F function_type;\
Packit bd1cd8
      typedef typename ::testing::internal::Function<F>::Result return_type;\
Packit bd1cd8
      typedef typename ::testing::internal::Function<F>::ArgumentTuple\
Packit bd1cd8
          args_type;\
Packit bd1cd8
      explicit gmock_Impl GMOCK_INTERNAL_INIT_##value_params {}\
Packit bd1cd8
      virtual return_type Perform(const args_type& args) {\
Packit bd1cd8
        return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
Packit bd1cd8
            Perform(this, args);\
Packit bd1cd8
      }\
Packit bd1cd8
      template <$for k, [[typename arg$k[[]]_type]]>\
Packit bd1cd8
      return_type gmock_PerformImpl(const args_type& args[[]]
Packit bd1cd8
$for k [[, arg$k[[]]_type arg$k]]) const;\
Packit bd1cd8
      GMOCK_INTERNAL_DEFN_##value_params\
Packit bd1cd8
     private:\
Packit bd1cd8
      GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
Packit bd1cd8
    };\
Packit bd1cd8
    template <typename F> operator ::testing::Action<F>() const {\
Packit bd1cd8
      return ::testing::Action<F>(\
Packit bd1cd8
          new gmock_Impl<F>(GMOCK_INTERNAL_LIST_##value_params));\
Packit bd1cd8
    }\
Packit bd1cd8
    GMOCK_INTERNAL_DEFN_##value_params\
Packit bd1cd8
   private:\
Packit bd1cd8
    GTEST_DISALLOW_ASSIGN_(GMOCK_ACTION_CLASS_(name, value_params));\
Packit bd1cd8
  };\
Packit bd1cd8
  template 
Packit bd1cd8
            GMOCK_INTERNAL_DECL_TYPE_##value_params>\
Packit bd1cd8
  inline GMOCK_ACTION_CLASS_(name, value_params)<\
Packit bd1cd8
      GMOCK_INTERNAL_LIST_##template_params\
Packit bd1cd8
      GMOCK_INTERNAL_LIST_TYPE_##value_params> name(\
Packit bd1cd8
          GMOCK_INTERNAL_DECL_##value_params) {\
Packit bd1cd8
    return GMOCK_ACTION_CLASS_(name, value_params)<\
Packit bd1cd8
        GMOCK_INTERNAL_LIST_##template_params\
Packit bd1cd8
        GMOCK_INTERNAL_LIST_TYPE_##value_params>(\
Packit bd1cd8
            GMOCK_INTERNAL_LIST_##value_params);\
Packit bd1cd8
  }\
Packit bd1cd8
  template 
Packit bd1cd8
            GMOCK_INTERNAL_DECL_TYPE_##value_params>\
Packit bd1cd8
  template <typename F>\
Packit bd1cd8
  template 
Packit bd1cd8
      typename arg3_type, typename arg4_type, typename arg5_type, \
Packit bd1cd8
      typename arg6_type, typename arg7_type, typename arg8_type, \
Packit bd1cd8
      typename arg9_type>\
Packit bd1cd8
  typename ::testing::internal::Function<F>::Result\
Packit bd1cd8
      GMOCK_ACTION_CLASS_(name, value_params)<\
Packit bd1cd8
          GMOCK_INTERNAL_LIST_##template_params\
Packit bd1cd8
          GMOCK_INTERNAL_LIST_TYPE_##value_params>::gmock_Impl<F>::\
Packit bd1cd8
              gmock_PerformImpl(\
Packit bd1cd8
          GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
Packit bd1cd8
Packit bd1cd8
$for i
Packit bd1cd8
Packit bd1cd8
[[
Packit bd1cd8
$var template = [[$if i==0 [[]] $else [[
Packit bd1cd8
$range j 0..i-1
Packit bd1cd8
Packit bd1cd8
  template <$for j, [[typename p$j##_type]]>\
Packit bd1cd8
]]]]
Packit bd1cd8
$var class_name = [[name##Action[[$if i==0 [[]] $elif i==1 [[P]]
Packit bd1cd8
                                                $else [[P$i]]]]]]
Packit bd1cd8
$range j 0..i-1
Packit bd1cd8
$var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
Packit bd1cd8
$var param_types_and_names = [[$for j, [[p$j##_type p$j]]]]
Packit bd1cd8
$var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]]
Packit bd1cd8
$var param_field_decls = [[$for j
Packit bd1cd8
[[
Packit bd1cd8
Packit bd1cd8
      p$j##_type p$j;\
Packit bd1cd8
]]]]
Packit bd1cd8
$var param_field_decls2 = [[$for j
Packit bd1cd8
[[
Packit bd1cd8
Packit bd1cd8
    p$j##_type p$j;\
Packit bd1cd8
]]]]
Packit bd1cd8
$var params = [[$for j, [[p$j]]]]
Packit bd1cd8
$var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]]
Packit bd1cd8
$var typename_arg_types = [[$for k, [[typename arg$k[[]]_type]]]]
Packit bd1cd8
$var arg_types_and_names = [[$for k, [[arg$k[[]]_type arg$k]]]]
Packit bd1cd8
$var macro_name = [[$if i==0 [[ACTION]] $elif i==1 [[ACTION_P]]
Packit bd1cd8
                                        $else [[ACTION_P$i]]]]
Packit bd1cd8
Packit bd1cd8
#define $macro_name(name$for j [[, p$j]])\$template
Packit bd1cd8
  class $class_name {\
Packit bd1cd8
   public:\
Packit bd1cd8
    [[$if i==1 [[explicit ]]]]$class_name($ctor_param_list)$inits {}\
Packit bd1cd8
    template <typename F>\
Packit bd1cd8
    class gmock_Impl : public ::testing::ActionInterface<F> {\
Packit bd1cd8
     public:\
Packit bd1cd8
      typedef F function_type;\
Packit bd1cd8
      typedef typename ::testing::internal::Function<F>::Result return_type;\
Packit bd1cd8
      typedef typename ::testing::internal::Function<F>::ArgumentTuple\
Packit bd1cd8
          args_type;\
Packit bd1cd8
      [[$if i==1 [[explicit ]]]]gmock_Impl($ctor_param_list)$inits {}\
Packit bd1cd8
      virtual return_type Perform(const args_type& args) {\
Packit bd1cd8
        return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\
Packit bd1cd8
            Perform(this, args);\
Packit bd1cd8
      }\
Packit bd1cd8
      template <$typename_arg_types>\
Packit bd1cd8
      return_type gmock_PerformImpl(const args_type& args, [[]]
Packit bd1cd8
$arg_types_and_names) const;\$param_field_decls
Packit bd1cd8
     private:\
Packit bd1cd8
      GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
Packit bd1cd8
    };\
Packit bd1cd8
    template <typename F> operator ::testing::Action<F>() const {\
Packit bd1cd8
      return ::testing::Action<F>(new gmock_Impl<F>($params));\
Packit bd1cd8
    }\$param_field_decls2
Packit bd1cd8
   private:\
Packit bd1cd8
    GTEST_DISALLOW_ASSIGN_($class_name);\
Packit bd1cd8
  };\$template
Packit bd1cd8
  inline $class_name$param_types name($param_types_and_names) {\
Packit bd1cd8
    return $class_name$param_types($params);\
Packit bd1cd8
  }\$template
Packit bd1cd8
  template <typename F>\
Packit bd1cd8
  template <$typename_arg_types>\
Packit bd1cd8
  typename ::testing::internal::Function<F>::Result\
Packit bd1cd8
      $class_name$param_types::gmock_Impl<F>::gmock_PerformImpl(\
Packit bd1cd8
          GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
Packit bd1cd8
]]
Packit bd1cd8
$$ }  // This meta comment fixes auto-indentation in Emacs.  It won't
Packit bd1cd8
$$    // show up in the generated code.
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
namespace testing {
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
// The ACTION*() macros trigger warning C4100 (unreferenced formal
Packit bd1cd8
// parameter) in MSVC with -W4.  Unfortunately they cannot be fixed in
Packit bd1cd8
// the macro definition, as the warnings are generated when the macro
Packit bd1cd8
// is expanded and macro expansion cannot contain #pragma.  Therefore
Packit bd1cd8
// we suppress them here.
Packit bd1cd8
#ifdef _MSC_VER
Packit bd1cd8
# pragma warning(push)
Packit bd1cd8
# pragma warning(disable:4100)
Packit bd1cd8
#endif
Packit bd1cd8
Packit bd1cd8
// Various overloads for InvokeArgument<N>().
Packit bd1cd8
//
Packit bd1cd8
// The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th
Packit bd1cd8
// (0-based) argument, which must be a k-ary callable, of the mock
Packit bd1cd8
// function, with arguments a1, a2, ..., a_k.
Packit bd1cd8
//
Packit bd1cd8
// Notes:
Packit bd1cd8
//
Packit bd1cd8
//   1. The arguments are passed by value by default.  If you need to
Packit bd1cd8
//   pass an argument by reference, wrap it inside ByRef().  For
Packit bd1cd8
//   example,
Packit bd1cd8
//
Packit bd1cd8
//     InvokeArgument<1>(5, string("Hello"), ByRef(foo))
Packit bd1cd8
//
Packit bd1cd8
//   passes 5 and string("Hello") by value, and passes foo by
Packit bd1cd8
//   reference.
Packit bd1cd8
//
Packit bd1cd8
//   2. If the callable takes an argument by reference but ByRef() is
Packit bd1cd8
//   not used, it will receive the reference to a copy of the value,
Packit bd1cd8
//   instead of the original value.  For example, when the 0-th
Packit bd1cd8
//   argument of the mock function takes a const string&, the action
Packit bd1cd8
//
Packit bd1cd8
//     InvokeArgument<0>(string("Hello"))
Packit bd1cd8
//
Packit bd1cd8
//   makes a copy of the temporary string("Hello") object and passes a
Packit bd1cd8
//   reference of the copy, instead of the original temporary object,
Packit bd1cd8
//   to the callable.  This makes it easy for a user to define an
Packit bd1cd8
//   InvokeArgument action from temporary values and have it performed
Packit bd1cd8
//   later.
Packit bd1cd8
Packit bd1cd8
namespace internal {
Packit bd1cd8
namespace invoke_argument {
Packit bd1cd8
Packit bd1cd8
// Appears in InvokeArgumentAdl's argument list to help avoid
Packit bd1cd8
// accidental calls to user functions of the same name.
Packit bd1cd8
struct AdlTag {};
Packit bd1cd8
Packit bd1cd8
// InvokeArgumentAdl - a helper for InvokeArgument.
Packit bd1cd8
// The basic overloads are provided here for generic functors.
Packit bd1cd8
// Overloads for other custom-callables are provided in the
Packit bd1cd8
// internal/custom/callback-actions.h header.
Packit bd1cd8
Packit bd1cd8
$range i 0..n
Packit bd1cd8
$for i
Packit bd1cd8
[[
Packit bd1cd8
$range j 1..i
Packit bd1cd8
Packit bd1cd8
template <typename R, typename F[[$for j [[, typename A$j]]]]>
Packit bd1cd8
R InvokeArgumentAdl(AdlTag, F f[[$for j [[, A$j a$j]]]]) {
Packit bd1cd8
  return f([[$for j, [[a$j]]]]);
Packit bd1cd8
}
Packit bd1cd8
]]
Packit bd1cd8
Packit bd1cd8
}  // namespace invoke_argument
Packit bd1cd8
}  // namespace internal
Packit bd1cd8
Packit bd1cd8
$range i 0..n
Packit bd1cd8
$for i [[
Packit bd1cd8
$range j 0..i-1
Packit bd1cd8
Packit bd1cd8
ACTION_TEMPLATE(InvokeArgument,
Packit bd1cd8
                HAS_1_TEMPLATE_PARAMS(int, k),
Packit bd1cd8
                AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])) {
Packit bd1cd8
  using internal::invoke_argument::InvokeArgumentAdl;
Packit bd1cd8
  return InvokeArgumentAdl<return_type>(
Packit bd1cd8
      internal::invoke_argument::AdlTag(),
Packit bd1cd8
      ::testing::get<k>(args)$for j [[, p$j]]);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
]]
Packit bd1cd8
Packit bd1cd8
// Various overloads for ReturnNew<T>().
Packit bd1cd8
//
Packit bd1cd8
// The ReturnNew<T>(a1, a2, ..., a_k) action returns a pointer to a new
Packit bd1cd8
// instance of type T, constructed on the heap with constructor arguments
Packit bd1cd8
// a1, a2, ..., and a_k. The caller assumes ownership of the returned value.
Packit bd1cd8
$range i 0..n
Packit bd1cd8
$for i [[
Packit bd1cd8
$range j 0..i-1
Packit bd1cd8
$var ps = [[$for j, [[p$j]]]]
Packit bd1cd8
Packit bd1cd8
ACTION_TEMPLATE(ReturnNew,
Packit bd1cd8
                HAS_1_TEMPLATE_PARAMS(typename, T),
Packit bd1cd8
                AND_$i[[]]_VALUE_PARAMS($ps)) {
Packit bd1cd8
  return new T($ps);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
]]
Packit bd1cd8
Packit bd1cd8
#ifdef _MSC_VER
Packit bd1cd8
# pragma warning(pop)
Packit bd1cd8
#endif
Packit bd1cd8
Packit bd1cd8
}  // namespace testing
Packit bd1cd8
Packit bd1cd8
// Include any custom callback actions added by the local installation.
Packit bd1cd8
// We must include this header at the end to make sure it can use the
Packit bd1cd8
// declarations from this file.
Packit bd1cd8
#include "gmock/internal/custom/gmock-generated-actions.h"
Packit bd1cd8
Packit bd1cd8
#endif  // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_