]> git.sesse.net Git - vlc/blob - modules/gui/skins2/utils/observer.hpp
skins2: prefer ++it over it++ for iterators
[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 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  *****************************************************************************/
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 = void> class Subject
36 {
37 private:
38     typedef std::set<Observer<S, ARG>*> observers_t;
39
40 public:
41     ~Subject() { }
42
43 #if 0
44     /// Remove all observers; should only be used for debugging purposes
45     void clearObservers()
46     {
47         m_observers.clear();
48     }
49 #endif
50
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 )
54     {
55         if( pObserver ) m_observers.insert( pObserver );
56     }
57
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 )
61     {
62         if( pObserver ) m_observers.erase( pObserver );
63     }
64
65     /// Notify the observers when the status has changed
66     void notify( ARG *arg )
67     {
68         typename observers_t::const_iterator iter;
69         for( iter = m_observers.begin(); iter != m_observers.end(); ++iter )
70             (*iter)->onUpdate( *this , arg );
71     }
72
73     /// Notify without any argument
74     void notify() { notify( NULL ); }
75
76 protected:
77     Subject() { }
78
79 private:
80     /// Set of observers for this subject
81     observers_t m_observers;
82 };
83
84
85 /// Template for observers in the Observer design pattern
86 template <class S, class ARG = void> class Observer
87 {
88 public:
89     virtual ~Observer() { }
90
91     /// Method called when the subject is modified
92     virtual void onUpdate( Subject<S, ARG> &rSubject, ARG *arg) = 0;
93
94 protected:
95     Observer() { }
96 };
97
98
99 #endif