Ensure that on Windows 7, relaunching the browser via chrome://restart or changing...
[chromium-blink-merge.git] / base / memory / scoped_vector.h
blob1b30f635ca11128f50333ede00deddd462082d69
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/move.h"
13 #include "base/stl_util.h"
15 // ScopedVector wraps a vector deleting the elements from its
16 // destructor.
17 template <class T>
18 class ScopedVector {
19 MOVE_ONLY_TYPE_FOR_CPP_03(ScopedVector, RValue)
21 public:
22 typedef typename std::vector<T*>::allocator_type allocator_type;
23 typedef typename std::vector<T*>::size_type size_type;
24 typedef typename std::vector<T*>::difference_type difference_type;
25 typedef typename std::vector<T*>::pointer pointer;
26 typedef typename std::vector<T*>::const_pointer const_pointer;
27 typedef typename std::vector<T*>::reference reference;
28 typedef typename std::vector<T*>::const_reference const_reference;
29 typedef typename std::vector<T*>::value_type value_type;
30 typedef typename std::vector<T*>::iterator iterator;
31 typedef typename std::vector<T*>::const_iterator const_iterator;
32 typedef typename std::vector<T*>::reverse_iterator reverse_iterator;
33 typedef typename std::vector<T*>::const_reverse_iterator
34 const_reverse_iterator;
36 ScopedVector() {}
37 ~ScopedVector() { clear(); }
38 ScopedVector(RValue other) { swap(*other.object); }
40 ScopedVector& operator=(RValue rhs) {
41 swap(*rhs.object);
42 return *this;
45 reference operator[](size_t index) { return v_[index]; }
46 const_reference operator[](size_t index) const { return v_[index]; }
48 bool empty() const { return v_.empty(); }
49 size_t size() const { return v_.size(); }
51 reverse_iterator rbegin() { return v_.rbegin(); }
52 const_reverse_iterator rbegin() const { return v_.rbegin(); }
53 reverse_iterator rend() { return v_.rend(); }
54 const_reverse_iterator rend() const { return v_.rend(); }
56 iterator begin() { return v_.begin(); }
57 const_iterator begin() const { return v_.begin(); }
58 iterator end() { return v_.end(); }
59 const_iterator end() const { return v_.end(); }
61 const_reference front() const { return v_.front(); }
62 reference front() { return v_.front(); }
63 const_reference back() const { return v_.back(); }
64 reference back() { return v_.back(); }
66 void push_back(T* elem) { v_.push_back(elem); }
68 void pop_back() {
69 DCHECK(!empty());
70 delete v_.back();
71 v_.pop_back();
74 std::vector<T*>& get() { return v_; }
75 const std::vector<T*>& get() const { return v_; }
76 void swap(std::vector<T*>& other) { v_.swap(other); }
77 void swap(ScopedVector<T>& other) { v_.swap(other.v_); }
78 void release(std::vector<T*>* out) {
79 out->swap(v_);
80 v_.clear();
83 void reserve(size_t capacity) { v_.reserve(capacity); }
85 // Resize, deleting elements in the disappearing range if we are shrinking.
86 void resize(size_t new_size) {
87 if (v_.size() > new_size)
88 STLDeleteContainerPointers(v_.begin() + new_size, v_.end());
89 v_.resize(new_size);
92 template<typename InputIterator>
93 void assign(InputIterator begin, InputIterator end) {
94 v_.assign(begin, end);
97 void clear() { STLDeleteElements(&v_); }
99 // Like |clear()|, but doesn't delete any elements.
100 void weak_clear() { v_.clear(); }
102 // Lets the ScopedVector take ownership of |x|.
103 iterator insert(iterator position, T* x) {
104 return v_.insert(position, x);
107 // Lets the ScopedVector take ownership of elements in [first,last).
108 template<typename InputIterator>
109 void insert(iterator position, InputIterator first, InputIterator last) {
110 v_.insert(position, first, last);
113 iterator erase(iterator position) {
114 delete *position;
115 return v_.erase(position);
118 iterator erase(iterator first, iterator last) {
119 STLDeleteContainerPointers(first, last);
120 return v_.erase(first, last);
123 // Like |erase()|, but doesn't delete the element at |position|.
124 iterator weak_erase(iterator position) {
125 return v_.erase(position);
128 // Like |erase()|, but doesn't delete the elements in [first, last).
129 iterator weak_erase(iterator first, iterator last) {
130 return v_.erase(first, last);
133 private:
134 std::vector<T*> v_;
137 #endif // BASE_MEMORY_SCOPED_VECTOR_H_