ScummVM API documentation
observable.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef ULTIMA4_CORE_OBSERVABLE_H
23 #define ULTIMA4_CORE_OBSERVABLE_H
24 
25 #include "ultima/ultima4/core/observer.h"
26 #include "ultima/shared/std/containers.h"
27 
28 namespace Ultima {
29 namespace Ultima4 {
30 
44 template <class O, class A = NoArg *>
45 class Observable {
46 public:
47  Observable() : _changed(false) {}
48 
49  void addObserver(Observer<O, A> *o) {
50  typename Std::vector< Observer<O, A> *>::iterator i;
51  i = Common::find(_observers.begin(), _observers.end(), o);
52  if (i == _observers.end())
53  _observers.push_back(o);
54  }
55 
56  int countObservers() const {
57  return _observers.size();
58  }
59 
60  void deleteObserver(Observer<O, A> *o) {
61  typename Std::vector< Observer<O, A> *>::iterator i;
62  i = Common::find(_observers.begin(), _observers.end(), o);
63  if (i != _observers.end())
64  _observers.erase(i);
65  }
66 
67  void deleteObservers() {
68  _observers.clear();
69  }
70 
71  bool hasChanged() const {
72  return _changed;
73  }
74 
75  void notifyObservers(A arg) {
76  if (!_changed)
77  return;
78 
79  // vector iterators are invalidated if erase is called, so a copy
80  // is used to prevent problems if the observer removes itself (or
81  // otherwise changes the observer list)
82  typename Std::vector< Observer<O, A> *> tmp = _observers;
83  typename Std::vector< Observer<O, A> *>::iterator i;
84 
85  clearChanged();
86 
87  for (i = tmp.begin(); i != tmp.end(); i++) {
88  Observer<O, A> *observer = *i;
89  observer->update(static_cast<O>(this), arg);
90  }
91  }
92 
93 protected:
94  void clearChanged() {
95  _changed = false;
96  }
97  void setChanged() {
98  _changed = true;
99  }
100 
101 private:
102  bool _changed;
103  Std::vector< Observer<O, A> *> _observers;
104 };
105 
106 } // End of namespace Ultima4
107 } // End of namespace Ultima
108 
109 #endif
Definition: observer.h:37
iterator end()
Definition: array.h:379
iterator begin()
Definition: array.h:374
In find(In first, In last, const T &v)
Definition: algorithm.h:225
Definition: detection.h:27
Definition: observable.h:45
Definition: containers.h:38