OLD | NEW |
---|---|
1 // Copyright (c) 2006-2008 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_ID_MAP_H_ | 5 #ifndef BASE_ID_MAP_H_ |
6 #define BASE_ID_MAP_H_ | 6 #define BASE_ID_MAP_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/hash_tables.h" | 12 #include "base/hash_tables.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/threading/non_thread_safe.h" | |
14 | 15 |
15 // Ownership semantics - own pointer means the pointer is deleted in Remove() | 16 // Ownership semantics - own pointer means the pointer is deleted in Remove() |
16 // & during destruction | 17 // & during destruction |
17 enum IDMapOwnershipSemantics { | 18 enum IDMapOwnershipSemantics { |
18 IDMapExternalPointer, | 19 IDMapExternalPointer, |
19 IDMapOwnPointer | 20 IDMapOwnPointer |
20 }; | 21 }; |
21 | 22 |
22 // This object maintains a list of IDs that can be quickly converted to | 23 // This object maintains a list of IDs that can be quickly converted to |
23 // pointers to objects. It is implemented as a hash table, optimized for | 24 // pointers to objects. It is implemented as a hash table, optimized for |
24 // relatively small data sets (in the common case, there will be exactly one | 25 // relatively small data sets (in the common case, there will be exactly one |
25 // item in the list). | 26 // item in the list). |
26 // | 27 // |
27 // Items can be inserted into the container with arbitrary ID, but the caller | 28 // Items can be inserted into the container with arbitrary ID, but the caller |
28 // must ensure they are unique. Inserting IDs and relying on automatically | 29 // must ensure they are unique. Inserting IDs and relying on automatically |
29 // generated ones is not allowed because they can collide. | 30 // generated ones is not allowed because they can collide. |
30 // | 31 // |
31 // This class does not have a virtual destructor, do not inherit from it when | 32 // This class does not have a virtual destructor, do not inherit from it when |
32 // ownership semantics are set to own because pointers will leak. | 33 // ownership semantics are set to own because pointers will leak. |
33 template<typename T, IDMapOwnershipSemantics OS = IDMapExternalPointer> | 34 template<typename T, IDMapOwnershipSemantics OS = IDMapExternalPointer> |
34 class IDMap { | 35 class IDMap : public base::NonThreadSafe { |
35 private: | 36 private: |
36 typedef int32 KeyType; | 37 typedef int32 KeyType; |
37 typedef base::hash_map<KeyType, T*> HashTable; | 38 typedef base::hash_map<KeyType, T*> HashTable; |
38 | 39 |
39 public: | 40 public: |
40 IDMap() : iteration_depth_(0), next_id_(1), check_on_null_data_(false) { | 41 IDMap() : iteration_depth_(0), next_id_(1), check_on_null_data_(false) { |
42 // A number of consumers of IDMap create it on one thread but always access | |
43 // it from a different, but consitent, thread post-construction. | |
44 // TODO(cbentzel): Fix users of IDMap and remove this. | |
brettw
2011/03/02 18:42:47
Unless you're actually going to change callers (wh
cbentzel
2011/03/02 19:02:45
Agreed. Removed.
| |
45 DetachFromThread(); | |
41 } | 46 } |
42 | 47 |
43 ~IDMap() { | 48 ~IDMap() { |
44 Releaser<OS, 0>::release_all(&data_); | 49 Releaser<OS, 0>::release_all(&data_); |
45 } | 50 } |
46 | 51 |
47 // Sets whether Add should CHECK if passed in NULL data. Default is false. | 52 // Sets whether Add should CHECK if passed in NULL data. Default is false. |
48 void set_check_on_null_data(bool value) { check_on_null_data_ = value; } | 53 void set_check_on_null_data(bool value) { check_on_null_data_ = value; } |
49 | 54 |
50 // Adds a view with an automatically generated unique ID. See AddWithID. | 55 // Adds a view with an automatically generated unique ID. See AddWithID. |
51 KeyType Add(T* data) { | 56 KeyType Add(T* data) { |
57 DCHECK(CalledOnValidThread()); | |
52 CHECK(!check_on_null_data_ || data); | 58 CHECK(!check_on_null_data_ || data); |
53 KeyType this_id = next_id_; | 59 KeyType this_id = next_id_; |
54 DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item"; | 60 DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item"; |
55 data_[this_id] = data; | 61 data_[this_id] = data; |
56 next_id_++; | 62 next_id_++; |
57 return this_id; | 63 return this_id; |
58 } | 64 } |
59 | 65 |
60 // Adds a new data member with the specified ID. The ID must not be in | 66 // Adds a new data member with the specified ID. The ID must not be in |
61 // the list. The caller either must generate all unique IDs itself and use | 67 // the list. The caller either must generate all unique IDs itself and use |
62 // this function, or allow this object to generate IDs and call Add. These | 68 // this function, or allow this object to generate IDs and call Add. These |
63 // two methods may not be mixed, or duplicate IDs may be generated | 69 // two methods may not be mixed, or duplicate IDs may be generated |
64 void AddWithID(T* data, KeyType id) { | 70 void AddWithID(T* data, KeyType id) { |
71 DCHECK(CalledOnValidThread()); | |
65 CHECK(!check_on_null_data_ || data); | 72 CHECK(!check_on_null_data_ || data); |
66 DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item"; | 73 DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item"; |
67 data_[id] = data; | 74 data_[id] = data; |
68 } | 75 } |
69 | 76 |
70 void Remove(KeyType id) { | 77 void Remove(KeyType id) { |
78 DCHECK(CalledOnValidThread()); | |
71 typename HashTable::iterator i = data_.find(id); | 79 typename HashTable::iterator i = data_.find(id); |
72 if (i == data_.end()) { | 80 if (i == data_.end()) { |
73 NOTREACHED() << "Attempting to remove an item not in the list"; | 81 NOTREACHED() << "Attempting to remove an item not in the list"; |
74 return; | 82 return; |
75 } | 83 } |
76 | 84 |
77 if (iteration_depth_ == 0) { | 85 if (iteration_depth_ == 0) { |
78 Releaser<OS, 0>::release(i->second); | 86 Releaser<OS, 0>::release(i->second); |
79 data_.erase(i); | 87 data_.erase(i); |
80 } else { | 88 } else { |
81 removed_ids_.insert(id); | 89 removed_ids_.insert(id); |
82 } | 90 } |
83 } | 91 } |
84 | 92 |
85 bool IsEmpty() const { | 93 bool IsEmpty() const { |
94 DCHECK(CalledOnValidThread()); | |
86 return size() == 0u; | 95 return size() == 0u; |
87 } | 96 } |
88 | 97 |
89 T* Lookup(KeyType id) const { | 98 T* Lookup(KeyType id) const { |
99 DCHECK(CalledOnValidThread()); | |
90 typename HashTable::const_iterator i = data_.find(id); | 100 typename HashTable::const_iterator i = data_.find(id); |
91 if (i == data_.end()) | 101 if (i == data_.end()) |
92 return NULL; | 102 return NULL; |
93 return i->second; | 103 return i->second; |
94 } | 104 } |
95 | 105 |
96 size_t size() const { | 106 size_t size() const { |
107 DCHECK(CalledOnValidThread()); | |
97 return data_.size() - removed_ids_.size(); | 108 return data_.size() - removed_ids_.size(); |
98 } | 109 } |
99 | 110 |
100 // It is safe to remove elements from the map during iteration. All iterators | 111 // It is safe to remove elements from the map during iteration. All iterators |
101 // will remain valid. | 112 // will remain valid. |
102 template<class ReturnType> | 113 template<class ReturnType> |
103 class Iterator { | 114 class Iterator { |
104 public: | 115 public: |
105 Iterator(IDMap<T, OS>* map) | 116 Iterator(IDMap<T, OS>* map) |
106 : map_(map), | 117 : map_(map), |
107 iter_(map_->data_.begin()) { | 118 iter_(map_->data_.begin()) { |
119 DCHECK(map->CalledOnValidThread()); | |
108 ++map_->iteration_depth_; | 120 ++map_->iteration_depth_; |
109 SkipRemovedEntries(); | 121 SkipRemovedEntries(); |
110 } | 122 } |
111 | 123 |
112 ~Iterator() { | 124 ~Iterator() { |
125 DCHECK(map_->CalledOnValidThread()); | |
113 if (--map_->iteration_depth_ == 0) | 126 if (--map_->iteration_depth_ == 0) |
114 map_->Compact(); | 127 map_->Compact(); |
115 } | 128 } |
116 | 129 |
117 bool IsAtEnd() const { | 130 bool IsAtEnd() const { |
131 DCHECK(map_->CalledOnValidThread()); | |
118 return iter_ == map_->data_.end(); | 132 return iter_ == map_->data_.end(); |
119 } | 133 } |
120 | 134 |
121 KeyType GetCurrentKey() const { | 135 KeyType GetCurrentKey() const { |
136 DCHECK(map_->CalledOnValidThread()); | |
122 return iter_->first; | 137 return iter_->first; |
123 } | 138 } |
124 | 139 |
125 ReturnType* GetCurrentValue() const { | 140 ReturnType* GetCurrentValue() const { |
141 DCHECK(map_->CalledOnValidThread()); | |
126 return iter_->second; | 142 return iter_->second; |
127 } | 143 } |
128 | 144 |
129 void Advance() { | 145 void Advance() { |
146 DCHECK(map_->CalledOnValidThread()); | |
130 ++iter_; | 147 ++iter_; |
131 SkipRemovedEntries(); | 148 SkipRemovedEntries(); |
132 } | 149 } |
133 | 150 |
134 private: | 151 private: |
135 void SkipRemovedEntries() { | 152 void SkipRemovedEntries() { |
136 while (iter_ != map_->data_.end() && | 153 while (iter_ != map_->data_.end() && |
137 map_->removed_ids_.find(iter_->first) != | 154 map_->removed_ids_.find(iter_->first) != |
138 map_->removed_ids_.end()) { | 155 map_->removed_ids_.end()) { |
139 ++iter_; | 156 ++iter_; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 | 207 |
191 HashTable data_; | 208 HashTable data_; |
192 | 209 |
193 // See description above setter. | 210 // See description above setter. |
194 bool check_on_null_data_; | 211 bool check_on_null_data_; |
195 | 212 |
196 DISALLOW_COPY_AND_ASSIGN(IDMap); | 213 DISALLOW_COPY_AND_ASSIGN(IDMap); |
197 }; | 214 }; |
198 | 215 |
199 #endif // BASE_ID_MAP_H_ | 216 #endif // BASE_ID_MAP_H_ |
OLD | NEW |