akalin@chromium.org | cbec5fb | 2011-06-05 16:07:12 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
| 5 | #ifndef BASE_OBSERVER_LIST_H__ |
| 6 | #define BASE_OBSERVER_LIST_H__ |
| 7 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 8 | #include <algorithm> |
sky@google.com | b92a56b | 2008-10-31 12:32:06 +0900 | [diff] [blame] | 9 | #include <limits> |
| 10 | #include <vector> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 11 | |
| 12 | #include "base/basictypes.h" |
| 13 | #include "base/logging.h" |
akalin@chromium.org | a7d2bc4 | 2011-06-09 12:41:18 +0900 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 15 | |
| 16 | /////////////////////////////////////////////////////////////////////////////// |
| 17 | // |
| 18 | // OVERVIEW: |
| 19 | // |
| 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. |
| 24 | // |
| 25 | // TYPICAL USAGE: |
| 26 | // |
| 27 | // class MyWidget { |
| 28 | // public: |
| 29 | // ... |
| 30 | // |
| 31 | // class Observer { |
| 32 | // public: |
| 33 | // virtual void OnFoo(MyWidget* w) = 0; |
| 34 | // virtual void OnBar(MyWidget* w, int x, int y) = 0; |
| 35 | // }; |
| 36 | // |
| 37 | // void AddObserver(Observer* obs) { |
| 38 | // observer_list_.AddObserver(obs); |
| 39 | // } |
| 40 | // |
| 41 | // void RemoveObserver(Observer* obs) { |
| 42 | // observer_list_.RemoveObserver(obs); |
| 43 | // } |
| 44 | // |
| 45 | // void NotifyFoo() { |
| 46 | // FOR_EACH_OBSERVER(Observer, observer_list_, OnFoo(this)); |
| 47 | // } |
| 48 | // |
| 49 | // void NotifyBar(int x, int y) { |
| 50 | // FOR_EACH_OBSERVER(Observer, observer_list_, OnBar(this, x, y)); |
| 51 | // } |
| 52 | // |
| 53 | // private: |
| 54 | // ObserverList<Observer> observer_list_; |
| 55 | // }; |
| 56 | // |
sky@google.com | b92a56b | 2008-10-31 12:32:06 +0900 | [diff] [blame] | 57 | // |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 58 | /////////////////////////////////////////////////////////////////////////////// |
| 59 | |
willchan@chromium.org | 57c5630 | 2010-02-25 12:09:41 +0900 | [diff] [blame] | 60 | template <typename ObserverType> |
| 61 | class ObserverListThreadSafe; |
| 62 | |
| 63 | template <class ObserverType> |
akalin@chromium.org | a7d2bc4 | 2011-06-09 12:41:18 +0900 | [diff] [blame] | 64 | class ObserverListBase |
| 65 | : public base::SupportsWeakPtr<ObserverListBase<ObserverType> > { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 66 | public: |
sky@google.com | b92a56b | 2008-10-31 12:32:06 +0900 | [diff] [blame] | 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. |
| 71 | NOTIFY_ALL, |
| 72 | |
| 73 | // Specifies that observers added while sending out notification are not |
| 74 | // notified. |
| 75 | NOTIFY_EXISTING_ONLY |
| 76 | }; |
| 77 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 78 | // An iterator class that can be used to access the list of observers. See |
willchan@chromium.org | 57c5630 | 2010-02-25 12:09:41 +0900 | [diff] [blame] | 79 | // also the FOR_EACH_OBSERVER macro defined below. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 80 | class Iterator { |
| 81 | public: |
bratell@opera.com | ddd442d | 2014-07-17 21:39:34 +0900 | [diff] [blame] | 82 | Iterator(ObserverListBase<ObserverType>& list); |
| 83 | ~Iterator(); |
| 84 | ObserverType* GetNext(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 85 | |
| 86 | private: |
akalin@chromium.org | a7d2bc4 | 2011-06-09 12:41:18 +0900 | [diff] [blame] | 87 | base::WeakPtr<ObserverListBase<ObserverType> > list_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 88 | size_t index_; |
sky@google.com | b92a56b | 2008-10-31 12:32:06 +0900 | [diff] [blame] | 89 | size_t max_index_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 90 | }; |
| 91 | |
willchan@chromium.org | 57c5630 | 2010-02-25 12:09:41 +0900 | [diff] [blame] | 92 | ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {} |
| 93 | explicit ObserverListBase(NotificationType type) |
| 94 | : notify_depth_(0), type_(type) {} |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 95 | |
akalin@chromium.org | cbec5fb | 2011-06-05 16:07:12 +0900 | [diff] [blame] | 96 | // Add an observer to the list. An observer should not be added to |
| 97 | // the same list more than once. |
bratell@opera.com | ddd442d | 2014-07-17 21:39:34 +0900 | [diff] [blame] | 98 | void AddObserver(ObserverType* obs); |
willchan@chromium.org | 57c5630 | 2010-02-25 12:09:41 +0900 | [diff] [blame] | 99 | |
akalin@chromium.org | cbec5fb | 2011-06-05 16:07:12 +0900 | [diff] [blame] | 100 | // Remove an observer from the list if it is in the list. |
bratell@opera.com | ddd442d | 2014-07-17 21:39:34 +0900 | [diff] [blame] | 101 | void RemoveObserver(ObserverType* obs); |
willchan@chromium.org | 57c5630 | 2010-02-25 12:09:41 +0900 | [diff] [blame] | 102 | |
bratell@opera.com | ddd442d | 2014-07-17 21:39:34 +0900 | [diff] [blame] | 103 | bool HasObserver(ObserverType* observer) const; |
willchan@chromium.org | 57c5630 | 2010-02-25 12:09:41 +0900 | [diff] [blame] | 104 | |
bratell@opera.com | ddd442d | 2014-07-17 21:39:34 +0900 | [diff] [blame] | 105 | void Clear(); |
willchan@chromium.org | 57c5630 | 2010-02-25 12:09:41 +0900 | [diff] [blame] | 106 | |
caitkp@chromium.org | 9eb60b7 | 2013-08-29 12:55:13 +0900 | [diff] [blame] | 107 | protected: |
pkasting@chromium.org | cebf2a3 | 2010-04-30 03:17:29 +0900 | [diff] [blame] | 108 | size_t size() const { return observers_.size(); } |
willchan@chromium.org | 57c5630 | 2010-02-25 12:09:41 +0900 | [diff] [blame] | 109 | |
bratell@opera.com | ddd442d | 2014-07-17 21:39:34 +0900 | [diff] [blame] | 110 | void Compact(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 111 | |
willchan@chromium.org | 57c5630 | 2010-02-25 12:09:41 +0900 | [diff] [blame] | 112 | private: |
| 113 | friend class ObserverListThreadSafe<ObserverType>; |
| 114 | |
| 115 | typedef std::vector<ObserverType*> ListType; |
| 116 | |
| 117 | ListType observers_; |
| 118 | int notify_depth_; |
sky@google.com | b92a56b | 2008-10-31 12:32:06 +0900 | [diff] [blame] | 119 | NotificationType type_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 120 | |
willchan@chromium.org | 57c5630 | 2010-02-25 12:09:41 +0900 | [diff] [blame] | 121 | friend class ObserverListBase::Iterator; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 122 | |
willchan@chromium.org | 57c5630 | 2010-02-25 12:09:41 +0900 | [diff] [blame] | 123 | DISALLOW_COPY_AND_ASSIGN(ObserverListBase); |
| 124 | }; |
| 125 | |
bratell@opera.com | ddd442d | 2014-07-17 21:39:34 +0900 | [diff] [blame] | 126 | template <class ObserverType> |
| 127 | ObserverListBase<ObserverType>::Iterator::Iterator( |
| 128 | ObserverListBase<ObserverType>& list) |
| 129 | : list_(list.AsWeakPtr()), |
| 130 | index_(0), |
| 131 | max_index_(list.type_ == NOTIFY_ALL ? |
| 132 | std::numeric_limits<size_t>::max() : |
| 133 | list.observers_.size()) { |
| 134 | ++list_->notify_depth_; |
| 135 | } |
| 136 | |
| 137 | template <class ObserverType> |
| 138 | ObserverListBase<ObserverType>::Iterator::~Iterator() { |
| 139 | if (list_.get() && --list_->notify_depth_ == 0) |
| 140 | list_->Compact(); |
| 141 | } |
| 142 | |
| 143 | template <class ObserverType> |
| 144 | ObserverType* ObserverListBase<ObserverType>::Iterator::GetNext() { |
| 145 | if (!list_.get()) |
| 146 | return NULL; |
| 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_]) |
| 151 | ++index_; |
| 152 | return index_ < max_index ? observers[index_++] : NULL; |
| 153 | } |
| 154 | |
| 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!"; |
| 160 | return; |
| 161 | } |
| 162 | observers_.push_back(obs); |
| 163 | } |
| 164 | |
| 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()) { |
| 170 | if (notify_depth_) { |
| 171 | *it = 0; |
| 172 | } else { |
| 173 | observers_.erase(it); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | template <class ObserverType> |
| 179 | bool ObserverListBase<ObserverType>::HasObserver(ObserverType* observer) const { |
| 180 | for (size_t i = 0; i < observers_.size(); ++i) { |
| 181 | if (observers_[i] == observer) |
| 182 | return true; |
| 183 | } |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | template <class ObserverType> |
| 188 | void ObserverListBase<ObserverType>::Clear() { |
| 189 | if (notify_depth_) { |
| 190 | for (typename ListType::iterator it = observers_.begin(); |
| 191 | it != observers_.end(); ++it) { |
| 192 | *it = 0; |
| 193 | } |
| 194 | } else { |
| 195 | observers_.clear(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | template <class ObserverType> |
| 200 | void ObserverListBase<ObserverType>::Compact() { |
| 201 | observers_.erase( |
| 202 | std::remove(observers_.begin(), observers_.end(), |
| 203 | static_cast<ObserverType*>(NULL)), observers_.end()); |
| 204 | } |
| 205 | |
willchan@chromium.org | 57c5630 | 2010-02-25 12:09:41 +0900 | [diff] [blame] | 206 | template <class ObserverType, bool check_empty = false> |
| 207 | class ObserverList : public ObserverListBase<ObserverType> { |
| 208 | public: |
| 209 | typedef typename ObserverListBase<ObserverType>::NotificationType |
| 210 | NotificationType; |
| 211 | |
| 212 | ObserverList() {} |
| 213 | explicit ObserverList(NotificationType type) |
| 214 | : ObserverListBase<ObserverType>(type) {} |
| 215 | |
| 216 | ~ObserverList() { |
| 217 | // When check_empty is true, assert that the list is empty on destruction. |
| 218 | if (check_empty) { |
| 219 | ObserverListBase<ObserverType>::Compact(); |
| 220 | DCHECK_EQ(ObserverListBase<ObserverType>::size(), 0U); |
| 221 | } |
| 222 | } |
jar@chromium.org | c4cbacf | 2011-10-13 23:13:43 +0900 | [diff] [blame] | 223 | |
| 224 | bool might_have_observers() const { |
| 225 | return ObserverListBase<ObserverType>::size() != 0; |
| 226 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 227 | }; |
| 228 | |
finnur@chromium.org | aaa7250 | 2013-09-24 21:47:54 +0900 | [diff] [blame] | 229 | #define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \ |
| 230 | do { \ |
| 231 | if ((observer_list).might_have_observers()) { \ |
| 232 | ObserverListBase<ObserverType>::Iterator \ |
| 233 | it_inside_observer_macro(observer_list); \ |
| 234 | ObserverType* obs; \ |
| 235 | while ((obs = it_inside_observer_macro.GetNext()) != NULL) \ |
| 236 | obs->func; \ |
| 237 | } \ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 238 | } while (0) |
| 239 | |
| 240 | #endif // BASE_OBSERVER_LIST_H__ |