Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(247)

Side by Side Diff: cc/scoped_ptr_deque.h

Issue 12472028: Part 1 of cc/ directory shuffles: base (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #ifndef CC_SCOPED_PTR_DEQUE_H_
6 #define CC_SCOPED_PTR_DEQUE_H_
7
8 #include "base/basictypes.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/stl_util.h"
12 #include <deque>
13
14 namespace cc {
15
16 // This type acts like a deque<scoped_ptr> based on top of std::deque. The
17 // ScopedPtrDeque has ownership of all elements in the deque.
18 template <typename T>
19 class ScopedPtrDeque {
20 public:
21 typedef typename std::deque<T*>::const_iterator const_iterator;
22 typedef typename std::deque<T*>::reverse_iterator reverse_iterator;
23 typedef typename std::deque<T*>::const_reverse_iterator
24 const_reverse_iterator;
25
26 #if defined(OS_ANDROID)
27 // On Android the iterator is not a class, so we can't block assignment.
28 typedef typename std::deque<T*>::iterator iterator;
29 #else
30 // Ban setting values on the iterator directly. New pointers must be passed
31 // to methods on the ScopedPtrDeque class to appear in the deque.
32 class iterator : public std::deque<T*>::iterator {
33 public:
34 iterator(const typename std::deque<T*>::iterator& other)
35 : std::deque<T*>::iterator(other) {}
36 T* const& operator*() { return std::deque<T*>::iterator::operator*(); }
37 };
38 #endif
39
40 ScopedPtrDeque() {}
41
42 ~ScopedPtrDeque() { clear(); }
43
44 size_t size() const {
45 return data_.size();
46 }
47
48 T* at(size_t index) const {
49 DCHECK(index < size());
50 return data_[index];
51 }
52
53 T* operator[](size_t index) const {
54 return at(index);
55 }
56
57 T* front() const {
58 DCHECK(!empty());
59 return at(0);
60 }
61
62 T* back() const {
63 DCHECK(!empty());
64 return at(size() - 1);
65 }
66
67 bool empty() const {
68 return data_.empty();
69 }
70
71 scoped_ptr<T> take_front() {
72 scoped_ptr<T> ret(front());
73 data_.pop_front();
74 return ret.Pass();
75 }
76
77 scoped_ptr<T> take_back() {
78 scoped_ptr<T> ret(back());
79 data_.pop_back();
80 return ret.Pass();
81 }
82
83 void clear() {
84 STLDeleteElements(&data_);
85 }
86
87 void push_front(scoped_ptr<T> item) {
88 data_.push_front(item.release());
89 }
90
91 void push_back(scoped_ptr<T> item) {
92 data_.push_back(item.release());
93 }
94
95 void insert(iterator position, scoped_ptr<T> item) {
96 DCHECK(position <= end());
97 data_.insert(position, item.release());
98 }
99
100 scoped_ptr<T> take(iterator position) {
101 DCHECK(position < end());
102 scoped_ptr<T> ret(*position);
103 data_.erase(position);
104 return ret.Pass();
105 }
106
107 void swap(iterator a, iterator b) {
108 DCHECK(a < end());
109 DCHECK(b < end());
110 if (a == end() || b == end() || a == b)
111 return;
112 typename std::deque<T*>::iterator writable_a = a;
113 typename std::deque<T*>::iterator writable_b = b;
114 std::swap(*writable_a, *writable_b);
115 }
116
117 iterator begin() { return static_cast<iterator>(data_.begin()); }
118 const_iterator begin() const { return data_.begin(); }
119 iterator end() { return static_cast<iterator>(data_.end()); }
120 const_iterator end() const { return data_.end(); }
121
122 reverse_iterator rbegin() { return data_.rbegin(); }
123 const_reverse_iterator rbegin() const { return data_.rbegin(); }
124 reverse_iterator rend() { return data_.rend(); }
125 const_reverse_iterator rend() const { return data_.rend(); }
126
127 private:
128 std::deque<T*> data_;
129
130 DISALLOW_COPY_AND_ASSIGN(ScopedPtrDeque);
131 };
132
133 } // namespace cc
134
135 #endif // CC_SCOPED_PTR_DEQUE_H_
OLDNEW
« cc/cc.gyp ('K') | « cc/scoped_ptr_algorithm.h ('k') | cc/scoped_ptr_hash_map.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698