| 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 <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/move.h" | 14 #include "base/move.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 | 16 |
| 17 // ScopedVector wraps a vector deleting the elements from its | 17 // ScopedVector wraps a vector deleting the elements from its |
| 18 // destructor. | 18 // destructor. |
| 19 // | 19 // |
| 20 // TODO(http://crbug.com/554289): DEPRECATED: Use std::vector instead (now that | 20 // TODO(http://crbug.com/554289): DEPRECATED: Use std::vector instead (now that |
| 21 // we have support for moveable types inside containers). | 21 // we have support for moveable types inside containers). |
| 22 template <class T> | 22 template <class T> |
| 23 class ScopedVector { | 23 class ScopedVector { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 const_iterator begin() const { return v_.begin(); } | 62 const_iterator begin() const { return v_.begin(); } |
| 63 iterator end() { return v_.end(); } | 63 iterator end() { return v_.end(); } |
| 64 const_iterator end() const { return v_.end(); } | 64 const_iterator end() const { return v_.end(); } |
| 65 | 65 |
| 66 const_reference front() const { return v_.front(); } | 66 const_reference front() const { return v_.front(); } |
| 67 reference front() { return v_.front(); } | 67 reference front() { return v_.front(); } |
| 68 const_reference back() const { return v_.back(); } | 68 const_reference back() const { return v_.back(); } |
| 69 reference back() { return v_.back(); } | 69 reference back() { return v_.back(); } |
| 70 | 70 |
| 71 void push_back(T* elem) { v_.push_back(elem); } | 71 void push_back(T* elem) { v_.push_back(elem); } |
| 72 void push_back(scoped_ptr<T> elem) { v_.push_back(elem.release()); } | 72 void push_back(std::unique_ptr<T> elem) { v_.push_back(elem.release()); } |
| 73 | 73 |
| 74 void pop_back() { | 74 void pop_back() { |
| 75 DCHECK(!empty()); | 75 DCHECK(!empty()); |
| 76 delete v_.back(); | 76 delete v_.back(); |
| 77 v_.pop_back(); | 77 v_.pop_back(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 std::vector<T*>& get() { return v_; } | 80 std::vector<T*>& get() { return v_; } |
| 81 const std::vector<T*>& get() const { return v_; } | 81 const std::vector<T*>& get() const { return v_; } |
| 82 void swap(std::vector<T*>& other) { v_.swap(other); } | 82 void swap(std::vector<T*>& other) { v_.swap(other); } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 103 void clear() { STLDeleteElements(&v_); } | 103 void clear() { STLDeleteElements(&v_); } |
| 104 | 104 |
| 105 // Like |clear()|, but doesn't delete any elements. | 105 // Like |clear()|, but doesn't delete any elements. |
| 106 void weak_clear() { v_.clear(); } | 106 void weak_clear() { v_.clear(); } |
| 107 | 107 |
| 108 // Lets the ScopedVector take ownership of |x|. | 108 // Lets the ScopedVector take ownership of |x|. |
| 109 iterator insert(iterator position, T* x) { | 109 iterator insert(iterator position, T* x) { |
| 110 return v_.insert(position, x); | 110 return v_.insert(position, x); |
| 111 } | 111 } |
| 112 | 112 |
| 113 iterator insert(iterator position, scoped_ptr<T> x) { | 113 iterator insert(iterator position, std::unique_ptr<T> x) { |
| 114 return v_.insert(position, x.release()); | 114 return v_.insert(position, x.release()); |
| 115 } | 115 } |
| 116 | 116 |
| 117 // Lets the ScopedVector take ownership of elements in [first,last). | 117 // Lets the ScopedVector take ownership of elements in [first,last). |
| 118 template<typename InputIterator> | 118 template<typename InputIterator> |
| 119 void insert(iterator position, InputIterator first, InputIterator last) { | 119 void insert(iterator position, InputIterator first, InputIterator last) { |
| 120 v_.insert(position, first, last); | 120 v_.insert(position, first, last); |
| 121 } | 121 } |
| 122 | 122 |
| 123 iterator erase(iterator position) { | 123 iterator erase(iterator position) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 138 // Like |erase()|, but doesn't delete the elements in [first, last). | 138 // Like |erase()|, but doesn't delete the elements in [first, last). |
| 139 iterator weak_erase(iterator first, iterator last) { | 139 iterator weak_erase(iterator first, iterator last) { |
| 140 return v_.erase(first, last); | 140 return v_.erase(first, last); |
| 141 } | 141 } |
| 142 | 142 |
| 143 private: | 143 private: |
| 144 std::vector<T*> v_; | 144 std::vector<T*> v_; |
| 145 }; | 145 }; |
| 146 | 146 |
| 147 #endif // BASE_MEMORY_SCOPED_VECTOR_H_ | 147 #endif // BASE_MEMORY_SCOPED_VECTOR_H_ |
| OLD | NEW |