From 24bb55371a5d8d81b23521ebdfcde68f0035697c Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sun, 20 Sep 2015 11:36:22 +0200 Subject: Basic project based on DrumGizmo. --- plugingui/test.cc | 172 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 plugingui/test.cc (limited to 'plugingui/test.cc') diff --git a/plugingui/test.cc b/plugingui/test.cc new file mode 100644 index 0000000..2f60d37 --- /dev/null +++ b/plugingui/test.cc @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include +#include +#include + +///////////////////////////////////////// +// +// Listener and Notifier classes: +// +///////////////////////////////////////// + +class Listener; +class NotifierBase { +public: + virtual void disconnect(Listener* object) {} +}; + +class Listener { +public: + virtual ~Listener() + { + for (auto signal: signals) { + signal->disconnect(this); + } + } + + void registerNotifier(NotifierBase* signal) + { + signals.insert(signal); + } + + void unregisterNotifier(NotifierBase* signal) + { + signals.erase(signal); + } + + std::set signals; +}; + +template +class Notifier : public NotifierBase { +public: + Notifier() {} + ~Notifier() + { + for (auto slot: slots) { + slot.first->unregisterNotifier(this); + } + } + + void connect(Listener* object, std::function slot) + { + slots[object] = slot; + object->registerNotifier(this); + } + + void disconnect(Listener* object) + { + slots.erase(object); + } + + void notify(T t, Args...args) + { + for (auto slot: slots) { + slot.second(t, args...); + } + } + + std::map> slots; +}; + +///////////////////////////////////////// +// +// Code which I don't understand but does something clever and produces a compile warning: +// +///////////////////////////////////////// + +template struct seq{}; +template +struct gen_seq : gen_seq{}; +template +struct gen_seq<0, Is...> : seq{}; + +template struct placeholder{}; + +namespace std{ +template +struct is_placeholder< ::placeholder > : integral_constant{}; +} // std:: + +namespace aux{ +template +auto easy_bind(seq, F&& f, Ts&&... vs) -> decltype(std::bind(std::forward(f), std::forward(vs)..., ::placeholder<1 + Is>()...)) { + return std::bind(std::forward(f), std::forward(vs)..., ::placeholder<1 + Is>()...); +} +} // aux:: + +template +auto mem_bind(R (C::*ptmf)(FArgs...), Args&&... vs) -> decltype(aux::easy_bind(gen_seq<(sizeof...(FArgs) + 1) - sizeof...(Args)>(), ptmf, std::forward(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(vs)...); +} + +template +auto mem_bind(T C::*ptmd, Args&&... vs) -> decltype(aux::easy_bind(gen_seq<1 - sizeof...(Args)>(), ptmd, std::forward(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(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 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 _connect(SRC, SIG, TAR, SLO) SRC.SIG.connect(&TAR, mem_bind(&decltype(TAR)::SLO, TAR)) + +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 + + _connect(btn, mouseMove, object, onMouseMoved); + + MyObject2 object2; + btn.mouseMove.connect(CONN(object2, mouseMoved)); + + // Now trigger the notification + btn.emulateMouseMove(10,10); +} -- cgit v1.2.3