diff --git a/ext/libclementine-common/core/closure.h b/ext/libclementine-common/core/closure.h index f7582c7dd..ed7ab49d6 100644 --- a/ext/libclementine-common/core/closure.h +++ b/ext/libclementine-common/core/closure.h @@ -213,6 +213,15 @@ _detail::ClosureBase* NewClosure( return NewClosure(sender, signal, boost::bind(callback, args...)); } +template +_detail::ClosureBase* NewClosure( + QObject* sender, + const char* signal, + T* receiver, void (T::*callback)(Args...), + const Args&... args) { + return NewClosure(sender, signal, boost::bind(callback, receiver, args...)); +} + void DoAfter(QObject* receiver, const char* slot, int msec); void DoInAMinuteOrSo(QObject* receiver, const char* slot); diff --git a/src/internet/oauthenticator.cpp b/src/internet/oauthenticator.cpp index f8cb4a3bd..42b161de6 100644 --- a/src/internet/oauthenticator.cpp +++ b/src/internet/oauthenticator.cpp @@ -44,8 +44,7 @@ void OAuthenticator::StartAuthorisation( url.addQueryItem("scope", scope); NewClosure(server, SIGNAL(Finished()), - this, SLOT(RedirectArrived(LocalRedirectServer*,QUrl)), - server, redirect_url); + this, &OAuthenticator::RedirectArrived, server, redirect_url); QDesktopServices::openUrl(url); } diff --git a/tests/closure_test.cpp b/tests/closure_test.cpp index 1c8bbddd8..2eeb48059 100644 --- a/tests/closure_test.cpp +++ b/tests/closure_test.cpp @@ -78,7 +78,7 @@ TEST(ClosureTest, ClosureWorksWithFunctionPointers) { bool called = false; int question = 42; int answer = 0; - _detail::ClosureBase* closure = NewClosure( + NewClosure( &sender, SIGNAL(Emitted()), &Foo, &called, question, &answer); EXPECT_FALSE(called); @@ -93,7 +93,7 @@ TEST(ClosureTest, ClosureWorksWithStandardFunctions) { int question = 42; int answer = 0; std::tr1::function callback(&Foo); - _detail::ClosureBase* closure = NewClosure( + NewClosure( &sender, SIGNAL(Emitted()), callback, &called, question, &answer); EXPECT_FALSE(called); @@ -102,11 +102,38 @@ TEST(ClosureTest, ClosureWorksWithStandardFunctions) { EXPECT_EQ(question, answer); } +namespace { + +class Bar { + public: + explicit Bar(int a) : foo_(a) {} + void Foo(int* answer) { + *answer = foo_; + } + + private: + int foo_; +}; + +} + +TEST(ClosureTest, ClosureWorksWithMemberFunctionPointers) { + TestQObject sender; + Bar receiver(42); + int q = 1; + NewClosure( + &sender, SIGNAL(Emitted()), + &receiver, &Bar::Foo, &q); + EXPECT_EQ(1, q); + sender.Emit(); + EXPECT_EQ(42, q); +} + #ifdef HAVE_LAMBDAS TEST(ClosureTest, ClosureCallsLambda) { TestQObject sender; bool called = false; - _detail::ClosureBase* closure = NewClosure( + NewClosure( &sender, SIGNAL(Emitted()), [&called] () { called = true; }); EXPECT_FALSE(called);