gmock-actions.h
Go to the documentation of this file.
00001 // Copyright 2007, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 // Google Mock - a framework for writing C++ mock classes.
00033 //
00034 // This file implements some commonly used actions.
00035 
00036 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
00037 #define GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
00038 
00039 #ifndef _WIN32_WCE
00040 # include <errno.h>
00041 #endif
00042 
00043 #include <algorithm>
00044 #include <string>
00045 
00046 #include "gmock/internal/gmock-internal-utils.h"
00047 #include "gmock/internal/gmock-port.h"
00048 
00049 namespace testing {
00050 
00051 // To implement an action Foo, define:
00052 //   1. a class FooAction that implements the ActionInterface interface, and
00053 //   2. a factory function that creates an Action object from a
00054 //      const FooAction*.
00055 //
00056 // The two-level delegation design follows that of Matcher, providing
00057 // consistency for extension developers.  It also eases ownership
00058 // management as Action objects can now be copied like plain values.
00059 
00060 namespace internal {
00061 
00062 template <typename F1, typename F2>
00063 class ActionAdaptor;
00064 
00065 // BuiltInDefaultValue<T>::Get() returns the "built-in" default
00066 // value for type T, which is NULL when T is a pointer type, 0 when T
00067 // is a numeric type, false when T is bool, or "" when T is string or
00068 // std::string.  For any other type T, this value is undefined and the
00069 // function will abort the process.
00070 template <typename T>
00071 class BuiltInDefaultValue {
00072  public:
00073   // This function returns true iff type T has a built-in default value.
00074   static bool Exists() { return false; }
00075   static T Get() {
00076     Assert(false, __FILE__, __LINE__,
00077            "Default action undefined for the function return type.");
00078     return internal::Invalid<T>();
00079     // The above statement will never be reached, but is required in
00080     // order for this function to compile.
00081   }
00082 };
00083 
00084 // This partial specialization says that we use the same built-in
00085 // default value for T and const T.
00086 template <typename T>
00087 class BuiltInDefaultValue<const T> {
00088  public:
00089   static bool Exists() { return BuiltInDefaultValue<T>::Exists(); }
00090   static T Get() { return BuiltInDefaultValue<T>::Get(); }
00091 };
00092 
00093 // This partial specialization defines the default values for pointer
00094 // types.
00095 template <typename T>
00096 class BuiltInDefaultValue<T*> {
00097  public:
00098   static bool Exists() { return true; }
00099   static T* Get() { return NULL; }
00100 };
00101 
00102 // The following specializations define the default values for
00103 // specific types we care about.
00104 #define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(type, value) \
00105   template <> \
00106   class BuiltInDefaultValue<type> { \
00107    public: \
00108     static bool Exists() { return true; } \
00109     static type Get() { return value; } \
00110   }
00111 
00112 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(void, );  // NOLINT
00113 #if GTEST_HAS_GLOBAL_STRING
00114 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::string, "");
00115 #endif  // GTEST_HAS_GLOBAL_STRING
00116 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::std::string, "");
00117 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(bool, false);
00118 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned char, '\0');
00119 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed char, '\0');
00120 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(char, '\0');
00121 
00122 // There's no need for a default action for signed wchar_t, as that
00123 // type is the same as wchar_t for gcc, and invalid for MSVC.
00124 //
00125 // There's also no need for a default action for unsigned wchar_t, as
00126 // that type is the same as unsigned int for gcc, and invalid for
00127 // MSVC.
00128 #if GMOCK_WCHAR_T_IS_NATIVE_
00129 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U);  // NOLINT
00130 #endif
00131 
00132 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned short, 0U);  // NOLINT
00133 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed short, 0);     // NOLINT
00134 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned int, 0U);
00135 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed int, 0);
00136 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long, 0UL);  // NOLINT
00137 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long, 0L);     // NOLINT
00138 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(UInt64, 0);
00139 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(Int64, 0);
00140 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(float, 0);
00141 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(double, 0);
00142 
00143 #undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_
00144 
00145 }  // namespace internal
00146 
00147 // When an unexpected function call is encountered, Google Mock will
00148 // let it return a default value if the user has specified one for its
00149 // return type, or if the return type has a built-in default value;
00150 // otherwise Google Mock won't know what value to return and will have
00151 // to abort the process.
00152 //
00153 // The DefaultValue<T> class allows a user to specify the
00154 // default value for a type T that is both copyable and publicly
00155 // destructible (i.e. anything that can be used as a function return
00156 // type).  The usage is:
00157 //
00158 //   // Sets the default value for type T to be foo.
00159 //   DefaultValue<T>::Set(foo);
00160 template <typename T>
00161 class DefaultValue {
00162  public:
00163   // Sets the default value for type T; requires T to be
00164   // copy-constructable and have a public destructor.
00165   static void Set(T x) {
00166     delete value_;
00167     value_ = new T(x);
00168   }
00169 
00170   // Unsets the default value for type T.
00171   static void Clear() {
00172     delete value_;
00173     value_ = NULL;
00174   }
00175 
00176   // Returns true iff the user has set the default value for type T.
00177   static bool IsSet() { return value_ != NULL; }
00178 
00179   // Returns true if T has a default return value set by the user or there
00180   // exists a built-in default value.
00181   static bool Exists() {
00182     return IsSet() || internal::BuiltInDefaultValue<T>::Exists();
00183   }
00184 
00185   // Returns the default value for type T if the user has set one;
00186   // otherwise returns the built-in default value if there is one;
00187   // otherwise aborts the process.
00188   static T Get() {
00189     return value_ == NULL ?
00190         internal::BuiltInDefaultValue<T>::Get() : *value_;
00191   }
00192 
00193  private:
00194   static const T* value_;
00195 };
00196 
00197 // This partial specialization allows a user to set default values for
00198 // reference types.
00199 template <typename T>
00200 class DefaultValue<T&> {
00201  public:
00202   // Sets the default value for type T&.
00203   static void Set(T& x) {  // NOLINT
00204     address_ = &x;
00205   }
00206 
00207   // Unsets the default value for type T&.
00208   static void Clear() {
00209     address_ = NULL;
00210   }
00211 
00212   // Returns true iff the user has set the default value for type T&.
00213   static bool IsSet() { return address_ != NULL; }
00214 
00215   // Returns true if T has a default return value set by the user or there
00216   // exists a built-in default value.
00217   static bool Exists() {
00218     return IsSet() || internal::BuiltInDefaultValue<T&>::Exists();
00219   }
00220 
00221   // Returns the default value for type T& if the user has set one;
00222   // otherwise returns the built-in default value if there is one;
00223   // otherwise aborts the process.
00224   static T& Get() {
00225     return address_ == NULL ?
00226         internal::BuiltInDefaultValue<T&>::Get() : *address_;
00227   }
00228 
00229  private:
00230   static T* address_;
00231 };
00232 
00233 // This specialization allows DefaultValue<void>::Get() to
00234 // compile.
00235 template <>
00236 class DefaultValue<void> {
00237  public:
00238   static bool Exists() { return true; }
00239   static void Get() {}
00240 };
00241 
00242 // Points to the user-set default value for type T.
00243 template <typename T>
00244 const T* DefaultValue<T>::value_ = NULL;
00245 
00246 // Points to the user-set default value for type T&.
00247 template <typename T>
00248 T* DefaultValue<T&>::address_ = NULL;
00249 
00250 // Implement this interface to define an action for function type F.
00251 template <typename F>
00252 class ActionInterface {
00253  public:
00254   typedef typename internal::Function<F>::Result Result;
00255   typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
00256 
00257   ActionInterface() {}
00258   virtual ~ActionInterface() {}
00259 
00260   // Performs the action.  This method is not const, as in general an
00261   // action can have side effects and be stateful.  For example, a
00262   // get-the-next-element-from-the-collection action will need to
00263   // remember the current element.
00264   virtual Result Perform(const ArgumentTuple& args) = 0;
00265 
00266  private:
00267   GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionInterface);
00268 };
00269 
00270 // An Action<F> is a copyable and IMMUTABLE (except by assignment)
00271 // object that represents an action to be taken when a mock function
00272 // of type F is called.  The implementation of Action<T> is just a
00273 // linked_ptr to const ActionInterface<T>, so copying is fairly cheap.
00274 // Don't inherit from Action!
00275 //
00276 // You can view an object implementing ActionInterface<F> as a
00277 // concrete action (including its current state), and an Action<F>
00278 // object as a handle to it.
00279 template <typename F>
00280 class Action {
00281  public:
00282   typedef typename internal::Function<F>::Result Result;
00283   typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
00284 
00285   // Constructs a null Action.  Needed for storing Action objects in
00286   // STL containers.
00287   Action() : impl_(NULL) {}
00288 
00289   // Constructs an Action from its implementation.  A NULL impl is
00290   // used to represent the "do-default" action.
00291   explicit Action(ActionInterface<F>* impl) : impl_(impl) {}
00292 
00293   // Copy constructor.
00294   Action(const Action& action) : impl_(action.impl_) {}
00295 
00296   // This constructor allows us to turn an Action<Func> object into an
00297   // Action<F>, as long as F's arguments can be implicitly converted
00298   // to Func's and Func's return type can be implicitly converted to
00299   // F's.
00300   template <typename Func>
00301   explicit Action(const Action<Func>& action);
00302 
00303   // Returns true iff this is the DoDefault() action.
00304   bool IsDoDefault() const { return impl_.get() == NULL; }
00305 
00306   // Performs the action.  Note that this method is const even though
00307   // the corresponding method in ActionInterface is not.  The reason
00308   // is that a const Action<F> means that it cannot be re-bound to
00309   // another concrete action, not that the concrete action it binds to
00310   // cannot change state.  (Think of the difference between a const
00311   // pointer and a pointer to const.)
00312   Result Perform(const ArgumentTuple& args) const {
00313     internal::Assert(
00314         !IsDoDefault(), __FILE__, __LINE__,
00315         "You are using DoDefault() inside a composite action like "
00316         "DoAll() or WithArgs().  This is not supported for technical "
00317         "reasons.  Please instead spell out the default action, or "
00318         "assign the default action to an Action variable and use "
00319         "the variable in various places.");
00320     return impl_->Perform(args);
00321   }
00322 
00323  private:
00324   template <typename F1, typename F2>
00325   friend class internal::ActionAdaptor;
00326 
00327   internal::linked_ptr<ActionInterface<F> > impl_;
00328 };
00329 
00330 // The PolymorphicAction class template makes it easy to implement a
00331 // polymorphic action (i.e. an action that can be used in mock
00332 // functions of than one type, e.g. Return()).
00333 //
00334 // To define a polymorphic action, a user first provides a COPYABLE
00335 // implementation class that has a Perform() method template:
00336 //
00337 //   class FooAction {
00338 //    public:
00339 //     template <typename Result, typename ArgumentTuple>
00340 //     Result Perform(const ArgumentTuple& args) const {
00341 //       // Processes the arguments and returns a result, using
00342 //       // tr1::get<N>(args) to get the N-th (0-based) argument in the tuple.
00343 //     }
00344 //     ...
00345 //   };
00346 //
00347 // Then the user creates the polymorphic action using
00348 // MakePolymorphicAction(object) where object has type FooAction.  See
00349 // the definition of Return(void) and SetArgumentPointee<N>(value) for
00350 // complete examples.
00351 template <typename Impl>
00352 class PolymorphicAction {
00353  public:
00354   explicit PolymorphicAction(const Impl& impl) : impl_(impl) {}
00355 
00356   template <typename F>
00357   operator Action<F>() const {
00358     return Action<F>(new MonomorphicImpl<F>(impl_));
00359   }
00360 
00361  private:
00362   template <typename F>
00363   class MonomorphicImpl : public ActionInterface<F> {
00364    public:
00365     typedef typename internal::Function<F>::Result Result;
00366     typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
00367 
00368     explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
00369 
00370     virtual Result Perform(const ArgumentTuple& args) {
00371       return impl_.template Perform<Result>(args);
00372     }
00373 
00374    private:
00375     Impl impl_;
00376 
00377     GTEST_DISALLOW_ASSIGN_(MonomorphicImpl);
00378   };
00379 
00380   Impl impl_;
00381 
00382   GTEST_DISALLOW_ASSIGN_(PolymorphicAction);
00383 };
00384 
00385 // Creates an Action from its implementation and returns it.  The
00386 // created Action object owns the implementation.
00387 template <typename F>
00388 Action<F> MakeAction(ActionInterface<F>* impl) {
00389   return Action<F>(impl);
00390 }
00391 
00392 // Creates a polymorphic action from its implementation.  This is
00393 // easier to use than the PolymorphicAction<Impl> constructor as it
00394 // doesn't require you to explicitly write the template argument, e.g.
00395 //
00396 //   MakePolymorphicAction(foo);
00397 // vs
00398 //   PolymorphicAction<TypeOfFoo>(foo);
00399 template <typename Impl>
00400 inline PolymorphicAction<Impl> MakePolymorphicAction(const Impl& impl) {
00401   return PolymorphicAction<Impl>(impl);
00402 }
00403 
00404 namespace internal {
00405 
00406 // Allows an Action<F2> object to pose as an Action<F1>, as long as F2
00407 // and F1 are compatible.
00408 template <typename F1, typename F2>
00409 class ActionAdaptor : public ActionInterface<F1> {
00410  public:
00411   typedef typename internal::Function<F1>::Result Result;
00412   typedef typename internal::Function<F1>::ArgumentTuple ArgumentTuple;
00413 
00414   explicit ActionAdaptor(const Action<F2>& from) : impl_(from.impl_) {}
00415 
00416   virtual Result Perform(const ArgumentTuple& args) {
00417     return impl_->Perform(args);
00418   }
00419 
00420  private:
00421   const internal::linked_ptr<ActionInterface<F2> > impl_;
00422 
00423   GTEST_DISALLOW_ASSIGN_(ActionAdaptor);
00424 };
00425 
00426 // Implements the polymorphic Return(x) action, which can be used in
00427 // any function that returns the type of x, regardless of the argument
00428 // types.
00429 //
00430 // Note: The value passed into Return must be converted into
00431 // Function<F>::Result when this action is cast to Action<F> rather than
00432 // when that action is performed. This is important in scenarios like
00433 //
00434 // MOCK_METHOD1(Method, T(U));
00435 // ...
00436 // {
00437 //   Foo foo;
00438 //   X x(&foo);
00439 //   EXPECT_CALL(mock, Method(_)).WillOnce(Return(x));
00440 // }
00441 //
00442 // In the example above the variable x holds reference to foo which leaves
00443 // scope and gets destroyed.  If copying X just copies a reference to foo,
00444 // that copy will be left with a hanging reference.  If conversion to T
00445 // makes a copy of foo, the above code is safe. To support that scenario, we
00446 // need to make sure that the type conversion happens inside the EXPECT_CALL
00447 // statement, and conversion of the result of Return to Action<T(U)> is a
00448 // good place for that.
00449 //
00450 template <typename R>
00451 class ReturnAction {
00452  public:
00453   // Constructs a ReturnAction object from the value to be returned.
00454   // 'value' is passed by value instead of by const reference in order
00455   // to allow Return("string literal") to compile.
00456   explicit ReturnAction(R value) : value_(value) {}
00457 
00458   // This template type conversion operator allows Return(x) to be
00459   // used in ANY function that returns x's type.
00460   template <typename F>
00461   operator Action<F>() const {
00462     // Assert statement belongs here because this is the best place to verify
00463     // conditions on F. It produces the clearest error messages
00464     // in most compilers.
00465     // Impl really belongs in this scope as a local class but can't
00466     // because MSVC produces duplicate symbols in different translation units
00467     // in this case. Until MS fixes that bug we put Impl into the class scope
00468     // and put the typedef both here (for use in assert statement) and
00469     // in the Impl class. But both definitions must be the same.
00470     typedef typename Function<F>::Result Result;
00471     GTEST_COMPILE_ASSERT_(
00472         !internal::is_reference<Result>::value,
00473         use_ReturnRef_instead_of_Return_to_return_a_reference);
00474     return Action<F>(new Impl<F>(value_));
00475   }
00476 
00477  private:
00478   // Implements the Return(x) action for a particular function type F.
00479   template <typename F>
00480   class Impl : public ActionInterface<F> {
00481    public:
00482     typedef typename Function<F>::Result Result;
00483     typedef typename Function<F>::ArgumentTuple ArgumentTuple;
00484 
00485     // The implicit cast is necessary when Result has more than one
00486     // single-argument constructor (e.g. Result is std::vector<int>) and R
00487     // has a type conversion operator template.  In that case, value_(value)
00488     // won't compile as the compiler doesn't known which constructor of
00489     // Result to call.  ImplicitCast_ forces the compiler to convert R to
00490     // Result without considering explicit constructors, thus resolving the
00491     // ambiguity. value_ is then initialized using its copy constructor.
00492     explicit Impl(R value)
00493         : value_(::testing::internal::ImplicitCast_<Result>(value)) {}
00494 
00495     virtual Result Perform(const ArgumentTuple&) { return value_; }
00496 
00497    private:
00498     GTEST_COMPILE_ASSERT_(!internal::is_reference<Result>::value,
00499                           Result_cannot_be_a_reference_type);
00500     Result value_;
00501 
00502     GTEST_DISALLOW_ASSIGN_(Impl);
00503   };
00504 
00505   R value_;
00506 
00507   GTEST_DISALLOW_ASSIGN_(ReturnAction);
00508 };
00509 
00510 // Implements the ReturnNull() action.
00511 class ReturnNullAction {
00512  public:
00513   // Allows ReturnNull() to be used in any pointer-returning function.
00514   template <typename Result, typename ArgumentTuple>
00515   static Result Perform(const ArgumentTuple&) {
00516     GTEST_COMPILE_ASSERT_(internal::is_pointer<Result>::value,
00517                           ReturnNull_can_be_used_to_return_a_pointer_only);
00518     return NULL;
00519   }
00520 };
00521 
00522 // Implements the Return() action.
00523 class ReturnVoidAction {
00524  public:
00525   // Allows Return() to be used in any void-returning function.
00526   template <typename Result, typename ArgumentTuple>
00527   static void Perform(const ArgumentTuple&) {
00528     CompileAssertTypesEqual<void, Result>();
00529   }
00530 };
00531 
00532 // Implements the polymorphic ReturnRef(x) action, which can be used
00533 // in any function that returns a reference to the type of x,
00534 // regardless of the argument types.
00535 template <typename T>
00536 class ReturnRefAction {
00537  public:
00538   // Constructs a ReturnRefAction object from the reference to be returned.
00539   explicit ReturnRefAction(T& ref) : ref_(ref) {}  // NOLINT
00540 
00541   // This template type conversion operator allows ReturnRef(x) to be
00542   // used in ANY function that returns a reference to x's type.
00543   template <typename F>
00544   operator Action<F>() const {
00545     typedef typename Function<F>::Result Result;
00546     // Asserts that the function return type is a reference.  This
00547     // catches the user error of using ReturnRef(x) when Return(x)
00548     // should be used, and generates some helpful error message.
00549     GTEST_COMPILE_ASSERT_(internal::is_reference<Result>::value,
00550                           use_Return_instead_of_ReturnRef_to_return_a_value);
00551     return Action<F>(new Impl<F>(ref_));
00552   }
00553 
00554  private:
00555   // Implements the ReturnRef(x) action for a particular function type F.
00556   template <typename F>
00557   class Impl : public ActionInterface<F> {
00558    public:
00559     typedef typename Function<F>::Result Result;
00560     typedef typename Function<F>::ArgumentTuple ArgumentTuple;
00561 
00562     explicit Impl(T& ref) : ref_(ref) {}  // NOLINT
00563 
00564     virtual Result Perform(const ArgumentTuple&) {
00565       return ref_;
00566     }
00567 
00568    private:
00569     T& ref_;
00570 
00571     GTEST_DISALLOW_ASSIGN_(Impl);
00572   };
00573 
00574   T& ref_;
00575 
00576   GTEST_DISALLOW_ASSIGN_(ReturnRefAction);
00577 };
00578 
00579 // Implements the polymorphic ReturnRefOfCopy(x) action, which can be
00580 // used in any function that returns a reference to the type of x,
00581 // regardless of the argument types.
00582 template <typename T>
00583 class ReturnRefOfCopyAction {
00584  public:
00585   // Constructs a ReturnRefOfCopyAction object from the reference to
00586   // be returned.
00587   explicit ReturnRefOfCopyAction(const T& value) : value_(value) {}  // NOLINT
00588 
00589   // This template type conversion operator allows ReturnRefOfCopy(x) to be
00590   // used in ANY function that returns a reference to x's type.
00591   template <typename F>
00592   operator Action<F>() const {
00593     typedef typename Function<F>::Result Result;
00594     // Asserts that the function return type is a reference.  This
00595     // catches the user error of using ReturnRefOfCopy(x) when Return(x)
00596     // should be used, and generates some helpful error message.
00597     GTEST_COMPILE_ASSERT_(
00598         internal::is_reference<Result>::value,
00599         use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
00600     return Action<F>(new Impl<F>(value_));
00601   }
00602 
00603  private:
00604   // Implements the ReturnRefOfCopy(x) action for a particular function type F.
00605   template <typename F>
00606   class Impl : public ActionInterface<F> {
00607    public:
00608     typedef typename Function<F>::Result Result;
00609     typedef typename Function<F>::ArgumentTuple ArgumentTuple;
00610 
00611     explicit Impl(const T& value) : value_(value) {}  // NOLINT
00612 
00613     virtual Result Perform(const ArgumentTuple&) {
00614       return value_;
00615     }
00616 
00617    private:
00618     T value_;
00619 
00620     GTEST_DISALLOW_ASSIGN_(Impl);
00621   };
00622 
00623   const T value_;
00624 
00625   GTEST_DISALLOW_ASSIGN_(ReturnRefOfCopyAction);
00626 };
00627 
00628 // Implements the polymorphic DoDefault() action.
00629 class DoDefaultAction {
00630  public:
00631   // This template type conversion operator allows DoDefault() to be
00632   // used in any function.
00633   template <typename F>
00634   operator Action<F>() const { return Action<F>(NULL); }
00635 };
00636 
00637 // Implements the Assign action to set a given pointer referent to a
00638 // particular value.
00639 template <typename T1, typename T2>
00640 class AssignAction {
00641  public:
00642   AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {}
00643 
00644   template <typename Result, typename ArgumentTuple>
00645   void Perform(const ArgumentTuple& /* args */) const {
00646     *ptr_ = value_;
00647   }
00648 
00649  private:
00650   T1* const ptr_;
00651   const T2 value_;
00652 
00653   GTEST_DISALLOW_ASSIGN_(AssignAction);
00654 };
00655 
00656 #if !GTEST_OS_WINDOWS_MOBILE
00657 
00658 // Implements the SetErrnoAndReturn action to simulate return from
00659 // various system calls and libc functions.
00660 template <typename T>
00661 class SetErrnoAndReturnAction {
00662  public:
00663   SetErrnoAndReturnAction(int errno_value, T result)
00664       : errno_(errno_value),
00665         result_(result) {}
00666   template <typename Result, typename ArgumentTuple>
00667   Result Perform(const ArgumentTuple& /* args */) const {
00668     errno = errno_;
00669     return result_;
00670   }
00671 
00672  private:
00673   const int errno_;
00674   const T result_;
00675 
00676   GTEST_DISALLOW_ASSIGN_(SetErrnoAndReturnAction);
00677 };
00678 
00679 #endif  // !GTEST_OS_WINDOWS_MOBILE
00680 
00681 // Implements the SetArgumentPointee<N>(x) action for any function
00682 // whose N-th argument (0-based) is a pointer to x's type.  The
00683 // template parameter kIsProto is true iff type A is ProtocolMessage,
00684 // proto2::Message, or a sub-class of those.
00685 template <size_t N, typename A, bool kIsProto>
00686 class SetArgumentPointeeAction {
00687  public:
00688   // Constructs an action that sets the variable pointed to by the
00689   // N-th function argument to 'value'.
00690   explicit SetArgumentPointeeAction(const A& value) : value_(value) {}
00691 
00692   template <typename Result, typename ArgumentTuple>
00693   void Perform(const ArgumentTuple& args) const {
00694     CompileAssertTypesEqual<void, Result>();
00695     *::std::tr1::get<N>(args) = value_;
00696   }
00697 
00698  private:
00699   const A value_;
00700 
00701   GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
00702 };
00703 
00704 template <size_t N, typename Proto>
00705 class SetArgumentPointeeAction<N, Proto, true> {
00706  public:
00707   // Constructs an action that sets the variable pointed to by the
00708   // N-th function argument to 'proto'.  Both ProtocolMessage and
00709   // proto2::Message have the CopyFrom() method, so the same
00710   // implementation works for both.
00711   explicit SetArgumentPointeeAction(const Proto& proto) : proto_(new Proto) {
00712     proto_->CopyFrom(proto);
00713   }
00714 
00715   template <typename Result, typename ArgumentTuple>
00716   void Perform(const ArgumentTuple& args) const {
00717     CompileAssertTypesEqual<void, Result>();
00718     ::std::tr1::get<N>(args)->CopyFrom(*proto_);
00719   }
00720 
00721  private:
00722   const internal::linked_ptr<Proto> proto_;
00723 
00724   GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
00725 };
00726 
00727 // Implements the InvokeWithoutArgs(f) action.  The template argument
00728 // FunctionImpl is the implementation type of f, which can be either a
00729 // function pointer or a functor.  InvokeWithoutArgs(f) can be used as an
00730 // Action<F> as long as f's type is compatible with F (i.e. f can be
00731 // assigned to a tr1::function<F>).
00732 template <typename FunctionImpl>
00733 class InvokeWithoutArgsAction {
00734  public:
00735   // The c'tor makes a copy of function_impl (either a function
00736   // pointer or a functor).
00737   explicit InvokeWithoutArgsAction(FunctionImpl function_impl)
00738       : function_impl_(function_impl) {}
00739 
00740   // Allows InvokeWithoutArgs(f) to be used as any action whose type is
00741   // compatible with f.
00742   template <typename Result, typename ArgumentTuple>
00743   Result Perform(const ArgumentTuple&) { return function_impl_(); }
00744 
00745  private:
00746   FunctionImpl function_impl_;
00747 
00748   GTEST_DISALLOW_ASSIGN_(InvokeWithoutArgsAction);
00749 };
00750 
00751 // Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action.
00752 template <class Class, typename MethodPtr>
00753 class InvokeMethodWithoutArgsAction {
00754  public:
00755   InvokeMethodWithoutArgsAction(Class* obj_ptr, MethodPtr method_ptr)
00756       : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
00757 
00758   template <typename Result, typename ArgumentTuple>
00759   Result Perform(const ArgumentTuple&) const {
00760     return (obj_ptr_->*method_ptr_)();
00761   }
00762 
00763  private:
00764   Class* const obj_ptr_;
00765   const MethodPtr method_ptr_;
00766 
00767   GTEST_DISALLOW_ASSIGN_(InvokeMethodWithoutArgsAction);
00768 };
00769 
00770 // Implements the IgnoreResult(action) action.
00771 template <typename A>
00772 class IgnoreResultAction {
00773  public:
00774   explicit IgnoreResultAction(const A& action) : action_(action) {}
00775 
00776   template <typename F>
00777   operator Action<F>() const {
00778     // Assert statement belongs here because this is the best place to verify
00779     // conditions on F. It produces the clearest error messages
00780     // in most compilers.
00781     // Impl really belongs in this scope as a local class but can't
00782     // because MSVC produces duplicate symbols in different translation units
00783     // in this case. Until MS fixes that bug we put Impl into the class scope
00784     // and put the typedef both here (for use in assert statement) and
00785     // in the Impl class. But both definitions must be the same.
00786     typedef typename internal::Function<F>::Result Result;
00787 
00788     // Asserts at compile time that F returns void.
00789     CompileAssertTypesEqual<void, Result>();
00790 
00791     return Action<F>(new Impl<F>(action_));
00792   }
00793 
00794  private:
00795   template <typename F>
00796   class Impl : public ActionInterface<F> {
00797    public:
00798     typedef typename internal::Function<F>::Result Result;
00799     typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
00800 
00801     explicit Impl(const A& action) : action_(action) {}
00802 
00803     virtual void Perform(const ArgumentTuple& args) {
00804       // Performs the action and ignores its result.
00805       action_.Perform(args);
00806     }
00807 
00808    private:
00809     // Type OriginalFunction is the same as F except that its return
00810     // type is IgnoredValue.
00811     typedef typename internal::Function<F>::MakeResultIgnoredValue
00812         OriginalFunction;
00813 
00814     const Action<OriginalFunction> action_;
00815 
00816     GTEST_DISALLOW_ASSIGN_(Impl);
00817   };
00818 
00819   const A action_;
00820 
00821   GTEST_DISALLOW_ASSIGN_(IgnoreResultAction);
00822 };
00823 
00824 // A ReferenceWrapper<T> object represents a reference to type T,
00825 // which can be either const or not.  It can be explicitly converted
00826 // from, and implicitly converted to, a T&.  Unlike a reference,
00827 // ReferenceWrapper<T> can be copied and can survive template type
00828 // inference.  This is used to support by-reference arguments in the
00829 // InvokeArgument<N>(...) action.  The idea was from "reference
00830 // wrappers" in tr1, which we don't have in our source tree yet.
00831 template <typename T>
00832 class ReferenceWrapper {
00833  public:
00834   // Constructs a ReferenceWrapper<T> object from a T&.
00835   explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {}  // NOLINT
00836 
00837   // Allows a ReferenceWrapper<T> object to be implicitly converted to
00838   // a T&.
00839   operator T&() const { return *pointer_; }
00840  private:
00841   T* pointer_;
00842 };
00843 
00844 // Allows the expression ByRef(x) to be printed as a reference to x.
00845 template <typename T>
00846 void PrintTo(const ReferenceWrapper<T>& ref, ::std::ostream* os) {
00847   T& value = ref;
00848   UniversalPrinter<T&>::Print(value, os);
00849 }
00850 
00851 // Does two actions sequentially.  Used for implementing the DoAll(a1,
00852 // a2, ...) action.
00853 template <typename Action1, typename Action2>
00854 class DoBothAction {
00855  public:
00856   DoBothAction(Action1 action1, Action2 action2)
00857       : action1_(action1), action2_(action2) {}
00858 
00859   // This template type conversion operator allows DoAll(a1, ..., a_n)
00860   // to be used in ANY function of compatible type.
00861   template <typename F>
00862   operator Action<F>() const {
00863     return Action<F>(new Impl<F>(action1_, action2_));
00864   }
00865 
00866  private:
00867   // Implements the DoAll(...) action for a particular function type F.
00868   template <typename F>
00869   class Impl : public ActionInterface<F> {
00870    public:
00871     typedef typename Function<F>::Result Result;
00872     typedef typename Function<F>::ArgumentTuple ArgumentTuple;
00873     typedef typename Function<F>::MakeResultVoid VoidResult;
00874 
00875     Impl(const Action<VoidResult>& action1, const Action<F>& action2)
00876         : action1_(action1), action2_(action2) {}
00877 
00878     virtual Result Perform(const ArgumentTuple& args) {
00879       action1_.Perform(args);
00880       return action2_.Perform(args);
00881     }
00882 
00883    private:
00884     const Action<VoidResult> action1_;
00885     const Action<F> action2_;
00886 
00887     GTEST_DISALLOW_ASSIGN_(Impl);
00888   };
00889 
00890   Action1 action1_;
00891   Action2 action2_;
00892 
00893   GTEST_DISALLOW_ASSIGN_(DoBothAction);
00894 };
00895 
00896 }  // namespace internal
00897 
00898 // An Unused object can be implicitly constructed from ANY value.
00899 // This is handy when defining actions that ignore some or all of the
00900 // mock function arguments.  For example, given
00901 //
00902 //   MOCK_METHOD3(Foo, double(const string& label, double x, double y));
00903 //   MOCK_METHOD3(Bar, double(int index, double x, double y));
00904 //
00905 // instead of
00906 //
00907 //   double DistanceToOriginWithLabel(const string& label, double x, double y) {
00908 //     return sqrt(x*x + y*y);
00909 //   }
00910 //   double DistanceToOriginWithIndex(int index, double x, double y) {
00911 //     return sqrt(x*x + y*y);
00912 //   }
00913 //   ...
00914 //   EXEPCT_CALL(mock, Foo("abc", _, _))
00915 //       .WillOnce(Invoke(DistanceToOriginWithLabel));
00916 //   EXEPCT_CALL(mock, Bar(5, _, _))
00917 //       .WillOnce(Invoke(DistanceToOriginWithIndex));
00918 //
00919 // you could write
00920 //
00921 //   // We can declare any uninteresting argument as Unused.
00922 //   double DistanceToOrigin(Unused, double x, double y) {
00923 //     return sqrt(x*x + y*y);
00924 //   }
00925 //   ...
00926 //   EXEPCT_CALL(mock, Foo("abc", _, _)).WillOnce(Invoke(DistanceToOrigin));
00927 //   EXEPCT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin));
00928 typedef internal::IgnoredValue Unused;
00929 
00930 // This constructor allows us to turn an Action<From> object into an
00931 // Action<To>, as long as To's arguments can be implicitly converted
00932 // to From's and From's return type cann be implicitly converted to
00933 // To's.
00934 template <typename To>
00935 template <typename From>
00936 Action<To>::Action(const Action<From>& from)
00937     : impl_(new internal::ActionAdaptor<To, From>(from)) {}
00938 
00939 // Creates an action that returns 'value'.  'value' is passed by value
00940 // instead of const reference - otherwise Return("string literal")
00941 // will trigger a compiler error about using array as initializer.
00942 template <typename R>
00943 internal::ReturnAction<R> Return(R value) {
00944   return internal::ReturnAction<R>(value);
00945 }
00946 
00947 // Creates an action that returns NULL.
00948 inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() {
00949   return MakePolymorphicAction(internal::ReturnNullAction());
00950 }
00951 
00952 // Creates an action that returns from a void function.
00953 inline PolymorphicAction<internal::ReturnVoidAction> Return() {
00954   return MakePolymorphicAction(internal::ReturnVoidAction());
00955 }
00956 
00957 // Creates an action that returns the reference to a variable.
00958 template <typename R>
00959 inline internal::ReturnRefAction<R> ReturnRef(R& x) {  // NOLINT
00960   return internal::ReturnRefAction<R>(x);
00961 }
00962 
00963 // Creates an action that returns the reference to a copy of the
00964 // argument.  The copy is created when the action is constructed and
00965 // lives as long as the action.
00966 template <typename R>
00967 inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) {
00968   return internal::ReturnRefOfCopyAction<R>(x);
00969 }
00970 
00971 // Creates an action that does the default action for the give mock function.
00972 inline internal::DoDefaultAction DoDefault() {
00973   return internal::DoDefaultAction();
00974 }
00975 
00976 // Creates an action that sets the variable pointed by the N-th
00977 // (0-based) function argument to 'value'.
00978 template <size_t N, typename T>
00979 PolymorphicAction<
00980   internal::SetArgumentPointeeAction<
00981     N, T, internal::IsAProtocolMessage<T>::value> >
00982 SetArgPointee(const T& x) {
00983   return MakePolymorphicAction(internal::SetArgumentPointeeAction<
00984       N, T, internal::IsAProtocolMessage<T>::value>(x));
00985 }
00986 
00987 #if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
00988 // This overload allows SetArgPointee() to accept a string literal.
00989 // GCC prior to the version 4.0 and Symbian C++ compiler cannot distinguish
00990 // this overload from the templated version and emit a compile error.
00991 template <size_t N>
00992 PolymorphicAction<
00993   internal::SetArgumentPointeeAction<N, const char*, false> >
00994 SetArgPointee(const char* p) {
00995   return MakePolymorphicAction(internal::SetArgumentPointeeAction<
00996       N, const char*, false>(p));
00997 }
00998 
00999 template <size_t N>
01000 PolymorphicAction<
01001   internal::SetArgumentPointeeAction<N, const wchar_t*, false> >
01002 SetArgPointee(const wchar_t* p) {
01003   return MakePolymorphicAction(internal::SetArgumentPointeeAction<
01004       N, const wchar_t*, false>(p));
01005 }
01006 #endif
01007 
01008 // The following version is DEPRECATED.
01009 template <size_t N, typename T>
01010 PolymorphicAction<
01011   internal::SetArgumentPointeeAction<
01012     N, T, internal::IsAProtocolMessage<T>::value> >
01013 SetArgumentPointee(const T& x) {
01014   return MakePolymorphicAction(internal::SetArgumentPointeeAction<
01015       N, T, internal::IsAProtocolMessage<T>::value>(x));
01016 }
01017 
01018 // Creates an action that sets a pointer referent to a given value.
01019 template <typename T1, typename T2>
01020 PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) {
01021   return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val));
01022 }
01023 
01024 #if !GTEST_OS_WINDOWS_MOBILE
01025 
01026 // Creates an action that sets errno and returns the appropriate error.
01027 template <typename T>
01028 PolymorphicAction<internal::SetErrnoAndReturnAction<T> >
01029 SetErrnoAndReturn(int errval, T result) {
01030   return MakePolymorphicAction(
01031       internal::SetErrnoAndReturnAction<T>(errval, result));
01032 }
01033 
01034 #endif  // !GTEST_OS_WINDOWS_MOBILE
01035 
01036 // Various overloads for InvokeWithoutArgs().
01037 
01038 // Creates an action that invokes 'function_impl' with no argument.
01039 template <typename FunctionImpl>
01040 PolymorphicAction<internal::InvokeWithoutArgsAction<FunctionImpl> >
01041 InvokeWithoutArgs(FunctionImpl function_impl) {
01042   return MakePolymorphicAction(
01043       internal::InvokeWithoutArgsAction<FunctionImpl>(function_impl));
01044 }
01045 
01046 // Creates an action that invokes the given method on the given object
01047 // with no argument.
01048 template <class Class, typename MethodPtr>
01049 PolymorphicAction<internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> >
01050 InvokeWithoutArgs(Class* obj_ptr, MethodPtr method_ptr) {
01051   return MakePolymorphicAction(
01052       internal::InvokeMethodWithoutArgsAction<Class, MethodPtr>(
01053           obj_ptr, method_ptr));
01054 }
01055 
01056 // Creates an action that performs an_action and throws away its
01057 // result.  In other words, it changes the return type of an_action to
01058 // void.  an_action MUST NOT return void, or the code won't compile.
01059 template <typename A>
01060 inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) {
01061   return internal::IgnoreResultAction<A>(an_action);
01062 }
01063 
01064 // Creates a reference wrapper for the given L-value.  If necessary,
01065 // you can explicitly specify the type of the reference.  For example,
01066 // suppose 'derived' is an object of type Derived, ByRef(derived)
01067 // would wrap a Derived&.  If you want to wrap a const Base& instead,
01068 // where Base is a base class of Derived, just write:
01069 //
01070 //   ByRef<const Base>(derived)
01071 template <typename T>
01072 inline internal::ReferenceWrapper<T> ByRef(T& l_value) {  // NOLINT
01073   return internal::ReferenceWrapper<T>(l_value);
01074 }
01075 
01076 }  // namespace testing
01077 
01078 #endif  // GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:40