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_OWN_PTR_VECTOR_H_
6 #define CC_OWN_PTR_VECTOR_H_
8 #include "base/basictypes.h"
9 #include "base/stl_util.h"
10 #include <wtf/PassOwnPtr.h>
11 #include <wtf/OwnPtr.h>
15 // This type acts like a Vector<OwnPtr> but based on top of std::vector. The
16 // OwnPtrVector has ownership of all elements in the vector.
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 ~OwnPtrVector() { clear(); }
34 T
* Peek(size_t index
) const {
35 ASSERT(index
< size());
39 T
* operator[](size_t index
) const {
50 return Peek(size() - 1);
53 bool isEmpty() const {
57 PassOwnPtr
<T
> take(size_t index
) {
58 ASSERT(index
< size());
59 OwnPtr
<T
> ret
= adoptPtr(data_
[index
]);
64 void remove(size_t index
) {
65 ASSERT(index
< size());
67 data_
.erase(data_
.begin() + index
);
71 STLDeleteElements(&data_
);
74 void append(PassOwnPtr
<T
> item
) {
75 data_
.push_back(item
.leakPtr());
78 void insert(size_t index
, PassOwnPtr
<T
> item
) {
79 ASSERT(index
< size());
80 data_
.insert(data_
.begin() + index
, item
.leakPtr());
83 iterator
begin() { return data_
.begin(); }
84 const_iterator
begin() const { return data_
.begin(); }
85 iterator
end() { return data_
.end(); }
86 const_iterator
end() const { return data_
.end(); }
88 reverse_iterator
rbegin() { return data_
.rbegin(); }
89 const_reverse_iterator
rbegin() const { return data_
.rbegin(); }
90 reverse_iterator
rend() { return data_
.rend(); }
91 const_reverse_iterator
rend() const { return data_
.rend(); }
94 std::vector
<T
*> data_
;
96 DISALLOW_COPY_AND_ASSIGN(OwnPtrVector
);
101 #endif // CC_OWN_PTR_VECTOR_H_