From: Christian P. <cp...@us...> - 2004-12-31 18:44:14
|
Update of /cvsroot/pclasses/pclasses2/include/pclasses In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32377/include/pclasses Added Files: Signal.h Log Message: Added beginning of Signals --- NEW FILE: Signal.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_Signal_h #define P_Signal_h #include <pclasses/Callback.h> #include <list> namespace P { template <class ListType, class CallbackType> void bind_slot(ListType& list, CallbackType cb) { list.push_back(new CallbackType(cb)); } 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) { delete *i; list.erase(i); break; } ++i; } } template <class ListType> void unbind_slots(ListType& list) { typename ListType::iterator i = list.begin(); while(i != list.end()) { delete *i; i = list.erase(i); } } /* -------------------- Signal0 ---------------- */ template <class RetType> class SignalBase0 { public: typedef std::list< Callback0<RetType>* > CallbackList; SignalBase0() { } virtual ~SignalBase0() { 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 exec() const = 0; protected: CallbackList _slots; }; template <class RetType> class Signal0: public SignalBase0<RetType> { public: RetType exec() const { RetType ret = RetType(); typename CallbackList::const_iterator i = _slots.begin(); while(i != _slots.end()) { ret = (*i)->exec(); if(!ret) break; ++i; } return ret; } }; template <> class Signal0<void>: public SignalBase0<void> { public: void exec() const { CallbackList::const_iterator i = _slots.begin(); while(i != _slots.end()) { (*i)->exec(); ++i; } } }; } // !namespace P #endif |