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
34namespace XALAN_CPP_NAMESPACE {
35
36
37template<
38class ObjectType,
39#if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
40class CreateFunctorType,
41class DeleteFunctorType,
42class ResetFunctorType>
43#else
44class CreateFunctorType = DefaultCacheCreateFunctor<ObjectType>,
45class DeleteFunctorType = DeleteFunctor<ObjectType>,
46class ResetFunctorType = DefaultCacheResetFunctor<ObjectType> >
47#endif
49{
50public:
51
53
54 typedef ObjectType CacheObjectType;
55
56 explicit
58 MemoryManager& theManager,
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
73 m_stack.begin(),
74 m_stack.end(),
75 m_deleteFunctor);
76 }
77
78 ObjectType*
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*
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...
124
125 DeleteFunctorType m_deleteFunctor;
126
128
129private:
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
148template<class ObjectType>
149class XalanObjectStackCacheDefault : public XalanObjectStackCache<ObjectType, DefaultCacheCreateFunctor<ObjectType>, DeleteFunctor<ObjectType>, DefaultCacheResetFunctor<ObjectType> >
150{
151public:
152
154
155 explicit
160};
161
162
163
164}
165
166
167
168#endif
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
XalanObjectStackCacheDefault(XalanSize_t initialListSize=0)
XalanObjectStackCache< ObjectType, DefaultCacheCreateFunctor< ObjectType >, DeleteFunctor< ObjectType >, DefaultCacheResetFunctor< ObjectType > > BaseClassType
XalanObjectStackCache(MemoryManager &theManager, XalanSize_t initialListSize=0)
XalanVector< ObjectType * > VectorType