source: NonGTP/Xerces/xerces/include/xercesc/util/Janitor.c @ 358

Revision 358, 5.5 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: Janitor.c,v $
19 * Revision 1.7  2004/09/14 19:47:30  amassari
20 * The signature for implementation for ArrayJanitor::reset was not aligned with the declaration (jira#1261)
21 *
22 * Revision 1.6  2004/09/08 13:56:22  peiyongz
23 * Apache License Version 2.0
24 *
25 * Revision 1.5  2003/11/06 19:28:11  knoaman
26 * PSVI support for annotations.
27 *
28 * Revision 1.4  2003/05/21 03:34:52  jberry
29 * Cast away CodeWarrior complaint of casting away const if we're holding a const obj
30 *
31 * Revision 1.3  2003/05/15 19:04:35  knoaman
32 * Partial implementation of the configurable memory manager.
33 *
34 * Revision 1.2  2002/11/04 15:22:04  tng
35 * C++ Namespace Support.
36 *
37 * Revision 1.1.1.1  2002/02/01 22:22:10  peiyongz
38 * sane_include
39 *
40 * Revision 1.6  2000/10/13 22:45:11  andyh
41 * Complete removal of ArrayJanitory::operator->().  Was just commented out earlier.
42 *
43 * Revision 1.5  2000/10/10 23:52:10  andyh
44 * From Janitor, remove the addition that is having compile problems in MSVC.
45 *
46 * Revision 1.4  2000/10/09 18:32:31  jberry
47 * Add some auto_ptr functionality to allow modification of monitored
48 * pointer value. This eases use of Janitor in some situations.
49 *
50 * Revision 1.3  2000/03/02 19:54:40  roddey
51 * This checkin includes many changes done while waiting for the
52 * 1.1.0 code to be finished. I can't list them all here, but a list is
53 * available elsewhere.
54 *
55 * Revision 1.2  2000/02/06 07:48:02  rahulj
56 * Year 2K copyright swat.
57 *
58 * Revision 1.1.1.1  1999/11/09 01:04:26  twl
59 * Initial checkin
60 *
61 * Revision 1.2  1999/11/08 20:45:08  rahul
62 * Swat for adding in Product name and CVS comment log variable.
63 *
64 */
65
66
67// ---------------------------------------------------------------------------
68//  Includes
69// ---------------------------------------------------------------------------
70#if defined(XERCES_TMPLSINC)
71#include <xercesc/util/Janitor.hpp>
72#endif
73
74XERCES_CPP_NAMESPACE_BEGIN
75
76// ---------------------------------------------------------------------------
77//  Janitor: Constructors and Destructor
78// ---------------------------------------------------------------------------
79template <class T> Janitor<T>::Janitor(T* const toDelete) :
80    fData(toDelete)
81{
82}
83
84
85template <class T> Janitor<T>::~Janitor()
86{
87    reset();
88}
89
90
91// ---------------------------------------------------------------------------
92//  Janitor: Public, non-virtual methods
93// ---------------------------------------------------------------------------
94template <class T> void
95Janitor<T>::orphan()
96{
97   release();
98}
99
100
101template <class T> T&
102Janitor<T>::operator*() const
103{
104        return *fData;
105}
106
107
108template <class T> T*
109Janitor<T>::operator->() const
110{
111        return fData;
112}
113
114
115template <class T> T*
116Janitor<T>::get() const
117{
118        return fData;
119}
120
121
122template <class T> T*
123Janitor<T>::release()
124{
125        T* p = fData;
126        fData = 0;
127        return p;
128}
129
130
131template <class T> void Janitor<T>::reset(T* p)
132{
133    if (fData)
134        delete fData;
135
136    fData = p;
137}
138
139template <class T> bool Janitor<T>::isDataNull()
140{
141    return (fData == 0);
142}
143
144
145// -----------------------------------------------------------------------
146//  ArrayJanitor: Constructors and Destructor
147// -----------------------------------------------------------------------
148template <class T> ArrayJanitor<T>::ArrayJanitor(T* const toDelete) :
149    fData(toDelete)
150    , fMemoryManager(0)
151{
152}
153
154template <class T>
155ArrayJanitor<T>::ArrayJanitor(T* const toDelete,
156                              MemoryManager* const manager) :
157    fData(toDelete)
158    , fMemoryManager(manager)
159{
160}
161
162
163template <class T> ArrayJanitor<T>::~ArrayJanitor()
164{
165        reset();
166}
167
168
169// -----------------------------------------------------------------------
170//  ArrayJanitor: Public, non-virtual methods
171// -----------------------------------------------------------------------
172template <class T> void
173ArrayJanitor<T>::orphan()
174{
175   release();
176}
177
178
179//      Look, Ma! No hands! Don't call this with null data!
180template <class T> T&
181ArrayJanitor<T>::operator[](int index) const
182{
183        //      TODO: Add appropriate exception
184        return fData[index];
185}
186
187
188template <class T> T*
189ArrayJanitor<T>::get() const
190{
191        return fData;
192}
193
194
195template <class T> T*
196ArrayJanitor<T>::release()
197{
198        T* p = fData;
199        fData = 0;
200        return p;
201}
202
203
204template <class T> void
205ArrayJanitor<T>::reset(T* p)
206{
207        if (fData) {
208
209                if (fMemoryManager)
210            fMemoryManager->deallocate((void*)fData);
211        else
212            delete [] fData;
213    }
214
215        fData = p;
216    fMemoryManager = 0;
217}
218
219template <class T> void
220ArrayJanitor<T>::reset(T* p, MemoryManager* const manager)
221{
222        if (fData) {
223
224                if (fMemoryManager)
225            fMemoryManager->deallocate((void*)fData);
226        else
227            delete [] fData;
228    }
229
230        fData = p;
231    fMemoryManager = manager;
232}
233
234XERCES_CPP_NAMESPACE_END
235
Note: See TracBrowser for help on using the repository browser.