From: Christian P. <cp...@us...> - 2004-12-31 18:43:51
|
Update of /cvsroot/pclasses/pclasses2/include/pclasses In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32154/include/pclasses Added Files: Callback.h Log Message: Added Callback proxy templates. --- NEW FILE: Callback.h --- /*************************************************************************** * Copyright (C) 2004 by Christian Prochnow * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License as * * published by the Free Software Foundation; either version 2 of the * * License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifndef P_Callback_h #define P_Callback_h namespace P { /* ----------------- Callback0 ------------------ */ template <typename RetType> class Callback0 { public: virtual ~Callback0() { } virtual RetType exec() = 0; protected: Callback0() { } }; template <> class Callback0<void> { public: virtual ~Callback0() { } virtual void exec() = 0; protected: Callback0() { } }; /* -------------- Function0 ------------- */ template <typename RetType> class Function0: public Callback0<RetType> { public: typedef RetType (*FuncPtr)(); Function0(FuncPtr ptr) : _funcPtr(ptr) { } RetType exec() { return (*_funcPtr)(); } private: FuncPtr _funcPtr; }; template <> class Function0<void>: public Callback0<void> { public: typedef void (*FuncPtr)(); Function0(FuncPtr ptr) : _funcPtr(ptr) { } void exec() { (*_funcPtr)(); } private: FuncPtr _funcPtr; }; template <typename RetType> Function0<RetType> callback(RetType (*ptr)()) { return Function0<RetType>(ptr); } /* ---------- Method0 ------------ */ template <typename RetType, class ObjT> class Method0: public Callback0<RetType> { public: typedef RetType (ObjT::*FuncPtr)(); Method0(ObjT* obj, FuncPtr ptr) : _obj(obj), _funcPtr(ptr) { } RetType exec() { return (_obj->*_funcPtr)(); } private: ObjT* _obj; FuncPtr _funcPtr; }; template <class ObjT> class Method0<void, ObjT>: public Callback0<void> { public: typedef void (ObjT::*FuncPtr)(); Method0(ObjT* obj, FuncPtr ptr) : _obj(obj), _funcPtr(ptr) { } void exec() { (_obj->*_funcPtr)(); } private: ObjT* _obj; FuncPtr _funcPtr; }; template <typename RetType, class ObjT> Method0<RetType,ObjT> callback(ObjT* obj, RetType (ObjT::*ptr)()) { return Method0<RetType,ObjT>(obj, ptr); } } // !namespace P #endif |