diff --git a/src/Core/Object.h b/src/Core/Object.h index 4f4a278..3c8865a 100644 --- a/src/Core/Object.h +++ b/src/Core/Object.h @@ -116,16 +116,15 @@ public: connect(typename FunctionPointer::Object *sender, Func1 sigf, typename FunctionPointer::Object *receiver, Func2 slof) { SignalBase *sigb = sender->findOrAddSignal(sigf); - typedef boost::signals2::signal< - typename FunctionPointer::SignalSignature> - SigT; - ConnectSignal(sigb, slof, receiver); + ConnectSignal::SignalSignature>(sigb, slof, + receiver); return true; } template static inline bool connect(SignalBase *sigb, FuncT slof, Object *receiver) { - ConnectSignal(sigb, slof, receiver); + ConnectSignal::SignalSignature>(sigb, slof, + receiver); return true; } diff --git a/src/Core/Signal.h b/src/Core/Signal.h index ee0ad99..79dbad5 100644 --- a/src/Core/Signal.h +++ b/src/Core/Signal.h @@ -50,8 +50,11 @@ using namespace boost::placeholders; #define SIGNAL(a) BOOST_STRINGIZE(a) #define _ULIB_DETAIL_SIGNAL_EMIT(_name, ...) \ - static BOOST_AUTO(sig, this->findOrAddSignal(&_name)); \ - sig->operator()(__VA_ARGS__); + do { \ + BOOST_AUTO(sig, this->findOrAddSignal(&_name)); \ + if (sig) \ + sig->operator()(__VA_ARGS__); \ + } while (0) /** * Utility macro to implement signal emission implementa una delle seguenti: @@ -84,66 +87,61 @@ template struct Signal { namespace detail { -template struct ConnectSignal {}; +template +struct ConnectSignal {}; -template struct ConnectSignal { +template +struct ConnectSignal { static void connect(SignalBase *sigb, FuncT slof, typename FunctionPointer::Object *receiver) { - typedef - typename Signal::SignalSignature>::type - SigT; + typedef typename Signal::type SigT; reinterpret_cast(sigb)->connect(slof); } }; -template struct ConnectSignal { +template +struct ConnectSignal { static void connect(SignalBase *sigb, FuncT slof, typename FunctionPointer::Object *receiver) { - typedef - typename Signal::SignalSignature>::type - SigT; + typedef typename Signal::type SigT; reinterpret_cast(sigb)->connect(boost::bind(slof, receiver)); } }; -template struct ConnectSignal { +template +struct ConnectSignal { static void connect(SignalBase *sigb, FuncT slof, typename FunctionPointer::Object *receiver) { - typedef - typename Signal::SignalSignature>::type - SigT; + typedef typename Signal::type SigT; reinterpret_cast(sigb)->connect(boost::bind(slof, receiver, _1)); } }; -template struct ConnectSignal { +template +struct ConnectSignal { static void connect(SignalBase *sigb, FuncT slof, typename FunctionPointer::Object *receiver) { - typedef - typename Signal::SignalSignature>::type - SigT; + typedef typename Signal::type SigT; reinterpret_cast(sigb)->connect( boost::bind(slof, receiver, _1, _2)); } }; -template struct ConnectSignal { +template +struct ConnectSignal { static void connect(SignalBase *sigb, FuncT slof, typename FunctionPointer::Object *receiver) { - typedef - typename Signal::SignalSignature>::type - SigT; + typedef typename Signal::type SigT; reinterpret_cast(sigb)->connect( boost::bind(slof, receiver, _1, _2, _3)); } }; -template struct ConnectSignal { +template +struct ConnectSignal { static void connect(SignalBase *sigb, FuncT slof, typename FunctionPointer::Object *receiver) { - typedef - typename Signal::SignalSignature>::type - SigT; + typedef typename Signal::type SigT; reinterpret_cast(sigb)->connect( boost::bind(slof, receiver, _1, _2, _3, _4)); } @@ -152,15 +150,16 @@ template struct ConnectSignal { } // namespace detail template SignalBase *NewSignal(FuncT f) { - // seems to work wow ! - return new Signal::type; + return new + typename Signal::SignalSignature>::type; } -template +template void ConnectSignal(SignalBase *sigb, FuncT slof, typename FunctionPointer::Object *receiver) { - detail::ConnectSignal::arity>::connect( - sigb, slof, receiver); + detail::ConnectSignal::arity>::connect(sigb, slof, + receiver); } } // namespace uLib diff --git a/src/Core/testing/CMakeLists.txt b/src/Core/testing/CMakeLists.txt index c045b98..a7fc3a7 100644 --- a/src/Core/testing/CMakeLists.txt +++ b/src/Core/testing/CMakeLists.txt @@ -19,6 +19,7 @@ set( TESTS UuidTest TypeIntrospectionTraversal OptionsTest + PingPongTest ) set(LIBRARIES diff --git a/src/Core/testing/PingPongTest.cpp b/src/Core/testing/PingPongTest.cpp new file mode 100644 index 0000000..9039cc7 --- /dev/null +++ b/src/Core/testing/PingPongTest.cpp @@ -0,0 +1,52 @@ +#include "Core/Object.h" +#include "Core/Signal.h" +#include "testing-prototype.h" +#include + +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; +} diff --git a/src/Core/testing/SignalTest.cpp b/src/Core/testing/SignalTest.cpp index 491af54..26cde24 100644 --- a/src/Core/testing/SignalTest.cpp +++ b/src/Core/testing/SignalTest.cpp @@ -23,93 +23,63 @@ //////////////////////////////////////////////////////////////////////////////*/ - - #include #include - -#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; } - -