| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 // | 44 // |
| 45 // void NotifyFoo() { | 45 // void NotifyFoo() { |
| 46 // FOR_EACH_OBSERVER(Observer, observer_list_, OnFoo(this)); | 46 // FOR_EACH_OBSERVER(Observer, observer_list_, OnFoo(this)); |
| 47 // } | 47 // } |
| 48 // | 48 // |
| 49 // void NotifyBar(int x, int y) { | 49 // void NotifyBar(int x, int y) { |
| 50 // FOR_EACH_OBSERVER(Observer, observer_list_, OnBar(this, x, y)); | 50 // FOR_EACH_OBSERVER(Observer, observer_list_, OnBar(this, x, y)); |
| 51 // } | 51 // } |
| 52 // | 52 // |
| 53 // private: | 53 // private: |
| 54 // ObserverList<Observer> observer_list_; | 54 // base::ObserverList<Observer> observer_list_; |
| 55 // }; | 55 // }; |
| 56 // | 56 // |
| 57 // | 57 // |
| 58 /////////////////////////////////////////////////////////////////////////////// | 58 /////////////////////////////////////////////////////////////////////////////// |
| 59 | 59 |
| 60 namespace base { |
| 61 |
| 60 template <typename ObserverType> | 62 template <typename ObserverType> |
| 61 class ObserverListThreadSafe; | 63 class ObserverListThreadSafe; |
| 62 | 64 |
| 63 template <class ObserverType> | 65 template <class ObserverType> |
| 64 class ObserverListBase | 66 class ObserverListBase |
| 65 : public base::SupportsWeakPtr<ObserverListBase<ObserverType> > { | 67 : public SupportsWeakPtr<ObserverListBase<ObserverType>> { |
| 66 public: | 68 public: |
| 67 // Enumeration of which observers are notified. | 69 // Enumeration of which observers are notified. |
| 68 enum NotificationType { | 70 enum NotificationType { |
| 69 // Specifies that any observers added during notification are notified. | 71 // Specifies that any observers added during notification are notified. |
| 70 // This is the default type if non type is provided to the constructor. | 72 // This is the default type if non type is provided to the constructor. |
| 71 NOTIFY_ALL, | 73 NOTIFY_ALL, |
| 72 | 74 |
| 73 // Specifies that observers added while sending out notification are not | 75 // Specifies that observers added while sending out notification are not |
| 74 // notified. | 76 // notified. |
| 75 NOTIFY_EXISTING_ONLY | 77 NOTIFY_EXISTING_ONLY |
| 76 }; | 78 }; |
| 77 | 79 |
| 78 // An iterator class that can be used to access the list of observers. See | 80 // An iterator class that can be used to access the list of observers. See |
| 79 // also the FOR_EACH_OBSERVER macro defined below. | 81 // also the FOR_EACH_OBSERVER macro defined below. |
| 80 class Iterator { | 82 class Iterator { |
| 81 public: | 83 public: |
| 82 explicit Iterator(ObserverListBase<ObserverType>* list); | 84 explicit Iterator(ObserverListBase<ObserverType>* list); |
| 83 ~Iterator(); | 85 ~Iterator(); |
| 84 ObserverType* GetNext(); | 86 ObserverType* GetNext(); |
| 85 | 87 |
| 86 private: | 88 private: |
| 87 base::WeakPtr<ObserverListBase<ObserverType> > list_; | 89 WeakPtr<ObserverListBase<ObserverType>> list_; |
| 88 size_t index_; | 90 size_t index_; |
| 89 size_t max_index_; | 91 size_t max_index_; |
| 90 }; | 92 }; |
| 91 | 93 |
| 92 ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {} | 94 ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {} |
| 93 explicit ObserverListBase(NotificationType type) | 95 explicit ObserverListBase(NotificationType type) |
| 94 : notify_depth_(0), type_(type) {} | 96 : notify_depth_(0), type_(type) {} |
| 95 | 97 |
| 96 // Add an observer to the list. An observer should not be added to | 98 // Add an observer to the list. An observer should not be added to |
| 97 // the same list more than once. | 99 // the same list more than once. |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 } | 227 } |
| 226 | 228 |
| 227 bool might_have_observers() const { | 229 bool might_have_observers() const { |
| 228 return ObserverListBase<ObserverType>::size() != 0; | 230 return ObserverListBase<ObserverType>::size() != 0; |
| 229 } | 231 } |
| 230 }; | 232 }; |
| 231 | 233 |
| 232 #define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \ | 234 #define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \ |
| 233 do { \ | 235 do { \ |
| 234 if ((observer_list).might_have_observers()) { \ | 236 if ((observer_list).might_have_observers()) { \ |
| 235 ObserverListBase<ObserverType>::Iterator it_inside_observer_macro( \ | 237 base::ObserverListBase<ObserverType>::Iterator it_inside_observer_macro( \ |
| 236 &observer_list); \ | 238 &observer_list); \ |
| 237 ObserverType* obs; \ | 239 ObserverType* obs; \ |
| 238 while ((obs = it_inside_observer_macro.GetNext()) != nullptr) \ | 240 while ((obs = it_inside_observer_macro.GetNext()) != nullptr) \ |
| 239 obs->func; \ | 241 obs->func; \ |
| 240 } \ | 242 } \ |
| 241 } while (0) | 243 } while (0) |
| 242 | 244 |
| 245 } // namespace base |
| 246 |
| 247 // TODO(brettw) remove this when callers use the correct namespace. |
| 248 using base::ObserverList; |
| 249 |
| 243 #endif // BASE_OBSERVER_LIST_H_ | 250 #endif // BASE_OBSERVER_LIST_H_ |
| OLD | NEW |