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

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

Issue 1413333002: system_wrappers: rename interface -> include (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Rebased again! Created 5 years, 1 month 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_INTERFACE_SCOPED_VECTOR_H_
14 #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SCOPED_VECTOR_H_
15
16 #include <vector>
17
18 #include "webrtc/base/checks.h"
19 #include "webrtc/system_wrappers/interface/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) {
47 *this = static_cast<ScopedVector&&>(other);
48 }
49 ScopedVector& operator=(ScopedVector&& other) {
50 std::swap(v_, other.v_); // The arguments are std::vectors, so std::swap
51 // is the one that we want.
52 other.clear();
53 return *this;
54 }
55
56 // Deleted copy constructor and copy assignment, to make the type move-only.
57 ScopedVector(const ScopedVector& other) = delete;
58 ScopedVector& operator=(const ScopedVector& other) = delete;
59
60 // Get an rvalue reference. (sv.Pass() does the same thing as std::move(sv).)
61 ScopedVector&& Pass() { return static_cast<ScopedVector&&>(*this); }
62
63 reference operator[](size_t index) { return v_[index]; }
64 const_reference operator[](size_t index) const { return v_[index]; }
65
66 bool empty() const { return v_.empty(); }
67 size_t size() const { return v_.size(); }
68
69 reverse_iterator rbegin() { return v_.rbegin(); }
70 const_reverse_iterator rbegin() const { return v_.rbegin(); }
71 reverse_iterator rend() { return v_.rend(); }
72 const_reverse_iterator rend() const { return v_.rend(); }
73
74 iterator begin() { return v_.begin(); }
75 const_iterator begin() const { return v_.begin(); }
76 iterator end() { return v_.end(); }
77 const_iterator end() const { return v_.end(); }
78
79 const_reference front() const { return v_.front(); }
80 reference front() { return v_.front(); }
81 const_reference back() const { return v_.back(); }
82 reference back() { return v_.back(); }
83
84 void push_back(T* elem) { v_.push_back(elem); }
85
86 void pop_back() {
87 RTC_DCHECK(!empty());
88 delete v_.back();
89 v_.pop_back();
90 }
91
92 std::vector<T*>& get() { return v_; }
93 const std::vector<T*>& get() const { return v_; }
94 void swap(std::vector<T*>& other) { v_.swap(other); }
95 void swap(ScopedVector<T>& other) { v_.swap(other.v_); }
96 void release(std::vector<T*>* out) {
97 out->swap(v_);
98 v_.clear();
99 }
100
101 void reserve(size_t capacity) { v_.reserve(capacity); }
102
103 // Resize, deleting elements in the disappearing range if we are shrinking.
104 void resize(size_t new_size) {
105 if (v_.size() > new_size)
106 STLDeleteContainerPointers(v_.begin() + new_size, v_.end());
107 v_.resize(new_size);
108 }
109
110 template<typename InputIterator>
111 void assign(InputIterator begin, InputIterator end) {
112 v_.assign(begin, end);
113 }
114
115 void clear() { STLDeleteElements(&v_); }
116
117 // Like |clear()|, but doesn't delete any elements.
118 void weak_clear() { v_.clear(); }
119
120 // Lets the ScopedVector take ownership of |x|.
121 iterator insert(iterator position, T* x) {
122 return v_.insert(position, x);
123 }
124
125 // Lets the ScopedVector take ownership of elements in [first,last).
126 template<typename InputIterator>
127 void insert(iterator position, InputIterator first, InputIterator last) {
128 v_.insert(position, first, last);
129 }
130
131 iterator erase(iterator position) {
132 delete *position;
133 return v_.erase(position);
134 }
135
136 iterator erase(iterator first, iterator last) {
137 STLDeleteContainerPointers(first, last);
138 return v_.erase(first, last);
139 }
140
141 // Like |erase()|, but doesn't delete the element at |position|.
142 iterator weak_erase(iterator position) {
143 return v_.erase(position);
144 }
145
146 // Like |erase()|, but doesn't delete the elements in [first, last).
147 iterator weak_erase(iterator first, iterator last) {
148 return v_.erase(first, last);
149 }
150
151 private:
152 std::vector<T*> v_;
153 };
154
155 } // namespace webrtc
156
157 #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SCOPED_VECTOR_H_
OLDNEW
« no previous file with comments | « webrtc/system_wrappers/interface/rw_lock_wrapper.h ('k') | webrtc/system_wrappers/interface/sleep.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698