source: icXML/icXML-devel/src/xercesc/util/Janitor.c @ 2732

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

Original Xerces files with import mods for icxercesc

File size: 4.6 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: 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
30XERCES_CPP_NAMESPACE_BEGIN
31
32// ---------------------------------------------------------------------------
33//  Janitor: Constructors and Destructor
34// ---------------------------------------------------------------------------
35template <class T> Janitor<T>::Janitor(T* const toDelete) :
36    fData(toDelete)
37{
38}
39
40
41template <class T> Janitor<T>::~Janitor()
42{
43    reset();
44}
45
46
47// ---------------------------------------------------------------------------
48//  Janitor: Public, non-virtual methods
49// ---------------------------------------------------------------------------
50template <class T> void
51Janitor<T>::orphan()
52{
53   release();
54}
55
56
57template <class T> T&
58Janitor<T>::operator*() const
59{
60        return *fData;
61}
62
63
64template <class T> T*
65Janitor<T>::operator->() const
66{
67        return fData;
68}
69
70
71template <class T> T*
72Janitor<T>::get() const
73{
74        return fData;
75}
76
77
78template <class T> T*
79Janitor<T>::release()
80{
81        T* p = fData;
82        fData = 0;
83        return p;
84}
85
86
87template <class T> void Janitor<T>::reset(T* p)
88{
89    if (fData)
90        delete fData;
91
92    fData = p;
93}
94
95template <class T> bool Janitor<T>::isDataNull()
96{
97    return (fData == 0);
98}
99
100
101// -----------------------------------------------------------------------
102//  ArrayJanitor: Constructors and Destructor
103// -----------------------------------------------------------------------
104template <class T> ArrayJanitor<T>::ArrayJanitor(T* const toDelete) :
105    fData(toDelete)
106    , fMemoryManager(0)
107{
108}
109
110template <class T>
111ArrayJanitor<T>::ArrayJanitor(T* const toDelete,
112                              MemoryManager* const manager) :
113    fData(toDelete)
114    , fMemoryManager(manager)
115{
116}
117
118
119template <class T> ArrayJanitor<T>::~ArrayJanitor()
120{
121        reset();
122}
123
124
125// -----------------------------------------------------------------------
126//  ArrayJanitor: Public, non-virtual methods
127// -----------------------------------------------------------------------
128template <class T> void
129ArrayJanitor<T>::orphan()
130{
131   release();
132}
133
134
135//      Look, Ma! No hands! Don't call this with null data!
136template <class T> T&
137ArrayJanitor<T>::operator[](int index) const
138{
139        //      TODO: Add appropriate exception
140        return fData[index];
141}
142
143
144template <class T> T*
145ArrayJanitor<T>::get() const
146{
147        return fData;
148}
149
150
151template <class T> T*
152ArrayJanitor<T>::release()
153{
154        T* p = fData;
155        fData = 0;
156        return p;
157}
158
159
160template <class T> void
161ArrayJanitor<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
175template <class T> void
176ArrayJanitor<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
194template <class T>
195JanitorMemFunCall<T>::JanitorMemFunCall(
196            T*      object,
197            MFPT    toCall) :
198    fObject(object),
199    fToCall(toCall)
200{
201}
202
203template <class T>
204JanitorMemFunCall<T>::~JanitorMemFunCall()
205{
206  reset ();
207}
208
209template <class T>
210T& JanitorMemFunCall<T>::operator*() const
211{
212  return *fObject;
213}
214
215
216template <class T>
217T* JanitorMemFunCall<T>::operator->() const
218{
219  return fObject;
220}
221
222
223template <class T>
224T* JanitorMemFunCall<T>::get() const
225{
226  return fObject;
227}
228
229
230template <class T>
231T* JanitorMemFunCall<T>::release()
232{
233  T* p = fObject;
234  fObject = 0;
235  return p;
236}
237
238template <class T>
239void JanitorMemFunCall<T>::reset(T* p)
240{
241  if (fObject != 0 && fToCall != 0)
242    (fObject->*fToCall)();
243
244  fObject = p;
245}
246
247
248XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.