00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : Ptr 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 PtrHPP 00033 #define PtrHPP 00034 00035 #include <zeusbase/System/Interfaces/IZUnknown.hpp> 00036 00037 BEGIN_NAMESPACE_Zeus 00038 00039 00040 /**************************************************************************/ 00047 /**************************************************************************/ 00048 template <class T> class TPtr 00049 { 00050 public: 00051 00052 /***********************************************************************/ 00055 /***********************************************************************/ 00056 inline TPtr() 00057 { 00058 m_pPtr = NULL; 00059 m_pReferenceCounter = NULL; 00060 } 00061 00062 /***********************************************************************/ 00066 /***********************************************************************/ 00067 TPtr(const T* pPtr) 00068 { 00069 m_pReferenceCounter = NULL; 00070 m_pPtr = (T*)pPtr; 00071 if (m_pPtr != NULL) 00072 { 00073 m_pReferenceCounter = new Int(); 00074 *m_pReferenceCounter = 1; 00075 } 00076 } 00077 00078 /***********************************************************************/ 00082 /***********************************************************************/ 00083 inline TPtr(const T& rPtr) 00084 { 00085 m_pPtr = (T*)(&rPtr); 00086 m_pReferenceCounter = new Int(); 00087 *m_pReferenceCounter = 1; 00088 } 00089 00090 /***********************************************************************/ 00095 /***********************************************************************/ 00096 inline TPtr(const TPtr<T>& rWrapper) 00097 { 00098 m_pPtr = NULL; 00099 m_pReferenceCounter = NULL; 00100 assignWrapper(rWrapper); 00101 } 00102 00103 /***********************************************************************/ 00106 /***********************************************************************/ 00107 inline virtual ~TPtr() 00108 { 00109 release(); 00110 } 00111 00112 /***********************************************************************/ 00115 /***********************************************************************/ 00116 void assignWrapper(const TPtr& rWrapper) 00117 { 00118 release(); 00119 m_pPtr = rWrapper.m_pPtr; 00120 m_pReferenceCounter = rWrapper.m_pReferenceCounter; 00121 if (m_pPtr != NULL && m_pReferenceCounter != NULL && *m_pReferenceCounter > 0) 00122 { 00123 (*m_pReferenceCounter)++; 00124 } 00125 else 00126 { 00127 m_pPtr = NULL; 00128 m_pReferenceCounter = NULL; 00129 } 00130 } 00131 00132 /***********************************************************************/ 00135 /***********************************************************************/ 00136 void assign(const T* pInterface) 00137 { 00138 release(); 00139 m_pPtr = (T*)pInterface; 00140 if (m_pPtr != NULL) 00141 { 00142 m_pReferenceCounter = new Int(); 00143 *m_pReferenceCounter = 1; 00144 } 00145 } 00146 00147 /***********************************************************************/ 00150 /***********************************************************************/ 00151 inline bool equals(const T* pPtr) const 00152 { 00153 return (m_pPtr == pPtr); 00154 } 00155 00156 /***********************************************************************/ 00159 /***********************************************************************/ 00160 inline T* getPointer() 00161 { 00162 return m_pPtr; 00163 } 00164 00165 /***********************************************************************/ 00168 /***********************************************************************/ 00169 inline const T* getConstPointer() const 00170 { 00171 return m_pPtr; 00172 } 00173 00174 /***********************************************************************/ 00177 /***********************************************************************/ 00178 void release() 00179 { 00180 if (this->m_pPtr != NULL) 00181 { 00182 (*m_pReferenceCounter)--; 00183 if (*m_pReferenceCounter <= 0) 00184 { 00185 delete this->m_pReferenceCounter; 00186 delete this->m_pPtr; 00187 this->m_pPtr = NULL; 00188 this->m_pReferenceCounter = NULL; 00189 } //(*m_pReferenceCounter <= 0) 00190 } //(this->m_pPtr != NULL) 00191 } 00192 00193 /***********************************************************************/ 00197 /***********************************************************************/ 00198 inline TPtr<T>& operator=(const TPtr<T>& rWrapper) 00199 { 00200 assignWrapper(rWrapper); 00201 return *this; 00202 } 00203 00204 /***********************************************************************/ 00208 /***********************************************************************/ 00209 inline TPtr<T>& operator=(const T* pPtr) 00210 { 00211 assign(pPtr); 00212 return *this; 00213 } 00214 00215 /***********************************************************************/ 00219 /***********************************************************************/ 00220 inline TPtr<T>& operator=(const T& rPtr) 00221 { 00222 assign(&rPtr); 00223 return *this; 00224 } 00225 00226 /***********************************************************************/ 00230 /***********************************************************************/ 00231 inline bool operator==(const TPtr<T>& rWrapper) const 00232 { 00233 return equals(rWrapper.m_pPtr); 00234 } 00235 00236 /***********************************************************************/ 00240 /***********************************************************************/ 00241 inline bool operator!=(const TPtr<T>& rWrapper) const 00242 { 00243 return !equals(rWrapper.m_pPtr); 00244 } 00245 00246 /***********************************************************************/ 00250 /***********************************************************************/ 00251 inline bool operator==(const T* pPtr) const 00252 { 00253 return equals(pPtr); 00254 } 00255 00256 /***********************************************************************/ 00260 /***********************************************************************/ 00261 inline bool operator==(const T& rPtr) const 00262 { 00263 return equals(&rPtr); 00264 } 00265 00266 /***********************************************************************/ 00270 /***********************************************************************/ 00271 inline bool operator!=(const T* pPtr) const 00272 { 00273 return !equals(pPtr); 00274 } 00275 00276 /***********************************************************************/ 00280 /***********************************************************************/ 00281 inline bool operator!=(const T& rPtr) const 00282 { 00283 return !equals(&rPtr); 00284 } 00285 00286 /***********************************************************************/ 00291 /***********************************************************************/ 00292 inline T* operator->() {return m_pPtr; } 00293 00294 /***********************************************************************/ 00299 /***********************************************************************/ 00300 inline const T* operator->() const {return m_pPtr; } 00301 00302 /***********************************************************************/ 00306 /***********************************************************************/ 00307 inline T& operator*() const {return *m_pPtr; } 00308 00309 00310 /***********************************************************************/ 00313 /***********************************************************************/ 00314 inline operator T*() const {return m_pPtr; } 00315 00316 protected: 00318 T* m_pPtr; 00320 Int* m_pReferenceCounter; 00321 }; 00322 00323 END_NAMESPACE_Zeus 00324 00325 #endif