Horus Doc || C++ Reference || Class Overview   Pixels   Images   Detector   Geometry   Registry || Doxygen's quick Index  

HxRcPtr.h

00001 /*
00002  *  Copyright (c) 1998, University of Amsterdam, The Netherlands.
00003  *  All rights reserved.
00004  *
00005  *
00006  *  Author(s):
00007  *  Dennis Koelma (koelma@wins.uva.nl)
00008  *  Edo Poll (poll@wins.uva.nl)
00009  */
00010 
00011 #ifndef HxRcPtr_h
00012 #define HxRcPtr_h
00013 
00014 
00018 template<class T>
00019 class HxRcPtr
00020 {
00021 public:
00022                 HxRcPtr(const T* ptr = 0);
00023                 HxRcPtr(const HxRcPtr& rhs);
00024                 ~HxRcPtr();
00025 
00026     HxRcPtr&    operator=(const HxRcPtr& rhs);
00027     HxRcPtr&    operator=(T* rhs);
00028 
00029     T*          operator->() const;
00030     T&          operator*() const;
00031 
00032                 operator int() const;
00033 
00034     T*          pointee() const;
00035 
00036     int         isShared() const;
00037     void        getUnshared();
00038 
00039     int         refCnt() const;
00040 private:
00041     T*          _pointee;
00042 };
00043 
00044 template<class T>
00045 inline
00046 HxRcPtr<T>::HxRcPtr(const T* ptr) : _pointee((T*)ptr)
00047 {
00048     if (_pointee)
00049         _pointee->addRef();
00050 }
00051 
00052 template<class T>
00053 inline
00054 HxRcPtr<T>::HxRcPtr(const HxRcPtr& rhs) : _pointee(rhs._pointee)
00055 {
00056     if (_pointee) 
00057         _pointee->addRef();
00058 }
00059 
00060 template<class T>
00061 inline
00062 HxRcPtr<T>::~HxRcPtr()
00063 { 
00064     if (_pointee) 
00065         _pointee->removeRef(); 
00066 }
00067 
00068 template<class T>
00069 inline HxRcPtr<T>&
00070 HxRcPtr<T>::operator=(const HxRcPtr& rhs)
00071 {
00072     if (rhs._pointee)
00073         rhs._pointee->addRef();
00074     if (_pointee) 
00075         _pointee->removeRef();
00076     _pointee = rhs._pointee;
00077     return *this;
00078 }
00079 
00080 template<class T>
00081 inline HxRcPtr<T>&
00082 HxRcPtr<T>::operator=(T* rhs)
00083 {
00084     if (rhs)
00085         rhs->addRef();
00086     if (_pointee) 
00087         _pointee->removeRef();
00088     _pointee = rhs;
00089     return *this;
00090 }
00091 
00092 template<class T>
00093 inline
00094 HxRcPtr<T>::operator int() const
00095 {
00096     return _pointee ? 1 : 0; 
00097 }
00098 
00099 template<class T>
00100 inline T*
00101 HxRcPtr<T>::pointee() const
00102 { 
00103     return _pointee; 
00104 }
00105 
00106 template<class T>
00107 inline int
00108 HxRcPtr<T>::isShared() const
00109 {
00110     return _pointee ? _pointee->isShared() : 0;
00111 }
00112 
00113 template<class T>
00114 inline void
00115 HxRcPtr<T>::getUnshared()
00116 {
00117     if (_pointee && _pointee->isShared())
00118         _pointee = (T*)_pointee->getUnshared();
00119 }
00120 
00121 template<class T>
00122 inline T*
00123 HxRcPtr<T>::operator->() const
00124 { 
00125     return _pointee; 
00126 }
00127 
00128 template<class T>
00129 inline T&
00130 HxRcPtr<T>::operator*() const
00131 { 
00132     return *_pointee; 
00133 }
00134 
00135 template<class T>
00136 inline int
00137 HxRcPtr<T>::refCnt() const
00138 {
00139     return _pointee->refCnt();
00140 }
00141 
00142 #endif

Generated on Tue Feb 3 14:18:41 2004 for C++Reference by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001