Depend on stored sync session GUID for Android.
[chromium-blink-merge.git] / cc / scoped_ptr_vector.h
blobb4271e8231066f5d1bf14c5481e350658d52a183
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_SCOPED_PTR_VECTOR_H_
6 #define CC_SCOPED_PTR_VECTOR_H_
8 #include "base/basictypes.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/stl_util.h"
13 namespace cc {
15 // This type acts like a vector<scoped_ptr> based on top of std::vector. The
16 // ScopedPtrVector has ownership of all elements in the vector.
17 template <typename T>
18 class ScopedPtrVector {
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;
26 ScopedPtrVector() {}
28 ~ScopedPtrVector() { clear(); }
30 size_t size() const {
31 return data_.size();
34 T* Peek(size_t index) const {
35 DCHECK(index < size());
36 return data_[index];
39 T* operator[](size_t index) const {
40 return Peek(index);
43 T* first() const {
44 DCHECK(!isEmpty());
45 return Peek(0);
48 T* last() const {
49 DCHECK(!isEmpty());
50 return Peek(size() - 1);
53 bool isEmpty() const {
54 return size() == 0;
57 scoped_ptr<T> take(size_t index) {
58 DCHECK(index < size());
59 scoped_ptr<T> ret(data_[index]);
60 data_[index] = NULL;
61 return ret.Pass();
64 void remove(size_t index) {
65 DCHECK(index < size());
66 delete data_[index];
67 data_.erase(data_.begin() + index);
70 void reserve(size_t size) {
71 data_.reserve(size);
74 void clear() {
75 STLDeleteElements(&data_);
78 void append(scoped_ptr<T> item) {
79 data_.push_back(item.release());
82 void insert(size_t index, scoped_ptr<T> item) {
83 DCHECK(index < size());
84 data_.insert(data_.begin() + index, item.release());
87 iterator begin() { return data_.begin(); }
88 const_iterator begin() const { return data_.begin(); }
89 iterator end() { return data_.end(); }
90 const_iterator end() const { return data_.end(); }
92 reverse_iterator rbegin() { return data_.rbegin(); }
93 const_reverse_iterator rbegin() const { return data_.rbegin(); }
94 reverse_iterator rend() { return data_.rend(); }
95 const_reverse_iterator rend() const { return data_.rend(); }
97 private:
98 std::vector<T*> data_;
100 DISALLOW_COPY_AND_ASSIGN(ScopedPtrVector);
103 } // namespace cc
105 #endif // CC_SCOPED_PTR_VECTOR_H_