source: NonGTP/Xerces/xerces/include/xercesc/framework/XMLBufferMgr.hpp @ 358

Revision 358, 6.9 KB checked in by bittner, 19 years ago (diff)

xerces added

Line 
1/*
2 * Copyright 1999-2000,2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * $Log: XMLBufferMgr.hpp,v $
19 * Revision 1.8  2004/09/08 13:55:58  peiyongz
20 * Apache License Version 2.0
21 *
22 * Revision 1.7  2004/01/29 11:46:29  cargilld
23 * Code cleanup changes to get rid of various compiler diagnostic messages.
24 *
25 * Revision 1.6  2003/05/16 21:36:55  knoaman
26 * Memory manager implementation: Modify constructors to pass in the memory manager.
27 *
28 * Revision 1.5  2003/05/15 18:26:07  knoaman
29 * Partial implementation of the configurable memory manager.
30 *
31 * Revision 1.4  2002/12/04 02:32:43  knoaman
32 * #include cleanup.
33 *
34 * Revision 1.3  2002/11/04 15:00:21  tng
35 * C++ Namespace Support.
36 *
37 * Revision 1.2  2002/08/21 18:54:52  tng
38 * [Bug 11869] Add the const modifier (XMLBuffer.hpp).
39 *
40 * Revision 1.1.1.1  2002/02/01 22:21:51  peiyongz
41 * sane_include
42 *
43 * Revision 1.5  2000/03/02 19:54:24  roddey
44 * This checkin includes many changes done while waiting for the
45 * 1.1.0 code to be finished. I can't list them all here, but a list is
46 * available elsewhere.
47 *
48 * Revision 1.4  2000/02/24 20:00:23  abagchi
49 * Swat for removing Log from API docs
50 *
51 * Revision 1.3  2000/02/15 01:21:30  roddey
52 * Some initial documentation improvements. More to come...
53 *
54 * Revision 1.2  2000/02/06 07:47:47  rahulj
55 * Year 2K copyright swat.
56 *
57 * Revision 1.1.1.1  1999/11/09 01:08:30  twl
58 * Initial checkin
59 *
60 * Revision 1.2  1999/11/08 20:44:36  rahul
61 * Swat for adding in Product name and CVS comment log variable.
62 *
63 */
64
65
66#if !defined(XMLBUFFERMGR_HPP)
67#define XMLBUFFERMGR_HPP
68
69#include <xercesc/framework/XMLBuffer.hpp>
70
71XERCES_CPP_NAMESPACE_BEGIN
72
73class XMLBufBid;
74
75/**
76 *  There are many places where XMLBuffer objects are needed. In order to
77 *  avoid either constantly creating and destroying them or maintaining a
78 *  fixed set and worrying about accidental reuse, a buffer manager can
79 *  provide a pool of buffers which can be temporarily used and then put
80 *  back into the pool. This provides a good compromise between performance
81 *  and easier maintenance.
82 */
83class XMLPARSER_EXPORT XMLBufferMgr : public XMemory
84{
85public :
86    // -----------------------------------------------------------------------
87    //  Constructors and Destructor
88    // -----------------------------------------------------------------------
89
90    /** @name Constructor */
91    //@{
92    XMLBufferMgr(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
93    //@}
94
95    /** @name Destructor */
96    //@{
97    ~XMLBufferMgr();
98    //@}
99
100
101    // -----------------------------------------------------------------------
102    //  Buffer management
103    // -----------------------------------------------------------------------
104    XMLBuffer& bidOnBuffer();
105    void releaseBuffer(XMLBuffer& toRelease);
106
107
108private :
109    // -----------------------------------------------------------------------
110    //  Unimplemented constructors and operators
111    // -----------------------------------------------------------------------
112    XMLBufferMgr(const XMLBufferMgr&);
113    XMLBufferMgr& operator=(const XMLBufferMgr&);
114
115    // -----------------------------------------------------------------------
116    //  Private data members
117    //
118    //  fBufCount
119    //      The count of buffers that have been allocated so far.
120    //
121    //  fBufList;
122    //      The list of pointers to buffers that are loaned out. There will
123    //      never be a lot of them, so a flat list is good enough.
124    // -----------------------------------------------------------------------
125    unsigned int    fBufCount;
126    MemoryManager*  fMemoryManager;
127    XMLBuffer**     fBufList;
128};
129
130
131/**
132 *  XMLBufBid is a scoped based janitor that allows the scanner code to ask
133 *  for a buffer on a scoped basis and then insure that it gets freed back
134 *  into the pool no matter how the scope is exited (exception or normal exit.)
135 */
136class XMLBufBid : public XMemory
137{
138public :
139    // -----------------------------------------------------------------------
140    //  Constructors and Destructor
141    // -----------------------------------------------------------------------
142    XMLBufBid(XMLBufferMgr* const srcMgr) :
143
144        fBuffer(srcMgr->bidOnBuffer())
145        , fMgr(srcMgr)
146    {
147    }
148
149    ~XMLBufBid()
150    {
151        fMgr->releaseBuffer(fBuffer);
152    }
153
154
155
156    // -----------------------------------------------------------------------
157    //  Buffer access
158    // -----------------------------------------------------------------------
159    void append(const XMLCh toAppend)
160    {
161        fBuffer.append(toAppend);
162    }
163
164    void append(const XMLCh* const toAppend, const unsigned int count = 0)
165    {
166        fBuffer.append(toAppend, count);
167    }
168
169    const XMLBuffer& getBuffer() const
170    {
171        return fBuffer;
172    }
173
174    XMLBuffer& getBuffer()
175    {
176        return fBuffer;
177    }
178
179    const XMLCh* getRawBuffer() const
180    {
181        fBuffer.fBuffer[fBuffer.fIndex] = 0;
182        return fBuffer.fBuffer;
183    }
184
185    XMLCh* getRawBuffer()
186    {
187        fBuffer.fBuffer[fBuffer.fIndex] = 0;
188        return fBuffer.fBuffer;
189    }
190
191    unsigned int getLen() const
192    {
193        return fBuffer.fIndex;
194    }
195
196    bool isEmpty() const
197    {
198        return (fBuffer.fIndex == 0);
199    }
200
201    void reset()
202    {
203        fBuffer.reset();
204    }
205
206    void set(const XMLCh* const chars, const unsigned int count = 0)
207    {
208        fBuffer.set(chars, count);
209    }
210
211
212private :
213    // -----------------------------------------------------------------------
214    //  Unimplemented constructors and operators
215    // -----------------------------------------------------------------------
216    XMLBufBid(const XMLBufBid&);
217    XMLBufBid& operator=(const XMLBufBid&);
218
219    // -----------------------------------------------------------------------
220    //  Private data members
221    //
222    //  fBuffer
223    //      This is the buffer we got, and which we will release.
224    //
225    //  fMgr
226    //      This is the buffer manager we got the buffer from. This is needed
227    //      to release the buffer later.
228    // -----------------------------------------------------------------------
229    XMLBuffer&          fBuffer;
230    XMLBufferMgr* const fMgr;
231};
232
233XERCES_CPP_NAMESPACE_END
234
235#endif
Note: See TracBrowser for help on using the repository browser.