aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/Subject.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/Subject.cc')
-rw-r--r--src/FbTk/Subject.cc85
1 files changed, 0 insertions, 85 deletions
diff --git a/src/FbTk/Subject.cc b/src/FbTk/Subject.cc
deleted file mode 100644
index 594eb23..0000000
--- a/src/FbTk/Subject.cc
+++ /dev/null
@@ -1,85 +0,0 @@
1// Subject.cc for FbTk
2// Copyright (c) 2002 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
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#include "Subject.hh"
23#include "Observer.hh"
24
25#include <algorithm>
26#include <functional>
27
28namespace FbTk {
29
30Subject::SubjectList Subject::s_subjectlist;
31
32Subject::Subject():m_notify_mode(false) {
33 s_subjectlist.push_back(this);
34}
35
36Subject::~Subject() {
37 s_subjectlist.erase(std::remove(s_subjectlist.begin(),
38 s_subjectlist.end(), this));
39}
40
41void Subject::attach(Observer *obj) {
42 m_observerlist.push_back(obj);
43 // no need to have more than one instance of an observer
44 m_observerlist.erase(std::unique(m_observerlist.begin(), m_observerlist.end()),
45 m_observerlist.end());
46}
47
48void Subject::detach(Observer *obj) {
49 if (m_notify_mode)
50 m_dead_observers.push_back(obj);
51 else {
52 m_observerlist.erase(std::remove(m_observerlist.begin(),
53 m_observerlist.end(), obj),
54 m_observerlist.end());
55 }
56}
57
58void Subject::notify() {
59 ObserverList::iterator it = m_observerlist.begin(),
60 it_end = m_observerlist.end();
61 for (; it != it_end; ++it) {
62 m_notify_mode = true;
63 (*it)->update(this);
64 ObserverList::iterator d_it = m_dead_observers.begin(),
65 d_it_end = m_dead_observers.end();
66 m_notify_mode = false;
67
68 // there might be dead observers later in the list, so we must remove
69 // them now
70 for (; d_it != d_it_end; ++d_it) {
71 if (*d_it == *it)
72 --it; // don't invalidate our iterator
73 detach(*d_it);
74 }
75 m_dead_observers.clear();
76 }
77}
78
79void Subject::removeObserver(Observer *obj) {
80 std::for_each(s_subjectlist.begin(), s_subjectlist.end(),
81 std::bind2nd(std::mem_fun(&Subject::detach), obj));
82
83}
84
85} // end namespace FbTk