]> git.sesse.net Git - vlc/blob - modules/gui/skins2/utils/observer.hpp
Uniformize source files encoding
[vlc] / modules / gui / skins2 / utils / observer.hpp
1 /*****************************************************************************
2  * observer.hpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifndef OBSERVER_HPP
26 #define OBSERVER_HPP
27
28 #include <set>
29
30 // Forward declaration
31 template <class S, class ARG> class Observer;
32
33
34 /// Template for subjects in the Observer design pattern
35 template <class S, class ARG> class Subject
36 {
37     public:
38         virtual ~Subject() {}
39
40         /// Remove all observers; should only be used for debugging purposes
41         virtual void clearObservers()
42         {
43             m_observers.clear();
44         }
45
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 )
49         {
50             m_observers.insert( pObserver );
51         }
52
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 )
56         {
57             m_observers.erase( pObserver );
58         }
59
60         /// Notify the observers when the status has changed
61         virtual void notify( ARG arg )
62         {
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();
66                  iter++ )
67             {
68                 if( *iter == NULL )
69                 {
70                     fprintf( stderr, "iter NULL !\n" );
71                     return;
72                 }
73                 (*iter)->onUpdate( *this , arg );
74             }
75         }
76
77         /// Notify without any argument
78         virtual void notify() { notify( NULL ); }
79
80     protected:
81         Subject() {}
82
83     private:
84         /// Set of observers for this subject
85         set<Observer<S, ARG>*> m_observers;
86 };
87
88
89 /// Template for observers in the Observer design pattern
90 template <class S, class ARG> class Observer
91 {
92     public:
93         virtual ~Observer() {}
94
95         /// Method called when the subject is modified
96         virtual void onUpdate( Subject<S,ARG> &rSubject , ARG arg) = 0;
97
98     protected:
99         Observer() {}
100 };
101
102
103 #endif