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

Side by Side Diff: cc/scoped_ptr_deque.h

Issue 11418108: cc: Make the ScopedPtrVector and ScopedPtrDeque containers act like STL vector and deque. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: android!! Created 7 years, 11 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
« no previous file with comments | « cc/scoped_ptr_algorithm.h ('k') | cc/scoped_ptr_vector.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CC_SCOPED_PTR_DEQUE_H_ 5 #ifndef CC_SCOPED_PTR_DEQUE_H_
6 #define CC_SCOPED_PTR_DEQUE_H_ 6 #define CC_SCOPED_PTR_DEQUE_H_
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include <deque> 12 #include <deque>
13 13
14 namespace cc { 14 namespace cc {
15 15
16 // This type acts like a deque<scoped_ptr> based on top of std::deque. The 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. 17 // ScopedPtrDeque has ownership of all elements in the deque.
18 template <typename T> 18 template <typename T>
19 class ScopedPtrDeque { 19 class ScopedPtrDeque {
20 public: 20 public:
21 typedef typename std::deque<T*>::iterator iterator;
22 typedef typename std::deque<T*>::const_iterator const_iterator; 21 typedef typename std::deque<T*>::const_iterator const_iterator;
23 typedef typename std::deque<T*>::reverse_iterator reverse_iterator; 22 typedef typename std::deque<T*>::reverse_iterator reverse_iterator;
24 typedef typename std::deque<T*>::const_reverse_iterator const_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
25 39
26 ScopedPtrDeque() {} 40 ScopedPtrDeque() {}
27 41
28 ~ScopedPtrDeque() { clear(); } 42 ~ScopedPtrDeque() { clear(); }
29 43
30 size_t size() const { 44 size_t size() const {
31 return data_.size(); 45 return data_.size();
32 } 46 }
33 47
34 T* Peek(size_t index) const { 48 T* at(size_t index) const {
35 DCHECK(index < size()); 49 DCHECK(index < size());
36 return data_[index]; 50 return data_[index];
37 } 51 }
38 52
39 T* operator[](size_t index) const { 53 T* operator[](size_t index) const {
40 return Peek(index); 54 return at(index);
41 } 55 }
42 56
43 T* first() const { 57 T* front() const {
44 DCHECK(!isEmpty()); 58 DCHECK(!empty());
45 return Peek(0); 59 return at(0);
46 } 60 }
47 61
48 T* last() const { 62 T* back() const {
49 DCHECK(!isEmpty()); 63 DCHECK(!empty());
50 return Peek(size() - 1); 64 return at(size() - 1);
51 } 65 }
52 66
53 bool isEmpty() const { 67 bool empty() const {
54 return size() == 0; 68 return data_.empty();
55 } 69 }
56 70
57 scoped_ptr<T> takeFirst() { 71 scoped_ptr<T> take_front() {
58 scoped_ptr<T> ret(first()); 72 scoped_ptr<T> ret(front());
59 data_.pop_front(); 73 data_.pop_front();
60 return ret.Pass(); 74 return ret.Pass();
61 } 75 }
62 76
63 scoped_ptr<T> takeLast() { 77 scoped_ptr<T> take_back() {
64 scoped_ptr<T> ret(last()); 78 scoped_ptr<T> ret(back());
65 data_.pop_back(); 79 data_.pop_back();
66 return ret.Pass(); 80 return ret.Pass();
67 } 81 }
68 82
69 void clear() { 83 void clear() {
70 STLDeleteElements(&data_); 84 STLDeleteElements(&data_);
71 } 85 }
72 86
73 void append(scoped_ptr<T> item) { 87 void push_front(scoped_ptr<T> item) {
88 data_.push_front(item.release());
89 }
90
91 void push_back(scoped_ptr<T> item) {
74 data_.push_back(item.release()); 92 data_.push_back(item.release());
75 } 93 }
76 94
77 void insert(size_t index, scoped_ptr<T> item) { 95 void insert(iterator position, scoped_ptr<T> item) {
78 DCHECK(index < size()); 96 DCHECK(position <= end());
79 data_.insert(data_.begin() + index, item.release()); 97 data_.insert(position, item.release());
80 } 98 }
81 99
82 iterator begin() { return data_.begin(); } 100 void swap(iterator a, iterator b) {
101 DCHECK(a < end());
102 DCHECK(b < end());
103 if (a == end() || b == end() || a == b)
104 return;
105 typename std::deque<T*>::iterator writable_a = a;
106 typename std::deque<T*>::iterator writable_b = b;
107 std::swap(*writable_a, *writable_b);
108 }
109
110 iterator begin() { return static_cast<iterator>(data_.begin()); }
83 const_iterator begin() const { return data_.begin(); } 111 const_iterator begin() const { return data_.begin(); }
84 iterator end() { return data_.end(); } 112 iterator end() { return static_cast<iterator>(data_.end()); }
85 const_iterator end() const { return data_.end(); } 113 const_iterator end() const { return data_.end(); }
86 114
87 reverse_iterator rbegin() { return data_.rbegin(); } 115 reverse_iterator rbegin() { return data_.rbegin(); }
88 const_reverse_iterator rbegin() const { return data_.rbegin(); } 116 const_reverse_iterator rbegin() const { return data_.rbegin(); }
89 reverse_iterator rend() { return data_.rend(); } 117 reverse_iterator rend() { return data_.rend(); }
90 const_reverse_iterator rend() const { return data_.rend(); } 118 const_reverse_iterator rend() const { return data_.rend(); }
91 119
92 private: 120 private:
93 std::deque<T*> data_; 121 std::deque<T*> data_;
94 122
95 DISALLOW_COPY_AND_ASSIGN(ScopedPtrDeque); 123 DISALLOW_COPY_AND_ASSIGN(ScopedPtrDeque);
96 }; 124 };
97 125
98 } // namespace cc 126 } // namespace cc
99 127
100 #endif // CC_SCOPED_PTR_DEQUE_H_ 128 #endif // CC_SCOPED_PTR_DEQUE_H_
OLDNEW
« no previous file with comments | « cc/scoped_ptr_algorithm.h ('k') | cc/scoped_ptr_vector.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698