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