2
// Copyright (c) 2002 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot org)
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:
11
// The above copyright notice and this permission notice shall be included in
12
// all copies or substantial portions of the Software.
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.
22
// $Id: Subject.cc 3864 2005-01-24 18:02:34Z mathias $
25
#include "Observer.hh"
32
Subject::SubjectList Subject::s_subjectlist;
34
Subject::Subject():m_notify_mode(false) {
35
s_subjectlist.push_back(this);
39
s_subjectlist.erase(std::remove(s_subjectlist.begin(),
40
s_subjectlist.end(), this));
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());
50
void Subject::detach(Observer *obj) {
52
m_dead_observers.push_back(obj);
54
m_observerlist.erase(std::remove(m_observerlist.begin(),
55
m_observerlist.end(), obj),
56
m_observerlist.end());
60
void Subject::notify() {
62
std::for_each(m_observerlist.begin(), m_observerlist.end(),
63
std::bind2nd(std::mem_fun(&Observer::update), this));
64
m_notify_mode = false;
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();
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));
81
}; // end namespace FbTk