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

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 actions.
Packit bd1cd8
Packit bd1cd8
#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
Packit bd1cd8
#define GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
Packit bd1cd8
Packit bd1cd8
#ifndef _WIN32_WCE
Packit bd1cd8
# include <errno.h>
Packit bd1cd8
#endif
Packit bd1cd8
Packit bd1cd8
#include <algorithm>
Packit bd1cd8
#include <string>
Packit bd1cd8
Packit bd1cd8
#include "gmock/internal/gmock-internal-utils.h"
Packit bd1cd8
#include "gmock/internal/gmock-port.h"
Packit bd1cd8
Packit bd1cd8
#if GTEST_HAS_STD_TYPE_TRAITS_  // Defined by gtest-port.h via gmock-port.h.
Packit bd1cd8
#include <type_traits>
Packit bd1cd8
#endif
Packit bd1cd8
Packit bd1cd8
namespace testing {
Packit bd1cd8
Packit bd1cd8
// To implement an action Foo, define:
Packit bd1cd8
//   1. a class FooAction that implements the ActionInterface interface, and
Packit bd1cd8
//   2. a factory function that creates an Action object from a
Packit bd1cd8
//      const FooAction*.
Packit bd1cd8
//
Packit bd1cd8
// The two-level delegation design follows that of Matcher, providing
Packit bd1cd8
// consistency for extension developers.  It also eases ownership
Packit bd1cd8
// management as Action objects can now be copied like plain values.
Packit bd1cd8
Packit bd1cd8
namespace internal {
Packit bd1cd8
Packit bd1cd8
template <typename F1, typename F2>
Packit bd1cd8
class ActionAdaptor;
Packit bd1cd8
Packit bd1cd8
// BuiltInDefaultValueGetter<T, true>::Get() returns a
Packit bd1cd8
// default-constructed T value.  BuiltInDefaultValueGetter
Packit bd1cd8
// false>::Get() crashes with an error.
Packit bd1cd8
//
Packit bd1cd8
// This primary template is used when kDefaultConstructible is true.
Packit bd1cd8
template <typename T, bool kDefaultConstructible>
Packit bd1cd8
struct BuiltInDefaultValueGetter {
Packit bd1cd8
  static T Get() { return T(); }
Packit bd1cd8
};
Packit bd1cd8
template <typename T>
Packit bd1cd8
struct BuiltInDefaultValueGetter<T, false> {
Packit bd1cd8
  static T Get() {
Packit bd1cd8
    Assert(false, __FILE__, __LINE__,
Packit bd1cd8
           "Default action undefined for the function return type.");
Packit bd1cd8
    return internal::Invalid<T>();
Packit bd1cd8
    // The above statement will never be reached, but is required in
Packit bd1cd8
    // order for this function to compile.
Packit bd1cd8
  }
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// BuiltInDefaultValue<T>::Get() returns the "built-in" default value
Packit bd1cd8
// for type T, which is NULL when T is a raw pointer type, 0 when T is
Packit bd1cd8
// a numeric type, false when T is bool, or "" when T is string or
Packit bd1cd8
// std::string.  In addition, in C++11 and above, it turns a
Packit bd1cd8
// default-constructed T value if T is default constructible.  For any
Packit bd1cd8
// other type T, the built-in default T value is undefined, and the
Packit bd1cd8
// function will abort the process.
Packit bd1cd8
template <typename T>
Packit bd1cd8
class BuiltInDefaultValue {
Packit bd1cd8
 public:
Packit bd1cd8
#if GTEST_HAS_STD_TYPE_TRAITS_
Packit bd1cd8
  // This function returns true iff type T has a built-in default value.
Packit bd1cd8
  static bool Exists() {
Packit bd1cd8
    return ::std::is_default_constructible<T>::value;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  static T Get() {
Packit bd1cd8
    return BuiltInDefaultValueGetter<
Packit bd1cd8
        T, ::std::is_default_constructible<T>::value>::Get();
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
#else  // GTEST_HAS_STD_TYPE_TRAITS_
Packit bd1cd8
  // This function returns true iff type T has a built-in default value.
Packit bd1cd8
  static bool Exists() {
Packit bd1cd8
    return false;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  static T Get() {
Packit bd1cd8
    return BuiltInDefaultValueGetter<T, false>::Get();
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
#endif  // GTEST_HAS_STD_TYPE_TRAITS_
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// This partial specialization says that we use the same built-in
Packit bd1cd8
// default value for T and const T.
Packit bd1cd8
template <typename T>
Packit bd1cd8
class BuiltInDefaultValue<const T> {
Packit bd1cd8
 public:
Packit bd1cd8
  static bool Exists() { return BuiltInDefaultValue<T>::Exists(); }
Packit bd1cd8
  static T Get() { return BuiltInDefaultValue<T>::Get(); }
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// This partial specialization defines the default values for pointer
Packit bd1cd8
// types.
Packit bd1cd8
template <typename T>
Packit bd1cd8
class BuiltInDefaultValue<T*> {
Packit bd1cd8
 public:
Packit bd1cd8
  static bool Exists() { return true; }
Packit bd1cd8
  static T* Get() { return NULL; }
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// The following specializations define the default values for
Packit bd1cd8
// specific types we care about.
Packit bd1cd8
#define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(type, value) \
Packit bd1cd8
  template <> \
Packit bd1cd8
  class BuiltInDefaultValue<type> { \
Packit bd1cd8
   public: \
Packit bd1cd8
    static bool Exists() { return true; } \
Packit bd1cd8
    static type Get() { return value; } \
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(void, );  // NOLINT
Packit bd1cd8
#if GTEST_HAS_GLOBAL_STRING
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::string, "");
Packit bd1cd8
#endif  // GTEST_HAS_GLOBAL_STRING
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::std::string, "");
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(bool, false);
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned char, '\0');
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed char, '\0');
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(char, '\0');
Packit bd1cd8
Packit bd1cd8
// There's no need for a default action for signed wchar_t, as that
Packit bd1cd8
// type is the same as wchar_t for gcc, and invalid for MSVC.
Packit bd1cd8
//
Packit bd1cd8
// There's also no need for a default action for unsigned wchar_t, as
Packit bd1cd8
// that type is the same as unsigned int for gcc, and invalid for
Packit bd1cd8
// MSVC.
Packit bd1cd8
#if GMOCK_WCHAR_T_IS_NATIVE_
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U);  // NOLINT
Packit bd1cd8
#endif
Packit bd1cd8
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned short, 0U);  // NOLINT
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed short, 0);     // NOLINT
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned int, 0U);
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed int, 0);
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long, 0UL);  // NOLINT
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long, 0L);     // NOLINT
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(UInt64, 0);
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(Int64, 0);
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(float, 0);
Packit bd1cd8
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(double, 0);
Packit bd1cd8
Packit bd1cd8
#undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_
Packit bd1cd8
Packit bd1cd8
}  // namespace internal
Packit bd1cd8
Packit bd1cd8
// When an unexpected function call is encountered, Google Mock will
Packit bd1cd8
// let it return a default value if the user has specified one for its
Packit bd1cd8
// return type, or if the return type has a built-in default value;
Packit bd1cd8
// otherwise Google Mock won't know what value to return and will have
Packit bd1cd8
// to abort the process.
Packit bd1cd8
//
Packit bd1cd8
// The DefaultValue<T> class allows a user to specify the
Packit bd1cd8
// default value for a type T that is both copyable and publicly
Packit bd1cd8
// destructible (i.e. anything that can be used as a function return
Packit bd1cd8
// type).  The usage is:
Packit bd1cd8
//
Packit bd1cd8
//   // Sets the default value for type T to be foo.
Packit bd1cd8
//   DefaultValue<T>::Set(foo);
Packit bd1cd8
template <typename T>
Packit bd1cd8
class DefaultValue {
Packit bd1cd8
 public:
Packit bd1cd8
  // Sets the default value for type T; requires T to be
Packit bd1cd8
  // copy-constructable and have a public destructor.
Packit bd1cd8
  static void Set(T x) {
Packit bd1cd8
    delete producer_;
Packit bd1cd8
    producer_ = new FixedValueProducer(x);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Provides a factory function to be called to generate the default value.
Packit bd1cd8
  // This method can be used even if T is only move-constructible, but it is not
Packit bd1cd8
  // limited to that case.
Packit bd1cd8
  typedef T (*FactoryFunction)();
Packit bd1cd8
  static void SetFactory(FactoryFunction factory) {
Packit bd1cd8
    delete producer_;
Packit bd1cd8
    producer_ = new FactoryValueProducer(factory);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Unsets the default value for type T.
Packit bd1cd8
  static void Clear() {
Packit bd1cd8
    delete producer_;
Packit bd1cd8
    producer_ = NULL;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns true iff the user has set the default value for type T.
Packit bd1cd8
  static bool IsSet() { return producer_ != NULL; }
Packit bd1cd8
Packit bd1cd8
  // Returns true if T has a default return value set by the user or there
Packit bd1cd8
  // exists a built-in default value.
Packit bd1cd8
  static bool Exists() {
Packit bd1cd8
    return IsSet() || internal::BuiltInDefaultValue<T>::Exists();
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns the default value for type T if the user has set one;
Packit bd1cd8
  // otherwise returns the built-in default value. Requires that Exists()
Packit bd1cd8
  // is true, which ensures that the return value is well-defined.
Packit bd1cd8
  static T Get() {
Packit bd1cd8
    return producer_ == NULL ?
Packit bd1cd8
        internal::BuiltInDefaultValue<T>::Get() : producer_->Produce();
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  class ValueProducer {
Packit bd1cd8
   public:
Packit bd1cd8
    virtual ~ValueProducer() {}
Packit bd1cd8
    virtual T Produce() = 0;
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  class FixedValueProducer : public ValueProducer {
Packit bd1cd8
   public:
Packit bd1cd8
    explicit FixedValueProducer(T value) : value_(value) {}
Packit bd1cd8
    virtual T Produce() { return value_; }
Packit bd1cd8
Packit bd1cd8
   private:
Packit bd1cd8
    const T value_;
Packit bd1cd8
    GTEST_DISALLOW_COPY_AND_ASSIGN_(FixedValueProducer);
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  class FactoryValueProducer : public ValueProducer {
Packit bd1cd8
   public:
Packit bd1cd8
    explicit FactoryValueProducer(FactoryFunction factory)
Packit bd1cd8
        : factory_(factory) {}
Packit bd1cd8
    virtual T Produce() { return factory_(); }
Packit bd1cd8
Packit bd1cd8
   private:
Packit bd1cd8
    const FactoryFunction factory_;
Packit bd1cd8
    GTEST_DISALLOW_COPY_AND_ASSIGN_(FactoryValueProducer);
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  static ValueProducer* producer_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// This partial specialization allows a user to set default values for
Packit bd1cd8
// reference types.
Packit bd1cd8
template <typename T>
Packit bd1cd8
class DefaultValue<T&> {
Packit bd1cd8
 public:
Packit bd1cd8
  // Sets the default value for type T&.
Packit bd1cd8
  static void Set(T& x) {  // NOLINT
Packit bd1cd8
    address_ = &x;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Unsets the default value for type T&.
Packit bd1cd8
  static void Clear() {
Packit bd1cd8
    address_ = NULL;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns true iff the user has set the default value for type T&.
Packit bd1cd8
  static bool IsSet() { return address_ != NULL; }
Packit bd1cd8
Packit bd1cd8
  // Returns true if T has a default return value set by the user or there
Packit bd1cd8
  // exists a built-in default value.
Packit bd1cd8
  static bool Exists() {
Packit bd1cd8
    return IsSet() || internal::BuiltInDefaultValue<T&>::Exists();
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns the default value for type T& if the user has set one;
Packit bd1cd8
  // otherwise returns the built-in default value if there is one;
Packit bd1cd8
  // otherwise aborts the process.
Packit bd1cd8
  static T& Get() {
Packit bd1cd8
    return address_ == NULL ?
Packit bd1cd8
        internal::BuiltInDefaultValue<T&>::Get() : *address_;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  static T* address_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// This specialization allows DefaultValue<void>::Get() to
Packit bd1cd8
// compile.
Packit bd1cd8
template <>
Packit bd1cd8
class DefaultValue<void> {
Packit bd1cd8
 public:
Packit bd1cd8
  static bool Exists() { return true; }
Packit bd1cd8
  static void Get() {}
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Points to the user-set default value for type T.
Packit bd1cd8
template <typename T>
Packit bd1cd8
typename DefaultValue<T>::ValueProducer* DefaultValue<T>::producer_ = NULL;
Packit bd1cd8
Packit bd1cd8
// Points to the user-set default value for type T&.
Packit bd1cd8
template <typename T>
Packit bd1cd8
T* DefaultValue<T&>::address_ = NULL;
Packit bd1cd8
Packit bd1cd8
// Implement this interface to define an action for function type F.
Packit bd1cd8
template <typename F>
Packit bd1cd8
class ActionInterface {
Packit bd1cd8
 public:
Packit bd1cd8
  typedef typename internal::Function<F>::Result Result;
Packit bd1cd8
  typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
Packit bd1cd8
Packit bd1cd8
  ActionInterface() {}
Packit bd1cd8
  virtual ~ActionInterface() {}
Packit bd1cd8
Packit bd1cd8
  // Performs the action.  This method is not const, as in general an
Packit bd1cd8
  // action can have side effects and be stateful.  For example, a
Packit bd1cd8
  // get-the-next-element-from-the-collection action will need to
Packit bd1cd8
  // remember the current element.
Packit bd1cd8
  virtual Result Perform(const ArgumentTuple& args) = 0;
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionInterface);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// An Action<F> is a copyable and IMMUTABLE (except by assignment)
Packit bd1cd8
// object that represents an action to be taken when a mock function
Packit bd1cd8
// of type F is called.  The implementation of Action<T> is just a
Packit bd1cd8
// linked_ptr to const ActionInterface<T>, so copying is fairly cheap.
Packit bd1cd8
// Don't inherit from Action!
Packit bd1cd8
//
Packit bd1cd8
// You can view an object implementing ActionInterface<F> as a
Packit bd1cd8
// concrete action (including its current state), and an Action<F>
Packit bd1cd8
// object as a handle to it.
Packit bd1cd8
template <typename F>
Packit bd1cd8
class Action {
Packit bd1cd8
 public:
Packit bd1cd8
  typedef typename internal::Function<F>::Result Result;
Packit bd1cd8
  typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
Packit bd1cd8
Packit bd1cd8
  // Constructs a null Action.  Needed for storing Action objects in
Packit bd1cd8
  // STL containers.
Packit bd1cd8
  Action() : impl_(NULL) {}
Packit bd1cd8
Packit bd1cd8
  // Constructs an Action from its implementation.  A NULL impl is
Packit bd1cd8
  // used to represent the "do-default" action.
Packit bd1cd8
  explicit Action(ActionInterface<F>* impl) : impl_(impl) {}
Packit bd1cd8
Packit bd1cd8
  // Copy constructor.
Packit bd1cd8
  Action(const Action& action) : impl_(action.impl_) {}
Packit bd1cd8
Packit bd1cd8
  // This constructor allows us to turn an Action<Func> object into an
Packit bd1cd8
  // Action<F>, as long as F's arguments can be implicitly converted
Packit bd1cd8
  // to Func's and Func's return type can be implicitly converted to
Packit bd1cd8
  // F's.
Packit bd1cd8
  template <typename Func>
Packit bd1cd8
  explicit Action(const Action<Func>& action);
Packit bd1cd8
Packit bd1cd8
  // Returns true iff this is the DoDefault() action.
Packit bd1cd8
  bool IsDoDefault() const { return impl_.get() == NULL; }
Packit bd1cd8
Packit bd1cd8
  // Performs the action.  Note that this method is const even though
Packit bd1cd8
  // the corresponding method in ActionInterface is not.  The reason
Packit bd1cd8
  // is that a const Action<F> means that it cannot be re-bound to
Packit bd1cd8
  // another concrete action, not that the concrete action it binds to
Packit bd1cd8
  // cannot change state.  (Think of the difference between a const
Packit bd1cd8
  // pointer and a pointer to const.)
Packit bd1cd8
  Result Perform(const ArgumentTuple& args) const {
Packit bd1cd8
    internal::Assert(
Packit bd1cd8
        !IsDoDefault(), __FILE__, __LINE__,
Packit bd1cd8
        "You are using DoDefault() inside a composite action like "
Packit bd1cd8
        "DoAll() or WithArgs().  This is not supported for technical "
Packit bd1cd8
        "reasons.  Please instead spell out the default action, or "
Packit bd1cd8
        "assign the default action to an Action variable and use "
Packit bd1cd8
        "the variable in various places.");
Packit bd1cd8
    return impl_->Perform(args);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  template <typename F1, typename F2>
Packit bd1cd8
  friend class internal::ActionAdaptor;
Packit bd1cd8
Packit bd1cd8
  internal::linked_ptr<ActionInterface<F> > impl_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// The PolymorphicAction class template makes it easy to implement a
Packit bd1cd8
// polymorphic action (i.e. an action that can be used in mock
Packit bd1cd8
// functions of than one type, e.g. Return()).
Packit bd1cd8
//
Packit bd1cd8
// To define a polymorphic action, a user first provides a COPYABLE
Packit bd1cd8
// implementation class that has a Perform() method template:
Packit bd1cd8
//
Packit bd1cd8
//   class FooAction {
Packit bd1cd8
//    public:
Packit bd1cd8
//     template <typename Result, typename ArgumentTuple>
Packit bd1cd8
//     Result Perform(const ArgumentTuple& args) const {
Packit bd1cd8
//       // Processes the arguments and returns a result, using
Packit bd1cd8
//       // tr1::get<N>(args) to get the N-th (0-based) argument in the tuple.
Packit bd1cd8
//     }
Packit bd1cd8
//     ...
Packit bd1cd8
//   };
Packit bd1cd8
//
Packit bd1cd8
// Then the user creates the polymorphic action using
Packit bd1cd8
// MakePolymorphicAction(object) where object has type FooAction.  See
Packit bd1cd8
// the definition of Return(void) and SetArgumentPointee<N>(value) for
Packit bd1cd8
// complete examples.
Packit bd1cd8
template <typename Impl>
Packit bd1cd8
class PolymorphicAction {
Packit bd1cd8
 public:
Packit bd1cd8
  explicit PolymorphicAction(const Impl& impl) : impl_(impl) {}
Packit bd1cd8
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  operator Action<F>() const {
Packit bd1cd8
    return Action<F>(new MonomorphicImpl<F>(impl_));
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  class MonomorphicImpl : public ActionInterface<F> {
Packit bd1cd8
   public:
Packit bd1cd8
    typedef typename internal::Function<F>::Result Result;
Packit bd1cd8
    typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
Packit bd1cd8
Packit bd1cd8
    explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
Packit bd1cd8
Packit bd1cd8
    virtual Result Perform(const ArgumentTuple& args) {
Packit bd1cd8
      return impl_.template Perform<Result>(args);
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
   private:
Packit bd1cd8
    Impl impl_;
Packit bd1cd8
Packit bd1cd8
    GTEST_DISALLOW_ASSIGN_(MonomorphicImpl);
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  Impl impl_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(PolymorphicAction);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Creates an Action from its implementation and returns it.  The
Packit bd1cd8
// created Action object owns the implementation.
Packit bd1cd8
template <typename F>
Packit bd1cd8
Action<F> MakeAction(ActionInterface<F>* impl) {
Packit bd1cd8
  return Action<F>(impl);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Creates a polymorphic action from its implementation.  This is
Packit bd1cd8
// easier to use than the PolymorphicAction<Impl> constructor as it
Packit bd1cd8
// doesn't require you to explicitly write the template argument, e.g.
Packit bd1cd8
//
Packit bd1cd8
//   MakePolymorphicAction(foo);
Packit bd1cd8
// vs
Packit bd1cd8
//   PolymorphicAction<TypeOfFoo>(foo);
Packit bd1cd8
template <typename Impl>
Packit bd1cd8
inline PolymorphicAction<Impl> MakePolymorphicAction(const Impl& impl) {
Packit bd1cd8
  return PolymorphicAction<Impl>(impl);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
namespace internal {
Packit bd1cd8
Packit bd1cd8
// Allows an Action<F2> object to pose as an Action<F1>, as long as F2
Packit bd1cd8
// and F1 are compatible.
Packit bd1cd8
template <typename F1, typename F2>
Packit bd1cd8
class ActionAdaptor : public ActionInterface<F1> {
Packit bd1cd8
 public:
Packit bd1cd8
  typedef typename internal::Function<F1>::Result Result;
Packit bd1cd8
  typedef typename internal::Function<F1>::ArgumentTuple ArgumentTuple;
Packit bd1cd8
Packit bd1cd8
  explicit ActionAdaptor(const Action<F2>& from) : impl_(from.impl_) {}
Packit bd1cd8
Packit bd1cd8
  virtual Result Perform(const ArgumentTuple& args) {
Packit bd1cd8
    return impl_->Perform(args);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  const internal::linked_ptr<ActionInterface<F2> > impl_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(ActionAdaptor);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Helper struct to specialize ReturnAction to execute a move instead of a copy
Packit bd1cd8
// on return. Useful for move-only types, but could be used on any type.
Packit bd1cd8
template <typename T>
Packit bd1cd8
struct ByMoveWrapper {
Packit bd1cd8
  explicit ByMoveWrapper(T value) : payload(internal::move(value)) {}
Packit bd1cd8
  T payload;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Implements the polymorphic Return(x) action, which can be used in
Packit bd1cd8
// any function that returns the type of x, regardless of the argument
Packit bd1cd8
// types.
Packit bd1cd8
//
Packit bd1cd8
// Note: The value passed into Return must be converted into
Packit bd1cd8
// Function<F>::Result when this action is cast to Action<F> rather than
Packit bd1cd8
// when that action is performed. This is important in scenarios like
Packit bd1cd8
//
Packit bd1cd8
// MOCK_METHOD1(Method, T(U));
Packit bd1cd8
// ...
Packit bd1cd8
// {
Packit bd1cd8
//   Foo foo;
Packit bd1cd8
//   X x(&foo;;
Packit bd1cd8
//   EXPECT_CALL(mock, Method(_)).WillOnce(Return(x));
Packit bd1cd8
// }
Packit bd1cd8
//
Packit bd1cd8
// In the example above the variable x holds reference to foo which leaves
Packit bd1cd8
// scope and gets destroyed.  If copying X just copies a reference to foo,
Packit bd1cd8
// that copy will be left with a hanging reference.  If conversion to T
Packit bd1cd8
// makes a copy of foo, the above code is safe. To support that scenario, we
Packit bd1cd8
// need to make sure that the type conversion happens inside the EXPECT_CALL
Packit bd1cd8
// statement, and conversion of the result of Return to Action<T(U)> is a
Packit bd1cd8
// good place for that.
Packit bd1cd8
//
Packit bd1cd8
template <typename R>
Packit bd1cd8
class ReturnAction {
Packit bd1cd8
 public:
Packit bd1cd8
  // Constructs a ReturnAction object from the value to be returned.
Packit bd1cd8
  // 'value' is passed by value instead of by const reference in order
Packit bd1cd8
  // to allow Return("string literal") to compile.
Packit bd1cd8
  explicit ReturnAction(R value) : value_(new R(internal::move(value))) {}
Packit bd1cd8
Packit bd1cd8
  // This template type conversion operator allows Return(x) to be
Packit bd1cd8
  // used in ANY function that returns x's type.
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  operator Action<F>() const {
Packit bd1cd8
    // Assert statement belongs here because this is the best place to verify
Packit bd1cd8
    // conditions on F. It produces the clearest error messages
Packit bd1cd8
    // in most compilers.
Packit bd1cd8
    // Impl really belongs in this scope as a local class but can't
Packit bd1cd8
    // because MSVC produces duplicate symbols in different translation units
Packit bd1cd8
    // in this case. Until MS fixes that bug we put Impl into the class scope
Packit bd1cd8
    // and put the typedef both here (for use in assert statement) and
Packit bd1cd8
    // in the Impl class. But both definitions must be the same.
Packit bd1cd8
    typedef typename Function<F>::Result Result;
Packit bd1cd8
    GTEST_COMPILE_ASSERT_(
Packit bd1cd8
        !is_reference<Result>::value,
Packit bd1cd8
        use_ReturnRef_instead_of_Return_to_return_a_reference);
Packit bd1cd8
    return Action<F>(new Impl<R, F>(value_));
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  // Implements the Return(x) action for a particular function type F.
Packit bd1cd8
  template <typename R_, 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
    // The implicit cast is necessary when Result has more than one
Packit bd1cd8
    // single-argument constructor (e.g. Result is std::vector<int>) and R
Packit bd1cd8
    // has a type conversion operator template.  In that case, value_(value)
Packit bd1cd8
    // won't compile as the compiler doesn't known which constructor of
Packit bd1cd8
    // Result to call.  ImplicitCast_ forces the compiler to convert R to
Packit bd1cd8
    // Result without considering explicit constructors, thus resolving the
Packit bd1cd8
    // ambiguity. value_ is then initialized using its copy constructor.
Packit bd1cd8
    explicit Impl(const linked_ptr<R>& value)
Packit bd1cd8
        : value_before_cast_(*value),
Packit bd1cd8
          value_(ImplicitCast_<Result>(value_before_cast_)) {}
Packit bd1cd8
Packit bd1cd8
    virtual Result Perform(const ArgumentTuple&) { return value_; }
Packit bd1cd8
Packit bd1cd8
   private:
Packit bd1cd8
    GTEST_COMPILE_ASSERT_(!is_reference<Result>::value,
Packit bd1cd8
                          Result_cannot_be_a_reference_type);
Packit bd1cd8
    // We save the value before casting just in case it is being cast to a
Packit bd1cd8
    // wrapper type.
Packit bd1cd8
    R value_before_cast_;
Packit bd1cd8
    Result value_;
Packit bd1cd8
Packit bd1cd8
    GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl);
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  // Partially specialize for ByMoveWrapper. This version of ReturnAction will
Packit bd1cd8
  // move its contents instead.
Packit bd1cd8
  template <typename R_, typename F>
Packit bd1cd8
  class Impl<ByMoveWrapper<R_>, F> : 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 linked_ptr<R>& wrapper)
Packit bd1cd8
        : performed_(false), wrapper_(wrapper) {}
Packit bd1cd8
Packit bd1cd8
    virtual Result Perform(const ArgumentTuple&) {
Packit bd1cd8
      GTEST_CHECK_(!performed_)
Packit bd1cd8
          << "A ByMove() action should only be performed once.";
Packit bd1cd8
      performed_ = true;
Packit bd1cd8
      return internal::move(wrapper_->payload);
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
   private:
Packit bd1cd8
    bool performed_;
Packit bd1cd8
    const linked_ptr<R> wrapper_;
Packit bd1cd8
Packit bd1cd8
    GTEST_DISALLOW_ASSIGN_(Impl);
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  const linked_ptr<R> value_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(ReturnAction);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Implements the ReturnNull() action.
Packit bd1cd8
class ReturnNullAction {
Packit bd1cd8
 public:
Packit bd1cd8
  // Allows ReturnNull() to be used in any pointer-returning function. In C++11
Packit bd1cd8
  // this is enforced by returning nullptr, and in non-C++11 by asserting a
Packit bd1cd8
  // pointer type on compile time.
Packit bd1cd8
  template <typename Result, typename ArgumentTuple>
Packit bd1cd8
  static Result Perform(const ArgumentTuple&) {
Packit bd1cd8
#if GTEST_LANG_CXX11
Packit bd1cd8
    return nullptr;
Packit bd1cd8
#else
Packit bd1cd8
    GTEST_COMPILE_ASSERT_(internal::is_pointer<Result>::value,
Packit bd1cd8
                          ReturnNull_can_be_used_to_return_a_pointer_only);
Packit bd1cd8
    return NULL;
Packit bd1cd8
#endif  // GTEST_LANG_CXX11
Packit bd1cd8
  }
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Implements the Return() action.
Packit bd1cd8
class ReturnVoidAction {
Packit bd1cd8
 public:
Packit bd1cd8
  // Allows Return() to be used in any void-returning function.
Packit bd1cd8
  template <typename Result, typename ArgumentTuple>
Packit bd1cd8
  static void Perform(const ArgumentTuple&) {
Packit bd1cd8
    CompileAssertTypesEqual<void, Result>();
Packit bd1cd8
  }
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Implements the polymorphic ReturnRef(x) action, which can be used
Packit bd1cd8
// in any function that returns a reference to the type of x,
Packit bd1cd8
// regardless of the argument types.
Packit bd1cd8
template <typename T>
Packit bd1cd8
class ReturnRefAction {
Packit bd1cd8
 public:
Packit bd1cd8
  // Constructs a ReturnRefAction object from the reference to be returned.
Packit bd1cd8
  explicit ReturnRefAction(T& ref) : ref_(ref) {}  // NOLINT
Packit bd1cd8
Packit bd1cd8
  // This template type conversion operator allows ReturnRef(x) to be
Packit bd1cd8
  // used in ANY function that returns a reference to x's type.
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  operator Action<F>() const {
Packit bd1cd8
    typedef typename Function<F>::Result Result;
Packit bd1cd8
    // Asserts that the function return type is a reference.  This
Packit bd1cd8
    // catches the user error of using ReturnRef(x) when Return(x)
Packit bd1cd8
    // should be used, and generates some helpful error message.
Packit bd1cd8
    GTEST_COMPILE_ASSERT_(internal::is_reference<Result>::value,
Packit bd1cd8
                          use_Return_instead_of_ReturnRef_to_return_a_value);
Packit bd1cd8
    return Action<F>(new Impl<F>(ref_));
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  // Implements the ReturnRef(x) action for a particular function type F.
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(T& ref) : ref_(ref) {}  // NOLINT
Packit bd1cd8
Packit bd1cd8
    virtual Result Perform(const ArgumentTuple&) {
Packit bd1cd8
      return ref_;
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
   private:
Packit bd1cd8
    T& ref_;
Packit bd1cd8
Packit bd1cd8
    GTEST_DISALLOW_ASSIGN_(Impl);
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  T& ref_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(ReturnRefAction);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Implements the polymorphic ReturnRefOfCopy(x) action, which can be
Packit bd1cd8
// used in any function that returns a reference to the type of x,
Packit bd1cd8
// regardless of the argument types.
Packit bd1cd8
template <typename T>
Packit bd1cd8
class ReturnRefOfCopyAction {
Packit bd1cd8
 public:
Packit bd1cd8
  // Constructs a ReturnRefOfCopyAction object from the reference to
Packit bd1cd8
  // be returned.
Packit bd1cd8
  explicit ReturnRefOfCopyAction(const T& value) : value_(value) {}  // NOLINT
Packit bd1cd8
Packit bd1cd8
  // This template type conversion operator allows ReturnRefOfCopy(x) to be
Packit bd1cd8
  // used in ANY function that returns a reference to x's type.
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  operator Action<F>() const {
Packit bd1cd8
    typedef typename Function<F>::Result Result;
Packit bd1cd8
    // Asserts that the function return type is a reference.  This
Packit bd1cd8
    // catches the user error of using ReturnRefOfCopy(x) when Return(x)
Packit bd1cd8
    // should be used, and generates some helpful error message.
Packit bd1cd8
    GTEST_COMPILE_ASSERT_(
Packit bd1cd8
        internal::is_reference<Result>::value,
Packit bd1cd8
        use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
Packit bd1cd8
    return Action<F>(new Impl<F>(value_));
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  // Implements the ReturnRefOfCopy(x) action for a particular function type F.
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 T& value) : value_(value) {}  // NOLINT
Packit bd1cd8
Packit bd1cd8
    virtual Result Perform(const ArgumentTuple&) {
Packit bd1cd8
      return value_;
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
   private:
Packit bd1cd8
    T value_;
Packit bd1cd8
Packit bd1cd8
    GTEST_DISALLOW_ASSIGN_(Impl);
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  const T value_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(ReturnRefOfCopyAction);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Implements the polymorphic DoDefault() action.
Packit bd1cd8
class DoDefaultAction {
Packit bd1cd8
 public:
Packit bd1cd8
  // This template type conversion operator allows DoDefault() to be
Packit bd1cd8
  // used in any function.
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  operator Action<F>() const { return Action<F>(NULL); }
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Implements the Assign action to set a given pointer referent to a
Packit bd1cd8
// particular value.
Packit bd1cd8
template <typename T1, typename T2>
Packit bd1cd8
class AssignAction {
Packit bd1cd8
 public:
Packit bd1cd8
  AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {}
Packit bd1cd8
Packit bd1cd8
  template <typename Result, typename ArgumentTuple>
Packit bd1cd8
  void Perform(const ArgumentTuple& /* args */) const {
Packit bd1cd8
    *ptr_ = value_;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  T1* const ptr_;
Packit bd1cd8
  const T2 value_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(AssignAction);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
#if !GTEST_OS_WINDOWS_MOBILE
Packit bd1cd8
Packit bd1cd8
// Implements the SetErrnoAndReturn action to simulate return from
Packit bd1cd8
// various system calls and libc functions.
Packit bd1cd8
template <typename T>
Packit bd1cd8
class SetErrnoAndReturnAction {
Packit bd1cd8
 public:
Packit bd1cd8
  SetErrnoAndReturnAction(int errno_value, T result)
Packit bd1cd8
      : errno_(errno_value),
Packit bd1cd8
        result_(result) {}
Packit bd1cd8
  template <typename Result, typename ArgumentTuple>
Packit bd1cd8
  Result Perform(const ArgumentTuple& /* args */) const {
Packit bd1cd8
    errno = errno_;
Packit bd1cd8
    return result_;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  const int errno_;
Packit bd1cd8
  const T result_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(SetErrnoAndReturnAction);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
#endif  // !GTEST_OS_WINDOWS_MOBILE
Packit bd1cd8
Packit bd1cd8
// Implements the SetArgumentPointee<N>(x) action for any function
Packit bd1cd8
// whose N-th argument (0-based) is a pointer to x's type.  The
Packit bd1cd8
// template parameter kIsProto is true iff type A is ProtocolMessage,
Packit bd1cd8
// proto2::Message, or a sub-class of those.
Packit bd1cd8
template <size_t N, typename A, bool kIsProto>
Packit bd1cd8
class SetArgumentPointeeAction {
Packit bd1cd8
 public:
Packit bd1cd8
  // Constructs an action that sets the variable pointed to by the
Packit bd1cd8
  // N-th function argument to 'value'.
Packit bd1cd8
  explicit SetArgumentPointeeAction(const A& value) : value_(value) {}
Packit bd1cd8
Packit bd1cd8
  template <typename Result, typename ArgumentTuple>
Packit bd1cd8
  void Perform(const ArgumentTuple& args) const {
Packit bd1cd8
    CompileAssertTypesEqual<void, Result>();
Packit bd1cd8
    *::testing::get<N>(args) = value_;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  const A value_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
template <size_t N, typename Proto>
Packit bd1cd8
class SetArgumentPointeeAction<N, Proto, true> {
Packit bd1cd8
 public:
Packit bd1cd8
  // Constructs an action that sets the variable pointed to by the
Packit bd1cd8
  // N-th function argument to 'proto'.  Both ProtocolMessage and
Packit bd1cd8
  // proto2::Message have the CopyFrom() method, so the same
Packit bd1cd8
  // implementation works for both.
Packit bd1cd8
  explicit SetArgumentPointeeAction(const Proto& proto) : proto_(new Proto) {
Packit bd1cd8
    proto_->CopyFrom(proto);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  template <typename Result, typename ArgumentTuple>
Packit bd1cd8
  void Perform(const ArgumentTuple& args) const {
Packit bd1cd8
    CompileAssertTypesEqual<void, Result>();
Packit bd1cd8
    ::testing::get<N>(args)->CopyFrom(*proto_);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  const internal::linked_ptr<Proto> proto_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Implements the InvokeWithoutArgs(f) action.  The template argument
Packit bd1cd8
// FunctionImpl is the implementation type of f, which can be either a
Packit bd1cd8
// function pointer or a functor.  InvokeWithoutArgs(f) can be used as an
Packit bd1cd8
// Action<F> as long as f's type is compatible with F (i.e. f can be
Packit bd1cd8
// assigned to a tr1::function<F>).
Packit bd1cd8
template <typename FunctionImpl>
Packit bd1cd8
class InvokeWithoutArgsAction {
Packit bd1cd8
 public:
Packit bd1cd8
  // The c'tor makes a copy of function_impl (either a function
Packit bd1cd8
  // pointer or a functor).
Packit bd1cd8
  explicit InvokeWithoutArgsAction(FunctionImpl function_impl)
Packit bd1cd8
      : function_impl_(function_impl) {}
Packit bd1cd8
Packit bd1cd8
  // Allows InvokeWithoutArgs(f) to be used as any action whose type is
Packit bd1cd8
  // compatible with f.
Packit bd1cd8
  template <typename Result, typename ArgumentTuple>
Packit bd1cd8
  Result Perform(const ArgumentTuple&) { return function_impl_(); }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  FunctionImpl function_impl_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(InvokeWithoutArgsAction);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action.
Packit bd1cd8
template <class Class, typename MethodPtr>
Packit bd1cd8
class InvokeMethodWithoutArgsAction {
Packit bd1cd8
 public:
Packit bd1cd8
  InvokeMethodWithoutArgsAction(Class* obj_ptr, MethodPtr method_ptr)
Packit bd1cd8
      : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
Packit bd1cd8
Packit bd1cd8
  template <typename Result, typename ArgumentTuple>
Packit bd1cd8
  Result Perform(const ArgumentTuple&) const {
Packit bd1cd8
    return (obj_ptr_->*method_ptr_)();
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  Class* const obj_ptr_;
Packit bd1cd8
  const MethodPtr method_ptr_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(InvokeMethodWithoutArgsAction);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Implements the IgnoreResult(action) action.
Packit bd1cd8
template <typename A>
Packit bd1cd8
class IgnoreResultAction {
Packit bd1cd8
 public:
Packit bd1cd8
  explicit IgnoreResultAction(const A& action) : action_(action) {}
Packit bd1cd8
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  operator Action<F>() const {
Packit bd1cd8
    // Assert statement belongs here because this is the best place to verify
Packit bd1cd8
    // conditions on F. It produces the clearest error messages
Packit bd1cd8
    // in most compilers.
Packit bd1cd8
    // Impl really belongs in this scope as a local class but can't
Packit bd1cd8
    // because MSVC produces duplicate symbols in different translation units
Packit bd1cd8
    // in this case. Until MS fixes that bug we put Impl into the class scope
Packit bd1cd8
    // and put the typedef both here (for use in assert statement) and
Packit bd1cd8
    // in the Impl class. But both definitions must be the same.
Packit bd1cd8
    typedef typename internal::Function<F>::Result Result;
Packit bd1cd8
Packit bd1cd8
    // Asserts at compile time that F returns void.
Packit bd1cd8
    CompileAssertTypesEqual<void, Result>();
Packit bd1cd8
Packit bd1cd8
    return Action<F>(new Impl<F>(action_));
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  class Impl : public ActionInterface<F> {
Packit bd1cd8
   public:
Packit bd1cd8
    typedef typename internal::Function<F>::Result Result;
Packit bd1cd8
    typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
Packit bd1cd8
Packit bd1cd8
    explicit Impl(const A& action) : action_(action) {}
Packit bd1cd8
Packit bd1cd8
    virtual void Perform(const ArgumentTuple& args) {
Packit bd1cd8
      // Performs the action and ignores its result.
Packit bd1cd8
      action_.Perform(args);
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
   private:
Packit bd1cd8
    // Type OriginalFunction is the same as F except that its return
Packit bd1cd8
    // type is IgnoredValue.
Packit bd1cd8
    typedef typename internal::Function<F>::MakeResultIgnoredValue
Packit bd1cd8
        OriginalFunction;
Packit bd1cd8
Packit bd1cd8
    const Action<OriginalFunction> action_;
Packit bd1cd8
Packit bd1cd8
    GTEST_DISALLOW_ASSIGN_(Impl);
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  const A action_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(IgnoreResultAction);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// A ReferenceWrapper<T> object represents a reference to type T,
Packit bd1cd8
// which can be either const or not.  It can be explicitly converted
Packit bd1cd8
// from, and implicitly converted to, a T&.  Unlike a reference,
Packit bd1cd8
// ReferenceWrapper<T> can be copied and can survive template type
Packit bd1cd8
// inference.  This is used to support by-reference arguments in the
Packit bd1cd8
// InvokeArgument<N>(...) action.  The idea was from "reference
Packit bd1cd8
// wrappers" in tr1, which we don't have in our source tree yet.
Packit bd1cd8
template <typename T>
Packit bd1cd8
class ReferenceWrapper {
Packit bd1cd8
 public:
Packit bd1cd8
  // Constructs a ReferenceWrapper<T> object from a T&.
Packit bd1cd8
  explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {}  // NOLINT
Packit bd1cd8
Packit bd1cd8
  // Allows a ReferenceWrapper<T> object to be implicitly converted to
Packit bd1cd8
  // a T&.
Packit bd1cd8
  operator T&() const { return *pointer_; }
Packit bd1cd8
 private:
Packit bd1cd8
  T* pointer_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Allows the expression ByRef(x) to be printed as a reference to x.
Packit bd1cd8
template <typename T>
Packit bd1cd8
void PrintTo(const ReferenceWrapper<T>& ref, ::std::ostream* os) {
Packit bd1cd8
  T& value = ref;
Packit bd1cd8
  UniversalPrinter<T&>::Print(value, os);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Does two actions sequentially.  Used for implementing the DoAll(a1,
Packit bd1cd8
// a2, ...) action.
Packit bd1cd8
template <typename Action1, typename Action2>
Packit bd1cd8
class DoBothAction {
Packit bd1cd8
 public:
Packit bd1cd8
  DoBothAction(Action1 action1, Action2 action2)
Packit bd1cd8
      : action1_(action1), action2_(action2) {}
Packit bd1cd8
Packit bd1cd8
  // This template type conversion operator allows DoAll(a1, ..., a_n)
Packit bd1cd8
  // to be used in ANY function of compatible type.
Packit bd1cd8
  template <typename F>
Packit bd1cd8
  operator Action<F>() const {
Packit bd1cd8
    return Action<F>(new Impl<F>(action1_, action2_));
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  // Implements the DoAll(...) action for a particular function type F.
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
    typedef typename Function<F>::MakeResultVoid VoidResult;
Packit bd1cd8
Packit bd1cd8
    Impl(const Action<VoidResult>& action1, const Action<F>& action2)
Packit bd1cd8
        : action1_(action1), action2_(action2) {}
Packit bd1cd8
Packit bd1cd8
    virtual Result Perform(const ArgumentTuple& args) {
Packit bd1cd8
      action1_.Perform(args);
Packit bd1cd8
      return action2_.Perform(args);
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
   private:
Packit bd1cd8
    const Action<VoidResult> action1_;
Packit bd1cd8
    const Action<F> action2_;
Packit bd1cd8
Packit bd1cd8
    GTEST_DISALLOW_ASSIGN_(Impl);
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  Action1 action1_;
Packit bd1cd8
  Action2 action2_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(DoBothAction);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
}  // namespace internal
Packit bd1cd8
Packit bd1cd8
// An Unused object can be implicitly constructed from ANY value.
Packit bd1cd8
// This is handy when defining actions that ignore some or all of the
Packit bd1cd8
// mock function arguments.  For example, given
Packit bd1cd8
//
Packit bd1cd8
//   MOCK_METHOD3(Foo, double(const string& label, double x, double y));
Packit bd1cd8
//   MOCK_METHOD3(Bar, double(int index, double x, double y));
Packit bd1cd8
//
Packit bd1cd8
// instead of
Packit bd1cd8
//
Packit bd1cd8
//   double DistanceToOriginWithLabel(const string& label, double x, double y) {
Packit bd1cd8
//     return sqrt(x*x + y*y);
Packit bd1cd8
//   }
Packit bd1cd8
//   double DistanceToOriginWithIndex(int index, double x, double y) {
Packit bd1cd8
//     return sqrt(x*x + y*y);
Packit bd1cd8
//   }
Packit bd1cd8
//   ...
Packit bd1cd8
//   EXEPCT_CALL(mock, Foo("abc", _, _))
Packit bd1cd8
//       .WillOnce(Invoke(DistanceToOriginWithLabel));
Packit bd1cd8
//   EXEPCT_CALL(mock, Bar(5, _, _))
Packit bd1cd8
//       .WillOnce(Invoke(DistanceToOriginWithIndex));
Packit bd1cd8
//
Packit bd1cd8
// you could write
Packit bd1cd8
//
Packit bd1cd8
//   // We can declare any uninteresting argument as Unused.
Packit bd1cd8
//   double DistanceToOrigin(Unused, double x, double y) {
Packit bd1cd8
//     return sqrt(x*x + y*y);
Packit bd1cd8
//   }
Packit bd1cd8
//   ...
Packit bd1cd8
//   EXEPCT_CALL(mock, Foo("abc", _, _)).WillOnce(Invoke(DistanceToOrigin));
Packit bd1cd8
//   EXEPCT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin));
Packit bd1cd8
typedef internal::IgnoredValue Unused;
Packit bd1cd8
Packit bd1cd8
// This constructor allows us to turn an Action<From> object into an
Packit bd1cd8
// Action<To>, as long as To's arguments can be implicitly converted
Packit bd1cd8
// to From's and From's return type cann be implicitly converted to
Packit bd1cd8
// To's.
Packit bd1cd8
template <typename To>
Packit bd1cd8
template <typename From>
Packit bd1cd8
Action<To>::Action(const Action<From>& from)
Packit bd1cd8
    : impl_(new internal::ActionAdaptor<To, From>(from)) {}
Packit bd1cd8
Packit bd1cd8
// Creates an action that returns 'value'.  'value' is passed by value
Packit bd1cd8
// instead of const reference - otherwise Return("string literal")
Packit bd1cd8
// will trigger a compiler error about using array as initializer.
Packit bd1cd8
template <typename R>
Packit bd1cd8
internal::ReturnAction<R> Return(R value) {
Packit bd1cd8
  return internal::ReturnAction<R>(internal::move(value));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Creates an action that returns NULL.
Packit bd1cd8
inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() {
Packit bd1cd8
  return MakePolymorphicAction(internal::ReturnNullAction());
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Creates an action that returns from a void function.
Packit bd1cd8
inline PolymorphicAction<internal::ReturnVoidAction> Return() {
Packit bd1cd8
  return MakePolymorphicAction(internal::ReturnVoidAction());
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Creates an action that returns the reference to a variable.
Packit bd1cd8
template <typename R>
Packit bd1cd8
inline internal::ReturnRefAction<R> ReturnRef(R& x) {  // NOLINT
Packit bd1cd8
  return internal::ReturnRefAction<R>(x);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Creates an action that returns the reference to a copy of the
Packit bd1cd8
// argument.  The copy is created when the action is constructed and
Packit bd1cd8
// lives as long as the action.
Packit bd1cd8
template <typename R>
Packit bd1cd8
inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) {
Packit bd1cd8
  return internal::ReturnRefOfCopyAction<R>(x);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Modifies the parent action (a Return() action) to perform a move of the
Packit bd1cd8
// argument instead of a copy.
Packit bd1cd8
// Return(ByMove()) actions can only be executed once and will assert this
Packit bd1cd8
// invariant.
Packit bd1cd8
template <typename R>
Packit bd1cd8
internal::ByMoveWrapper<R> ByMove(R x) {
Packit bd1cd8
  return internal::ByMoveWrapper<R>(internal::move(x));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Creates an action that does the default action for the give mock function.
Packit bd1cd8
inline internal::DoDefaultAction DoDefault() {
Packit bd1cd8
  return internal::DoDefaultAction();
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Creates an action that sets the variable pointed by the N-th
Packit bd1cd8
// (0-based) function argument to 'value'.
Packit bd1cd8
template <size_t N, typename T>
Packit bd1cd8
PolymorphicAction<
Packit bd1cd8
  internal::SetArgumentPointeeAction<
Packit bd1cd8
    N, T, internal::IsAProtocolMessage<T>::value> >
Packit bd1cd8
SetArgPointee(const T& x) {
Packit bd1cd8
  return MakePolymorphicAction(internal::SetArgumentPointeeAction<
Packit bd1cd8
      N, T, internal::IsAProtocolMessage<T>::value>(x));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
#if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
Packit bd1cd8
// This overload allows SetArgPointee() to accept a string literal.
Packit bd1cd8
// GCC prior to the version 4.0 and Symbian C++ compiler cannot distinguish
Packit bd1cd8
// this overload from the templated version and emit a compile error.
Packit bd1cd8
template <size_t N>
Packit bd1cd8
PolymorphicAction<
Packit bd1cd8
  internal::SetArgumentPointeeAction<N, const char*, false> >
Packit bd1cd8
SetArgPointee(const char* p) {
Packit bd1cd8
  return MakePolymorphicAction(internal::SetArgumentPointeeAction<
Packit bd1cd8
      N, const char*, false>(p));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
template <size_t N>
Packit bd1cd8
PolymorphicAction<
Packit bd1cd8
  internal::SetArgumentPointeeAction<N, const wchar_t*, false> >
Packit bd1cd8
SetArgPointee(const wchar_t* p) {
Packit bd1cd8
  return MakePolymorphicAction(internal::SetArgumentPointeeAction<
Packit bd1cd8
      N, const wchar_t*, false>(p));
Packit bd1cd8
}
Packit bd1cd8
#endif
Packit bd1cd8
Packit bd1cd8
// The following version is DEPRECATED.
Packit bd1cd8
template <size_t N, typename T>
Packit bd1cd8
PolymorphicAction<
Packit bd1cd8
  internal::SetArgumentPointeeAction<
Packit bd1cd8
    N, T, internal::IsAProtocolMessage<T>::value> >
Packit bd1cd8
SetArgumentPointee(const T& x) {
Packit bd1cd8
  return MakePolymorphicAction(internal::SetArgumentPointeeAction<
Packit bd1cd8
      N, T, internal::IsAProtocolMessage<T>::value>(x));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Creates an action that sets a pointer referent to a given value.
Packit bd1cd8
template <typename T1, typename T2>
Packit bd1cd8
PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) {
Packit bd1cd8
  return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
#if !GTEST_OS_WINDOWS_MOBILE
Packit bd1cd8
Packit bd1cd8
// Creates an action that sets errno and returns the appropriate error.
Packit bd1cd8
template <typename T>
Packit bd1cd8
PolymorphicAction<internal::SetErrnoAndReturnAction<T> >
Packit bd1cd8
SetErrnoAndReturn(int errval, T result) {
Packit bd1cd8
  return MakePolymorphicAction(
Packit bd1cd8
      internal::SetErrnoAndReturnAction<T>(errval, result));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
#endif  // !GTEST_OS_WINDOWS_MOBILE
Packit bd1cd8
Packit bd1cd8
// Various overloads for InvokeWithoutArgs().
Packit bd1cd8
Packit bd1cd8
// Creates an action that invokes 'function_impl' with no argument.
Packit bd1cd8
template <typename FunctionImpl>
Packit bd1cd8
PolymorphicAction<internal::InvokeWithoutArgsAction<FunctionImpl> >
Packit bd1cd8
InvokeWithoutArgs(FunctionImpl function_impl) {
Packit bd1cd8
  return MakePolymorphicAction(
Packit bd1cd8
      internal::InvokeWithoutArgsAction<FunctionImpl>(function_impl));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Creates an action that invokes the given method on the given object
Packit bd1cd8
// with no argument.
Packit bd1cd8
template <class Class, typename MethodPtr>
Packit bd1cd8
PolymorphicAction<internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> >
Packit bd1cd8
InvokeWithoutArgs(Class* obj_ptr, MethodPtr method_ptr) {
Packit bd1cd8
  return MakePolymorphicAction(
Packit bd1cd8
      internal::InvokeMethodWithoutArgsAction<Class, MethodPtr>(
Packit bd1cd8
          obj_ptr, method_ptr));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Creates an action that performs an_action and throws away its
Packit bd1cd8
// result.  In other words, it changes the return type of an_action to
Packit bd1cd8
// void.  an_action MUST NOT return void, or the code won't compile.
Packit bd1cd8
template <typename A>
Packit bd1cd8
inline internal::IgnoreResultAction IgnoreResult(const A& an_action) {
Packit bd1cd8
  return internal::IgnoreResultAction(an_action);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Creates a reference wrapper for the given L-value.  If necessary,
Packit bd1cd8
// you can explicitly specify the type of the reference.  For example,
Packit bd1cd8
// suppose 'derived' is an object of type Derived, ByRef(derived)
Packit bd1cd8
// would wrap a Derived&.  If you want to wrap a const Base& instead,
Packit bd1cd8
// where Base is a base class of Derived, just write:
Packit bd1cd8
//
Packit bd1cd8
//   ByRef<const Base>(derived)
Packit bd1cd8
template <typename T>
Packit bd1cd8
inline internal::ReferenceWrapper<T> ByRef(T& l_value) {  // NOLINT
Packit bd1cd8
  return internal::ReferenceWrapper<T>(l_value);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
}  // namespace testing
Packit bd1cd8
Packit bd1cd8
#endif  // GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_