OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 CC_BASE_SCOPED_PTR_VECTOR_H_ | 5 #ifndef CC_BASE_SCOPED_PTR_VECTOR_H_ |
6 #define CC_BASE_SCOPED_PTR_VECTOR_H_ | 6 #define CC_BASE_SCOPED_PTR_VECTOR_H_ |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 | 15 |
16 namespace cc { | 16 namespace cc { |
17 | 17 |
18 // This type acts like a vector<scoped_ptr> based on top of std::vector. The | 18 // This type acts like a vector<scoped_ptr> based on top of std::vector. The |
19 // ScopedPtrVector has ownership of all elements in the vector. | 19 // ScopedPtrVector has ownership of all elements in the vector. |
20 template <typename T> | 20 template <typename T> |
21 class ScopedPtrVector { | 21 class ScopedPtrVector { |
22 public: | 22 public: |
23 typedef typename std::vector<const T*>::const_iterator const_iterator; | 23 typedef typename std::vector<T*>::const_iterator const_iterator; |
24 typedef typename std::vector<T*>::reverse_iterator reverse_iterator; | 24 typedef typename std::vector<T*>::reverse_iterator reverse_iterator; |
25 typedef typename std::vector<T*>::const_reverse_iterator | 25 typedef typename std::vector<T*>::const_reverse_iterator |
26 const_reverse_iterator; | 26 const_reverse_iterator; |
27 | 27 |
28 #if defined(OS_ANDROID) | 28 #if defined(OS_ANDROID) |
29 // On Android the iterator is not a class, so we can't block assignment. | 29 // On Android the iterator is not a class, so we can't block assignment. |
30 typedef typename std::vector<T*>::iterator iterator; | 30 typedef typename std::vector<T*>::iterator iterator; |
31 #else | 31 #else |
32 // Ban setting values on the iterator directly. New pointers must be passed | 32 // Ban setting values on the iterator directly. New pointers must be passed |
33 // to methods on the ScopedPtrVector class to appear in the vector. | 33 // to methods on the ScopedPtrVector class to appear in the vector. |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 void pop_back() { | 122 void pop_back() { |
123 delete data_.back(); | 123 delete data_.back(); |
124 data_.pop_back(); | 124 data_.pop_back(); |
125 } | 125 } |
126 | 126 |
127 void insert(iterator position, scoped_ptr<T> item) { | 127 void insert(iterator position, scoped_ptr<T> item) { |
128 DCHECK(position <= end()); | 128 DCHECK(position <= end()); |
129 data_.insert(position, item.release()); | 129 data_.insert(position, item.release()); |
130 } | 130 } |
131 | 131 |
132 void insert_and_take(iterator position, | 132 void insert_and_take(iterator position, ScopedPtrVector<T>* other) { |
133 ScopedPtrVector<T>& other) { | |
134 std::vector<T*> tmp_data; | 133 std::vector<T*> tmp_data; |
135 for (ScopedPtrVector<T>::iterator it = other.begin(); | 134 for (ScopedPtrVector<T>::iterator it = other->begin(); it != other->end(); |
136 it != other.end(); | |
137 ++it) { | 135 ++it) { |
138 tmp_data.push_back(other.take(it).release()); | 136 tmp_data.push_back(other->take(it).release()); |
139 } | 137 } |
140 data_.insert(position, tmp_data.begin(), tmp_data.end()); | 138 data_.insert(position, tmp_data.begin(), tmp_data.end()); |
141 } | 139 } |
142 | 140 |
143 template <typename Predicate> | 141 template <typename Predicate> |
144 iterator partition(Predicate predicate) { | 142 iterator partition(Predicate predicate) { |
145 typename std::vector<T*>::iterator first = begin(); | 143 typename std::vector<T*>::iterator first = begin(); |
146 typename std::vector<T*>::iterator last = end(); | 144 typename std::vector<T*>::iterator last = end(); |
147 return static_cast<iterator>(std::partition(first, last, predicate)); | 145 return static_cast<iterator>(std::partition(first, last, predicate)); |
148 } | 146 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 191 |
194 private: | 192 private: |
195 std::vector<T*> data_; | 193 std::vector<T*> data_; |
196 | 194 |
197 DISALLOW_COPY_AND_ASSIGN(ScopedPtrVector); | 195 DISALLOW_COPY_AND_ASSIGN(ScopedPtrVector); |
198 }; | 196 }; |
199 | 197 |
200 } // namespace cc | 198 } // namespace cc |
201 | 199 |
202 #endif // CC_BASE_SCOPED_PTR_VECTOR_H_ | 200 #endif // CC_BASE_SCOPED_PTR_VECTOR_H_ |
OLD | NEW |