00001 /* This file is part of the KDE libraries 00002 Copyright (c) 1999 Waldo Bastian <bastian@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00016 Boston, MA 02111-1307, USA. 00017 */ 00018 #ifndef KSharedPTR_H 00019 #define KSharedPTR_H 00020 00039 class KShared { 00040 public: 00045 KShared() : count(0) { } 00046 00051 KShared( const KShared & ) : count(0) { } 00052 00056 KShared &operator=(const KShared & ) { return *this; } 00057 00061 void _KShared_ref() { count++; } 00062 00067 void _KShared_unref() { if (!--count) delete this; } 00068 00074 int _KShared_count() { return count; } 00075 00076 protected: 00077 virtual ~KShared() { } 00078 int count; // ### KDE 3.0: rename to something like _KShared_count 00079 // or make private 00080 }; 00081 00095 template< class T > 00096 struct KSharedPtr 00097 { 00098 public: 00099 KSharedPtr() 00100 : ptr(0) { } 00101 KSharedPtr( T* t ) 00102 : ptr(t) { if ( ptr ) ptr->_KShared_ref(); } 00103 KSharedPtr( const KSharedPtr& p ) 00104 : ptr(p.ptr) { if ( ptr ) ptr->_KShared_ref(); } 00105 00106 ~KSharedPtr() { if ( ptr ) ptr->_KShared_unref(); } 00107 00108 KSharedPtr<T>& operator= ( const KSharedPtr<T>& p ) { 00109 if ( ptr == p.ptr ) return *this; 00110 if ( ptr ) ptr->_KShared_unref(); 00111 ptr = p.ptr; 00112 if ( ptr ) ptr->_KShared_ref(); 00113 return *this; 00114 } 00115 KSharedPtr<T>& operator= ( T* p ) { 00116 if ( ptr == p ) return *this; 00117 if ( ptr ) ptr->_KShared_unref(); 00118 ptr = p; 00119 if ( ptr ) ptr->_KShared_ref(); 00120 return *this; 00121 } 00122 bool operator== ( const KSharedPtr<T>& p ) const { return ( ptr == p.ptr ); } 00123 bool operator!= ( const KSharedPtr<T>& p ) const { return ( ptr != p.ptr ); } 00124 bool operator== ( const T* p ) const { return ( ptr == p ); } 00125 bool operator!= ( const T* p ) const { return ( ptr != p ); } 00126 bool operator!() const { return ( ptr == 0 ); } 00127 operator T*() const { return ptr; } 00128 00129 T* data() { return ptr; } 00130 const T* data() const { return ptr; } 00131 00132 const T& operator*() const { return *ptr; } 00133 T& operator*() { return *ptr; } 00134 const T* operator->() const { return ptr; } 00135 T* operator->() { return ptr; } 00136 00137 int count() const { return ptr->_KShared_count(); } // for debugging purposes 00138 private: 00139 T* ptr; 00140 }; 00141 00142 #endif
1.4.2