observer.hpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * observer.hpp
00003  *****************************************************************************
00004  * Copyright (C) 2003 the VideoLAN team
00005  * $Id$
00006  *
00007  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
00008  *          Olivier Teulière <ipkiss@via.ecp.fr>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
00023  *****************************************************************************/
00024 
00025 #ifndef OBSERVER_HPP
00026 #define OBSERVER_HPP
00027 
00028 #include <set>
00029 
00030 // Forward declaration
00031 template <class S, class ARG> class Observer;
00032 
00033 
00034 /// Template for subjects in the Observer design pattern
00035 template <class S, class ARG = void> class Subject
00036 {
00037     public:
00038         virtual ~Subject() {}
00039 
00040         /// Remove all observers; should only be used for debugging purposes
00041         virtual void clearObservers()
00042         {
00043             m_observers.clear();
00044         }
00045 
00046         /// Add an observer to this subject
00047         /// Note: adding twice the same observer is not harmful
00048         virtual void addObserver( Observer<S, ARG>* pObserver )
00049         {
00050             m_observers.insert( pObserver );
00051         }
00052 
00053         /// Remove an observer from this subject
00054         /// Note: removing twice the same observer is not harmful
00055         virtual void delObserver( Observer<S, ARG>* pObserver )
00056         {
00057             m_observers.erase( pObserver );
00058         }
00059 
00060         /// Notify the observers when the status has changed
00061         virtual void notify( ARG *arg )
00062         {
00063             // This stupid gcc 3.2 needs "typename"
00064             typename set<Observer<S, ARG>*>::const_iterator iter;
00065             for( iter = m_observers.begin(); iter != m_observers.end();
00066                  iter++ )
00067             {
00068                 if( *iter == NULL )
00069                 {
00070                     fprintf( stderr, "iter NULL !\n" );
00071                     return;
00072                 }
00073                 (*iter)->onUpdate( *this , arg );
00074             }
00075         }
00076 
00077         /// Notify without any argument
00078         virtual void notify() { notify( NULL ); }
00079 
00080     protected:
00081         Subject() {}
00082 
00083     private:
00084         /// Set of observers for this subject
00085         set<Observer<S, ARG>*> m_observers;
00086 };
00087 
00088 
00089 /// Template for observers in the Observer design pattern
00090 template <class S, class ARG = void> class Observer
00091 {
00092     public:
00093         virtual ~Observer() {}
00094 
00095         /// Method called when the subject is modified
00096         virtual void onUpdate( Subject<S, ARG> &rSubject, ARG *arg) = 0;
00097 
00098     protected:
00099         Observer() {}
00100 };
00101 
00102 
00103 #endif

Generated on Wed Aug 13 08:02:38 2008 for VLC by  doxygen 1.5.1