| 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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 explicit ObserverListBase(NotificationType type) | 93 explicit ObserverListBase(NotificationType type) |
| 94 : notify_depth_(0), type_(type) {} | 94 : notify_depth_(0), type_(type) {} |
| 95 | 95 |
| 96 // 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 |
| 97 // the same list more than once. | 97 // the same list more than once. |
| 98 void AddObserver(ObserverType* obs); | 98 void AddObserver(ObserverType* obs); |
| 99 | 99 |
| 100 // 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. |
| 101 void RemoveObserver(ObserverType* obs); | 101 void RemoveObserver(ObserverType* obs); |
| 102 | 102 |
| 103 bool HasObserver(ObserverType* observer) const; | 103 // Determine whether a particular observer is in the list. |
| 104 bool HasObserver(const ObserverType* observer) const; |
| 104 | 105 |
| 105 void Clear(); | 106 void Clear(); |
| 106 | 107 |
| 107 protected: | 108 protected: |
| 108 size_t size() const { return observers_.size(); } | 109 size_t size() const { return observers_.size(); } |
| 109 | 110 |
| 110 void Compact(); | 111 void Compact(); |
| 111 | 112 |
| 112 private: | 113 private: |
| 113 friend class ObserverListThreadSafe<ObserverType>; | 114 friend class ObserverListThreadSafe<ObserverType>; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 if (it != observers_.end()) { | 170 if (it != observers_.end()) { |
| 170 if (notify_depth_) { | 171 if (notify_depth_) { |
| 171 *it = 0; | 172 *it = 0; |
| 172 } else { | 173 } else { |
| 173 observers_.erase(it); | 174 observers_.erase(it); |
| 174 } | 175 } |
| 175 } | 176 } |
| 176 } | 177 } |
| 177 | 178 |
| 178 template <class ObserverType> | 179 template <class ObserverType> |
| 179 bool ObserverListBase<ObserverType>::HasObserver(ObserverType* observer) const { | 180 bool ObserverListBase<ObserverType>::HasObserver( |
| 181 const ObserverType* observer) const { |
| 180 for (size_t i = 0; i < observers_.size(); ++i) { | 182 for (size_t i = 0; i < observers_.size(); ++i) { |
| 181 if (observers_[i] == observer) | 183 if (observers_[i] == observer) |
| 182 return true; | 184 return true; |
| 183 } | 185 } |
| 184 return false; | 186 return false; |
| 185 } | 187 } |
| 186 | 188 |
| 187 template <class ObserverType> | 189 template <class ObserverType> |
| 188 void ObserverListBase<ObserverType>::Clear() { | 190 void ObserverListBase<ObserverType>::Clear() { |
| 189 if (notify_depth_) { | 191 if (notify_depth_) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 if ((observer_list).might_have_observers()) { \ | 233 if ((observer_list).might_have_observers()) { \ |
| 232 ObserverListBase<ObserverType>::Iterator \ | 234 ObserverListBase<ObserverType>::Iterator \ |
| 233 it_inside_observer_macro(observer_list); \ | 235 it_inside_observer_macro(observer_list); \ |
| 234 ObserverType* obs; \ | 236 ObserverType* obs; \ |
| 235 while ((obs = it_inside_observer_macro.GetNext()) != NULL) \ | 237 while ((obs = it_inside_observer_macro.GetNext()) != NULL) \ |
| 236 obs->func; \ | 238 obs->func; \ |
| 237 } \ | 239 } \ |
| 238 } while (0) | 240 } while (0) |
| 239 | 241 |
| 240 #endif // BASE_OBSERVER_LIST_H__ | 242 #endif // BASE_OBSERVER_LIST_H__ |
| OLD | NEW |