Home | History | Annotate | Download | only in base
      1 /*
      2  *  Copyright 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 // Stores a collection of pointers that are deleted when the container is
     12 // destructed.
     13 
     14 #ifndef WEBRTC_BASE_SCOPEDPTRCOLLECTION_H_
     15 #define WEBRTC_BASE_SCOPEDPTRCOLLECTION_H_
     16 
     17 #include <algorithm>
     18 #include <vector>
     19 
     20 #include "webrtc/base/basictypes.h"
     21 #include "webrtc/base/constructormagic.h"
     22 
     23 namespace rtc {
     24 
     25 template<class T>
     26 class ScopedPtrCollection {
     27  public:
     28   typedef std::vector<T*> VectorT;
     29 
     30   ScopedPtrCollection() { }
     31   ~ScopedPtrCollection() {
     32     for (typename VectorT::iterator it = collection_.begin();
     33          it != collection_.end(); ++it) {
     34       delete *it;
     35     }
     36   }
     37 
     38   const VectorT& collection() const { return collection_; }
     39   void Reserve(size_t size) {
     40     collection_.reserve(size);
     41   }
     42   void PushBack(T* t) {
     43     collection_.push_back(t);
     44   }
     45 
     46   // Remove |t| from the collection without deleting it.
     47   void Remove(T* t) {
     48     collection_.erase(std::remove(collection_.begin(), collection_.end(), t),
     49                       collection_.end());
     50   }
     51 
     52  private:
     53   VectorT collection_;
     54 
     55   RTC_DISALLOW_COPY_AND_ASSIGN(ScopedPtrCollection);
     56 };
     57 
     58 }  // namespace rtc
     59 
     60 #endif  // WEBRTC_BASE_SCOPEDPTRCOLLECTION_H_
     61