From 94f76a9d081cbfb9cd442915cd21ec192d6ab6ba Mon Sep 17 00:00:00 2001 From: John Maguire Date: Thu, 13 Dec 2012 16:13:38 +0100 Subject: [PATCH] Support function pointers and std functions with arguments in Closure. --- ext/libclementine-common/core/closure.h | 18 ++++++++++++ tests/closure_test.cpp | 38 +++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/ext/libclementine-common/core/closure.h b/ext/libclementine-common/core/closure.h index 55b31ca9f..f7582c7dd 100644 --- a/ext/libclementine-common/core/closure.h +++ b/ext/libclementine-common/core/closure.h @@ -195,6 +195,24 @@ _detail::ClosureBase* NewClosure( const char* signal, std::tr1::function callback); +template +_detail::ClosureBase* NewClosure( + QObject* sender, + const char* signal, + std::tr1::function callback, + const Args&... args) { + return NewClosure(sender, signal, boost::bind(callback, args...)); +} + +template +_detail::ClosureBase* NewClosure( + QObject* sender, + const char* signal, + void (*callback)(Args...), + const Args&... args) { + return NewClosure(sender, signal, boost::bind(callback, args...)); +} + void DoAfter(QObject* receiver, const char* slot, int msec); void DoInAMinuteOrSo(QObject* receiver, const char* slot); diff --git a/tests/closure_test.cpp b/tests/closure_test.cpp index 4a76dc0e2..1c8bbddd8 100644 --- a/tests/closure_test.cpp +++ b/tests/closure_test.cpp @@ -64,6 +64,44 @@ TEST(ClosureTest, ClosureDoesNotCrashWithSharedPointerSender) { EXPECT_TRUE(closure.isNull()); } +namespace { + +void Foo(bool* called, int question, int* answer) { + *called = true; + *answer = question; +} + +} // namespace + +TEST(ClosureTest, ClosureWorksWithFunctionPointers) { + TestQObject sender; + bool called = false; + int question = 42; + int answer = 0; + _detail::ClosureBase* closure = NewClosure( + &sender, SIGNAL(Emitted()), + &Foo, &called, question, &answer); + EXPECT_FALSE(called); + sender.Emit(); + EXPECT_TRUE(called); + EXPECT_EQ(question, answer); +} + +TEST(ClosureTest, ClosureWorksWithStandardFunctions) { + TestQObject sender; + bool called = false; + int question = 42; + int answer = 0; + std::tr1::function callback(&Foo); + _detail::ClosureBase* closure = NewClosure( + &sender, SIGNAL(Emitted()), + callback, &called, question, &answer); + EXPECT_FALSE(called); + sender.Emit(); + EXPECT_TRUE(called); + EXPECT_EQ(question, answer); +} + #ifdef HAVE_LAMBDAS TEST(ClosureTest, ClosureCallsLambda) { TestQObject sender;