Landing Recent QUIC changes until 8/19/2015 17:00 UTC.
[chromium-blink-merge.git] / cc / base / scoped_ptr_deque.h
blobcb4adfc15be5273d49a1966708effee66b6873a2
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_
8 #include <algorithm>
9 #include <deque>
11 #include "base/basictypes.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/stl_util.h"
16 namespace cc {
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.
20 template <typename T>
21 class ScopedPtrDeque {
22 public:
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;
31 #else
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 {
35 public:
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*(); }
40 #endif
42 ScopedPtrDeque() {}
44 ~ScopedPtrDeque() { clear(); }
46 size_t size() const {
47 return data_.size();
50 T* at(size_t index) const {
51 DCHECK(index < size());
52 return data_[index];
55 T* operator[](size_t index) const {
56 return at(index);
59 T* front() const {
60 DCHECK(!empty());
61 return at(0);
64 T* back() const {
65 DCHECK(!empty());
66 return at(size() - 1);
69 bool empty() const {
70 return data_.empty();
73 scoped_ptr<T> take_front() {
74 scoped_ptr<T> ret(front());
75 data_.pop_front();
76 return ret.Pass();
79 scoped_ptr<T> take_back() {
80 scoped_ptr<T> ret(back());
81 data_.pop_back();
82 return ret.Pass();
85 void clear() {
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);
106 return ret.Pass();
109 void swap(iterator a, iterator b) {
110 DCHECK(a < end());
111 DCHECK(b < end());
112 if (a == end() || b == end() || a == b)
113 return;
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(); }
129 private:
130 std::deque<T*> data_;
132 DISALLOW_COPY_AND_ASSIGN(ScopedPtrDeque);
135 } // namespace cc
137 #endif // CC_BASE_SCOPED_PTR_DEQUE_H_