00001
00002 #ifndef INCLUDED_EventCallback_h_
00003 #define INCLUDED_EventCallback_h_
00004
00005 #include "Events/EventListener.h"
00006 #include "Shared/string_util.h"
00007 #include "Events/EventRouter.h"
00008 #include <typeinfo>
00009
00010
00011
00012 template<class EV>
00013 class EventCallbackAs : public EventListener {
00014 public:
00015
00016 template<typename F>
00017 EventCallbackAs(const F& cb) : EventListener(), fun(new FunctorInstance<F,void>(cb)) { }
00018
00019
00020 template<typename F, typename C>
00021 EventCallbackAs(const F& cb, C& userdata) : EventListener(), fun(new FunctorInstance<F,C>(cb,userdata)) { }
00022
00023
00024 template<typename F, typename C>
00025 EventCallbackAs(const F& cb, const C& userdata) : EventListener(), fun(new FunctorInstance<F,C>(cb,userdata)) { }
00026
00027
00028 template<typename F>
00029 void redirect(const F& cb) { delete fun; fun = new FunctorInstance<F,void>(cb); }
00030
00031
00032 template<typename F, typename C>
00033 void redirect(const F& cb, C& userdata) { delete fun; fun=new FunctorInstance<F,C>(cb,userdata); }
00034
00035
00036 template<typename F, typename C>
00037 void redirect(const F& cb, const C& userdata) { delete fun; fun=new FunctorInstance<F,C>(cb,userdata); }
00038
00039
00040 ~EventCallbackAs() { erouter->remove(this); delete fun; fun=NULL; }
00041
00042 protected:
00043
00044
00045
00046
00047 struct FunctorAdapter {
00048 virtual ~FunctorAdapter() {}
00049 virtual void operator()(const EV& event)=0;
00050 };
00051
00052
00053 template<typename F, typename C> struct FunctorInstance : public FunctorAdapter {
00054 FunctorInstance(const F& cb, const C& x) : fun(cb), data(x) {}
00055 virtual void operator()(const EV& event) { fun(data,event); }
00056 F fun;
00057 C data;
00058 private:
00059 FunctorInstance(const FunctorInstance&);
00060 FunctorInstance& operator=(const FunctorInstance&);
00061 };
00062
00063
00064 template<typename R, class C, class SUB, class ET> struct FunctorInstance<R (C::*)(const ET&),SUB> : public FunctorAdapter {
00065 FunctorInstance(R (C::*cb)(const EV&), SUB& x) : fun(cb), cl(x) {}
00066 virtual void operator()(const EV& event) { (cl.*fun)(event); }
00067 R (C::*fun)(const EV& event);
00068 SUB& cl;
00069 };
00070
00071 template<typename R, class C, class SUB, class ET> struct FunctorInstance<R (C::*)(const ET&) const,SUB> : public FunctorAdapter {
00072 FunctorInstance(R (C::*cb)(const EV&) const, const SUB& x) : fun(cb), cl(x) {}
00073 virtual void operator()(const EV& event) { (cl.*fun)(event); }
00074 R (C::*fun)(const EV& event) const;
00075 const SUB& cl;
00076 };
00077
00078 template<typename R, class ET> struct FunctorInstance<R (*)(const ET&),void> : public FunctorAdapter {
00079 FunctorInstance(R (*cb)(const EV&)) : fun(cb) {}
00080 virtual void operator()(const EV& event) { fun(event); }
00081 R (*fun)(const EV& event);
00082 };
00083
00084
00085 template<typename R, class C, class SUB, class ET> struct FunctorInstance<R* (C::*)(const ET&),SUB> : public FunctorAdapter {
00086 FunctorInstance(R* (C::*cb)(const EV&), SUB& x) : fun(cb), cl(x) {}
00087 virtual void operator()(const EV& event) { (cl.*fun)(event); }
00088 R* (C::*fun)(const EV& event);
00089 SUB& cl;
00090 };
00091
00092 template<typename R, class C, class SUB, class ET> struct FunctorInstance<R* (C::*)(const ET&) const,SUB> : public FunctorAdapter {
00093 FunctorInstance(R* (C::*cb)(const EV&) const, const SUB& x) : fun(cb), cl(x) {}
00094 virtual void operator()(const EV& event) { (cl.*fun)(event); }
00095 R* (C::*fun)(const EV& event) const;
00096 const SUB& cl;
00097 };
00098
00099 template<typename R, class ET> struct FunctorInstance<R* (*)(const ET&),void> : public FunctorAdapter {
00100 FunctorInstance(R* (*cb)(const EV&)) : fun(cb) {}
00101 virtual void operator()(const EV& event) { fun(event); }
00102 R* (*fun)(const EV& event);
00103 };
00104
00105
00106
00107 virtual void processEvent(const EventBase& event) {
00108 const EV* ev = dynamic_cast<const EV*>(&event);
00109 if(ev==NULL) {
00110 std::cerr << "EventCallback dropping " << event.getDescription() << ", was expected type " << string_util::demangle(typeid(EV).name()) << " not " << string_util::demangle(typeid(event).name()) << std::endl;
00111 } else {
00112 (*fun)(*ev);
00113 }
00114 }
00115 FunctorAdapter * fun;
00116
00117 private:
00118 EventCallbackAs(const EventCallbackAs&);
00119 EventCallbackAs& operator=(const EventCallbackAs&);
00120 };
00121
00122 typedef EventCallbackAs<EventBase> EventCallback;
00123
00124
00125
00126
00127
00128
00129 #endif