Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pool.h
Go to the documentation of this file.
1 // Copyright 2011 Google Inc.
16 
17 #ifndef PAGESPEED_KERNEL_BASE_POOL_H_
18 #define PAGESPEED_KERNEL_BASE_POOL_H_
19 #include <cstddef>
20 #include <list>
21 #include "base/logging.h"
25 
26 namespace net_instaweb {
27 
39 template<class T>
40 class Pool {
41  public:
43  typedef typename PoolElement<T>::Position iterator;
44  typedef typename std::list<T*>::const_iterator const_iterator;
45 
46  Pool() { }
47 
48  ~Pool() {
49  DeleteAll();
50  }
51 
53  bool empty() const {
54  return contents_.empty();
55  }
56 
58  size_t size() const {
59  return contents_.size();
60  }
61 
64  return contents_.begin();
65  }
66 
68  const_iterator begin() const {
69  return contents_.begin();
70  }
71 
74  return contents_.end();
75  }
76 
78  const_iterator end() const {
79  return contents_.end();
80  }
81 
83  void Add(T* object) {
84  iterator* position = object->pool_position();
85  contents_.push_back(object);
88  iterator back_iter = contents_.end();
89  --back_iter;
90  *position = back_iter;
91  }
92 
95  T* Remove(T* object) {
96  iterator* position = object->pool_position();
97  DCHECK(**position == object);
98  contents_.erase(*position);
99  *position = contents_.end();
100  return object;
101  }
102 
104  T* oldest() const {
105  T* result = NULL;
106  if (!contents_.empty()) {
107  result = contents_.front();
108  }
109  return result;
110  }
111 
115  T* result = NULL;
116  if (!contents_.empty()) {
117  result = contents_.front();
118  iterator* position = result->pool_position();
119  DCHECK(*position == contents_.begin());
120  contents_.pop_front();
121  *position = contents_.end();
122  }
123  return result;
124  }
125 
127  void DeleteAll() {
128  STLDeleteElements(&contents_);
129  }
130 
132  void Clear() {
133  contents_.clear();
134  }
135 
136  private:
137  std::list<T*> contents_;
138 
139 
140 };
141 
142 }
143 
144 #endif
const_iterator begin() const
const Iterator pointing to beginning of pool
Definition: pool.h:68
Position * pool_position()
Definition: pool_element.h:41
Definition: pool.h:40
bool empty() const
Is pool empty?
Definition: pool.h:53
T * Remove(T *object)
Definition: pool.h:95
iterator begin()
Iterator pointing to beginning of pool.
Definition: pool.h:63
const_iterator end() const
Iterator pointing just past end of pool.
Definition: pool.h:78
T * RemoveOldest()
Definition: pool.h:114
PoolElement< T >::Position iterator
We can iterate over a pool using this iterator type.
Definition: pool.h:43
void Add(T *object)
Add object to pool. The object must not currently reside in a pool.
Definition: pool.h:83
iterator end()
Iterator pointing just past end of pool.
Definition: pool.h:73
size_t size() const
Size of pool.
Definition: pool.h:58
T * oldest() const
Return oldest object in pool, or NULL.
Definition: pool.h:104
void DeleteAll()
DeleteAll: delete all elements of pool.
Definition: pool.h:127
void Clear()
Clear: clear pool without deleting elements.
Definition: pool.h:132