147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org/*
247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Copyright 2014 The WebRTC Project Authors. All rights reserved.
347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *
447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Use of this source code is governed by a BSD-style license
547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  that can be found in the LICENSE file in the root of the source
647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  tree. An additional intellectual property rights grant can be found
747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  in the file PATENTS.  All contributing project authors may
847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org */
1047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Stores a collection of pointers that are deleted when the container is
1247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// destructed.
1347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#ifndef WEBRTC_BASE_SCOPEDPTRCOLLECTION_H_
1547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#define WEBRTC_BASE_SCOPEDPTRCOLLECTION_H_
1647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <algorithm>
1847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <vector>
1947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/basictypes.h"
2147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/constructormagic.h"
2247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgnamespace rtc {
2447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgtemplate<class T>
2647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgclass ScopedPtrCollection {
2747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org public:
2847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  typedef std::vector<T*> VectorT;
2947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  ScopedPtrCollection() { }
3147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  ~ScopedPtrCollection() {
3247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    for (typename VectorT::iterator it = collection_.begin();
3347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org         it != collection_.end(); ++it) {
3447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      delete *it;
3547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
3647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
3747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  const VectorT& collection() const { return collection_; }
3947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void Reserve(size_t size) {
4047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    collection_.reserve(size);
4147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
4247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void PushBack(T* t) {
4347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    collection_.push_back(t);
4447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
4547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Remove |t| from the collection without deleting it.
4747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void Remove(T* t) {
4847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    collection_.erase(std::remove(collection_.begin(), collection_.end(), t),
4947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                      collection_.end());
5047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
5147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org private:
5347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  VectorT collection_;
5447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  DISALLOW_COPY_AND_ASSIGN(ScopedPtrCollection);
5647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org};
5747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}  // namespace rtc
5947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
6047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif  // WEBRTC_BASE_SCOPEDPTRCOLLECTION_H_
61