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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
75 | 80 |
76 // Adds a new data member with the specified ID. The ID must not be in | 81 // 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 | 82 // 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 | 83 // 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. | 84 // two methods may not be mixed, or duplicate IDs may be generated. |
80 // (This unique_ptr<> variant will not compile in IDMapExternalPointer mode.) | 85 // (This unique_ptr<> variant will not compile in IDMapExternalPointer mode.) |
81 void AddWithID(std::unique_ptr<T> data, KeyType id) { | 86 void AddWithID(std::unique_ptr<T> data, KeyType id) { |
82 AddWithIDInternal(std::move(data), id); | 87 AddWithIDInternal(std::move(data), id); |
83 } | 88 } |
84 | 89 |
85 // http://crbug.com/647091: Raw pointer Add()s in IDMapOwnPointer mode are | 90 // The following two methods are used in IDMapExternalPointer mode. |
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) { | 91 KeyType Add(T* data) { |
aelias_OOO_until_Jul13
2016/11/08 01:55:09
I don't think the explicit overloading is needed a
| |
90 return AddInternal(V(data)); | 92 return AddInternal(data); |
91 } | 93 } |
92 void AddWithID(T* data, KeyType id) { | 94 void AddWithID(T* data, KeyType id) { |
93 AddWithIDInternal(V(data), id); | 95 AddWithIDInternal(data, id); |
94 } | 96 } |
95 | 97 |
96 void Remove(KeyType id) { | 98 void Remove(KeyType id) { |
97 DCHECK(sequence_checker_.CalledOnValidSequence()); | 99 DCHECK(sequence_checker_.CalledOnValidSequence()); |
98 typename HashTable::iterator i = data_.find(id); | 100 typename HashTable::iterator i = data_.find(id); |
99 if (i == data_.end()) { | 101 if (i == data_.end()) { |
100 NOTREACHED() << "Attempting to remove an item not in the list"; | 102 NOTREACHED() << "Attempting to remove an item not in the list"; |
101 return; | 103 return; |
102 } | 104 } |
103 | 105 |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
275 | 277 |
276 // See description above setter. | 278 // See description above setter. |
277 bool check_on_null_data_; | 279 bool check_on_null_data_; |
278 | 280 |
279 base::SequenceChecker sequence_checker_; | 281 base::SequenceChecker sequence_checker_; |
280 | 282 |
281 DISALLOW_COPY_AND_ASSIGN(IDMap); | 283 DISALLOW_COPY_AND_ASSIGN(IDMap); |
282 }; | 284 }; |
283 | 285 |
284 #endif // BASE_ID_MAP_H_ | 286 #endif // BASE_ID_MAP_H_ |
OLD | NEW |