~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/base/pool.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2011 Google Inc.
2
 
//
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
6
 
//
7
 
//      http://www.apache.org/licenses/LICENSE-2.0
8
 
//
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.
14
 
//
15
 
// Author: jmaessen@google.com (Jan Maessen)
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"  // 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"
25
 
 
26
 
namespace net_instaweb {
27
 
 
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.
33
 
//
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
38
 
// destruction.
39
 
template<class T>
40
 
class Pool {
41
 
 public:
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;
45
 
 
46
 
  Pool() { }
47
 
 
48
 
  ~Pool() {
49
 
    DeleteAll();
50
 
  }
51
 
 
52
 
  // Is pool empty?
53
 
  bool empty() const {
54
 
    return contents_.empty();
55
 
  }
56
 
 
57
 
  // Size of pool
58
 
  size_t size() const {
59
 
    return contents_.size();
60
 
  }
61
 
 
62
 
  // Iterator pointing to beginning of pool
63
 
  iterator begin() {
64
 
    return contents_.begin();
65
 
  }
66
 
 
67
 
  // const Iterator pointing to beginning of pool
68
 
  const_iterator begin() const {
69
 
    return contents_.begin();
70
 
  }
71
 
 
72
 
  // Iterator pointing just past end of pool
73
 
  iterator end() {
74
 
    return contents_.end();
75
 
  }
76
 
 
77
 
  // Iterator pointing just past end of pool
78
 
  const_iterator end() const {
79
 
    return contents_.end();
80
 
  }
81
 
 
82
 
  // Add object to pool.  The object must not currently reside in a pool.
83
 
  void Add(T* object) {
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();
89
 
    --back_iter;
90
 
    *position = back_iter;
91
 
  }
92
 
 
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();
100
 
    return object;
101
 
  }
102
 
 
103
 
  // Return oldest object in pool, or NULL.
104
 
  T* oldest() const {
105
 
    T* result = NULL;
106
 
    if (!contents_.empty()) {
107
 
      result = contents_.front();
108
 
    }
109
 
    return result;
110
 
  }
111
 
 
112
 
  // Remove the least-recently-inserted object from the pool.  Potentially
113
 
  // cheaper than Remove(Oldest()).
114
 
  T* RemoveOldest() {
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
 
 
126
 
  // DeleteAll: delete all elements of pool
127
 
  void DeleteAll() {
128
 
    STLDeleteElements(&contents_);
129
 
  }
130
 
 
131
 
  // Clear: clear pool without deleting elements
132
 
  void Clear() {
133
 
    contents_.clear();
134
 
  }
135
 
 
136
 
 private:
137
 
  std::list<T*> contents_;
138
 
 
139
 
  DISALLOW_COPY_AND_ASSIGN(Pool);
140
 
};
141
 
 
142
 
}  // namespace net_instaweb
143
 
 
144
 
#endif  // PAGESPEED_KERNEL_BASE_POOL_H_