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 <stddef.h>
9 
10 #include <vector>
11 
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/move.h"
15 #include "base/stl_util.h"
16 
17 // ScopedVector wraps a vector deleting the elements from its
18 // destructor.
19 //
20 // TODO(http://crbug.com/554289): DEPRECATED: Use std::vector instead (now that
21 // we have support for moveable types inside containers).
22 template <class T>
23 class ScopedVector {
24   MOVE_ONLY_TYPE_FOR_CPP_03(ScopedVector)
25 
26  public:
27   typedef typename std::vector<T*>::allocator_type allocator_type;
28   typedef typename std::vector<T*>::size_type size_type;
29   typedef typename std::vector<T*>::difference_type difference_type;
30   typedef typename std::vector<T*>::pointer pointer;
31   typedef typename std::vector<T*>::const_pointer const_pointer;
32   typedef typename std::vector<T*>::reference reference;
33   typedef typename std::vector<T*>::const_reference const_reference;
34   typedef typename std::vector<T*>::value_type value_type;
35   typedef typename std::vector<T*>::iterator iterator;
36   typedef typename std::vector<T*>::const_iterator const_iterator;
37   typedef typename std::vector<T*>::reverse_iterator reverse_iterator;
38   typedef typename std::vector<T*>::const_reverse_iterator
39       const_reverse_iterator;
40 
ScopedVector()41   ScopedVector() {}
~ScopedVector()42   ~ScopedVector() { clear(); }
ScopedVector(ScopedVector && other)43   ScopedVector(ScopedVector&& other) { swap(other); }
44 
45   ScopedVector& operator=(ScopedVector&& rhs) {
46     swap(rhs);
47     return *this;
48   }
49 
50   reference operator[](size_t index) { return v_[index]; }
51   const_reference operator[](size_t index) const { return v_[index]; }
52 
empty()53   bool empty() const { return v_.empty(); }
size()54   size_t size() const { return v_.size(); }
55 
rbegin()56   reverse_iterator rbegin() { return v_.rbegin(); }
rbegin()57   const_reverse_iterator rbegin() const { return v_.rbegin(); }
rend()58   reverse_iterator rend() { return v_.rend(); }
rend()59   const_reverse_iterator rend() const { return v_.rend(); }
60 
begin()61   iterator begin() { return v_.begin(); }
begin()62   const_iterator begin() const { return v_.begin(); }
end()63   iterator end() { return v_.end(); }
end()64   const_iterator end() const { return v_.end(); }
65 
front()66   const_reference front() const { return v_.front(); }
front()67   reference front() { return v_.front(); }
back()68   const_reference back() const { return v_.back(); }
back()69   reference back() { return v_.back(); }
70 
push_back(T * elem)71   void push_back(T* elem) { v_.push_back(elem); }
push_back(scoped_ptr<T> elem)72   void push_back(scoped_ptr<T> elem) { v_.push_back(elem.release()); }
73 
pop_back()74   void pop_back() {
75     DCHECK(!empty());
76     delete v_.back();
77     v_.pop_back();
78   }
79 
get()80   std::vector<T*>& get() { return v_; }
get()81   const std::vector<T*>& get() const { return v_; }
swap(std::vector<T * > & other)82   void swap(std::vector<T*>& other) { v_.swap(other); }
swap(ScopedVector<T> & other)83   void swap(ScopedVector<T>& other) { v_.swap(other.v_); }
release(std::vector<T * > * out)84   void release(std::vector<T*>* out) {
85     out->swap(v_);
86     v_.clear();
87   }
88 
reserve(size_t capacity)89   void reserve(size_t capacity) { v_.reserve(capacity); }
90 
91   // Resize, deleting elements in the disappearing range if we are shrinking.
resize(size_t new_size)92   void resize(size_t new_size) {
93     if (v_.size() > new_size)
94       STLDeleteContainerPointers(v_.begin() + new_size, v_.end());
95     v_.resize(new_size);
96   }
97 
98   template<typename InputIterator>
assign(InputIterator begin,InputIterator end)99   void assign(InputIterator begin, InputIterator end) {
100     v_.assign(begin, end);
101   }
102 
clear()103   void clear() { STLDeleteElements(&v_); }
104 
105   // Like |clear()|, but doesn't delete any elements.
weak_clear()106   void weak_clear() { v_.clear(); }
107 
108   // Lets the ScopedVector take ownership of |x|.
insert(iterator position,T * x)109   iterator insert(iterator position, T* x) {
110     return v_.insert(position, x);
111   }
112 
insert(iterator position,scoped_ptr<T> x)113   iterator insert(iterator position, scoped_ptr<T> x) {
114     return v_.insert(position, x.release());
115   }
116 
117   // Lets the ScopedVector take ownership of elements in [first,last).
118   template<typename InputIterator>
insert(iterator position,InputIterator first,InputIterator last)119   void insert(iterator position, InputIterator first, InputIterator last) {
120     v_.insert(position, first, last);
121   }
122 
erase(iterator position)123   iterator erase(iterator position) {
124     delete *position;
125     return v_.erase(position);
126   }
127 
erase(iterator first,iterator last)128   iterator erase(iterator first, iterator last) {
129     STLDeleteContainerPointers(first, last);
130     return v_.erase(first, last);
131   }
132 
133   // Like |erase()|, but doesn't delete the element at |position|.
weak_erase(iterator position)134   iterator weak_erase(iterator position) {
135     return v_.erase(position);
136   }
137 
138   // Like |erase()|, but doesn't delete the elements in [first, last).
weak_erase(iterator first,iterator last)139   iterator weak_erase(iterator first, iterator last) {
140     return v_.erase(first, last);
141   }
142 
143  private:
144   std::vector<T*> v_;
145 };
146 
147 #endif  // BASE_MEMORY_SCOPED_VECTOR_H_
148