1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_OBSERVER_LIST_H__
6 #define BASE_OBSERVER_LIST_H__
12 #include "base/basictypes.h"
13 #include "base/logging.h"
14 #include "base/memory/weak_ptr.h"
16 ///////////////////////////////////////////////////////////////////////////////
20 // A container for a list of observers. Unlike a normal STL vector or list,
21 // this container can be modified during iteration without invalidating the
22 // iterator. So, it safely handles the case of an observer removing itself
23 // or other observers from the list while observers are being notified.
33 // virtual void OnFoo(MyWidget* w) = 0;
34 // virtual void OnBar(MyWidget* w, int x, int y) = 0;
37 // void AddObserver(Observer* obs) {
38 // observer_list_.AddObserver(obs);
41 // void RemoveObserver(Observer* obs) {
42 // observer_list_.RemoveObserver(obs);
46 // FOR_EACH_OBSERVER(Observer, observer_list_, OnFoo(this));
49 // void NotifyBar(int x, int y) {
50 // FOR_EACH_OBSERVER(Observer, observer_list_, OnBar(this, x, y));
54 // ObserverList<Observer> observer_list_;
58 ///////////////////////////////////////////////////////////////////////////////
60 template <typename ObserverType
>
61 class ObserverListThreadSafe
;
63 template <class ObserverType
>
64 class ObserverListBase
65 : public base::SupportsWeakPtr
<ObserverListBase
<ObserverType
> > {
67 // Enumeration of which observers are notified.
68 enum NotificationType
{
69 // Specifies that any observers added during notification are notified.
70 // This is the default type if non type is provided to the constructor.
73 // Specifies that observers added while sending out notification are not
78 // An iterator class that can be used to access the list of observers. See
79 // also the FOR_EACH_OBSERVER macro defined below.
82 explicit Iterator(ObserverListBase
<ObserverType
>* list
);
84 ObserverType
* GetNext();
87 base::WeakPtr
<ObserverListBase
<ObserverType
> > list_
;
92 ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL
) {}
93 explicit ObserverListBase(NotificationType type
)
94 : notify_depth_(0), type_(type
) {}
96 // Add an observer to the list. An observer should not be added to
97 // the same list more than once.
98 void AddObserver(ObserverType
* obs
);
100 // Remove an observer from the list if it is in the list.
101 void RemoveObserver(ObserverType
* obs
);
103 // Determine whether a particular observer is in the list.
104 bool HasObserver(const ObserverType
* observer
) const;
109 size_t size() const { return observers_
.size(); }
114 friend class ObserverListThreadSafe
<ObserverType
>;
116 typedef std::vector
<ObserverType
*> ListType
;
120 NotificationType type_
;
122 friend class ObserverListBase::Iterator
;
124 DISALLOW_COPY_AND_ASSIGN(ObserverListBase
);
127 template <class ObserverType
>
128 ObserverListBase
<ObserverType
>::Iterator::Iterator(
129 ObserverListBase
<ObserverType
>* list
)
130 : list_(list
->AsWeakPtr()),
132 max_index_(list
->type_
== NOTIFY_ALL
? std::numeric_limits
<size_t>::max()
133 : list
->observers_
.size()) {
134 ++list_
->notify_depth_
;
137 template <class ObserverType
>
138 ObserverListBase
<ObserverType
>::Iterator::~Iterator() {
139 if (list_
.get() && --list_
->notify_depth_
== 0)
143 template <class ObserverType
>
144 ObserverType
* ObserverListBase
<ObserverType
>::Iterator::GetNext() {
147 ListType
& observers
= list_
->observers_
;
148 // Advance if the current element is null
149 size_t max_index
= std::min(max_index_
, observers
.size());
150 while (index_
< max_index
&& !observers
[index_
])
152 return index_
< max_index
? observers
[index_
++] : NULL
;
155 template <class ObserverType
>
156 void ObserverListBase
<ObserverType
>::AddObserver(ObserverType
* obs
) {
157 if (std::find(observers_
.begin(), observers_
.end(), obs
)
158 != observers_
.end()) {
159 NOTREACHED() << "Observers can only be added once!";
162 observers_
.push_back(obs
);
165 template <class ObserverType
>
166 void ObserverListBase
<ObserverType
>::RemoveObserver(ObserverType
* obs
) {
167 typename
ListType::iterator it
=
168 std::find(observers_
.begin(), observers_
.end(), obs
);
169 if (it
!= observers_
.end()) {
173 observers_
.erase(it
);
178 template <class ObserverType
>
179 bool ObserverListBase
<ObserverType
>::HasObserver(
180 const ObserverType
* observer
) const {
181 for (size_t i
= 0; i
< observers_
.size(); ++i
) {
182 if (observers_
[i
] == observer
)
188 template <class ObserverType
>
189 void ObserverListBase
<ObserverType
>::Clear() {
191 for (typename
ListType::iterator it
= observers_
.begin();
192 it
!= observers_
.end(); ++it
) {
200 template <class ObserverType
>
201 void ObserverListBase
<ObserverType
>::Compact() {
203 std::remove(observers_
.begin(), observers_
.end(),
204 static_cast<ObserverType
*>(NULL
)), observers_
.end());
207 template <class ObserverType
, bool check_empty
= false>
208 class ObserverList
: public ObserverListBase
<ObserverType
> {
210 typedef typename ObserverListBase
<ObserverType
>::NotificationType
214 explicit ObserverList(NotificationType type
)
215 : ObserverListBase
<ObserverType
>(type
) {}
218 // When check_empty is true, assert that the list is empty on destruction.
220 ObserverListBase
<ObserverType
>::Compact();
221 DCHECK_EQ(ObserverListBase
<ObserverType
>::size(), 0U);
225 bool might_have_observers() const {
226 return ObserverListBase
<ObserverType
>::size() != 0;
230 #define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \
232 if ((observer_list).might_have_observers()) { \
233 ObserverListBase<ObserverType>::Iterator it_inside_observer_macro( \
236 while ((obs = it_inside_observer_macro.GetNext()) != NULL) \
241 #endif // BASE_OBSERVER_LIST_H__