1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
30 // Forward declaration
31 template <class S, class ARG> class Observer;
34 /// Template for subjects in the Observer design pattern
35 template <class S, class ARG = void> class Subject
38 typedef std::set<Observer<S, ARG>*> observers_t;
44 /// Remove all observers; should only be used for debugging purposes
51 /// Add an observer to this subject. Ignore NULL observers.
52 /// Note: adding the same observer twice is not harmful.
53 void addObserver( Observer<S, ARG>* pObserver )
55 if( pObserver ) m_observers.insert( pObserver );
58 /// Remove an observer from this subject. Ignore NULL observers.
59 /// Note: removing the same observer twice is not harmful.
60 void delObserver( Observer<S, ARG>* pObserver )
62 if( pObserver ) m_observers.erase( pObserver );
65 /// Notify the observers when the status has changed
66 void notify( ARG *arg )
68 typename observers_t::const_iterator iter;
69 for( iter = m_observers.begin(); iter != m_observers.end(); ++iter )
70 (*iter)->onUpdate( *this , arg );
73 /// Notify without any argument
74 void notify() { notify( NULL ); }
80 /// Set of observers for this subject
81 observers_t m_observers;
85 /// Template for observers in the Observer design pattern
86 template <class S, class ARG = void> class Observer
89 virtual ~Observer() { }
91 /// Method called when the subject is modified
92 virtual void onUpdate( Subject<S, ARG> &rSubject, ARG *arg) = 0;