Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 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 | |
michael@0 | 15 | #if defined(ANDROID) && defined(_STLP_STD_NAME) |
michael@0 | 16 | using _STLP_STD_NAME::find; |
michael@0 | 17 | #endif |
michael@0 | 18 | |
michael@0 | 19 | namespace base { |
michael@0 | 20 | |
michael@0 | 21 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 22 | // |
michael@0 | 23 | // OVERVIEW: |
michael@0 | 24 | // |
michael@0 | 25 | // A container for a list of observers. Unlike a normal STL vector or list, |
michael@0 | 26 | // this container can be modified during iteration without invalidating the |
michael@0 | 27 | // iterator. So, it safely handles the case of an observer removing itself |
michael@0 | 28 | // or other observers from the list while observers are being notified. |
michael@0 | 29 | // |
michael@0 | 30 | // TYPICAL USAGE: |
michael@0 | 31 | // |
michael@0 | 32 | // class MyWidget { |
michael@0 | 33 | // public: |
michael@0 | 34 | // ... |
michael@0 | 35 | // |
michael@0 | 36 | // class Observer { |
michael@0 | 37 | // public: |
michael@0 | 38 | // virtual void OnFoo(MyWidget* w) = 0; |
michael@0 | 39 | // virtual void OnBar(MyWidget* w, int x, int y) = 0; |
michael@0 | 40 | // }; |
michael@0 | 41 | // |
michael@0 | 42 | // void AddObserver(Observer* obs) { |
michael@0 | 43 | // observer_list_.AddObserver(obs); |
michael@0 | 44 | // } |
michael@0 | 45 | // |
michael@0 | 46 | // void RemoveObserver(Observer* obs) { |
michael@0 | 47 | // observer_list_.RemoveObserver(obs); |
michael@0 | 48 | // } |
michael@0 | 49 | // |
michael@0 | 50 | // void NotifyFoo() { |
michael@0 | 51 | // FOR_EACH_OBSERVER(Observer, observer_list_, OnFoo(this)); |
michael@0 | 52 | // } |
michael@0 | 53 | // |
michael@0 | 54 | // void NotifyBar(int x, int y) { |
michael@0 | 55 | // FOR_EACH_OBSERVER(Observer, observer_list_, OnBar(this, x, y)); |
michael@0 | 56 | // } |
michael@0 | 57 | // |
michael@0 | 58 | // private: |
michael@0 | 59 | // ObserverList<Observer> observer_list_; |
michael@0 | 60 | // }; |
michael@0 | 61 | // |
michael@0 | 62 | // |
michael@0 | 63 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 64 | |
michael@0 | 65 | template <class ObserverType, bool check_empty = false> |
michael@0 | 66 | class ObserverList { |
michael@0 | 67 | public: |
michael@0 | 68 | // Enumeration of which observers are notified. |
michael@0 | 69 | enum NotificationType { |
michael@0 | 70 | // Specifies that any observers added during notification are notified. |
michael@0 | 71 | // This is the default type if non type is provided to the constructor. |
michael@0 | 72 | NOTIFY_ALL, |
michael@0 | 73 | |
michael@0 | 74 | // Specifies that observers added while sending out notification are not |
michael@0 | 75 | // notified. |
michael@0 | 76 | NOTIFY_EXISTING_ONLY |
michael@0 | 77 | }; |
michael@0 | 78 | |
michael@0 | 79 | ObserverList() : notify_depth_(0), type_(NOTIFY_ALL) {} |
michael@0 | 80 | ObserverList(NotificationType type) : notify_depth_(0), type_(type) {} |
michael@0 | 81 | ~ObserverList() { |
michael@0 | 82 | // When check_empty is true, assert that the list is empty on destruction. |
michael@0 | 83 | if (check_empty) { |
michael@0 | 84 | Compact(); |
michael@0 | 85 | DCHECK_EQ(observers_.size(), 0U); |
michael@0 | 86 | } |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | // Add an observer to the list. |
michael@0 | 90 | void AddObserver(ObserverType* obs) { |
michael@0 | 91 | DCHECK(find(observers_.begin(), observers_.end(), obs) == observers_.end()) |
michael@0 | 92 | << "Observers can only be added once!"; |
michael@0 | 93 | observers_.push_back(obs); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | // Remove an observer from the list. |
michael@0 | 97 | void RemoveObserver(ObserverType* obs) { |
michael@0 | 98 | typename ListType::iterator it = |
michael@0 | 99 | std::find(observers_.begin(), observers_.end(), obs); |
michael@0 | 100 | if (it != observers_.end()) { |
michael@0 | 101 | if (notify_depth_) { |
michael@0 | 102 | *it = 0; |
michael@0 | 103 | } else { |
michael@0 | 104 | observers_.erase(it); |
michael@0 | 105 | } |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | size_t size() const { |
michael@0 | 110 | return observers_.size(); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | ObserverType* GetElementAt(int index) const { |
michael@0 | 114 | return observers_[index]; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | // An iterator class that can be used to access the list of observers. See |
michael@0 | 118 | // also the FOREACH_OBSERVER macro defined below. |
michael@0 | 119 | class Iterator { |
michael@0 | 120 | public: |
michael@0 | 121 | Iterator(const ObserverList<ObserverType>& list) |
michael@0 | 122 | : list_(list), |
michael@0 | 123 | index_(0), |
michael@0 | 124 | max_index_(list.type_ == NOTIFY_ALL ? |
michael@0 | 125 | std::numeric_limits<size_t>::max() : |
michael@0 | 126 | list.observers_.size()) { |
michael@0 | 127 | ++list_.notify_depth_; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | ~Iterator() { |
michael@0 | 131 | if (--list_.notify_depth_ == 0) |
michael@0 | 132 | list_.Compact(); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | ObserverType* GetNext() { |
michael@0 | 136 | ListType& observers = list_.observers_; |
michael@0 | 137 | // Advance if the current element is null |
michael@0 | 138 | size_t max_index = std::min(max_index_, observers.size()); |
michael@0 | 139 | while (index_ < max_index && !observers[index_]) |
michael@0 | 140 | ++index_; |
michael@0 | 141 | return index_ < max_index ? observers[index_++] : NULL; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | private: |
michael@0 | 145 | const ObserverList<ObserverType>& list_; |
michael@0 | 146 | size_t index_; |
michael@0 | 147 | size_t max_index_; |
michael@0 | 148 | }; |
michael@0 | 149 | |
michael@0 | 150 | private: |
michael@0 | 151 | typedef std::vector<ObserverType*> ListType; |
michael@0 | 152 | |
michael@0 | 153 | void Compact() const { |
michael@0 | 154 | typename ListType::iterator it = observers_.begin(); |
michael@0 | 155 | while (it != observers_.end()) { |
michael@0 | 156 | if (*it) { |
michael@0 | 157 | ++it; |
michael@0 | 158 | } else { |
michael@0 | 159 | it = observers_.erase(it); |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | // These are marked mutable to facilitate having NotifyAll be const. |
michael@0 | 165 | mutable ListType observers_; |
michael@0 | 166 | mutable int notify_depth_; |
michael@0 | 167 | NotificationType type_; |
michael@0 | 168 | |
michael@0 | 169 | friend class ObserverList::Iterator; |
michael@0 | 170 | |
michael@0 | 171 | DISALLOW_EVIL_CONSTRUCTORS(ObserverList); |
michael@0 | 172 | }; |
michael@0 | 173 | |
michael@0 | 174 | } // namespace base |
michael@0 | 175 | |
michael@0 | 176 | #define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \ |
michael@0 | 177 | do { \ |
michael@0 | 178 | base::ObserverList<ObserverType>::Iterator it(observer_list); \ |
michael@0 | 179 | ObserverType* obs; \ |
michael@0 | 180 | while ((obs = it.GetNext()) != NULL) \ |
michael@0 | 181 | obs->func; \ |
michael@0 | 182 | } while (0) |
michael@0 | 183 | |
michael@0 | 184 | #endif // BASE_OBSERVER_LIST_H__ |