~ubuntu-branches/ubuntu/precise/fluxbox/precise

« back to all changes in this revision

Viewing changes to src/FbTk/Subject.cc

  • Committer: Bazaar Package Importer
  • Author(s): Dmitry E. Oboukhov
  • Date: 2008-07-01 10:38:14 UTC
  • mfrom: (2.1.12 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080701103814-khx2b6il152x9p93
Tags: 1.0.0+deb1-8
* x-dev has been removed from build-depends (out-of-date package).
* Standards-Version bumped to 3.8.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Subject.cc for FbTk
2
 
// Copyright (c) 2002 - 2005 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
 
// $Id: Subject.cc 3864 2005-01-24 18:02:34Z mathias $
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