Xalan-C++ API Documentation

The Xalan C++ XSLT Processor Version 1.11


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 
33 XALAN_CPP_NAMESPACE_BEGIN
34 
35 
36 
37 template<class ObjectType>
39 {
40 public:
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 
52  theGuard.release();
53 
54  return theResult;
55  }
56 };
57 
58 
59 
60 template<class ObjectType>
62 {
63 public:
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 
75  theGuard.release();
76 
77  return theResult;
78  }
79 };
80 
81 
82 
83 template<class ObjectType>
85 {
86 public:
87 
88  void
89  operator()(ObjectType*) const
90  {
91  }
92 };
93 
94 
95 
96 template<class ObjectType>
98 {
99 public:
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 
112 template<
113 class ObjectType,
114 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
115 class CreateFunctorType,
116 class DeleteFunctorType,
117 class ResetFunctorType>
118 #else
119 class CreateFunctorType = DefaultCacheCreateFunctor<ObjectType>,
120 class DeleteFunctorType = DeleteFunctor<ObjectType>,
121 class ResetFunctorType = DefaultCacheResetFunctor<ObjectType> >
122 #endif
123 class XalanObjectCache
124 {
125 public:
126 
127  typedef XalanVector<ObjectType*> VectorType;
128 
129  typedef ObjectType CacheObjectType;
130 
131  explicit
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 
144  {
145  reset();
146 
147 #if !defined(XALAN_NO_STD_NAMESPACE)
148  using std::for_each;
149 #endif
150 
151  for_each(
152  m_availableList.begin(),
153  m_availableList.end(),
154  m_deleteFunctor(theManager));
155  }
156 
157  ObjectType*
158  get()
159  {
160  // We'll always return the back of the free list, since
161  // that's the cheapest thing.
162  if (m_availableList.empty() == true)
163  {
164  ObjectType* const theNewObject = m_createFunctor(m_availableList.getMemoryManager());
165 
166  m_busyList.push_back(theNewObject);
167 
168  return theNewObject;
169  }
170  else
171  {
172  ObjectType* const theObject = m_availableList.back();
173 
174  m_busyList.push_back(theObject);
175 
176  m_availableList.pop_back();
177 
178  return theObject;
179  }
180  }
181 
182  bool
183  release(ObjectType* theInstance)
184  {
185 #if !defined(XALAN_NO_STD_NAMESPACE)
186  using std::find;
187 #endif
188 
189  typedef typename VectorType::iterator IteratorType;
190 
191  const IteratorType i =
192  find(
193  m_busyList.begin(),
194  m_busyList.end(),
195  theInstance);
196 
197  if (i == m_busyList.end())
198  {
199  return false;
200  }
201  else
202  {
203  m_resetFunctor(theInstance);
204 
205  m_availableList.push_back(theInstance);
206 
207  m_busyList.erase(i);
208 
209  return true;
210  }
211  }
212 
213  void
214  reset()
215  {
216  while (m_busyList.empty() == false)
217  {
218  ObjectType* const theInstance = m_busyList.back();
219 
220  m_resetFunctor(theInstance);
221 
222  m_availableList.push_back(theInstance);
223 
224  m_busyList.pop_back();
225  }
226  }
227 
228  // Functors for various operations...
229  CreateFunctorType m_createFunctor;
230 
231  DeleteFunctorType m_deleteFunctor;
232 
233  ResetFunctorType m_resetFunctor;
234 
235 private:
236 
237  // There are not defined...
239 
242 
243 
244  // Data members...
245  VectorType m_availableList;
246 
247  VectorType m_busyList;
248 };
249 
250 
251 
252 #else
253 
254 
255 
256 template<
257 class ObjectType,
258 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
259 class CreateFunctorType,
260 class DeleteFunctorType,
261 class ResetFunctorType>
262 #else
263 class CreateFunctorType = DefaultCacheCreateFunctor<ObjectType>,
264 class DeleteFunctorType = DeleteFunctor<ObjectType>,
265 class ResetFunctorType = DefaultCacheResetFunctor<ObjectType> >
266 #endif
268 {
269 public:
270 
272 
273  typedef ObjectType CacheObjectType;
274 
275  explicit
276  XalanObjectCache(MemoryManager& theManager,
277  XalanSize_t initialListSize = 0) :
278  m_deleteFunctor(theManager),
279  m_availableList(theManager)
280  {
281  m_availableList.reserve(initialListSize);
282  }
283 
285  {
286  reset();
287 
288 #if !defined(XALAN_NO_STD_NAMESPACE)
289  using std::for_each;
290 #endif
291 
292  for_each(
293  m_availableList.begin(),
294  m_availableList.end(),
295  m_deleteFunctor);
296  }
297 
298  ObjectType*
299  get()
300  {
301  // We'll always return the back of the free list, since
302  // that's the cheapest thing.
303  if (m_availableList.empty() == true)
304  {
305  return m_createFunctor(m_availableList.getMemoryManager());
306  }
307  else
308  {
309  ObjectType* const theObject = m_availableList.back();
310 
311  m_availableList.pop_back();
312 
313  return theObject;
314  }
315  }
316 
317  bool
318  release(ObjectType* theInstance)
319  {
320  m_resetFunctor(theInstance);
321 
322  m_availableList.push_back(theInstance);
323 
324  return true;
325  }
326 
327  void
329  {
330  }
331 
332  // Functors for various operations...
333  CreateFunctorType m_createFunctor;
334 
335  DeleteFunctorType m_deleteFunctor;
336 
337  ResetFunctorType m_resetFunctor;
338 
339 private:
340 
341  // These are not defined...
343 
346 
347 
348  // Data members...
349  VectorType m_availableList;
350 };
351 
352 
353 
354 #endif
355 
356 
357 
358 template<class XalanObjectCacheType>
360 {
361 public:
362 
363  typedef typename XalanObjectCacheType::CacheObjectType CacheObjectType;
364 
365  GuardCachedObject(XalanObjectCacheType& theCache) :
366  m_cache(theCache),
367  m_cachedObject(theCache.get())
368  {
369  }
370 
372  {
373  if (m_cachedObject != 0)
374  {
375  m_cache.release(m_cachedObject);
376  }
377  }
378 
379  CacheObjectType*
380  get() const
381  {
382  return m_cachedObject;
383  }
384 
385  CacheObjectType*
387  {
388  CacheObjectType* const temp = m_cachedObject;
389 
390  m_cachedObject = 0;
391 
392  return temp;
393  }
394 
395 private:
396 
397  // Not implemented...
399 
400 
401  // Data members...
402  XalanObjectCacheType& m_cache;
403 
404  CacheObjectType* m_cachedObject;
405 };
406 
407 
408 
409 template<class ObjectType>
411  public XalanObjectCache<
412  ObjectType,
413  DefaultCacheCreateFunctor<ObjectType>,
414  DeleteFunctor<ObjectType>,
415  DefaultCacheResetFunctor<ObjectType> >
416 {
417 public:
418 
419  typedef XalanObjectCache<
420  ObjectType,
424 
425  explicit
427  MemoryManager& theManager,
428  XalanSize_t initialListSize = 0) :
429  BaseClassType(theManager, initialListSize)
430  {
431  }
432 };
433 
434 
435 
436 template<class ObjectType>
438  public XalanObjectCache<
439  ObjectType,
440  DefaultCacheCreateFunctorMemMgr<ObjectType>,
441  DeleteFunctor<ObjectType>,
442  DefaultCacheResetFunctor<ObjectType> >
443 {
444 public:
445 
446  typedef XalanObjectCache<
447  ObjectType,
451 
452  explicit
454  MemoryManager& theManager,
455  XalanSize_t initialListSize = 0) :
456  BaseClassType(theManager, initialListSize)
457  {
458  }
459 };
460 
461 
462 
463 XALAN_CPP_NAMESPACE_END
464 
465 
466 
467 #endif
XalanObjectCache< ObjectType, DefaultCacheCreateFunctor< ObjectType >, DeleteFunctor< ObjectType >, DefaultCacheResetFunctor< ObjectType > > BaseClassType
ObjectType CacheObjectType
XalanObjectCache< ObjectType, DefaultCacheCreateFunctorMemMgr< ObjectType >, DeleteFunctor< ObjectType >, DefaultCacheResetFunctor< ObjectType > > BaseClassType
void operator()(ObjectType *) const
ResetFunctorType m_resetFunctor
void operator()(ObjectType *theInstance) const
XalanMemoryManagerObjectCacheDefault(MemoryManager &theManager, XalanSize_t initialListSize=0)
XalanVector< ObjectType * > VectorType
CacheObjectType * release()
DeleteFunctorType m_deleteFunctor
ObjectType * operator()(MemoryManager &theManager) const
ObjectType * operator()(MemoryManager &theManager) const
XalanObjectCacheDefault(MemoryManager &theManager, XalanSize_t initialListSize=0)
CreateFunctorType m_createFunctor
XalanObjectCache(MemoryManager &theManager, XalanSize_t initialListSize=0)
XalanObjectCacheType::CacheObjectType CacheObjectType
GuardCachedObject(XalanObjectCacheType &theCache)
bool release(ObjectType *theInstance)

Interpreting class diagrams

Doxygen and GraphViz are used to generate this API documentation from the Xalan-C header files.

Xalan-C++ XSLT Processor Version 1.11
Copyright © 1999-2012 The Apache Software Foundation.
All Rights Reserved.

Apache Logo