Xalan-C++ API Documentation

The Xalan C++ XSLT Processor Version 1.4

Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

XalanObjectCache.hpp

Go to the documentation of this file.
00001 /*
00002  * The Apache Software License, Version 1.1
00003  *
00004  *
00005  * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights 
00006  * reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  *
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer. 
00014  *
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in
00017  *    the documentation and/or other materials provided with the
00018  *    distribution.
00019  *
00020  * 3. The end-user documentation included with the redistribution,
00021  *    if any, must include the following acknowledgment:  
00022  *       "This product includes software developed by the
00023  *        Apache Software Foundation (http://www.apache.org/)."
00024  *    Alternately, this acknowledgment may appear in the software itself,
00025  *    if and wherever such third-party acknowledgments normally appear.
00026  *
00027  * 4. The names "Xalan" and "Apache Software Foundation" must
00028  *    not be used to endorse or promote products derived from this
00029  *    software without prior written permission. For written 
00030  *    permission, please contact apache@apache.org.
00031  *
00032  * 5. Products derived from this software may not be called "Apache",
00033  *    nor may "Apache" appear in their name, without prior written
00034  *    permission of the Apache Software Foundation.
00035  *
00036  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
00037  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00038  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00039  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
00040  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00041  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00042  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00043  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00044  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00045  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00046  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00047  * SUCH DAMAGE.
00048  * ====================================================================
00049  *
00050  * This software consists of voluntary contributions made by many
00051  * individuals on behalf of the Apache Software Foundation and was
00052  * originally based on software copyright (c) 1999, International
00053  * Business Machines, Inc., http://www.ibm.com.  For more
00054  * information on the Apache Software Foundation, please see
00055  * <http://www.apache.org/>.
00056  */
00057 #if !defined(XALAN_OBJECTCACHE_HEADER_GUARD)
00058 #define XALAN_OBJECTCACHE_HEADER_GUARD
00059 
00060 
00061 
00062 #include <algorithm>
00063 #include <vector>
00064 
00065 
00066 
00067 #include <Include/STLHelper.hpp>
00068 #include <Include/XalanAutoPtr.hpp>
00069 
00070 
00071 
00072 template<class ObjectType>
00073 class DefaultCacheCreateFunctor
00074 {
00075 public:
00076 
00077     ObjectType*
00078     operator()() const
00079     {
00080         return new ObjectType;
00081     }
00082 };
00083 
00084 
00085 
00086 template<class ObjectType>
00087 class DefaultCacheResetFunctor
00088 {
00089 public:
00090 
00091     void
00092     operator()(ObjectType*) const
00093     {
00094     }
00095 };
00096 
00097 
00098 
00099 template<class ObjectType>
00100 class ClearCacheResetFunctor
00101 {
00102 public:
00103 
00104     void
00105     operator()(ObjectType*  theInstance) const
00106     {
00107         theInstance->clear();
00108     }
00109 };
00110 
00111 
00112 
00113 #if defined(XALAN_OBJECT_CACHE_KEEP_BUSY_LIST)
00114 
00115 template<
00116 class ObjectType,
00117 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
00118 class CreateFunctorType,
00119 class DeleteFunctorType,
00120 class ResetFunctorType>
00121 #else
00122 class CreateFunctorType = DefaultCacheCreateFunctor<ObjectType>,
00123 class DeleteFunctorType = DeleteFunctor<ObjectType>,
00124 class ResetFunctorType = DefaultCacheResetFunctor<ObjectType> >
00125 #endif
00126 class XalanObjectCache
00127 {
00128 public:
00129 
00130 #if defined(XALAN_NO_NAMESPACES)
00131     typedef vector<ObjectType*>         VectorType;
00132 #else
00133     typedef std::vector<ObjectType*>    VectorType;
00134 #endif
00135 
00136     typedef ObjectType  CacheObjectType;
00137 
00138     explicit
00139     XalanObjectCache(unsigned int   initialListSize = 0) :
00140         m_availableList(),
00141         m_busyList()
00142     {
00143         m_availableList.reserve(initialListSize);
00144 
00145         m_busyList.reserve(initialListSize);
00146     }
00147 
00148     ~XalanObjectCache()
00149     {
00150         reset();
00151 
00152 #if !defined(XALAN_NO_NAMESPACES)
00153         using std::for_each;
00154 #endif
00155 
00156         for_each(
00157                 m_availableList.begin(),
00158                 m_availableList.end(),
00159                 m_deleteFunctor);
00160     }
00161 
00162     ObjectType*
00163     get()
00164     {
00165         // We'll always return the back of the free list, since
00166         // that's the cheapest thing.
00167         if (m_availableList.empty() == true)
00168         {
00169             ObjectType* const   theNewObject = m_createFunctor();
00170 
00171             m_busyList.push_back(theNewObject);
00172 
00173             return theNewObject;
00174         }
00175         else
00176         {
00177             ObjectType* const   theObject = m_availableList.back();
00178 
00179             m_busyList.push_back(theObject);
00180 
00181             m_availableList.pop_back();
00182 
00183             return theObject;
00184         }
00185     }
00186 
00187     bool
00188     release(ObjectType*     theInstance)
00189     {
00190 #if !defined(XALAN_NO_NAMESPACES)
00191         using std::find;
00192 #endif
00193 
00194         typedef typename VectorType::iterator   IteratorType;
00195 
00196         const IteratorType  i =
00197             find(
00198                 m_busyList.begin(),
00199                 m_busyList.end(),
00200                 theInstance);
00201 
00202         if (i == m_busyList.end())
00203         {
00204             return false;
00205         }
00206         else
00207         {
00208             m_resetFunctor(theInstance);
00209 
00210             m_availableList.push_back(theInstance);
00211 
00212             m_busyList.erase(i);
00213 
00214             return true;
00215         }
00216     }
00217 
00218     void
00219     reset()
00220     {
00221         while (m_busyList.empty() == false)
00222         {
00223             ObjectType* const   theInstance = m_busyList.back();
00224 
00225             m_resetFunctor(theInstance);
00226 
00227             m_availableList.push_back(theInstance);
00228 
00229             m_busyList.pop_back();
00230         }
00231     }
00232 
00233     // Functors for various operations...
00234     CreateFunctorType   m_createFunctor;
00235 
00236     DeleteFunctorType   m_deleteFunctor;
00237 
00238     ResetFunctorType    m_resetFunctor;
00239 
00240 private:
00241 
00242     // There are not defined...
00243     XalanObjectCache(const XalanObjectCache<ObjectType, CreateFunctorType, DeleteFunctorType, ResetFunctorType>&    theRHS);
00244 
00245     XalanObjectCache<ObjectType, CreateFunctorType, DeleteFunctorType, ResetFunctorType>&
00246     operator=(const XalanObjectCache<ObjectType, CreateFunctorType, DeleteFunctorType, ResetFunctorType>&   theRHS);
00247 
00248 
00249     // Data members...
00250     VectorType          m_availableList;
00251 
00252     VectorType          m_busyList;
00253 };
00254 
00255 
00256 
00257 #else
00258 
00259 
00260 
00261 template<
00262 class ObjectType,
00263 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
00264 class CreateFunctorType,
00265 class DeleteFunctorType,
00266 class ResetFunctorType>
00267 #else
00268 class CreateFunctorType = DefaultCacheCreateFunctor<ObjectType>,
00269 class DeleteFunctorType = DeleteFunctor<ObjectType>,
00270 class ResetFunctorType = DefaultCacheResetFunctor<ObjectType> >
00271 #endif
00272 class XalanObjectCache
00273 {
00274 public:
00275 
00276 #if defined(XALAN_NO_NAMESPACES)
00277     typedef vector<ObjectType*>         VectorType;
00278 #else
00279     typedef std::vector<ObjectType*>    VectorType;
00280 #endif
00281 
00282     typedef ObjectType  CacheObjectType;
00283 
00284     explicit
00285     XalanObjectCache(unsigned int   initialListSize = 0) :
00286         m_availableList()
00287     {
00288         m_availableList.reserve(initialListSize);
00289     }
00290 
00291     ~XalanObjectCache()
00292     {
00293         reset();
00294 
00295 #if !defined(XALAN_NO_NAMESPACES)
00296         using std::for_each;
00297 #endif
00298 
00299         for_each(
00300                 m_availableList.begin(),
00301                 m_availableList.end(),
00302                 m_deleteFunctor);
00303     }
00304 
00305     ObjectType*
00306     get()
00307     {
00308         // We'll always return the back of the free list, since
00309         // that's the cheapest thing.
00310         if (m_availableList.empty() == true)
00311         {
00312             return m_createFunctor();
00313         }
00314         else
00315         {
00316             ObjectType* const   theObject = m_availableList.back();
00317 
00318             m_availableList.pop_back();
00319 
00320             return theObject;
00321         }
00322     }
00323 
00324     bool
00325     release(ObjectType*     theInstance)
00326     {
00327         m_resetFunctor(theInstance);
00328 
00329         m_availableList.push_back(theInstance);
00330 
00331         return true;
00332     }
00333 
00334     void
00335     reset()
00336     {
00337     }
00338 
00339     // Functors for various operations...
00340     CreateFunctorType   m_createFunctor;
00341 
00342     DeleteFunctorType   m_deleteFunctor;
00343 
00344     ResetFunctorType    m_resetFunctor;
00345 
00346 private:
00347 
00348     // These are not defined...
00349     XalanObjectCache(const XalanObjectCache<ObjectType, CreateFunctorType, DeleteFunctorType, ResetFunctorType>&    theRHS);
00350 
00351     XalanObjectCache<ObjectType, CreateFunctorType, DeleteFunctorType, ResetFunctorType>&
00352     operator=(const XalanObjectCache<ObjectType, CreateFunctorType, DeleteFunctorType, ResetFunctorType>&   theRHS);
00353 
00354 
00355     // Data members...
00356     VectorType          m_availableList;
00357 };
00358 
00359 
00360 
00361 #endif
00362 
00363 
00364 
00365 template<class XalanObjectCacheType>
00366 class GuardCachedObject
00367 {
00368 public:
00369 
00370     typedef typename XalanObjectCacheType::CacheObjectType  CacheObjectType;
00371 
00372     GuardCachedObject(XalanObjectCacheType& theCache) :
00373         m_cache(theCache),
00374         m_cachedObject(theCache.get())
00375     {
00376     }
00377 
00378     ~GuardCachedObject()
00379     {
00380         if (m_cachedObject != 0)
00381         {
00382             m_cache.release(m_cachedObject);
00383         }
00384     }
00385 
00386     CacheObjectType*
00387     get() const
00388     {
00389         return m_cachedObject;
00390     }
00391 
00392     CacheObjectType*
00393     release()
00394     {
00395         CacheObjectType* const  temp = m_cachedObject;
00396 
00397         m_cachedObject = 0;
00398 
00399         return temp;
00400     }
00401 
00402 private:
00403 
00404     // Not implemented...
00405     GuardCachedObject(const GuardCachedObject<XalanObjectCacheType>&);
00406 
00407 
00408     // Data members...
00409     XalanObjectCacheType&   m_cache;
00410 
00411     CacheObjectType*        m_cachedObject;
00412 };
00413 
00414 
00415 
00416 template<class ObjectType>
00417 class XalanObjectCacheDefault : public XalanObjectCache<ObjectType, DefaultCacheCreateFunctor<ObjectType>, DeleteFunctor<ObjectType>, DefaultCacheResetFunctor<ObjectType> >
00418 {
00419 public:
00420 
00421     typedef XalanObjectCache<ObjectType, DefaultCacheCreateFunctor<ObjectType>, DeleteFunctor<ObjectType>, DefaultCacheResetFunctor<ObjectType> >       BaseClassType;
00422 
00423     explicit
00424     XalanObjectCacheDefault(unsigned int    initialListSize = 0) :
00425         BaseClassType(initialListSize)
00426     {
00427     }
00428 };
00429 
00430 
00431 
00432 #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.4
Copyright © 2000, 2001, 2002 The Apache Software Foundation. All Rights Reserved.