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 <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 #include <memory> | 10 #include <memory> |
11 #include <set> | 11 #include <set> |
12 #include <type_traits> | 12 #include <type_traits> |
13 #include <utility> | 13 #include <utility> |
14 | 14 |
15 #include "base/containers/hash_tables.h" | 15 #include "base/containers/hash_tables.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/sequence_checker.h" | 18 #include "base/sequence_checker.h" |
19 | 19 |
20 // Ownership semantics - own pointer means the pointer is deleted in Remove() | 20 // Ownership semantics: |
21 // & during destruction | 21 // - OwnPointer means we store a unique_ptr that owns the object (so the |
| 22 // object is deleted in Remove() and during destruction). |
| 23 // - ExternalPointer means we store a raw pointer and don't own the object |
| 24 |
| 25 // TODO (http://crbug.com/647091): eliminate this enum, replace OwnPointer |
| 26 // mode in callsites with IDMap<unique_ptr<T>> |
22 enum IDMapOwnershipSemantics { | 27 enum IDMapOwnershipSemantics { |
23 IDMapExternalPointer, | 28 IDMapExternalPointer, |
24 IDMapOwnPointer | 29 IDMapOwnPointer |
25 }; | 30 }; |
26 | 31 |
27 // This object maintains a list of IDs that can be quickly converted to | 32 // This object maintains a list of IDs that can be quickly converted to |
28 // pointers to objects. It is implemented as a hash table, optimized for | 33 // pointers to objects. It is implemented as a hash table, optimized for |
29 // relatively small data sets (in the common case, there will be exactly one | 34 // relatively small data sets (in the common case, there will be exactly one |
30 // item in the list). | 35 // item in the list). |
31 // | 36 // |
(...skipping 29 matching lines...) Expand all Loading... |
61 // thread. However, all the accesses may take place on another thread (or | 66 // thread. However, all the accesses may take place on another thread (or |
62 // sequence), such as the IO thread. Detaching again to clean this up. | 67 // sequence), such as the IO thread. Detaching again to clean this up. |
63 sequence_checker_.DetachFromSequence(); | 68 sequence_checker_.DetachFromSequence(); |
64 } | 69 } |
65 | 70 |
66 // Sets whether Add and Replace should DCHECK if passed in NULL data. | 71 // Sets whether Add and Replace should DCHECK if passed in NULL data. |
67 // Default is false. | 72 // Default is false. |
68 void set_check_on_null_data(bool value) { check_on_null_data_ = value; } | 73 void set_check_on_null_data(bool value) { check_on_null_data_ = value; } |
69 | 74 |
70 // Adds a view with an automatically generated unique ID. See AddWithID. | 75 // Adds a view with an automatically generated unique ID. See AddWithID. |
71 // (This unique_ptr<> variant will not compile in IDMapExternalPointer mode.) | 76 KeyType Add(V data) { return AddInternal(std::move(data)); } |
72 KeyType Add(std::unique_ptr<T> data) { | |
73 return AddInternal(std::move(data)); | |
74 } | |
75 | 77 |
76 // Adds a new data member with the specified ID. The ID must not be in | 78 // Adds a new data member with the specified ID. The ID must not be in |
77 // the list. The caller either must generate all unique IDs itself and use | 79 // the list. The caller either must generate all unique IDs itself and use |
78 // this function, or allow this object to generate IDs and call Add. These | 80 // this function, or allow this object to generate IDs and call Add. These |
79 // two methods may not be mixed, or duplicate IDs may be generated. | 81 // two methods may not be mixed, or duplicate IDs may be generated. |
80 // (This unique_ptr<> variant will not compile in IDMapExternalPointer mode.) | 82 void AddWithID(V data, KeyType id) { AddWithIDInternal(std::move(data), id); } |
81 void AddWithID(std::unique_ptr<T> data, KeyType id) { | |
82 AddWithIDInternal(std::move(data), id); | |
83 } | |
84 | |
85 // http://crbug.com/647091: Raw pointer Add()s in IDMapOwnPointer mode are | |
86 // deprecated. Users of IDMapOwnPointer should transition to the unique_ptr | |
87 // variant above, and the following methods should only be used in | |
88 // IDMapExternalPointer mode. | |
89 KeyType Add(T* data) { | |
90 return AddInternal(V(data)); | |
91 } | |
92 void AddWithID(T* data, KeyType id) { | |
93 AddWithIDInternal(V(data), id); | |
94 } | |
95 | 83 |
96 void Remove(KeyType id) { | 84 void Remove(KeyType id) { |
97 DCHECK(sequence_checker_.CalledOnValidSequence()); | 85 DCHECK(sequence_checker_.CalledOnValidSequence()); |
98 typename HashTable::iterator i = data_.find(id); | 86 typename HashTable::iterator i = data_.find(id); |
99 if (i == data_.end()) { | 87 if (i == data_.end()) { |
100 NOTREACHED() << "Attempting to remove an item not in the list"; | 88 NOTREACHED() << "Attempting to remove an item not in the list"; |
101 return; | 89 return; |
102 } | 90 } |
103 | 91 |
104 if (iteration_depth_ == 0) { | 92 if (iteration_depth_ == 0) { |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 | 263 |
276 // See description above setter. | 264 // See description above setter. |
277 bool check_on_null_data_; | 265 bool check_on_null_data_; |
278 | 266 |
279 base::SequenceChecker sequence_checker_; | 267 base::SequenceChecker sequence_checker_; |
280 | 268 |
281 DISALLOW_COPY_AND_ASSIGN(IDMap); | 269 DISALLOW_COPY_AND_ASSIGN(IDMap); |
282 }; | 270 }; |
283 | 271 |
284 #endif // BASE_ID_MAP_H_ | 272 #endif // BASE_ID_MAP_H_ |
OLD | NEW |