16958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org/*
26958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
36958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org *
46958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org *  Use of this source code is governed by a BSD-style license
56958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org *  that can be found in the LICENSE file in the root of the source
66958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org *  tree. An additional intellectual property rights grant can be found
76958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org *  in the file PATENTS.  All contributing project authors may
86958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
96958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org */
106958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
116958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org// Borrowed from Chromium's src/base/memory/scoped_vector.h.
126958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
136958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SCOPED_VECTOR_H_
146958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SCOPED_VECTOR_H_
156958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
166958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org#include <assert.h>
176958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org#include <algorithm>
186958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org#include <vector>
196958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
206958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org#include "webrtc/system_wrappers/interface/stl_util.h"
216958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org#include "webrtc/system_wrappers/source/move.h"
226958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
236958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.orgnamespace webrtc {
246958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
256958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org// ScopedVector wraps a vector deleting the elements from its
266958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org// destructor.
276958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.orgtemplate <class T>
286958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.orgclass ScopedVector {
296958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  WEBRTC_MOVE_ONLY_TYPE_FOR_CPP_03(ScopedVector, RValue)
306958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
316958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org public:
326958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  typedef typename std::vector<T*>::allocator_type allocator_type;
336958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  typedef typename std::vector<T*>::size_type size_type;
346958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  typedef typename std::vector<T*>::difference_type difference_type;
356958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  typedef typename std::vector<T*>::pointer pointer;
366958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  typedef typename std::vector<T*>::const_pointer const_pointer;
376958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  typedef typename std::vector<T*>::reference reference;
386958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  typedef typename std::vector<T*>::const_reference const_reference;
396958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  typedef typename std::vector<T*>::value_type value_type;
406958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  typedef typename std::vector<T*>::iterator iterator;
416958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  typedef typename std::vector<T*>::const_iterator const_iterator;
426958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  typedef typename std::vector<T*>::reverse_iterator reverse_iterator;
436958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  typedef typename std::vector<T*>::const_reverse_iterator
446958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org      const_reverse_iterator;
456958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
466958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  ScopedVector() {}
476958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  ~ScopedVector() { clear(); }
486958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  ScopedVector(RValue other) { swap(*other.object); }
496958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
506958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  ScopedVector& operator=(RValue rhs) {
516958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    swap(*rhs.object);
526958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    return *this;
536958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  }
546958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
556958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  reference operator[](size_t index) { return v_[index]; }
566958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  const_reference operator[](size_t index) const { return v_[index]; }
576958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
586958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  bool empty() const { return v_.empty(); }
596958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  size_t size() const { return v_.size(); }
606958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
616958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  reverse_iterator rbegin() { return v_.rbegin(); }
626958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  const_reverse_iterator rbegin() const { return v_.rbegin(); }
636958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  reverse_iterator rend() { return v_.rend(); }
646958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  const_reverse_iterator rend() const { return v_.rend(); }
656958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
666958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  iterator begin() { return v_.begin(); }
676958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  const_iterator begin() const { return v_.begin(); }
686958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  iterator end() { return v_.end(); }
696958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  const_iterator end() const { return v_.end(); }
706958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
716958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  const_reference front() const { return v_.front(); }
726958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  reference front() { return v_.front(); }
736958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  const_reference back() const { return v_.back(); }
746958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  reference back() { return v_.back(); }
756958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
766958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  void push_back(T* elem) { v_.push_back(elem); }
776958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
786958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  void pop_back() {
796958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    assert(!empty());
806958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    delete v_.back();
816958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    v_.pop_back();
826958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  }
836958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
846958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  std::vector<T*>& get() { return v_; }
856958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  const std::vector<T*>& get() const { return v_; }
866958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  void swap(std::vector<T*>& other) { v_.swap(other); }
876958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  void swap(ScopedVector<T>& other) { v_.swap(other.v_); }
886958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  void release(std::vector<T*>* out) {
896958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    out->swap(v_);
906958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    v_.clear();
916958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  }
926958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
936958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  void reserve(size_t capacity) { v_.reserve(capacity); }
946958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
956958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  // Resize, deleting elements in the disappearing range if we are shrinking.
966958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  void resize(size_t new_size) {
976958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    if (v_.size() > new_size)
986958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org      STLDeleteContainerPointers(v_.begin() + new_size, v_.end());
996958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    v_.resize(new_size);
1006958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  }
1016958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
1026958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  template<typename InputIterator>
1036958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  void assign(InputIterator begin, InputIterator end) {
1046958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    v_.assign(begin, end);
1056958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  }
1066958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
1076958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  void clear() { STLDeleteElements(&v_); }
1086958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
1096958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  // Like |clear()|, but doesn't delete any elements.
1106958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  void weak_clear() { v_.clear(); }
1116958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
1126958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  // Lets the ScopedVector take ownership of |x|.
1136958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  iterator insert(iterator position, T* x) {
1146958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    return v_.insert(position, x);
1156958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  }
1166958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
1176958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  // Lets the ScopedVector take ownership of elements in [first,last).
1186958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  template<typename InputIterator>
1196958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  void insert(iterator position, InputIterator first, InputIterator last) {
1206958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    v_.insert(position, first, last);
1216958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  }
1226958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
1236958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  iterator erase(iterator position) {
1246958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    delete *position;
1256958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    return v_.erase(position);
1266958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  }
1276958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
1286958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  iterator erase(iterator first, iterator last) {
1296958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    STLDeleteContainerPointers(first, last);
1306958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    return v_.erase(first, last);
1316958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  }
1326958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
1336958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  // Like |erase()|, but doesn't delete the element at |position|.
1346958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  iterator weak_erase(iterator position) {
1356958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    return v_.erase(position);
1366958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  }
1376958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
1386958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  // Like |erase()|, but doesn't delete the elements in [first, last).
1396958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  iterator weak_erase(iterator first, iterator last) {
1406958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org    return v_.erase(first, last);
1416958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  }
1426958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
1436958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org private:
1446958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org  std::vector<T*> v_;
1456958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org};
1466958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
1476958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org}  // namespace webrtc
1486958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org
1496958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org#endif  // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SCOPED_VECTOR_H_
150