00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : NativePointer 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 00033 #ifndef NativePointerHPP 00034 #define NativePointerHPP 00035 00036 00037 #include <zeusbase/System/Interfaces/INativePointer.hpp> 00038 00039 BEGIN_NAMESPACE_Zeus 00040 00041 /******************************************************************************/ 00045 /******************************************************************************/ 00046 template <class T> class TNativePointer : public INativePointer 00047 { 00048 public: 00049 /***********************************************************************/ 00052 /***********************************************************************/ 00053 inline TNativePointer() 00054 { 00055 m_pPtr = NULL; 00056 } 00057 00058 /***********************************************************************/ 00061 /***********************************************************************/ 00062 inline TNativePointer(T* pPointer) 00063 { 00064 m_pPtr = pPointer; 00065 } 00066 00067 /***********************************************************************/ 00070 /***********************************************************************/ 00071 inline TNativePointer(T& rPointer) 00072 { 00073 m_pPtr = &rPointer; 00074 } 00075 00076 /***********************************************************************/ 00079 /***********************************************************************/ 00080 inline virtual ~TNativePointer() 00081 { 00082 //do not free memory here because memory might be used some where else 00083 } 00084 00085 /***********************************************************************/ 00088 /***********************************************************************/ 00089 inline void freeMemory() 00090 { 00091 if (m_pPtr != NULL) 00092 { 00093 delete m_pPtr; 00094 m_pPtr = NULL; 00095 } 00096 } 00097 00098 /***********************************************************************/ 00101 /***********************************************************************/ 00102 inline T* getPointer() 00103 { 00104 return m_pPtr; 00105 } 00106 00107 /***********************************************************************/ 00110 /***********************************************************************/ 00111 inline const T* getConstPointer() const 00112 { 00113 return m_pPtr; 00114 } 00115 00116 /***********************************************************************/ 00119 /***********************************************************************/ 00120 inline virtual void* MQUALIFIER getIntPtr() 00121 { 00122 return (void*)m_pPtr; 00123 } 00124 00125 /***********************************************************************/ 00128 /***********************************************************************/ 00129 inline virtual const void* MQUALIFIER getConstIntPtr() const 00130 { 00131 return (void*)m_pPtr; 00132 } 00133 00134 /***********************************************************************/ 00137 /***********************************************************************/ 00138 inline virtual void MQUALIFIER setIntPtr(void* pValue) 00139 { 00140 m_pPtr = reinterpret_cast<T*>(pValue); 00141 } 00142 00143 /***********************************************************************/ 00147 /***********************************************************************/ 00148 inline TNativePointer<T>& operator=(const T* pPtr) 00149 { 00150 m_pPtr = const_cast<T*>(pPtr); 00151 return *this; 00152 } 00153 00154 /***********************************************************************/ 00158 /***********************************************************************/ 00159 inline TNativePointer<T>& operator=(const T& rPtr) 00160 { 00161 m_pPtr = const_cast<T*>(&rPtr); 00162 return *this; 00163 } 00164 00165 /***********************************************************************/ 00170 /***********************************************************************/ 00171 inline T* operator->() { return m_pPtr; } 00172 00173 /***********************************************************************/ 00178 /***********************************************************************/ 00179 inline const T* operator->() const { return m_pPtr; } 00180 00181 /***********************************************************************/ 00185 /***********************************************************************/ 00186 inline T& operator*() const { return *m_pPtr; } 00187 00188 00189 /***********************************************************************/ 00192 /***********************************************************************/ 00193 inline operator T*() const { return m_pPtr; } 00194 00195 private: 00197 T* m_pPtr; 00198 }; 00199 00200 END_NAMESPACE_Zeus 00201 00202 #endif 00203