Home | History | Annotate | Download | only in memory
      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 
     25   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 
     33   bool empty() const { return v.empty(); }
     34   size_t size() const { return v.size(); }
     35 
     36   reverse_iterator rbegin() { return v.rbegin(); }
     37   const_reverse_iterator rbegin() const { return v.rbegin(); }
     38   reverse_iterator rend() { return v.rend(); }
     39   const_reverse_iterator rend() const { return v.rend(); }
     40 
     41   iterator begin() { return v.begin(); }
     42   const_iterator begin() const { return v.begin(); }
     43   iterator end() { return v.end(); }
     44   const_iterator end() const { return v.end(); }
     45 
     46   void push_back(T* elem) { v.push_back(elem); }
     47 
     48   std::vector<T*>& get() { return v; }
     49   const std::vector<T*>& get() const { return v; }
     50   void swap(ScopedVector<T>& other) { v.swap(other.v); }
     51   void release(std::vector<T*>* out) {
     52     out->swap(v);
     53     v.clear();
     54   }
     55 
     56   void reset() { STLDeleteElements(&v); }
     57   void reserve(size_t capacity) { v.reserve(capacity); }
     58   void resize(size_t new_size) { v.resize(new_size); }
     59 
     60   // Lets the ScopedVector take ownership of |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>
     67   void insert(iterator position, InputIterator first, InputIterator last) {
     68     v.insert(position, first, last);
     69   }
     70 
     71   iterator erase(iterator position) {
     72     delete *position;
     73     return v.erase(position);
     74   }
     75 
     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|.
     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).
     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