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 <map> | 9 #include <map> |
9 #include <utility> | 10 #include <utility> |
10 | 11 |
11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/move.h" | 14 #include "base/move.h" |
14 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
15 | 16 |
16 // ScopedPtrMap provides a std::map that supports scoped_ptr values. It ensures | 17 // ScopedPtrMap provides a std::map that supports scoped_ptr values. It ensures |
17 // that the map's values are properly deleted when removed from the map, or when | 18 // that the map's values are properly deleted when removed from the map, or when |
18 // the map is destroyed. | 19 // the map is destroyed. |
19 // | 20 // |
20 // |ScopedPtr| must be a type scoped_ptr<T>. This is for compatibility with | 21 // |ScopedPtr| must be a type scoped_ptr<T>. This is for compatibility with |
21 // std::map in C++11. | 22 // std::map in C++11. |
22 template <class Key, class ScopedPtr> | 23 template <class Key, class ScopedPtr, class Compare = std::less<Key>> |
23 class ScopedPtrMap { | 24 class ScopedPtrMap { |
24 MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(ScopedPtrMap) | 25 MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(ScopedPtrMap) |
25 | 26 |
26 using Container = std::map<Key, typename ScopedPtr::element_type*>; | 27 using Container = std::map<Key, typename ScopedPtr::element_type*, Compare>; |
27 | 28 |
28 public: | 29 public: |
29 using allocator_type = typename Container::allocator_type; | 30 using allocator_type = typename Container::allocator_type; |
30 using size_type = typename Container::size_type; | 31 using size_type = typename Container::size_type; |
31 using difference_type = typename Container::difference_type; | 32 using difference_type = typename Container::difference_type; |
32 using reference = typename Container::reference; | 33 using reference = typename Container::reference; |
33 using const_reference = typename Container::const_reference; | 34 using const_reference = typename Container::const_reference; |
34 using key_type = typename Container::key_type; | 35 using key_type = typename Container::key_type; |
| 36 using mapped_type = ScopedPtr; |
| 37 using key_compare = typename Container::key_compare; |
35 using const_iterator = typename Container::const_iterator; | 38 using const_iterator = typename Container::const_iterator; |
36 using const_reverse_iterator = typename Container::const_reverse_iterator; | 39 using const_reverse_iterator = typename Container::const_reverse_iterator; |
37 | 40 |
38 ScopedPtrMap() {} | 41 ScopedPtrMap() {} |
39 ~ScopedPtrMap() { clear(); } | 42 ~ScopedPtrMap() { clear(); } |
40 ScopedPtrMap(ScopedPtrMap<Key, ScopedPtr>&& other) { swap(other); } | 43 ScopedPtrMap(ScopedPtrMap<Key, ScopedPtr>&& other) { swap(other); } |
41 | 44 |
42 ScopedPtrMap& operator=(ScopedPtrMap<Key, ScopedPtr>&& rhs) { | 45 ScopedPtrMap& operator=(ScopedPtrMap<Key, ScopedPtr>&& rhs) { |
43 swap(rhs); | 46 swap(rhs); |
44 return *this; | 47 return *this; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 typename Container::iterator ConstIteratorToIterator(const_iterator it) { | 131 typename Container::iterator ConstIteratorToIterator(const_iterator it) { |
129 // This is the only way to convert a const iterator to a non-const iterator | 132 // This is the only way to convert a const iterator to a non-const iterator |
130 // in C++03 (get the key and do the lookup again). | 133 // in C++03 (get the key and do the lookup again). |
131 if (it == data_.end()) | 134 if (it == data_.end()) |
132 return data_.end(); | 135 return data_.end(); |
133 return data_.find(it->first); | 136 return data_.find(it->first); |
134 }; | 137 }; |
135 }; | 138 }; |
136 | 139 |
137 #endif // BASE_CONTAINERS_SCOPED_PTR_MAP_H_ | 140 #endif // BASE_CONTAINERS_SCOPED_PTR_MAP_H_ |
OLD | NEW |