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