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/base/deprecation.h"
20#include "webrtc/system_wrappers/include/stl_util.h"
21
22namespace webrtc {
23
24// ScopedVector wraps a vector deleting the elements from its
25// destructor.
26template <class T>
27class ScopedVector {
28 public:
29  typedef typename std::vector<T*>::allocator_type allocator_type;
30  typedef typename std::vector<T*>::size_type size_type;
31  typedef typename std::vector<T*>::difference_type difference_type;
32  typedef typename std::vector<T*>::pointer pointer;
33  typedef typename std::vector<T*>::const_pointer const_pointer;
34  typedef typename std::vector<T*>::reference reference;
35  typedef typename std::vector<T*>::const_reference const_reference;
36  typedef typename std::vector<T*>::value_type value_type;
37  typedef typename std::vector<T*>::iterator iterator;
38  typedef typename std::vector<T*>::const_iterator const_iterator;
39  typedef typename std::vector<T*>::reverse_iterator reverse_iterator;
40  typedef typename std::vector<T*>::const_reverse_iterator
41      const_reverse_iterator;
42
43  ScopedVector() {}
44  ~ScopedVector() { clear(); }
45
46  // Move construction and assignment.
47  ScopedVector(ScopedVector&& other) { *this = std::move(other); }
48  ScopedVector& operator=(ScopedVector&& other) {
49    std::swap(v_, other.v_);  // The arguments are std::vectors, so std::swap
50                              // is the one that we want.
51    other.clear();
52    return *this;
53  }
54
55  // Deleted copy constructor and copy assignment, to make the type move-only.
56  ScopedVector(const ScopedVector& other) = delete;
57  ScopedVector& operator=(const ScopedVector& other) = delete;
58
59  // Get an rvalue reference. (sv.Pass() does the same thing as std::move(sv).)
60  // Deprecated; remove in March 2016 (bug 5373).
61  RTC_DEPRECATED ScopedVector&& Pass() { return DEPRECATED_Pass(); }
62  ScopedVector&& DEPRECATED_Pass() {
63    return std::move(*this);
64  }
65
66  reference operator[](size_t index) { return v_[index]; }
67  const_reference operator[](size_t index) const { return v_[index]; }
68
69  bool empty() const { return v_.empty(); }
70  size_t size() const { return v_.size(); }
71
72  reverse_iterator rbegin() { return v_.rbegin(); }
73  const_reverse_iterator rbegin() const { return v_.rbegin(); }
74  reverse_iterator rend() { return v_.rend(); }
75  const_reverse_iterator rend() const { return v_.rend(); }
76
77  iterator begin() { return v_.begin(); }
78  const_iterator begin() const { return v_.begin(); }
79  iterator end() { return v_.end(); }
80  const_iterator end() const { return v_.end(); }
81
82  const_reference front() const { return v_.front(); }
83  reference front() { return v_.front(); }
84  const_reference back() const { return v_.back(); }
85  reference back() { return v_.back(); }
86
87  void push_back(T* elem) { v_.push_back(elem); }
88
89  void pop_back() {
90    RTC_DCHECK(!empty());
91    delete v_.back();
92    v_.pop_back();
93  }
94
95  std::vector<T*>& get() { return v_; }
96  const std::vector<T*>& get() const { return v_; }
97  void swap(std::vector<T*>& other) { v_.swap(other); }
98  void swap(ScopedVector<T>& other) { v_.swap(other.v_); }
99  void release(std::vector<T*>* out) {
100    out->swap(v_);
101    v_.clear();
102  }
103
104  void reserve(size_t capacity) { v_.reserve(capacity); }
105
106  // Resize, deleting elements in the disappearing range if we are shrinking.
107  void resize(size_t new_size) {
108    if (v_.size() > new_size)
109      STLDeleteContainerPointers(v_.begin() + new_size, v_.end());
110    v_.resize(new_size);
111  }
112
113  template<typename InputIterator>
114  void assign(InputIterator begin, InputIterator end) {
115    v_.assign(begin, end);
116  }
117
118  void clear() { STLDeleteElements(&v_); }
119
120  // Like |clear()|, but doesn't delete any elements.
121  void weak_clear() { v_.clear(); }
122
123  // Lets the ScopedVector take ownership of |x|.
124  iterator insert(iterator position, T* x) {
125    return v_.insert(position, x);
126  }
127
128  // Lets the ScopedVector take ownership of elements in [first,last).
129  template<typename InputIterator>
130  void insert(iterator position, InputIterator first, InputIterator last) {
131    v_.insert(position, first, last);
132  }
133
134  iterator erase(iterator position) {
135    delete *position;
136    return v_.erase(position);
137  }
138
139  iterator erase(iterator first, iterator last) {
140    STLDeleteContainerPointers(first, last);
141    return v_.erase(first, last);
142  }
143
144  // Like |erase()|, but doesn't delete the element at |position|.
145  iterator weak_erase(iterator position) {
146    return v_.erase(position);
147  }
148
149  // Like |erase()|, but doesn't delete the elements in [first, last).
150  iterator weak_erase(iterator first, iterator last) {
151    return v_.erase(first, last);
152  }
153
154 private:
155  std::vector<T*> v_;
156};
157
158}  // namespace webrtc
159
160#endif  // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_SCOPED_VECTOR_H_
161