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 <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" |
12 #include "base/move.h" | 13 #include "base/move.h" |
13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
14 | 15 |
15 // ScopedVector wraps a vector deleting the elements from its | 16 // ScopedVector wraps a vector deleting the elements from its |
16 // destructor. | 17 // destructor. |
17 template <class T> | 18 template <class T> |
18 class ScopedVector { | 19 class ScopedVector { |
19 MOVE_ONLY_TYPE_FOR_CPP_03(ScopedVector, RValue) | 20 MOVE_ONLY_TYPE_FOR_CPP_03(ScopedVector, RValue) |
20 | 21 |
21 public: | 22 public: |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 const_iterator begin() const { return v_.begin(); } | 58 const_iterator begin() const { return v_.begin(); } |
58 iterator end() { return v_.end(); } | 59 iterator end() { return v_.end(); } |
59 const_iterator end() const { return v_.end(); } | 60 const_iterator end() const { return v_.end(); } |
60 | 61 |
61 const_reference front() const { return v_.front(); } | 62 const_reference front() const { return v_.front(); } |
62 reference front() { return v_.front(); } | 63 reference front() { return v_.front(); } |
63 const_reference back() const { return v_.back(); } | 64 const_reference back() const { return v_.back(); } |
64 reference back() { return v_.back(); } | 65 reference back() { return v_.back(); } |
65 | 66 |
66 void push_back(T* elem) { v_.push_back(elem); } | 67 void push_back(T* elem) { v_.push_back(elem); } |
| 68 void push_back(scoped_ptr<T> elem) { v_.push_back(elem.release()); } |
67 | 69 |
68 void pop_back() { | 70 void pop_back() { |
69 DCHECK(!empty()); | 71 DCHECK(!empty()); |
70 delete v_.back(); | 72 delete v_.back(); |
71 v_.pop_back(); | 73 v_.pop_back(); |
72 } | 74 } |
73 | 75 |
74 std::vector<T*>& get() { return v_; } | 76 std::vector<T*>& get() { return v_; } |
75 const std::vector<T*>& get() const { return v_; } | 77 const std::vector<T*>& get() const { return v_; } |
76 void swap(std::vector<T*>& other) { v_.swap(other); } | 78 void swap(std::vector<T*>& other) { v_.swap(other); } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 // Like |erase()|, but doesn't delete the elements in [first, last). | 130 // Like |erase()|, but doesn't delete the elements in [first, last). |
129 iterator weak_erase(iterator first, iterator last) { | 131 iterator weak_erase(iterator first, iterator last) { |
130 return v_.erase(first, last); | 132 return v_.erase(first, last); |
131 } | 133 } |
132 | 134 |
133 private: | 135 private: |
134 std::vector<T*> v_; | 136 std::vector<T*> v_; |
135 }; | 137 }; |
136 | 138 |
137 #endif // BASE_MEMORY_SCOPED_VECTOR_H_ | 139 #endif // BASE_MEMORY_SCOPED_VECTOR_H_ |
OLD | NEW |