Crazy Eddies GUI System: CEGUIRefPtr.h Source File

- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages

# CEGUIRefPtr.h

    00001 /************************************************************************
    00002         filename:       CEGUIRefPtr.h
    00003         created:        15/10/2004
    00004         author:         Gerald Lindsly
    00005 *************************************************************************/
    00006 /*************************************************************************
    00007     Crazy Eddie's GUI System (http://www.cegui.org.uk)
    00008     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
    00009
    00010     This library is free software; you can redistribute it and/or
    00011     modify it under the terms of the GNU Lesser General Public
    00012     License as published by the Free Software Foundation; either
    00013     version 2.1 of the License, or (at your option) any later version.
    00014
    00015     This library is distributed in the hope that it will be useful,
    00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00018     Lesser General Public License for more details.
    00019
    00020     You should have received a copy of the GNU Lesser General Public
    00021     License along with this library; if not, write to the Free Software
    00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00023 *************************************************************************/
    00024 #ifndef _CEGUIRefPtr_h_
    00025 #define _CEGUIRefPtr_h_
    00026
    00027 #include "CEGUIBase.h"
    00028
    00029 namespace CEGUI {
    00030
    00031 class CEGUIEXPORT Referenced
    00032 {
    00033 public:
    00034   Referenced() : d_refCount(0) {}
    00035   Referenced(const Referenced&) : d_refCount(0) {}
    00036
    00037   Referenced& operator=(Referenced&) { return *this; }
    00038
    00039   /* Increment the reference count by one, indicating that
    00040       a pointer to this object is referencing it. */
    00041   void addRef() const { ++d_refCount; }
    00042
    00043   /* Decrement the reference count by one, indicating that
    00044      a pointer to this object is no longer referencing it.
    00045      if the count drops to zero, this is deleted. */
    00046   void release() const { if (!--d_refCount) delete this; }
    00047
    00048   /* Decrement the reference count by one, indicating that
    00049       a pointer to this object is no longer referencing it;
    00050       however, do not delete it, even if ref count goes to 0. */
    00051   void releaseButKeep() const { --d_refCount; }
    00052
    00054   int refCount() const { return d_refCount; }
    00055
    00056 protected:
    00057   virtual ~Referenced();
    00058   mutable int d_refCount;
    00059 };
    00060
    00061
    00062 template<class T> class RefPtr
    00063 {
    00064   T* d_p;
    00065
    00066 public:
    00067 //  RefPtr()     : d_p(0) {}
    00068
    00069   RefPtr()     : d_p(new T()) { d_p->addRef(); }
    00070
    00071   RefPtr(T* t) : d_p(t) { if (t)   d_p->addRef(); }
    00072   RefPtr(const RefPtr& r) : d_p(r.d_p)
    00073                         { if (r.d_p) d_p->addRef(); }
    00074   ~RefPtr()
    00075   { if (d_p) {
    00076       d_p->release();
    00077       d_p = 0;
    00078     }
    00079   }
    00080
    00081   RefPtr& operator=(T* q)
    00082   {
    00083     if (d_p != q) {
    00084       T* t = d_p;
    00085       d_p = q;
    00086       if (q) q->addRef();
    00087       if (t) t->release();
    00088     }
    00089     return *this;
    00090   }
    00091
    00092   RefPtr& operator=(const RefPtr& r) { return *this = r.d_p; }
    00093
    00094   bool operator==(const RefPtr& r) const { return d_p == r.d_p; }
    00095   bool operator!=(const RefPtr& r) const { return d_p != r.d_p; }
    00096   bool operator< (const RefPtr& r) const { return d_p <  r.d_p; }
    00097   bool operator> (const RefPtr& r) const { return d_p >  r.d_p; }
    00098
    00099   bool operator ==(const T* q) const { return d_p == q; }
    00100   bool operator !=(const T* q) const { return d_p != q; }
    00101   bool operator < (const T* q) const { return d_p <  q; }
    00102   bool operator > (const T* q) const { return d_p >  q; }
    00103
    00104
    00105         T& operator*()        { return *d_p; }
    00106   const T& operator*() const  { return *d_p; }
    00107
    00108         T* operator->()       { return d_p; }
    00109   const T* operator->() const { return d_p; }
    00110
    00111         T* get()              { return d_p; }
    00112   const T* get() const        { return d_p; }
    00113
    00114   bool operator!() const            { return d_p == 0; }
    00115   bool valid() const            { return d_p != 0; }
    00116
    00117   T* release()
    00118   {
    00119     T* t = d_p;
    00120     if (d_p) {
    00121       d_p->releaseButKeep();
    00122       d_p = 0;
    00123     }
    00124     return t;
    00125   }
    00126 };
    00127
    00128 } // end namespace CEGUI
    00129
    00130 #endif