[vlc-devel] commit: 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. (JP Dinger )

git version control git at videolan.org
Sat Dec 5 22:35:14 CET 2009


vlc | branch: master | JP Dinger <jpd at videolan.org> | Sun Nov 22 19:38:23 2009 +0100| [4e7c493eb69c0083895f07683673ed1322168bdd] | committer: JP Dinger 

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.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=4e7c493eb69c0083895f07683673ed1322168bdd
---

 modules/gui/skins2/utils/observer.hpp |   44 +++++++++++++++------------------
 1 files changed, 20 insertions(+), 24 deletions(-)

diff --git a/modules/gui/skins2/utils/observer.hpp b/modules/gui/skins2/utils/observer.hpp
index f88ee08..54f1755 100644
--- a/modules/gui/skins2/utils/observer.hpp
+++ b/modules/gui/skins2/utils/observer.hpp
@@ -34,55 +34,51 @@ template <class S, class ARG> class Observer;
 /// Template for subjects in the Observer design pattern
 template <class S, class ARG = void> class Subject
 {
+private:
+    typedef std::set<Observer<S, ARG>*> observers_t;
+
 public:
-    virtual ~Subject() { }
+    ~Subject() { }
 
+#if 0
     /// Remove all observers; should only be used for debugging purposes
-    virtual void clearObservers()
+    void clearObservers()
     {
         m_observers.clear();
     }
+#endif
 
-    /// Add an observer to this subject
-    /// Note: adding twice the same observer is not harmful
-    virtual void addObserver( Observer<S, ARG>* pObserver )
+    /// Add an observer to this subject. Ignore NULL observers.
+    /// Note: adding the same observer twice is not harmful.
+    void addObserver( Observer<S, ARG>* pObserver )
     {
-        m_observers.insert( pObserver );
+        if( pObserver ) m_observers.insert( pObserver );
     }
 
-    /// Remove an observer from this subject
-    /// Note: removing twice the same observer is not harmful
-    virtual void delObserver( Observer<S, ARG>* pObserver )
+    /// Remove an observer from this subject. Ignore NULL observers.
+    /// Note: removing the same observer twice is not harmful.
+    void delObserver( Observer<S, ARG>* pObserver )
     {
-        m_observers.erase( pObserver );
+        if( pObserver ) m_observers.erase( pObserver );
     }
 
     /// 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 set<Observer<S, ARG>*>::const_iterator iter;
-        for( iter = m_observers.begin(); iter != m_observers.end();
-             iter++ )
-        {
-            if( *iter == NULL )
-            {
-                fprintf( stderr, "iter NULL !\n" );
-                return;
-            }
+        typename observers_t::const_iterator iter;
+        for( iter = m_observers.begin(); iter != m_observers.end(); ++iter )
             (*iter)->onUpdate( *this , arg );
-        }
     }
 
     /// Notify without any argument
-    virtual void notify() { notify( NULL ); }
+    void notify() { notify( NULL ); }
 
 protected:
     Subject() { }
 
 private:
     /// Set of observers for this subject
-    set<Observer<S, ARG>*> m_observers;
+    observers_t m_observers;
 };
 
 




More information about the vlc-devel mailing list