Page Speed Optimization Libraries  1.2.24.1
net/instaweb/util/public/pool.h
Go to the documentation of this file.
00001 // Copyright 2011 Google Inc.
00016 
00017 #ifndef NET_INSTAWEB_UTIL_PUBLIC_POOL_H_
00018 #define NET_INSTAWEB_UTIL_PUBLIC_POOL_H_
00019 #include <cstddef>
00020 #include <list>
00021 #include "base/logging.h"  
00022 #include "net/instaweb/util/public/basictypes.h"
00023 #include "net/instaweb/util/public/pool_element.h"
00024 #include "net/instaweb/util/public/stl_util.h"
00025 
00026 namespace net_instaweb {
00027 
00039 template<class T>
00040 class Pool {
00041  public:
00043   typedef typename PoolElement<T>::Position iterator;
00044   typedef typename std::list<T*>::const_iterator const_iterator;
00045 
00046   Pool() { }
00047 
00048   ~Pool() {
00049     DeleteAll();
00050   }
00051 
00053   bool empty() const {
00054     return contents_.empty();
00055   }
00056 
00058   size_t size() const {
00059     return contents_.size();
00060   }
00061 
00063   iterator begin() {
00064     return contents_.begin();
00065   }
00066 
00068   const_iterator begin() const {
00069     return contents_.begin();
00070   }
00071 
00073   iterator end() {
00074     return contents_.end();
00075   }
00076 
00078   const_iterator end() const {
00079     return contents_.end();
00080   }
00081 
00083   void Add(T* object) {
00084     iterator* position = object->pool_position();
00085     contents_.push_back(object);
00088     iterator back_iter = contents_.end();
00089     --back_iter;
00090     *position = back_iter;
00091   }
00092 
00095   T* Remove(T* object) {
00096     iterator* position = object->pool_position();
00097     DCHECK(**position == object);
00098     contents_.erase(*position);
00099     *position = contents_.end();
00100     return object;
00101   }
00102 
00104   T* oldest() const {
00105     T* result = NULL;
00106     if (!contents_.empty()) {
00107       result = contents_.front();
00108     }
00109     return result;
00110   }
00111 
00114   T* RemoveOldest() {
00115     T* result = NULL;
00116     if (!contents_.empty()) {
00117       result = contents_.front();
00118       iterator* position = result->pool_position();
00119       DCHECK(*position == contents_.begin());
00120       contents_.pop_front();
00121       *position = contents_.end();
00122     }
00123     return result;
00124   }
00125 
00127   void DeleteAll() {
00128     STLDeleteElements(&contents_);
00129   }
00130 
00132   void Clear() {
00133     contents_.clear();
00134   }
00135 
00136  private:
00137   std::list<T*> contents_;
00138 
00139   DISALLOW_COPY_AND_ASSIGN(Pool);
00140 };
00141 
00142 }  
00143 
00144 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_POOL_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines