1 | // |
---|
2 | // DefaultStrategy.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/DefaultStrategy.h#3 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Events |
---|
8 | // Module: DefaultStrategy |
---|
9 | // |
---|
10 | // Implementation of the DefaultStrategy template. |
---|
11 | // |
---|
12 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_DefaultStrategy_INCLUDED |
---|
40 | #define Foundation_DefaultStrategy_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/NotificationStrategy.h" |
---|
44 | #include <memory> |
---|
45 | #include <set> |
---|
46 | #include <vector> |
---|
47 | |
---|
48 | |
---|
49 | namespace Poco { |
---|
50 | |
---|
51 | |
---|
52 | template <class TArgs, class TDelegate, class TCompare> |
---|
53 | class DefaultStrategy: public NotificationStrategy<TArgs, TDelegate> |
---|
54 | /// Default notification strategy. Allows one observer |
---|
55 | /// to register exactly once. The observer must provide an |
---|
56 | /// < (less-than) operator. |
---|
57 | { |
---|
58 | public: |
---|
59 | typedef std::set<TDelegate*, TCompare> Delegates; |
---|
60 | typedef typename Delegates::iterator Iterator; |
---|
61 | typedef typename Delegates::const_iterator ConstIterator; |
---|
62 | |
---|
63 | public: |
---|
64 | DefaultStrategy() |
---|
65 | { |
---|
66 | } |
---|
67 | |
---|
68 | DefaultStrategy(const DefaultStrategy& s) |
---|
69 | { |
---|
70 | operator = (s); |
---|
71 | } |
---|
72 | |
---|
73 | ~DefaultStrategy() |
---|
74 | { |
---|
75 | clear(); |
---|
76 | } |
---|
77 | |
---|
78 | void notify(const void* sender, TArgs& arguments) |
---|
79 | { |
---|
80 | std::vector<Iterator> delMe; |
---|
81 | |
---|
82 | for (Iterator it = _observers.begin(); it != _observers.end(); ++it) |
---|
83 | { |
---|
84 | if (!(*it)->notify(sender, arguments)) |
---|
85 | { |
---|
86 | // schedule for deletion |
---|
87 | delMe.push_back(it); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | while (!delMe.empty()) |
---|
92 | { |
---|
93 | typename std::vector<Iterator>::iterator vit = delMe.end(); |
---|
94 | --vit; |
---|
95 | delete **vit; |
---|
96 | _observers.erase(*vit); |
---|
97 | delMe.pop_back(); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | void add(const TDelegate& delegate) |
---|
102 | { |
---|
103 | Iterator it = _observers.find(const_cast<TDelegate*>(&delegate)); |
---|
104 | if (it != _observers.end()) |
---|
105 | { |
---|
106 | delete *it; |
---|
107 | _observers.erase(it); |
---|
108 | } |
---|
109 | std::auto_ptr<TDelegate> pDelegate(delegate.clone()); |
---|
110 | bool tmp = _observers.insert(pDelegate.get()).second; |
---|
111 | poco_assert (tmp); |
---|
112 | pDelegate.release(); |
---|
113 | } |
---|
114 | |
---|
115 | void remove(const TDelegate& delegate) |
---|
116 | { |
---|
117 | Iterator it = _observers.find(const_cast<TDelegate*>(&delegate)); |
---|
118 | if (it != _observers.end()) |
---|
119 | { |
---|
120 | delete *it; |
---|
121 | _observers.erase(it); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | DefaultStrategy& operator = (const DefaultStrategy& s) |
---|
126 | { |
---|
127 | if (this != &s) |
---|
128 | { |
---|
129 | for (ConstIterator it = s._observers.begin(); it != s._observers.end(); ++it) |
---|
130 | { |
---|
131 | add(**it); |
---|
132 | } |
---|
133 | } |
---|
134 | return *this; |
---|
135 | } |
---|
136 | |
---|
137 | void clear() |
---|
138 | { |
---|
139 | for (Iterator it = _observers.begin(); it != _observers.end(); ++it) |
---|
140 | { |
---|
141 | delete *it; |
---|
142 | } |
---|
143 | _observers.clear(); |
---|
144 | } |
---|
145 | |
---|
146 | bool empty() const |
---|
147 | { |
---|
148 | return _observers.empty(); |
---|
149 | } |
---|
150 | |
---|
151 | protected: |
---|
152 | Delegates _observers; |
---|
153 | }; |
---|
154 | |
---|
155 | |
---|
156 | } // namespace Poco |
---|
157 | |
---|
158 | |
---|
159 | #endif |
---|