Chromium Code Reviews| OLD | NEW |
|---|---|
| 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_VECTOR_H_ | 5 #ifndef CC_SCOPED_PTR_VECTOR_H_ |
| 6 #define CC_SCOPED_PTR_VECTOR_H_ | 6 #define CC_SCOPED_PTR_VECTOR_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" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 const_reverse_iterator; | 24 const_reverse_iterator; |
| 25 | 25 |
| 26 ScopedPtrVector() {} | 26 ScopedPtrVector() {} |
| 27 | 27 |
| 28 ~ScopedPtrVector() { clear(); } | 28 ~ScopedPtrVector() { clear(); } |
| 29 | 29 |
| 30 size_t size() const { | 30 size_t size() const { |
| 31 return data_.size(); | 31 return data_.size(); |
| 32 } | 32 } |
| 33 | 33 |
| 34 T* Peek(size_t index) const { | 34 T* at(size_t index) const { |
| 35 DCHECK(index < size()); | 35 DCHECK(index < size()); |
| 36 return data_[index]; | 36 return data_[index]; |
| 37 } | 37 } |
| 38 | 38 |
| 39 T* operator[](size_t index) const { | 39 T* operator[](size_t index) const { |
| 40 return Peek(index); | 40 return at(index); |
| 41 } | 41 } |
| 42 | 42 |
| 43 T* first() const { | 43 T* front() const { |
| 44 DCHECK(!isEmpty()); | 44 DCHECK(!empty()); |
| 45 return Peek(0); | 45 return at(0); |
| 46 } | 46 } |
| 47 | 47 |
| 48 T* last() const { | 48 T* back() const { |
| 49 DCHECK(!isEmpty()); | 49 DCHECK(!empty()); |
| 50 return Peek(size() - 1); | 50 return at(size() - 1); |
| 51 } | 51 } |
| 52 | 52 |
| 53 bool isEmpty() const { | 53 bool empty() const { |
| 54 return size() == 0; | 54 return size() == 0; |
| 55 } | 55 } |
| 56 | 56 |
| 57 scoped_ptr<T> take(size_t index) { | 57 scoped_ptr<T> take(iterator position) { |
|
enne (OOO)
2012/11/21 04:12:57
Same question here.
danakj
2012/11/21 04:23:21
But here, take is different than erase. It removes
| |
| 58 DCHECK(index < size()); | 58 if (position == end()) |
| 59 scoped_ptr<T> ret(data_[index]); | 59 return scoped_ptr<T>(NULL); |
| 60 data_[index] = NULL; | 60 scoped_ptr<T> ret(*position); |
| 61 *position = NULL; | |
| 61 return ret.Pass(); | 62 return ret.Pass(); |
| 62 } | 63 } |
| 63 | 64 |
| 64 void remove(size_t index) { | 65 void erase(iterator position) { |
| 65 DCHECK(index < size()); | 66 if (position == end()) |
| 66 delete data_[index]; | 67 return; |
| 67 data_.erase(data_.begin() + index); | 68 delete *position; |
| 69 data_.erase(position); | |
| 70 } | |
| 71 | |
| 72 void erase(iterator first, iterator last) { | |
| 73 DCHECK(first <= last); | |
| 74 for (iterator it = first; it != last; ++it) { | |
| 75 DCHECK(it != end()); | |
| 76 delete *it; | |
| 77 data_.erase(it); | |
| 78 } | |
| 68 } | 79 } |
| 69 | 80 |
| 70 void clear() { | 81 void clear() { |
| 71 STLDeleteElements(&data_); | 82 STLDeleteElements(&data_); |
| 72 } | 83 } |
| 73 | 84 |
| 74 void append(scoped_ptr<T> item) { | 85 void push_back(scoped_ptr<T> item) { |
| 75 data_.push_back(item.release()); | 86 data_.push_back(item.release()); |
| 76 } | 87 } |
| 77 | 88 |
| 78 void insert(size_t index, scoped_ptr<T> item) { | 89 void insert(iterator position, scoped_ptr<T> item) { |
| 79 DCHECK(index < size()); | 90 data_.insert(position, item.release()); |
| 80 data_.insert(data_.begin() + index, item.release()); | |
| 81 } | 91 } |
| 82 | 92 |
| 83 iterator begin() { return data_.begin(); } | 93 iterator begin() { return data_.begin(); } |
| 84 const_iterator begin() const { return data_.begin(); } | 94 const_iterator begin() const { return data_.begin(); } |
| 85 iterator end() { return data_.end(); } | 95 iterator end() { return data_.end(); } |
| 86 const_iterator end() const { return data_.end(); } | 96 const_iterator end() const { return data_.end(); } |
| 87 | 97 |
| 88 reverse_iterator rbegin() { return data_.rbegin(); } | 98 reverse_iterator rbegin() { return data_.rbegin(); } |
| 89 const_reverse_iterator rbegin() const { return data_.rbegin(); } | 99 const_reverse_iterator rbegin() const { return data_.rbegin(); } |
| 90 reverse_iterator rend() { return data_.rend(); } | 100 reverse_iterator rend() { return data_.rend(); } |
| 91 const_reverse_iterator rend() const { return data_.rend(); } | 101 const_reverse_iterator rend() const { return data_.rend(); } |
| 92 | 102 |
| 93 private: | 103 private: |
| 94 std::vector<T*> data_; | 104 std::vector<T*> data_; |
| 95 | 105 |
| 96 DISALLOW_COPY_AND_ASSIGN(ScopedPtrVector); | 106 DISALLOW_COPY_AND_ASSIGN(ScopedPtrVector); |
| 97 }; | 107 }; |
| 98 | 108 |
| 99 } // namespace cc | 109 } // namespace cc |
| 100 | 110 |
| 101 #endif // CC_SCOPED_PTR_VECTOR_H_ | 111 #endif // CC_SCOPED_PTR_VECTOR_H_ |
| OLD | NEW |