Set up inlining for InvokeWithoutArgs when passed a functor. Actions can now be implicitly constructed from zero-argument callables. There is no need to create wrapper objects using InvokeWithoutArgs(). PiperOrigin-RevId: 949656497 Change-Id: Ida2bc38e446e7b33aaf185ec593caf2a1959138e
diff --git a/googlemock/include/gmock/gmock-actions.h b/googlemock/include/gmock/gmock-actions.h index 94552ce..687da5c 100644 --- a/googlemock/include/gmock/gmock-actions.h +++ b/googlemock/include/gmock/gmock-actions.h
@@ -1331,22 +1331,6 @@ } }; -// 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<F> as long as f's type is compatible with F. -template <typename FunctionImpl> -struct InvokeWithoutArgsAction { - FunctionImpl function_impl; - - // Allows InvokeWithoutArgs(f) to be used as any action whose type is - // compatible with f. - template <typename... Args> - auto operator()(const Args&...) -> decltype(function_impl()) { - return function_impl(); - } -}; - // Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action. template <class Class, typename MethodPtr> struct InvokeMethodWithoutArgsAction { @@ -2070,9 +2054,11 @@ // Creates an action that invokes 'function_impl' with no argument. template <typename FunctionImpl> -internal::InvokeWithoutArgsAction<typename std::decay<FunctionImpl>::type> -InvokeWithoutArgs(FunctionImpl function_impl) { - return {std::move(function_impl)}; +GTEST_INTERNAL_DEPRECATE_AND_INLINE( + "Actions can now be implicitly constructed from zero-argument callables. " + "No need to create wrapper objects using InvokeWithoutArgs().") +std::decay_t<FunctionImpl> InvokeWithoutArgs(FunctionImpl&& function_impl) { + return std::forward<FunctionImpl>(function_impl); } // Creates an action that invokes the given method on the given object
diff --git a/googlemock/test/gmock-actions_test.cc b/googlemock/test/gmock-actions_test.cc index 507db6b..0a03349 100644 --- a/googlemock/test/gmock-actions_test.cc +++ b/googlemock/test/gmock-actions_test.cc
@@ -1223,15 +1223,15 @@ // Tests InvokeWithoutArgs(function). TEST(InvokeWithoutArgsTest, Function) { // As an action that takes one argument. - Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT + Action<int(int)> a = Nullary; // NOLINT EXPECT_EQ(1, a.Perform(std::make_tuple(2))); // As an action that takes two arguments. - Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT + Action<int(int, double)> a2 = Nullary; // NOLINT EXPECT_EQ(1, a2.Perform(std::make_tuple(2, 3.5))); // As an action that returns void. - Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT + Action<void(int)> a3 = VoidNullary; // NOLINT g_done = false; a3.Perform(std::make_tuple(1)); EXPECT_TRUE(g_done);
diff --git a/googlemock/test/gmock-more-actions_test.cc b/googlemock/test/gmock-more-actions_test.cc index 7c51b78..efe2a52 100644 --- a/googlemock/test/gmock-more-actions_test.cc +++ b/googlemock/test/gmock-more-actions_test.cc
@@ -1026,9 +1026,8 @@ TEST(DoAllTest, MoveOnlyArgs) { bool ran_first = false; - Action<int(std::unique_ptr<int>)> a = - DoAll(InvokeWithoutArgs([&] { ran_first = true; }), - [](std::unique_ptr<int> p) { return *p; }); + Action<int(std::unique_ptr<int>)> a = DoAll( + [&] { ran_first = true; }, [](std::unique_ptr<int> p) { return *p; }); EXPECT_EQ(7, a.Perform(std::make_tuple(std::unique_ptr<int>(new int(7))))); EXPECT_TRUE(ran_first); }