aboutsummaryrefslogtreecommitdiff
path: root/webrtc/system_wrappers/include/scoped_vector.h
diff options
context:
space:
mode:
authorHenrik Kjellander <kjellander@webrtc.org>2015-10-28 18:17:40 +0100
committerHenrik Kjellander <kjellander@webrtc.org>2015-10-28 17:17:50 +0000
commit98f53510b222f71fdd8b799b2f33737ceeb28c61 (patch)
tree897af57562951bc2d7e31062731184dd14a7c9a9 /webrtc/system_wrappers/include/scoped_vector.h
parentebc0b4e99365443111857a0c7cfcc8944d8f1b6e (diff)
downloadwebrtc-98f53510b222f71fdd8b799b2f33737ceeb28c61.tar.gz
system_wrappers: rename interface -> include
BUG=webrtc:5095 R=tommi@webrtc.org Review URL: https://codereview.webrtc.org/1413333002 . Cr-Commit-Position: refs/heads/master@{#10438}
Diffstat (limited to 'webrtc/system_wrappers/include/scoped_vector.h')
-rw-r--r--webrtc/system_wrappers/include/scoped_vector.h157
1 files changed, 157 insertions, 0 deletions
diff --git a/webrtc/system_wrappers/include/scoped_vector.h b/webrtc/system_wrappers/include/scoped_vector.h
new file mode 100644
index 0000000000..1e89a9d245
--- /dev/null
+++ b/webrtc/system_wrappers/include/scoped_vector.h
@@ -0,0 +1,157 @@
+/*
+ * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+// Borrowed from Chromium's src/base/memory/scoped_vector.h.
+
+#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SCOPED_VECTOR_H_
+#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SCOPED_VECTOR_H_
+
+#include <vector>
+
+#include "webrtc/base/checks.h"
+#include "webrtc/system_wrappers/include/stl_util.h"
+
+namespace webrtc {
+
+// ScopedVector wraps a vector deleting the elements from its
+// destructor.
+template <class T>
+class ScopedVector {
+ public:
+ typedef typename std::vector<T*>::allocator_type allocator_type;
+ typedef typename std::vector<T*>::size_type size_type;
+ typedef typename std::vector<T*>::difference_type difference_type;
+ typedef typename std::vector<T*>::pointer pointer;
+ typedef typename std::vector<T*>::const_pointer const_pointer;
+ typedef typename std::vector<T*>::reference reference;
+ typedef typename std::vector<T*>::const_reference const_reference;
+ typedef typename std::vector<T*>::value_type value_type;
+ typedef typename std::vector<T*>::iterator iterator;
+ typedef typename std::vector<T*>::const_iterator const_iterator;
+ typedef typename std::vector<T*>::reverse_iterator reverse_iterator;
+ typedef typename std::vector<T*>::const_reverse_iterator
+ const_reverse_iterator;
+
+ ScopedVector() {}
+ ~ScopedVector() { clear(); }
+
+ // Move construction and assignment.
+ ScopedVector(ScopedVector&& other) {
+ *this = static_cast<ScopedVector&&>(other);
+ }
+ ScopedVector& operator=(ScopedVector&& other) {
+ std::swap(v_, other.v_); // The arguments are std::vectors, so std::swap
+ // is the one that we want.
+ other.clear();
+ return *this;
+ }
+
+ // Deleted copy constructor and copy assignment, to make the type move-only.
+ ScopedVector(const ScopedVector& other) = delete;
+ ScopedVector& operator=(const ScopedVector& other) = delete;
+
+ // Get an rvalue reference. (sv.Pass() does the same thing as std::move(sv).)
+ ScopedVector&& Pass() { return static_cast<ScopedVector&&>(*this); }
+
+ reference operator[](size_t index) { return v_[index]; }
+ const_reference operator[](size_t index) const { return v_[index]; }
+
+ bool empty() const { return v_.empty(); }
+ size_t size() const { return v_.size(); }
+
+ reverse_iterator rbegin() { return v_.rbegin(); }
+ const_reverse_iterator rbegin() const { return v_.rbegin(); }
+ reverse_iterator rend() { return v_.rend(); }
+ const_reverse_iterator rend() const { return v_.rend(); }
+
+ iterator begin() { return v_.begin(); }
+ const_iterator begin() const { return v_.begin(); }
+ iterator end() { return v_.end(); }
+ const_iterator end() const { return v_.end(); }
+
+ const_reference front() const { return v_.front(); }
+ reference front() { return v_.front(); }
+ const_reference back() const { return v_.back(); }
+ reference back() { return v_.back(); }
+
+ void push_back(T* elem) { v_.push_back(elem); }
+
+ void pop_back() {
+ RTC_DCHECK(!empty());
+ delete v_.back();
+ v_.pop_back();
+ }
+
+ std::vector<T*>& get() { return v_; }
+ const std::vector<T*>& get() const { return v_; }
+ void swap(std::vector<T*>& other) { v_.swap(other); }
+ void swap(ScopedVector<T>& other) { v_.swap(other.v_); }
+ void release(std::vector<T*>* out) {
+ out->swap(v_);
+ v_.clear();
+ }
+
+ void reserve(size_t capacity) { v_.reserve(capacity); }
+
+ // Resize, deleting elements in the disappearing range if we are shrinking.
+ void resize(size_t new_size) {
+ if (v_.size() > new_size)
+ STLDeleteContainerPointers(v_.begin() + new_size, v_.end());
+ v_.resize(new_size);
+ }
+
+ template<typename InputIterator>
+ void assign(InputIterator begin, InputIterator end) {
+ v_.assign(begin, end);
+ }
+
+ void clear() { STLDeleteElements(&v_); }
+
+ // Like |clear()|, but doesn't delete any elements.
+ void weak_clear() { v_.clear(); }
+
+ // Lets the ScopedVector take ownership of |x|.
+ iterator insert(iterator position, T* x) {
+ return v_.insert(position, x);
+ }
+
+ // Lets the ScopedVector take ownership of elements in [first,last).
+ template<typename InputIterator>
+ void insert(iterator position, InputIterator first, InputIterator last) {
+ v_.insert(position, first, last);
+ }
+
+ iterator erase(iterator position) {
+ delete *position;
+ return v_.erase(position);
+ }
+
+ iterator erase(iterator first, iterator last) {
+ STLDeleteContainerPointers(first, last);
+ return v_.erase(first, last);
+ }
+
+ // Like |erase()|, but doesn't delete the element at |position|.
+ iterator weak_erase(iterator position) {
+ return v_.erase(position);
+ }
+
+ // Like |erase()|, but doesn't delete the elements in [first, last).
+ iterator weak_erase(iterator first, iterator last) {
+ return v_.erase(first, last);
+ }
+
+ private:
+ std::vector<T*> v_;
+};
+
+} // namespace webrtc
+
+#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SCOPED_VECTOR_H_