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_ID_MAP_H_ | 5 #ifndef BASE_ID_MAP_H_ |
6 #define BASE_ID_MAP_H_ | 6 #define BASE_ID_MAP_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 } | 46 } |
47 | 47 |
48 ~IDMap() { | 48 ~IDMap() { |
49 // Many IDMap's are static, and hence will be destroyed on the main thread. | 49 // Many IDMap's are static, and hence will be destroyed on the main thread. |
50 // However, all the accesses may take place on another thread, such as the | 50 // However, all the accesses may take place on another thread, such as the |
51 // IO thread. Detaching again to clean this up. | 51 // IO thread. Detaching again to clean this up. |
52 DetachFromThread(); | 52 DetachFromThread(); |
53 Releaser<OS, 0>::release_all(&data_); | 53 Releaser<OS, 0>::release_all(&data_); |
54 } | 54 } |
55 | 55 |
56 // Sets whether Add should CHECK if passed in NULL data. Default is false. | 56 // Sets whether Add and Replace should DCHECK if passed in NULL data. |
| 57 // Default is false. |
57 void set_check_on_null_data(bool value) { check_on_null_data_ = value; } | 58 void set_check_on_null_data(bool value) { check_on_null_data_ = value; } |
58 | 59 |
59 // Adds a view with an automatically generated unique ID. See AddWithID. | 60 // Adds a view with an automatically generated unique ID. See AddWithID. |
60 KeyType Add(T* data) { | 61 KeyType Add(T* data) { |
61 DCHECK(CalledOnValidThread()); | 62 DCHECK(CalledOnValidThread()); |
62 CHECK(!check_on_null_data_ || data); | 63 DCHECK(!check_on_null_data_ || data); |
63 KeyType this_id = next_id_; | 64 KeyType this_id = next_id_; |
64 DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item"; | 65 DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item"; |
65 data_[this_id] = data; | 66 data_[this_id] = data; |
66 next_id_++; | 67 next_id_++; |
67 return this_id; | 68 return this_id; |
68 } | 69 } |
69 | 70 |
70 // Adds a new data member with the specified ID. The ID must not be in | 71 // Adds a new data member with the specified ID. The ID must not be in |
71 // the list. The caller either must generate all unique IDs itself and use | 72 // the list. The caller either must generate all unique IDs itself and use |
72 // this function, or allow this object to generate IDs and call Add. These | 73 // this function, or allow this object to generate IDs and call Add. These |
73 // two methods may not be mixed, or duplicate IDs may be generated | 74 // two methods may not be mixed, or duplicate IDs may be generated |
74 void AddWithID(T* data, KeyType id) { | 75 void AddWithID(T* data, KeyType id) { |
75 DCHECK(CalledOnValidThread()); | 76 DCHECK(CalledOnValidThread()); |
76 CHECK(!check_on_null_data_ || data); | 77 DCHECK(!check_on_null_data_ || data); |
77 DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item"; | 78 DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item"; |
78 data_[id] = data; | 79 data_[id] = data; |
79 } | 80 } |
80 | 81 |
81 void Remove(KeyType id) { | 82 void Remove(KeyType id) { |
82 DCHECK(CalledOnValidThread()); | 83 DCHECK(CalledOnValidThread()); |
83 typename HashTable::iterator i = data_.find(id); | 84 typename HashTable::iterator i = data_.find(id); |
84 if (i == data_.end()) { | 85 if (i == data_.end()) { |
85 NOTREACHED() << "Attempting to remove an item not in the list"; | 86 NOTREACHED() << "Attempting to remove an item not in the list"; |
86 return; | 87 return; |
87 } | 88 } |
88 | 89 |
89 if (iteration_depth_ == 0) { | 90 if (iteration_depth_ == 0) { |
90 Releaser<OS, 0>::release(i->second); | 91 Releaser<OS, 0>::release(i->second); |
91 data_.erase(i); | 92 data_.erase(i); |
92 } else { | 93 } else { |
93 removed_ids_.insert(id); | 94 removed_ids_.insert(id); |
94 } | 95 } |
95 } | 96 } |
96 | 97 |
| 98 // Replaces the value for |id| with |new_data| and returns a pointer to the |
| 99 // existing value. If there is no entry for |id|, the map is not altered and |
| 100 // nullptr is returned. The OwnershipSemantics of the map have no effect on |
| 101 // how the existing value is treated, the IDMap does not delete the existing |
| 102 // value being replaced. |
| 103 T* Replace(KeyType id, T* new_data) { |
| 104 DCHECK(CalledOnValidThread()); |
| 105 DCHECK(!check_on_null_data_ || new_data); |
| 106 typename HashTable::iterator i = data_.find(id); |
| 107 if (i == data_.end()) { |
| 108 NOTREACHED() << "Attempting to replace an item not in the list"; |
| 109 return nullptr; |
| 110 } |
| 111 |
| 112 T* temp = i->second; |
| 113 i->second = new_data; |
| 114 return temp; |
| 115 } |
| 116 |
97 void Clear() { | 117 void Clear() { |
98 DCHECK(CalledOnValidThread()); | 118 DCHECK(CalledOnValidThread()); |
99 if (iteration_depth_ == 0) { | 119 if (iteration_depth_ == 0) { |
100 Releaser<OS, 0>::release_all(&data_); | 120 Releaser<OS, 0>::release_all(&data_); |
101 } else { | 121 } else { |
102 for (typename HashTable::iterator i = data_.begin(); | 122 for (typename HashTable::iterator i = data_.begin(); |
103 i != data_.end(); ++i) | 123 i != data_.end(); ++i) |
104 removed_ids_.insert(i->first); | 124 removed_ids_.insert(i->first); |
105 } | 125 } |
106 } | 126 } |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 | 270 |
251 HashTable data_; | 271 HashTable data_; |
252 | 272 |
253 // See description above setter. | 273 // See description above setter. |
254 bool check_on_null_data_; | 274 bool check_on_null_data_; |
255 | 275 |
256 DISALLOW_COPY_AND_ASSIGN(IDMap); | 276 DISALLOW_COPY_AND_ASSIGN(IDMap); |
257 }; | 277 }; |
258 | 278 |
259 #endif // BASE_ID_MAP_H_ | 279 #endif // BASE_ID_MAP_H_ |
OLD | NEW |