Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / base / memory / scoped_vector.h
blobe1e5c722a4fbe2e6d9f6b902640246b3209cb41c
1 // Copyright (c) 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 BASE_MEMORY_SCOPED_VECTOR_H_
6 #define BASE_MEMORY_SCOPED_VECTOR_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/move.h"
14 #include "base/stl_util.h"
16 // ScopedVector wraps a vector deleting the elements from its
17 // destructor.
18 template <class T>
19 class ScopedVector {
20 MOVE_ONLY_TYPE_FOR_CPP_03(ScopedVector, RValue)
22 public:
23 typedef typename std::vector<T*>::allocator_type allocator_type;
24 typedef typename std::vector<T*>::size_type size_type;
25 typedef typename std::vector<T*>::difference_type difference_type;
26 typedef typename std::vector<T*>::pointer pointer;
27 typedef typename std::vector<T*>::const_pointer const_pointer;
28 typedef typename std::vector<T*>::reference reference;
29 typedef typename std::vector<T*>::const_reference const_reference;
30 typedef typename std::vector<T*>::value_type value_type;
31 typedef typename std::vector<T*>::iterator iterator;
32 typedef typename std::vector<T*>::const_iterator const_iterator;
33 typedef typename std::vector<T*>::reverse_iterator reverse_iterator;
34 typedef typename std::vector<T*>::const_reverse_iterator
35 const_reverse_iterator;
37 ScopedVector() {}
38 ~ScopedVector() { clear(); }
39 ScopedVector(RValue other) { swap(*other.object); }
41 ScopedVector& operator=(RValue rhs) {
42 swap(*rhs.object);
43 return *this;
46 reference operator[](size_t index) { return v_[index]; }
47 const_reference operator[](size_t index) const { return v_[index]; }
49 bool empty() const { return v_.empty(); }
50 size_t size() const { return v_.size(); }
52 reverse_iterator rbegin() { return v_.rbegin(); }
53 const_reverse_iterator rbegin() const { return v_.rbegin(); }
54 reverse_iterator rend() { return v_.rend(); }
55 const_reverse_iterator rend() const { return v_.rend(); }
57 iterator begin() { return v_.begin(); }
58 const_iterator begin() const { return v_.begin(); }
59 iterator end() { return v_.end(); }
60 const_iterator end() const { return v_.end(); }
62 const_reference front() const { return v_.front(); }
63 reference front() { return v_.front(); }
64 const_reference back() const { return v_.back(); }
65 reference back() { return v_.back(); }
67 void push_back(T* elem) { v_.push_back(elem); }
68 void push_back(scoped_ptr<T> elem) { v_.push_back(elem.release()); }
70 void pop_back() {
71 DCHECK(!empty());
72 delete v_.back();
73 v_.pop_back();
76 std::vector<T*>& get() { return v_; }
77 const std::vector<T*>& get() const { return v_; }
78 void swap(std::vector<T*>& other) { v_.swap(other); }
79 void swap(ScopedVector<T>& other) { v_.swap(other.v_); }
80 void release(std::vector<T*>* out) {
81 out->swap(v_);
82 v_.clear();
85 void reserve(size_t capacity) { v_.reserve(capacity); }
87 // Resize, deleting elements in the disappearing range if we are shrinking.
88 void resize(size_t new_size) {
89 if (v_.size() > new_size)
90 STLDeleteContainerPointers(v_.begin() + new_size, v_.end());
91 v_.resize(new_size);
94 template<typename InputIterator>
95 void assign(InputIterator begin, InputIterator end) {
96 v_.assign(begin, end);
99 void clear() { STLDeleteElements(&v_); }
101 // Like |clear()|, but doesn't delete any elements.
102 void weak_clear() { v_.clear(); }
104 // Lets the ScopedVector take ownership of |x|.
105 iterator insert(iterator position, T* x) {
106 return v_.insert(position, x);
109 iterator insert(iterator position, scoped_ptr<T> x) {
110 return v_.insert(position, x.release());
113 // Lets the ScopedVector take ownership of elements in [first,last).
114 template<typename InputIterator>
115 void insert(iterator position, InputIterator first, InputIterator last) {
116 v_.insert(position, first, last);
119 iterator erase(iterator position) {
120 delete *position;
121 return v_.erase(position);
124 iterator erase(iterator first, iterator last) {
125 STLDeleteContainerPointers(first, last);
126 return v_.erase(first, last);
129 // Like |erase()|, but doesn't delete the element at |position|.
130 iterator weak_erase(iterator position) {
131 return v_.erase(position);
134 // Like |erase()|, but doesn't delete the elements in [first, last).
135 iterator weak_erase(iterator first, iterator last) {
136 return v_.erase(first, last);
139 private:
140 std::vector<T*> v_;
143 #endif // BASE_MEMORY_SCOPED_VECTOR_H_