Commit 4e7c493e authored by JP Dinger's avatar JP Dinger

skins2: I fail to see why Subject (not Observer) needs to have virtual...

skins2: I fail to see why Subject (not Observer) needs to have virtual methods. If you do find a reason do tell. Observer should probably be defined inside Subject instead.
parent 82dfc1b3
...@@ -34,55 +34,51 @@ template <class S, class ARG> class Observer; ...@@ -34,55 +34,51 @@ template <class S, class ARG> class Observer;
/// Template for subjects in the Observer design pattern /// Template for subjects in the Observer design pattern
template <class S, class ARG = void> class Subject template <class S, class ARG = void> class Subject
{ {
private:
typedef std::set<Observer<S, ARG>*> observers_t;
public: public:
virtual ~Subject() { } ~Subject() { }
#if 0
/// Remove all observers; should only be used for debugging purposes /// Remove all observers; should only be used for debugging purposes
virtual void clearObservers() void clearObservers()
{ {
m_observers.clear(); m_observers.clear();
} }
#endif
/// Add an observer to this subject /// Add an observer to this subject. Ignore NULL observers.
/// Note: adding twice the same observer is not harmful /// Note: adding the same observer twice is not harmful.
virtual void addObserver( Observer<S, ARG>* pObserver ) void addObserver( Observer<S, ARG>* pObserver )
{ {
m_observers.insert( pObserver ); if( pObserver ) m_observers.insert( pObserver );
} }
/// Remove an observer from this subject /// Remove an observer from this subject. Ignore NULL observers.
/// Note: removing twice the same observer is not harmful /// Note: removing the same observer twice is not harmful.
virtual void delObserver( Observer<S, ARG>* pObserver ) void delObserver( Observer<S, ARG>* pObserver )
{ {
m_observers.erase( pObserver ); if( pObserver ) m_observers.erase( pObserver );
} }
/// Notify the observers when the status has changed /// Notify the observers when the status has changed
virtual void notify( ARG *arg ) void notify( ARG *arg )
{ {
// This stupid gcc 3.2 needs "typename" typename observers_t::const_iterator iter;
typename set<Observer<S, ARG>*>::const_iterator iter; for( iter = m_observers.begin(); iter != m_observers.end(); ++iter )
for( iter = m_observers.begin(); iter != m_observers.end();
iter++ )
{
if( *iter == NULL )
{
fprintf( stderr, "iter NULL !\n" );
return;
}
(*iter)->onUpdate( *this , arg ); (*iter)->onUpdate( *this , arg );
}
} }
/// Notify without any argument /// Notify without any argument
virtual void notify() { notify( NULL ); } void notify() { notify( NULL ); }
protected: protected:
Subject() { } Subject() { }
private: private:
/// Set of observers for this subject /// Set of observers for this subject
set<Observer<S, ARG>*> m_observers; observers_t m_observers;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment