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