class IDoStuff{public: virtual void doSometning() = 0;};class CDoStuff: public QObject, piblic IDoStuff{ Q_OBJECTpublic: virtual void doSometning() { // some stuff emitSomeSignal(); // some another stuff }Q_SIGNALS: void someSignal();protected: void emitSomeSignal() { emit someSignal(); }};
class IDoStuff{public: virtual void doSometning() = 0;};...
C++ (Qt)class Demo{public: enum { visibleChanged, enabledChanged, positionChanged, sizeChanged }; template <size_t SignalID, class Slot> void connect(Slot slot) { std::get<SignalID>(signal_map).connect(slot); } template <size_t SignalID, class Slot> void disconnect(Slot slot) { std::get<SignalID>(signal_map).disconnect(slot); } template <size_t SignalID, class ...Args> void emit(Args ...args) { std::get<SignalID>(signal_map)(args...); } protected: typedef std::tuple< signal<void(bool)>, signal<void(bool)>, signal<void(int, int)>, signal<void(int, int)>> signal_map_t; signal_map_t signal_map;}; void func(bool){ std::cout << "ok" << std::endl;} int main(){ Demo demo; demo.connect<Demo::visibleChanged>(&func); demo.emit<Demo::visibleChanged>(true); retrun 0;}
C++ (Qt)class Demo{public:#define SIGNALS \ X(signal<void(bool)>, visibleChanged), \ X(signal<void(bool)>, enabledChanged), \ X(signal<void(int, int)>, positionChanged), \ X(signal<void(int, int)>, sizeChanged) #define X(a,b) b enum { SIGNALS };#undef X template <size_t SignalID, class Slot> void connect(Slot slot) { std::get<SignalID>(signal_map).connect(slot); } template <size_t SignalID, class Slot> void disconnect(Slot slot) { std::get<SignalID>(signal_map).disconnect(slot); } template <size_t SignalID, class ...Args> void emit(Args ...args) { std::get<SignalID>(signal_map)(args...); } protected:#define X(a,b) a typedef std::tuple<SIGNALS> signal_map_t;#undef X signal_map_t signal_map;};
C++ (Qt)class Demo{public: enum { visibleChanged, enabledChanged, positionChanged, sizeChanged }; void test() { emit<visibleChanged>(true); } protected: template <size_t SignalID, class ...Args> void emit(Args ...args) { std::get<SignalID>(signal_map)(args...); } typedef std::tuple< signals2::signal<void(bool)>, signals2::signal<void(bool)>, signals2::signal<void(int, int)>, signals2::signal<void(int, int)> > signal_map_t; signal_map_t signal_map;}; template < class Sender >struct Connector : public Sender{ template <size_t SignalID, class Slot> void connect(Slot slot) { std::get<SignalID>(this->signal_map).connect(slot); std::cout << "signal " << SignalID << " connected" << std::endl; }}; template < class Sender >struct Disconnector : public Sender{ template <size_t SignalID, class Slot> void disconnect(Slot slot) { std::get<SignalID>(this->signal_map).disconnect(slot); std::cout << "signal " << SignalID << " disconnected" << std::endl; }}; template < size_t SignalID, class Sender, class Slot >void connect(Sender &sender, Slot slot){ typedef Connector<Sender> Connector; Connector &connector = static_cast<Connector &>(sender); connector.connect<SignalID>(slot);} template < size_t SignalID, class Sender, class Slot >void disconnect(Sender &sender, Slot slot){ typedef Disconnector<Sender> Disconnector; Disconnector &disconnector = static_cast<Disconnector &>(sender); disconnector.disconnect<SignalID>(slot);} void func(bool value){ std::cout << "func " << value << std::endl;} int main(){ Demo demo; connect<Demo::visibleChanged>(demo, &func); demo.test(); disconnect<Demo::visibleChanged>(demo, &func); demo.test(); return 0;}
C++ (Qt)struct Derived : public Demo{}; int main(){ Derived derived; derived.connect<Demo::visibleChanged>(&func); // or derived.connect<Derived::visibleChanged>(&func); derived.emit<Derived::visibleChanged>(true); }