Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(549)

Side by Side Diff: cc/scoped_ptr_vector.h

Issue 12472028: Part 1 of cc/ directory shuffles: base (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef CC_SCOPED_PTR_VECTOR_H_
6 #define CC_SCOPED_PTR_VECTOR_H_
7
8 #include "base/basictypes.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/stl_util.h"
12
13 namespace cc {
14
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.
17 template <typename T>
18 class ScopedPtrVector {
19 public:
20 typedef typename std::vector<T*>::const_iterator const_iterator;
21 typedef typename std::vector<T*>::reverse_iterator reverse_iterator;
22 typedef typename std::vector<T*>::const_reverse_iterator
23 const_reverse_iterator;
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
39 ScopedPtrVector() {}
40
41 ~ScopedPtrVector() { clear(); }
42
43 size_t size() const {
44 return data_.size();
45 }
46
47 T* at(size_t index) const {
48 DCHECK(index < size());
49 return data_[index];
50 }
51
52 T* operator[](size_t index) const {
53 return at(index);
54 }
55
56 T* front() const {
57 DCHECK(!empty());
58 return at(0);
59 }
60
61 T* back() const {
62 DCHECK(!empty());
63 return at(size() - 1);
64 }
65
66 bool empty() const {
67 return data_.empty();
68 }
69
70 scoped_ptr<T> take(iterator position) {
71 if (position == end())
72 return scoped_ptr<T>(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;
78 return ret.Pass();
79 }
80
81 scoped_ptr<T> take_back() {
82 DCHECK(!empty());
83 if (empty())
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);
105 }
106
107 void reserve(size_t size) {
108 data_.reserve(size);
109 }
110
111 void clear() {
112 STLDeleteElements(&data_);
113 }
114
115 void push_back(scoped_ptr<T> item) {
116 data_.push_back(item.release());
117 }
118
119 void pop_back() {
120 delete data_.back();
121 data_.pop_back();
122 }
123
124 void insert(iterator position, scoped_ptr<T> item) {
125 DCHECK(position <= end());
126 data_.insert(position, item.release());
127 }
128
129 void swap(ScopedPtrVector<T>& other) {
130 data_.swap(other.data_);
131 }
132
133 void swap(iterator a, iterator b) {
134 DCHECK(a < end());
135 DCHECK(b < end());
136 if (a == end() || b == end() || a == b)
137 return;
138 typename std::vector<T*>::iterator writable_a = a;
139 typename std::vector<T*>::iterator writable_b = b;
140 std::swap(*writable_a, *writable_b);
141 }
142
143 template<class Compare>
144 inline void sort(Compare comp) {
145 std::sort(data_.begin(), data_.end(), comp);
146 }
147
148 iterator begin() { return static_cast<iterator>(data_.begin()); }
149 const_iterator begin() const { return data_.begin(); }
150 iterator end() { return static_cast<iterator>(data_.end()); }
151 const_iterator end() const { return data_.end(); }
152
153 reverse_iterator rbegin() { return data_.rbegin(); }
154 const_reverse_iterator rbegin() const { return data_.rbegin(); }
155 reverse_iterator rend() { return data_.rend(); }
156 const_reverse_iterator rend() const { return data_.rend(); }
157
158 private:
159 std::vector<T*> data_;
160
161 DISALLOW_COPY_AND_ASSIGN(ScopedPtrVector);
162 };
163
164 } // namespace cc
165
166 #endif // CC_SCOPED_PTR_VECTOR_H_
OLDNEW
« cc/cc.gyp ('K') | « cc/scoped_ptr_hash_map.h ('k') | cc/scoped_resource.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698