source: icXML/icXML-devel/src/xercesc/framework/XMLBufferMgr.hpp @ 2722

Last change on this file since 2722 was 2722, checked in by cameron, 6 years ago

Original Xerces files with import mods for icxercesc

File size: 6.1 KB
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/*
19 * $Id: XMLBufferMgr.hpp 673679 2008-07-03 13:50:10Z borisk $
20 */
21
22#if !defined(XERCESC_INCLUDE_GUARD_XMLBUFFERMGR_HPP)
23#define XERCESC_INCLUDE_GUARD_XMLBUFFERMGR_HPP
24
25#include <icxercesc/framework/XMLBuffer.hpp>
26
27XERCES_CPP_NAMESPACE_BEGIN
28
29class XMLBufBid;
30
31/**
32 *  There are many places where XMLBuffer objects are needed. In order to
33 *  avoid either constantly creating and destroying them or maintaining a
34 *  fixed set and worrying about accidental reuse, a buffer manager can
35 *  provide a pool of buffers which can be temporarily used and then put
36 *  back into the pool. This provides a good compromise between performance
37 *  and easier maintenance.
38 */
39class XMLPARSER_EXPORT XMLBufferMgr : public XMemory
40{
41public :
42    // -----------------------------------------------------------------------
43    //  Constructors and Destructor
44    // -----------------------------------------------------------------------
45
46    /** @name Constructor */
47    //@{
48    XMLBufferMgr(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
49    //@}
50
51    /** @name Destructor */
52    //@{
53    ~XMLBufferMgr();
54    //@}
55
56
57    // -----------------------------------------------------------------------
58    //  Buffer management
59    // -----------------------------------------------------------------------
60    XMLBuffer& bidOnBuffer();
61    void releaseBuffer(XMLBuffer& toRelease);
62
63    // -----------------------------------------------------------------------
64    //  Getter methods
65    // -----------------------------------------------------------------------
66    XMLSize_t getBufferCount() const;
67    XMLSize_t getAvailableBufferCount() const;
68
69private :
70    // -----------------------------------------------------------------------
71    //  Unimplemented constructors and operators
72    // -----------------------------------------------------------------------
73    XMLBufferMgr(const XMLBufferMgr&);
74    XMLBufferMgr& operator=(const XMLBufferMgr&);
75
76    // -----------------------------------------------------------------------
77    //  Private data members
78    //
79    //  fBufCount
80    //      The count of buffers that have been allocated so far.
81    //
82    //  fBufList;
83    //      The list of pointers to buffers that are loaned out. There will
84    //      never be a lot of them, so a flat list is good enough.
85    // -----------------------------------------------------------------------
86    XMLSize_t       fBufCount;
87    MemoryManager*  fMemoryManager;
88    XMLBuffer**     fBufList;
89};
90
91inline XMLSize_t XMLBufferMgr::getBufferCount() const
92{
93    return fBufCount;
94}
95
96inline XMLSize_t XMLBufferMgr::getAvailableBufferCount() const
97{
98    XMLSize_t available = fBufCount;
99    for (XMLSize_t index = 0; index < fBufCount && fBufList[index]; index++)
100    {
101        if (fBufList[index]->getInUse())
102            --available;
103    }
104    return available;
105}
106
107
108/**
109 *  XMLBufBid is a scoped based janitor that allows the scanner code to ask
110 *  for a buffer on a scoped basis and then insure that it gets freed back
111 *  into the pool no matter how the scope is exited (exception or normal exit.)
112 */
113class XMLBufBid : public XMemory
114{
115public :
116    // -----------------------------------------------------------------------
117    //  Constructors and Destructor
118    // -----------------------------------------------------------------------
119    XMLBufBid(XMLBufferMgr* const srcMgr) :
120
121        fBuffer(srcMgr->bidOnBuffer())
122        , fMgr(srcMgr)
123    {
124    }
125
126    ~XMLBufBid()
127    {
128        fMgr->releaseBuffer(fBuffer);
129    }
130
131
132
133    // -----------------------------------------------------------------------
134    //  Buffer access
135    // -----------------------------------------------------------------------
136    void append(const XMLCh toAppend)
137    {
138        fBuffer.append(toAppend);
139    }
140
141    void append(const XMLCh* const toAppend, const XMLSize_t count = 0)
142    {
143        fBuffer.append(toAppend, count);
144    }
145
146    const XMLBuffer& getBuffer() const
147    {
148        return fBuffer;
149    }
150
151    XMLBuffer& getBuffer()
152    {
153        return fBuffer;
154    }
155
156    const XMLCh* getRawBuffer() const
157    {
158        fBuffer.fBuffer[fBuffer.fIndex] = 0;
159        return fBuffer.fBuffer;
160    }
161
162    XMLCh* getRawBuffer()
163    {
164        fBuffer.fBuffer[fBuffer.fIndex] = 0;
165        return fBuffer.fBuffer;
166    }
167
168    XMLSize_t getLen() const
169    {
170        return fBuffer.fIndex;
171    }
172
173    bool isEmpty() const
174    {
175        return (fBuffer.fIndex == 0);
176    }
177
178    void reset()
179    {
180        fBuffer.reset();
181    }
182
183    void set(const XMLCh* const chars, const XMLSize_t count = 0)
184    {
185        fBuffer.set(chars, count);
186    }
187
188
189private :
190    // -----------------------------------------------------------------------
191    //  Unimplemented constructors and operators
192    // -----------------------------------------------------------------------
193    XMLBufBid(const XMLBufBid&);
194    XMLBufBid& operator=(const XMLBufBid&);
195
196    // -----------------------------------------------------------------------
197    //  Private data members
198    //
199    //  fBuffer
200    //      This is the buffer we got, and which we will release.
201    //
202    //  fMgr
203    //      This is the buffer manager we got the buffer from. This is needed
204    //      to release the buffer later.
205    // -----------------------------------------------------------------------
206    XMLBuffer&          fBuffer;
207    XMLBufferMgr* const fMgr;
208};
209
210XERCES_CPP_NAMESPACE_END
211
212#endif
Note: See TracBrowser for help on using the repository browser.