Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CC_BASE_OBSERVER_MAP_H_ | |
| 6 #define CC_BASE_OBSERVER_MAP_H_ | |
| 7 | |
| 8 #include "base/containers/hash_tables.h" | |
| 9 #include "base/logging.h" | |
| 10 | |
| 11 namespace cc { | |
| 12 | |
| 13 template <class ObserverType, class ObserverDataType> | |
|
danakj
2015/09/11 17:56:09
Do you need to use this with multiple ObserverType
mithro-old
2015/09/11 19:03:57
For our use case we probably only have one Observe
| |
| 14 class ObserverMap { | |
| 15 private: | |
| 16 typedef typename base::hash_set<ObserverType*> SetType; | |
| 17 typedef typename base::hash_map<ObserverType*, ObserverDataType*> MapType; | |
| 18 | |
| 19 MapType observers_; | |
| 20 | |
| 21 volatile size_t const_iterators_; | |
| 22 size_t iterators_; | |
| 23 MapType observers_to_add_; | |
| 24 SetType observers_to_remove_; | |
| 25 | |
| 26 public: | |
| 27 ObserverMap() | |
| 28 : observers_(), | |
| 29 const_iterators_(0), | |
| 30 iterators_(0), | |
| 31 observers_to_add_(), | |
| 32 observers_to_remove_() {} | |
| 33 | |
| 34 class iterator : public MapType::iterator { | |
| 35 public: | |
| 36 explicit iterator(typename MapType::iterator it, | |
| 37 ObserverMap<ObserverType, ObserverDataType>* list) | |
| 38 : MapType::iterator(it), list_(list) { | |
| 39 list_->iterators_++; | |
| 40 } | |
| 41 ~iterator() { | |
| 42 DCHECK_GT(list_->iterators_, 0U); | |
| 43 list_->iterators_--; | |
| 44 list_->CompactMaps(); | |
| 45 } | |
| 46 | |
| 47 private: | |
| 48 ObserverMap<ObserverType, ObserverDataType>* list_; | |
| 49 }; | |
| 50 | |
| 51 class const_iterator : public MapType::const_iterator { | |
| 52 public: | |
| 53 explicit const_iterator( | |
| 54 typename MapType::const_iterator it, | |
| 55 const ObserverMap<ObserverType, ObserverDataType>* list) | |
| 56 : MapType::const_iterator(it), | |
| 57 list_( | |
| 58 const_cast<ObserverMap<ObserverType, ObserverDataType>*>(list)) { | |
| 59 list_->const_iterators_++; | |
| 60 } | |
| 61 ~const_iterator() { | |
| 62 DCHECK_GT(list_->const_iterators_, 0U); | |
| 63 list_->const_iterators_--; | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 ObserverMap<ObserverType, ObserverDataType>* list_; | |
| 68 }; | |
| 69 | |
| 70 void AddObserver(ObserverType* obs) { | |
| 71 DCHECK_EQ(const_iterators_, 0U); | |
| 72 if (observers_to_add_.count(obs) == 0) { | |
| 73 observers_to_add_[obs] = new ObserverDataType(); | |
| 74 } | |
| 75 CompactMaps(); | |
| 76 } | |
| 77 | |
| 78 void RemoveObserver(ObserverType* obs) { | |
| 79 DCHECK_EQ(const_iterators_, 0U); | |
| 80 observers_to_remove_.insert(obs); | |
| 81 CompactMaps(); | |
| 82 } | |
| 83 | |
| 84 bool HasObservers() const { return !observers_.empty(); } | |
| 85 bool HasObserver(ObserverType* const obs) const { | |
| 86 return (*this)[obs] != nullptr; | |
| 87 } | |
| 88 | |
| 89 iterator begin() { return iterator(observers_.begin(), this); } | |
| 90 iterator end() { return iterator(observers_.end(), this); } | |
| 91 | |
| 92 const_iterator cbegin() const { | |
| 93 return const_iterator(const_cast<const MapType*>(&observers_)->begin(), | |
| 94 this); | |
| 95 } | |
| 96 | |
| 97 const_iterator cend() const { | |
| 98 return const_iterator(const_cast<const MapType*>(&observers_)->end(), this); | |
| 99 } | |
| 100 | |
| 101 const_iterator begin() const { return cbegin(); } | |
| 102 const_iterator end() const { return cend(); } | |
| 103 | |
| 104 ObserverDataType* operator[](ObserverType* const obs) { | |
| 105 { | |
| 106 auto it = observers_.find(obs); | |
| 107 if (it != observers_.end()) | |
| 108 return it->second; | |
| 109 } | |
| 110 { | |
| 111 auto it = observers_to_add_.find(obs); | |
| 112 if (it != observers_to_add_.end()) | |
| 113 return it->second; | |
| 114 } | |
| 115 return nullptr; | |
| 116 } | |
| 117 const ObserverDataType* operator[](ObserverType* const obs) const { | |
| 118 { | |
| 119 auto it = observers_.find(obs); | |
| 120 if (it != observers_.end()) | |
| 121 return it->second; | |
| 122 } | |
| 123 { | |
| 124 auto it = observers_to_add_.find(obs); | |
| 125 if (it != observers_to_add_.end()) | |
| 126 return it->second; | |
| 127 } | |
| 128 return nullptr; | |
| 129 } | |
| 130 | |
| 131 private: | |
| 132 void CompactMaps() { | |
| 133 if (iterators_ != 0) | |
| 134 return; | |
| 135 | |
| 136 for (auto it = observers_to_remove_.begin(); | |
| 137 it != observers_to_remove_.end(); it++) { | |
| 138 delete observers_[*it]; | |
| 139 observers_[*it] = nullptr; | |
| 140 observers_.erase(*it); | |
| 141 } | |
| 142 observers_to_remove_.clear(); | |
| 143 | |
| 144 for (auto it = observers_to_add_.begin(); it != observers_to_add_.end(); | |
| 145 it++) { | |
| 146 observers_[it->first] = it->second; | |
| 147 } | |
| 148 observers_to_add_.clear(); | |
| 149 } | |
| 150 | |
| 151 friend class ObserverMapTest; | |
| 152 }; | |
| 153 | |
| 154 } // namespace cc | |
| 155 | |
| 156 #endif // CC_BASE_OBSERVER_MAP_H_ | |
| OLD | NEW |