CLAM-Development  1.4.0
Signalv0ImplVC6.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef __SIGNALV0IMPLVC6__
23 #define __SIGNALV0IMPLVC6__
24 
25 #ifndef __SIGNALV0__
26 #error "This is an internal implementation header. You are not allowed to include it directly!"
27 #endif
28 
29 namespace SigSlot
30 {
31 
32  class Signalv0
33  : public Signal
34  {
35  public:
36  typedef CBL::Functor0 tCallbackType;
37  // Begin of ConnectionHandler
38  struct tCallback
39  {
43 
45  : mConnection( id ), mSlot( slot ), mCallback( cb )
46  {
47  }
48  };
49 
51  typedef std::list<tCallbackPtr> tCallList;
52  typedef std::list<tCallbackPtr >::iterator tCallIterator;
53  typedef std::list<tCallback> tCallbackList;
54  typedef std::list<tCallback>::iterator tCbListIterator;
55  typedef std::list<tCallback>::const_iterator const_tCbListIterator;
56 
57 
58  protected:
59 
60  void AddCallback( tConnectionId pConnection, Slot* slot, tCallbackType cb )
61  {
62  mCallbacks.push_back( tCallback( pConnection, slot, cb ) );
63  }
64 
65  bool HasNoCallbacks( ) const
66  {
67  return mCallbacks.empty();
68  }
69 
71  {
72  mCalls.clear();
73 
74  tCbListIterator i = mCallbacks.begin();
75  tCbListIterator end = mCallbacks.end();
76 
77  while ( i!=end)
78  {
79  mCalls.push_back( &(i->mCallback) );
80  i++;
81  }
82 
83  return mCalls;
84  }
85 
87  {
88  tCbListIterator i = mCallbacks.begin();
89  tCbListIterator end = mCallbacks.end();
90 
91  while ( i!=end )
92  {
93  if ( i->mConnection == id )
94  {
95  mCallbacks.erase( i );
96  break;
97  }
98  i++;
99  }
100  }
101 
103  {
104  tCbListIterator elem;
105 
106  while ( !mCallbacks.empty() )
107  {
108  elem = mCallbacks.begin();
109 
110  elem->mSlot->Unbind( elem->mConnection );
111  }
112  }
113  // End of "ConnectionHandler"
114  public:
115  virtual ~Signalv0()
116  {
118  }
119 
120 
121  void Connect( Slotv0& slot )
122  {
123  Connection c ( AssignConnection(), this );
124 
125  AddCallback( c.GetID(), &slot, slot.GetMethod() );
126 
127  slot.Bind(c);
128  }
129 
130  void Emit()
131  {
132  if ( HasNoCallbacks() )
133  return;
134 
135  tCallList calls = GetCalls();
136  tCallIterator i = calls.begin();
137  tCallIterator end = calls.end();
138 
139  while( i != end )
140  {
141  (*(*i))();
142  i++;
143  }
144  }
145 
146  void FreeConnection( Connection* pConnection )
147  {
148  RemoveCall( pConnection->GetID() );
149  FreeConnectionId( pConnection->GetID() );
150  }
151 
152  private:
153 
154  tCallList mCalls;
155  tCallbackList mCallbacks;
156 
157 
158  };
159 
160 }
161 
162 #endif // Signalv0ImplVC6.hxx
163