From: Christian P. <cp...@us...> - 2005-01-28 17:26:15
|
Update of /cvsroot/pclasses/pclasses2/include/pclasses In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30997/include/pclasses Modified Files: Callback.h Log Message: Added CallbackArgs. Added generic exec() method. Index: Callback.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/Callback.h,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- Callback.h 28 Jan 2005 11:33:28 -0000 1.4 +++ Callback.h 28 Jan 2005 17:11:57 -0000 1.5 @@ -23,48 +23,79 @@ namespace P { +struct CallbackArgs { }; + +//! Callback base class +class Callback { + public: + struct RetType { }; + + Callback() { } + virtual ~Callback() { } + + virtual Callback* clone() const = 0; + virtual RetType exec(const CallbackArgs& args) const = 0; +}; + /* ----------------- Callback0 ------------------ */ +//! Callback arguments with no arguments +struct CallbackArgs0: CallbackArgs { }; + //! Callback base class with no arguments -template <typename RetType> -class Callback0 { +template <typename _RetT> +class Callback0: public Callback { public: - virtual ~Callback0() { } - virtual RetType exec() const = 0; - virtual Callback0* clone() const = 0; + typedef CallbackArgs0 Args; + struct RetType: public Callback::RetType { _RetT retVal; }; - protected: Callback0() { } + virtual ~Callback0() { } + + Callback::RetType exec(const CallbackArgs& args) const + { + RetType ret; + ret.retVal = exec(); + return ret; + } + + virtual _RetT exec() const = 0; }; //! Callback base class with no arguments (void specialisation) template <> -class Callback0<void> { +class Callback0<void>: public Callback { public: - virtual ~Callback0() { } - virtual void exec() const = 0; - virtual Callback0* clone() const = 0; + typedef CallbackArgs0 Args; + struct RetType: public Callback::RetType { }; - protected: Callback0() { } + virtual ~Callback0() { } + Callback::RetType exec(const CallbackArgs& args) const + { + exec(); + return RetType(); + } + + virtual void exec() const = 0; }; -/* -------------- Function0 ------------- */ +/* ----------------- Function0 ------------------ */ //! Function callback class with no arguments -template <typename RetType> -class Function0: public Callback0<RetType> { +template <typename _RetT> +class Function0: public Callback0<_RetT> { public: - typedef RetType (*FuncPtr)(); + typedef _RetT (*FuncPtr)(); Function0(FuncPtr ptr) : _funcPtr(ptr) { } - RetType exec() const + _RetT exec() const { return (*_funcPtr)(); } - Callback0<RetType>* clone() const + Callback0<_RetT>* clone() const { return new Function0(*this); } bool operator==(const Function0& f) const @@ -97,25 +128,25 @@ }; //! Returns a function-callback object -template <typename RetType> -Function0<RetType> function(RetType (*ptr)()) -{ return Function0<RetType>(ptr); } +template <typename _RetT> +Function0<_RetT> make_function(_RetT (*ptr)()) +{ return Function0<_RetT>(ptr); } -/* ---------- Method0 ------------ */ +/* ----------------- Method0 ------------------ */ //! Method callback class with no arguments -template <typename RetType, class ObjT> -class Method0: public Callback0<RetType> { +template <typename _RetT, class ObjT> +class Method0: public Callback0<_RetT> { public: - typedef RetType (ObjT::*FuncPtr)(); + typedef _RetT (ObjT::*FuncPtr)(); Method0(ObjT* obj, FuncPtr ptr) : _obj(obj), _funcPtr(ptr) { } - RetType exec() const + _RetT exec() const { return (_obj->*_funcPtr)(); } - Callback0<RetType>* clone() const + Callback0<_RetT>* clone() const { return new Method0(*this); } bool operator==(const Method0& f) const @@ -150,282 +181,15 @@ }; //! Returns a method-callback object -template <typename RetType, class ObjT> -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; - virtual Callback1* clone() 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; - virtual Callback1* clone() 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); } - - Callback1<RetType, ArgType1>* clone() const - { return new Function1(*this); } - - 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); } - - Callback1<void, ArgType1>* clone() const - { return new Function1(*this); } - - 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); } - - Callback1<RetType, ArgType1>* clone() const - { return new Method1(*this); } - - 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); } - - Callback1<void, ArgType1>* clone() const - { return new Method1(*this); } - - 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); } - - - - -/* ----------------- Callback2 ------------------ */ - -//! Callback base class with two arguments -template <typename RetType, typename ArgType1, typename ArgType2> -class Callback2 { - public: - virtual ~Callback2() { } - virtual RetType exec(ArgType1, ArgType2) const = 0; - virtual Callback2* clone() const = 0; - - protected: - Callback2() { } -}; - -//! Callback base class with two arguments (void specialisation) -template <typename ArgType1, typename ArgType2> -class Callback2<void, ArgType1, ArgType2> { - public: - virtual ~Callback2() { } - virtual void exec(ArgType1, ArgType2) const = 0; - virtual Callback2* clone() const = 0; - - protected: - Callback2() { } - -}; - -/* -------------- Function2 ------------- */ - -//! Function callback class with two arguments -template <typename RetType, typename ArgType1, typename ArgType2> -class Function2: public Callback2<RetType, ArgType1, ArgType2> { - public: - typedef RetType (*FuncPtr)(ArgType1, ArgType2); - - Function2(FuncPtr ptr) - : _funcPtr(ptr) { } - - RetType exec(ArgType1 arg1, ArgType2 arg2) const - { return (*_funcPtr)(arg1, arg2); } - - Callback2<RetType, ArgType1, ArgType2>* clone() const - { return new Function2(*this); } - - bool operator==(const Function2& f) const - { return _funcPtr == f._funcPtr; } - - private: - FuncPtr _funcPtr; -}; - -//! Function callback class with two arguments (void specialisation) -template <typename ArgType1, typename ArgType2> -class Function2<void, ArgType1, ArgType2>: public Callback2<void, ArgType1, ArgType2> { - public: - typedef void (*FuncPtr)(ArgType1, ArgType2); - - Function2(FuncPtr ptr) - : _funcPtr(ptr) { } - - void exec(ArgType1 arg1, ArgType2 arg2) const - { (*_funcPtr)(arg1, arg2); } - - Callback2<void, ArgType1, ArgType2>* clone() const - { return new Function2(*this); } - - bool operator==(const Function2& f) const - { return _funcPtr == f._funcPtr; } - - private: - FuncPtr _funcPtr; -}; - -//! Returns a function-callback object -template <typename RetType, typename ArgType1, typename ArgType2> -Function2<RetType, ArgType1, ArgType2> function(RetType (*ptr)(ArgType1,ArgType2)) -{ return Function2<RetType, ArgType1, ArgType2>(ptr); } - -/* ---------- Method2 ------------ */ - -//! Method callback class with two arguments -template <typename RetType, class ObjT, typename ArgType1, typename ArgType2> -class Method2: public Callback2<RetType, ArgType1, ArgType2> { - public: - typedef RetType (ObjT::*FuncPtr)(ArgType1, ArgType2); - - Method2(ObjT* obj, FuncPtr ptr) - : _obj(obj), _funcPtr(ptr) { } - - RetType exec(ArgType1 arg1, ArgType2 arg2) const - { return (_obj->*_funcPtr)(arg1, arg2); } - - Callback2<RetType, ArgType1, ArgType2>* clone() const - { return new Method2(*this); } - - bool operator==(const Method2& f) const - { return _obj == f._obj && _funcPtr == f._funcPtr; } - - private: - ObjT* _obj; - FuncPtr _funcPtr; -}; - -//! Method callback class with two arguments (void specialisation) -template <class ObjT, typename ArgType1, typename ArgType2> -class Method2<void, ObjT, ArgType1, ArgType2>: - public Callback2<void, ArgType1, ArgType2> -{ - public: - typedef void (ObjT::*FuncPtr)(ArgType1, ArgType2); - - Method2(ObjT* obj, FuncPtr ptr) - : _obj(obj), _funcPtr(ptr) { } - - void exec(ArgType1 arg1, ArgType2 arg2) const - { (_obj->*_funcPtr)(arg1, arg2); } - - Callback2<void, ArgType1, ArgType2>* clone() const - { return new Method2(*this); } - - bool operator==(const Method2& 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, typename ArgType2> -Method2<RetType, ObjT, ArgType1, ArgType2> -method(ObjT* obj, RetType (ObjT::*ptr)(ArgType1, ArgType2)) -{ return Method2<RetType, ObjT, ArgType1, ArgType2>(obj, ptr); } - +template <typename _RetT, class _ObjT> +Method0<_RetT,_ObjT> make_method(_ObjT* obj, _RetT (_ObjT::*ptr)()) +{ return Method0<_RetT,_ObjT>(obj, ptr); } } // !namespace P +#include <pclasses/CallbackN1.h> +#include <pclasses/CallbackN2.h> +#include <pclasses/CallbackN3.h> +#include <pclasses/CallbackN4.h> + #endif |