• entries
    743
  • comments
    1924
  • views
    580088

C++11 variable args applied to signals/slots system

Sign in to follow this  
Aardvajk

1244 views

Quick snippetty post today. Been looking at making a clean signals/slots system using varadic templates. This is only tested with GCC 4.7.2#ifndef DELEGATE_H#define DELEGATE_H#include #include template class Event;class BaseDelegate{public: virtual ~BaseDelegate(){ }};template class AbstractDelegate : public BaseDelegate{protected: virtual ~AbstractDelegate(); friend class Event; virtual void add(Event *s){ v.push_back(s); } virtual void remove(Event *s){ v.erase(std::remove(v.begin(), v.end(), s), v.end()); } virtual void call(Args... args) = 0; std::vector*> v;};template class ConcreteDelegate : public AbstractDelegate{public: ConcreteDelegate(T *t, void(T::*f)(Args...), Event &s);private: ConcreteDelegate(const ConcreteDelegate&); void operator=(const ConcreteDelegate&); friend class Event; virtual void call(Args... args){ (t->*f)(args...); } T *t; void(T::*f)(Args...);};template class Event{public: Event(){ } ~Event(){ for(auto i: v) i->remove(this); } void connect(AbstractDelegate &s){ v.push_back(&s); s.add(this); } void disconnect(AbstractDelegate &s){ v.erase(std::remove(v.begin(), v.end(), &s), v.end()); } void operator()(Args... args){ for(auto i: v) i->call(args...); }private: Event(const Event&); void operator=(const Event&); std::vector*> v;};template AbstractDelegate::~AbstractDelegate(){ for(auto i : v) i->disconnect(*this);}template ConcreteDelegate::ConcreteDelegate(T *t, void(T::*f)(Args...), Event &s) : t(t), f(f){ s.connect(*this);}class Delegate{public: Delegate(){ } ~Delegate(){ for(auto i: v) delete i; } template void connect(T *t, void(T::*f)(Args...), Event &s){ v.push_back(new ConcreteDelegate(t, f, s)); }private: Delegate(const Delegate&); void operator=(const Delegate&); std::vector v;};#endif // DELEGATE_H
Everything is non-copyable, can't quite work out sensible copying strategies for events or slots.

I haven't used Signal and Slot terminology because I use QtCreator and it highlights words like "slots" as they are sort of Qt defined keywords which was annoying.

Some usage:class SomeObject{public: Event someEvent; void f(){ someEvent(23, 45.0f); }};class AnotherObject{public: AnotherObject(SomeObject *o);private: void handle(int i, float f){ /* ... */ } Delegate delegate;};AnotherObject::AnotherObject(SomeObject *o){ delegate.connect(this, &AnotherObject::handle, o->someEvent);}
So defining an event in an emitter is just a case of adding a an Event<> object to its public interface. You can then call it using function syntax.

A listener just needs a Delegate object and can then connect events to any member function using the standard address-of syntax. This is compile-time type safe and will only allow connection to a member function with the correct parameter types.

The destructors of the Event and Delegate classes take care of doing auto-disconnects when the owning objects get destroyed.

A nice example of how varadic templates are taking traditionally complex and ugly code and making it almost trivial. I also like the way that you don't have to use any template syntax in the actual usage, apart from when you define the Event itself.

Thanks for reading.

If anyone wants to see an article on this, explaining step by step how it works, just let me know. Not something I'd do without some interest first but happy to contribute if anyone wants it.
Sign in to follow this  


4 Comments


Recommended Comments

I think an article would be great - you will reach many more people if you have a more distilled description of the system.  Nice work!

Share this comment


Link to comment
Cheers jb. Stretching the truth a bit if I claimed to be "on it" but will try to make time. Nothing like writing an article to make you discover you don't understand something as well as you think you do :)

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now