00001 /*************************************************************************** 00002 * Copyright (C) 2005 by Benjamin Hadorn (bhadorn@swissinfo.org) * 00003 *************************************************************************** 00004 * Projekt : Zeus 00005 * Module : AutoPtr 00006 * Package : System 00007 * Author : Benjamin Hadorn 00008 * Datum : $Date: 5.10.09 22:10 $ 00009 * Ablage : $File$ 00010 * System : Cell Computing Model 00011 *************************************************************************** 00012 * Licence: * 00013 * This library is free software; you can redistribute it and/or modify * 00014 * it under the terms of the GNU Lesser General Public License as * 00015 * published by the Free Software Foundation; either version * 00016 * 2.1 of the License, or (at your option) any later version. * 00017 * * 00018 * This library is distributed in the hope that it will be useful, * 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00021 * GNU Lesser General Public License for more details. * 00022 * * 00023 * You should have received a copy of the GNU Lesser General Public * 00024 * License along with this library; if not, write to the Free Software * 00025 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00026 ***************************************************************************/ 00027 00028 /*************************************************************************** 00029 Changes : 00030 $Log: /Development_F/StuderWIN/dev/Zeus/src/zeusbase/System/AutoPtr.hpp $ 00031 ** 00032 ** 9 5.10.09 22:10 Bha 00033 ** improving speed using inline methods 00034 ** 00035 ** 8 15.09.08 7:09 Bha 00036 ** new zeus version 0.6.4 00037 ** 00038 ** 7 11.08.08 11:26 Mab 00039 ** operator < implemented. 00040 ** 00041 ** 6 4.04.08 19:04 Bha 00042 ** eliminated coding errors and warnings for VC2005 and C++Builder 2007 00043 ** 00044 ** 5 14.01.08 14:31 Bha 00045 ** New methods to handle references added 00046 ** 00047 ** 4 30.10.07 16:43 bha 00048 ** New method added to avoid icast macros 00049 ** 00050 ** 3 4.10.07 7:10 bha 00051 ** const method implemented 00052 ** 00053 ** 2 3.09.07 7:03 bha 00054 ** New Framework using auto pointer and improved lists 00055 ** 00056 ** 1 13.08.07 7:30 bha 00057 ** Created 00058 ***************************************************************************/ 00059 00060 #ifndef AutoPtrHPP 00061 #define AutoPtrHPP 00062 00063 #include <zeusbase/System/Interfaces/IZUnknown.hpp> 00064 00065 BEGIN_NAMESPACE_Zeus 00066 00067 00068 /**************************************************************************/ 00071 /**************************************************************************/ 00072 template <class T> class TAutoPtrBase 00073 { 00074 public: 00075 /***********************************************************************/ 00078 /***********************************************************************/ 00079 inline TAutoPtrBase() 00080 { 00081 m_pInterface = NULL; 00082 } 00083 00084 /***********************************************************************/ 00090 /***********************************************************************/ 00091 TAutoPtrBase(const T* pIface, bool bAllocPointer = false) 00092 { 00093 m_pInterface = const_cast<T*>(pIface); 00094 00095 if (bAllocPointer && m_pInterface != NULL) 00096 { 00097 m_pInterface->addRef(); 00098 } 00099 } 00100 00101 /***********************************************************************/ 00107 /***********************************************************************/ 00108 TAutoPtrBase(const T& rIface, bool bAllocPointer = false) 00109 { 00110 m_pInterface = const_cast<T*>(&rIface); 00111 if (bAllocPointer && m_pInterface != NULL) 00112 { 00113 m_pInterface->addRef(); 00114 } 00115 } 00116 00117 /***********************************************************************/ 00120 /***********************************************************************/ 00121 inline virtual ~TAutoPtrBase() 00122 { 00123 release(); 00124 } 00125 00126 /***********************************************************************/ 00131 /***********************************************************************/ 00132 void assign(const T* pInterface) 00133 { 00134 release(); 00135 m_pInterface = const_cast<T*>(pInterface); 00136 if (m_pInterface != NULL) 00137 { 00138 m_pInterface->addRef(); 00139 } 00140 } 00141 00142 /***********************************************************************/ 00147 /***********************************************************************/ 00148 void assign(const T& rInterface) 00149 { 00150 release(); 00151 m_pInterface = const_cast<T*>(&rInterface); 00152 if (m_pInterface != NULL) 00153 { 00154 m_pInterface->addRef(); 00155 } 00156 } 00157 00158 /***********************************************************************/ 00163 /***********************************************************************/ 00164 inline void attach(const T* pInterface) 00165 { 00166 release(); 00167 m_pInterface = const_cast<T*>(pInterface); 00168 } 00169 00170 /***********************************************************************/ 00175 /***********************************************************************/ 00176 inline void attach(const T& rInterface) 00177 { 00178 release(); 00179 m_pInterface = const_cast<T*>(&rInterface); 00180 } 00181 00182 /***********************************************************************/ 00186 /***********************************************************************/ 00187 inline bool equals(const T* pInterface) const 00188 { 00189 return (m_pInterface == pInterface); 00190 } 00191 00192 /***********************************************************************/ 00196 /***********************************************************************/ 00197 inline bool equals(const T& rInterface) const 00198 { 00199 return (m_pInterface == &rInterface); 00200 } 00201 00202 /***********************************************************************/ 00205 /***********************************************************************/ 00206 inline T*& getPointerReference() 00207 { 00208 return m_pInterface; 00209 } 00210 00211 /***********************************************************************/ 00214 /***********************************************************************/ 00215 inline const T*& getConstPointerReference() const 00216 { 00217 #if defined(_MSC_VER) 00218 return (const T*&)(m_pInterface); 00219 #else 00220 return m_pInterface; 00221 #endif 00222 } 00223 00224 /***********************************************************************/ 00227 /***********************************************************************/ 00228 inline IZUnknown*& getInterfaceReference() 00229 { 00230 return (IZUnknown*&)(m_pInterface); 00231 } 00232 00233 /***********************************************************************/ 00236 /***********************************************************************/ 00237 inline const IZUnknown*& getInterfaceReferenceConst() const 00238 { 00239 return (const IZUnknown*&)(m_pInterface); 00240 } 00241 00242 /***********************************************************************/ 00245 /***********************************************************************/ 00246 inline T* getPointer() 00247 { 00248 return m_pInterface; 00249 } 00250 00251 /***********************************************************************/ 00254 /***********************************************************************/ 00255 inline const T* getConstPointer() const 00256 { 00257 return m_pInterface; 00258 } 00259 00260 /***********************************************************************/ 00264 /***********************************************************************/ 00265 inline T* detach() 00266 { 00267 T* pRetval = this->m_pInterface; 00268 this->m_pInterface = NULL; 00269 return pRetval; 00270 } 00271 00272 /***********************************************************************/ 00275 /***********************************************************************/ 00276 inline void release() 00277 { 00278 if (this->m_pInterface != NULL) 00279 { 00280 this->m_pInterface->release(); 00281 this->m_pInterface = NULL; 00282 } 00283 } 00284 00285 protected: 00286 //Interface pointer 00287 T* m_pInterface; 00288 00289 private: 00291 TAutoPtrBase<T>& operator=(const TAutoPtrBase<T>& rWrapper); 00293 TAutoPtrBase(const TAutoPtrBase& rWrapper); 00294 }; 00295 00296 00297 /**************************************************************************/ 00304 /**************************************************************************/ 00305 template <class T> class TAutoPtr : public TAutoPtrBase<T> 00306 { 00307 public: 00308 00309 /***********************************************************************/ 00312 /***********************************************************************/ 00313 inline TAutoPtr() : TAutoPtrBase<T>() 00314 {} 00315 00316 /***********************************************************************/ 00322 /***********************************************************************/ 00323 inline TAutoPtr(const T* pIface, bool bAllocPointer = false) 00324 : TAutoPtrBase<T>(pIface, bAllocPointer) 00325 {} 00326 00327 /***********************************************************************/ 00333 /***********************************************************************/ 00334 inline TAutoPtr(const T& rIface, bool bAllocPointer = false) 00335 : TAutoPtrBase<T>(rIface, bAllocPointer) 00336 {} 00337 00338 /***********************************************************************/ 00342 /***********************************************************************/ 00343 inline TAutoPtr(const TAutoPtr<T>& rWrapper) 00344 : TAutoPtrBase<T>(rWrapper.m_pInterface, true) 00345 {} 00346 00347 /***********************************************************************/ 00350 /***********************************************************************/ 00351 inline virtual ~TAutoPtr() 00352 {} 00353 00354 00355 /***********************************************************************/ 00359 /***********************************************************************/ 00360 inline TAutoPtr<T>& operator=(const T* pIface) 00361 { 00362 attach(pIface); 00363 return *this; 00364 } 00365 00366 /***********************************************************************/ 00370 /***********************************************************************/ 00371 inline TAutoPtr<T>& operator=(const T& rIface) 00372 { 00373 attach(&rIface); 00374 return *this; 00375 } 00376 00377 /***********************************************************************/ 00381 /***********************************************************************/ 00382 inline TAutoPtr<T>& operator=(const TAutoPtr<T>& rWrapper) 00383 { 00384 assign(rWrapper.m_pInterface); 00385 return *this; 00386 } 00387 00388 /***********************************************************************/ 00392 /***********************************************************************/ 00393 inline bool operator==(const TAutoPtr<T>& rWrapper) const 00394 { 00395 return equals(rWrapper.m_pInterface); 00396 } 00397 00398 /***********************************************************************/ 00402 /***********************************************************************/ 00403 inline bool operator!=(const TAutoPtr<T>& rWrapper) const 00404 { 00405 return !equals(rWrapper.m_pInterface); 00406 } 00407 00408 /***********************************************************************/ 00412 /***********************************************************************/ 00413 inline bool operator==(const T* pIface) const 00414 { 00415 return equals(pIface); 00416 } 00417 00418 /***********************************************************************/ 00422 /***********************************************************************/ 00423 inline bool operator==(const T& rIface) const 00424 { 00425 return equals(&rIface); 00426 } 00427 00428 /***********************************************************************/ 00432 /***********************************************************************/ 00433 inline bool operator!=(const T* pIface) const 00434 { 00435 return !equals(pIface); 00436 } 00437 00438 /***********************************************************************/ 00442 /***********************************************************************/ 00443 inline bool operator!=(const T& rIface) const 00444 { 00445 return !equals(&rIface); 00446 } 00447 00448 /***********************************************************************/ 00456 /***********************************************************************/ 00457 inline bool operator<(const TAutoPtr<T>& ptr) const 00458 { 00459 return this->m_pInterface < ptr.m_pInterface; 00460 } 00461 00462 /***********************************************************************/ 00467 /***********************************************************************/ 00468 inline T* operator->() {return this->m_pInterface; } 00469 00470 /***********************************************************************/ 00475 /***********************************************************************/ 00476 inline const T* operator->() const {return this->m_pInterface; } 00477 00478 /***********************************************************************/ 00482 /***********************************************************************/ 00483 inline T& operator*() const {return *this->m_pInterface; } 00484 00485 00486 /***********************************************************************/ 00489 /***********************************************************************/ 00490 inline operator T*() const {return (T*)this->m_pInterface; } 00491 00492 /***********************************************************************/ 00495 /***********************************************************************/ 00496 inline operator T*&() {return (T*&)this->m_pInterface; } 00497 00498 }; 00499 00500 END_NAMESPACE_Zeus 00501 00502 #endif 00503