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