ksharedptr.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef KSharedPTR_H
00019 #define KSharedPTR_H
00020
00021 #include "kdelibs_export.h"
00022
00041 class KDECORE_EXPORT KShared {
00042 public:
00047 KShared() : count(0) { }
00048
00053 KShared( const KShared & ) : count(0) { }
00054
00058 KShared &operator=(const KShared & ) { return *this; }
00059
00063 void _KShared_ref() const { count++; }
00064
00069 void _KShared_unref() const { if (!--count) delete this; }
00070
00076 int _KShared_count() const { return count; }
00077
00078 protected:
00079 virtual ~KShared() { }
00080 private:
00081 mutable int count;
00082 };
00083
00101 template< class T >
00102 class KSharedPtr
00103 {
00104 public:
00108 KSharedPtr()
00109 : ptr(0) { }
00114 KSharedPtr( T* t )
00115 : ptr(t) { if ( ptr ) ptr->_KShared_ref(); }
00116
00121 KSharedPtr( const KSharedPtr& p )
00122 : ptr(p.ptr) { if ( ptr ) ptr->_KShared_ref(); }
00123
00128 ~KSharedPtr() { if ( ptr ) ptr->_KShared_unref(); }
00129
00130 KSharedPtr<T>& operator= ( const KSharedPtr<T>& p ) {
00131 if ( ptr == p.ptr ) return *this;
00132 if ( ptr ) ptr->_KShared_unref();
00133 ptr = p.ptr;
00134 if ( ptr ) ptr->_KShared_ref();
00135 return *this;
00136 }
00137 KSharedPtr<T>& operator= ( T* p ) {
00138 if ( ptr == p ) return *this;
00139 if ( ptr ) ptr->_KShared_unref();
00140 ptr = p;
00141 if ( ptr ) ptr->_KShared_ref();
00142 return *this;
00143 }
00144 bool operator== ( const KSharedPtr<T>& p ) const { return ( ptr == p.ptr ); }
00145 bool operator!= ( const KSharedPtr<T>& p ) const { return ( ptr != p.ptr ); }
00146 bool operator== ( const T* p ) const { return ( ptr == p ); }
00147 bool operator!= ( const T* p ) const { return ( ptr != p ); }
00148 bool operator!() const { return ( ptr == 0 ); }
00149 operator T*() const { return ptr; }
00150
00155 T* data() { return ptr; }
00156
00161 const T* data() const { return ptr; }
00162
00163 const T& operator*() const { return *ptr; }
00164 T& operator*() { return *ptr; }
00165 const T* operator->() const { return ptr; }
00166 T* operator->() { return ptr; }
00167
00172 int count() const { return ptr->_KShared_count(); }
00173 private:
00174 T* ptr;
00175 };
00176
00177 #endif
This file is part of the documentation for kdecore Library Version 3.3.90.