Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

cxx_ptr.h

00001 #ifndef __UPF_CXX_PTR_H__
00002 #define __UPF_CXX_PTR_H__
00003 
00004 #include <assert.h>
00005 
00006 namespace upf
00007 {
00008 
00009 class IObject;
00010 
00011 // -----------------------------------------------------------------------------------
00012 // Smart pointer:
00013 // -----------------------------------------------------------------------------------
00014 
00015 
00048 template<class T> class Ptr
00049 {
00050 public:
00052     Ptr() : m_obj(NULL) {}
00053 
00055     Ptr(const Ptr& ptr)
00056     {
00057         m_obj = ptr.m_obj;
00058         if (m_obj) m_obj->incRef();
00059     }
00060 
00061 #if !defined(_MSC_VER) || _MSC_VER > 1200
00062 
00067     template<class O> Ptr(const Ptr<O>& ptr)
00068     {
00069         m_obj = upf::queryInterface<T>((O*)ptr);
00070         if (m_obj) m_obj->incRef();
00071     }
00072 #endif
00073 
00079     Ptr(IObject *obj)
00080     {
00081         m_obj = upf::queryInterface<T>(obj);
00082         if (m_obj) m_obj->incRef();
00083     }
00084 
00086     Ptr(T *obj)
00087     {
00088         m_obj = obj;
00089         if (m_obj) m_obj->incRef();
00090     }
00091 
00093     ~Ptr()
00094     {
00095         if (m_obj) m_obj->decRef();
00096     }
00097 
00099     T* operator->() const
00100     {
00101         assert(m_obj != NULL);
00102         return m_obj;
00103     }
00104 
00109     const Ptr& operator=(IObject *obj)
00110     { 
00111         if (m_obj == obj)
00112             return *this;
00113         T* oldobj = m_obj;
00114         m_obj = upf::queryInterface<T>(obj);
00115         if (m_obj) m_obj->incRef();
00116         if (oldobj) oldobj->decRef();
00117         return *this;
00118     }
00119 
00121     const Ptr& operator=(const Ptr& ptr)
00122     {
00123         return *this = ptr.m_obj;
00124     }
00125 
00126 #if !defined(_MSC_VER) || _MSC_VER > 1200
00127 
00131     template <class O> const Ptr& operator=(const Ptr<O>& ptr)
00132     {
00133         const Ptr& x = *this = (O*)ptr;
00134         return x;
00135     }
00136 #endif
00137 
00139     const Ptr& operator=(T *obj)
00140     { 
00141         if (m_obj == obj)
00142             return *this;
00143         if (m_obj) m_obj->decRef();
00144         m_obj = obj;
00145         if (m_obj) m_obj->incRef();
00146         return *this;
00147     }
00148 
00150     operator T*() const { return m_obj; }
00151 
00152 private:
00153     T *m_obj;
00154 };
00155 
00156 
00157 // NB: Ptr<IObject> needs a version without IObject operators: 
00158 template<> class Ptr<IObject>
00159 {
00160 public:
00161     Ptr() : m_obj(NULL) {}
00162 
00163     Ptr(const Ptr& ptr)
00164     {
00165         m_obj = ptr.m_obj;
00166         if (m_obj) m_obj->incRef();
00167     }
00168 
00169 #if !defined(_MSC_VER) || _MSC_VER > 1200
00170     template<class O> Ptr(const Ptr<O>& ptr)
00171     {
00172         m_obj = (O*)ptr;
00173         if (m_obj) m_obj->incRef();
00174     }
00175 #endif
00176 
00177     Ptr(IObject *obj)
00178     {
00179         m_obj = obj;
00180         if (m_obj) m_obj->incRef();
00181     }
00182 
00183     ~Ptr()
00184     {
00185         if (m_obj) m_obj->decRef();
00186     }
00187 
00188     IObject* operator->() const
00189     {
00190         assert(m_obj != NULL);
00191         return m_obj;
00192     }
00193 
00194     const Ptr& operator=(IObject *obj)
00195     { 
00196         if (m_obj == obj)
00197             return *this;
00198         if (m_obj) m_obj->decRef();
00199         m_obj = obj;
00200         if (m_obj) m_obj->incRef();
00201         return *this;
00202     }
00203 
00204     const Ptr& operator=(const Ptr& ptr)
00205     { 
00206         if (m_obj == ptr.m_obj)
00207             return *this;
00208         if (m_obj) m_obj->decRef();
00209         m_obj = ptr.m_obj;
00210         if (m_obj) m_obj->incRef();
00211         return *this;
00212     }
00213 
00214 #if !defined(_MSC_VER) || _MSC_VER > 1200
00215     template <class O> const Ptr& operator=(const Ptr<O>& ptr)
00216     {
00217         return *this = (O*)ptr;
00218     }
00219 #endif
00220 
00221     operator IObject*() const
00222         { return m_obj; }
00223 
00224 private:
00225     IObject *m_obj;
00226 };
00227 
00228 
00229 }
00230 
00231 #endif

Generated on Wed Jan 15 23:10:55 2003 for Universal Plugins Framework by doxygen1.2.18