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 XALAN_USING_XERCES(MemoryManager) 00041 00042 // An auto_ptr-like class that supports the MemoryManager class. 00043 template< 00044 class Type, 00045 bool toCallDestructor = true> 00046 class XalanMemMgrAutoPtr 00047 { 00048 public: 00049 00050 typedef XALAN_STD_QUALIFIER pair<MemoryManager*, Type*> AutoPtrPairType; 00051 00052 class MemMgrAutoPtrData : public AutoPtrPairType 00053 { 00054 public: 00055 MemMgrAutoPtrData(): 00056 AutoPtrPairType(0,0) 00057 { 00058 } 00059 00060 MemMgrAutoPtrData( 00061 MemoryManager* memoryManager, 00062 Type* dataPointer): 00063 AutoPtrPairType(memoryManager, dataPointer) 00064 { 00065 invariants(); 00066 } 00067 00068 bool 00069 isInitilized()const 00070 { 00071 return this->first != 0 && this->second != 0; 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( 00092 MemoryManager* memoryManager , 00093 Type* dataPointer) 00094 { 00095 invariants(); 00096 00097 this->first = memoryManager; 00098 00099 this->second = dataPointer; 00100 00101 invariants(); 00102 } 00103 00104 private: 00105 00106 void 00107 invariants() const 00108 { 00109 assert( 00110 isInitilized() || 00111 (this->first == 0 && this->second == 0)); 00112 } 00113 }; 00114 00115 00116 XalanMemMgrAutoPtr( 00117 MemoryManager& theManager, 00118 Type* ptr) : 00119 m_pointerInfo(&theManager, ptr) 00120 { 00121 } 00122 00123 XalanMemMgrAutoPtr() : 00124 m_pointerInfo() 00125 { 00126 } 00127 00128 XalanMemMgrAutoPtr(const XalanMemMgrAutoPtr<Type, toCallDestructor>& theSource) : 00129 m_pointerInfo(((XalanMemMgrAutoPtr<Type>&)theSource).release()) 00130 { 00131 } 00132 00133 XalanMemMgrAutoPtr<Type,toCallDestructor>& 00134 operator=(XalanMemMgrAutoPtr<Type,toCallDestructor>& theRHS) 00135 { 00136 if (this != &theRHS) 00137 { 00138 m_pointerInfo.deallocate(); 00139 00140 m_pointerInfo = theRHS.release(); 00141 } 00142 00143 return *this; 00144 } 00145 00146 ~XalanMemMgrAutoPtr() 00147 { 00148 m_pointerInfo.deallocate(); 00149 } 00150 00151 Type& 00152 operator*() const 00153 { 00154 return *m_pointerInfo.second; 00155 } 00156 00157 Type* 00158 operator->() const 00159 { 00160 return m_pointerInfo.second; 00161 } 00162 00163 Type* 00164 get() const 00165 { 00166 return m_pointerInfo.second; 00167 } 00168 00169 MemoryManager* 00170 getMemoryManager() 00171 { 00172 return m_pointerInfo.first; 00173 } 00174 00175 const MemoryManager* 00176 getMemoryManager() const 00177 { 00178 return m_pointerInfo.first; 00179 } 00180 00181 MemMgrAutoPtrData 00182 release() 00183 { 00184 MemMgrAutoPtrData tmp = m_pointerInfo; 00185 00186 m_pointerInfo.reset(0, 0); 00187 00188 return MemMgrAutoPtrData(tmp); 00189 } 00190 00191 Type* 00192 releasePtr() 00193 { 00194 MemMgrAutoPtrData tmp = release(); 00195 00196 return tmp.second; 00197 } 00198 00199 void 00200 reset( 00201 MemoryManager* theManager = 0, 00202 Type* thePointer = 0) 00203 { 00204 m_pointerInfo.deallocate(); 00205 00206 m_pointerInfo.reset(theManager, thePointer); 00207 } 00208 00209 private: 00210 00211 // data member 00212 MemMgrAutoPtrData m_pointerInfo; 00213 }; 00214 00215 00216 00217 00218 template<class Type> 00219 class XalanMemMgrAutoPtrArray 00220 { 00221 public: 00222 00223 #if defined(XALAN_STRICT_ANSI_HEADERS) 00224 typedef std::size_t size_type; 00225 #else 00226 typedef size_t size_type; 00227 #endif 00228 00229 class MemMgrAutoPtrArrayData 00230 { 00231 public: 00232 00233 MemMgrAutoPtrArrayData(): 00234 m_memoryManager(0), 00235 m_dataArray(0), 00236 m_size(0) 00237 { 00238 } 00239 00240 MemMgrAutoPtrArrayData( 00241 MemoryManager* memoryManager, 00242 Type* dataPointer, 00243 size_type size): 00244 m_memoryManager(memoryManager), 00245 m_dataArray(dataPointer), 00246 m_size(size) 00247 { 00248 invariants(); 00249 } 00250 00251 bool 00252 isInitilized()const 00253 { 00254 return m_memoryManager != 0 && m_dataArray != 0 && m_size != 0; 00255 } 00256 00257 void 00258 deallocate() 00259 { 00260 invariants(); 00261 00262 if ( isInitilized() ) 00263 { 00264 assert ( m_dataArray != 0 ); 00265 00266 for ( size_type i = 0; i < m_size ; ++i ) 00267 { 00268 m_dataArray[i].~Type(); 00269 } 00270 00271 m_memoryManager->deallocate(m_dataArray); 00272 } 00273 } 00274 00275 void 00276 reset( 00277 MemoryManager* theMemoryManager, 00278 Type* thePointer, 00279 size_type size) 00280 { 00281 invariants(); 00282 00283 m_memoryManager = theMemoryManager; 00284 00285 m_dataArray = thePointer; 00286 00287 m_size = size; 00288 00289 invariants(); 00290 } 00291 00292 MemoryManager* m_memoryManager; 00293 00294 Type* m_dataArray; 00295 00296 size_type m_size; 00297 00298 private: 00299 00300 void 00301 invariants()const 00302 { 00303 assert( 00304 isInitilized() || 00305 (m_memoryManager == 0 && m_dataArray == 0 && m_size == 0)); 00306 } 00307 }; 00308 00309 XalanMemMgrAutoPtrArray( 00310 MemoryManager& theManager, 00311 Type* ptr, 00312 size_type size) : 00313 m_pointerInfo( 00314 &theManager, 00315 ptr, 00316 size) 00317 { 00318 } 00319 00320 XalanMemMgrAutoPtrArray() : 00321 m_pointerInfo() 00322 { 00323 } 00324 00325 XalanMemMgrAutoPtrArray(const XalanMemMgrAutoPtrArray<Type>& theSource) : 00326 m_pointerInfo(((XalanMemMgrAutoPtr<Type>&)theSource).release()) 00327 { 00328 } 00329 00330 XalanMemMgrAutoPtrArray<Type>& 00331 operator=(XalanMemMgrAutoPtrArray<Type>& theRHS) 00332 { 00333 if (this != &theRHS) 00334 { 00335 m_pointerInfo.deallocate(); 00336 00337 m_pointerInfo = theRHS.release(); 00338 } 00339 00340 return *this; 00341 } 00342 00343 ~XalanMemMgrAutoPtrArray() 00344 { 00345 m_pointerInfo.deallocate(); 00346 } 00347 00348 Type& 00349 operator*() const 00350 { 00351 return *m_pointerInfo.m_dataArray; 00352 } 00353 00354 Type* 00355 operator->() const 00356 { 00357 return m_pointerInfo.m_dataArray; 00358 } 00359 00360 Type* 00361 get() const 00362 { 00363 return m_pointerInfo.m_dataArray; 00364 } 00365 00366 size_type 00367 getSize()const 00368 { 00369 return m_pointerInfo.m_size; 00370 } 00371 00372 MemoryManager* 00373 getMemoryManager() 00374 { 00375 return m_pointerInfo.m_memoryManager; 00376 } 00377 00378 const MemoryManager* 00379 getMemoryManager() const 00380 { 00381 return m_pointerInfo.m_memoryManager; 00382 } 00383 00384 XalanMemMgrAutoPtrArray<Type>& 00385 operator++ () 00386 { 00387 ++m_pointerInfo.m_size; 00388 00389 return *this; 00390 } 00391 00392 /* Since this class is not reference-counted, I don't see how this 00393 could work, since the destruction of the temporary will free 00394 the controlled pointer. 00395 XalanMemMgrAutoPtrArray<Type> 00396 operator++ (int) 00397 { 00398 XalanMemMgrAutoPtrArray<Type> temp = *this; 00399 ++*this; 00400 00401 return temp; 00402 } 00403 */ 00404 00405 MemMgrAutoPtrArrayData 00406 release() 00407 { 00408 MemMgrAutoPtrArrayData tmp = m_pointerInfo; 00409 00410 m_pointerInfo.reset(0, 0, 0); 00411 00412 return MemMgrAutoPtrArrayData(tmp); 00413 } 00414 00415 Type* 00416 releasePtr() 00417 { 00418 MemMgrAutoPtrArrayData tmp = release(); 00419 00420 return tmp.m_dataArray; 00421 } 00422 00423 void 00424 reset( 00425 MemoryManager* theManager = 0, 00426 Type* thePointer = 0 , 00427 size_type size = 0) 00428 { 00429 m_pointerInfo.deallocate(); 00430 00431 m_pointerInfo.reset(theManager, thePointer, size); 00432 } 00433 00434 Type& 00435 operator[](size_type index) const 00436 { 00437 return m_pointerInfo.m_dataArray[index]; 00438 } 00439 00440 private: 00441 00442 // data member 00443 MemMgrAutoPtrArrayData m_pointerInfo; 00444 }; 00445 00446 00447 00448 00449 XALAN_CPP_NAMESPACE_END 00450 00451 00452 00453 #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.10 |
|