00001 /* NewNet - A networking framework in C++ 00002 Copyright (C) 2006 Ingmar K. Steen (iksteen@gmail.com) 00003 00004 This program is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; if not, write to the Free Software 00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 */ 00019 00020 #ifndef NEWNET_REFPTR_H 00021 #define NEWNET_REFPTR_H 00022 00023 #include "nnbaseptr.h" 00024 00025 namespace NewNet 00026 { 00027 class Object; 00028 00030 00033 template<class T> class RefPtr : public BasePtr<T> 00034 { 00035 public: 00037 00038 RefPtr() : BasePtr<T>() 00039 { 00040 } 00041 00043 00044 RefPtr(T * t) : BasePtr<T>(t) 00045 { 00046 if(t) 00047 ++(t->refCounter()); 00048 } 00049 00051 00052 RefPtr(const RefPtr& t) : BasePtr<T>() 00053 { 00054 BasePtr<T>::m_Ptr = t.m_Ptr; 00055 if(t.m_Ptr) 00056 ++(t.m_Ptr->refCounter()); 00057 } 00058 00060 00061 RefPtr& operator=(const RefPtr& t) 00062 { 00063 if(BasePtr<T>::m_Ptr == t.m_Ptr) 00064 return *this; 00065 if(BasePtr<T>::m_Ptr) 00066 { 00067 if(--(BasePtr<T>::m_Ptr->refCounter())) 00068 delete BasePtr<T>::m_Ptr; 00069 } 00070 BasePtr<T>::m_Ptr = t.m_Ptr; 00071 if(t.m_Ptr) 00072 { 00073 ++(t.m_Ptr->refCounter()); 00074 } 00075 return *this; 00076 } 00077 00079 00080 RefPtr& operator=(T * t) 00081 { 00082 if(BasePtr<T>::m_Ptr == t) 00083 return *this; 00084 if(BasePtr<T>::m_Ptr) 00085 { 00086 if(--(BasePtr<T>::m_Ptr->refCounter())) 00087 delete BasePtr<T>::m_Ptr; 00088 } 00089 BasePtr<T>::m_Ptr = t; 00090 if(t) 00091 ++(t->refCounter()); 00092 return *this; 00093 } 00094 00096 00098 ~RefPtr() 00099 { 00100 if(BasePtr<T>::m_Ptr) 00101 { 00102 if(--(BasePtr<T>::m_Ptr->refCounter())) 00103 delete BasePtr<T>::m_Ptr; 00104 } 00105 } 00106 }; 00107 } 00108 00109 #endif // NEWNET_REFPTR_H
1.5.1