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_SCOPED_PTR_VECTOR_H_ | 5 #ifndef CC_SCOPED_PTR_VECTOR_H_ |
6 #define CC_SCOPED_PTR_VECTOR_H_ | 6 #define CC_SCOPED_PTR_VECTOR_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 | 12 |
13 namespace cc { | 13 namespace cc { |
14 | 14 |
15 // This type acts like a vector<scoped_ptr> based on top of std::vector. The | 15 // This type acts like a vector<scoped_ptr> based on top of std::vector. The |
16 // ScopedPtrVector has ownership of all elements in the vector. | 16 // ScopedPtrVector has ownership of all elements in the vector. |
17 template <typename T> | 17 template <typename T> |
18 class ScopedPtrVector { | 18 class ScopedPtrVector { |
19 public: | 19 public: |
20 typedef typename std::vector<T*>::iterator iterator; | |
21 typedef typename std::vector<T*>::const_iterator const_iterator; | 20 typedef typename std::vector<T*>::const_iterator const_iterator; |
22 typedef typename std::vector<T*>::reverse_iterator reverse_iterator; | 21 typedef typename std::vector<T*>::reverse_iterator reverse_iterator; |
23 typedef typename std::vector<T*>::const_reverse_iterator | 22 typedef typename std::vector<T*>::const_reverse_iterator |
24 const_reverse_iterator; | 23 const_reverse_iterator; |
25 | 24 |
| 25 #if defined(OS_ANDROID) |
| 26 // On Android the iterator is not a class, so we can't block assignment. |
| 27 typedef typename std::vector<T*>::iterator iterator; |
| 28 #else |
| 29 // Ban setting values on the iterator directly. New pointers must be passed |
| 30 // to methods on the ScopedPtrVector class to appear in the vector. |
| 31 class iterator : public std::vector<T*>::iterator { |
| 32 public: |
| 33 iterator(const typename std::vector<T*>::iterator& other) |
| 34 : std::vector<T*>::iterator(other) {} |
| 35 T* const& operator*() { return std::vector<T*>::iterator::operator*(); } |
| 36 }; |
| 37 #endif |
| 38 |
26 ScopedPtrVector() {} | 39 ScopedPtrVector() {} |
27 | 40 |
28 ~ScopedPtrVector() { clear(); } | 41 ~ScopedPtrVector() { clear(); } |
29 | 42 |
30 size_t size() const { | 43 size_t size() const { |
31 return data_.size(); | 44 return data_.size(); |
32 } | 45 } |
33 | 46 |
34 T* Peek(size_t index) const { | 47 T* at(size_t index) const { |
35 DCHECK(index < size()); | 48 DCHECK(index < size()); |
36 return data_[index]; | 49 return data_[index]; |
37 } | 50 } |
38 | 51 |
39 T* operator[](size_t index) const { | 52 T* operator[](size_t index) const { |
40 return Peek(index); | 53 return at(index); |
41 } | 54 } |
42 | 55 |
43 T* first() const { | 56 T* front() const { |
44 DCHECK(!isEmpty()); | 57 DCHECK(!empty()); |
45 return Peek(0); | 58 return at(0); |
46 } | 59 } |
47 | 60 |
48 T* last() const { | 61 T* back() const { |
49 DCHECK(!isEmpty()); | 62 DCHECK(!empty()); |
50 return Peek(size() - 1); | 63 return at(size() - 1); |
51 } | 64 } |
52 | 65 |
53 bool isEmpty() const { | 66 bool empty() const { |
54 return size() == 0; | 67 return data_.empty(); |
55 } | 68 } |
56 | 69 |
57 scoped_ptr<T> take(size_t index) { | 70 scoped_ptr<T> take(iterator position) { |
58 DCHECK(index < size()); | 71 if (position == end()) |
59 scoped_ptr<T> ret(data_[index]); | 72 return scoped_ptr<T>(NULL); |
60 data_[index] = NULL; | 73 DCHECK(position < end()); |
| 74 |
| 75 typename std::vector<T*>::iterator writable_position = position; |
| 76 scoped_ptr<T> ret(*writable_position); |
| 77 *writable_position = NULL; |
61 return ret.Pass(); | 78 return ret.Pass(); |
62 } | 79 } |
63 | 80 |
64 void remove(size_t index) { | 81 scoped_ptr<T> take_back() { |
65 DCHECK(index < size()); | 82 DCHECK(!empty()); |
66 delete data_[index]; | 83 if (empty()) |
67 data_.erase(data_.begin() + index); | 84 return scoped_ptr<T>(NULL); |
| 85 return take(end() - 1); |
| 86 } |
| 87 |
| 88 void erase(iterator position) { |
| 89 if (position == end()) |
| 90 return; |
| 91 typename std::vector<T*>::iterator writable_position = position; |
| 92 delete *writable_position; |
| 93 data_.erase(position); |
| 94 } |
| 95 |
| 96 void erase(iterator first, iterator last) { |
| 97 DCHECK(first <= last); |
| 98 for (iterator it = first; it != last; ++it) { |
| 99 DCHECK(it < end()); |
| 100 |
| 101 typename std::vector<T*>::iterator writable_it = it; |
| 102 delete *writable_it; |
| 103 } |
| 104 data_.erase(first, last); |
68 } | 105 } |
69 | 106 |
70 void reserve(size_t size) { | 107 void reserve(size_t size) { |
71 data_.reserve(size); | 108 data_.reserve(size); |
72 } | 109 } |
73 | 110 |
74 void clear() { | 111 void clear() { |
75 STLDeleteElements(&data_); | 112 STLDeleteElements(&data_); |
76 } | 113 } |
77 | 114 |
78 void append(scoped_ptr<T> item) { | 115 void push_back(scoped_ptr<T> item) { |
79 data_.push_back(item.release()); | 116 data_.push_back(item.release()); |
80 } | 117 } |
81 | 118 |
82 void insert(size_t index, scoped_ptr<T> item) { | 119 void pop_back() { |
83 DCHECK(index <= size()); | 120 data_.pop_back(); |
84 data_.insert(data_.begin() + index, item.release()); | 121 } |
| 122 |
| 123 void insert(iterator position, scoped_ptr<T> item) { |
| 124 DCHECK(position <= end()); |
| 125 data_.insert(position, item.release()); |
85 } | 126 } |
86 | 127 |
87 void swap(ScopedPtrVector<T>& other) { | 128 void swap(ScopedPtrVector<T>& other) { |
88 data_.swap(other.data_); | 129 data_.swap(other.data_); |
89 } | 130 } |
90 | 131 |
91 iterator begin() { return data_.begin(); } | 132 void swap(iterator a, iterator b) { |
| 133 DCHECK(a < end()); |
| 134 DCHECK(b < end()); |
| 135 if (a == end() || b == end() || a == b) |
| 136 return; |
| 137 typename std::vector<T*>::iterator writable_a = a; |
| 138 typename std::vector<T*>::iterator writable_b = b; |
| 139 std::swap(*writable_a, *writable_b); |
| 140 } |
| 141 |
| 142 iterator begin() { return static_cast<iterator>(data_.begin()); } |
92 const_iterator begin() const { return data_.begin(); } | 143 const_iterator begin() const { return data_.begin(); } |
93 iterator end() { return data_.end(); } | 144 iterator end() { return static_cast<iterator>(data_.end()); } |
94 const_iterator end() const { return data_.end(); } | 145 const_iterator end() const { return data_.end(); } |
95 | 146 |
96 reverse_iterator rbegin() { return data_.rbegin(); } | 147 reverse_iterator rbegin() { return data_.rbegin(); } |
97 const_reverse_iterator rbegin() const { return data_.rbegin(); } | 148 const_reverse_iterator rbegin() const { return data_.rbegin(); } |
98 reverse_iterator rend() { return data_.rend(); } | 149 reverse_iterator rend() { return data_.rend(); } |
99 const_reverse_iterator rend() const { return data_.rend(); } | 150 const_reverse_iterator rend() const { return data_.rend(); } |
100 | 151 |
101 private: | 152 private: |
102 std::vector<T*> data_; | 153 std::vector<T*> data_; |
103 | 154 |
104 DISALLOW_COPY_AND_ASSIGN(ScopedPtrVector); | 155 DISALLOW_COPY_AND_ASSIGN(ScopedPtrVector); |
105 }; | 156 }; |
106 | 157 |
107 } // namespace cc | 158 } // namespace cc |
108 | 159 |
109 #endif // CC_SCOPED_PTR_VECTOR_H_ | 160 #endif // CC_SCOPED_PTR_VECTOR_H_ |
OLD | NEW |