OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_MEMORY_SCOPED_VECTOR_H_ | 5 #ifndef BASE_MEMORY_SCOPED_VECTOR_H_ |
6 #define BASE_MEMORY_SCOPED_VECTOR_H_ | 6 #define BASE_MEMORY_SCOPED_VECTOR_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/stl_util.h" | |
16 | 15 |
17 // ScopedVector wraps a vector deleting the elements from its | 16 // ScopedVector wraps a vector deleting the elements from its |
18 // destructor. | 17 // destructor. |
19 // | 18 // |
20 // TODO(http://crbug.com/554289): DEPRECATED: Use std::vector instead (now that | 19 // TODO(http://crbug.com/554289): DEPRECATED: Use std::vector instead (now that |
21 // we have support for moveable types inside containers). | 20 // we have support for moveable types inside containers). |
22 template <class T> | 21 template <class T> |
23 class ScopedVector { | 22 class ScopedVector { |
24 public: | 23 public: |
25 typedef typename std::vector<T*>::allocator_type allocator_type; | 24 typedef typename std::vector<T*>::allocator_type allocator_type; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 void swap(ScopedVector<T>& other) { v_.swap(other.v_); } | 80 void swap(ScopedVector<T>& other) { v_.swap(other.v_); } |
82 void release(std::vector<T*>* out) { | 81 void release(std::vector<T*>* out) { |
83 out->swap(v_); | 82 out->swap(v_); |
84 v_.clear(); | 83 v_.clear(); |
85 } | 84 } |
86 | 85 |
87 void reserve(size_t capacity) { v_.reserve(capacity); } | 86 void reserve(size_t capacity) { v_.reserve(capacity); } |
88 | 87 |
89 // Resize, deleting elements in the disappearing range if we are shrinking. | 88 // Resize, deleting elements in the disappearing range if we are shrinking. |
90 void resize(size_t new_size) { | 89 void resize(size_t new_size) { |
91 if (v_.size() > new_size) | 90 if (v_.size() > new_size) { |
92 base::STLDeleteContainerPointers(v_.begin() + new_size, v_.end()); | 91 for (auto it = v_.begin() + new_size; it != v_.end(); ++it) |
| 92 delete *it; |
| 93 } |
93 v_.resize(new_size); | 94 v_.resize(new_size); |
94 } | 95 } |
95 | 96 |
96 template<typename InputIterator> | 97 template<typename InputIterator> |
97 void assign(InputIterator begin, InputIterator end) { | 98 void assign(InputIterator begin, InputIterator end) { |
98 v_.assign(begin, end); | 99 v_.assign(begin, end); |
99 } | 100 } |
100 | 101 |
101 void clear() { base::STLDeleteElements(&v_); } | 102 void clear() { |
| 103 for (auto* item : *this) |
| 104 delete item; |
| 105 v_.clear(); |
| 106 } |
102 | 107 |
103 // Like |clear()|, but doesn't delete any elements. | 108 // Like |clear()|, but doesn't delete any elements. |
104 void weak_clear() { v_.clear(); } | 109 void weak_clear() { v_.clear(); } |
105 | 110 |
106 // Lets the ScopedVector take ownership of |x|. | 111 // Lets the ScopedVector take ownership of |x|. |
107 iterator insert(iterator position, T* x) { | 112 iterator insert(iterator position, T* x) { |
108 return v_.insert(position, x); | 113 return v_.insert(position, x); |
109 } | 114 } |
110 | 115 |
111 iterator insert(iterator position, std::unique_ptr<T> x) { | 116 iterator insert(iterator position, std::unique_ptr<T> x) { |
112 return v_.insert(position, x.release()); | 117 return v_.insert(position, x.release()); |
113 } | 118 } |
114 | 119 |
115 // Lets the ScopedVector take ownership of elements in [first,last). | 120 // Lets the ScopedVector take ownership of elements in [first,last). |
116 template<typename InputIterator> | 121 template<typename InputIterator> |
117 void insert(iterator position, InputIterator first, InputIterator last) { | 122 void insert(iterator position, InputIterator first, InputIterator last) { |
118 v_.insert(position, first, last); | 123 v_.insert(position, first, last); |
119 } | 124 } |
120 | 125 |
121 iterator erase(iterator position) { | 126 iterator erase(iterator position) { |
122 delete *position; | 127 delete *position; |
123 return v_.erase(position); | 128 return v_.erase(position); |
124 } | 129 } |
125 | 130 |
126 iterator erase(iterator first, iterator last) { | 131 iterator erase(iterator first, iterator last) { |
127 base::STLDeleteContainerPointers(first, last); | 132 for (auto it = first; it != last; ++it) |
| 133 delete *it; |
128 return v_.erase(first, last); | 134 return v_.erase(first, last); |
129 } | 135 } |
130 | 136 |
131 // Like |erase()|, but doesn't delete the element at |position|. | 137 // Like |erase()|, but doesn't delete the element at |position|. |
132 iterator weak_erase(iterator position) { | 138 iterator weak_erase(iterator position) { |
133 return v_.erase(position); | 139 return v_.erase(position); |
134 } | 140 } |
135 | 141 |
136 // Like |erase()|, but doesn't delete the elements in [first, last). | 142 // Like |erase()|, but doesn't delete the elements in [first, last). |
137 iterator weak_erase(iterator first, iterator last) { | 143 iterator weak_erase(iterator first, iterator last) { |
138 return v_.erase(first, last); | 144 return v_.erase(first, last); |
139 } | 145 } |
140 | 146 |
141 private: | 147 private: |
142 std::vector<T*> v_; | 148 std::vector<T*> v_; |
143 | 149 |
144 DISALLOW_COPY_AND_ASSIGN(ScopedVector); | 150 DISALLOW_COPY_AND_ASSIGN(ScopedVector); |
145 }; | 151 }; |
146 | 152 |
147 #endif // BASE_MEMORY_SCOPED_VECTOR_H_ | 153 #endif // BASE_MEMORY_SCOPED_VECTOR_H_ |
OLD | NEW |