C++ (Qt)#ifndef SIGNAL_SLOT_H#define SIGNAL_SLOT_H #include <list> template <class T_receiver, class T_return, class T_arg>class Slot{public: typedef T_return (T_receiver::*function_type_1)(T_arg); typedef T_return (T_receiver::*function_type_2)(const T_arg &); typedef T_return (T_receiver::*function_type_3)(T_arg) const; typedef T_return (T_receiver::*function_type_4)(const T_arg &) const; Slot(T_receiver *obj, function_type_1 slot); Slot(T_receiver *obj, function_type_2 slot); Slot(T_receiver *obj, function_type_3 slot); Slot(T_receiver *obj, function_type_4 slot); ~Slot(); T_return operator() (const T_arg &arg); private: Slot(); function_type_1 _slot_1; function_type_2 _slot_2; function_type_3 _slot_3; function_type_4 _slot_4; T_receiver *_obj; int _index;}; template <class T_receiver, class T_return, class T_arg>inline Slot<T_receiver, T_return, T_arg>::Slot(){} template <class T_receiver, class T_return, class T_arg>inline Slot<T_receiver, T_return, T_arg>::Slot(T_receiver *obj, function_type_1 slot){ _obj = obj; _slot_1 = slot; _index = 1;} template <class T_receiver, class T_return, class T_arg>inline Slot<T_receiver, T_return, T_arg>::Slot(T_receiver *obj, function_type_2 slot){ _obj = obj; _slot_2 = slot; _index = 2;} template <class T_receiver, class T_return, class T_arg>inline Slot<T_receiver, T_return, T_arg>::Slot(T_receiver *obj, function_type_3 slot){ _obj = obj; _slot_3 = slot; _index = 3;} template <class T_receiver, class T_return, class T_arg>inline Slot<T_receiver, T_return, T_arg>::Slot(T_receiver *obj, function_type_4 slot){ _obj = obj; _slot_4 = slot; _index = 4;} template <class T_receiver, class T_return, class T_arg>inline T_return Slot<T_receiver, T_return, T_arg>::operator ()(const T_arg &arg){ if (_index == 1) return (_obj->*_slot_1)(arg); if (_index == 2) return (_obj->*_slot_2)(arg); if (_index == 3) return (_obj->*_slot_3)(arg); return (_obj->*_slot_4)(arg);} template <class T_receiver, class T_return, class T_arg>class Signal{public: typedef T_return (T_receiver::*function_type_1)(T_arg); typedef T_return (T_receiver::*function_type_2)(const T_arg &); typedef T_return (T_receiver::*function_type_3)(T_arg) const; typedef T_return (T_receiver::*function_type_4)(const T_arg &) const; Signal(); ~Signal(); void operator()(const T_arg &arg); void connect(T_receiver &obj, function_type_1 slot); void connect(T_receiver &obj, function_type_2 slot); void connect(T_receiver &obj, function_type_3 slot); void connect(T_receiver &obj, function_type_4 slot); private: std::list<Slot<T_receiver, T_return, T_arg>* > _list; typedef typename std::list<Slot<T_receiver, T_return, T_arg>* >::iterator _iterator; }; template <class T_receiver, class T_return, class T_arg>inline Signal<T_receiver, T_return, T_arg>::Signal(){} template <class T_receiver, class T_return, class T_arg>inline Signal<T_receiver, T_return, T_arg>::~Signal(){ _list.clear();} template <class T_receiver, class T_return, class T_arg>inline void Signal<T_receiver, T_return, T_arg>::connect(T_receiver &obj, function_type_1 slot){ _list.push_back(new Slot<T_receiver, T_return, T_arg>(&obj, slot));} template <class T_receiver, class T_return, class T_arg>inline void Signal<T_receiver, T_return, T_arg>::connect(T_receiver &obj, function_type_2 slot){ _list.push_back(new Slot<T_receiver, T_return, T_arg>(&obj, slot));} template <class T_receiver, class T_return, class T_arg>inline void Signal<T_receiver, T_return, T_arg>::connect(T_receiver &obj, function_type_3 slot){ _list.push_back(new Slot<T_receiver, T_return, T_arg>(&obj, slot));} template <class T_receiver, class T_return, class T_arg>inline void Signal<T_receiver, T_return, T_arg>::connect(T_receiver &obj, function_type_4 slot){ _list.push_back(new Slot<T_receiver, T_return, T_arg>(&obj, slot));} template <class T_receiver, class T_return, class T_arg>inline void Signal<T_receiver, T_return, T_arg>::operator ()(const T_arg &arg){ for (_iterator it = _list.begin(); it != _list.end(); it++) (*it)->operator()(arg);} #endif // SIGNAL_SLOT_H
C++ (Qt)template <class T_receiver, class T_return, class T_arg> class Signal
C++ (Qt)#include <iostream>#include <string> #include "signal_slot.h" template <class T>class Sender{public: Sender() {} Signal<T, void, std::string> signal; void run() { signal("Hello word!"); }}; class Receiver{public: Receiver(){} void mySlot1(const std::string &str) { std::cout << "mySlot1 received a signal: " << str << std::endl; } void mySlot2(std::string str) { std::cout << "mySlot2 receiver a signal: " << str << std::endl; } }; int main(){ Sender<Receiver> sender; Receiver receiver; sender.signal.connect(receiver, &Receiver::mySlot1); sender.signal.connect(receiver, &Receiver::mySlot2); sender.run(); return 0;}
C++ (Qt)#include <iostream>#include <string>#include "signal_slot.h" class A{public: A() {} signal<void, std::string> my_signal; void run() { my_signal("Hello Word!"); }}; class B{public: B() {} void slot1(const std::string &str) { std::cout << "slot1, signal - " << str << std::endl; } void slot2(std::string str) const { std::cout << "slot2, signal - " << str << std::endl; }}; int main(){ A a; B b; connect(&b, &B::slot1, &a.my_signal); connect(&b, &B::slot2, &a.my_signal); a.run(); return 0;}
C++ (Qt)class A{public: A() {}/*Объявляем сигнал, который будет соеденён с функцией, которая возвращает void и принимает один аргумент std::string */ signal<void, std::string> my_signal; /*В той функции, где необходимо вызвать сигнал, просто вызываем сигнал как обычную функцию */ void run() { my_signal("Hello Word!"); }};
C++ (Qt)#ifndef SIGNAL_SLOT_H#define SIGNAL_SLOT_H #include <list> template <class T_return, class T_arg>class base_signal{public: base_signal() {} virtual ~base_signal() {} virtual T_return operator() (const T_arg &) = 0;}; template <class T_receiver, class T_return, class T_arg>class manager_connection; template <class T_return, class T_arg>class signal{public: signal() {} ~signal() { for (_iterator it = _list.begin(); it != _list.end(); it++) { delete *it; } _list.clear(); } T_return operator() (const T_arg &arg) { for (_iterator it = _list.begin(); it != _list.end(); it++) { (*it)->operator()(arg); } } template <class T1, class T2, class T3> friend class manager_connection; private: std::list<base_signal<T_return, T_arg>* > _list; typedef typename std::list<base_signal<T_return, T_arg>* >::iterator _iterator; void connect(base_signal<T_return, T_arg> *_signal) { _list.push_back(_signal); }}; template <class T_receiver, class T_return, class T_arg>class manager_connection : public base_signal<T_return, T_arg>{public: typedef T_return (T_receiver::*function_type_1)(T_arg); typedef T_return (T_receiver::*function_type_2)(const T_arg &); typedef T_return (T_receiver::*function_type_3)(T_arg) const; typedef T_return (T_receiver::*function_type_4)(const T_arg &) const; manager_connection(T_receiver *obj, function_type_1 slot, signal<T_return, T_arg> *s) { _receiver = obj; _slot_1 = slot; _index = 1; s->connect(this); } manager_connection(T_receiver *obj, function_type_2 slot, signal<T_return, T_arg> *s) { _receiver = obj; _slot_2 = slot; _index = 2; s->connect(this); } manager_connection(T_receiver *obj, function_type_3 slot, signal<T_return, T_arg> *s) { _receiver = obj; _slot_3 = slot; _index = 3; s->connect(this); } manager_connection(T_receiver *obj, function_type_4 slot, signal<T_return, T_arg> *s) { _receiver = obj; _slot_4 = slot; _index = 4; s->connect(this); } ~manager_connection() { } T_return operator()(const T_arg &arg) { if (_index == 1) return (_receiver->*_slot_1)(arg); if (_index == 2) return (_receiver->*_slot_2)(arg); if (_index == 3) return (_receiver->*_slot_3)(arg); if (_index == 4) return (_receiver->*_slot_4)(arg); return T_return(); } private: T_receiver *_receiver; function_type_1 _slot_1; function_type_2 _slot_2; function_type_3 _slot_3; function_type_4 _slot_4; int _index; manager_connection() : _receiver(0), _index(0) {} manager_connection(const manager_connection &) {} }; template <class T_receiver, class T_return, class T_arg>void connect(T_receiver *obj, T_return (T_receiver::*slot)(T_arg), signal<T_return, T_arg> *s) { new manager_connection<T_receiver, T_return, T_arg>(obj, slot, s);} template <class T_receiver, class T_return, class T_arg>void connect(T_receiver *obj, T_return (T_receiver::*slot)(const T_arg &), signal<T_return, T_arg> *s) { new manager_connection<T_receiver, T_return, T_arg>(obj, slot, s);} template <class T_receiver, class T_return, class T_arg>void connect(T_receiver *obj, T_return (T_receiver::*slot)(T_arg) const, signal<T_return, T_arg> *s) { new manager_connection<T_receiver, T_return, T_arg>(obj, slot, s);} template <class T_receiver, class T_return, class T_arg>void connect(T_receiver *obj, T_return (T_receiver::*slot)(const T_arg &) const, signal<T_return, T_arg> *s) { new manager_connection<T_receiver, T_return, T_arg>(obj, slot, s);} #endif // SIGNAL_SLOT_H