blob: 68db3a1210a0f8634bdb60eb6859ead2db9667e6 [file] [log] [blame]
andrew@webrtc.org0daa8be2014-04-18 21:20:54 +00001/*
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
23namespace webrtc {
24
25// ScopedVector wraps a vector deleting the elements from its
26// destructor.
27template <class T>
28class 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
46 ScopedVector() {}
47 ~ScopedVector() { clear(); }
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
58 bool empty() const { return v_.empty(); }
59 size_t size() const { return v_.size(); }
60
61 reverse_iterator rbegin() { return v_.rbegin(); }
62 const_reverse_iterator rbegin() const { return v_.rbegin(); }
63 reverse_iterator rend() { return v_.rend(); }
64 const_reverse_iterator rend() const { return v_.rend(); }
65
66 iterator begin() { return v_.begin(); }
67 const_iterator begin() const { return v_.begin(); }
68 iterator end() { return v_.end(); }
69 const_iterator end() const { return v_.end(); }
70
71 const_reference front() const { return v_.front(); }
72 reference front() { return v_.front(); }
73 const_reference back() const { return v_.back(); }
74 reference back() { return v_.back(); }
75
76 void push_back(T* elem) { v_.push_back(elem); }
77
78 void pop_back() {
79 assert(!empty());
80 delete v_.back();
81 v_.pop_back();
82 }
83
84 std::vector<T*>& get() { return v_; }
85 const std::vector<T*>& get() const { return v_; }
86 void swap(std::vector<T*>& other) { v_.swap(other); }
87 void swap(ScopedVector<T>& other) { v_.swap(other.v_); }
88 void release(std::vector<T*>* out) {
89 out->swap(v_);
90 v_.clear();
91 }
92
93 void reserve(size_t capacity) { v_.reserve(capacity); }
94
95 // Resize, deleting elements in the disappearing range if we are shrinking.
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>
103 void assign(InputIterator begin, InputIterator end) {
104 v_.assign(begin, end);
105 }
106
107 void clear() { STLDeleteElements(&v_); }
108
109 // Like |clear()|, but doesn't delete any elements.
110 void weak_clear() { v_.clear(); }
111
112 // Lets the ScopedVector take ownership of |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>
119 void insert(iterator position, InputIterator first, InputIterator last) {
120 v_.insert(position, first, last);
121 }
122
123 iterator erase(iterator position) {
124 delete *position;
125 return v_.erase(position);
126 }
127
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|.
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).
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_