diff options
-rw-r--r-- | src/FbTk/Subject.cc | 41 |
1 files changed, 14 insertions, 27 deletions
diff --git a/src/FbTk/Subject.cc b/src/FbTk/Subject.cc index 9443112..961d6a8 100644 --- a/src/FbTk/Subject.cc +++ b/src/FbTk/Subject.cc | |||
@@ -1,5 +1,5 @@ | |||
1 | // Subject.cc for FbTk | 1 | // Subject.cc for FbTk |
2 | // Copyright (c) 2002 Henrik Kinnunen (fluxgen@fluxbox.org) | 2 | // Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen<at>users.sourceforge.net) |
3 | // | 3 | // |
4 | // Permission is hereby granted, free of charge, to any person obtaining a | 4 | // Permission is hereby granted, free of charge, to any person obtaining a |
5 | // copy of this software and associated documentation files (the "Software"), | 5 | // copy of this software and associated documentation files (the "Software"), |
@@ -19,7 +19,7 @@ | |||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. | 20 | // DEALINGS IN THE SOFTWARE. |
21 | 21 | ||
22 | // $Id: Subject.cc,v 1.1 2003/02/15 01:21:40 fluxgen Exp $ | 22 | // $Id: Subject.cc,v 1.2 2003/08/19 16:03:26 fluxgen Exp $ |
23 | 23 | ||
24 | #include "Subject.hh" | 24 | #include "Subject.hh" |
25 | #include "Observer.hh" | 25 | #include "Observer.hh" |
@@ -36,45 +36,32 @@ Subject::Subject() { | |||
36 | } | 36 | } |
37 | 37 | ||
38 | Subject::~Subject() { | 38 | Subject::~Subject() { |
39 | SubjectList::iterator it = s_subjectlist.begin(); | 39 | s_subjectlist.erase(std::remove(s_subjectlist.begin(), |
40 | SubjectList::iterator it_end = s_subjectlist.end(); | 40 | s_subjectlist.end(), this)); |
41 | for (; it != it_end; ++it) { | ||
42 | if (this == (*it)) { | ||
43 | s_subjectlist.erase(it); | ||
44 | break; | ||
45 | } | ||
46 | } | ||
47 | } | 41 | } |
48 | 42 | ||
49 | void Subject::attach(Observer *obj) { | 43 | void Subject::attach(Observer *obj) { |
50 | m_observerlist.push_back(obj); | 44 | m_observerlist.push_back(obj); |
51 | // no need to have more than one instance of an observer | 45 | // no need to have more than one instance of an observer |
52 | std::unique(m_observerlist.begin(), m_observerlist.end()); | 46 | m_observerlist.erase(std::unique(m_observerlist.begin(), m_observerlist.end()), |
47 | m_observerlist.end()); | ||
53 | } | 48 | } |
54 | 49 | ||
55 | void Subject::detach(Observer *obj) { | 50 | void Subject::detach(Observer *obj) { |
56 | ObserverList::iterator it = m_observerlist.begin(); | 51 | m_observerlist.erase(std::remove(m_observerlist.begin(), |
57 | ObserverList::iterator it_end = m_observerlist.end(); | 52 | m_observerlist.end(), obj), |
58 | for (; it != it_end; ++it) { | 53 | m_observerlist.end()); |
59 | if (obj == (*it)) { | ||
60 | m_observerlist.erase(it); | ||
61 | break; | ||
62 | } | ||
63 | } | ||
64 | } | 54 | } |
65 | 55 | ||
66 | void Subject::notify() { | 56 | void Subject::notify() { |
67 | ObserverList::iterator it = m_observerlist.begin(); | 57 | std::for_each(m_observerlist.begin(), m_observerlist.end(), |
68 | for (; it != m_observerlist.end(); ++it) { | 58 | std::bind2nd(std::mem_fun(&Observer::update), this)); |
69 | (*it)->update(this); | ||
70 | } | ||
71 | } | 59 | } |
72 | 60 | ||
73 | void Subject::removeObserver(Observer *obj) { | 61 | void Subject::removeObserver(Observer *obj) { |
74 | SubjectList::iterator it = s_subjectlist.begin(); | 62 | std::for_each(s_subjectlist.begin(), s_subjectlist.end(), |
75 | for(; it != s_subjectlist.end(); ++it) { | 63 | std::bind2nd(std::mem_fun(&Subject::detach), obj)); |
76 | (*it)->detach(obj); | 64 | |
77 | } | ||
78 | } | 65 | } |
79 | 66 | ||
80 | }; // end namespace FbTk | 67 | }; // end namespace FbTk |