From: Christian P. <cp...@us...> - 2005-01-06 16:59:34
|
Update of /cvsroot/pclasses/pclasses2/include/pclasses In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16476/include/pclasses Modified Files: Callback.h Signal.h Log Message: Added Callback1, Fucntion1, Method1 templates. Added Signal1 template. Index: Callback.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/Callback.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- Callback.h 31 Dec 2004 18:43:41 -0000 1.1 +++ Callback.h 6 Jan 2005 16:59:23 -0000 1.2 @@ -25,21 +25,23 @@ /* ----------------- Callback0 ------------------ */ +//! Callback base class with no arguments template <typename RetType> class Callback0 { public: virtual ~Callback0() { } - virtual RetType exec() = 0; + virtual RetType exec() const = 0; protected: Callback0() { } }; +//! Callback base class with no arguments (void specialisation) template <> class Callback0<void> { public: virtual ~Callback0() { } - virtual void exec() = 0; + virtual void exec() const = 0; protected: Callback0() { } @@ -48,6 +50,7 @@ /* -------------- Function0 ------------- */ +//! Function callback class with no arguments template <typename RetType> class Function0: public Callback0<RetType> { public: @@ -56,13 +59,17 @@ Function0(FuncPtr ptr) : _funcPtr(ptr) { } - RetType exec() + RetType exec() const { return (*_funcPtr)(); } + bool operator==(const Function0& f) const + { return _funcPtr == f._funcPtr; } + private: FuncPtr _funcPtr; }; +//! Function callback class with no arguments (void specialisation) template <> class Function0<void>: public Callback0<void> { public: @@ -71,19 +78,24 @@ Function0(FuncPtr ptr) : _funcPtr(ptr) { } - void exec() + void exec() const { (*_funcPtr)(); } + bool operator==(const Function0& f) const + { return _funcPtr == f._funcPtr; } + private: FuncPtr _funcPtr; }; +//! Returns a function-callback object template <typename RetType> -Function0<RetType> callback(RetType (*ptr)()) +Function0<RetType> function(RetType (*ptr)()) { return Function0<RetType>(ptr); } /* ---------- Method0 ------------ */ +//! Method callback class with no arguments template <typename RetType, class ObjT> class Method0: public Callback0<RetType> { public: @@ -92,14 +104,18 @@ Method0(ObjT* obj, FuncPtr ptr) : _obj(obj), _funcPtr(ptr) { } - RetType exec() + RetType exec() const { return (_obj->*_funcPtr)(); } + bool operator==(const Method0& f) const + { return _obj == f._obj && _funcPtr == f._funcPtr; } + private: ObjT* _obj; FuncPtr _funcPtr; }; +//! Method callback class with no arguments (void specialisation) template <class ObjT> class Method0<void, ObjT>: public Callback0<void> { public: @@ -108,18 +124,145 @@ Method0(ObjT* obj, FuncPtr ptr) : _obj(obj), _funcPtr(ptr) { } - void exec() + void exec() const { (_obj->*_funcPtr)(); } + bool operator==(const Method0& f) const + { return _obj == f._obj && _funcPtr == f._funcPtr; } + private: ObjT* _obj; FuncPtr _funcPtr; }; +//! Returns a method-callback object template <typename RetType, class ObjT> -Method0<RetType,ObjT> callback(ObjT* obj, RetType (ObjT::*ptr)()) +Method0<RetType,ObjT> method(ObjT* obj, RetType (ObjT::*ptr)()) { return Method0<RetType,ObjT>(obj, ptr); } + + + +/* ----------------- Callback1 ------------------ */ + +//! Callback base class with one argument +template <typename RetType, typename ArgType1> +class Callback1 { + public: + virtual ~Callback1() { } + virtual RetType exec(ArgType1) const = 0; + + protected: + Callback1() { } +}; + +//! Callback base class with one argument (void specialisation) +template <typename ArgType1> +class Callback1<void, ArgType1> { + public: + virtual ~Callback1() { } + virtual void exec(ArgType1) const = 0; + + protected: + Callback1() { } + +}; + +/* -------------- Function1 ------------- */ + +//! Function callback class with one argument +template <typename RetType, typename ArgType1> +class Function1: public Callback1<RetType, ArgType1> { + public: + typedef RetType (*FuncPtr)(ArgType1); + + Function1(FuncPtr ptr) + : _funcPtr(ptr) { } + + RetType exec(ArgType1 arg1) const + { return (*_funcPtr)(arg1); } + + bool operator==(const Function1& f) const + { return _funcPtr == f._funcPtr; } + + private: + FuncPtr _funcPtr; +}; + +//! Function callback class with one argument (void specialisation) +template <typename ArgType1> +class Function1<void, ArgType1>: public Callback1<void, ArgType1> { + public: + typedef void (*FuncPtr)(ArgType1); + + Function1(FuncPtr ptr) + : _funcPtr(ptr) { } + + void exec(ArgType1 arg1) const + { (*_funcPtr)(arg1); } + + bool operator==(const Function1& f) const + { return _funcPtr == f._funcPtr; } + + private: + FuncPtr _funcPtr; +}; + +//! Returns a function-callback object +template <typename RetType, typename ArgType1> +Function1<RetType, ArgType1> function(RetType (*ptr)(ArgType1)) +{ return Function1<RetType, ArgType1>(ptr); } + +/* ---------- Method1 ------------ */ + +//! Method callback class with one arguments +template <typename RetType, class ObjT, typename ArgType1> +class Method1: public Callback1<RetType, ArgType1> { + public: + typedef RetType (ObjT::*FuncPtr)(ArgType1); + + Method1(ObjT* obj, FuncPtr ptr) + : _obj(obj), _funcPtr(ptr) { } + + RetType exec(ArgType1 arg1) const + { return (_obj->*_funcPtr)(arg1); } + + bool operator==(const Method1& f) const + { return _obj == f._obj && _funcPtr == f._funcPtr; } + + private: + ObjT* _obj; + FuncPtr _funcPtr; +}; + +//! Method callback class with no arguments (void specialisation) +template <class ObjT, typename ArgType1> +class Method1<void, ObjT, ArgType1>: public Callback1<void, ArgType1> { + public: + typedef void (ObjT::*FuncPtr)(ArgType1); + + Method1(ObjT* obj, FuncPtr ptr) + : _obj(obj), _funcPtr(ptr) { } + + void exec(ArgType1 arg1) const + { (_obj->*_funcPtr)(arg1); } + + bool operator==(const Method1& f) const + { return _obj == f._obj && _funcPtr == f._funcPtr; } + + private: + ObjT* _obj; + FuncPtr _funcPtr; +}; + +//! Returns a method-callback object +template <typename RetType, class ObjT, typename ArgType1> +Method1<RetType, ObjT, ArgType1> method(ObjT* obj, RetType (ObjT::*ptr)(ArgType1)) +{ return Method1<RetType, ObjT, ArgType1>(obj, ptr); } + + + + } // !namespace P #endif Index: Signal.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/Signal.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- Signal.h 31 Dec 2004 18:44:03 -0000 1.1 +++ Signal.h 6 Jan 2005 16:59:24 -0000 1.2 @@ -26,19 +26,22 @@ namespace P { +//! Internal helper for binding a callback to a signal template <class ListType, class CallbackType> void bind_slot(ListType& list, CallbackType cb) { list.push_back(new CallbackType(cb)); } +//! Internal helper for unbinding a callback from a signal template <class ListType, class CallbackType> void unbind_slot(ListType& list, CallbackType cb) { typename ListType::iterator i = list.begin(); while(i != list.end()) { - if(*i == cb) + CallbackType* val = static_cast<CallbackType*>(*i); + if(cb == *val) { delete *i; list.erase(i); @@ -48,6 +51,7 @@ } } +//! Internal helper for unbinding all callbacks template <class ListType> void unbind_slots(ListType& list) { @@ -61,7 +65,8 @@ /* -------------------- Signal0 ---------------- */ -template <class RetType> +//! Base class for Signal0 +template <typename RetType> class SignalBase0 { public: typedef std::list< @@ -82,23 +87,24 @@ void unbind(CallbackType slot) { unbind_slot(_slots, slot); } - virtual RetType exec() const = 0; + virtual RetType fire() const = 0; protected: CallbackList _slots; }; -template <class RetType> +//! Signal with no arguments +template <typename RetType> class Signal0: public SignalBase0<RetType> { public: - RetType exec() const + RetType fire() const { RetType ret = RetType(); typename CallbackList::const_iterator i = _slots.begin(); while(i != _slots.end()) { ret = (*i)->exec(); - if(!ret) + if(ret) break; ++i; } @@ -107,10 +113,11 @@ } }; +//! Signal with no arguments (void specialisation) template <> class Signal0<void>: public SignalBase0<void> { public: - void exec() const + void fire() const { CallbackList::const_iterator i = _slots.begin(); while(i != _slots.end()) @@ -121,6 +128,71 @@ } }; +/* -------------------- Signal1 ---------------- */ + +//! Base class for Signal1 +template <typename RetType, typename ArgType1> +class SignalBase1 { + public: + typedef std::list< + Callback1<RetType, ArgType1>* + > CallbackList; + + SignalBase1() + { } + + virtual ~SignalBase1() + { unbind_slots(_slots); } + + template <class CallbackType> + void bind(CallbackType slot) + { bind_slot(_slots, slot); } + + template <class CallbackType> + void unbind(CallbackType slot) + { unbind_slot(_slots, slot); } + + virtual RetType fire() const = 0; + + protected: + CallbackList _slots; +}; + +//! Signal with one argument +template <typename RetType, typename ArgType1> +class Signal1: public SignalBase1<RetType, ArgType1> { + public: + RetType fire(ArgType1 arg1) const + { + RetType ret = RetType(); + typename CallbackList::const_iterator i = _slots.begin(); + while(i != _slots.end()) + { + ret = (*i)->exec(arg1); + if(ret) + break; + ++i; + } + + return ret; + } +}; + +//! Signal with one arguments (void specialisation) +template <typename ArgType1> +class Signal1<void, ArgType1>: public SignalBase1<void, ArgType1> { + public: + void fire(ArgType1 arg1) const + { + typename CallbackList::const_iterator i = _slots.begin(); + while(i != _slots.end()) + { + (*i)->exec(arg1); + ++i; + } + } +}; + } // !namespace P |