Xalan-C++ API Reference  1.12.0
XalanObjectStackCache.hpp
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #if !defined(XALAN_OBJECTSTACKCACHE_HEADER_GUARD)
19 #define XALAN_OBJECTSTACKCACHE_HEADER_GUARD
20 
21 
22 
23 #include <algorithm>
24 
25 
26 
31 
32 
33 
34 namespace XALAN_CPP_NAMESPACE {
35 
36 
37 template<
38 class ObjectType,
39 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
40 class CreateFunctorType,
41 class DeleteFunctorType,
42 class ResetFunctorType>
43 #else
44 class CreateFunctorType = DefaultCacheCreateFunctor<ObjectType>,
45 class DeleteFunctorType = DeleteFunctor<ObjectType>,
46 class ResetFunctorType = DefaultCacheResetFunctor<ObjectType> >
47 #endif
49 {
50 public:
51 
53 
54  typedef ObjectType CacheObjectType;
55 
56  explicit
58  MemoryManager& theManager,
59  XalanSize_t initialListSize = 0) :
60  m_createFunctor(),
61  m_deleteFunctor(theManager),
62  m_stack(theManager),
63  m_numObjectsOnStack(0)
64  {
65  m_stack.reserve(initialListSize);
66  }
67 
69  {
70  using std::for_each;
71 
72  for_each(
73  m_stack.begin(),
74  m_stack.end(),
75  m_deleteFunctor);
76  }
77 
78  ObjectType*
79  get()
80  {
81 
82  if (m_stack.size() == m_numObjectsOnStack)
83  {
84  ObjectType* const theNewObject = m_createFunctor(m_stack.getMemoryManager());
85  m_stack.push_back(theNewObject);
86  ++m_numObjectsOnStack;
87  return theNewObject;
88  }
89  else
90  {
91  return m_stack[m_numObjectsOnStack++];
92  }
93  }
94 
95  ObjectType*
96  top()
97  {
98  assert (m_numObjectsOnStack > 0);
99 
100  return m_stack[m_numObjectsOnStack-1];
101  }
102 
103  ObjectType*
105  {
106  assert(m_numObjectsOnStack > 0);
107 
108  return m_stack[--m_numObjectsOnStack];
109  }
110 
111  void
113  {
114  typename VectorType::iterator iterator;
115 
116  for (iterator = m_stack.begin(); iterator < m_stack.end(); iterator++)
117  {
118  m_resetFunctor(*iterator);
119  }
120  }
121 
122  // Functors for various operations...
123  CreateFunctorType m_createFunctor;
124 
125  DeleteFunctorType m_deleteFunctor;
126 
127  ResetFunctorType m_resetFunctor;
128 
129 private:
130 
131  // There are not defined...
133 
136 
137 
138  // Data members...
139  VectorType m_stack;
140 
141  typename VectorType::size_type m_numObjectsOnStack;
142 
143 };
144 
145 
146 
147 
148 template<class ObjectType>
149 class XalanObjectStackCacheDefault : public XalanObjectStackCache<ObjectType, DefaultCacheCreateFunctor<ObjectType>, DeleteFunctor<ObjectType>, DefaultCacheResetFunctor<ObjectType> >
150 {
151 public:
152 
154 
155  explicit
156  XalanObjectStackCacheDefault(XalanSize_t initialListSize = 0) :
157  BaseClassType(initialListSize)
158  {
159  }
160 };
161 
162 
163 
164 }
165 
166 
167 
168 #endif
XALAN_CPP_NAMESPACE
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
Definition: XalanVersion.hpp:76
xalanc::XalanObjectStackCacheDefault::BaseClassType
XalanObjectStackCache< ObjectType, DefaultCacheCreateFunctor< ObjectType >, DeleteFunctor< ObjectType >, DefaultCacheResetFunctor< ObjectType > > BaseClassType
Definition: XalanObjectStackCache.hpp:153
xalanc::XalanObjectStackCache::m_resetFunctor
ResetFunctorType m_resetFunctor
Definition: XalanObjectStackCache.hpp:127
xalanc::XalanVector< ObjectType * >
xalanc::XalanObjectStackCache::release
ObjectType * release()
Definition: XalanObjectStackCache.hpp:104
xalanc::size_type
size_t size_type
Definition: XalanMap.hpp:46
xalanc::XalanObjectStackCache::top
ObjectType * top()
Definition: XalanObjectStackCache.hpp:96
xalanc::XalanObjectStackCache::m_createFunctor
CreateFunctorType m_createFunctor
Definition: XalanObjectStackCache.hpp:123
xalanc::XalanObjectStackCache::CacheObjectType
ObjectType CacheObjectType
Definition: XalanObjectStackCache.hpp:54
XalanVector.hpp
xalanc::XalanObjectStackCache::get
ObjectType * get()
Definition: XalanObjectStackCache.hpp:79
xalanc::XalanObjectStackCache::XalanObjectStackCache
XalanObjectStackCache(MemoryManager &theManager, XalanSize_t initialListSize=0)
Definition: XalanObjectStackCache.hpp:57
xalanc::XalanObjectStackCacheDefault::XalanObjectStackCacheDefault
XalanObjectStackCacheDefault(XalanSize_t initialListSize=0)
Definition: XalanObjectStackCache.hpp:156
STLHelper.hpp
xalanc::XalanObjectStackCache::reset
void reset()
Definition: XalanObjectStackCache.hpp:112
xalanc::XalanObjectStackCache::m_deleteFunctor
DeleteFunctorType m_deleteFunctor
Definition: XalanObjectStackCache.hpp:125
xalanc::XalanObjectStackCache::~XalanObjectStackCache
~XalanObjectStackCache()
Definition: XalanObjectStackCache.hpp:68
xalanc::XalanObjectStackCacheDefault
Definition: XalanObjectStackCache.hpp:149
xalanc::XalanVector< XalanDOMString * >::iterator
value_type * iterator
Definition: XalanVector.hpp:71
xalanc::XalanObjectCache
Definition: XalanObjectCache.hpp:263
XalanAutoPtr.hpp
xalanc::DeleteFunctor< ObjectType >
XalanObjectCache.hpp
xalanc::DefaultCacheResetFunctor
Definition: XalanObjectCache.hpp:84
xalanc::XalanObjectStackCache::VectorType
XalanVector< ObjectType * > VectorType
Definition: XalanObjectStackCache.hpp:52
xalanc::XalanObjectStackCache
Definition: XalanObjectStackCache.hpp:48