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 // base::ObserverList<Observer> observer_list_;
58 ///////////////////////////////////////////////////////////////////////////////
62 template <typename ObserverType
>
63 class ObserverListThreadSafe
;
65 template <class ObserverType
>
66 class ObserverListBase
67 : public SupportsWeakPtr
<ObserverListBase
<ObserverType
>> {
69 // Enumeration of which observers are notified.
70 enum NotificationType
{
71 // Specifies that any observers added during notification are notified.
72 // This is the default type if non type is provided to the constructor.
75 // Specifies that observers added while sending out notification are not
80 // An iterator class that can be used to access the list of observers. See
81 // also the FOR_EACH_OBSERVER macro defined below.
84 explicit Iterator(ObserverListBase
<ObserverType
>* list
);
86 ObserverType
* GetNext();
89 WeakPtr
<ObserverListBase
<ObserverType
>> list_
;
94 ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL
) {}
95 explicit ObserverListBase(NotificationType type
)
96 : notify_depth_(0), type_(type
) {}
98 // Add an observer to the list. An observer should not be added to
99 // the same list more than once.
100 void AddObserver(ObserverType
* obs
);
102 // Remove an observer from the list if it is in the list.
103 void RemoveObserver(ObserverType
* obs
);
105 // Determine whether a particular observer is in the list.
106 bool HasObserver(const ObserverType
* observer
) const;
111 size_t size() const { return observers_
.size(); }
116 friend class ObserverListThreadSafe
<ObserverType
>;
118 typedef std::vector
<ObserverType
*> ListType
;
122 NotificationType type_
;
124 friend class ObserverListBase::Iterator
;
126 DISALLOW_COPY_AND_ASSIGN(ObserverListBase
);
129 template <class ObserverType
>
130 ObserverListBase
<ObserverType
>::Iterator::Iterator(
131 ObserverListBase
<ObserverType
>* list
)
132 : list_(list
->AsWeakPtr()),
134 max_index_(list
->type_
== NOTIFY_ALL
? std::numeric_limits
<size_t>::max()
135 : list
->observers_
.size()) {
136 ++list_
->notify_depth_
;
139 template <class ObserverType
>
140 ObserverListBase
<ObserverType
>::Iterator::~Iterator() {
141 if (list_
.get() && --list_
->notify_depth_
== 0)
145 template <class ObserverType
>
146 ObserverType
* ObserverListBase
<ObserverType
>::Iterator::GetNext() {
149 ListType
& observers
= list_
->observers_
;
150 // Advance if the current element is null
151 size_t max_index
= std::min(max_index_
, observers
.size());
152 while (index_
< max_index
&& !observers
[index_
])
154 return index_
< max_index
? observers
[index_
++] : nullptr;
157 template <class ObserverType
>
158 void ObserverListBase
<ObserverType
>::AddObserver(ObserverType
* obs
) {
160 if (std::find(observers_
.begin(), observers_
.end(), obs
)
161 != observers_
.end()) {
162 NOTREACHED() << "Observers can only be added once!";
165 observers_
.push_back(obs
);
168 template <class ObserverType
>
169 void ObserverListBase
<ObserverType
>::RemoveObserver(ObserverType
* obs
) {
171 typename
ListType::iterator it
=
172 std::find(observers_
.begin(), observers_
.end(), obs
);
173 if (it
!= observers_
.end()) {
177 observers_
.erase(it
);
182 template <class ObserverType
>
183 bool ObserverListBase
<ObserverType
>::HasObserver(
184 const ObserverType
* observer
) const {
185 for (size_t i
= 0; i
< observers_
.size(); ++i
) {
186 if (observers_
[i
] == observer
)
192 template <class ObserverType
>
193 void ObserverListBase
<ObserverType
>::Clear() {
195 for (typename
ListType::iterator it
= observers_
.begin();
196 it
!= observers_
.end(); ++it
) {
204 template <class ObserverType
>
205 void ObserverListBase
<ObserverType
>::Compact() {
207 std::remove(observers_
.begin(), observers_
.end(), nullptr),
211 template <class ObserverType
, bool check_empty
= false>
212 class ObserverList
: public ObserverListBase
<ObserverType
> {
214 typedef typename ObserverListBase
<ObserverType
>::NotificationType
218 explicit ObserverList(NotificationType type
)
219 : ObserverListBase
<ObserverType
>(type
) {}
222 // When check_empty is true, assert that the list is empty on destruction.
224 ObserverListBase
<ObserverType
>::Compact();
225 DCHECK_EQ(ObserverListBase
<ObserverType
>::size(), 0U);
229 bool might_have_observers() const {
230 return ObserverListBase
<ObserverType
>::size() != 0;
234 #define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \
236 if ((observer_list).might_have_observers()) { \
237 base::ObserverListBase<ObserverType>::Iterator it_inside_observer_macro( \
240 while ((obs = it_inside_observer_macro.GetNext()) != nullptr) \
247 // TODO(brettw) remove this when callers use the correct namespace.
248 using base::ObserverList
;
250 #endif // BASE_OBSERVER_LIST_H_