CLAM-Development  1.4.0
Signalv1ImplVC6.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 __SIGNALV1IMPLVC6__
23 #define __SIGNALV1IMPLVC6__
24 
25 #ifndef __SIGNALV1__
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 >
36  class Signalv1 : public Signal
37 {
38 public:
39  typedef typename CBL::Functor1<ParmType1> tCallbackType;
40 // Begin of ConnectionHandler
41  struct tCallback
42  {
46 
48  : mConnection( id ), mSlot( slot ), mCallback( cb )
49  {
50  }
51  };
52 
54  typedef std::list<tCallbackPtr> tCallList;
55  typedef typename std::list<tCallbackPtr >::iterator tCallIterator;
56  typedef std::list<tCallback> tCallbackList;
57  typedef typename std::list<tCallback>::iterator tCbListIterator;
58  typedef typename std::list<tCallback>::const_iterator const_tCbListIterator;
59 
60 
61 protected:
62 
63  void AddCallback( tConnectionId pConnection, Slot* slot, tCallbackType cb )
64  {
65  mCallbacks.push_back( tCallback( pConnection, slot, cb ) );
66  }
67 
68  bool HasNoCallbacks( ) const
69  {
70  return mCallbacks.empty();
71  }
72 
74  {
75  mCalls.clear();
76 
77  tCbListIterator i = mCallbacks.begin();
78  tCbListIterator end = mCallbacks.end();
79 
80  while ( i!=end)
81  {
82  mCalls.push_back( &(i->mCallback) );
83  i++;
84  }
85 
86  return mCalls;
87  }
88 
90  {
91  tCbListIterator i = mCallbacks.begin();
92  tCbListIterator end = mCallbacks.end();
93 
94  while ( i!=end )
95  {
96  if ( i->mConnection == id )
97  {
98  mCallbacks.erase( i );
99  break;
100  }
101  i++;
102  }
103  }
104 
106  {
107  tCbListIterator elem;
108 
109  while ( !mCallbacks.empty() )
110  {
111  elem = mCallbacks.begin();
112 
113  elem->mSlot->Unbind( elem->mConnection );
114  }
115  }
116 // End of "ConnectionHandler"
117 
118 public:
119 
120  virtual ~Signalv1()
121  {
123  }
124 
126  {
127  Connection c( AssignConnection(), this );
128 
129  AddCallback( c.GetID(), &slot, slot.GetMethod() );
130 
131  slot.Bind(c);
132  }
133 
134  void Emit( ParmType1 parm )
135  {
136  if ( HasNoCallbacks() )
137  return;
138 
139  tCallList calls = GetCalls();
140  tCallIterator i = calls.begin();
141  tCallIterator end = calls.end();
142 
143  while ( i != end )
144  {
145  (*(*i))( parm );
146  i++;
147  }
148 
149  }
150 
151  void FreeConnection( Connection* pConnection )
152  {
153  RemoveCall( pConnection->GetID() );
154  FreeConnectionId( pConnection->GetID() );
155  }
156 
157 private:
158 
159  tCallList mCalls;
160  tCallbackList mCallbacks;
161 
162 
163 };
164 
165 }
166 
167 
168 #endif // Signalv1ImplVC6.hxx
169