00001 /* 00002 * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG) 00003 * UNIVERSITAT POMPEU FABRA 00004 * 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 * 00020 */ 00021 00025 #ifndef __CONNECTIONHANDLER__ 00026 #define __CONNECTIONHANDLER__ 00027 00028 #include <list> 00029 #include <utility> 00030 #include "Connection.hxx" 00031 00038 namespace SigSlot 00039 { 00040 class Slot; 00041 00056 template < class SignalType > 00057 class ConnectionHandler 00058 { 00059 public: 00061 typedef typename SignalType::tCallbackType tCallbackType; 00063 typedef typename SignalType::tConnectionId tConnectionId; 00064 00074 struct tCallback 00075 { 00076 tConnectionId mConnection; 00077 Slot* mSlot; 00078 tCallbackType mCallback; 00079 00080 tCallback( tConnectionId id, Slot* slot, tCallbackType cb ) 00081 : mConnection( id ), mSlot( slot ), mCallback( cb ) 00082 { 00083 } 00084 }; 00085 00088 typedef tCallbackType* tCallbackPtr; 00089 00092 typedef std::list<tCallbackPtr> tCallList; 00093 00096 typedef typename std::list<tCallbackPtr >::iterator tCallIterator; 00097 00100 typedef std::list<tCallback> tCallbackList; 00101 00104 typedef typename std::list<tCallback>::iterator tCbListIterator; 00105 00108 typedef typename std::list<tCallback>::const_iterator const_tCbListIterator; 00109 00110 00111 public: 00118 void AddCallback( tConnectionId pConnection, Slot* slot, tCallbackType cb ) 00119 { 00120 mCallbacks.push_back( tCallback( pConnection, slot, cb ) ); 00121 } 00122 00123 00128 bool HasNoCallbacks( ) const 00129 { 00130 return mCallbacks.empty(); 00131 } 00132 00141 tCallList& GetCalls( ) 00142 { 00143 mCalls.clear(); 00144 00145 tCbListIterator i = mCallbacks.begin(); 00146 tCbListIterator end = mCallbacks.end(); 00147 00148 while ( i!=end) 00149 { 00150 mCalls.push_back( &(i->mCallback) ); 00151 i++; 00152 } 00153 00154 return mCalls; 00155 } 00156 00165 void RemoveCall( tConnectionId id ) 00166 { 00167 tCbListIterator i = mCallbacks.begin(); 00168 tCbListIterator end = mCallbacks.end(); 00169 00170 while ( i!=end ) 00171 { 00172 if ( i->mConnection == id ) 00173 { 00174 mCallbacks.erase( i ); 00175 break; 00176 } 00177 i++; 00178 } 00179 } 00180 00185 void DestroyConnections() 00186 { 00187 tCbListIterator elem; 00188 00189 while ( !mCallbacks.empty() ) 00190 { 00191 elem = mCallbacks.begin(); 00192 00193 elem->mSlot->Unbind( elem->mConnection ); 00194 } 00195 } 00196 00197 private: 00198 00199 tCallList mCalls; 00200 tCallbackList mCallbacks; 00202 }; 00203 00204 } 00205 00206 #endif //ConnectionHandler.hxx 00207