observer.hpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * observer.hpp
00003  *****************************************************************************
00004  * Copyright (C) 2003 the VideoLAN team
00005  * $Id: 54f1755420abd8eb943c570b57ea8cc77253e5f2 $
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 along
00021  * with this program; if not, write to the Free Software Foundation, Inc.,
00022  * 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 private:
00038     typedef std::set<Observer<S, ARG>*> observers_t;
00039 
00040 public:
00041     ~Subject() { }
00042 
00043 #if 0
00044     /// Remove all observers; should only be used for debugging purposes
00045     void clearObservers()
00046     {
00047         m_observers.clear();
00048     }
00049 #endif
00050 
00051     /// Add an observer to this subject. Ignore NULL observers.
00052     /// Note: adding the same observer twice is not harmful.
00053     void addObserver( Observer<S, ARG>* pObserver )
00054     {
00055         if( pObserver ) m_observers.insert( pObserver );
00056     }
00057 
00058     /// Remove an observer from this subject. Ignore NULL observers.
00059     /// Note: removing the same observer twice is not harmful.
00060     void delObserver( Observer<S, ARG>* pObserver )
00061     {
00062         if( pObserver ) m_observers.erase( pObserver );
00063     }
00064 
00065     /// Notify the observers when the status has changed
00066     void notify( ARG *arg )
00067     {
00068         typename observers_t::const_iterator iter;
00069         for( iter = m_observers.begin(); iter != m_observers.end(); ++iter )
00070             (*iter)->onUpdate( *this , arg );
00071     }
00072 
00073     /// Notify without any argument
00074     void notify() { notify( NULL ); }
00075 
00076 protected:
00077     Subject() { }
00078 
00079 private:
00080     /// Set of observers for this subject
00081     observers_t m_observers;
00082 };
00083 
00084 
00085 /// Template for observers in the Observer design pattern
00086 template <class S, class ARG = void> class Observer
00087 {
00088 public:
00089     virtual ~Observer() { }
00090 
00091     /// Method called when the subject is modified
00092     virtual void onUpdate( Subject<S, ARG> &rSubject, ARG *arg) = 0;
00093 
00094 protected:
00095     Observer() { }
00096 };
00097 
00098 
00099 #endif

Generated on Tue May 25 08:04:58 2010 for VLC by  doxygen 1.5.6