Xalan-C++ API Documentation

The Xalan C++ XSLT Processor Version 1.8

Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

XalanObjectCache.hpp

Go to the documentation of this file.
00001 /*
00002  * Copyright 1999-2004 The Apache Software Foundation.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #if !defined(XALAN_OBJECTCACHE_HEADER_GUARD)
00017 #define XALAN_OBJECTCACHE_HEADER_GUARD
00018 
00019 
00020 
00021 #include <algorithm>
00022 #include <vector>
00023 
00024 
00025 
00026 #include <xalanc/Include/STLHelper.hpp>
00027 #include <xalanc/Include/XalanAutoPtr.hpp>
00028 
00029 
00030 
00031 XALAN_CPP_NAMESPACE_BEGIN
00032 
00033 
00034 
00035 template<class ObjectType>
00036 class DefaultCacheCreateFunctor
00037 {
00038 public:
00039 
00040     ObjectType*
00041     operator()() const
00042     {
00043         return new ObjectType;
00044     }
00045 };
00046 
00047 
00048 
00049 template<class ObjectType>
00050 class DefaultCacheResetFunctor
00051 {
00052 public:
00053 
00054     void
00055     operator()(ObjectType*) const
00056     {
00057     }
00058 };
00059 
00060 
00061 
00062 template<class ObjectType>
00063 class ClearCacheResetFunctor
00064 {
00065 public:
00066 
00067     void
00068     operator()(ObjectType*  theInstance) const
00069     {
00070         theInstance->clear();
00071     }
00072 };
00073 
00074 
00075 
00076 #if defined(XALAN_OBJECT_CACHE_KEEP_BUSY_LIST)
00077 
00078 template<
00079 class ObjectType,
00080 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
00081 class CreateFunctorType,
00082 class DeleteFunctorType,
00083 class ResetFunctorType>
00084 #else
00085 class CreateFunctorType = DefaultCacheCreateFunctor<ObjectType>,
00086 class DeleteFunctorType = DeleteFunctor<ObjectType>,
00087 class ResetFunctorType = DefaultCacheResetFunctor<ObjectType> >
00088 #endif
00089 class XalanObjectCache
00090 {
00091 public:
00092 
00093 #if defined(XALAN_NO_STD_NAMESPACE)
00094     typedef vector<ObjectType*>         VectorType;
00095 #else
00096     typedef std::vector<ObjectType*>    VectorType;
00097 #endif
00098 
00099     typedef ObjectType  CacheObjectType;
00100 
00101     explicit
00102     XalanObjectCache(unsigned int   initialListSize = 0) :
00103         m_availableList(),
00104         m_busyList()
00105     {
00106         m_availableList.reserve(initialListSize);
00107 
00108         m_busyList.reserve(initialListSize);
00109     }
00110 
00111     ~XalanObjectCache()
00112     {
00113         reset();
00114 
00115 #if !defined(XALAN_NO_STD_NAMESPACE)
00116         using std::for_each;
00117 #endif
00118 
00119         for_each(
00120                 m_availableList.begin(),
00121                 m_availableList.end(),
00122                 m_deleteFunctor);
00123     }
00124 
00125     ObjectType*
00126     get()
00127     {
00128         // We'll always return the back of the free list, since
00129         // that's the cheapest thing.
00130         if (m_availableList.empty() == true)
00131         {
00132             ObjectType* const   theNewObject = m_createFunctor();
00133 
00134             m_busyList.push_back(theNewObject);
00135 
00136             return theNewObject;
00137         }
00138         else
00139         {
00140             ObjectType* const   theObject = m_availableList.back();
00141 
00142             m_busyList.push_back(theObject);
00143 
00144             m_availableList.pop_back();
00145 
00146             return theObject;
00147         }
00148     }
00149 
00150     bool
00151     release(ObjectType*     theInstance)
00152     {
00153 #if !defined(XALAN_NO_STD_NAMESPACE)
00154         using std::find;
00155 #endif
00156 
00157         typedef typename VectorType::iterator   IteratorType;
00158 
00159         const IteratorType  i =
00160             find(
00161                 m_busyList.begin(),
00162                 m_busyList.end(),
00163                 theInstance);
00164 
00165         if (i == m_busyList.end())
00166         {
00167             return false;
00168         }
00169         else
00170         {
00171             m_resetFunctor(theInstance);
00172 
00173             m_availableList.push_back(theInstance);
00174 
00175             m_busyList.erase(i);
00176 
00177             return true;
00178         }
00179     }
00180 
00181     void
00182     reset()
00183     {
00184         while (m_busyList.empty() == false)
00185         {
00186             ObjectType* const   theInstance = m_busyList.back();
00187 
00188             m_resetFunctor(theInstance);
00189 
00190             m_availableList.push_back(theInstance);
00191 
00192             m_busyList.pop_back();
00193         }
00194     }
00195 
00196     // Functors for various operations...
00197     CreateFunctorType   m_createFunctor;
00198 
00199     DeleteFunctorType   m_deleteFunctor;
00200 
00201     ResetFunctorType    m_resetFunctor;
00202 
00203 private:
00204 
00205     // There are not defined...
00206     XalanObjectCache(const XalanObjectCache<ObjectType, CreateFunctorType, DeleteFunctorType, ResetFunctorType>&    theRHS);
00207 
00208     XalanObjectCache<ObjectType, CreateFunctorType, DeleteFunctorType, ResetFunctorType>&
00209     operator=(const XalanObjectCache<ObjectType, CreateFunctorType, DeleteFunctorType, ResetFunctorType>&   theRHS);
00210 
00211 
00212     // Data members...
00213     VectorType          m_availableList;
00214 
00215     VectorType          m_busyList;
00216 };
00217 
00218 
00219 
00220 #else
00221 
00222 
00223 
00224 template<
00225 class ObjectType,
00226 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
00227 class CreateFunctorType,
00228 class DeleteFunctorType,
00229 class ResetFunctorType>
00230 #else
00231 class CreateFunctorType = DefaultCacheCreateFunctor<ObjectType>,
00232 class DeleteFunctorType = DeleteFunctor<ObjectType>,
00233 class ResetFunctorType = DefaultCacheResetFunctor<ObjectType> >
00234 #endif
00235 class XalanObjectCache
00236 {
00237 public:
00238 
00239 #if defined(XALAN_NO_STD_NAMESPACE)
00240     typedef vector<ObjectType*>         VectorType;
00241 #else
00242     typedef std::vector<ObjectType*>    VectorType;
00243 #endif
00244 
00245     typedef ObjectType  CacheObjectType;
00246 
00247     explicit
00248     XalanObjectCache(unsigned int   initialListSize = 0) :
00249         m_availableList()
00250     {
00251         m_availableList.reserve(initialListSize);
00252     }
00253 
00254     ~XalanObjectCache()
00255     {
00256         reset();
00257 
00258 #if !defined(XALAN_NO_STD_NAMESPACE)
00259         using std::for_each;
00260 #endif
00261 
00262         for_each(
00263                 m_availableList.begin(),
00264                 m_availableList.end(),
00265                 m_deleteFunctor);
00266     }
00267 
00268     ObjectType*
00269     get()
00270     {
00271         // We'll always return the back of the free list, since
00272         // that's the cheapest thing.
00273         if (m_availableList.empty() == true)
00274         {
00275             return m_createFunctor();
00276         }
00277         else
00278         {
00279             ObjectType* const   theObject = m_availableList.back();
00280 
00281             m_availableList.pop_back();
00282 
00283             return theObject;
00284         }
00285     }
00286 
00287     bool
00288     release(ObjectType*     theInstance)
00289     {
00290         m_resetFunctor(theInstance);
00291 
00292         m_availableList.push_back(theInstance);
00293 
00294         return true;
00295     }
00296 
00297     void
00298     reset()
00299     {
00300     }
00301 
00302     // Functors for various operations...
00303     CreateFunctorType   m_createFunctor;
00304 
00305     DeleteFunctorType   m_deleteFunctor;
00306 
00307     ResetFunctorType    m_resetFunctor;
00308 
00309 private:
00310 
00311     // These are not defined...
00312     XalanObjectCache(const XalanObjectCache<ObjectType, CreateFunctorType, DeleteFunctorType, ResetFunctorType>&    theRHS);
00313 
00314     XalanObjectCache<ObjectType, CreateFunctorType, DeleteFunctorType, ResetFunctorType>&
00315     operator=(const XalanObjectCache<ObjectType, CreateFunctorType, DeleteFunctorType, ResetFunctorType>&   theRHS);
00316 
00317 
00318     // Data members...
00319     VectorType          m_availableList;
00320 };
00321 
00322 
00323 
00324 #endif
00325 
00326 
00327 
00328 template<class XalanObjectCacheType>
00329 class GuardCachedObject
00330 {
00331 public:
00332 
00333     typedef typename XalanObjectCacheType::CacheObjectType  CacheObjectType;
00334 
00335     GuardCachedObject(XalanObjectCacheType& theCache) :
00336         m_cache(theCache),
00337         m_cachedObject(theCache.get())
00338     {
00339     }
00340 
00341     ~GuardCachedObject()
00342     {
00343         if (m_cachedObject != 0)
00344         {
00345             m_cache.release(m_cachedObject);
00346         }
00347     }
00348 
00349     CacheObjectType*
00350     get() const
00351     {
00352         return m_cachedObject;
00353     }
00354 
00355     CacheObjectType*
00356     release()
00357     {
00358         CacheObjectType* const  temp = m_cachedObject;
00359 
00360         m_cachedObject = 0;
00361 
00362         return temp;
00363     }
00364 
00365 private:
00366 
00367     // Not implemented...
00368     GuardCachedObject(const GuardCachedObject<XalanObjectCacheType>&);
00369 
00370 
00371     // Data members...
00372     XalanObjectCacheType&   m_cache;
00373 
00374     CacheObjectType*        m_cachedObject;
00375 };
00376 
00377 
00378 
00379 template<class ObjectType>
00380 class XalanObjectCacheDefault : public XalanObjectCache<ObjectType, DefaultCacheCreateFunctor<ObjectType>, DeleteFunctor<ObjectType>, DefaultCacheResetFunctor<ObjectType> >
00381 {
00382 public:
00383 
00384     typedef XalanObjectCache<ObjectType, DefaultCacheCreateFunctor<ObjectType>, DeleteFunctor<ObjectType>, DefaultCacheResetFunctor<ObjectType> >       BaseClassType;
00385 
00386     explicit
00387     XalanObjectCacheDefault(unsigned int    initialListSize = 0) :
00388         BaseClassType(initialListSize)
00389     {
00390     }
00391 };
00392 
00393 
00394 
00395 XALAN_CPP_NAMESPACE_END
00396 
00397 
00398 
00399 #endif

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.8
Copyright © 1999-2004 The Apache Software Foundation. All Rights Reserved.