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_BASE_SCOPED_PTR_DEQUE_H_
6 #define CC_BASE_SCOPED_PTR_DEQUE_H_
11 #include "base/basictypes.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/stl_util.h"
18 // This type acts like a deque<scoped_ptr> based on top of std::deque. The
19 // ScopedPtrDeque has ownership of all elements in the deque.
21 class ScopedPtrDeque
{
23 typedef typename
std::deque
<T
*>::const_iterator const_iterator
;
24 typedef typename
std::deque
<T
*>::reverse_iterator reverse_iterator
;
25 typedef typename
std::deque
<T
*>::const_reverse_iterator
26 const_reverse_iterator
;
28 #if defined(OS_ANDROID)
29 // On Android the iterator is not a class, so we can't block assignment.
30 typedef typename
std::deque
<T
*>::iterator iterator
;
32 // Ban setting values on the iterator directly. New pointers must be passed
33 // to methods on the ScopedPtrDeque class to appear in the deque.
34 class iterator
: public std::deque
<T
*>::iterator
{
36 explicit iterator(const typename
std::deque
<T
*>::iterator
& other
)
37 : std::deque
<T
*>::iterator(other
) {}
38 T
* const& operator*() { return std::deque
<T
*>::iterator::operator*(); }
44 ~ScopedPtrDeque() { clear(); }
50 T
* at(size_t index
) const {
51 DCHECK(index
< size());
55 T
* operator[](size_t index
) const {
66 return at(size() - 1);
73 scoped_ptr
<T
> take_front() {
74 scoped_ptr
<T
> ret(front());
79 scoped_ptr
<T
> take_back() {
80 scoped_ptr
<T
> ret(back());
86 STLDeleteElements(&data_
);
89 void push_front(scoped_ptr
<T
> item
) {
90 data_
.push_front(item
.release());
93 void push_back(scoped_ptr
<T
> item
) {
94 data_
.push_back(item
.release());
97 void insert(iterator position
, scoped_ptr
<T
> item
) {
98 DCHECK(position
<= end());
99 data_
.insert(position
, item
.release());
102 scoped_ptr
<T
> take(iterator position
) {
103 DCHECK(position
< end());
104 scoped_ptr
<T
> ret(*position
);
105 data_
.erase(position
);
109 void swap(iterator a
, iterator b
) {
112 if (a
== end() || b
== end() || a
== b
)
114 typename
std::deque
<T
*>::iterator writable_a
= a
;
115 typename
std::deque
<T
*>::iterator writable_b
= b
;
116 std::swap(*writable_a
, *writable_b
);
119 iterator
begin() { return static_cast<iterator
>(data_
.begin()); }
120 const_iterator
begin() const { return data_
.begin(); }
121 iterator
end() { return static_cast<iterator
>(data_
.end()); }
122 const_iterator
end() const { return data_
.end(); }
124 reverse_iterator
rbegin() { return data_
.rbegin(); }
125 const_reverse_iterator
rbegin() const { return data_
.rbegin(); }
126 reverse_iterator
rend() { return data_
.rend(); }
127 const_reverse_iterator
rend() const { return data_
.rend(); }
130 std::deque
<T
*> data_
;
132 DISALLOW_COPY_AND_ASSIGN(ScopedPtrDeque
);
137 #endif // CC_BASE_SCOPED_PTR_DEQUE_H_