Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: base/id_map.h

Issue 2480293004: Mandate unique_ptr for base::IDMap in IDMapOwnPointer mode. (Closed)
Patch Set: Fix typo breaking a bunch of trybot builds, oops Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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) {
72 KeyType Add(std::unique_ptr<T> data) {
73 return AddInternal(std::move(data)); 77 return AddInternal(std::move(data));
74 } 78 }
75 79
76 // Adds a new data member with the specified ID. The ID must not be in 80 // 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 81 // 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 82 // 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. 83 // two methods may not be mixed, or duplicate IDs may be generated.
80 // (This unique_ptr<> variant will not compile in IDMapExternalPointer mode.) 84 void AddWithID(V data, KeyType id) {
81 void AddWithID(std::unique_ptr<T> data, KeyType id) {
82 AddWithIDInternal(std::move(data), id); 85 AddWithIDInternal(std::move(data), id);
83 } 86 }
84 87
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
96 void Remove(KeyType id) { 88 void Remove(KeyType id) {
97 DCHECK(sequence_checker_.CalledOnValidSequence()); 89 DCHECK(sequence_checker_.CalledOnValidSequence());
98 typename HashTable::iterator i = data_.find(id); 90 typename HashTable::iterator i = data_.find(id);
99 if (i == data_.end()) { 91 if (i == data_.end()) {
100 NOTREACHED() << "Attempting to remove an item not in the list"; 92 NOTREACHED() << "Attempting to remove an item not in the list";
101 return; 93 return;
102 } 94 }
103 95
104 if (iteration_depth_ == 0) { 96 if (iteration_depth_ == 0) {
105 data_.erase(i); 97 data_.erase(i);
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 267
276 // See description above setter. 268 // See description above setter.
277 bool check_on_null_data_; 269 bool check_on_null_data_;
278 270
279 base::SequenceChecker sequence_checker_; 271 base::SequenceChecker sequence_checker_;
280 272
281 DISALLOW_COPY_AND_ASSIGN(IDMap); 273 DISALLOW_COPY_AND_ASSIGN(IDMap);
282 }; 274 };
283 275
284 #endif // BASE_ID_MAP_H_ 276 #endif // BASE_ID_MAP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698