Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreSharedPtr.h

Go to the documentation of this file.
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             if (pUseCount)
00064             {
00065                 if (--(*pUseCount) == 0) {
00066                     destroy();
00067                 }
00068             }
00069             pRep = r.pRep;
00070             pUseCount = r.pUseCount;
00071             if (pUseCount)
00072             {
00073                 ++(*pUseCount);
00074             }
00075             return *this;
00076         }
00077         virtual ~SharedPtr() {
00078             if(pUseCount)
00079             {
00080                 if (--(*pUseCount) == 0) {
00081                     destroy();
00082                 }
00083             }
00084         }
00085 
00086         virtual void destroy(void)
00087         {
00088             delete pRep;
00089             delete pUseCount;
00090         }
00091 
00092 
00093         inline T& operator*() const { assert(pRep); return *pRep; }
00094         inline T* operator->() const { assert(pRep); return pRep; }
00095         inline T* get() const { assert(pRep); return pRep; }
00096 
00101         void bind(T* rep) {
00102             assert(!pRep && !pUseCount);
00103             pUseCount = new unsigned int(1);
00104             pRep = rep;
00105         }
00106 
00107         inline bool unique() const { assert(pUseCount); return *pUseCount == 1; }
00108         inline unsigned int useCount() const { assert(pUseCount); return *pUseCount; }
00109 
00110         inline T* getPointer() { assert(pRep); return pRep; }
00111 
00112         inline bool isNull(void) const { return pRep == 0; }
00113     };
00114 
00115     template<class T, class U> inline bool operator==(SharedPtr<T> const& a, SharedPtr<U> const& b)
00116     {
00117         return a.get() == b.get();
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 }
00126 
00127 #endif

Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:28 2004