source: NonGTP/Xerces/xerces-c_2_8_0/include/xercesc/framework/XMLBufferMgr.hpp @ 2674

Revision 2674, 6.0 KB checked in by mattausch, 16 years ago (diff)
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#if !defined(XMLBUFFERMGR_HPP)
19#define XMLBUFFERMGR_HPP
20
21#include <xercesc/framework/XMLBuffer.hpp>
22
23XERCES_CPP_NAMESPACE_BEGIN
24
25class XMLBufBid;
26
27/**
28 *  There are many places where XMLBuffer objects are needed. In order to
29 *  avoid either constantly creating and destroying them or maintaining a
30 *  fixed set and worrying about accidental reuse, a buffer manager can
31 *  provide a pool of buffers which can be temporarily used and then put
32 *  back into the pool. This provides a good compromise between performance
33 *  and easier maintenance.
34 */
35class XMLPARSER_EXPORT XMLBufferMgr : public XMemory
36{
37public :
38    // -----------------------------------------------------------------------
39    //  Constructors and Destructor
40    // -----------------------------------------------------------------------
41
42    /** @name Constructor */
43    //@{
44    XMLBufferMgr(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
45    //@}
46
47    /** @name Destructor */
48    //@{
49    ~XMLBufferMgr();
50    //@}
51
52
53    // -----------------------------------------------------------------------
54    //  Buffer management
55    // -----------------------------------------------------------------------
56    XMLBuffer& bidOnBuffer();
57    void releaseBuffer(XMLBuffer& toRelease);
58
59        // -----------------------------------------------------------------------
60    //  Getter methods
61    // -----------------------------------------------------------------------
62    unsigned int getBufferCount() const;
63    unsigned int getAvailableBufferCount() const;
64
65private :
66    // -----------------------------------------------------------------------
67    //  Unimplemented constructors and operators
68    // -----------------------------------------------------------------------
69    XMLBufferMgr(const XMLBufferMgr&);
70    XMLBufferMgr& operator=(const XMLBufferMgr&);
71
72    // -----------------------------------------------------------------------
73    //  Private data members
74    //
75    //  fBufCount
76    //      The count of buffers that have been allocated so far.
77    //
78    //  fBufList;
79    //      The list of pointers to buffers that are loaned out. There will
80    //      never be a lot of them, so a flat list is good enough.
81    // -----------------------------------------------------------------------
82    unsigned int    fBufCount;
83    MemoryManager*  fMemoryManager;
84    XMLBuffer**     fBufList;
85};
86
87inline unsigned int XMLBufferMgr::getBufferCount() const
88{
89    return fBufCount;
90}
91
92inline unsigned int XMLBufferMgr::getAvailableBufferCount() const
93{
94    unsigned available = fBufCount;
95    for (unsigned int index = 0; index < fBufCount && fBufList[index]; index++)
96    {
97        if (fBufList[index]->getInUse())
98            --available;
99    }
100    return available;
101}
102
103
104/**
105 *  XMLBufBid is a scoped based janitor that allows the scanner code to ask
106 *  for a buffer on a scoped basis and then insure that it gets freed back
107 *  into the pool no matter how the scope is exited (exception or normal exit.)
108 */
109class XMLBufBid : public XMemory
110{
111public :
112    // -----------------------------------------------------------------------
113    //  Constructors and Destructor
114    // -----------------------------------------------------------------------
115    XMLBufBid(XMLBufferMgr* const srcMgr) :
116
117        fBuffer(srcMgr->bidOnBuffer())
118        , fMgr(srcMgr)
119    {
120    }
121
122    ~XMLBufBid()
123    {
124        fMgr->releaseBuffer(fBuffer);
125    }
126
127
128
129    // -----------------------------------------------------------------------
130    //  Buffer access
131    // -----------------------------------------------------------------------
132    void append(const XMLCh toAppend)
133    {
134        fBuffer.append(toAppend);
135    }
136
137    void append(const XMLCh* const toAppend, const unsigned int count = 0)
138    {
139        fBuffer.append(toAppend, count);
140    }
141
142    const XMLBuffer& getBuffer() const
143    {
144        return fBuffer;
145    }
146
147    XMLBuffer& getBuffer()
148    {
149        return fBuffer;
150    }
151
152    const XMLCh* getRawBuffer() const
153    {
154        fBuffer.fBuffer[fBuffer.fIndex] = 0;
155        return fBuffer.fBuffer;
156    }
157
158    XMLCh* getRawBuffer()
159    {
160        fBuffer.fBuffer[fBuffer.fIndex] = 0;
161        return fBuffer.fBuffer;
162    }
163
164    unsigned int getLen() const
165    {
166        return fBuffer.fIndex;
167    }
168
169    bool isEmpty() const
170    {
171        return (fBuffer.fIndex == 0);
172    }
173
174    void reset()
175    {
176        fBuffer.reset();
177    }
178
179    void set(const XMLCh* const chars, const unsigned int count = 0)
180    {
181        fBuffer.set(chars, count);
182    }
183
184
185private :
186    // -----------------------------------------------------------------------
187    //  Unimplemented constructors and operators
188    // -----------------------------------------------------------------------
189    XMLBufBid(const XMLBufBid&);
190    XMLBufBid& operator=(const XMLBufBid&);
191
192    // -----------------------------------------------------------------------
193    //  Private data members
194    //
195    //  fBuffer
196    //      This is the buffer we got, and which we will release.
197    //
198    //  fMgr
199    //      This is the buffer manager we got the buffer from. This is needed
200    //      to release the buffer later.
201    // -----------------------------------------------------------------------
202    XMLBuffer&          fBuffer;
203    XMLBufferMgr* const fMgr;
204};
205
206XERCES_CPP_NAMESPACE_END
207
208#endif
Note: See TracBrowser for help on using the repository browser.