| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_OBSERVER_LIST_H__ | 5 #ifndef BASE_OBSERVER_LIST_H__ |
| 6 #define BASE_OBSERVER_LIST_H__ | 6 #define BASE_OBSERVER_LIST_H__ |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 | 72 |
| 73 // Specifies that observers added while sending out notification are not | 73 // Specifies that observers added while sending out notification are not |
| 74 // notified. | 74 // notified. |
| 75 NOTIFY_EXISTING_ONLY | 75 NOTIFY_EXISTING_ONLY |
| 76 }; | 76 }; |
| 77 | 77 |
| 78 // An iterator class that can be used to access the list of observers. See | 78 // An iterator class that can be used to access the list of observers. See |
| 79 // also the FOR_EACH_OBSERVER macro defined below. | 79 // also the FOR_EACH_OBSERVER macro defined below. |
| 80 class Iterator { | 80 class Iterator { |
| 81 public: | 81 public: |
| 82 Iterator(ObserverListBase<ObserverType>& list) | 82 Iterator(ObserverListBase<ObserverType>& list); |
| 83 : list_(list.AsWeakPtr()), | 83 ~Iterator(); |
| 84 index_(0), | 84 ObserverType* GetNext(); |
| 85 max_index_(list.type_ == NOTIFY_ALL ? | |
| 86 std::numeric_limits<size_t>::max() : | |
| 87 list.observers_.size()) { | |
| 88 ++list_->notify_depth_; | |
| 89 } | |
| 90 | |
| 91 ~Iterator() { | |
| 92 if (list_.get() && --list_->notify_depth_ == 0) | |
| 93 list_->Compact(); | |
| 94 } | |
| 95 | |
| 96 ObserverType* GetNext() { | |
| 97 if (!list_.get()) | |
| 98 return NULL; | |
| 99 ListType& observers = list_->observers_; | |
| 100 // Advance if the current element is null | |
| 101 size_t max_index = std::min(max_index_, observers.size()); | |
| 102 while (index_ < max_index && !observers[index_]) | |
| 103 ++index_; | |
| 104 return index_ < max_index ? observers[index_++] : NULL; | |
| 105 } | |
| 106 | 85 |
| 107 private: | 86 private: |
| 108 base::WeakPtr<ObserverListBase<ObserverType> > list_; | 87 base::WeakPtr<ObserverListBase<ObserverType> > list_; |
| 109 size_t index_; | 88 size_t index_; |
| 110 size_t max_index_; | 89 size_t max_index_; |
| 111 }; | 90 }; |
| 112 | 91 |
| 113 ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {} | 92 ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {} |
| 114 explicit ObserverListBase(NotificationType type) | 93 explicit ObserverListBase(NotificationType type) |
| 115 : notify_depth_(0), type_(type) {} | 94 : notify_depth_(0), type_(type) {} |
| 116 | 95 |
| 117 // Add an observer to the list. An observer should not be added to | 96 // Add an observer to the list. An observer should not be added to |
| 118 // the same list more than once. | 97 // the same list more than once. |
| 119 void AddObserver(ObserverType* obs) { | 98 void AddObserver(ObserverType* obs); |
| 120 if (std::find(observers_.begin(), observers_.end(), obs) | |
| 121 != observers_.end()) { | |
| 122 NOTREACHED() << "Observers can only be added once!"; | |
| 123 return; | |
| 124 } | |
| 125 observers_.push_back(obs); | |
| 126 } | |
| 127 | 99 |
| 128 // Remove an observer from the list if it is in the list. | 100 // Remove an observer from the list if it is in the list. |
| 129 void RemoveObserver(ObserverType* obs) { | 101 void RemoveObserver(ObserverType* obs); |
| 130 typename ListType::iterator it = | |
| 131 std::find(observers_.begin(), observers_.end(), obs); | |
| 132 if (it != observers_.end()) { | |
| 133 if (notify_depth_) { | |
| 134 *it = 0; | |
| 135 } else { | |
| 136 observers_.erase(it); | |
| 137 } | |
| 138 } | |
| 139 } | |
| 140 | 102 |
| 141 bool HasObserver(ObserverType* observer) const { | 103 bool HasObserver(ObserverType* observer) const; |
| 142 for (size_t i = 0; i < observers_.size(); ++i) { | |
| 143 if (observers_[i] == observer) | |
| 144 return true; | |
| 145 } | |
| 146 return false; | |
| 147 } | |
| 148 | 104 |
| 149 void Clear() { | 105 void Clear(); |
| 150 if (notify_depth_) { | |
| 151 for (typename ListType::iterator it = observers_.begin(); | |
| 152 it != observers_.end(); ++it) { | |
| 153 *it = 0; | |
| 154 } | |
| 155 } else { | |
| 156 observers_.clear(); | |
| 157 } | |
| 158 } | |
| 159 | 106 |
| 160 protected: | 107 protected: |
| 161 size_t size() const { return observers_.size(); } | 108 size_t size() const { return observers_.size(); } |
| 162 | 109 |
| 163 void Compact() { | 110 void Compact(); |
| 164 observers_.erase( | |
| 165 std::remove(observers_.begin(), observers_.end(), | |
| 166 static_cast<ObserverType*>(NULL)), observers_.end()); | |
| 167 } | |
| 168 | 111 |
| 169 private: | 112 private: |
| 170 friend class ObserverListThreadSafe<ObserverType>; | 113 friend class ObserverListThreadSafe<ObserverType>; |
| 171 | 114 |
| 172 typedef std::vector<ObserverType*> ListType; | 115 typedef std::vector<ObserverType*> ListType; |
| 173 | 116 |
| 174 ListType observers_; | 117 ListType observers_; |
| 175 int notify_depth_; | 118 int notify_depth_; |
| 176 NotificationType type_; | 119 NotificationType type_; |
| 177 | 120 |
| 178 friend class ObserverListBase::Iterator; | 121 friend class ObserverListBase::Iterator; |
| 179 | 122 |
| 180 DISALLOW_COPY_AND_ASSIGN(ObserverListBase); | 123 DISALLOW_COPY_AND_ASSIGN(ObserverListBase); |
| 181 }; | 124 }; |
| 182 | 125 |
| 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 |
| 183 template <class ObserverType, bool check_empty = false> | 206 template <class ObserverType, bool check_empty = false> |
| 184 class ObserverList : public ObserverListBase<ObserverType> { | 207 class ObserverList : public ObserverListBase<ObserverType> { |
| 185 public: | 208 public: |
| 186 typedef typename ObserverListBase<ObserverType>::NotificationType | 209 typedef typename ObserverListBase<ObserverType>::NotificationType |
| 187 NotificationType; | 210 NotificationType; |
| 188 | 211 |
| 189 ObserverList() {} | 212 ObserverList() {} |
| 190 explicit ObserverList(NotificationType type) | 213 explicit ObserverList(NotificationType type) |
| 191 : ObserverListBase<ObserverType>(type) {} | 214 : ObserverListBase<ObserverType>(type) {} |
| 192 | 215 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 208 if ((observer_list).might_have_observers()) { \ | 231 if ((observer_list).might_have_observers()) { \ |
| 209 ObserverListBase<ObserverType>::Iterator \ | 232 ObserverListBase<ObserverType>::Iterator \ |
| 210 it_inside_observer_macro(observer_list); \ | 233 it_inside_observer_macro(observer_list); \ |
| 211 ObserverType* obs; \ | 234 ObserverType* obs; \ |
| 212 while ((obs = it_inside_observer_macro.GetNext()) != NULL) \ | 235 while ((obs = it_inside_observer_macro.GetNext()) != NULL) \ |
| 213 obs->func; \ | 236 obs->func; \ |
| 214 } \ | 237 } \ |
| 215 } while (0) | 238 } while (0) |
| 216 | 239 |
| 217 #endif // BASE_OBSERVER_LIST_H__ | 240 #endif // BASE_OBSERVER_LIST_H__ |
| OLD | NEW |