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: Janitor.c 669844 2008-06-20 10:11:44Z borisk $ |
---|
20 | */ |
---|
21 | |
---|
22 | |
---|
23 | // --------------------------------------------------------------------------- |
---|
24 | // Includes |
---|
25 | // --------------------------------------------------------------------------- |
---|
26 | #if defined(XERCES_TMPLSINC) |
---|
27 | #include <xercesc/util/Janitor.hpp> |
---|
28 | #endif |
---|
29 | |
---|
30 | XERCES_CPP_NAMESPACE_BEGIN |
---|
31 | |
---|
32 | // --------------------------------------------------------------------------- |
---|
33 | // Janitor: Constructors and Destructor |
---|
34 | // --------------------------------------------------------------------------- |
---|
35 | template <class T> Janitor<T>::Janitor(T* const toDelete) : |
---|
36 | fData(toDelete) |
---|
37 | { |
---|
38 | } |
---|
39 | |
---|
40 | |
---|
41 | template <class T> Janitor<T>::~Janitor() |
---|
42 | { |
---|
43 | reset(); |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | // --------------------------------------------------------------------------- |
---|
48 | // Janitor: Public, non-virtual methods |
---|
49 | // --------------------------------------------------------------------------- |
---|
50 | template <class T> void |
---|
51 | Janitor<T>::orphan() |
---|
52 | { |
---|
53 | release(); |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | template <class T> T& |
---|
58 | Janitor<T>::operator*() const |
---|
59 | { |
---|
60 | return *fData; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | template <class T> T* |
---|
65 | Janitor<T>::operator->() const |
---|
66 | { |
---|
67 | return fData; |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | template <class T> T* |
---|
72 | Janitor<T>::get() const |
---|
73 | { |
---|
74 | return fData; |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | template <class T> T* |
---|
79 | Janitor<T>::release() |
---|
80 | { |
---|
81 | T* p = fData; |
---|
82 | fData = 0; |
---|
83 | return p; |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | template <class T> void Janitor<T>::reset(T* p) |
---|
88 | { |
---|
89 | if (fData) |
---|
90 | delete fData; |
---|
91 | |
---|
92 | fData = p; |
---|
93 | } |
---|
94 | |
---|
95 | template <class T> bool Janitor<T>::isDataNull() |
---|
96 | { |
---|
97 | return (fData == 0); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | // ----------------------------------------------------------------------- |
---|
102 | // ArrayJanitor: Constructors and Destructor |
---|
103 | // ----------------------------------------------------------------------- |
---|
104 | template <class T> ArrayJanitor<T>::ArrayJanitor(T* const toDelete) : |
---|
105 | fData(toDelete) |
---|
106 | , fMemoryManager(0) |
---|
107 | { |
---|
108 | } |
---|
109 | |
---|
110 | template <class T> |
---|
111 | ArrayJanitor<T>::ArrayJanitor(T* const toDelete, |
---|
112 | MemoryManager* const manager) : |
---|
113 | fData(toDelete) |
---|
114 | , fMemoryManager(manager) |
---|
115 | { |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | template <class T> ArrayJanitor<T>::~ArrayJanitor() |
---|
120 | { |
---|
121 | reset(); |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | // ----------------------------------------------------------------------- |
---|
126 | // ArrayJanitor: Public, non-virtual methods |
---|
127 | // ----------------------------------------------------------------------- |
---|
128 | template <class T> void |
---|
129 | ArrayJanitor<T>::orphan() |
---|
130 | { |
---|
131 | release(); |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | // Look, Ma! No hands! Don't call this with null data! |
---|
136 | template <class T> T& |
---|
137 | ArrayJanitor<T>::operator[](int index) const |
---|
138 | { |
---|
139 | // TODO: Add appropriate exception |
---|
140 | return fData[index]; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | template <class T> T* |
---|
145 | ArrayJanitor<T>::get() const |
---|
146 | { |
---|
147 | return fData; |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | template <class T> T* |
---|
152 | ArrayJanitor<T>::release() |
---|
153 | { |
---|
154 | T* p = fData; |
---|
155 | fData = 0; |
---|
156 | return p; |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | template <class T> void |
---|
161 | ArrayJanitor<T>::reset(T* p) |
---|
162 | { |
---|
163 | if (fData) { |
---|
164 | |
---|
165 | if (fMemoryManager) |
---|
166 | fMemoryManager->deallocate((void*)fData); |
---|
167 | else |
---|
168 | delete [] fData; |
---|
169 | } |
---|
170 | |
---|
171 | fData = p; |
---|
172 | fMemoryManager = 0; |
---|
173 | } |
---|
174 | |
---|
175 | template <class T> void |
---|
176 | ArrayJanitor<T>::reset(T* p, MemoryManager* const manager) |
---|
177 | { |
---|
178 | if (fData) { |
---|
179 | |
---|
180 | if (fMemoryManager) |
---|
181 | fMemoryManager->deallocate((void*)fData); |
---|
182 | else |
---|
183 | delete [] fData; |
---|
184 | } |
---|
185 | |
---|
186 | fData = p; |
---|
187 | fMemoryManager = manager; |
---|
188 | } |
---|
189 | |
---|
190 | // |
---|
191 | // JanitorMemFunCall |
---|
192 | // |
---|
193 | |
---|
194 | template <class T> |
---|
195 | JanitorMemFunCall<T>::JanitorMemFunCall( |
---|
196 | T* object, |
---|
197 | MFPT toCall) : |
---|
198 | fObject(object), |
---|
199 | fToCall(toCall) |
---|
200 | { |
---|
201 | } |
---|
202 | |
---|
203 | template <class T> |
---|
204 | JanitorMemFunCall<T>::~JanitorMemFunCall() |
---|
205 | { |
---|
206 | reset (); |
---|
207 | } |
---|
208 | |
---|
209 | template <class T> |
---|
210 | T& JanitorMemFunCall<T>::operator*() const |
---|
211 | { |
---|
212 | return *fObject; |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | template <class T> |
---|
217 | T* JanitorMemFunCall<T>::operator->() const |
---|
218 | { |
---|
219 | return fObject; |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | template <class T> |
---|
224 | T* JanitorMemFunCall<T>::get() const |
---|
225 | { |
---|
226 | return fObject; |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | template <class T> |
---|
231 | T* JanitorMemFunCall<T>::release() |
---|
232 | { |
---|
233 | T* p = fObject; |
---|
234 | fObject = 0; |
---|
235 | return p; |
---|
236 | } |
---|
237 | |
---|
238 | template <class T> |
---|
239 | void JanitorMemFunCall<T>::reset(T* p) |
---|
240 | { |
---|
241 | if (fObject != 0 && fToCall != 0) |
---|
242 | (fObject->*fToCall)(); |
---|
243 | |
---|
244 | fObject = p; |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | XERCES_CPP_NAMESPACE_END |
---|