00001 /* 00002 ----------------------------------------------------------------------------- 00003 This source file is part of OGRE 00004 (Object-oriented Graphics Rendering Engine) 00005 For the latest info, see http://www.ogre3d.org/ 00006 00007 Copyright © 2000-2002 The OGRE Team 00008 Also see acknowledgements in Readme.html 00009 00010 This program is free software; you can redistribute it and/or modify it under 00011 the terms of the GNU Lesser General Public License as published by the Free Software 00012 Foundation; either version 2 of the License, or (at your option) any later 00013 version. 00014 00015 This program is distributed in the hope that it will be useful, but WITHOUT 00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public License along with 00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple 00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to 00022 http://www.gnu.org/copyleft/lesser.txt. 00023 ----------------------------------------------------------------------------- 00024 */ 00025 #ifndef __SharedPtr_H__ 00026 #define __SharedPtr_H__ 00027 00028 #include "OgrePrerequisites.h" 00029 00030 namespace Ogre { 00031 00042 template<class T> class SharedPtr { 00043 protected: 00044 T* pRep; 00045 unsigned int* pUseCount; 00046 public: 00051 SharedPtr() : pRep(0), pUseCount(0) {} 00052 SharedPtr(T* rep) : pRep(rep), pUseCount(new unsigned int(1)) {} 00053 SharedPtr(const SharedPtr& r) : pRep(r.pRep), pUseCount(r.pUseCount) { 00054 // Handle zero pointer gracefully to manage STL containers 00055 if(pUseCount) 00056 { 00057 ++(*pUseCount); 00058 } 00059 } 00060 SharedPtr& operator=(const SharedPtr& r) { 00061 if (pRep == r.pRep) 00062 return *this; 00063 release(); 00064 pRep = r.pRep; 00065 pUseCount = r.pUseCount; 00066 if (pUseCount) 00067 { 00068 ++(*pUseCount); 00069 } 00070 return *this; 00071 } 00072 virtual ~SharedPtr() { 00073 release(); 00074 } 00075 00076 virtual void destroy(void) 00077 { 00078 delete pRep; 00079 delete pUseCount; 00080 } 00081 00082 00083 inline T& operator*() const { assert(pRep); return *pRep; } 00084 inline T* operator->() const { assert(pRep); return pRep; } 00085 inline T* get() const { assert(pRep); return pRep; } 00086 00091 void bind(T* rep) { 00092 assert(!pRep && !pUseCount); 00093 pUseCount = new unsigned int(1); 00094 pRep = rep; 00095 } 00096 00097 inline bool unique() const { assert(pUseCount); return *pUseCount == 1; } 00098 inline unsigned int useCount() const { assert(pUseCount); return *pUseCount; } 00099 00100 inline T* getPointer() const { assert(pRep); return pRep; } 00101 00102 inline bool isNull(void) const { return pRep == 0; } 00103 00104 inline void setNull(void) { 00105 release(); 00106 pRep = 0; 00107 pUseCount = 0; 00108 } 00109 00110 inline void release(void) { 00111 if (pUseCount) 00112 { 00113 if (--(*pUseCount) == 0) { 00114 destroy(); 00115 } 00116 } 00117 } 00118 }; 00119 00120 template<class T, class U> inline bool operator==(SharedPtr<T> const& a, SharedPtr<U> const& b) 00121 { 00122 return a.get() == b.get(); 00123 } 00124 00125 template<class T, class U> inline bool operator!=(SharedPtr<T> const& a, SharedPtr<U> const& b) 00126 { 00127 return a.get() != b.get(); 00128 } 00129 00130 } 00131 00132 #endif
Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:47 2004