| 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> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/move.h" | |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 | 18 |
| 19 // ScopedPtrMap provides a std::map that supports scoped_ptr values. It ensures | 19 // ScopedPtrMap provides a std::map that supports scoped_ptr values. It ensures |
| 20 // that the map's values are properly deleted when removed from the map, or when | 20 // that the map's values are properly deleted when removed from the map, or when |
| 21 // the map is destroyed. | 21 // the map is destroyed. |
| 22 // | 22 // |
| 23 // |ScopedPtr| must be a type scoped_ptr<T>. This is for compatibility with | 23 // |ScopedPtr| must be a type scoped_ptr<T>. This is for compatibility with |
| 24 // std::map in C++11. | 24 // std::map in C++11. |
| 25 // | 25 // |
| 26 // TODO(http://crbug.com/554291): DEPRECATED: Use std::map instead (now that we | 26 // TODO(http://crbug.com/554291): DEPRECATED: Use std::map instead (now that we |
| 27 // have support for moveable types inside containers). | 27 // have support for moveable types inside containers). |
| 28 template <class Key, class ScopedPtr, class Compare = std::less<Key>> | 28 template <class Key, class ScopedPtr, class Compare = std::less<Key>> |
| 29 class ScopedPtrMap { | 29 class ScopedPtrMap { |
| 30 MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(ScopedPtrMap) | |
| 31 | |
| 32 using Container = std::map<Key, typename ScopedPtr::element_type*, Compare>; | 30 using Container = std::map<Key, typename ScopedPtr::element_type*, Compare>; |
| 33 | |
| 34 public: | 31 public: |
| 35 using allocator_type = typename Container::allocator_type; | 32 using allocator_type = typename Container::allocator_type; |
| 36 using size_type = typename Container::size_type; | 33 using size_type = typename Container::size_type; |
| 37 using difference_type = typename Container::difference_type; | 34 using difference_type = typename Container::difference_type; |
| 38 using reference = typename Container::reference; | 35 using reference = typename Container::reference; |
| 39 using const_reference = typename Container::const_reference; | 36 using const_reference = typename Container::const_reference; |
| 40 using key_type = typename Container::key_type; | 37 using key_type = typename Container::key_type; |
| 41 using mapped_type = ScopedPtr; | 38 using mapped_type = ScopedPtr; |
| 42 using key_compare = typename Container::key_compare; | 39 using key_compare = typename Container::key_compare; |
| 43 using const_iterator = typename Container::const_iterator; | 40 using const_iterator = typename Container::const_iterator; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 private: | 130 private: |
| 134 Container data_; | 131 Container data_; |
| 135 | 132 |
| 136 typename Container::iterator ConstIteratorToIterator(const_iterator it) { | 133 typename Container::iterator ConstIteratorToIterator(const_iterator it) { |
| 137 // This is the only way to convert a const iterator to a non-const iterator | 134 // 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). | 135 // in C++03 (get the key and do the lookup again). |
| 139 if (it == data_.end()) | 136 if (it == data_.end()) |
| 140 return data_.end(); | 137 return data_.end(); |
| 141 return data_.find(it->first); | 138 return data_.find(it->first); |
| 142 }; | 139 }; |
| 140 |
| 141 DISALLOW_COPY_AND_ASSIGN(ScopedPtrMap); |
| 143 }; | 142 }; |
| 144 | 143 |
| 145 } // namespace base | 144 } // namespace base |
| 146 | 145 |
| 147 #endif // BASE_CONTAINERS_SCOPED_PTR_MAP_H_ | 146 #endif // BASE_CONTAINERS_SCOPED_PTR_MAP_H_ |
| OLD | NEW |