- Your Widget Set For OpenGL
Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | Related Pages

usharedptr.hpp

00001 /*************************************************************************** 00002 LibUFO - UI For OpenGL 00003 copyright : (C) 2001-2005 by Johannes Schmidt 00004 email : schmidtjf at users.sourceforge.net 00005 ------------------- 00006 00007 file : include/ufo/usharedptr.hpp 00008 begin : Thu Aug 22 2002 00009 $Id: usharedptr.hpp,v 1.9 2005/07/05 12:05:44 schmidtjf Exp $ 00010 ***************************************************************************/ 00011 00012 /*************************************************************************** 00013 * This library is free software; you can redistribute it and/or * 00014 * modify it under the terms of the GNU Lesser General Public * 00015 * License as published by the Free Software Foundation; either * 00016 * version 2.1 of the License, or (at your option) any later version. * 00017 * * 00018 * This library is distributed in the hope that it will be useful, * 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00021 * Lesser General Public License for more details. * 00022 * * 00023 * You should have received a copy of the GNU Lesser General Public * 00024 * License along with this library; if not, write to the Free Software * 00025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 00026 ***************************************************************************/ 00027 00028 #ifndef USHAREDPTR_HPP 00029 #define USHAREDPTR_HPP 00030 00031 #include "config/ufo_config.hpp" 00032 00033 namespace ufo { 00034 00040 template<typename T> 00041 class USharedPtr { 00042 private: 00043 T * m_object; // shared object 00044 int * m_refCount; // shared counter 00045 00046 public: 00047 explicit USharedPtr() : m_object(NULL), m_refCount(NULL) {} 00048 00049 explicit USharedPtr(const USharedPtr<T> & right) { 00050 if (right.m_object) { 00051 m_object = right.m_object; 00052 m_refCount = right.m_refCount; 00053 ++*m_refCount; 00054 } else { 00055 m_object = NULL; 00056 m_refCount = NULL; 00057 } 00058 } 00059 00060 explicit USharedPtr(T * right) : m_object(NULL), m_refCount(NULL) { 00061 assign(right); 00062 } 00063 /* 00064 explicit USharedPtr(const T * right) : m_object(NULL), m_refCount(NULL) { 00065 assign(const_cast<T*>(right)); 00066 } 00067 */ 00068 ~USharedPtr() { 00069 release(); 00070 } 00071 00072 USharedPtr<T>& operator =(const USharedPtr<T>& right) { 00073 if (right.m_object) { 00074 ++*right.m_refCount; 00075 } 00076 00077 release(); 00078 00079 m_object = right.m_object; 00080 m_refCount = right.m_refCount; 00081 00082 return *this; 00083 } 00084 00085 USharedPtr<T>& operator =(T* right) { 00086 assign(right); 00087 00088 return *this; 00089 } 00090 /* 00091 USharedPtr<T>& operator =(const T* right) { 00092 assign(const_cast<T*>(right)); 00093 00094 return *this; 00095 } 00096 */ 00097 bool valid() { 00098 return (m_object != NULL); 00099 } 00100 bool operator()() const { 00101 return (m_object != NULL); 00102 } 00103 bool operator!() const { 00104 return (m_object == NULL); 00105 } 00106 00107 T * operator ->() const { 00108 return m_object; 00109 } 00110 00111 T & operator *() const { 00112 return *m_object; 00113 } 00114 00115 operator T*() const { 00116 return m_object; 00117 } 00118 /* msvc6 don't like this for abstract classes 00119 operator T() const { 00120 return *m_object; 00121 } 00122 */ 00123 int refCount() { 00124 return *m_refCount; 00125 } 00126 private: // Private methods 00127 void assign(T * t) { 00128 if (t == m_object) { return; } 00129 00130 release(); 00131 if (t) { 00132 m_object = t; 00133 m_refCount = new int; 00134 *m_refCount = 1; 00135 } 00136 } 00137 void release() { 00138 if (m_object) { 00139 if (*m_refCount == 1) { 00140 delete m_refCount; 00141 delete m_object; 00142 } else { 00143 --*m_refCount; 00144 } 00145 m_object = NULL; 00146 m_refCount = NULL; 00147 } 00148 } 00149 }; 00150 00151 } // namespace ufo 00152 00153 #endif // USHAREDPTR_HPP

The libUFO Project - written by Johannes Schmidt