OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_CONTAINERS_SCOPED_PTR_HASH_MAP_H_ | 5 #ifndef BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_ |
6 #define BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_ | 6 #define BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_ |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 | 18 |
19 // This type acts like a hash_map<K, scoped_ptr<V> >, based on top of | 19 // This type acts like a hash_map<K, scoped_ptr<V, D> >, based on top of |
20 // base::hash_map. The ScopedPtrHashMap has ownership of all values in the data | 20 // base::hash_map. The ScopedPtrHashMap has ownership of all values in the data |
21 // structure. | 21 // structure. |
22 template <typename Key, typename Value> | 22 template <typename Key, typename ScopedPtr> |
23 class ScopedPtrHashMap { | 23 class ScopedPtrHashMap { |
24 typedef base::hash_map<Key, Value*> Container; | 24 typedef base::hash_map<Key, typename ScopedPtr::element_type*> Container; |
25 | 25 |
26 public: | 26 public: |
27 typedef typename Container::key_type key_type; | 27 typedef typename Container::key_type key_type; |
28 typedef typename Container::mapped_type mapped_type; | 28 typedef typename Container::mapped_type mapped_type; |
29 typedef typename Container::value_type value_type; | 29 typedef typename Container::value_type value_type; |
30 typedef typename Container::iterator iterator; | 30 typedef typename Container::iterator iterator; |
31 typedef typename Container::const_iterator const_iterator; | 31 typedef typename Container::const_iterator const_iterator; |
32 | 32 |
33 ScopedPtrHashMap() {} | 33 ScopedPtrHashMap() {} |
34 | 34 |
35 ~ScopedPtrHashMap() { clear(); } | 35 ~ScopedPtrHashMap() { clear(); } |
36 | 36 |
37 void swap(ScopedPtrHashMap<Key, Value>& other) { | 37 void swap(ScopedPtrHashMap<Key, ScopedPtr>& other) { |
38 data_.swap(other.data_); | 38 data_.swap(other.data_); |
39 } | 39 } |
40 | 40 |
41 // Replaces value but not key if key is already present. | 41 // Replaces value but not key if key is already present. |
42 iterator set(const Key& key, scoped_ptr<Value> data) { | 42 iterator set(const Key& key, ScopedPtr data) { |
43 iterator it = find(key); | 43 iterator it = find(key); |
44 if (it != end()) { | 44 if (it != end()) { |
45 delete it->second; | 45 // Let ScopedPtr decide how to delete. For example, it may use custom |
| 46 // deleter. |
| 47 ScopedPtr(it->second).reset(); |
46 it->second = data.release(); | 48 it->second = data.release(); |
47 return it; | 49 return it; |
48 } | 50 } |
49 | 51 |
50 return data_.insert(std::make_pair(key, data.release())).first; | 52 return data_.insert(std::make_pair(key, data.release())).first; |
51 } | 53 } |
52 | 54 |
53 // Does nothing if key is already present | 55 // Does nothing if key is already present |
54 std::pair<iterator, bool> add(const Key& key, scoped_ptr<Value> data) { | 56 std::pair<iterator, bool> add(const Key& key, ScopedPtr data) { |
55 std::pair<iterator, bool> result = | 57 std::pair<iterator, bool> result = |
56 data_.insert(std::make_pair(key, data.get())); | 58 data_.insert(std::make_pair(key, data.get())); |
57 if (result.second) | 59 if (result.second) |
58 ignore_result(data.release()); | 60 ignore_result(data.release()); |
59 return result; | 61 return result; |
60 } | 62 } |
61 | 63 |
62 void erase(iterator it) { | 64 void erase(iterator it) { |
63 delete it->second; | 65 // Let ScopedPtr decide how to delete. |
| 66 ScopedPtr(it->second).reset(); |
64 data_.erase(it); | 67 data_.erase(it); |
65 } | 68 } |
66 | 69 |
67 size_t erase(const Key& k) { | 70 size_t erase(const Key& k) { |
68 iterator it = data_.find(k); | 71 iterator it = data_.find(k); |
69 if (it == data_.end()) | 72 if (it == data_.end()) |
70 return 0; | 73 return 0; |
71 erase(it); | 74 erase(it); |
72 return 1; | 75 return 1; |
73 } | 76 } |
74 | 77 |
75 scoped_ptr<Value> take(iterator it) { | 78 ScopedPtr take(iterator it) { |
76 DCHECK(it != data_.end()); | 79 DCHECK(it != data_.end()); |
77 if (it == data_.end()) | 80 if (it == data_.end()) |
78 return scoped_ptr<Value>(); | 81 return ScopedPtr(); |
79 | 82 |
80 scoped_ptr<Value> ret(it->second); | 83 ScopedPtr ret(it->second); |
81 it->second = NULL; | 84 it->second = NULL; |
82 return ret.Pass(); | 85 return ret.Pass(); |
83 } | 86 } |
84 | 87 |
85 scoped_ptr<Value> take(const Key& k) { | 88 ScopedPtr take(const Key& k) { |
86 iterator it = find(k); | 89 iterator it = find(k); |
87 if (it == data_.end()) | 90 if (it == data_.end()) |
88 return scoped_ptr<Value>(); | 91 return ScopedPtr(); |
89 | 92 |
90 return take(it); | 93 return take(it); |
91 } | 94 } |
92 | 95 |
93 scoped_ptr<Value> take_and_erase(iterator it) { | 96 ScopedPtr take_and_erase(iterator it) { |
94 DCHECK(it != data_.end()); | 97 DCHECK(it != data_.end()); |
95 if (it == data_.end()) | 98 if (it == data_.end()) |
96 return scoped_ptr<Value>(); | 99 return ScopedPtr(); |
97 | 100 |
98 scoped_ptr<Value> ret(it->second); | 101 ScopedPtr ret(it->second); |
99 data_.erase(it); | 102 data_.erase(it); |
100 return ret.Pass(); | 103 return ret.Pass(); |
101 } | 104 } |
102 | 105 |
103 scoped_ptr<Value> take_and_erase(const Key& k) { | 106 ScopedPtr take_and_erase(const Key& k) { |
104 iterator it = find(k); | 107 iterator it = find(k); |
105 if (it == data_.end()) | 108 if (it == data_.end()) |
106 return scoped_ptr<Value>(); | 109 return ScopedPtr(); |
107 | 110 |
108 return take_and_erase(it); | 111 return take_and_erase(it); |
109 } | 112 } |
110 | 113 |
111 // Returns the element in the hash_map that matches the given key. | 114 // Returns the element in the hash_map that matches the given key. |
112 // If no such element exists it returns NULL. | 115 // If no such element exists it returns NULL. |
113 Value* get(const Key& k) const { | 116 typename ScopedPtr::element_type* get(const Key& k) const { |
114 const_iterator it = find(k); | 117 const_iterator it = find(k); |
115 if (it == end()) | 118 if (it == end()) |
116 return NULL; | 119 return NULL; |
117 return it->second; | 120 return it->second; |
118 } | 121 } |
119 | 122 |
120 inline bool contains(const Key& k) const { return data_.count(k) > 0; } | 123 inline bool contains(const Key& k) const { return data_.count(k) > 0; } |
121 | 124 |
122 inline void clear() { STLDeleteValues(&data_); } | 125 inline void clear() { |
| 126 auto it = data_.begin(); |
| 127 while (it != data_.end()) { |
| 128 // NOTE: Like STLDeleteContainerPointers, deleting behind the iterator. |
| 129 // Deleting the value does not always invalidate the iterator, but it may |
| 130 // do so if the key is a pointer into the value object. |
| 131 auto temp = it; |
| 132 ++it; |
| 133 // Let ScopedPtr decide how to delete. |
| 134 ScopedPtr(temp->second).reset(); |
| 135 } |
| 136 data_.clear(); |
| 137 } |
123 | 138 |
124 inline const_iterator find(const Key& k) const { return data_.find(k); } | 139 inline const_iterator find(const Key& k) const { return data_.find(k); } |
125 inline iterator find(const Key& k) { return data_.find(k); } | 140 inline iterator find(const Key& k) { return data_.find(k); } |
126 | 141 |
127 inline size_t count(const Key& k) const { return data_.count(k); } | 142 inline size_t count(const Key& k) const { return data_.count(k); } |
128 inline std::pair<const_iterator, const_iterator> equal_range( | 143 inline std::pair<const_iterator, const_iterator> equal_range( |
129 const Key& k) const { | 144 const Key& k) const { |
130 return data_.equal_range(k); | 145 return data_.equal_range(k); |
131 } | 146 } |
132 inline std::pair<iterator, iterator> equal_range(const Key& k) { | 147 inline std::pair<iterator, iterator> equal_range(const Key& k) { |
(...skipping 15 matching lines...) Expand all Loading... |
148 | 163 |
149 private: | 164 private: |
150 Container data_; | 165 Container data_; |
151 | 166 |
152 DISALLOW_COPY_AND_ASSIGN(ScopedPtrHashMap); | 167 DISALLOW_COPY_AND_ASSIGN(ScopedPtrHashMap); |
153 }; | 168 }; |
154 | 169 |
155 } // namespace base | 170 } // namespace base |
156 | 171 |
157 #endif // BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_ | 172 #endif // BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_ |
OLD | NEW |