diff options
Diffstat (limited to 'src/FbTk/Subject.cc')
-rw-r--r-- | src/FbTk/Subject.cc | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/FbTk/Subject.cc b/src/FbTk/Subject.cc new file mode 100644 index 0000000..a575cfd --- /dev/null +++ b/src/FbTk/Subject.cc | |||
@@ -0,0 +1,81 @@ | |||
1 | // Subject.cc for FbTk | ||
2 | // Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen<at>users.sourceforge.net) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | // $Id: Subject.cc,v 1.3 2003/09/08 15:38:46 fluxgen Exp $ | ||
23 | |||
24 | #include "Subject.hh" | ||
25 | #include "Observer.hh" | ||
26 | |||
27 | #include <algorithm> | ||
28 | #include <functional> | ||
29 | |||
30 | namespace FbTk { | ||
31 | |||
32 | Subject::SubjectList Subject::s_subjectlist; | ||
33 | |||
34 | Subject::Subject():m_notify_mode(false) { | ||
35 | s_subjectlist.push_back(this); | ||
36 | } | ||
37 | |||
38 | Subject::~Subject() { | ||
39 | s_subjectlist.erase(std::remove(s_subjectlist.begin(), | ||
40 | s_subjectlist.end(), this)); | ||
41 | } | ||
42 | |||
43 | void Subject::attach(Observer *obj) { | ||
44 | m_observerlist.push_back(obj); | ||
45 | // no need to have more than one instance of an observer | ||
46 | m_observerlist.erase(std::unique(m_observerlist.begin(), m_observerlist.end()), | ||
47 | m_observerlist.end()); | ||
48 | } | ||
49 | |||
50 | void Subject::detach(Observer *obj) { | ||
51 | if (m_notify_mode) | ||
52 | m_dead_observers.push_back(obj); | ||
53 | else { | ||
54 | m_observerlist.erase(std::remove(m_observerlist.begin(), | ||
55 | m_observerlist.end(), obj), | ||
56 | m_observerlist.end()); | ||
57 | } | ||
58 | } | ||
59 | |||
60 | void Subject::notify() { | ||
61 | m_notify_mode = true; | ||
62 | std::for_each(m_observerlist.begin(), m_observerlist.end(), | ||
63 | std::bind2nd(std::mem_fun(&Observer::update), this)); | ||
64 | m_notify_mode = false; | ||
65 | |||
66 | // remove dead observers | ||
67 | if (m_dead_observers.size()) { | ||
68 | std::for_each(m_dead_observers.begin(), | ||
69 | m_dead_observers.end(), | ||
70 | std::bind1st(std::mem_fun(&Subject::detach), this)); | ||
71 | m_dead_observers.clear(); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | void Subject::removeObserver(Observer *obj) { | ||
76 | std::for_each(s_subjectlist.begin(), s_subjectlist.end(), | ||
77 | std::bind2nd(std::mem_fun(&Subject::detach), obj)); | ||
78 | |||
79 | } | ||
80 | |||
81 | }; // end namespace FbTk | ||