Support function pointers and std functions with arguments in Closure.
This commit is contained in:
parent
c57c47ae08
commit
94f76a9d08
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue