00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "CeylanEventListener.h"
00028
00029 #include "CeylanLogPlug.h"
00030 #include "CeylanOperators.h"
00031 #include "CeylanEventSource.h"
00032
00033
00034 #ifdef CEYLAN_USES_CONFIG_H
00035 #include "CeylanConfig.h"
00036 #endif // CEYLAN_USES_CONFIG_H
00037
00038
00039 using std::string ;
00040 using std::list ;
00041
00042
00043 using namespace Ceylan ;
00044 using namespace Ceylan::Log ;
00045
00046
00047
00048
00049 EventListener::EventListener() :
00050 _sources()
00051 {
00052
00053 }
00054
00055
00056
00057 EventListener::EventListener( EventSource & source ) :
00058 _sources()
00059 {
00060
00061 subscribeTo( source ) ;
00062
00063 }
00064
00065
00066
00067 EventListener::~EventListener() throw()
00068 {
00069
00070 #if CEYLAN_DEBUG_EVENTS
00071 if ( ! _sources.empty() )
00072 LogPlug::debug( "EventListener destructor: "
00073 "listener was still registered to "
00074 + Ceylan::toString(
00075 static_cast<Ceylan::Uint32>( _sources.size() ) )
00076 + " source(s), unsubscribing it." ) ;
00077 #endif // CEYLAN_DEBUG_EVENTS
00078
00079 unsubscribeFromAllSources() ;
00080
00081
00082
00083 }
00084
00085
00086
00087 void EventListener::subscribeTo( EventSource & source )
00088 {
00089
00090 for ( list<EventSource *>::const_iterator it = _sources.begin();
00091 it != _sources.end(); it ++ )
00092 {
00093 if ( (*it) == & source )
00094 throw EventException(
00095 "EventListener::subscribeTo: event source "
00096 + source.toString() + " is already registered." ) ;
00097
00098 }
00099
00100 _sources.push_back( & source ) ;
00101
00102 source.add( * this ) ;
00103
00104 #if CEYLAN_DEBUG_EVENTS
00105 LogPlug::debug( "EventListener subscribed to source '"
00106 + source.toString() + "'." ) ;
00107 #endif // CEYLAN_DEBUG_EVENTS
00108
00109 }
00110
00111
00112
00113 void EventListener::unsubscribeFrom( EventSource & source )
00114 {
00115
00116 list<EventSource *>::iterator it ;
00117
00118 for ( it = _sources.begin(); it != _sources.end(); it++ )
00119 {
00120
00121 if ( (*it) == & source )
00122 {
00123
00124
00125 (*it)->remove( *this ) ;
00126
00127 #if CEYLAN_DEBUG_EVENTS
00128
00129 LogPlug::debug(
00130 "EventListener unsubscribed from following source: "
00131 + (*it)->toString() ) ;
00132
00133 #endif // CEYLAN_DEBUG_EVENTS
00134
00135
00136
00137
00138
00139
00140 _sources.erase( it ) ;
00141
00142
00143
00144
00145 return ;
00146
00147 }
00148
00149 }
00150
00151 if ( it == _sources.end() )
00152 throw EventException( "EventListener::unsubscribeFrom: "
00153 "listener was not already registered to following event source: "
00154 + source.toString() ) ;
00155
00156 }
00157
00158
00159
00160 void EventListener::unsubscribeFromAllSources()
00161 {
00162
00163 for ( list<EventSource *>::iterator it = _sources.begin();
00164 it != _sources.end(); it++ )
00165 {
00166
00167
00168
00169
00170
00171
00172
00173
00174 (*it)->remove( * this ) ;
00175
00176 #if CEYLAN_DEBUG_EVENTS
00177
00178 LogPlug::debug( "EventListener unsubscribed from following source: "
00179 + (*it)->toString() ) ;
00180
00181 #endif // CEYLAN_DEBUG_EVENTS
00182
00183 }
00184
00185 _sources.clear() ;
00186
00187 }
00188
00189
00190
00191 void EventListener::forgetSource( EventSource & source )
00192 {
00193
00194 #if CEYLAN_DEBUG_EVENTS
00195
00196
00197
00198 LogPlug::warning( "EventListener had to forgot source "
00199 + source.toString() ) ;
00200
00201 bool forgotten = false ;
00202
00203 list<EventSource *>::iterator it = _sources.begin() ;
00204
00205 while ( it != _sources.end() )
00206 {
00207
00208 if ( (*it) == & source )
00209 {
00210
00211 it = _sources.erase( it ) ;
00212 forgotten = true ;
00213 }
00214 else
00215 {
00216
00217 it++ ;
00218
00219 }
00220
00221 }
00222
00223 if ( ! forgotten )
00224 throw EventException( "EventListener::forgetSource: "
00225 "listener was not already registered to following event source: "
00226 + source.toString() ) ;
00227
00228 #else // CEYLAN_DEBUG_EVENTS
00229
00230 _sources.remove( & source ) ;
00231
00232 #endif // CEYLAN_DEBUG_EVENTS
00233
00234 }
00235
00236
00237
00238 list<EventSource *> EventListener::getSources() const
00239 {
00240
00241 return _sources ;
00242
00243 }
00244
00245
00246
00247 const string EventListener::toString( Ceylan::VerbosityLevels level ) const
00248 {
00249
00250 if ( _sources.empty() )
00251 return "Event listener not currently subscribed to any event source" ;
00252 else
00253 return "Event listener subscribed currently to "
00254 + Ceylan::toString(
00255 static_cast<Ceylan::Uint32>( _sources.size() ) )
00256 + " different event source(s)" ;
00257 }
00258