Xalan-C++ API Documentation

The Xalan C++ XSLT Processor Version 1.10

XalanObjectCache.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #if !defined(XALAN_OBJECTCACHE_HEADER_GUARD)
17 #define XALAN_OBJECTCACHE_HEADER_GUARD
18 
19 
20 
21 #include <algorithm>
22 
23 
24 
27 
28 
29 
30 
31 XALAN_CPP_NAMESPACE_BEGIN
32 
33 
34 
35 template<class ObjectType>
37 {
38 public:
39 
40  ObjectType*
41  operator()(MemoryManagerType& theManager) const
42  {
43  typedef ObjectType ThisType;
44 
45  XalanMemMgrAutoPtr<ThisType, false> theGuard( theManager , (ThisType*)theManager.allocate(sizeof(ThisType)));
46 
47  ThisType* theResult = theGuard.get();
48 
49  new (theResult) ThisType();
50 
51  theGuard.release();
52 
53  return theResult;
54  }
55 };
56 
57 
58 template<class ObjectType>
60 {
61 public:
62 
63  ObjectType*
64  operator()(MemoryManagerType& theManager) const
65  {
66  typedef ObjectType ThisType;
67 
68  XalanMemMgrAutoPtr<ThisType, false> theGuard( theManager , (ThisType*)theManager.allocate(sizeof(ThisType)));
69 
70  ThisType* theResult = theGuard.get();
71 
72  new (theResult) ThisType(theManager);
73 
74  theGuard.release();
75 
76  return theResult;
77  }
78 };
79 
80 
81 template<class ObjectType>
83 {
84 public:
85 
86  void
87  operator()(ObjectType*) const
88  {
89  }
90 };
91 
92 
93 
94 template<class ObjectType>
96 {
97 public:
98 
99  void
100  operator()(ObjectType* theInstance) const
101  {
102  theInstance->clear();
103  }
104 };
105 
106 
107 
108 #if defined(XALAN_OBJECT_CACHE_KEEP_BUSY_LIST)
109 
110 template<
111 class ObjectType,
112 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
113 class CreateFunctorType,
114 class DeleteFunctorType,
115 class ResetFunctorType>
116 #else
117 class CreateFunctorType = DefaultCacheCreateFunctor<ObjectType>,
118 class DeleteFunctorType = DeleteFunctor<ObjectType>,
119 class ResetFunctorType = DefaultCacheResetFunctor<ObjectType> >
120 #endif
121 class XalanObjectCache
122 {
123 public:
124 
126 
127  typedef ObjectType CacheObjectType;
128 
129  explicit
131  MemoryManagerType& theManager,
132  unsigned int initialListSize = 0) :
133  m_availableList(theManager),
134  m_busyList(theManager)
135  {
136  m_availableList.reserve(initialListSize);
137 
138  m_busyList.reserve(initialListSize);
139  }
140 
142  {
143  reset();
144 
145 #if !defined(XALAN_NO_STD_NAMESPACE)
146  using std::for_each;
147 #endif
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 #if !defined(XALAN_NO_STD_NAMESPACE)
184  using std::find;
185 #endif
186 
187  typedef typename VectorType::iterator IteratorType;
188 
189  const IteratorType i =
190  find(
191  m_busyList.begin(),
192  m_busyList.end(),
193  theInstance);
194 
195  if (i == m_busyList.end())
196  {
197  return false;
198  }
199  else
200  {
201  m_resetFunctor(theInstance);
202 
203  m_availableList.push_back(theInstance);
204 
205  m_busyList.erase(i);
206 
207  return true;
208  }
209  }
210 
211  void
212  reset()
213  {
214  while (m_busyList.empty() == false)
215  {
216  ObjectType* const theInstance = m_busyList.back();
217 
218  m_resetFunctor(theInstance);
219 
220  m_availableList.push_back(theInstance);
221 
222  m_busyList.pop_back();
223  }
224  }
225 
226  // Functors for various operations...
227  CreateFunctorType m_createFunctor;
228 
229  DeleteFunctorType m_deleteFunctor;
230 
231  ResetFunctorType m_resetFunctor;
232 
233 private:
234 
235  // There are not defined...
237 
240 
241 
242  // Data members...
243  VectorType m_availableList;
244 
245  VectorType m_busyList;
246 };
247 
248 
249 
250 #else
251 
252 
253 
254 template<
255 class ObjectType,
256 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
257 class CreateFunctorType,
258 class DeleteFunctorType,
259 class ResetFunctorType>
260 #else
261 class CreateFunctorType = DefaultCacheCreateFunctor<ObjectType>,
262 class DeleteFunctorType = DeleteFunctor<ObjectType>,
263 class ResetFunctorType = DefaultCacheResetFunctor<ObjectType> >
264 #endif
266 {
267 public:
268 
270 
271  typedef ObjectType CacheObjectType;
272 
273  explicit
275  unsigned int initialListSize = 0) :
276  m_deleteFunctor(theManager),
277  m_availableList(theManager)
278  {
279  m_availableList.reserve(initialListSize);
280  }
281 
283  {
284  reset();
285 
286 #if !defined(XALAN_NO_STD_NAMESPACE)
287  using std::for_each;
288 #endif
289 
290  for_each(
291  m_availableList.begin(),
292  m_availableList.end(),
294  }
295 
296  ObjectType*
297  get()
298  {
299  // We'll always return the back of the free list, since
300  // that's the cheapest thing.
301  if (m_availableList.empty() == true)
302  {
303  return m_createFunctor(m_availableList.getMemoryManager());
304  }
305  else
306  {
307  ObjectType* const theObject = m_availableList.back();
308 
309  m_availableList.pop_back();
310 
311  return theObject;
312  }
313  }
314 
315  bool
316  release(ObjectType* theInstance)
317  {
318  m_resetFunctor(theInstance);
319 
320  m_availableList.push_back(theInstance);
321 
322  return true;
323  }
324 
325  void
327  {
328  }
329 
330  // Functors for various operations...
331  CreateFunctorType m_createFunctor;
332 
333  DeleteFunctorType m_deleteFunctor;
334 
335  ResetFunctorType m_resetFunctor;
336 
337 private:
338 
339  // These are not defined...
341 
344 
345 
346  // Data members...
347  VectorType m_availableList;
348 };
349 
350 
351 
352 #endif
353 
354 
355 
356 template<class XalanObjectCacheType>
358 {
359 public:
360 
361  typedef typename XalanObjectCacheType::CacheObjectType CacheObjectType;
362 
363  GuardCachedObject(XalanObjectCacheType& theCache) :
364  m_cache(theCache),
365  m_cachedObject(theCache.get())
366  {
367  }
368 
370  {
371  if (m_cachedObject != 0)
372  {
373  m_cache.release(m_cachedObject);
374  }
375  }
376 
378  get() const
379  {
380  return m_cachedObject;
381  }
382 
385  {
386  CacheObjectType* const temp = m_cachedObject;
387 
388  m_cachedObject = 0;
389 
390  return temp;
391  }
392 
393 private:
394 
395  // Not implemented...
397 
398 
399  // Data members...
400  XalanObjectCacheType& m_cache;
401 
402  CacheObjectType* m_cachedObject;
403 };
404 
405 
406 
407 template<class ObjectType>
409  public XalanObjectCache<
410  ObjectType,
411  DefaultCacheCreateFunctor<ObjectType>,
412  DeleteFunctor<ObjectType>,
413  DefaultCacheResetFunctor<ObjectType> >
414 {
415 public:
416 
417  typedef XalanObjectCache<
418  ObjectType,
422 
423  explicit
425  MemoryManagerType& theManager,
426  unsigned int initialListSize = 0) :
427  BaseClassType(theManager, initialListSize)
428  {
429  }
430 };
431 
432 
433 
434 template<class ObjectType>
436  public XalanObjectCache<
437  ObjectType,
438  DefaultCacheCreateFunctorMemMgr<ObjectType>,
439  DeleteFunctor<ObjectType>,
440  DefaultCacheResetFunctor<ObjectType> >
441 {
442 public:
443 
444  typedef XalanObjectCache<
445  ObjectType,
449 
450  explicit
452  MemoryManagerType& theManager,
453  unsigned int initialListSize = 0) :
454  BaseClassType(theManager, initialListSize)
455  {
456  }
457 };
458 
459 
460 
461 XALAN_CPP_NAMESPACE_END
462 
463 
464 
465 #endif
XalanObjectCache< ObjectType, DefaultCacheCreateFunctor< ObjectType >, DeleteFunctor< ObjectType >, DefaultCacheResetFunctor< ObjectType > > BaseClassType
Definition: XalanObjectCache.hpp:421
Definition: XalanObjectCache.hpp:265
ObjectType CacheObjectType
Definition: XalanObjectCache.hpp:271
XalanObjectCache< ObjectType, DefaultCacheCreateFunctorMemMgr< ObjectType >, DeleteFunctor< ObjectType >, DefaultCacheResetFunctor< ObjectType > > BaseClassType
Definition: XalanObjectCache.hpp:448
Definition: XalanObjectCache.hpp:95
XALAN_CPP_NAMESPACE_BEGIN typedef XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager MemoryManagerType
Definition: XalanMemoryManagement.hpp:39
iterator end()
Definition: XalanVector.hpp:701
Definition: XalanObjectCache.hpp:59
XalanMemoryManagerObjectCacheDefault(MemoryManagerType &theManager, unsigned int initialListSize=0)
Definition: XalanObjectCache.hpp:451
value_type * iterator
Definition: XalanVector.hpp:91
XalanObjectCache(MemoryManagerType &theManager, unsigned int initialListSize=0)
Definition: XalanObjectCache.hpp:274
ResetFunctorType m_resetFunctor
Definition: XalanObjectCache.hpp:335
~XalanObjectCache()
Definition: XalanObjectCache.hpp:282
reference back()
Definition: XalanVector.hpp:673
void operator()(ObjectType *) const
Definition: XalanObjectCache.hpp:87
void pop_back()
Definition: XalanVector.hpp:256
bool empty() const
Definition: XalanVector.hpp:636
Definition: XalanObjectCache.hpp:435
Definition: XalanObjectCache.hpp:82
void push_back(const value_type &data)
Definition: XalanVector.hpp:246
XalanVector< ObjectType * > VectorType
Definition: XalanObjectCache.hpp:269
iterator begin()
Definition: XalanVector.hpp:685
CacheObjectType * release()
Definition: XalanObjectCache.hpp:384
ObjectType * operator()(MemoryManagerType &theManager) const
Definition: XalanObjectCache.hpp:41
CacheObjectType * get() const
Definition: XalanObjectCache.hpp:378
~GuardCachedObject()
Definition: XalanObjectCache.hpp:369
void reserve(size_type theSize)
Definition: XalanVector.hpp:644
Definition: XalanObjectCache.hpp:357
Type * get() const
Definition: XalanMemMgrAutoPtr.hpp:164
DeleteFunctorType m_deleteFunctor
Definition: XalanObjectCache.hpp:333
void reset()
Definition: XalanObjectCache.hpp:326
ObjectType * operator()(MemoryManagerType &theManager) const
Definition: XalanObjectCache.hpp:64
Definition: XalanMemMgrAutoPtr.hpp:46
Definition: XalanObjectCache.hpp:36
Definition: XalanObjectCache.hpp:408
CreateFunctorType m_createFunctor
Definition: XalanObjectCache.hpp:331
XalanObjectCacheType::CacheObjectType CacheObjectType
Definition: XalanObjectCache.hpp:361
GuardCachedObject(XalanObjectCacheType &theCache)
Definition: XalanObjectCache.hpp:363
XalanObjectCacheDefault(MemoryManagerType &theManager, unsigned int initialListSize=0)
Definition: XalanObjectCache.hpp:424
bool release(ObjectType *theInstance)
Definition: XalanObjectCache.hpp:316
const MemoryManager * getMemoryManager() const
Definition: XalanVector.hpp:871
void operator()(ObjectType *theInstance) const
Definition: XalanObjectCache.hpp:100

Interpreting class diagrams

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

dot

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

Apache Logo