OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_MAP_H_ | 5 #ifndef BASE_CONTAINERS_SCOPED_PTR_MAP_H_ |
6 #define BASE_CONTAINERS_SCOPED_PTR_MAP_H_ | 6 #define BASE_CONTAINERS_SCOPED_PTR_MAP_H_ |
7 | 7 |
8 #include <functional> | 8 #include <functional> |
9 #include <map> | 9 #include <map> |
10 #include <utility> | 10 #include <utility> |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 | 109 |
110 // Like |erase()|, but returns the element instead of deleting it. | 110 // Like |erase()|, but returns the element instead of deleting it. |
111 ScopedPtr take_and_erase(const_iterator position) { | 111 ScopedPtr take_and_erase(const_iterator position) { |
112 DCHECK(position != end()); | 112 DCHECK(position != end()); |
113 if (position == end()) | 113 if (position == end()) |
114 return ScopedPtr(); | 114 return ScopedPtr(); |
115 | 115 |
116 ScopedPtr ret(position->second); | 116 ScopedPtr ret(position->second); |
117 // Key-based lookup (cannot use const_iterator overload in C++03 library). | 117 // Key-based lookup (cannot use const_iterator overload in C++03 library). |
118 data_.erase(position->first); | 118 data_.erase(position->first); |
119 return ret.Pass(); | 119 return ret; |
120 } | 120 } |
121 | 121 |
122 // Like |erase()|, but returns the element instead of deleting it. | 122 // Like |erase()|, but returns the element instead of deleting it. |
123 ScopedPtr take_and_erase(const Key& k) { | 123 ScopedPtr take_and_erase(const Key& k) { |
124 typename Container::iterator it = data_.find(k); | 124 typename Container::iterator it = data_.find(k); |
125 if (it == end()) | 125 if (it == end()) |
126 return ScopedPtr(); | 126 return ScopedPtr(); |
127 | 127 |
128 ScopedPtr ret(it->second); | 128 ScopedPtr ret(it->second); |
129 data_.erase(it); | 129 data_.erase(it); |
130 return ret.Pass(); | 130 return ret; |
131 } | 131 } |
132 | 132 |
133 private: | 133 private: |
134 Container data_; | 134 Container data_; |
135 | 135 |
136 typename Container::iterator ConstIteratorToIterator(const_iterator it) { | 136 typename Container::iterator ConstIteratorToIterator(const_iterator it) { |
137 // This is the only way to convert a const iterator to a non-const iterator | 137 // This is the only way to convert a const iterator to a non-const iterator |
138 // in C++03 (get the key and do the lookup again). | 138 // in C++03 (get the key and do the lookup again). |
139 if (it == data_.end()) | 139 if (it == data_.end()) |
140 return data_.end(); | 140 return data_.end(); |
141 return data_.find(it->first); | 141 return data_.find(it->first); |
142 }; | 142 }; |
143 }; | 143 }; |
144 | 144 |
145 } // namespace base | 145 } // namespace base |
146 | 146 |
147 #endif // BASE_CONTAINERS_SCOPED_PTR_MAP_H_ | 147 #endif // BASE_CONTAINERS_SCOPED_PTR_MAP_H_ |
OLD | NEW |