Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_OBSERVER_LIST_H__ |
michael@0 | 6 | #define BASE_OBSERVER_LIST_H__ |
michael@0 | 7 | |
michael@0 | 8 | #include <algorithm> |
michael@0 | 9 | #include <limits> |
michael@0 | 10 | #include <vector> |
michael@0 | 11 | |
michael@0 | 12 | #include "base/basictypes.h" |
michael@0 | 13 | #include "base/logging.h" |
michael@0 | 14 | #include "base/memory/weak_ptr.h" |
michael@0 | 15 | |
michael@0 | 16 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 17 | // |
michael@0 | 18 | // OVERVIEW: |
michael@0 | 19 | // |
michael@0 | 20 | // A container for a list of observers. Unlike a normal STL vector or list, |
michael@0 | 21 | // this container can be modified during iteration without invalidating the |
michael@0 | 22 | // iterator. So, it safely handles the case of an observer removing itself |
michael@0 | 23 | // or other observers from the list while observers are being notified. |
michael@0 | 24 | // |
michael@0 | 25 | // TYPICAL USAGE: |
michael@0 | 26 | // |
michael@0 | 27 | // class MyWidget { |
michael@0 | 28 | // public: |
michael@0 | 29 | // ... |
michael@0 | 30 | // |
michael@0 | 31 | // class Observer { |
michael@0 | 32 | // public: |
michael@0 | 33 | // virtual void OnFoo(MyWidget* w) = 0; |
michael@0 | 34 | // virtual void OnBar(MyWidget* w, int x, int y) = 0; |
michael@0 | 35 | // }; |
michael@0 | 36 | // |
michael@0 | 37 | // void AddObserver(Observer* obs) { |
michael@0 | 38 | // observer_list_.AddObserver(obs); |
michael@0 | 39 | // } |
michael@0 | 40 | // |
michael@0 | 41 | // void RemoveObserver(Observer* obs) { |
michael@0 | 42 | // observer_list_.RemoveObserver(obs); |
michael@0 | 43 | // } |
michael@0 | 44 | // |
michael@0 | 45 | // void NotifyFoo() { |
michael@0 | 46 | // FOR_EACH_OBSERVER(Observer, observer_list_, OnFoo(this)); |
michael@0 | 47 | // } |
michael@0 | 48 | // |
michael@0 | 49 | // void NotifyBar(int x, int y) { |
michael@0 | 50 | // FOR_EACH_OBSERVER(Observer, observer_list_, OnBar(this, x, y)); |
michael@0 | 51 | // } |
michael@0 | 52 | // |
michael@0 | 53 | // private: |
michael@0 | 54 | // ObserverList<Observer> observer_list_; |
michael@0 | 55 | // }; |
michael@0 | 56 | // |
michael@0 | 57 | // |
michael@0 | 58 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 59 | |
michael@0 | 60 | template <typename ObserverType> |
michael@0 | 61 | class ObserverListThreadSafe; |
michael@0 | 62 | |
michael@0 | 63 | template <class ObserverType> |
michael@0 | 64 | class ObserverListBase |
michael@0 | 65 | : public base::SupportsWeakPtr<ObserverListBase<ObserverType> > { |
michael@0 | 66 | public: |
michael@0 | 67 | // Enumeration of which observers are notified. |
michael@0 | 68 | enum NotificationType { |
michael@0 | 69 | // Specifies that any observers added during notification are notified. |
michael@0 | 70 | // This is the default type if non type is provided to the constructor. |
michael@0 | 71 | NOTIFY_ALL, |
michael@0 | 72 | |
michael@0 | 73 | // Specifies that observers added while sending out notification are not |
michael@0 | 74 | // notified. |
michael@0 | 75 | NOTIFY_EXISTING_ONLY |
michael@0 | 76 | }; |
michael@0 | 77 | |
michael@0 | 78 | // An iterator class that can be used to access the list of observers. See |
michael@0 | 79 | // also the FOR_EACH_OBSERVER macro defined below. |
michael@0 | 80 | class Iterator { |
michael@0 | 81 | public: |
michael@0 | 82 | Iterator(ObserverListBase<ObserverType>& list) |
michael@0 | 83 | : list_(list.AsWeakPtr()), |
michael@0 | 84 | index_(0), |
michael@0 | 85 | max_index_(list.type_ == NOTIFY_ALL ? |
michael@0 | 86 | std::numeric_limits<size_t>::max() : |
michael@0 | 87 | list.observers_.size()) { |
michael@0 | 88 | ++list_->notify_depth_; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | ~Iterator() { |
michael@0 | 92 | if (list_.get() && --list_->notify_depth_ == 0) |
michael@0 | 93 | list_->Compact(); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | ObserverType* GetNext() { |
michael@0 | 97 | if (!list_.get()) |
michael@0 | 98 | return NULL; |
michael@0 | 99 | ListType& observers = list_->observers_; |
michael@0 | 100 | // Advance if the current element is null |
michael@0 | 101 | size_t max_index = std::min(max_index_, observers.size()); |
michael@0 | 102 | while (index_ < max_index && !observers[index_]) |
michael@0 | 103 | ++index_; |
michael@0 | 104 | return index_ < max_index ? observers[index_++] : NULL; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | private: |
michael@0 | 108 | base::WeakPtr<ObserverListBase<ObserverType> > list_; |
michael@0 | 109 | size_t index_; |
michael@0 | 110 | size_t max_index_; |
michael@0 | 111 | }; |
michael@0 | 112 | |
michael@0 | 113 | ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {} |
michael@0 | 114 | explicit ObserverListBase(NotificationType type) |
michael@0 | 115 | : notify_depth_(0), type_(type) {} |
michael@0 | 116 | |
michael@0 | 117 | // Add an observer to the list. An observer should not be added to |
michael@0 | 118 | // the same list more than once. |
michael@0 | 119 | void AddObserver(ObserverType* obs) { |
michael@0 | 120 | if (std::find(observers_.begin(), observers_.end(), obs) |
michael@0 | 121 | != observers_.end()) { |
michael@0 | 122 | NOTREACHED() << "Observers can only be added once!"; |
michael@0 | 123 | return; |
michael@0 | 124 | } |
michael@0 | 125 | observers_.push_back(obs); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | // Remove an observer from the list if it is in the list. |
michael@0 | 129 | void RemoveObserver(ObserverType* obs) { |
michael@0 | 130 | typename ListType::iterator it = |
michael@0 | 131 | std::find(observers_.begin(), observers_.end(), obs); |
michael@0 | 132 | if (it != observers_.end()) { |
michael@0 | 133 | if (notify_depth_) { |
michael@0 | 134 | *it = 0; |
michael@0 | 135 | } else { |
michael@0 | 136 | observers_.erase(it); |
michael@0 | 137 | } |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | bool HasObserver(ObserverType* observer) const { |
michael@0 | 142 | for (size_t i = 0; i < observers_.size(); ++i) { |
michael@0 | 143 | if (observers_[i] == observer) |
michael@0 | 144 | return true; |
michael@0 | 145 | } |
michael@0 | 146 | return false; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | void Clear() { |
michael@0 | 150 | if (notify_depth_) { |
michael@0 | 151 | for (typename ListType::iterator it = observers_.begin(); |
michael@0 | 152 | it != observers_.end(); ++it) { |
michael@0 | 153 | *it = 0; |
michael@0 | 154 | } |
michael@0 | 155 | } else { |
michael@0 | 156 | observers_.clear(); |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | protected: |
michael@0 | 161 | size_t size() const { return observers_.size(); } |
michael@0 | 162 | |
michael@0 | 163 | void Compact() { |
michael@0 | 164 | observers_.erase( |
michael@0 | 165 | std::remove(observers_.begin(), observers_.end(), |
michael@0 | 166 | static_cast<ObserverType*>(NULL)), observers_.end()); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | private: |
michael@0 | 170 | friend class ObserverListThreadSafe<ObserverType>; |
michael@0 | 171 | |
michael@0 | 172 | typedef std::vector<ObserverType*> ListType; |
michael@0 | 173 | |
michael@0 | 174 | ListType observers_; |
michael@0 | 175 | int notify_depth_; |
michael@0 | 176 | NotificationType type_; |
michael@0 | 177 | |
michael@0 | 178 | friend class ObserverListBase::Iterator; |
michael@0 | 179 | |
michael@0 | 180 | DISALLOW_COPY_AND_ASSIGN(ObserverListBase); |
michael@0 | 181 | }; |
michael@0 | 182 | |
michael@0 | 183 | template <class ObserverType, bool check_empty = false> |
michael@0 | 184 | class ObserverList : public ObserverListBase<ObserverType> { |
michael@0 | 185 | public: |
michael@0 | 186 | typedef typename ObserverListBase<ObserverType>::NotificationType |
michael@0 | 187 | NotificationType; |
michael@0 | 188 | |
michael@0 | 189 | ObserverList() {} |
michael@0 | 190 | explicit ObserverList(NotificationType type) |
michael@0 | 191 | : ObserverListBase<ObserverType>(type) {} |
michael@0 | 192 | |
michael@0 | 193 | ~ObserverList() { |
michael@0 | 194 | // When check_empty is true, assert that the list is empty on destruction. |
michael@0 | 195 | if (check_empty) { |
michael@0 | 196 | ObserverListBase<ObserverType>::Compact(); |
michael@0 | 197 | DCHECK_EQ(ObserverListBase<ObserverType>::size(), 0U); |
michael@0 | 198 | } |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | bool might_have_observers() const { |
michael@0 | 202 | return ObserverListBase<ObserverType>::size() != 0; |
michael@0 | 203 | } |
michael@0 | 204 | }; |
michael@0 | 205 | |
michael@0 | 206 | #define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \ |
michael@0 | 207 | do { \ |
michael@0 | 208 | if ((observer_list).might_have_observers()) { \ |
michael@0 | 209 | ObserverListBase<ObserverType>::Iterator \ |
michael@0 | 210 | it_inside_observer_macro(observer_list); \ |
michael@0 | 211 | ObserverType* obs; \ |
michael@0 | 212 | while ((obs = it_inside_observer_macro.GetNext()) != NULL) \ |
michael@0 | 213 | obs->func; \ |
michael@0 | 214 | } \ |
michael@0 | 215 | } while (0) |
michael@0 | 216 | |
michael@0 | 217 | #endif // BASE_OBSERVER_LIST_H__ |