feat: fix signaling and implement a ping-pong signal/slot test
This commit is contained in:
@@ -19,6 +19,7 @@ set( TESTS
|
||||
UuidTest
|
||||
TypeIntrospectionTraversal
|
||||
OptionsTest
|
||||
PingPongTest
|
||||
)
|
||||
|
||||
set(LIBRARIES
|
||||
|
||||
52
src/Core/testing/PingPongTest.cpp
Normal file
52
src/Core/testing/PingPongTest.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "Core/Object.h"
|
||||
#include "Core/Signal.h"
|
||||
#include "testing-prototype.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace uLib;
|
||||
|
||||
class Ping : public Object {
|
||||
public:
|
||||
signals:
|
||||
void PingSignal(int count);
|
||||
public slots:
|
||||
void OnPong(int count) {
|
||||
std::cout << "Ping received Pong " << count << std::endl;
|
||||
if (count > 0)
|
||||
ULIB_SIGNAL_EMIT(Ping::PingSignal, count - 1);
|
||||
}
|
||||
};
|
||||
|
||||
void Ping::PingSignal(int count) { ULIB_SIGNAL_EMIT(Ping::PingSignal, count); }
|
||||
|
||||
class Pong : public Object {
|
||||
public:
|
||||
signals:
|
||||
void PongSignal(int count);
|
||||
public slots:
|
||||
void OnPing(int count) {
|
||||
std::cout << "Pong received Ping " << count << std::endl;
|
||||
if (count > 0)
|
||||
ULIB_SIGNAL_EMIT(Pong::PongSignal, count - 1);
|
||||
}
|
||||
};
|
||||
|
||||
void Pong::PongSignal(int count) { ULIB_SIGNAL_EMIT(Pong::PongSignal, count); }
|
||||
|
||||
int main() {
|
||||
BEGIN_TESTING(PingPong);
|
||||
Ping ping;
|
||||
Pong pong;
|
||||
|
||||
std::cout << "Connecting ping to pong" << std::endl;
|
||||
Object::connect(&ping, &Ping::PingSignal, &pong, &Pong::OnPing);
|
||||
|
||||
std::cout << "Connecting pong to ping" << std::endl;
|
||||
Object::connect(&pong, &Pong::PongSignal, &ping, &Ping::OnPong);
|
||||
|
||||
std::cout << "Emitting PingSignal(5)" << std::endl;
|
||||
ping.PingSignal(5);
|
||||
|
||||
END_TESTING;
|
||||
return 0;
|
||||
}
|
||||
@@ -23,93 +23,63 @@
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <typeinfo>
|
||||
|
||||
|
||||
#include "testing-prototype.h"
|
||||
#include "Core/Types.h"
|
||||
#include "Core/Object.h"
|
||||
#include "Core/Signal.h"
|
||||
|
||||
#include "Core/Types.h"
|
||||
#include "testing-prototype.h"
|
||||
|
||||
using namespace uLib;
|
||||
|
||||
|
||||
|
||||
|
||||
class Ob1 : public Object {
|
||||
public:
|
||||
|
||||
|
||||
signals:
|
||||
void V0();
|
||||
|
||||
int V1(int a);
|
||||
void V0();
|
||||
|
||||
void V1(int a);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
// should be done by moc //
|
||||
void Ob1::V0() {
|
||||
ULIB_SIGNAL_EMIT(Ob1::V0);
|
||||
}
|
||||
|
||||
int Ob1::V1(int a) {
|
||||
ULIB_SIGNAL_EMIT(Ob1::V1,a);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Ob1::V0() { ULIB_SIGNAL_EMIT(Ob1::V0); }
|
||||
|
||||
void Ob1::V1(int a) { ULIB_SIGNAL_EMIT(Ob1::V1, a); }
|
||||
|
||||
class Ob2 : public Object {
|
||||
|
||||
public slots:
|
||||
void PrintV0() {
|
||||
std::cout << "Ob2 prints V0\n" << std::flush;
|
||||
}
|
||||
void PrintV0() { std::cout << "Ob2 prints V0\n" << std::flush; }
|
||||
};
|
||||
|
||||
class Ob3 : public Object {
|
||||
|
||||
public slots:
|
||||
void PrintV0() {
|
||||
std::cout << "Ob3 prints V0\n" << std::flush;
|
||||
}
|
||||
void PrintV0() { std::cout << "Ob3 prints V0\n" << std::flush; }
|
||||
|
||||
void PrintNumber(int n) {
|
||||
std::cout << "Ob3 is printing number: " << n << "\n";
|
||||
}
|
||||
void PrintNumber(int n) {
|
||||
std::cout << "Ob3 is printing number: " << n << "\n";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
BEGIN_TESTING(Signals);
|
||||
BEGIN_TESTING(Signals);
|
||||
|
||||
Ob1 ob1;
|
||||
Ob2 ob2;
|
||||
Ob3 ob3;
|
||||
Ob1 ob1;
|
||||
Ob2 ob2;
|
||||
Ob3 ob3;
|
||||
|
||||
Object::connect(&ob1,&Ob1::V0,&ob2,&Ob2::PrintV0);
|
||||
Object::connect(&ob1,&Ob1::V0,&ob3,&Ob3::PrintV0);
|
||||
Object::connect(&ob1,&Ob1::V1,&ob3,&Ob3::PrintNumber);
|
||||
Object::connect(&ob1, &Ob1::V0, &ob2, &Ob2::PrintV0);
|
||||
Object::connect(&ob1, &Ob1::V0, &ob3, &Ob3::PrintV0);
|
||||
Object::connect(&ob1, &Ob1::V1, &ob3, &Ob3::PrintNumber);
|
||||
|
||||
// not working yet
|
||||
// Object::connect(&ob1,SIGNAL(V0(),&ob2,SLOT(PrintV0())
|
||||
// not working yet
|
||||
// Object::connect(&ob1,SIGNAL(V0(),&ob2,SLOT(PrintV0())
|
||||
|
||||
ob1.PrintSelf(std::cout);
|
||||
ob1.PrintSelf(std::cout);
|
||||
|
||||
emit ob1.V0();
|
||||
emit ob1.V1(5552368);
|
||||
emit ob1.V0();
|
||||
emit ob1.V1(5552368);
|
||||
|
||||
END_TESTING;
|
||||
END_TESTING;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user