00001 /* 00002 * Copyright 1999-2004 The Apache Software Foundation. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #if !defined(XALANMEMMGRAUTOPTR_HEADER_GUARD_1357924680) 00017 #define XALANMEMMGRAUTOPTR_HEADER_GUARD_1357924680 00018 00019 00020 00021 // Base include file. Must be first. 00022 #include <xalanc/PlatformSupport/PlatformSupportDefinitions.hpp> 00023 00024 00025 00026 #include <xalanc/Include/XalanMemoryManagement.hpp> 00027 00028 #include <cstddef> 00029 00030 #include <cassert> 00031 00032 #include <utility> 00033 00034 00035 00036 XALAN_CPP_NAMESPACE_BEGIN 00037 00038 00039 00040 // We're using our own auto_ptr-like class due to wide 00041 // variations amongst the varous platforms we have to 00042 // support 00043 template< class Type, 00044 bool toCallDestructor = true> 00045 class XalanMemMgrAutoPtr 00046 { 00047 public: 00048 00049 typedef XALAN_STD_QUALIFIER pair<MemoryManagerType*, Type*> AutoPtrPairType; 00050 00051 class MemMgrAutoPtrData : public AutoPtrPairType 00052 { 00053 public: 00054 MemMgrAutoPtrData(): 00055 AutoPtrPairType(0,0) 00056 { 00057 } 00058 00059 MemMgrAutoPtrData( 00060 MemoryManagerType* memoryManager, 00061 Type* dataPointer): 00062 AutoPtrPairType(memoryManager, dataPointer) 00063 { 00064 invariants(); 00065 } 00066 00067 00068 bool 00069 isInitilized()const 00070 { 00071 return ( (this->first != 0) && (this->second != 0) )? true : false; 00072 } 00073 00074 void 00075 deallocate() 00076 { 00077 invariants(); 00078 00079 if ( isInitilized() ) 00080 { 00081 if ( toCallDestructor ) 00082 { 00083 this->second->~Type(); 00084 } 00085 00086 this->first->deallocate(this->second); 00087 } 00088 } 00089 00090 void 00091 reset( MemoryManagerType* m_memoryManager , 00092 Type* m_dataPointer ) 00093 { 00094 invariants(); 00095 00096 this->first = m_memoryManager; 00097 00098 this->second = m_dataPointer; 00099 00100 invariants(); 00101 } 00102 private: 00103 void 00104 invariants()const 00105 { 00106 assert( isInitilized() || 00107 ( (this->first == 0) && (this->second ==0) ) ); 00108 } 00109 00110 }; 00111 00112 00113 XalanMemMgrAutoPtr( 00114 MemoryManagerType& theManager, 00115 Type* ptr ) : 00116 m_pointerInfo(&theManager, ptr) 00117 { 00118 } 00119 00120 XalanMemMgrAutoPtr() : 00121 m_pointerInfo() 00122 { 00123 } 00124 00125 XalanMemMgrAutoPtr(const XalanMemMgrAutoPtr<Type,toCallDestructor>& theSource) : 00126 m_pointerInfo(((XalanMemMgrAutoPtr<Type>&)theSource).release()) 00127 { 00128 } 00129 00130 XalanMemMgrAutoPtr<Type,toCallDestructor>& 00131 operator=(XalanMemMgrAutoPtr<Type,toCallDestructor>& theRHS) 00132 { 00133 if (this != &theRHS) 00134 { 00135 m_pointerInfo.deallocate(); 00136 00137 m_pointerInfo = theRHS.release(); 00138 } 00139 00140 return *this; 00141 } 00142 00143 ~XalanMemMgrAutoPtr() 00144 { 00145 m_pointerInfo.deallocate(); 00146 } 00147 00148 Type& 00149 operator*() const 00150 { 00151 return *m_pointerInfo.second; 00152 } 00153 00154 Type* 00155 operator->() const 00156 { 00157 return m_pointerInfo.second; 00158 } 00159 00160 Type* 00161 get() const 00162 { 00163 return m_pointerInfo.second; 00164 } 00165 00166 MemoryManagerType* 00167 getMemoryManager() 00168 { 00169 return m_pointerInfo.first; 00170 } 00171 00172 const MemoryManagerType* 00173 getMemoryManager() const 00174 { 00175 return m_pointerInfo.first; 00176 } 00177 00178 MemMgrAutoPtrData 00179 release() 00180 { 00181 MemMgrAutoPtrData tmp = m_pointerInfo; 00182 00183 m_pointerInfo.reset( 0 , 0 ); 00184 00185 return MemMgrAutoPtrData(tmp); 00186 } 00187 00188 Type* 00189 releasePtr() 00190 { 00191 MemMgrAutoPtrData tmp = release(); 00192 00193 return tmp.second; 00194 } 00195 00196 void 00197 reset( MemoryManagerType* theManager = 0, 00198 Type* thePointer = 0 ) 00199 { 00200 m_pointerInfo.deallocate(); 00201 00202 m_pointerInfo.reset ( theManager , thePointer ); 00203 } 00204 00205 private: 00206 00207 00208 // data member 00209 MemMgrAutoPtrData m_pointerInfo; 00210 }; 00211 00212 00213 00214 00215 template< class Type> 00216 class XalanMemMgrAutoPtrArray 00217 { 00218 public: 00219 00220 typedef unsigned int size_type; 00221 00222 class MemMgrAutoPtrArrayData 00223 { 00224 public: 00225 MemMgrAutoPtrArrayData(): 00226 m_memoryManager(0), 00227 m_dataArray(0), 00228 m_size(0) 00229 { 00230 } 00231 00232 MemMgrAutoPtrArrayData( 00233 MemoryManagerType* memoryManager, 00234 Type* dataPointer, 00235 size_type size): 00236 m_memoryManager(memoryManager), 00237 m_dataArray(dataPointer), 00238 m_size(size) 00239 { 00240 invariants(); 00241 } 00242 00243 00244 bool 00245 isInitilized()const 00246 { 00247 return ( (m_memoryManager != 0) && (m_dataArray != 0) && (m_size != 0) )? true : false; 00248 } 00249 00250 void 00251 deallocate() 00252 { 00253 invariants(); 00254 00255 if ( isInitilized() ) 00256 { 00257 assert ( m_dataArray != 0 ); 00258 00259 for ( size_type i = 0; i < m_size ; ++i ) 00260 { 00261 m_dataArray[i].~Type(); 00262 } 00263 m_memoryManager->deallocate ( m_dataArray); 00264 } 00265 } 00266 00267 void 00268 reset( MemoryManagerType* memoryManager , 00269 Type* dataPointer, 00270 size_type size) 00271 { 00272 invariants(); 00273 00274 m_memoryManager = memoryManager; 00275 00276 m_dataArray = dataPointer; 00277 00278 m_size = size; 00279 00280 invariants(); 00281 } 00282 00283 00284 MemoryManagerType* m_memoryManager; 00285 Type* m_dataArray; 00286 size_type m_size; 00287 00288 private: 00289 void 00290 invariants()const 00291 { 00292 assert( isInitilized() || 00293 ( (m_memoryManager == 0) && (m_dataArray ==0) && (m_size == 0)) ); 00294 } 00295 }; 00296 00297 XalanMemMgrAutoPtrArray( 00298 MemoryManagerType& theManager, 00299 Type* ptr, 00300 size_type size) : 00301 m_pointerInfo(&theManager, ptr, size) 00302 { 00303 } 00304 00305 XalanMemMgrAutoPtrArray() : 00306 m_pointerInfo() 00307 { 00308 } 00309 00310 XalanMemMgrAutoPtrArray(const XalanMemMgrAutoPtrArray<Type>& theSource) : 00311 m_pointerInfo(((XalanMemMgrAutoPtr<Type>&)theSource).release()) 00312 { 00313 } 00314 00315 00316 XalanMemMgrAutoPtrArray<Type>& 00317 operator=(XalanMemMgrAutoPtrArray<Type>& theRHS) 00318 { 00319 if (this != &theRHS) 00320 { 00321 m_pointerInfo.deallocate(); 00322 00323 m_pointerInfo = theRHS.release(); 00324 } 00325 00326 return *this; 00327 } 00328 00329 ~XalanMemMgrAutoPtrArray() 00330 { 00331 m_pointerInfo.deallocate(); 00332 } 00333 00334 Type& 00335 operator*() const 00336 { 00337 return *m_pointerInfo.m_dataArray; 00338 } 00339 00340 Type* 00341 operator->() const 00342 { 00343 return m_pointerInfo.m_dataArray; 00344 } 00345 00346 Type* 00347 get() const 00348 { 00349 return m_pointerInfo.m_dataArray; 00350 } 00351 00352 size_type 00353 getSize()const 00354 { 00355 return m_pointerInfo.m_size; 00356 } 00357 00358 MemoryManagerType* 00359 getMemoryManager() 00360 { 00361 return m_pointerInfo.m_memoryManager; 00362 } 00363 00364 const MemoryManagerType* 00365 getMemoryManager() const 00366 { 00367 return m_pointerInfo.m_memoryManager; 00368 } 00369 00370 XalanMemMgrAutoPtrArray<Type>& 00371 operator++ () 00372 { 00373 ++m_pointerInfo.m_size; 00374 00375 return *this; 00376 } 00377 00378 /* Since this class is not reference-counted, I don't see how this 00379 could work, since the destruction of the temporary will free 00380 the controlled pointer. 00381 XalanMemMgrAutoPtrArray<Type> 00382 operator++ (int) 00383 { 00384 XalanMemMgrAutoPtrArray<Type> temp = *this; 00385 ++*this; 00386 00387 return temp; 00388 } 00389 */ 00390 00391 MemMgrAutoPtrArrayData 00392 release() 00393 { 00394 MemMgrAutoPtrArrayData tmp = m_pointerInfo; 00395 00396 m_pointerInfo.reset( 0 , 0 , 0); 00397 00398 return MemMgrAutoPtrArrayData(tmp); 00399 } 00400 00401 Type* 00402 releasePtr() 00403 { 00404 MemMgrAutoPtrArrayData tmp = release(); 00405 00406 00407 return tmp.dataPointer; 00408 } 00409 00410 void 00411 reset( MemoryManagerType* theManager = 0, 00412 Type* thePointer = 0 , 00413 size_type size = 0) 00414 { 00415 m_pointerInfo.deallocate(); 00416 00417 m_pointerInfo.reset ( theManager , thePointer , size ); 00418 } 00419 00420 Type& 00421 operator[](size_type index) const 00422 { 00423 return m_pointerInfo.m_dataArray[index]; 00424 } 00425 00426 private: 00427 00428 00429 // data member 00430 MemMgrAutoPtrArrayData m_pointerInfo; 00431 }; 00432 00433 00434 00435 00436 XALAN_CPP_NAMESPACE_END 00437 00438 00439 00440 #endif // if !defined(XALANMEMMGRAUTOPTR_HEADER_GUARD_1357924680)
Doxygen and GraphViz are used to generate this API documentation from the Xalan-C header files.
Xalan-C++ XSLT Processor Version 1.9 |
|