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.
5 #ifndef CC_SCOPED_PTR_VECTOR_H_
6 #define CC_SCOPED_PTR_VECTOR_H_
8 #include "base/basictypes.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/stl_util.h"
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.
18 class ScopedPtrVector
{
20 typedef typename
std::vector
<T
*>::iterator iterator
;
21 typedef typename
std::vector
<T
*>::const_iterator const_iterator
;
22 typedef typename
std::vector
<T
*>::reverse_iterator reverse_iterator
;
23 typedef typename
std::vector
<T
*>::const_reverse_iterator
24 const_reverse_iterator
;
28 ~ScopedPtrVector() { clear(); }
34 T
* Peek(size_t index
) const {
35 DCHECK(index
< size());
39 T
* operator[](size_t index
) const {
50 return Peek(size() - 1);
53 bool isEmpty() const {
57 scoped_ptr
<T
> take(size_t index
) {
58 DCHECK(index
< size());
59 scoped_ptr
<T
> ret(data_
[index
]);
64 void remove(size_t index
) {
65 DCHECK(index
< size());
67 data_
.erase(data_
.begin() + index
);
70 void reserve(size_t size
) {
75 STLDeleteElements(&data_
);
78 void append(scoped_ptr
<T
> item
) {
79 data_
.push_back(item
.release());
82 void insert(size_t index
, scoped_ptr
<T
> item
) {
83 DCHECK(index
< size());
84 data_
.insert(data_
.begin() + index
, item
.release());
87 iterator
begin() { return data_
.begin(); }
88 const_iterator
begin() const { return data_
.begin(); }
89 iterator
end() { return data_
.end(); }
90 const_iterator
end() const { return data_
.end(); }
92 reverse_iterator
rbegin() { return data_
.rbegin(); }
93 const_reverse_iterator
rbegin() const { return data_
.rbegin(); }
94 reverse_iterator
rend() { return data_
.rend(); }
95 const_reverse_iterator
rend() const { return data_
.rend(); }
98 std::vector
<T
*> data_
;
100 DISALLOW_COPY_AND_ASSIGN(ScopedPtrVector
);
105 #endif // CC_SCOPED_PTR_VECTOR_H_