Support function pointers and std functions with arguments in Closure.

This commit is contained in:
John Maguire 2012-12-13 16:13:38 +01:00
parent c57c47ae08
commit 94f76a9d08
2 changed files with 56 additions and 0 deletions

View File

@ -195,6 +195,24 @@ _detail::ClosureBase* NewClosure(
const char* signal,
std::tr1::function<void()> callback);
template <typename... Args>
_detail::ClosureBase* NewClosure(
QObject* sender,
const char* signal,
std::tr1::function<void(Args...)> callback,
const Args&... args) {
return NewClosure(sender, signal, boost::bind(callback, args...));
}
template <typename... Args>
_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);

View File

@ -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<void(bool*,int,int*)> 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;