Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: webrtc/system_wrappers/include/scoped_vector.h

Issue 1839603002: Remove webrtc::ScopedVector (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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_INCLUDE_SCOPED_VECTOR_H_
14 #define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_SCOPED_VECTOR_H_
15
16 #include <vector>
17
18 #include "webrtc/base/checks.h"
19 #include "webrtc/system_wrappers/include/stl_util.h"
20
21 namespace webrtc {
22
23 // ScopedVector wraps a vector deleting the elements from its
24 // destructor.
25 template <class T>
26 class ScopedVector {
27 public:
28 typedef typename std::vector<T*>::allocator_type allocator_type;
29 typedef typename std::vector<T*>::size_type size_type;
30 typedef typename std::vector<T*>::difference_type difference_type;
31 typedef typename std::vector<T*>::pointer pointer;
32 typedef typename std::vector<T*>::const_pointer const_pointer;
33 typedef typename std::vector<T*>::reference reference;
34 typedef typename std::vector<T*>::const_reference const_reference;
35 typedef typename std::vector<T*>::value_type value_type;
36 typedef typename std::vector<T*>::iterator iterator;
37 typedef typename std::vector<T*>::const_iterator const_iterator;
38 typedef typename std::vector<T*>::reverse_iterator reverse_iterator;
39 typedef typename std::vector<T*>::const_reverse_iterator
40 const_reverse_iterator;
41
42 ScopedVector() {}
43 ~ScopedVector() { clear(); }
44
45 // Move construction and assignment.
46 ScopedVector(ScopedVector&& other) { *this = std::move(other); }
47 ScopedVector& operator=(ScopedVector&& other) {
48 std::swap(v_, other.v_); // The arguments are std::vectors, so std::swap
49 // is the one that we want.
50 other.clear();
51 return *this;
52 }
53
54 // Deleted copy constructor and copy assignment, to make the type move-only.
55 ScopedVector(const ScopedVector& other) = delete;
56 ScopedVector& operator=(const ScopedVector& other) = delete;
57
58 reference operator[](size_t index) { return v_[index]; }
59 const_reference operator[](size_t index) const { return v_[index]; }
60
61 bool empty() const { return v_.empty(); }
62 size_t size() const { return v_.size(); }
63
64 reverse_iterator rbegin() { return v_.rbegin(); }
65 const_reverse_iterator rbegin() const { return v_.rbegin(); }
66 reverse_iterator rend() { return v_.rend(); }
67 const_reverse_iterator rend() const { return v_.rend(); }
68
69 iterator begin() { return v_.begin(); }
70 const_iterator begin() const { return v_.begin(); }
71 iterator end() { return v_.end(); }
72 const_iterator end() const { return v_.end(); }
73
74 const_reference front() const { return v_.front(); }
75 reference front() { return v_.front(); }
76 const_reference back() const { return v_.back(); }
77 reference back() { return v_.back(); }
78
79 void push_back(T* elem) { v_.push_back(elem); }
80
81 void pop_back() {
82 RTC_DCHECK(!empty());
83 delete v_.back();
84 v_.pop_back();
85 }
86
87 std::vector<T*>& get() { return v_; }
88 const std::vector<T*>& get() const { return v_; }
89 void swap(std::vector<T*>& other) { v_.swap(other); }
90 void swap(ScopedVector<T>& other) { v_.swap(other.v_); }
91 void release(std::vector<T*>* out) {
92 out->swap(v_);
93 v_.clear();
94 }
95
96 void reserve(size_t capacity) { v_.reserve(capacity); }
97
98 // Resize, deleting elements in the disappearing range if we are shrinking.
99 void resize(size_t new_size) {
100 if (v_.size() > new_size)
101 STLDeleteContainerPointers(v_.begin() + new_size, v_.end());
102 v_.resize(new_size);
103 }
104
105 template<typename InputIterator>
106 void assign(InputIterator begin, InputIterator end) {
107 v_.assign(begin, end);
108 }
109
110 void clear() { STLDeleteElements(&v_); }
111
112 // Like |clear()|, but doesn't delete any elements.
113 void weak_clear() { v_.clear(); }
114
115 // Lets the ScopedVector take ownership of |x|.
116 iterator insert(iterator position, T* x) {
117 return v_.insert(position, x);
118 }
119
120 // Lets the ScopedVector take ownership of elements in [first,last).
121 template<typename InputIterator>
122 void insert(iterator position, InputIterator first, InputIterator last) {
123 v_.insert(position, first, last);
124 }
125
126 iterator erase(iterator position) {
127 delete *position;
128 return v_.erase(position);
129 }
130
131 iterator erase(iterator first, iterator last) {
132 STLDeleteContainerPointers(first, last);
133 return v_.erase(first, last);
134 }
135
136 // Like |erase()|, but doesn't delete the element at |position|.
137 iterator weak_erase(iterator position) {
138 return v_.erase(position);
139 }
140
141 // Like |erase()|, but doesn't delete the elements in [first, last).
142 iterator weak_erase(iterator first, iterator last) {
143 return v_.erase(first, last);
144 }
145
146 private:
147 std::vector<T*> v_;
148 };
149
150 } // namespace webrtc
151
152 #endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_SCOPED_VECTOR_H_
OLDNEW
« no previous file with comments | « webrtc/system_wrappers/BUILD.gn ('k') | webrtc/system_wrappers/source/scoped_vector_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698