GME  13
Janitor.c
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: Janitor.c 669844 2008-06-20 10:11:44Z borisk $
00020  */
00021 
00022 
00023 // ---------------------------------------------------------------------------
00024 //  Includes
00025 // ---------------------------------------------------------------------------
00026 #if defined(XERCES_TMPLSINC)
00027 #include <xercesc/util/Janitor.hpp>
00028 #endif
00029 
00030 XERCES_CPP_NAMESPACE_BEGIN
00031 
00032 // ---------------------------------------------------------------------------
00033 //  Janitor: Constructors and Destructor
00034 // ---------------------------------------------------------------------------
00035 template <class T> Janitor<T>::Janitor(T* const toDelete) :
00036     fData(toDelete)
00037 {
00038 }
00039 
00040 
00041 template <class T> Janitor<T>::~Janitor()
00042 {
00043     reset();
00044 }
00045 
00046 
00047 // ---------------------------------------------------------------------------
00048 //  Janitor: Public, non-virtual methods
00049 // ---------------------------------------------------------------------------
00050 template <class T> void
00051 Janitor<T>::orphan()
00052 {
00053    release();
00054 }
00055 
00056 
00057 template <class T> T&
00058 Janitor<T>::operator*() const
00059 {
00060         return *fData;
00061 }
00062 
00063 
00064 template <class T> T*
00065 Janitor<T>::operator->() const
00066 {
00067         return fData;
00068 }
00069 
00070 
00071 template <class T> T*
00072 Janitor<T>::get() const
00073 {
00074         return fData;
00075 }
00076 
00077 
00078 template <class T> T*
00079 Janitor<T>::release()
00080 {
00081         T* p = fData;
00082         fData = 0;
00083         return p;
00084 }
00085 
00086 
00087 template <class T> void Janitor<T>::reset(T* p)
00088 {
00089     if (fData)
00090         delete fData;
00091 
00092     fData = p;
00093 }
00094 
00095 template <class T> bool Janitor<T>::isDataNull()
00096 {
00097     return (fData == 0);
00098 }
00099 
00100 
00101 // -----------------------------------------------------------------------
00102 //  ArrayJanitor: Constructors and Destructor
00103 // -----------------------------------------------------------------------
00104 template <class T> ArrayJanitor<T>::ArrayJanitor(T* const toDelete) :
00105     fData(toDelete)
00106     , fMemoryManager(0)
00107 {
00108 }
00109 
00110 template <class T>
00111 ArrayJanitor<T>::ArrayJanitor(T* const toDelete,
00112                               MemoryManager* const manager) :
00113     fData(toDelete)
00114     , fMemoryManager(manager)
00115 {
00116 }
00117 
00118 
00119 template <class T> ArrayJanitor<T>::~ArrayJanitor()
00120 {
00121         reset();
00122 }
00123 
00124 
00125 // -----------------------------------------------------------------------
00126 //  ArrayJanitor: Public, non-virtual methods
00127 // -----------------------------------------------------------------------
00128 template <class T> void
00129 ArrayJanitor<T>::orphan()
00130 {
00131    release();
00132 }
00133 
00134 
00135 //      Look, Ma! No hands! Don't call this with null data!
00136 template <class T> T&
00137 ArrayJanitor<T>::operator[](int index) const
00138 {
00139         //      TODO: Add appropriate exception
00140         return fData[index];
00141 }
00142 
00143 
00144 template <class T> T*
00145 ArrayJanitor<T>::get() const
00146 {
00147         return fData;
00148 }
00149 
00150 
00151 template <class T> T*
00152 ArrayJanitor<T>::release()
00153 {
00154         T* p = fData;
00155         fData = 0;
00156         return p;
00157 }
00158 
00159 
00160 template <class T> void
00161 ArrayJanitor<T>::reset(T* p)
00162 {
00163         if (fData) {
00164 
00165                 if (fMemoryManager)
00166             fMemoryManager->deallocate((void*)fData);
00167         else
00168             delete [] fData;
00169     }
00170 
00171         fData = p;
00172     fMemoryManager = 0;
00173 }
00174 
00175 template <class T> void
00176 ArrayJanitor<T>::reset(T* p, MemoryManager* const manager)
00177 {
00178         if (fData) {
00179 
00180                 if (fMemoryManager)
00181             fMemoryManager->deallocate((void*)fData);
00182         else
00183             delete [] fData;
00184     }
00185 
00186         fData = p;
00187     fMemoryManager = manager;
00188 }
00189 
00190 //
00191 // JanitorMemFunCall
00192 //
00193 
00194 template <class T>
00195 JanitorMemFunCall<T>::JanitorMemFunCall(
00196             T*      object,
00197             MFPT    toCall) :
00198     fObject(object),
00199     fToCall(toCall)
00200 {
00201 }
00202 
00203 template <class T>
00204 JanitorMemFunCall<T>::~JanitorMemFunCall()
00205 {
00206   reset ();
00207 }
00208 
00209 template <class T>
00210 T& JanitorMemFunCall<T>::operator*() const
00211 {
00212   return *fObject;
00213 }
00214 
00215 
00216 template <class T>
00217 T* JanitorMemFunCall<T>::operator->() const
00218 {
00219   return fObject;
00220 }
00221 
00222 
00223 template <class T>
00224 T* JanitorMemFunCall<T>::get() const
00225 {
00226   return fObject;
00227 }
00228 
00229 
00230 template <class T>
00231 T* JanitorMemFunCall<T>::release()
00232 {
00233   T* p = fObject;
00234   fObject = 0;
00235   return p;
00236 }
00237 
00238 template <class T>
00239 void JanitorMemFunCall<T>::reset(T* p)
00240 {
00241   if (fObject != 0 && fToCall != 0)
00242     (fObject->*fToCall)();
00243 
00244   fObject = p;
00245 }
00246 
00247 
00248 XERCES_CPP_NAMESPACE_END