diff options
Diffstat (limited to 'plugingui/signal.cc')
-rw-r--r-- | plugingui/signal.cc | 478 |
1 files changed, 478 insertions, 0 deletions
diff --git a/plugingui/signal.cc b/plugingui/signal.cc new file mode 100644 index 0000000..5ba145e --- /dev/null +++ b/plugingui/signal.cc @@ -0,0 +1,478 @@ +#include <iostream> +#include <functional> +#include <vector> +#include <map> +#include <set> +#include <type_traits> +#include <utility> + +///////////////////////////////////////// +// +// Listener and Notifier classes: +// +///////////////////////////////////////// + +class Listener; +class NotifierBase { +public: + virtual void disconnect(Listener* object) {} +}; + +class Listener { +public: + virtual ~Listener() + { + for (auto signal = signals.begin(); signal != signals.end(); ++signal) { + (*signal)->disconnect(this); + } + } + + void registerNotifier(NotifierBase* signal) + { + signals.insert(signal); + } + + void unregisterNotifier(NotifierBase* signal) + { + signals.erase(signal); + } + + std::set<NotifierBase*> signals; +}; + +template<typename T, typename... Args> +class Notifier : public NotifierBase { +public: + Notifier() {} + ~Notifier() + { + for (auto slot = slots.begin(); slot != slots.end(); ++slot) { + (*slot).first->unregisterNotifier(this); + } + } + + void connect(Listener* object, std::function<void(T, Args...)> slot) + { + slots[object] = slot; + if(object) { + object->registerNotifier(this); + } + } + + void disconnect(Listener* object) + { + slots.erase(object); + } + + void notify(T t, Args...args) + { + for (auto slot = slots.begin(); slot != slots.end(); ++slot) { + (*slot).second(t, args...); + } + } + + std::map<Listener*, std::function<void(T, Args...)>> slots; +}; + +///////////////////////////////////////// +// +// Code which I don't understand but does something clever and produces a compile warning: +// +///////////////////////////////////////// + +template<unsigned... Is> struct seq{}; +template<unsigned I, unsigned... Is> +struct gen_seq : gen_seq<I-1, I-1, Is...>{}; +template<unsigned... Is> +struct gen_seq<0, Is...> : seq<Is...>{}; + +template<unsigned I> struct placeholder{}; + +namespace std{ +template<unsigned I> +struct is_placeholder< ::placeholder<I> > : integral_constant<int, I>{}; +} // std:: + +namespace aux{ +template<unsigned... Is, class F, class... Ts> +auto easy_bind(seq<Is...>, F&& f, Ts&&... vs) + -> decltype(std::bind(std::forward<F>(f), std::forward<Ts>(vs)..., ::placeholder<1 + Is>()...)) +{ + return std::bind(std::forward<F>(f), std::forward<Ts>(vs)..., ::placeholder<1 + Is>()...); +} +} // aux:: + +template<class R, class C, class... FArgs, class... Args> +auto mem_bind(R (C::*ptmf)(FArgs...), Args&&... vs) + -> decltype(aux::easy_bind(gen_seq<(sizeof...(FArgs) + 1) - sizeof...(Args)>(), ptmf, std::forward<Args>(vs)...)) +{ + // the +1s for 'this' argument + static_assert(sizeof...(Args) <= sizeof...(FArgs) + 1, "too many arguments to mem_bind"); + return aux::easy_bind(gen_seq<(sizeof...(FArgs) + 1) - sizeof...(Args)>(), ptmf, std::forward<Args>(vs)...); +} + +template<class T, class C, class... Args> +auto mem_bind(T C::*ptmd, Args&&... vs) + -> decltype(aux::easy_bind(gen_seq<1 - sizeof...(Args)>(), ptmd, std::forward<Args>(vs)...)) +{ + // just 'this' argument + static_assert(sizeof...(Args) <= 1, "too many arguments to mem_bind"); + return aux::easy_bind(gen_seq<1 - sizeof...(Args)>(), ptmd, std::forward<Args>(vs)...); +} + + + +///////////////////////////////////////// +// +// Example: +// +///////////////////////////////////////// + +class MyObject : public Listener { +public: + void onMouseMoved(int x, int y) + { + std::cout << __PRETTY_FUNCTION__ << std::endl; + std::cout << "Mouse moved: " << x << "," << y << std::endl; + } +}; + +class MyObject2 : public Listener { +public: + void mouseMoved(int x, int y) + { + std::cout << __PRETTY_FUNCTION__ << std::endl; + std::cout << "Mouse moved: " << x << "," << y << std::endl; + } +}; + +class Button { +public: + Notifier<int, int> mouseMove; // arguments: (int x, int y) + + void emulateMouseMove(int x, int y) + { + mouseMove.notify(x, y); + } +}; + + +#define CONN(O, M) &O, mem_bind(&decltype(O)::M, O) +#define obj_connect(SRC, SIG, TAR, SLO) SRC.SIG.connect(&TAR, mem_bind(&decltype(TAR)::SLO, TAR)) +#define fun_connect(SRC, SIG, SLO) SRC.SIG.connect(nullptr, SLO) + +int main() +{ + Button btn; + + MyObject object; + //btn.mouseMove.connect(&object, std::bind(&MyObject::onMouseMoved, std::ref(object), _1, _2 )); // 'Vanilla' interface + //btn.mouseMove.connect(&object, std::bind(&decltype::onMouseMoved, std::ref(object), _1, _2 )); // Use decltype + //btn.mouseMove.connect(&object, mem_bind(&decltype(object)::onMouseMoved, object)); // Use clever mem_bind construct + btn.mouseMove.connect(CONN(object, onMouseMoved)); // Use convenience macro + + obj_connect(btn, mouseMove, object, onMouseMoved); + fun_connect(btn, mouseMove, [](int x, int y) { + std::cout << __PRETTY_FUNCTION__ << std::endl; + std::cout << x << " x " << y << std::endl; + } ); + + MyObject2 object2; + btn.mouseMove.connect(CONN(object2, mouseMoved)); + + MyObject2* pobject2 = new MyObject2(); + btn.mouseMove.connect(pobject2, mem_bind(&decltype(std::remove_reference(*pobject2))::mouseMoved, std::ref(*pobject2))); + + + // Now trigger the notification + btn.emulateMouseMove(10,10); +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#if 0 + +#include <iostream> +#include <functional> +#include <vector> +#include <map> +#include <set> +#include <stdio.h> + +template<typename T> +class SignalBase { +public: + virtual T emit(T a) { return a; } + T slot(SignalBase* o, T a) { return o->emit(a); } +}; + +template<typename T> +class Signal : public SignalBase<T> { +public: + typedef SignalBase<T>* signal_ptr_t; + + static T do_emit(signal_ptr_t o1, signal_ptr_t o2, T arg) + { + std::cout << __PRETTY_FUNCTION__ << "\n"; + // printf("%p %p\n", (void*)o1, (void*)o2); + return o1->slot(o2, arg); + } + + using bound_emit_t = + decltype(std::bind(do_emit, signal_ptr_t(), signal_ptr_t(), std::placeholders::_1)); + + using mem_t = decltype() + + + void connect(Signal<T>& o) + { + connection_map[&o].emplace_back(do_emit, this, &o, std::placeholders::_1); + + if (o.connectee_objects.find(this) == o.connectee_objects.end()) { + o.connectee_objects.insert(this); + } + } + + void disconnect(Signal<T>& o) + { + if (connection_map.find(&o) != connection_map.end()) { + connection_map[&o].clear(); + connection_map.erase(&o); + } + + if (o.connectee_objects.find(this) != o.connectee_objects.end()) { + connectee_objects.erase(this); + } + } + + void send(T a) + { + std::cout << "send: " << a << std::endl; + for (auto& slot : connection_map) { + for (auto& conn : slot.second) { + std::cout << conn(a) << std::endl; + } + } + } + + virtual ~Signal() + { + // Iterate all objects to which this is connected and disconnect from them: + for (auto& c : connectee_objects) { + c->disconnect(*this); + } + + // Iterate all objects connected to this and disconnect them: + for (auto& kv : connection_map) { + disconnect(*kv.first); + } + } + + std::map<Signal<T>*, std::vector<bound_emit_t>> connection_map; + std::set<Signal<T>*> connectee_objects; // Signals to which this is connected +}; + +///////////////////////////////////////////////////////////////////////////////////// + +template<typename T> +class Signal2 : public Signal<T> { +public: + Signal2(T t) : t(t) {} + virtual T emit(T a) override { return a + t;} + T t; +}; + +int main() +{ + + std::cout << std::endl << "First:" << std::endl; + { + Signal<int> o1; + + { + Signal2<int> o2(1); + Signal2<int> o3(1); + + o1.connect(o1); + o1.connect(o2); + o1.connect(o3); + + o1.send(4); + + o1.disconnect(o2); + + o1.send(5); + } // implicit o1.disconnect(o3) in o3 destructor. + + o1.send(6); + } + + std::cout << std::endl << "Second:" << std::endl; + { + Signal<float>* o = new Signal<float>(); + Signal<float>& o1 = *o; + + { + Signal2<float> o2(1); + Signal2<float> o3(1); + + o1.connect(o1); + o1.connect(o2); + o1.connect(o3); + + o1.send(4.1); + + delete o; + std::cout << std::endl << " -- post delete:" << std::endl; + + o1.send(4.2); + + } // implicit o1.disconnect(o3) in o3 destructor. + } + + std::cout << std::endl << "Third:" << std::endl; + { + Signal<std::string> o1; + + { + Signal2<std::string> o2("a"); + Signal2<std::string> o3("b"); + + o1.connect(o1); + o1.connect(o2); + o1.connect(o3); + + o1.send("hello"); + + o1.disconnect(o2); + + o1.send("world"); + } // implicit o1.disconnect(o3) in o3 destructor. + + o1.send("nisse"); + } + + return 0; +} + +#endif + +///////////////////////////////////////////////////////// +// What I would like(tm) +///////////////////////////////////////////////////////// +/* + +class Foo { +public: + Signal<int, float, std::string> changeSignal; + + void worker() + { + somethingChangedSignal.emit(42, 1.234, "hello"); + } +}; + +class Bar { +public: + Bar(Foo &foo) + { + foo.changeSignal.connect(this, &Bar::notifyCallback); + } + + void notifyCallback(int a, float b, std::string c) + { + // ... + } +}; + +class Bas { +public: + void iWannaKnow(int a, float b, std::string c) + { + // ... + } +}; + +int main() +{ + Foo foo; + Bar bar(foo); + Bas bas; + foo.changeSignal.connect(&bas, &Bas::iWannaKnow); + foo.worker(); +} + +*/ |