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/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
11 | 12 |
12 namespace cc { | 13 namespace cc { |
13 | 14 |
14 // 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 |
15 // ScopedPtrVector has ownership of all elements in the vector. | 16 // ScopedPtrVector has ownership of all elements in the vector. |
16 template <typename T> | 17 template <typename T> |
17 class ScopedPtrVector { | 18 class ScopedPtrVector { |
18 public: | 19 public: |
19 typedef typename std::vector<T*>::iterator iterator; | 20 typedef typename std::vector<T*>::iterator iterator; |
20 typedef typename std::vector<T*>::const_iterator const_iterator; | 21 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*>::reverse_iterator reverse_iterator; |
22 typedef typename std::vector<T*>::const_reverse_iterator | 23 typedef typename std::vector<T*>::const_reverse_iterator |
23 const_reverse_iterator; | 24 const_reverse_iterator; |
24 | 25 |
25 ScopedPtrVector() {} | 26 ScopedPtrVector() {} |
26 | 27 |
27 ~ScopedPtrVector() { clear(); } | 28 ~ScopedPtrVector() { clear(); } |
28 | 29 |
29 size_t size() const { | 30 size_t size() const { |
30 return data_.size(); | 31 return data_.size(); |
31 } | 32 } |
32 | 33 |
33 T* Peek(size_t index) const { | 34 T* Peek(size_t index) const { |
34 ASSERT(index < size()); | 35 DCHECK(index < size()); |
35 return data_[index]; | 36 return data_[index]; |
36 } | 37 } |
37 | 38 |
38 T* operator[](size_t index) const { | 39 T* operator[](size_t index) const { |
39 return Peek(index); | 40 return Peek(index); |
40 } | 41 } |
41 | 42 |
42 T* first() const { | 43 T* first() const { |
43 ASSERT(!isEmpty()); | 44 DCHECK(!isEmpty()); |
44 return Peek(0); | 45 return Peek(0); |
45 } | 46 } |
46 | 47 |
47 T* last() const { | 48 T* last() const { |
48 ASSERT(!isEmpty()); | 49 DCHECK(!isEmpty()); |
49 return Peek(size() - 1); | 50 return Peek(size() - 1); |
50 } | 51 } |
51 | 52 |
52 bool isEmpty() const { | 53 bool isEmpty() const { |
53 return size() == 0; | 54 return size() == 0; |
54 } | 55 } |
55 | 56 |
56 scoped_ptr<T> take(size_t index) { | 57 scoped_ptr<T> take(size_t index) { |
57 ASSERT(index < size()); | 58 DCHECK(index < size()); |
58 scoped_ptr<T> ret(data_[index]); | 59 scoped_ptr<T> ret(data_[index]); |
59 data_[index] = NULL; | 60 data_[index] = NULL; |
60 return ret.Pass(); | 61 return ret.Pass(); |
61 } | 62 } |
62 | 63 |
63 void remove(size_t index) { | 64 void remove(size_t index) { |
64 ASSERT(index < size()); | 65 DCHECK(index < size()); |
65 delete data_[index]; | 66 delete data_[index]; |
66 data_.erase(data_.begin() + index); | 67 data_.erase(data_.begin() + index); |
67 } | 68 } |
68 | 69 |
69 void clear() { | 70 void clear() { |
70 STLDeleteElements(&data_); | 71 STLDeleteElements(&data_); |
71 } | 72 } |
72 | 73 |
73 void append(scoped_ptr<T> item) { | 74 void append(scoped_ptr<T> item) { |
74 data_.push_back(item.release()); | 75 data_.push_back(item.release()); |
75 } | 76 } |
76 | 77 |
77 void insert(size_t index, scoped_ptr<T> item) { | 78 void insert(size_t index, scoped_ptr<T> item) { |
78 ASSERT(index < size()); | 79 DCHECK(index < size()); |
79 data_.insert(data_.begin() + index, item.release()); | 80 data_.insert(data_.begin() + index, item.release()); |
80 } | 81 } |
81 | 82 |
82 iterator begin() { return data_.begin(); } | 83 iterator begin() { return data_.begin(); } |
83 const_iterator begin() const { return data_.begin(); } | 84 const_iterator begin() const { return data_.begin(); } |
84 iterator end() { return data_.end(); } | 85 iterator end() { return data_.end(); } |
85 const_iterator end() const { return data_.end(); } | 86 const_iterator end() const { return data_.end(); } |
86 | 87 |
87 reverse_iterator rbegin() { return data_.rbegin(); } | 88 reverse_iterator rbegin() { return data_.rbegin(); } |
88 const_reverse_iterator rbegin() const { return data_.rbegin(); } | 89 const_reverse_iterator rbegin() const { return data_.rbegin(); } |
89 reverse_iterator rend() { return data_.rend(); } | 90 reverse_iterator rend() { return data_.rend(); } |
90 const_reverse_iterator rend() const { return data_.rend(); } | 91 const_reverse_iterator rend() const { return data_.rend(); } |
91 | 92 |
92 private: | 93 private: |
93 std::vector<T*> data_; | 94 std::vector<T*> data_; |
94 | 95 |
95 DISALLOW_COPY_AND_ASSIGN(ScopedPtrVector); | 96 DISALLOW_COPY_AND_ASSIGN(ScopedPtrVector); |
96 }; | 97 }; |
97 | 98 |
98 } // namespace cc | 99 } // namespace cc |
99 | 100 |
100 #endif // CC_SCOPED_PTR_VECTOR_H_ | 101 #endif // CC_SCOPED_PTR_VECTOR_H_ |
OLD | NEW |