mirror of
https://github.com/google/googletest.git
synced 2026-09-25 04:09:59 +03:00
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
This commit is contained in:
committed by
Copybara-Service
parent
4141c384aa
commit
fa005b296f
@@ -1331,22 +1331,6 @@ struct InvokeMethodAction {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 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.
|
// Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action.
|
||||||
template <class Class, typename MethodPtr>
|
template <class Class, typename MethodPtr>
|
||||||
struct InvokeMethodWithoutArgsAction {
|
struct InvokeMethodWithoutArgsAction {
|
||||||
@@ -2070,9 +2054,11 @@ internal::InvokeMethodAction<Class, MethodPtr> Invoke(Class* obj_ptr,
|
|||||||
|
|
||||||
// Creates an action that invokes 'function_impl' with no argument.
|
// Creates an action that invokes 'function_impl' with no argument.
|
||||||
template <typename FunctionImpl>
|
template <typename FunctionImpl>
|
||||||
internal::InvokeWithoutArgsAction<typename std::decay<FunctionImpl>::type>
|
GTEST_INTERNAL_DEPRECATE_AND_INLINE(
|
||||||
InvokeWithoutArgs(FunctionImpl function_impl) {
|
"Actions can now be implicitly constructed from zero-argument callables. "
|
||||||
return {std::move(function_impl)};
|
"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
|
// Creates an action that invokes the given method on the given object
|
||||||
|
|||||||
@@ -1223,15 +1223,15 @@ class Foo {
|
|||||||
// Tests InvokeWithoutArgs(function).
|
// Tests InvokeWithoutArgs(function).
|
||||||
TEST(InvokeWithoutArgsTest, Function) {
|
TEST(InvokeWithoutArgsTest, Function) {
|
||||||
// As an action that takes one argument.
|
// 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)));
|
EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
|
||||||
|
|
||||||
// As an action that takes two arguments.
|
// 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)));
|
EXPECT_EQ(1, a2.Perform(std::make_tuple(2, 3.5)));
|
||||||
|
|
||||||
// As an action that returns void.
|
// As an action that returns void.
|
||||||
Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
|
Action<void(int)> a3 = VoidNullary; // NOLINT
|
||||||
g_done = false;
|
g_done = false;
|
||||||
a3.Perform(std::make_tuple(1));
|
a3.Perform(std::make_tuple(1));
|
||||||
EXPECT_TRUE(g_done);
|
EXPECT_TRUE(g_done);
|
||||||
|
|||||||
@@ -1026,9 +1026,8 @@ TEST(DoAllTest, NoArgs) {
|
|||||||
|
|
||||||
TEST(DoAllTest, MoveOnlyArgs) {
|
TEST(DoAllTest, MoveOnlyArgs) {
|
||||||
bool ran_first = false;
|
bool ran_first = false;
|
||||||
Action<int(std::unique_ptr<int>)> a =
|
Action<int(std::unique_ptr<int>)> a = DoAll(
|
||||||
DoAll(InvokeWithoutArgs([&] { ran_first = true; }),
|
[&] { ran_first = true; }, [](std::unique_ptr<int> p) { return *p; });
|
||||||
[](std::unique_ptr<int> p) { return *p; });
|
|
||||||
EXPECT_EQ(7, a.Perform(std::make_tuple(std::unique_ptr<int>(new int(7)))));
|
EXPECT_EQ(7, a.Perform(std::make_tuple(std::unique_ptr<int>(new int(7)))));
|
||||||
EXPECT_TRUE(ran_first);
|
EXPECT_TRUE(ran_first);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user