From 0adeadd2830211f827fd2908e4621f6a4afa810c Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 16 Jan 2019 15:23:44 -0500 Subject: Googletest export Refactor the `Invoke` and `InvokeWithoutArgs` actions: - Replace pump'd classes and functions with templates. - Make the polymorphic actions be polymorphic functors instead. - Fix Invoke(Callback*) to work with subclasses of the callbacks, instead of trying to diagnose that in gmock_doctor. PiperOrigin-RevId: 229604112 --- googlemock/include/gmock/gmock-actions.h | 108 ++++---- googlemock/include/gmock/gmock-generated-actions.h | 302 --------------------- .../include/gmock/gmock-generated-actions.h.pump | 70 ----- googlemock/include/gmock/gmock-more-actions.h | 66 +---- .../include/gmock/internal/gmock-internal-utils.h | 1 + googlemock/test/gmock-actions_test.cc | 3 +- googlemock/test/gmock-spec-builders_test.cc | 1 + 7 files changed, 55 insertions(+), 496 deletions(-) diff --git a/googlemock/include/gmock/gmock-actions.h b/googlemock/include/gmock/gmock-actions.h index df49fbc..2f936a1 100644 --- a/googlemock/include/gmock/gmock-actions.h +++ b/googlemock/include/gmock/gmock-actions.h @@ -817,69 +817,47 @@ class SetArgumentPointeeAction { GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction); }; +// Implements the Invoke(object_ptr, &Class::Method) action. +template +struct InvokeMethodAction { + Class* const obj_ptr; + const MethodPtr method_ptr; + + template + auto operator()(Args&&... args) const + -> decltype((obj_ptr->*method_ptr)(std::forward(args)...)) { + return (obj_ptr->*method_ptr)(std::forward(args)...); + } +}; + // Implements the InvokeWithoutArgs(f) action. The template argument // FunctionImpl is the implementation type of f, which can be either a // function pointer or a functor. InvokeWithoutArgs(f) can be used as an -// Action as long as f's type is compatible with F (i.e. f can be -// assigned to a tr1::function). +// Action as long as f's type is compatible with F. template -class InvokeWithoutArgsAction { - public: - // The c'tor makes a copy of function_impl (either a function - // pointer or a functor). - explicit InvokeWithoutArgsAction(FunctionImpl function_impl) - : function_impl_(function_impl) {} +struct InvokeWithoutArgsAction { + FunctionImpl function_impl; // Allows InvokeWithoutArgs(f) to be used as any action whose type is // compatible with f. - template - Result Perform(const ArgumentTuple&) { return function_impl_(); } - - private: - FunctionImpl function_impl_; - - GTEST_DISALLOW_ASSIGN_(InvokeWithoutArgsAction); + template + auto operator()(const Args&...) -> decltype(function_impl()) { + return function_impl(); + } }; // Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action. template -class InvokeMethodWithoutArgsAction { - public: - InvokeMethodWithoutArgsAction(Class* obj_ptr, MethodPtr method_ptr) - : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {} - - template - Result Perform(const ArgumentTuple&) const { - return (obj_ptr_->*method_ptr_)(); - } - - private: - Class* const obj_ptr_; - const MethodPtr method_ptr_; +struct InvokeMethodWithoutArgsAction { + Class* const obj_ptr; + const MethodPtr method_ptr; - GTEST_DISALLOW_ASSIGN_(InvokeMethodWithoutArgsAction); -}; + using ReturnType = typename std::result_of::type; -// Implements the InvokeWithoutArgs(callback) action. -template -class InvokeCallbackWithoutArgsAction { - public: - // The c'tor takes ownership of the callback. - explicit InvokeCallbackWithoutArgsAction(CallbackType* callback) - : callback_(callback) { - callback->CheckIsRepeatable(); // Makes sure the callback is permanent. + template + ReturnType operator()(const Args&...) const { + return (obj_ptr->*method_ptr)(); } - - // This type conversion operator template allows Invoke(callback) to - // be used wherever the callback's return type can be implicitly - // converted to that of the mock function. - template - Result Perform(const ArgumentTuple&) const { return callback_->Run(); } - - private: - const std::shared_ptr callback_; - - GTEST_DISALLOW_ASSIGN_(InvokeCallbackWithoutArgsAction); }; // Implements the IgnoreResult(action) action. @@ -1157,24 +1135,38 @@ SetErrnoAndReturn(int errval, T result) { #endif // !GTEST_OS_WINDOWS_MOBILE -// Various overloads for InvokeWithoutArgs(). +// Various overloads for Invoke(). + +// Legacy function. +// Actions can now be implicitly constructed from callables. No need to create +// wrapper objects. +// This function exists for backwards compatibility. +template +typename std::decay::type Invoke(FunctionImpl&& function_impl) { + return std::forward(function_impl); +} + +// Creates an action that invokes the given method on the given object +// with the mock function's arguments. +template +internal::InvokeMethodAction Invoke(Class* obj_ptr, + MethodPtr method_ptr) { + return {obj_ptr, method_ptr}; +} // Creates an action that invokes 'function_impl' with no argument. template -PolymorphicAction > +internal::InvokeWithoutArgsAction::type> InvokeWithoutArgs(FunctionImpl function_impl) { - return MakePolymorphicAction( - internal::InvokeWithoutArgsAction(function_impl)); + return {std::move(function_impl)}; } // Creates an action that invokes the given method on the given object // with no argument. template -PolymorphicAction > -InvokeWithoutArgs(Class* obj_ptr, MethodPtr method_ptr) { - return MakePolymorphicAction( - internal::InvokeMethodWithoutArgsAction( - obj_ptr, method_ptr)); +internal::InvokeMethodWithoutArgsAction InvokeWithoutArgs( + Class* obj_ptr, MethodPtr method_ptr) { + return {obj_ptr, method_ptr}; } // Creates an action that performs an_action and throws away its diff --git a/googlemock/include/gmock/gmock-generated-actions.h b/googlemock/include/gmock/gmock-generated-actions.h index 910436e..3264bca 100644 --- a/googlemock/include/gmock/gmock-generated-actions.h +++ b/googlemock/include/gmock/gmock-generated-actions.h @@ -50,308 +50,6 @@ namespace testing { namespace internal { -// InvokeHelper knows how to unpack an N-tuple and invoke an N-ary -// function, method, or callback with the unpacked values, where F is -// a function type that takes N arguments. -template -class InvokeHelper; - -template -class InvokeHelper > { - public: - template - static R Invoke(Function function, const ::std::tuple<>&) { - return function(); - } - - template - static R InvokeMethod(Class* obj_ptr, - MethodPtr method_ptr, - const ::std::tuple<>&) { - return (obj_ptr->*method_ptr)(); - } - - template - static R InvokeCallback(CallbackType* callback, - const ::std::tuple<>&) { - return callback->Run(); - } -}; - -template -class InvokeHelper > { - public: - template - static R Invoke(Function function, const ::std::tuple& args) { - return function(std::get<0>(args)); - } - - template - static R InvokeMethod(Class* obj_ptr, - MethodPtr method_ptr, - const ::std::tuple& args) { - return (obj_ptr->*method_ptr)(std::get<0>(args)); - } - - template - static R InvokeCallback(CallbackType* callback, - const ::std::tuple& args) { - return callback->Run(std::get<0>(args)); - } -}; - -template -class InvokeHelper > { - public: - template - static R Invoke(Function function, const ::std::tuple& args) { - return function(std::get<0>(args), std::get<1>(args)); - } - - template - static R InvokeMethod(Class* obj_ptr, - MethodPtr method_ptr, - const ::std::tuple& args) { - return (obj_ptr->*method_ptr)(std::get<0>(args), std::get<1>(args)); - } - - template - static R InvokeCallback(CallbackType* callback, - const ::std::tuple& args) { - return callback->Run(std::get<0>(args), std::get<1>(args)); - } -}; - -template -class InvokeHelper > { - public: - template - static R Invoke(Function function, const ::std::tuple& args) { - return function(std::get<0>(args), std::get<1>(args), - std::get<2>(args)); - } - - template - static R InvokeMethod(Class* obj_ptr, - MethodPtr method_ptr, - const ::std::tuple& args) { - return (obj_ptr->*method_ptr)(std::get<0>(args), std::get<1>(args), - std::get<2>(args)); - } - - template - static R InvokeCallback(CallbackType* callback, - const ::std::tuple& args) { - return callback->Run(std::get<0>(args), std::get<1>(args), - std::get<2>(args)); - } -}; - -template -class InvokeHelper > { - public: - template - static R Invoke(Function function, const ::std::tuple& args) { - return function(std::get<0>(args), std::get<1>(args), - std::get<2>(args), std::get<3>(args)); - } - - template - static R InvokeMethod(Class* obj_ptr, - MethodPtr method_ptr, - const ::std::tuple& args) { - return (obj_ptr->*method_ptr)(std::get<0>(args), std::get<1>(args), - std::get<2>(args), std::get<3>(args)); - } - - template - static R InvokeCallback(CallbackType* callback, - const ::std::tuple& args) { - return callback->Run(std::get<0>(args), std::get<1>(args), - std::get<2>(args), std::get<3>(args)); - } -}; - -template -class InvokeHelper > { - public: - template - static R Invoke(Function function, const ::std::tuple& args) { - return function(std::get<0>(args), std::get<1>(args), - std::get<2>(args), std::get<3>(args), std::get<4>(args)); - } - - template - static R InvokeMethod(Class* obj_ptr, - MethodPtr method_ptr, - const ::std::tuple& args) { - return (obj_ptr->*method_ptr)(std::get<0>(args), std::get<1>(args), - std::get<2>(args), std::get<3>(args), std::get<4>(args)); - } - - template - static R InvokeCallback(CallbackType* callback, - const ::std::tuple& args) { - return callback->Run(std::get<0>(args), std::get<1>(args), - std::get<2>(args), std::get<3>(args), std::get<4>(args)); - } -}; - -template -class InvokeHelper > { - public: - template - static R Invoke(Function function, const ::std::tuple& args) { - return function(std::get<0>(args), std::get<1>(args), - std::get<2>(args), std::get<3>(args), std::get<4>(args), - std::get<5>(args)); - } - - template - static R InvokeMethod(Class* obj_ptr, - MethodPtr method_ptr, - const ::std::tuple& args) { - return (obj_ptr->*method_ptr)(std::get<0>(args), std::get<1>(args), - std::get<2>(args), std::get<3>(args), std::get<4>(args), - std::get<5>(args)); - } - - // There is no InvokeCallback() for 6-tuples -}; - -template -class InvokeHelper > { - public: - template - static R Invoke(Function function, const ::std::tuple& args) { - return function(std::get<0>(args), std::get<1>(args), - std::get<2>(args), std::get<3>(args), std::get<4>(args), - std::get<5>(args), std::get<6>(args)); - } - - template - static R InvokeMethod(Class* obj_ptr, - MethodPtr method_ptr, - const ::std::tuple& args) { - return (obj_ptr->*method_ptr)(std::get<0>(args), std::get<1>(args), - std::get<2>(args), std::get<3>(args), std::get<4>(args), - std::get<5>(args), std::get<6>(args)); - } - - // There is no InvokeCallback() for 7-tuples -}; - -template -class InvokeHelper > { - public: - template - static R Invoke(Function function, const ::std::tuple& args) { - return function(std::get<0>(args), std::get<1>(args), - std::get<2>(args), std::get<3>(args), std::get<4>(args), - std::get<5>(args), std::get<6>(args), std::get<7>(args)); - } - - template - static R InvokeMethod(Class* obj_ptr, - MethodPtr method_ptr, - const ::std::tuple& args) { - return (obj_ptr->*method_ptr)(std::get<0>(args), std::get<1>(args), - std::get<2>(args), std::get<3>(args), std::get<4>(args), - std::get<5>(args), std::get<6>(args), std::get<7>(args)); - } - - // There is no InvokeCallback() for 8-tuples -}; - -template -class InvokeHelper > { - public: - template - static R Invoke(Function function, const ::std::tuple& args) { - return function(std::get<0>(args), std::get<1>(args), - std::get<2>(args), std::get<3>(args), std::get<4>(args), - std::get<5>(args), std::get<6>(args), std::get<7>(args), - std::get<8>(args)); - } - - template - static R InvokeMethod(Class* obj_ptr, - MethodPtr method_ptr, - const ::std::tuple& args) { - return (obj_ptr->*method_ptr)(std::get<0>(args), std::get<1>(args), - std::get<2>(args), std::get<3>(args), std::get<4>(args), - std::get<5>(args), std::get<6>(args), std::get<7>(args), - std::get<8>(args)); - } - - // There is no InvokeCallback() for 9-tuples -}; - -template -class InvokeHelper > { - public: - template - static R Invoke(Function function, const ::std::tuple& args) { - return function(std::get<0>(args), std::get<1>(args), - std::get<2>(args), std::get<3>(args), std::get<4>(args), - std::get<5>(args), std::get<6>(args), std::get<7>(args), - std::get<8>(args), std::get<9>(args)); - } - - template - static R InvokeMethod(Class* obj_ptr, - MethodPtr method_ptr, - const ::std::tuple& args) { - return (obj_ptr->*method_ptr)(std::get<0>(args), std::get<1>(args), - std::get<2>(args), std::get<3>(args), std::get<4>(args), - std::get<5>(args), std::get<6>(args), std::get<7>(args), - std::get<8>(args), std::get<9>(args)); - } - - // There is no InvokeCallback() for 10-tuples -}; - -// Implements the Invoke(callback) action. -template -class InvokeCallbackAction { - public: - // The c'tor takes ownership of the callback. - explicit InvokeCallbackAction(CallbackType* callback) - : callback_(callback) { - callback->CheckIsRepeatable(); // Makes sure the callback is permanent. - } - - // This type conversion operator template allows Invoke(callback) to - // be used wherever the callback's type is compatible with that of - // the mock function, i.e. if the mock function's arguments can be - // implicitly converted to the callback's arguments and the - // callback's result can be implicitly converted to the mock - // function's result. - template - Result Perform(const ArgumentTuple& args) const { - return InvokeHelper::InvokeCallback( - callback_.get(), args); - } - private: - const std::shared_ptr callback_; -}; - // A macro from the ACTION* family (defined later in this file) // defines an action that can be used in a mock function. Typically, // these actions only care about a subset of the arguments of the mock diff --git a/googlemock/include/gmock/gmock-generated-actions.h.pump b/googlemock/include/gmock/gmock-generated-actions.h.pump index 27c96ef..1e9549f 100644 --- a/googlemock/include/gmock/gmock-generated-actions.h.pump +++ b/googlemock/include/gmock/gmock-generated-actions.h.pump @@ -52,76 +52,6 @@ $$}} This meta comment fixes auto-indentation in editors. namespace testing { namespace internal { -// InvokeHelper knows how to unpack an N-tuple and invoke an N-ary -// function, method, or callback with the unpacked values, where F is -// a function type that takes N arguments. -template -class InvokeHelper; - - -$var max_callback_arity = 5 -$range i 0..n -$for i [[ -$range j 1..i -$var types = [[$for j [[, typename A$j]]]] -$var as = [[$for j, [[A$j]]]] -$var args = [[$if i==0 [[]] $else [[ args]]]] -$var gets = [[$for j, [[std::get<$(j - 1)>(args)]]]] -template -class InvokeHelper > { - public: - template - static R Invoke(Function function, const ::std::tuple<$as>&$args) { - return function($gets); - } - - template - static R InvokeMethod(Class* obj_ptr, - MethodPtr method_ptr, - const ::std::tuple<$as>&$args) { - return (obj_ptr->*method_ptr)($gets); - } - - -$if i <= max_callback_arity [[ - template - static R InvokeCallback(CallbackType* callback, - const ::std::tuple<$as>&$args) { - return callback->Run($gets); - } -]] $else [[ - // There is no InvokeCallback() for $i-tuples -]] - -}; - - -]] -// Implements the Invoke(callback) action. -template -class InvokeCallbackAction { - public: - // The c'tor takes ownership of the callback. - explicit InvokeCallbackAction(CallbackType* callback) - : callback_(callback) { - callback->CheckIsRepeatable(); // Makes sure the callback is permanent. - } - - // This type conversion operator template allows Invoke(callback) to - // be used wherever the callback's type is compatible with that of - // the mock function, i.e. if the mock function's arguments can be - // implicitly converted to the callback's arguments and the - // callback's result can be implicitly converted to the mock - // function's result. - template - Result Perform(const ArgumentTuple& args) const { - return InvokeHelper::InvokeCallback( - callback_.get(), args); - } - private: - const std::shared_ptr callback_; -}; - // A macro from the ACTION* family (defined later in this file) // defines an action that can be used in a mock function. Typically, // these actions only care about a subset of the arguments of the mock diff --git a/googlemock/include/gmock/gmock-more-actions.h b/googlemock/include/gmock/gmock-more-actions.h index 1098408..a052495 100644 --- a/googlemock/include/gmock/gmock-more-actions.h +++ b/googlemock/include/gmock/gmock-more-actions.h @@ -38,59 +38,13 @@ #define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_ #include +#include #include "gmock/gmock-generated-actions.h" namespace testing { namespace internal { -// Implements the Invoke(f) action. The template argument -// FunctionImpl is the implementation type of f, which can be either a -// function pointer or a functor. Invoke(f) can be used as an -// Action as long as f's type is compatible with F (i.e. f can be -// assigned to a tr1::function). -template -class InvokeAction { - public: - // The c'tor makes a copy of function_impl (either a function - // pointer or a functor). - explicit InvokeAction(FunctionImpl function_impl) - : function_impl_(function_impl) {} - - template - Result Perform(const ArgumentTuple& args) { - return InvokeHelper::Invoke(function_impl_, args); - } - - private: - FunctionImpl function_impl_; - - GTEST_DISALLOW_ASSIGN_(InvokeAction); -}; - -// Implements the Invoke(object_ptr, &Class::Method) action. -template -class InvokeMethodAction { - public: - InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr) - : method_ptr_(method_ptr), obj_ptr_(obj_ptr) {} - - template - Result Perform(const ArgumentTuple& args) const { - return InvokeHelper::InvokeMethod( - obj_ptr_, method_ptr_, args); - } - - private: - // The order of these members matters. Reversing the order can trigger - // warning C4121 in MSVC (see - // http://computer-programming-forum.com/7-vc.net/6fbc30265f860ad1.htm ). - const MethodPtr method_ptr_; - Class* const obj_ptr_; - - GTEST_DISALLOW_ASSIGN_(InvokeMethodAction); -}; - // An internal replacement for std::copy which mimics its behavior. This is // necessary because Visual Studio deprecates ::std::copy, issuing warning 4996. // However Visual Studio 2010 and later do not honor #pragmas which disable that @@ -109,24 +63,6 @@ inline OutputIterator CopyElements(InputIterator first, // Various overloads for Invoke(). -// Creates an action that invokes 'function_impl' with the mock -// function's arguments. -template -PolymorphicAction > Invoke( - FunctionImpl function_impl) { - return MakePolymorphicAction( - internal::InvokeAction(function_impl)); -} - -// Creates an action that invokes the given method on the given object -// with the mock function's arguments. -template -PolymorphicAction > Invoke( - Class* obj_ptr, MethodPtr method_ptr) { - return MakePolymorphicAction( - internal::InvokeMethodAction(obj_ptr, method_ptr)); -} - // The ACTION*() macros trigger warning C4100 (unreferenced formal // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in // the macro definition, as the warnings are generated when the macro diff --git a/googlemock/include/gmock/internal/gmock-internal-utils.h b/googlemock/include/gmock/internal/gmock-internal-utils.h index 661c835..093b465 100644 --- a/googlemock/include/gmock/internal/gmock-internal-utils.h +++ b/googlemock/include/gmock/internal/gmock-internal-utils.h @@ -42,6 +42,7 @@ #include #include // NOLINT #include +#include #include "gmock/internal/gmock-port.h" #include "gtest/gtest.h" diff --git a/googlemock/test/gmock-actions_test.cc b/googlemock/test/gmock-actions_test.cc index 77280f4..b3fef67 100644 --- a/googlemock/test/gmock-actions_test.cc +++ b/googlemock/test/gmock-actions_test.cc @@ -54,12 +54,14 @@ namespace { // This list should be kept sorted. +using testing::_; using testing::Action; using testing::ActionInterface; using testing::Assign; using testing::ByMove; using testing::ByRef; using testing::DefaultValue; +using testing::DoAll; using testing::DoDefault; using testing::IgnoreResult; using testing::Invoke; @@ -75,7 +77,6 @@ using testing::SetArgPointee; using testing::SetArgumentPointee; using testing::Unused; using testing::WithArgs; -using testing::_; using testing::internal::BuiltInDefaultValue; using testing::internal::Int64; using testing::internal::UInt64; diff --git a/googlemock/test/gmock-spec-builders_test.cc b/googlemock/test/gmock-spec-builders_test.cc index 557abae..10869b6 100644 --- a/googlemock/test/gmock-spec-builders_test.cc +++ b/googlemock/test/gmock-spec-builders_test.cc @@ -78,6 +78,7 @@ using testing::Expectation; using testing::ExpectationSet; using testing::GMOCK_FLAG(verbose); using testing::Gt; +using testing::IgnoreResult; using testing::InSequence; using testing::Invoke; using testing::InvokeWithoutArgs; -- cgit v0.12