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 00077 inline T& operator*() const { assert(pRep); return *pRep; } 00078 inline T* operator->() const { assert(pRep); return pRep; } 00079 inline T* get() const { assert(pRep); return pRep; } 00080 00085 void bind(T* rep) { 00086 assert(!pRep && !pUseCount); 00087 pUseCount = new unsigned int(1); 00088 pRep = rep; 00089 } 00090 00091 inline bool unique() const { assert(pUseCount); return *pUseCount == 1; } 00092 inline unsigned int useCount() const { assert(pUseCount); return *pUseCount; } 00093 00094 inline T* getPointer() const { assert(pRep); return pRep; } 00095 00096 inline bool isNull(void) const { return pRep == 0; } 00097 00098 inline void setNull(void) { 00099 release(); 00100 pRep = 0; 00101 pUseCount = 0; 00102 } 00103 00104 protected: 00105 00106 inline void release(void) { 00107 if (pUseCount) 00108 { 00109 if (--(*pUseCount) == 0) { 00110 destroy(); 00111 } 00112 } 00113 } 00114 00115 virtual void destroy(void) 00116 { 00117 delete pRep; 00118 delete pUseCount; 00119 } 00120 }; 00121 00122 template<class T, class U> inline bool operator==(SharedPtr<T> const& a, SharedPtr<U> const& b) 00123 { 00124 return a.get() == b.get(); 00125 } 00126 00127 template<class T, class U> inline bool operator!=(SharedPtr<T> const& a, SharedPtr<U> const& b) 00128 { 00129 return a.get() != b.get(); 00130 } 00131 00132 } 00133 00134 #endif
Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:46 2004