Crazy Eddies GUI System  0.7.6
CEGUIRefCounted.h
00001 /************************************************************************
00002     filename:   CEGUIRefCounted.h
00003     created:    Wed Mar 1 2006
00004     author:     Paul D Turner <paul@cegui.org.uk>
00005 *************************************************************************/
00006 /***************************************************************************
00007  *   Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
00008  *
00009  *   Permission is hereby granted, free of charge, to any person obtaining
00010  *   a copy of this software and associated documentation files (the
00011  *   "Software"), to deal in the Software without restriction, including
00012  *   without limitation the rights to use, copy, modify, merge, publish,
00013  *   distribute, sublicense, and/or sell copies of the Software, and to
00014  *   permit persons to whom the Software is furnished to do so, subject to
00015  *   the following conditions:
00016  *
00017  *   The above copyright notice and this permission notice shall be
00018  *   included in all copies or substantial portions of the Software.
00019  *
00020  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00021  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00022  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00023  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
00024  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00025  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00026  *   OTHER DEALINGS IN THE SOFTWARE.
00027  ***************************************************************************/
00028 #ifndef _CEGUIRefCounted_h_
00029 #define _CEGUIRefCounted_h_
00030 
00031 // Start of CEGUI namespace section
00032 namespace CEGUI
00033 {
00039 template<typename T>
00040 class RefCounted
00041 {
00042 public:
00047     RefCounted() :
00048         d_object(0),
00049         d_count(0)
00050     {
00051     }
00052 
00057     RefCounted(T* ob) :
00058         d_object(ob),
00059         d_count((ob != 0) ? new unsigned int(1) : 0)
00060     {
00061     }
00062 
00067     RefCounted(const RefCounted<T>& other) :
00068         d_object(other.d_object),
00069         d_count(other.d_count)
00070     {
00071         if (d_count)
00072             addRef();
00073     }
00074 
00080     ~RefCounted()
00081     {
00082         if (d_object)
00083             release();
00084     }
00085 
00092     RefCounted<T>& operator=(const RefCounted<T>& other)
00093     {
00094         if (*this != other)
00095         {
00096             if (d_object)
00097                 release();
00098 
00099             d_object = other.d_object;
00100             d_count = d_object ? other.d_count : 0;
00101 
00102             if (d_count)
00103                 addRef();
00104         }
00105 
00106         return *this;
00107     }
00108 
00114     bool operator==(const RefCounted<T>& other) const
00115     {
00116         return d_object == other.d_object;
00117     }
00118 
00124     bool operator!=(const RefCounted<T>& other) const
00125     {
00126         return d_object != other.d_object;
00127     }
00128 
00134     const T& operator*() const
00135     {
00136         return *d_object;
00137     }
00138 
00139     T& operator*()
00140     {
00141         return *d_object;
00142     }
00143 
00148     const T* operator->() const
00149     {
00150         return d_object;
00151     }
00152 
00153     T* operator->()
00154     {
00155         return d_object;
00156     }
00157 
00162     bool isValid() const
00163     {
00164         return d_object != 0;
00165     }
00166 
00167 private:
00172     void addRef()
00173     {
00174         ++*d_count;
00175     }
00176 
00182     void release()
00183     {
00184         if (!--*d_count)
00185         {
00186             delete d_object;
00187             delete d_count;
00188             d_object = 0;
00189             d_count = 0;
00190         }
00191     }
00192 
00193     T* d_object;            
00194     unsigned int* d_count;  
00195 };
00196 
00197 } // End of  CEGUI namespace section
00198 
00199 #endif  // end of guard _CEGUIRefCounted_h_