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 
33 namespace XALAN_CPP_NAMESPACE {
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
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 
231 private:
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 
252 template<
253 class ObjectType,
254 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
255 class CreateFunctorType,
256 class DeleteFunctorType,
257 class ResetFunctorType>
258 #else
259 class CreateFunctorType = DefaultCacheCreateFunctor<ObjectType>,
260 class DeleteFunctorType = DeleteFunctor<ObjectType>,
261 class ResetFunctorType = DefaultCacheResetFunctor<ObjectType> >
262 #endif
264 {
265 public:
266 
268 
269  typedef ObjectType CacheObjectType;
270 
271  explicit
272  XalanObjectCache(MemoryManager& theManager,
273  XalanSize_t initialListSize = 0) :
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*
293  get()
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...
327  CreateFunctorType m_createFunctor;
328 
329  DeleteFunctorType m_deleteFunctor;
330 
331  ResetFunctorType m_resetFunctor;
332 
333 private:
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 
352 template<class XalanObjectCacheType>
354 {
355 public:
356 
357  typedef typename XalanObjectCacheType::CacheObjectType CacheObjectType;
358 
359  GuardCachedObject(XalanObjectCacheType& theCache) :
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 
389 private:
390 
391  // Not implemented...
393 
394 
395  // Data members...
396  XalanObjectCacheType& m_cache;
397 
398  CacheObjectType* m_cachedObject;
399 };
400 
401 
402 
403 template<class ObjectType>
405  public XalanObjectCache<
406  ObjectType,
407  DefaultCacheCreateFunctor<ObjectType>,
408  DeleteFunctor<ObjectType>,
409  DefaultCacheResetFunctor<ObjectType> >
410 {
411 public:
412 
413  typedef XalanObjectCache<
414  ObjectType,
418 
419  explicit
421  MemoryManager& theManager,
422  XalanSize_t initialListSize = 0) :
423  BaseClassType(theManager, initialListSize)
424  {
425  }
426 };
427 
428 
429 
430 template<class ObjectType>
432  public XalanObjectCache<
433  ObjectType,
434  DefaultCacheCreateFunctorMemMgr<ObjectType>,
435  DeleteFunctor<ObjectType>,
436  DefaultCacheResetFunctor<ObjectType> >
437 {
438 public:
439 
440  typedef XalanObjectCache<
441  ObjectType,
445 
446  explicit
448  MemoryManager& theManager,
449  XalanSize_t initialListSize = 0) :
450  BaseClassType(theManager, initialListSize)
451  {
452  }
453 };
454 
455 
456 
457 }
458 
459 
460 
461 #endif
xalanc::XalanMemoryManagerObjectCacheDefault
Definition: XalanObjectCache.hpp:431
XALAN_CPP_NAMESPACE
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
Definition: XalanVersion.hpp:76
xalanc::XalanAllocationGuard::release
void release()
Definition: XalanMemoryManagement.hpp:133
xalanc::XalanObjectCache::m_createFunctor
CreateFunctorType m_createFunctor
Definition: XalanObjectCache.hpp:327
xalanc::XalanObjectCache::CacheObjectType
ObjectType CacheObjectType
Definition: XalanObjectCache.hpp:269
xalanc::GuardCachedObject::CacheObjectType
XalanObjectCacheType::CacheObjectType CacheObjectType
Definition: XalanObjectCache.hpp:357
xalanc::XalanVector< ObjectType * >
xalanc::XalanObjectCache::~XalanObjectCache
~XalanObjectCache()
Definition: XalanObjectCache.hpp:280
xalanc::XalanObjectCache::reset
void reset()
Definition: XalanObjectCache.hpp:322
xalanc::DefaultCacheCreateFunctor
Definition: XalanObjectCache.hpp:38
xalanc::XalanObjectCacheDefault::XalanObjectCacheDefault
XalanObjectCacheDefault(MemoryManager &theManager, XalanSize_t initialListSize=0)
Definition: XalanObjectCache.hpp:420
xalanc::XalanObjectCache::get
ObjectType * get()
Definition: XalanObjectCache.hpp:293
xalanc::DefaultCacheCreateFunctorMemMgr::operator()
ObjectType * operator()(MemoryManager &theManager) const
Definition: XalanObjectCache.hpp:66
XalanVector.hpp
xalanc::GuardCachedObject::GuardCachedObject
GuardCachedObject(XalanObjectCacheType &theCache)
Definition: XalanObjectCache.hpp:359
xalanc::ClearCacheResetFunctor
Definition: XalanObjectCache.hpp:97
xalanc::XalanMemoryManagerObjectCacheDefault::BaseClassType
XalanObjectCache< ObjectType, DefaultCacheCreateFunctorMemMgr< ObjectType >, DeleteFunctor< ObjectType >, DefaultCacheResetFunctor< ObjectType > > BaseClassType
Definition: XalanObjectCache.hpp:444
STLHelper.hpp
xalanc::DefaultCacheResetFunctor::operator()
void operator()(ObjectType *) const
Definition: XalanObjectCache.hpp:89
xalanc::ClearCacheResetFunctor::operator()
void operator()(ObjectType *theInstance) const
Definition: XalanObjectCache.hpp:102
xalanc::DefaultCacheCreateFunctorMemMgr
Definition: XalanObjectCache.hpp:61
xalanc::XalanAllocationGuard
Definition: XalanMemoryManagement.hpp:96
xalanc::XalanObjectCacheDefault::BaseClassType
XalanObjectCache< ObjectType, DefaultCacheCreateFunctor< ObjectType >, DeleteFunctor< ObjectType >, DefaultCacheResetFunctor< ObjectType > > BaseClassType
Definition: XalanObjectCache.hpp:417
xalanc::XalanObjectCache
Definition: XalanObjectCache.hpp:263
xalanc::GuardCachedObject::release
CacheObjectType * release()
Definition: XalanObjectCache.hpp:380
xalanc::DeleteFunctor< ObjectType >
xalanc::XalanObjectCache::VectorType
XalanVector< ObjectType * > VectorType
Definition: XalanObjectCache.hpp:267
xalanc::DefaultCacheCreateFunctor::operator()
ObjectType * operator()(MemoryManager &theManager) const
Definition: XalanObjectCache.hpp:43
xalanc::DefaultCacheResetFunctor
Definition: XalanObjectCache.hpp:84
xalanc::GuardCachedObject::get
CacheObjectType * get() const
Definition: XalanObjectCache.hpp:374
xalanc::GuardCachedObject
Definition: XalanObjectCache.hpp:353
xalanc::XalanObjectCache::release
bool release(ObjectType *theInstance)
Definition: XalanObjectCache.hpp:312
xalanc::XalanAllocationGuard::get
void * get() const
Definition: XalanMemoryManagement.hpp:127
xalanc::XalanObjectCacheDefault
Definition: XalanObjectCache.hpp:404
xalanc::XalanObjectCache::XalanObjectCache
XalanObjectCache(MemoryManager &theManager, XalanSize_t initialListSize=0)
Definition: XalanObjectCache.hpp:272
xalanc::XalanMemoryManagerObjectCacheDefault::XalanMemoryManagerObjectCacheDefault
XalanMemoryManagerObjectCacheDefault(MemoryManager &theManager, XalanSize_t initialListSize=0)
Definition: XalanObjectCache.hpp:447
xalanc::GuardCachedObject::~GuardCachedObject
~GuardCachedObject()
Definition: XalanObjectCache.hpp:365
xalanc::XalanObjectCache::m_resetFunctor
ResetFunctorType m_resetFunctor
Definition: XalanObjectCache.hpp:331
xalanc::XalanObjectCache::m_deleteFunctor
DeleteFunctorType m_deleteFunctor
Definition: XalanObjectCache.hpp:329