1
// Copyright 2011 Google Inc.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
15
// Author: jmaessen@google.com (Jan Maessen)
17
#ifndef PAGESPEED_KERNEL_BASE_POOL_H_
18
#define PAGESPEED_KERNEL_BASE_POOL_H_
21
#include "base/logging.h" // for DCHECK
22
#include "pagespeed/kernel/base/basictypes.h"
23
#include "pagespeed/kernel/base/pool_element.h"
24
#include "pagespeed/kernel/base/stl_util.h"
26
namespace net_instaweb {
28
// A pool holds references to objects of type T that provide a pool_position
29
// method (which they can do by extending PoolElement in pool_element.h, or by
30
// providing a getter method of type PoolElement::Position* pool_position() (an
31
// abstract settable pointer for a PoolElement::Position). Pool objects are
32
// maintained in insertion order.
34
// We can insert and remove references from a pool, ask for an arbitrary
35
// reference contained in a pool, and clear or delete all pool elements. By
36
// default on destruction we delete pool elements using DeleteAll(); if a pool
37
// does not acquire object ownership, we should instead .Clear() it before
42
// We can iterate over a pool using this iterator type.
43
typedef typename PoolElement<T>::Position iterator;
44
typedef typename std::list<T*>::const_iterator const_iterator;
54
return contents_.empty();
59
return contents_.size();
62
// Iterator pointing to beginning of pool
64
return contents_.begin();
67
// const Iterator pointing to beginning of pool
68
const_iterator begin() const {
69
return contents_.begin();
72
// Iterator pointing just past end of pool
74
return contents_.end();
77
// Iterator pointing just past end of pool
78
const_iterator end() const {
79
return contents_.end();
82
// Add object to pool. The object must not currently reside in a pool.
84
iterator* position = object->pool_position();
85
contents_.push_back(object);
86
// We need to get an iterator to the last element. We locally bind to avoid
87
// potential compiler trouble.
88
iterator back_iter = contents_.end();
90
*position = back_iter;
93
// Remove specified object from pool. The object must have previously been
94
// inserted into this pool. Returns object.
95
T* Remove(T* object) {
96
iterator* position = object->pool_position();
97
DCHECK(**position == object);
98
contents_.erase(*position);
99
*position = contents_.end();
103
// Return oldest object in pool, or NULL.
106
if (!contents_.empty()) {
107
result = contents_.front();
112
// Remove the least-recently-inserted object from the pool. Potentially
113
// cheaper than Remove(Oldest()).
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();
126
// DeleteAll: delete all elements of pool
128
STLDeleteElements(&contents_);
131
// Clear: clear pool without deleting elements
137
std::list<T*> contents_;
139
DISALLOW_COPY_AND_ASSIGN(Pool);
142
} // namespace net_instaweb
144
#endif // PAGESPEED_KERNEL_BASE_POOL_H_