Support member function pointers in Closure.
Bonus: type-safe slots
This commit is contained in:
parent
94f76a9d08
commit
020f08438f
|
@ -213,6 +213,15 @@ _detail::ClosureBase* NewClosure(
|
||||||
return NewClosure(sender, signal, boost::bind(callback, args...));
|
return NewClosure(sender, signal, boost::bind(callback, args...));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T, typename... Args>
|
||||||
|
_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 DoAfter(QObject* receiver, const char* slot, int msec);
|
||||||
void DoInAMinuteOrSo(QObject* receiver, const char* slot);
|
void DoInAMinuteOrSo(QObject* receiver, const char* slot);
|
||||||
|
|
|
@ -44,8 +44,7 @@ void OAuthenticator::StartAuthorisation(
|
||||||
url.addQueryItem("scope", scope);
|
url.addQueryItem("scope", scope);
|
||||||
|
|
||||||
NewClosure(server, SIGNAL(Finished()),
|
NewClosure(server, SIGNAL(Finished()),
|
||||||
this, SLOT(RedirectArrived(LocalRedirectServer*,QUrl)),
|
this, &OAuthenticator::RedirectArrived, server, redirect_url);
|
||||||
server, redirect_url);
|
|
||||||
|
|
||||||
QDesktopServices::openUrl(url);
|
QDesktopServices::openUrl(url);
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,7 +78,7 @@ TEST(ClosureTest, ClosureWorksWithFunctionPointers) {
|
||||||
bool called = false;
|
bool called = false;
|
||||||
int question = 42;
|
int question = 42;
|
||||||
int answer = 0;
|
int answer = 0;
|
||||||
_detail::ClosureBase* closure = NewClosure(
|
NewClosure(
|
||||||
&sender, SIGNAL(Emitted()),
|
&sender, SIGNAL(Emitted()),
|
||||||
&Foo, &called, question, &answer);
|
&Foo, &called, question, &answer);
|
||||||
EXPECT_FALSE(called);
|
EXPECT_FALSE(called);
|
||||||
|
@ -93,7 +93,7 @@ TEST(ClosureTest, ClosureWorksWithStandardFunctions) {
|
||||||
int question = 42;
|
int question = 42;
|
||||||
int answer = 0;
|
int answer = 0;
|
||||||
std::tr1::function<void(bool*,int,int*)> callback(&Foo);
|
std::tr1::function<void(bool*,int,int*)> callback(&Foo);
|
||||||
_detail::ClosureBase* closure = NewClosure(
|
NewClosure(
|
||||||
&sender, SIGNAL(Emitted()),
|
&sender, SIGNAL(Emitted()),
|
||||||
callback, &called, question, &answer);
|
callback, &called, question, &answer);
|
||||||
EXPECT_FALSE(called);
|
EXPECT_FALSE(called);
|
||||||
|
@ -102,11 +102,38 @@ TEST(ClosureTest, ClosureWorksWithStandardFunctions) {
|
||||||
EXPECT_EQ(question, answer);
|
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
|
#ifdef HAVE_LAMBDAS
|
||||||
TEST(ClosureTest, ClosureCallsLambda) {
|
TEST(ClosureTest, ClosureCallsLambda) {
|
||||||
TestQObject sender;
|
TestQObject sender;
|
||||||
bool called = false;
|
bool called = false;
|
||||||
_detail::ClosureBase* closure = NewClosure(
|
NewClosure(
|
||||||
&sender, SIGNAL(Emitted()),
|
&sender, SIGNAL(Emitted()),
|
||||||
[&called] () { called = true; });
|
[&called] () { called = true; });
|
||||||
EXPECT_FALSE(called);
|
EXPECT_FALSE(called);
|
||||||
|
|
Loading…
Reference in New Issue