Zaslal: signal slot v C++11
Popis: signal slot v C++11
Jazyk: C++
Vloženo: 30.11.2011, 20:43
Stáhnout jako soubor
#include <iostream> #include <memory> #include <map> using namespace std; template<typename ... P> class FunctionCall { public: virtual void call(P...par)=0; }; template<typename T, typename ... P> class FunctionCallT : public FunctionCall<P...> { T *o; void (T::*f)(P...); public: FunctionCallT(T &obj, void (T::*fun)(P...)) { o = &obj; f = fun; } void call(P...par) { (o->*f)(par...); } }; template <typename...P> class Signal { map<void *,unique_ptr<FunctionCall<P...>>> cal; public: template<typename T> void connect(T &o, void (T::*f)(P...)) { cal[&o] = unique_ptr<FunctionCallT<T, P...>>(new FunctionCallT<T, P...>(o, f)); } void disconnect(void *o) { cal.erase(o); } void operator()(P...par) { for(auto &i : cal) i.second->call(par...); } }; class Slot { public: int c; void funkcia(int a, float b) { cout << "funkcia " << c << " " << a << " " << b << endl; } void em() { cout << "null " << c << endl; } }; int main() { Slot slot1,slot2; slot1.c = 1; slot2.c = 2; Signal<int, float> signal1; signal1.connect(slot1, &Slot::funkcia); signal1.connect(slot2, &Slot::funkcia); Signal<> signal2; signal2.connect(slot1, &Slot::em); signal1(3, 4.0f); signal2(); return 0; }
© 2006 Michal Tuláček, Syntax Highlight - GeSHi (thx bref)