CLAM-Development
1.1
|
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 00022 #ifndef __SIGNALV4IMPLVC6__ 00023 #define __SIGNALV4IMPLVC6__ 00024 00025 #ifndef __SIGNALV4__ 00026 #error "This is an implementation header. You are not allowed to include it directly!" 00027 #endif 00028 00029 #include <list> 00030 #include <utility> 00031 00032 namespace SigSlot 00033 { 00034 00035 template < typename ParmType1, typename ParmType2, typename ParmType3, typename ParmType4 > 00036 class Signalv4 : public Signal 00037 { 00038 00039 public: 00040 typedef typename CBL::Functor4<ParmType1,ParmType2,ParmType3,ParmType4> tCallbackType; 00041 00042 // Begin of "Connection Handler" 00043 00044 struct tCallback 00045 { 00046 tConnectionId mConnection; 00047 Slot* mSlot; 00048 tCallbackType mCallback; 00049 00050 tCallback( tConnectionId id, Slot* slot, tCallbackType cb ) 00051 : mConnection( id ), mSlot( slot ), mCallback( cb ) 00052 { 00053 } 00054 }; 00055 00056 typedef tCallbackType* tCallbackPtr; 00057 typedef std::list<tCallbackPtr> tCallList; 00058 typedef typename std::list<tCallbackPtr >::iterator tCallIterator; 00059 typedef std::list<tCallback> tCallbackList; 00060 typedef typename std::list<tCallback>::iterator tCbListIterator; 00061 typedef typename std::list<tCallback>::const_iterator const_tCbListIterator; 00062 00063 00064 protected: 00065 00066 void AddCallback( tConnectionId pConnection, Slot* slot, tCallbackType cb ) 00067 { 00068 mCallbacks.push_back( tCallback( pConnection, slot, cb ) ); 00069 } 00070 00071 bool HasNoCallbacks( ) const 00072 { 00073 return mCallbacks.empty(); 00074 } 00075 00076 tCallList& GetCalls( ) 00077 { 00078 mCalls.clear(); 00079 00080 tCbListIterator i = mCallbacks.begin(); 00081 tCbListIterator end = mCallbacks.end(); 00082 00083 while ( i!=end) 00084 { 00085 mCalls.push_back( &(i->mCallback) ); 00086 i++; 00087 } 00088 00089 return mCalls; 00090 } 00091 00092 void RemoveCall( tConnectionId id ) 00093 { 00094 tCbListIterator i = mCallbacks.begin(); 00095 tCbListIterator end = mCallbacks.end(); 00096 00097 while ( i!=end ) 00098 { 00099 if ( i->mConnection == id ) 00100 { 00101 mCallbacks.erase( i ); 00102 break; 00103 } 00104 i++; 00105 } 00106 } 00107 00108 void DestroyConnections() 00109 { 00110 tCbListIterator elem; 00111 00112 while ( !mCallbacks.empty() ) 00113 { 00114 elem = mCallbacks.begin(); 00115 00116 elem->mSlot->Unbind( elem->mConnection ); 00117 } 00118 } 00119 00120 // End of "ConnectionHandler" 00121 00122 public: 00123 00124 virtual ~Signalv4() 00125 { 00126 DestroyConnections(); 00127 } 00128 00129 void Connect( Slotv4<ParmType1, ParmType2, ParmType3, ParmType4>& slot ) 00130 { 00131 Connection c( AssignConnection(), this ); 00132 00133 AddCallback( c.GetID(), &slot, slot.GetMethod() ); 00134 00135 slot.Bind(c); 00136 } 00137 00138 void Emit( ParmType1 parm1, ParmType2 parm2, ParmType3 parm3, ParmType4 parm4 ) 00139 { 00140 if ( HasNoCallbacks() ) 00141 return; 00142 00143 tCallList calls = GetCalls(); 00144 tCallIterator i = calls.begin(); 00145 tCallIterator end = calls.end(); 00146 00147 while ( i != end ) 00148 { 00149 (*(*i))( parm1, parm2, parm3, parm4 ); 00150 i++; 00151 } 00152 00153 } 00154 00155 void FreeConnection( Connection* pConnection ) 00156 { 00157 RemoveCall( pConnection->GetID() ); 00158 FreeConnectionId( pConnection->GetID() ); 00159 } 00160 00161 private: 00162 00163 tCallList mCalls; 00164 tCallbackList mCallbacks; 00165 00166 00167 }; 00168 00169 } 00170 00171 00172 00173 #endif // Signalv4ImplVC6.hxx 00174