CLAM-Development  1.4.0
Signalv2ImplVC6.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 __SIGNALV2IMPLVC6__
23 #define __SIGNALV2IMPLVC6__
24 
25 #ifndef __SIGNALV2__
26 #error "This is an implementation header. You are not allowed to include it directly!"
27 #endif
28 
29 #include <list>
30 #include <utility>
31 
32 namespace SigSlot
33 {
34 
35 template < typename ParmType1, typename ParmType2 >
36  class Signalv2 : public Signal
37 {
38 
39 public:
40  typedef typename CBL::Functor2<ParmType1,ParmType2> tCallbackType;
41 
42 // Begin of "Connection Handler"
43 
44  struct tCallback
45  {
49 
51  : mConnection( id ), mSlot( slot ), mCallback( cb )
52  {
53  }
54  };
55 
57  typedef std::list<tCallbackPtr> tCallList;
58  typedef typename std::list<tCallbackPtr >::iterator tCallIterator;
59  typedef std::list<tCallback> tCallbackList;
60  typedef typename std::list<tCallback>::iterator tCbListIterator;
61  typedef typename std::list<tCallback>::const_iterator const_tCbListIterator;
62 
63 
64 protected:
65 
66  void AddCallback( tConnectionId pConnection, Slot* slot, tCallbackType cb )
67  {
68  mCallbacks.push_back( tCallback( pConnection, slot, cb ) );
69  }
70 
71  bool HasNoCallbacks( ) const
72  {
73  return mCallbacks.empty();
74  }
75 
77  {
78  mCalls.clear();
79 
80  tCbListIterator i = mCallbacks.begin();
81  tCbListIterator end = mCallbacks.end();
82 
83  while ( i!=end)
84  {
85  mCalls.push_back( &(i->mCallback) );
86  i++;
87  }
88 
89  return mCalls;
90  }
91 
93  {
94  tCbListIterator i = mCallbacks.begin();
95  tCbListIterator end = mCallbacks.end();
96 
97  while ( i!=end )
98  {
99  if ( i->mConnection == id )
100  {
101  mCallbacks.erase( i );
102  break;
103  }
104  i++;
105  }
106  }
107 
109  {
110  tCbListIterator elem;
111 
112  while ( !mCallbacks.empty() )
113  {
114  elem = mCallbacks.begin();
115 
116  elem->mSlot->Unbind( elem->mConnection );
117  }
118  }
119 
120 // End of "ConnectionHandler"
121 
122 public:
123 
124  virtual ~Signalv2()
125  {
127  }
128 
130  {
131  Connection c( AssignConnection(), this );
132 
133  AddCallback( c.GetID(), &slot, slot.GetMethod() );
134 
135  slot.Bind(c);
136  }
137 
138 
139  void Emit( ParmType1 parm1, ParmType2 parm2 )
140  {
141  if ( HasNoCallbacks() )
142  return;
143 
144  tCallList calls = GetCalls();
145  tCallIterator i = calls.begin();
146  tCallIterator end = calls.end();
147 
148  while ( i != end )
149  {
150  (*(*i))( parm1, parm2 );
151  i++;
152  }
153 
154  }
155 
156  void FreeConnection( Connection* pConnection )
157  {
158  RemoveCall( pConnection->GetID() );
159  FreeConnectionId( pConnection->GetID() );
160  }
161 
162 private:
163 
164  tCallList mCalls;
165  tCallbackList mCallbacks;
166 
167 
168 };
169 
170 }
171 
172 
173 
174 #endif // Signalv2ImplVC6.hxx
175