CLAM-Development  1.4.0
Signalv4ImplVC6.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 __SIGNALV4IMPLVC6__
23 #define __SIGNALV4IMPLVC6__
24 
25 #ifndef __SIGNALV4__
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, typename ParmType3, typename ParmType4 >
36  class Signalv4 : public Signal
37 {
38 
39 public:
40  typedef typename CBL::Functor4<ParmType1,ParmType2,ParmType3,ParmType4> 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 ~Signalv4()
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  void Emit( ParmType1 parm1, ParmType2 parm2, ParmType3 parm3, ParmType4 parm4 )
139  {
140  if ( HasNoCallbacks() )
141  return;
142 
143  tCallList calls = GetCalls();
144  tCallIterator i = calls.begin();
145  tCallIterator end = calls.end();
146 
147  while ( i != end )
148  {
149  (*(*i))( parm1, parm2, parm3, parm4 );
150  i++;
151  }
152 
153  }
154 
155  void FreeConnection( Connection* pConnection )
156  {
157  RemoveCall( pConnection->GetID() );
158  FreeConnectionId( pConnection->GetID() );
159  }
160 
161 private:
162 
163  tCallList mCalls;
164  tCallbackList mCallbacks;
165 
166 
167 };
168 
169 }
170 
171 
172 
173 #endif // Signalv4ImplVC6.hxx
174