From e9cf0c67ede9350e816324e13cabb21c5ca8d169 Mon Sep 17 00:00:00 2001 From: q66 Date: Thu, 16 Jul 2015 20:47:59 +0100 Subject: [PATCH] more signal functionality --- ostd/signal.hh | 72 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 7 deletions(-) diff --git a/ostd/signal.hh b/ostd/signal.hh index b8e183a..6cc31c4 100644 --- a/ostd/signal.hh +++ b/ostd/signal.hh @@ -25,13 +25,29 @@ namespace detail { p_funcs = nbuf; } - EventBase(EventBase &&ev): p_class(ev.p_class), p_funcs(ev.p_funcs), - p_nfuncs(ev.p_nfuncs) { - ev.p_class = nullptr; - ev.p_funcs = nullptr; - ev.p_nfuncs = 0; + EventBase(EventBase &&ev): p_class(nullptr), p_funcs(nullptr), + p_nfuncs(0) { + swap(ev); } + EventBase &operator=(const EventBase &ev) { + using Func = Function; + p_class = ev.p_class; + p_nfuncs = ev.p_nfuncs; + Func *nbuf = (Func *)new byte[sizeof(Func) * p_nfuncs]; + for (Size i = 0; i < p_nfuncs; ++i) + new (&nbuf[i]) Func(ev.p_funcs[i]); + p_funcs = nbuf; + return *this; + } + + EventBase &operator=(EventBase &&ev) { + swap(ev); + return *this; + } + + ~EventBase() { clear(); } + void clear() { for (Size i = 0; i < p_nfuncs; ++i) p_funcs[i].~Function(); @@ -72,6 +88,22 @@ namespace detail { if (p_funcs[i]) p_funcs[i](*p_class, args...); } + C *get_class() const { + return p_class; + } + + C *set_class(C *cl) { + C *ocl = p_class; + p_class = cl; + return ocl; + } + + void swap(EventBase &ev) { + detail::swap_adl(p_class, ev.p_class); + detail::swap_adl(p_funcs, ev.p_funcs); + detail::swap_adl(p_nfuncs, ev.p_nfuncs); + } + private: C *p_class; Function *p_funcs; @@ -89,7 +121,15 @@ public: Event(const Event &ev): p_base(ev.p_base) {} Event(Event &&ev): p_base(move(ev.p_base)) {} - ~Event() { clear(); } + Event &operator=(const Event &ev) { + p_base = ev.p_base; + return *this; + } + + Event &operator=(Event &&ev) { + p_base = move(ev.p_base); + return *this; + } void clear() { p_base.clear(); } @@ -103,6 +143,11 @@ public: template void operator()(Args &&...args) { emit(forward(args)...); } + + C *get_class() const { return p_base.get_class(); } + C *set_class(C *cl) { return p_base.set_class(cl); } + + void swap(Event &ev) { p_base.swap(ev.p_base); } }; template @@ -115,7 +160,15 @@ public: Event(const Event &ev): p_base(ev.p_base) {} Event(Event &&ev): p_base(move(ev.p_base)) {} - ~Event() { clear(); } + Event &operator=(const Event &ev) { + p_base = ev.p_base; + return *this; + } + + Event &operator=(Event &&ev) { + p_base = move(ev.p_base); + return *this; + } void clear() { p_base.clear(); } @@ -129,6 +182,11 @@ public: template void operator()(Args &&...args) const { emit(forward(args)...); } + + C *get_class() const { return p_base.get_class(); } + C *set_class(C *cl) { return p_base.set_class(cl); } + + void swap(Event &ev) { p_base.swap(ev.p_base); } }; } /* namespace ostd */