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. 4 5 #ifndef BASE_MEMORY_SCOPED_VECTOR_H_ 6 #define BASE_MEMORY_SCOPED_VECTOR_H_ 7 8 #include <vector> 9 10 #include "base/basictypes.h" 11 #include "base/logging.h" 12 #include "base/move.h" 13 #include "base/stl_util.h" 14 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) 20 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; 35 ScopedVector()36 ScopedVector() {} ~ScopedVector()37 ~ScopedVector() { clear(); } ScopedVector(RValue other)38 ScopedVector(RValue other) { swap(*other.object); } 39 40 ScopedVector& operator=(RValue rhs) { 41 swap(*rhs.object); 42 return *this; 43 } 44 45 reference operator[](size_t index) { return v_[index]; } 46 const_reference operator[](size_t index) const { return v_[index]; } 47 empty()48 bool empty() const { return v_.empty(); } size()49 size_t size() const { return v_.size(); } 50 rbegin()51 reverse_iterator rbegin() { return v_.rbegin(); } rbegin()52 const_reverse_iterator rbegin() const { return v_.rbegin(); } rend()53 reverse_iterator rend() { return v_.rend(); } rend()54 const_reverse_iterator rend() const { return v_.rend(); } 55 begin()56 iterator begin() { return v_.begin(); } begin()57 const_iterator begin() const { return v_.begin(); } end()58 iterator end() { return v_.end(); } end()59 const_iterator end() const { return v_.end(); } 60 front()61 const_reference front() const { return v_.front(); } front()62 reference front() { return v_.front(); } back()63 const_reference back() const { return v_.back(); } back()64 reference back() { return v_.back(); } 65 push_back(T * elem)66 void push_back(T* elem) { v_.push_back(elem); } 67 pop_back()68 void pop_back() { 69 DCHECK(!empty()); 70 delete v_.back(); 71 v_.pop_back(); 72 } 73 get()74 std::vector<T*>& get() { return v_; } get()75 const std::vector<T*>& get() const { return v_; } swap(std::vector<T * > & other)76 void swap(std::vector<T*>& other) { v_.swap(other); } swap(ScopedVector<T> & other)77 void swap(ScopedVector<T>& other) { v_.swap(other.v_); } release(std::vector<T * > * out)78 void release(std::vector<T*>* out) { 79 out->swap(v_); 80 v_.clear(); 81 } 82 reserve(size_t capacity)83 void reserve(size_t capacity) { v_.reserve(capacity); } 84 85 // Resize, deleting elements in the disappearing range if we are shrinking. resize(size_t new_size)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); 90 } 91 92 template<typename InputIterator> assign(InputIterator begin,InputIterator end)93 void assign(InputIterator begin, InputIterator end) { 94 v_.assign(begin, end); 95 } 96 clear()97 void clear() { STLDeleteElements(&v_); } 98 99 // Like |clear()|, but doesn't delete any elements. weak_clear()100 void weak_clear() { v_.clear(); } 101 102 // Lets the ScopedVector take ownership of |x|. insert(iterator position,T * x)103 iterator insert(iterator position, T* x) { 104 return v_.insert(position, x); 105 } 106 107 // Lets the ScopedVector take ownership of elements in [first,last). 108 template<typename InputIterator> insert(iterator position,InputIterator first,InputIterator last)109 void insert(iterator position, InputIterator first, InputIterator last) { 110 v_.insert(position, first, last); 111 } 112 erase(iterator position)113 iterator erase(iterator position) { 114 delete *position; 115 return v_.erase(position); 116 } 117 erase(iterator first,iterator last)118 iterator erase(iterator first, iterator last) { 119 STLDeleteContainerPointers(first, last); 120 return v_.erase(first, last); 121 } 122 123 // Like |erase()|, but doesn't delete the element at |position|. weak_erase(iterator position)124 iterator weak_erase(iterator position) { 125 return v_.erase(position); 126 } 127 128 // Like |erase()|, but doesn't delete the elements in [first, last). weak_erase(iterator first,iterator last)129 iterator weak_erase(iterator first, iterator last) { 130 return v_.erase(first, last); 131 } 132 133 private: 134 std::vector<T*> v_; 135 }; 136 137 #endif // BASE_MEMORY_SCOPED_VECTOR_H_ 138