1 // Copyright (c) 2011 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 #pragma once 8 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/stl_util-inl.h" 13 14 // ScopedVector wraps a vector deleting the elements from its 15 // destructor. 16 template <class T> 17 class ScopedVector { 18 public: 19 typedef typename std::vector<T*>::iterator iterator; 20 typedef typename std::vector<T*>::const_iterator const_iterator; 21 typedef typename std::vector<T*>::reverse_iterator reverse_iterator; 22 typedef typename std::vector<T*>::const_reverse_iterator 23 const_reverse_iterator; 24 ScopedVector()25 ScopedVector() {} ~ScopedVector()26 ~ScopedVector() { reset(); } 27 28 std::vector<T*>* operator->() { return &v; } 29 const std::vector<T*>* operator->() const { return &v; } 30 T*& operator[](size_t i) { return v[i]; } 31 const T* operator[](size_t i) const { return v[i]; } 32 empty()33 bool empty() const { return v.empty(); } size()34 size_t size() const { return v.size(); } 35 rbegin()36 reverse_iterator rbegin() { return v.rbegin(); } rbegin()37 const_reverse_iterator rbegin() const { return v.rbegin(); } rend()38 reverse_iterator rend() { return v.rend(); } rend()39 const_reverse_iterator rend() const { return v.rend(); } 40 begin()41 iterator begin() { return v.begin(); } begin()42 const_iterator begin() const { return v.begin(); } end()43 iterator end() { return v.end(); } end()44 const_iterator end() const { return v.end(); } 45 push_back(T * elem)46 void push_back(T* elem) { v.push_back(elem); } 47 get()48 std::vector<T*>& get() { return v; } get()49 const std::vector<T*>& get() const { return v; } swap(ScopedVector<T> & other)50 void swap(ScopedVector<T>& other) { v.swap(other.v); } release(std::vector<T * > * out)51 void release(std::vector<T*>* out) { 52 out->swap(v); 53 v.clear(); 54 } 55 reset()56 void reset() { STLDeleteElements(&v); } reserve(size_t capacity)57 void reserve(size_t capacity) { v.reserve(capacity); } resize(size_t new_size)58 void resize(size_t new_size) { v.resize(new_size); } 59 60 // Lets the ScopedVector take ownership of |x|. insert(iterator position,T * x)61 iterator insert(iterator position, T* x) { 62 return v.insert(position, x); 63 } 64 65 // Lets the ScopedVector take ownership of elements in [first,last). 66 template<typename InputIterator> insert(iterator position,InputIterator first,InputIterator last)67 void insert(iterator position, InputIterator first, InputIterator last) { 68 v.insert(position, first, last); 69 } 70 erase(iterator position)71 iterator erase(iterator position) { 72 delete *position; 73 return v.erase(position); 74 } 75 erase(iterator first,iterator last)76 iterator erase(iterator first, iterator last) { 77 STLDeleteContainerPointers(first, last); 78 return v.erase(first, last); 79 } 80 81 // Like |erase()|, but doesn't delete the element at |position|. weak_erase(iterator position)82 iterator weak_erase(iterator position) { 83 return v.erase(position); 84 } 85 86 // Like |erase()|, but doesn't delete the elements in [first, last). weak_erase(iterator first,iterator last)87 iterator weak_erase(iterator first, iterator last) { 88 return v.erase(first, last); 89 } 90 private: 91 std::vector<T*> v; 92 93 DISALLOW_COPY_AND_ASSIGN(ScopedVector); 94 }; 95 96 #endif // BASE_MEMORY_SCOPED_VECTOR_H_ 97