1
// Copyright (c) 2010 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__
13
///////////////////////////////////////////////////////////////////////////////
17
// A container for a list of observers. Unlike a normal STL vector or list,
18
// this container can be modified during iteration without invalidating the
19
// iterator. So, it safely handles the case of an observer removing itself
20
// or other observers from the list while observers are being notified.
30
// virtual void OnFoo(MyWidget* w) = 0;
31
// virtual void OnBar(MyWidget* w, int x, int y) = 0;
34
// void AddObserver(Observer* obs) {
35
// observer_list_.AddObserver(obs);
38
// void RemoveObserver(Observer* obs) {
39
// observer_list_.RemoveObserver(obs);
43
// FOR_EACH_OBSERVER(Observer, observer_list_, OnFoo(this));
46
// void NotifyBar(int x, int y) {
47
// FOR_EACH_OBSERVER(Observer, observer_list_, OnBar(this, x, y));
51
// ObserverList<Observer> observer_list_;
55
///////////////////////////////////////////////////////////////////////////////
57
template <typename ObserverType>
58
class ObserverListThreadSafe;
60
template <class ObserverType>
61
class ObserverListBase {
63
// Enumeration of which observers are notified.
64
enum NotificationType {
65
// Specifies that any observers added during notification are notified.
66
// This is the default type if non type is provided to the constructor.
69
// Specifies that observers added while sending out notification are not
74
// An iterator class that can be used to access the list of observers. See
75
// also the FOR_EACH_OBSERVER macro defined below.
78
Iterator(ObserverListBase<ObserverType>& list)
81
max_index_(list.type_ == NOTIFY_ALL ?
82
std::numeric_limits<size_t>::max() :
83
list.observers_.size()) {
84
++list_.notify_depth_;
88
if (--list_.notify_depth_ == 0)
92
ObserverType* GetNext() {
93
ListType& observers = list_.observers_;
94
// Advance if the current element is null
95
size_t max_index = std::min(max_index_, observers.size());
96
while (index_ < max_index && !observers[index_])
98
return index_ < max_index ? observers[index_++] : NULL;
102
ObserverListBase<ObserverType>& list_;
107
ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {}
108
explicit ObserverListBase(NotificationType type)
109
: notify_depth_(0), type_(type) {}
111
// Add an observer to the list.
112
void AddObserver(ObserverType* obs) {
113
observers_.push_back(obs);
116
// Remove an observer from the list.
117
void RemoveObserver(ObserverType* obs) {
118
typename ListType::iterator it =
119
std::find(observers_.begin(), observers_.end(), obs);
120
if (it != observers_.end()) {
124
observers_.erase(it);
129
bool HasObserver(ObserverType* observer) const {
130
for (size_t i = 0; i < observers_.size(); ++i) {
131
if (observers_[i] == observer)
139
for (typename ListType::iterator it = observers_.begin();
140
it != observers_.end(); ++it) {
148
size_t size() const { return observers_.size(); }
152
typename ListType::iterator it = observers_.begin();
153
while (it != observers_.end()) {
157
it = observers_.erase(it);
163
friend class ObserverListThreadSafe<ObserverType>;
165
typedef std::vector<ObserverType*> ListType;
169
NotificationType type_;
171
friend class ObserverListBase::Iterator;
174
template <class ObserverType, bool check_empty = false>
175
class ObserverList : public ObserverListBase<ObserverType> {
177
typedef typename ObserverListBase<ObserverType>::NotificationType
181
explicit ObserverList(NotificationType type)
182
: ObserverListBase<ObserverType>(type) {}
185
// When check_empty is true, assert that the list is empty on destruction.
187
ObserverListBase<ObserverType>::Compact();
192
#define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \
194
ObserverListBase<ObserverType>::Iterator it(observer_list); \
196
while ((obs = it.GetNext()) != NULL) \
200
#endif // BASE_OBSERVER_LIST_H__