00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : AutoPtr 00006 * Package : Zeus.ZeusBase.System 00007 * Author : Benjamin Hadorn 00008 * Date : 27.12.2011 00009 * System : Zeus-Framework 00010 ***************************************************************************** 00011 * Licence: * 00012 * This library is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU Lesser General Public License as * 00014 * published by the Free Software Foundation; either version * 00015 * 2.1 of the License, or (at your option) any later version. * 00016 * * 00017 * This library is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU Lesser General Public License for more details. * 00021 * * 00022 * You should have received a copy of the GNU Lesser General Public * 00023 * License along with this library; if not, write to the Free Software * 00024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 * Changes: 00029 * 27.12.2011 bha: created zeus 2.0 00030 *****************************************************************************/ 00031 00032 #ifndef AutoPtrHPP 00033 #define AutoPtrHPP 00034 00035 #include <zeusbase/System/Interfaces/IZUnknown.hpp> 00036 00037 BEGIN_NAMESPACE_Zeus 00038 00039 00040 /**************************************************************************/ 00043 /**************************************************************************/ 00044 template <class T> class TAutoPtrBase 00045 { 00046 public: 00047 /***********************************************************************/ 00050 /***********************************************************************/ 00051 inline TAutoPtrBase() 00052 { 00053 m_pInterface = NULL; 00054 } 00055 00056 /***********************************************************************/ 00062 /***********************************************************************/ 00063 TAutoPtrBase(const T* pIface, bool bAllocPointer = false) 00064 { 00065 m_pInterface = const_cast<T*>(pIface); // This const cast is needed as long as Zeus collections do not distinguish between objects and pointers. 00066 00067 if (bAllocPointer && m_pInterface != NULL) 00068 { 00069 m_pInterface->addRef(); 00070 } 00071 } 00072 00073 /***********************************************************************/ 00079 /***********************************************************************/ 00080 TAutoPtrBase(const T& rIface, bool bAllocPointer = false) 00081 { 00082 m_pInterface = const_cast<T*>(&rIface); // This const cast is needed as long as Zeus collections do not distinguish between objects and pointers. 00083 if (bAllocPointer && m_pInterface != NULL) 00084 { 00085 m_pInterface->addRef(); 00086 } 00087 } 00088 00089 /***********************************************************************/ 00092 /***********************************************************************/ 00093 inline virtual ~TAutoPtrBase() 00094 { 00095 release(); 00096 } 00097 00098 /***********************************************************************/ 00103 /***********************************************************************/ 00104 void assign(const T* pInterface) 00105 { 00106 release(); 00107 m_pInterface = const_cast<T*>(pInterface); // This const cast is needed as long as Zeus collections do not distinguish between objects and pointers. 00108 if (m_pInterface != NULL) 00109 { 00110 m_pInterface->addRef(); 00111 } 00112 } 00113 00114 /***********************************************************************/ 00119 /***********************************************************************/ 00120 void assign(const T& rInterface) 00121 { 00122 release(); 00123 m_pInterface = const_cast<T*>(&rInterface); // This const cast is needed as long as Zeus collections do not distinguish between objects and pointers. 00124 if (m_pInterface != NULL) 00125 { 00126 m_pInterface->addRef(); 00127 } 00128 } 00129 00130 /***********************************************************************/ 00135 /***********************************************************************/ 00136 inline void attach(const T* pInterface) 00137 { 00138 release(); 00139 m_pInterface = const_cast<T*>(pInterface); // This const cast is needed as long as Zeus collections do not distinguish between objects and pointers. 00140 } 00141 00142 /***********************************************************************/ 00147 /***********************************************************************/ 00148 inline void attach(const T& rInterface) 00149 { 00150 release(); 00151 m_pInterface = const_cast<T*>(&rInterface); // This const cast is needed as long as Zeus collections do not distinguish between objects and pointers. 00152 } 00153 00154 /***********************************************************************/ 00158 /***********************************************************************/ 00159 inline bool equals(const T* pInterface) const 00160 { 00161 return (m_pInterface == pInterface); 00162 } 00163 00164 /***********************************************************************/ 00168 /***********************************************************************/ 00169 inline bool equals(const T& rInterface) const 00170 { 00171 return (m_pInterface == &rInterface); 00172 } 00173 00174 /***********************************************************************/ 00177 /***********************************************************************/ 00178 inline T*& getPointerReference() 00179 { 00180 return m_pInterface; 00181 } 00182 00183 /***********************************************************************/ 00186 /***********************************************************************/ 00187 inline const T*& getConstPointerReference() const 00188 { 00189 #if defined(_MSC_VER) 00190 return (const T*&)(m_pInterface); 00191 #else 00192 return m_pInterface; 00193 #endif 00194 } 00195 00196 /***********************************************************************/ 00199 /***********************************************************************/ 00200 inline IZUnknown*& getInterfaceReference() 00201 { 00202 return (IZUnknown*&)(m_pInterface); 00203 } 00204 00205 /***********************************************************************/ 00208 /***********************************************************************/ 00209 inline const IZUnknown*& getInterfaceReferenceConst() const 00210 { 00211 return (const IZUnknown*&)(m_pInterface); 00212 } 00213 00214 /***********************************************************************/ 00217 /***********************************************************************/ 00218 inline T* getPointer() 00219 { 00220 return m_pInterface; 00221 } 00222 00223 /***********************************************************************/ 00226 /***********************************************************************/ 00227 inline const T* getConstPointer() const 00228 { 00229 return m_pInterface; 00230 } 00231 00232 /***********************************************************************/ 00236 /***********************************************************************/ 00237 inline T* detach() 00238 { 00239 T* pRetval = this->m_pInterface; 00240 this->m_pInterface = NULL; 00241 return pRetval; 00242 } 00243 00244 /***********************************************************************/ 00247 /***********************************************************************/ 00248 inline void release() 00249 { 00250 if (this->m_pInterface != NULL) 00251 { 00252 this->m_pInterface->release(); 00253 this->m_pInterface = NULL; 00254 } 00255 } 00256 00257 protected: 00258 //Interface pointer 00259 T* m_pInterface; 00260 00261 private: 00263 TAutoPtrBase<T>& operator=(const TAutoPtrBase<T>& rWrapper); 00265 TAutoPtrBase(const TAutoPtrBase& rWrapper); 00266 }; 00267 00268 00269 /**************************************************************************/ 00276 /**************************************************************************/ 00277 template <class T> class TAutoPtr : public TAutoPtrBase<T> 00278 { 00279 public: 00280 00281 /***********************************************************************/ 00284 /***********************************************************************/ 00285 inline TAutoPtr() : TAutoPtrBase<T>() 00286 {} 00287 00288 /***********************************************************************/ 00294 /***********************************************************************/ 00295 inline TAutoPtr(const T* pIface, bool bAllocPointer = false) 00296 : TAutoPtrBase<T>(pIface, bAllocPointer) 00297 {} 00298 00299 /***********************************************************************/ 00305 /***********************************************************************/ 00306 inline TAutoPtr(const T& rIface, bool bAllocPointer = false) 00307 : TAutoPtrBase<T>(rIface, bAllocPointer) 00308 {} 00309 00310 /***********************************************************************/ 00314 /***********************************************************************/ 00315 inline TAutoPtr(const TAutoPtr<T>& rWrapper) 00316 : TAutoPtrBase<T>(rWrapper.m_pInterface, true) 00317 {} 00318 00319 /***********************************************************************/ 00322 /***********************************************************************/ 00323 inline virtual ~TAutoPtr() 00324 {} 00325 00326 00327 /***********************************************************************/ 00331 /***********************************************************************/ 00332 inline TAutoPtr<T>& operator=(const T* pIface) 00333 { 00334 attach(pIface); 00335 return *this; 00336 } 00337 00338 /***********************************************************************/ 00342 /***********************************************************************/ 00343 inline TAutoPtr<T>& operator=(const T& rIface) 00344 { 00345 attach(&rIface); 00346 return *this; 00347 } 00348 00349 /***********************************************************************/ 00353 /***********************************************************************/ 00354 inline TAutoPtr<T>& operator=(const TAutoPtr<T>& rWrapper) 00355 { 00356 assign(rWrapper.m_pInterface); 00357 return *this; 00358 } 00359 00360 /***********************************************************************/ 00364 /***********************************************************************/ 00365 inline bool operator==(const TAutoPtr<T>& rWrapper) const 00366 { 00367 return equals(rWrapper.m_pInterface); 00368 } 00369 00370 /***********************************************************************/ 00374 /***********************************************************************/ 00375 inline bool operator!=(const TAutoPtr<T>& rWrapper) const 00376 { 00377 return !equals(rWrapper.m_pInterface); 00378 } 00379 00380 /***********************************************************************/ 00384 /***********************************************************************/ 00385 inline bool operator==(const T* pIface) const 00386 { 00387 return equals(pIface); 00388 } 00389 00390 /***********************************************************************/ 00394 /***********************************************************************/ 00395 inline bool operator==(const T& rIface) const 00396 { 00397 return equals(&rIface); 00398 } 00399 00400 /***********************************************************************/ 00404 /***********************************************************************/ 00405 inline bool operator!=(const T* pIface) const 00406 { 00407 return !equals(pIface); 00408 } 00409 00410 /***********************************************************************/ 00414 /***********************************************************************/ 00415 inline bool operator!=(const T& rIface) const 00416 { 00417 return !equals(&rIface); 00418 } 00419 00420 /***********************************************************************/ 00428 /***********************************************************************/ 00429 inline bool operator<(const TAutoPtr<T>& ptr) const 00430 { 00431 return this->m_pInterface < ptr.m_pInterface; 00432 } 00433 00434 /***********************************************************************/ 00439 /***********************************************************************/ 00440 inline T* operator->() {return this->m_pInterface; } 00441 00442 /***********************************************************************/ 00447 /***********************************************************************/ 00448 inline const T* operator->() const {return this->m_pInterface; } 00449 00450 /***********************************************************************/ 00454 /***********************************************************************/ 00455 inline T& operator*() const {return *this->m_pInterface; } 00456 00457 00458 /***********************************************************************/ 00461 /***********************************************************************/ 00462 inline operator T*() const {return (T*)this->m_pInterface; } 00463 00464 /***********************************************************************/ 00467 /***********************************************************************/ 00468 inline operator T*&() {return (T*&)this->m_pInterface; } 00469 00470 }; 00471 00472 END_NAMESPACE_Zeus 00473 00474 #endif 00475