Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:27:05

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *      http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 /*
0019  * $Id$
0020  */
0021 
0022 #if !defined(XERCESC_INCLUDE_GUARD_XMLBUFFERMGR_HPP)
0023 #define XERCESC_INCLUDE_GUARD_XMLBUFFERMGR_HPP
0024 
0025 #include <xercesc/framework/XMLBuffer.hpp>
0026 
0027 XERCES_CPP_NAMESPACE_BEGIN
0028 
0029 class XMLBufBid;
0030 
0031 /**
0032  *  There are many places where XMLBuffer objects are needed. In order to
0033  *  avoid either constantly creating and destroying them or maintaining a
0034  *  fixed set and worrying about accidental reuse, a buffer manager can
0035  *  provide a pool of buffers which can be temporarily used and then put
0036  *  back into the pool. This provides a good compromise between performance
0037  *  and easier maintenance.
0038  */
0039 class XMLPARSER_EXPORT XMLBufferMgr : public XMemory
0040 {
0041 public :
0042     // -----------------------------------------------------------------------
0043     //  Constructors and Destructor
0044     // -----------------------------------------------------------------------
0045 
0046     /** @name Constructor */
0047     //@{
0048     XMLBufferMgr(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
0049     //@}
0050 
0051     /** @name Destructor */
0052     //@{
0053     ~XMLBufferMgr();
0054     //@}
0055 
0056 
0057     // -----------------------------------------------------------------------
0058     //  Buffer management
0059     // -----------------------------------------------------------------------
0060     XMLBuffer& bidOnBuffer();
0061     void releaseBuffer(XMLBuffer& toRelease);
0062 
0063     // -----------------------------------------------------------------------
0064     //  Getter methods
0065     // -----------------------------------------------------------------------
0066     XMLSize_t getBufferCount() const;
0067     XMLSize_t getAvailableBufferCount() const;
0068 
0069 private :
0070     // -----------------------------------------------------------------------
0071     //  Unimplemented constructors and operators
0072     // -----------------------------------------------------------------------
0073     XMLBufferMgr(const XMLBufferMgr&);
0074     XMLBufferMgr& operator=(const XMLBufferMgr&);
0075 
0076     // -----------------------------------------------------------------------
0077     //  Private data members
0078     //
0079     //  fBufCount
0080     //      The count of buffers that have been allocated so far.
0081     //
0082     //  fBufList;
0083     //      The list of pointers to buffers that are loaned out. There will
0084     //      never be a lot of them, so a flat list is good enough.
0085     // -----------------------------------------------------------------------
0086     XMLSize_t       fBufCount;
0087     MemoryManager*  fMemoryManager;
0088     XMLBuffer**     fBufList;
0089 };
0090 
0091 inline XMLSize_t XMLBufferMgr::getBufferCount() const
0092 {
0093     return fBufCount;
0094 }
0095 
0096 inline XMLSize_t XMLBufferMgr::getAvailableBufferCount() const
0097 {
0098     XMLSize_t available = fBufCount;
0099     for (XMLSize_t index = 0; index < fBufCount && fBufList[index]; index++)
0100     {
0101         if (fBufList[index]->getInUse())
0102             --available;
0103     }
0104     return available;
0105 }
0106 
0107 
0108 /**
0109  *  XMLBufBid is a scoped based janitor that allows the scanner code to ask
0110  *  for a buffer on a scoped basis and then insure that it gets freed back
0111  *  into the pool no matter how the scope is exited (exception or normal exit.)
0112  */
0113 class XMLBufBid : public XMemory
0114 {
0115 public :
0116     // -----------------------------------------------------------------------
0117     //  Constructors and Destructor
0118     // -----------------------------------------------------------------------
0119     XMLBufBid(XMLBufferMgr* const srcMgr) :
0120 
0121         fBuffer(srcMgr->bidOnBuffer())
0122         , fMgr(srcMgr)
0123     {
0124     }
0125 
0126     ~XMLBufBid()
0127     {
0128         fMgr->releaseBuffer(fBuffer);
0129     }
0130 
0131 
0132 
0133     // -----------------------------------------------------------------------
0134     //  Buffer access
0135     // -----------------------------------------------------------------------
0136     void append(const XMLCh toAppend)
0137     {
0138         fBuffer.append(toAppend);
0139     }
0140 
0141     void append(const XMLCh* const toAppend, const XMLSize_t count = 0)
0142     {
0143         fBuffer.append(toAppend, count);
0144     }
0145 
0146     const XMLBuffer& getBuffer() const
0147     {
0148         return fBuffer;
0149     }
0150 
0151     XMLBuffer& getBuffer()
0152     {
0153         return fBuffer;
0154     }
0155 
0156     const XMLCh* getRawBuffer() const
0157     {
0158         fBuffer.fBuffer[fBuffer.fIndex] = 0;
0159         return fBuffer.fBuffer;
0160     }
0161 
0162     XMLCh* getRawBuffer()
0163     {
0164         fBuffer.fBuffer[fBuffer.fIndex] = 0;
0165         return fBuffer.fBuffer;
0166     }
0167 
0168     XMLSize_t getLen() const
0169     {
0170         return fBuffer.fIndex;
0171     }
0172 
0173     bool isEmpty() const
0174     {
0175         return (fBuffer.fIndex == 0);
0176     }
0177 
0178     void reset()
0179     {
0180         fBuffer.reset();
0181     }
0182 
0183     void set(const XMLCh* const chars, const XMLSize_t count = 0)
0184     {
0185         fBuffer.set(chars, count);
0186     }
0187 
0188 
0189 private :
0190     // -----------------------------------------------------------------------
0191     //  Unimplemented constructors and operators
0192     // -----------------------------------------------------------------------
0193     XMLBufBid(const XMLBufBid&);
0194     XMLBufBid& operator=(const XMLBufBid&);
0195 
0196     // -----------------------------------------------------------------------
0197     //  Private data members
0198     //
0199     //  fBuffer
0200     //      This is the buffer we got, and which we will release.
0201     //
0202     //  fMgr
0203     //      This is the buffer manager we got the buffer from. This is needed
0204     //      to release the buffer later.
0205     // -----------------------------------------------------------------------
0206     XMLBuffer&          fBuffer;
0207     XMLBufferMgr* const fMgr;
0208 };
0209 
0210 XERCES_CPP_NAMESPACE_END
0211 
0212 #endif