• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 // Borrowed from Chromium's src/base/memory/scoped_vector.h.
12 
13 #ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SCOPED_VECTOR_H_
14 #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SCOPED_VECTOR_H_
15 
16 #include <assert.h>
17 #include <algorithm>
18 #include <vector>
19 
20 #include "webrtc/system_wrappers/interface/stl_util.h"
21 #include "webrtc/system_wrappers/source/move.h"
22 
23 namespace webrtc {
24 
25 // ScopedVector wraps a vector deleting the elements from its
26 // destructor.
27 template <class T>
28 class ScopedVector {
29   WEBRTC_MOVE_ONLY_TYPE_FOR_CPP_03(ScopedVector, RValue)
30 
31  public:
32   typedef typename std::vector<T*>::allocator_type allocator_type;
33   typedef typename std::vector<T*>::size_type size_type;
34   typedef typename std::vector<T*>::difference_type difference_type;
35   typedef typename std::vector<T*>::pointer pointer;
36   typedef typename std::vector<T*>::const_pointer const_pointer;
37   typedef typename std::vector<T*>::reference reference;
38   typedef typename std::vector<T*>::const_reference const_reference;
39   typedef typename std::vector<T*>::value_type value_type;
40   typedef typename std::vector<T*>::iterator iterator;
41   typedef typename std::vector<T*>::const_iterator const_iterator;
42   typedef typename std::vector<T*>::reverse_iterator reverse_iterator;
43   typedef typename std::vector<T*>::const_reverse_iterator
44       const_reverse_iterator;
45 
ScopedVector()46   ScopedVector() {}
~ScopedVector()47   ~ScopedVector() { clear(); }
ScopedVector(RValue other)48   ScopedVector(RValue other) { swap(*other.object); }
49 
50   ScopedVector& operator=(RValue rhs) {
51     swap(*rhs.object);
52     return *this;
53   }
54 
55   reference operator[](size_t index) { return v_[index]; }
56   const_reference operator[](size_t index) const { return v_[index]; }
57 
empty()58   bool empty() const { return v_.empty(); }
size()59   size_t size() const { return v_.size(); }
60 
rbegin()61   reverse_iterator rbegin() { return v_.rbegin(); }
rbegin()62   const_reverse_iterator rbegin() const { return v_.rbegin(); }
rend()63   reverse_iterator rend() { return v_.rend(); }
rend()64   const_reverse_iterator rend() const { return v_.rend(); }
65 
begin()66   iterator begin() { return v_.begin(); }
begin()67   const_iterator begin() const { return v_.begin(); }
end()68   iterator end() { return v_.end(); }
end()69   const_iterator end() const { return v_.end(); }
70 
front()71   const_reference front() const { return v_.front(); }
front()72   reference front() { return v_.front(); }
back()73   const_reference back() const { return v_.back(); }
back()74   reference back() { return v_.back(); }
75 
push_back(T * elem)76   void push_back(T* elem) { v_.push_back(elem); }
77 
pop_back()78   void pop_back() {
79     assert(!empty());
80     delete v_.back();
81     v_.pop_back();
82   }
83 
get()84   std::vector<T*>& get() { return v_; }
get()85   const std::vector<T*>& get() const { return v_; }
swap(std::vector<T * > & other)86   void swap(std::vector<T*>& other) { v_.swap(other); }
swap(ScopedVector<T> & other)87   void swap(ScopedVector<T>& other) { v_.swap(other.v_); }
release(std::vector<T * > * out)88   void release(std::vector<T*>* out) {
89     out->swap(v_);
90     v_.clear();
91   }
92 
reserve(size_t capacity)93   void reserve(size_t capacity) { v_.reserve(capacity); }
94 
95   // Resize, deleting elements in the disappearing range if we are shrinking.
resize(size_t new_size)96   void resize(size_t new_size) {
97     if (v_.size() > new_size)
98       STLDeleteContainerPointers(v_.begin() + new_size, v_.end());
99     v_.resize(new_size);
100   }
101 
102   template<typename InputIterator>
assign(InputIterator begin,InputIterator end)103   void assign(InputIterator begin, InputIterator end) {
104     v_.assign(begin, end);
105   }
106 
clear()107   void clear() { STLDeleteElements(&v_); }
108 
109   // Like |clear()|, but doesn't delete any elements.
weak_clear()110   void weak_clear() { v_.clear(); }
111 
112   // Lets the ScopedVector take ownership of |x|.
insert(iterator position,T * x)113   iterator insert(iterator position, T* x) {
114     return v_.insert(position, x);
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 }  // namespace webrtc
148 
149 #endif  // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SCOPED_VECTOR_H_
150