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
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 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> class Subject
40 /// Remove all observers; should only be used for debugging purposes
41 virtual void clearObservers()
46 /// Add an observer to this subject
47 /// Note: adding twice the same observer is not harmful
48 virtual void addObserver( Observer<S, ARG>* pObserver )
50 m_observers.insert( pObserver );
53 /// Remove an observer from this subject
54 /// Note: removing twice the same observer is not harmful
55 virtual void delObserver( Observer<S, ARG>* pObserver )
57 m_observers.erase( pObserver );
60 /// Notify the observers when the status has changed
61 virtual void notify( ARG arg )
63 // This stupid gcc 3.2 needs "typename"
64 typename set<Observer<S, ARG>*>::const_iterator iter;
65 for( iter = m_observers.begin(); iter != m_observers.end();
70 fprintf( stderr, "iter NULL !\n" );
73 (*iter)->onUpdate( *this , arg );
77 /// Notify without any argument
78 virtual void notify() { notify( NULL ); }
84 /// Set of observers for this subject
85 set<Observer<S, ARG>*> m_observers;
89 /// Template for observers in the Observer design pattern
90 template <class S, class ARG> class Observer
93 virtual ~Observer() {}
95 /// Method called when the subject is modified
96 virtual void onUpdate( Subject<S,ARG> &rSubject , ARG arg) = 0;