Xalan-C++ API Reference 1.12.0
XalanObjectCache.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_OBJECTCACHE_HEADER_GUARD)
19#define XALAN_OBJECTCACHE_HEADER_GUARD
20
21
22
23#include <algorithm>
24
25
26
29
30
31
32
33namespace XALAN_CPP_NAMESPACE {
34
35
36
37template<class ObjectType>
39{
40public:
41
42 ObjectType*
43 operator()(MemoryManager& theManager) const
44 {
45 typedef ObjectType ThisType;
46
47 XalanAllocationGuard theGuard(theManager, theManager.allocate(sizeof(ThisType)));
48
49 ThisType* const theResult =
50 new (theGuard.get()) ThisType();
51
53
54 return theResult;
55 }
56};
57
58
59
60template<class ObjectType>
62{
63public:
64
65 ObjectType*
66 operator()(MemoryManager& theManager) const
67 {
68 typedef ObjectType ThisType;
69
70 XalanAllocationGuard theGuard(theManager, theManager.allocate(sizeof(ThisType)));
71
72 ThisType* const theResult =
73 new (theGuard.get()) ThisType(theManager);
74
76
77 return theResult;
78 }
79};
80
81
82
83template<class ObjectType>
85{
86public:
87
88 void
89 operator()(ObjectType*) const
90 {
91 }
92};
93
94
95
96template<class ObjectType>
98{
99public:
100
101 void
102 operator()(ObjectType* theInstance) const
103 {
104 theInstance->clear();
105 }
106};
107
108
109
110#if defined(XALAN_OBJECT_CACHE_KEEP_BUSY_LIST)
111
112template<
113class ObjectType,
114#if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
115class CreateFunctorType,
116class DeleteFunctorType,
117class ResetFunctorType>
118#else
119class CreateFunctorType = DefaultCacheCreateFunctor<ObjectType>,
120class DeleteFunctorType = DeleteFunctor<ObjectType>,
121class ResetFunctorType = DefaultCacheResetFunctor<ObjectType> >
122#endif
123class XalanObjectCache
124{
125public:
126
127 typedef XalanVector<ObjectType*> VectorType;
128
129 typedef ObjectType CacheObjectType;
130
131 explicit
132 XalanObjectCache(
133 MemoryManager& theManager,
134 XalanSize_t initialListSize = 0) :
135 m_availableList(theManager),
136 m_busyList(theManager)
137 {
138 m_availableList.reserve(initialListSize);
139
140 m_busyList.reserve(initialListSize);
141 }
142
143 ~XalanObjectCache()
144 {
145 reset();
146
147 using std::for_each;
148
149 for_each(
150 m_availableList.begin(),
151 m_availableList.end(),
152 m_deleteFunctor(theManager));
153 }
154
155 ObjectType*
156 get()
157 {
158 // We'll always return the back of the free list, since
159 // that's the cheapest thing.
160 if (m_availableList.empty() == true)
161 {
162 ObjectType* const theNewObject = m_createFunctor(m_availableList.getMemoryManager());
163
164 m_busyList.push_back(theNewObject);
165
166 return theNewObject;
167 }
168 else
169 {
170 ObjectType* const theObject = m_availableList.back();
171
172 m_busyList.push_back(theObject);
173
174 m_availableList.pop_back();
175
176 return theObject;
177 }
178 }
179
180 bool
181 release(ObjectType* theInstance)
182 {
183 using std::find;
184
185 typedef typename VectorType::iterator IteratorType;
186
187 const IteratorType i =
188 find(
189 m_busyList.begin(),
190 m_busyList.end(),
191 theInstance);
192
193 if (i == m_busyList.end())
194 {
195 return false;
196 }
197 else
198 {
199 m_resetFunctor(theInstance);
200
201 m_availableList.push_back(theInstance);
202
203 m_busyList.erase(i);
204
205 return true;
206 }
207 }
208
209 void
210 reset()
211 {
212 while (m_busyList.empty() == false)
213 {
214 ObjectType* const theInstance = m_busyList.back();
215
216 m_resetFunctor(theInstance);
217
218 m_availableList.push_back(theInstance);
219
220 m_busyList.pop_back();
221 }
222 }
223
224 // Functors for various operations...
225 CreateFunctorType m_createFunctor;
226
227 DeleteFunctorType m_deleteFunctor;
228
229 ResetFunctorType m_resetFunctor;
230
231private:
232
233 // There are not defined...
234 XalanObjectCache(const XalanObjectCache<ObjectType, CreateFunctorType, DeleteFunctorType, ResetFunctorType>& theRHS);
235
236 XalanObjectCache<ObjectType, CreateFunctorType, DeleteFunctorType, ResetFunctorType>&
237 operator=(const XalanObjectCache<ObjectType, CreateFunctorType, DeleteFunctorType, ResetFunctorType>& theRHS);
238
239
240 // Data members...
241 VectorType m_availableList;
242
243 VectorType m_busyList;
244};
245
246
247
248#else
249
250
251
252template<
253class ObjectType,
254#if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
255class CreateFunctorType,
256class DeleteFunctorType,
257class ResetFunctorType>
258#else
259class CreateFunctorType = DefaultCacheCreateFunctor<ObjectType>,
260class DeleteFunctorType = DeleteFunctor<ObjectType>,
261class ResetFunctorType = DefaultCacheResetFunctor<ObjectType> >
262#endif
264{
265public:
266
268
269 typedef ObjectType CacheObjectType;
270
271 explicit
274 m_deleteFunctor(theManager),
275 m_availableList(theManager)
276 {
277 m_availableList.reserve(initialListSize);
278 }
279
281 {
282 reset();
283
284 using std::for_each;
285
286 for_each(
287 m_availableList.begin(),
288 m_availableList.end(),
289 m_deleteFunctor);
290 }
291
292 ObjectType*
294 {
295 // We'll always return the back of the free list, since
296 // that's the cheapest thing.
297 if (m_availableList.empty() == true)
298 {
299 return m_createFunctor(m_availableList.getMemoryManager());
300 }
301 else
302 {
303 ObjectType* const theObject = m_availableList.back();
304
305 m_availableList.pop_back();
306
307 return theObject;
308 }
309 }
310
311 bool
312 release(ObjectType* theInstance)
313 {
314 m_resetFunctor(theInstance);
315
316 m_availableList.push_back(theInstance);
317
318 return true;
319 }
320
321 void
323 {
324 }
325
326 // Functors for various operations...
328
329 DeleteFunctorType m_deleteFunctor;
330
332
333private:
334
335 // These are not defined...
337
340
341
342 // Data members...
343 VectorType m_availableList;
344};
345
346
347
348#endif
349
350
351
352template<class XalanObjectCacheType>
354{
355public:
356
357 typedef typename XalanObjectCacheType::CacheObjectType CacheObjectType;
358
360 m_cache(theCache),
361 m_cachedObject(theCache.get())
362 {
363 }
364
366 {
367 if (m_cachedObject != 0)
368 {
369 m_cache.release(m_cachedObject);
370 }
371 }
372
373 CacheObjectType*
374 get() const
375 {
376 return m_cachedObject;
377 }
378
379 CacheObjectType*
381 {
382 CacheObjectType* const temp = m_cachedObject;
383
384 m_cachedObject = 0;
385
386 return temp;
387 }
388
389private:
390
391 // Not implemented...
393
394
395 // Data members...
396 XalanObjectCacheType& m_cache;
397
398 CacheObjectType* m_cachedObject;
399};
400
401
402
403template<class ObjectType>
405 public XalanObjectCache<
406 ObjectType,
407 DefaultCacheCreateFunctor<ObjectType>,
408 DeleteFunctor<ObjectType>,
409 DefaultCacheResetFunctor<ObjectType> >
410{
411public:
412
413 typedef XalanObjectCache<
414 ObjectType,
418
419 explicit
426};
427
428
429
430template<class ObjectType>
432 public XalanObjectCache<
433 ObjectType,
434 DefaultCacheCreateFunctorMemMgr<ObjectType>,
435 DeleteFunctor<ObjectType>,
436 DefaultCacheResetFunctor<ObjectType> >
437{
438public:
439
440 typedef XalanObjectCache<
441 ObjectType,
445
446 explicit
453};
454
455
456
457}
458
459
460
461#endif
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
void operator()(ObjectType *theInstance) const
ObjectType * operator()(MemoryManager &theManager) const
ObjectType * operator()(MemoryManager &theManager) const
void operator()(ObjectType *) const
GuardCachedObject(XalanObjectCacheType &theCache)
XalanObjectCacheType::CacheObjectType CacheObjectType
CacheObjectType * get() const
XalanMemoryManagerObjectCacheDefault(MemoryManager &theManager, XalanSize_t initialListSize=0)
XalanObjectCache< ObjectType, DefaultCacheCreateFunctorMemMgr< ObjectType >, DeleteFunctor< ObjectType >, DefaultCacheResetFunctor< ObjectType > > BaseClassType
XalanObjectCacheDefault(MemoryManager &theManager, XalanSize_t initialListSize=0)
XalanObjectCache< ObjectType, DefaultCacheCreateFunctor< ObjectType >, DeleteFunctor< ObjectType >, DefaultCacheResetFunctor< ObjectType > > BaseClassType
XalanVector< ObjectType * > VectorType
XalanObjectCache(MemoryManager &theManager, XalanSize_t initialListSize=0)
bool release(ObjectType *theInstance)
CreateFunctorType m_createFunctor
DeleteFunctorType m_deleteFunctor