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

Side by Side Diff: cc/own_ptr_vector.h

Issue 10940002: Add wrapper container for a vector of OwnPtr<T> (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 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/TreeSynchronizerTest.cpp ('k') | cc/test/CCLayerTestCommon.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_OWN_PTR_VECTOR_H_
6 #define CC_OWN_PTR_VECTOR_H_
7
8 #include "base/basictypes.h"
9 #include "base/stl_util.h"
10 #include <wtf/PassOwnPtr.h>
11 #include <wtf/OwnPtr.h>
12
13 namespace cc {
14
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.
17 template <typename T>
18 class OwnPtrVector {
19 public:
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;
25
26 OwnPtrVector() {}
27
28 ~OwnPtrVector() { clear(); }
29
30 size_t size() const {
31 return data_.size();
32 }
33
34 T* Peek(size_t index) const {
35 ASSERT(index < size());
36 return data_[index];
37 }
38
39 T* operator[](size_t index) const {
40 return Peek(index);
41 }
42
43 T* first() const {
44 ASSERT(!isEmpty());
45 return Peek(0);
46 }
47
48 T* last() const {
49 ASSERT(!isEmpty());
50 return Peek(size() - 1);
51 }
52
53 bool isEmpty() const {
54 return size() == 0;
55 }
56
57 PassOwnPtr<T> take(size_t index) {
58 ASSERT(index < size());
59 OwnPtr<T> ret = adoptPtr(data_[index]);
60 data_[index] = NULL;
61 return ret.release();
62 }
63
64 void remove(size_t index) {
65 ASSERT(index < size());
66 delete data_[index];
67 data_.erase(data_.begin() + index);
68 }
69
70 void clear() {
71 STLDeleteElements(&data_);
72 }
73
74 void append(PassOwnPtr<T> item) {
75 data_.push_back(item.leakPtr());
76 }
77
78 void insert(size_t index, PassOwnPtr<T> item) {
79 ASSERT(index < size());
80 data_.insert(data_.begin() + index, item.leakPtr());
81 }
82
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(); }
87
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(); }
92
93 private:
94 std::vector<T*> data_;
95
96 DISALLOW_COPY_AND_ASSIGN(OwnPtrVector);
97 };
98
99 } // namespace cc
100
101 #endif // CC_OWN_PTR_VECTOR_H_
OLDNEW
« no previous file with comments | « cc/TreeSynchronizerTest.cpp ('k') | cc/test/CCLayerTestCommon.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698