Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

hyp.std.smart_pointercom.h

00001 
00002 #ifndef _hyp_std_smart_pointercom_h_
00003 #define _hyp_std_smart_pointercom_h_
00004 
00005 #ifndef hyp_KERNEL_INSIDE
00006 #error Inclusion not allowed (include hyp.std.kernel.h instead)
00007 #endif
00008 
00009 //ajout 
00010 #undef TRACE0
00011 #define TRACE0( args )
00012 
00013 #undef ASSERT
00014 #define ASSERT( args ) hyp_ASSERT( args )
00015 //
00016 
00017 //#define Release DONT_CALL_RELEASE
00018 
00025 template <class I>
00026 class CSmartInterface
00027 {
00028 public:
00029    // Construction
00030    CSmartInterface(I* pI = NULL) ;
00031 
00032    // Copy Constructor
00033    CSmartInterface(const CSmartInterface<I>& rSI) ;
00034    
00035    // Destruction
00036    ~CSmartInterface() ;
00037 
00038    //
00039    // Assignment Operators
00040    //
00041 
00042    // from I*
00043    CSmartInterface<I>& operator=(I* pI) ;
00044 
00045    //
00046    // Operators
00047    //
00048 
00049    // Conversion
00050    operator I*() ;
00051 
00052    // Deref
00053    I* operator->() ;
00054 
00055    // Address of
00056    I** operator&() ;
00057 
00058    // Equality
00059    BOOL operator==(I* pI) const;
00060 
00061    // In-equality
00062    BOOL operator!=(I* pI) const;
00063 
00064    // Negation
00065    BOOL operator!() const ;
00066 
00067 #if 0
00068    //
00069    // Release
00070    //
00071    ULONG Release() ;
00072 #endif
00073 
00074 protected:
00075    I* m_pI ;
00076 };
00077 
00079 //
00080 // Inline Implementations
00081 //
00082 
00083 //
00084 // Constructor
00085 //
00086  
00087 template <class I> inline
00088 CSmartInterface<I>::CSmartInterface(I* pI /*=NULL*/)
00089    : m_pI(pI)
00090 {
00091    TRACE0("SmartInterface Ctor\r\n") ;
00092    if (m_pI != NULL) 
00093    {
00094       // AddRef if we are copying an existing interface pointer.
00095       m_pI->AddRef() ;
00096    }
00097 } 
00098 
00099 //
00100 // Copy Constructor
00101 //
00102 template <class I> inline
00103 CSmartInterface<I>::CSmartInterface(const CSmartInterface<I>& rSI)
00104 : m_pI(rSI.m_pI)
00105 {
00106         if (m_pI != NULL)
00107                 m_pI->AddRef() ;
00108 }
00109 
00110 //
00111 // Destructor
00112 //
00113 template <class I> inline 
00114 CSmartInterface<I>::~CSmartInterface()
00115 {
00116    TRACE0("SmartInterface Dtor\r\n") ;
00117    if (m_pI != NULL)
00118    {
00119       m_pI->Release();
00120    }
00121 }
00122 
00123 //
00124 // Assignment
00125 //
00126 template <class I> inline 
00127 CSmartInterface<I>& CSmartInterface<I>::operator=(I* pI)
00128 {
00129    TRACE0("Assignment from I*\r\n") ;
00130    if (m_pI != pI) //OPTOMIZE Only need to AddRef/Release if difference
00131    {
00132       if (m_pI != NULL)
00133          m_pI->Release() ; //Smart Pointers don't use smart pointers :-)
00134       
00135       m_pI = pI ;
00136 
00137       if (m_pI != NULL)
00138          m_pI->AddRef() ;
00139    }      
00140    return *this ;
00141 }
00142 
00143 
00144 
00145 //
00146 // Conversion Operator -  Needed for cases like ASSERT(pIDrawing)
00147 //
00148 template <class I> inline 
00149 CSmartInterface<I>::operator I*()
00150 { 
00151    TRACE0("Conversion operator\r\n") ;
00152    return m_pI;
00153 } 
00154 
00155 //
00156 // Dereference : operator ->
00157 //
00158 template <class I> inline 
00159 I* CSmartInterface<I>::operator->()
00160 {
00161    TRACE0("operartor ->\r\n") ;
00162    ASSERT(m_pI != NULL) ;
00163    return m_pI ;
00164 }
00165 
00166 //
00167 // Address : operator &
00168 //
00169 template <class I> inline 
00170 I** CSmartInterface<I>::operator&()
00171 {
00172    TRACE0("operator & \r\n") ;
00173    // The Address of a pointer is a pointer pointer
00174    return &m_pI ;
00175 }
00176 
00177 //
00178 // Equality : operator ==
00179 //
00180 template <class I> inline 
00181 BOOL CSmartInterface<I>::operator==(I* pI) const
00182 {
00183    TRACE0("operator ==\r\n") ;
00184    return (m_pI == pI) ;
00185 }
00186 
00187 //
00188 // In-equality : operator !=
00189 //
00190 template <class I> inline 
00191 BOOL CSmartInterface<I>::operator!=(I* pI) const
00192 {
00193    TRACE0("operator !=\r\n") ;
00194    return (m_pI != pI) ;
00195 }
00196 
00197 //
00198 // Negation : operator !
00199 //
00200 template <class I> inline 
00201 BOOL CSmartInterface<I>::operator!() const
00202 {
00203    TRACE0("operator ! \r\n") ;
00204    return !m_pI ;
00205 }
00206 
00207 //
00208 // Release
00209 //
00210 #if 0
00211 /*
00212 The following code doesn't prevent the user of the class
00213 from calling release! This can cause problems when porting
00214 old code!
00215 
00216 */
00217 template <class I> inline 
00218 ULONG CSmartInterface<I>::Release()
00219 {
00220    // New rule don't call release :-)
00221    TRACE0("Calling release shouldn't be needed????\r\n") ;
00222    m_pI->Release() ;
00223    m_pI = NULL ;
00224 }
00225 #endif // 0
00226 
00228 //
00229 //
00230 #if 0
00231 // from IUnknown
00232 template <class I> inline 
00233 CSmartInterface<I>& CSmartInterface<I>::operator=(IUnknown* pI) 
00234 {
00235    // If its an IUnknown or other interface pointer, 
00236    // we need to do a QI to see if our interface is supported...
00237    TRACE0("Assignment from IUnknown*\r\n") ;
00238    if (m_pI != pI) //OPTOMIZE Only need to AddRef/Release if difference
00239    {
00240       if (m_pI != NULL)
00241          m_pI->Release() ; //Smart Pointers don't use smart pointers :-)
00242       
00243       if (pIUnknown != NULL)
00244          pI->QueryInteface( , (void*)&
00245 
00246       if (m_pI != NULL)
00247          m_pI->AddRef() ;
00248    }      
00249    return *this ;
00250 }
00251 #endif
00252 
00253 #endif

Top of Page
written by Pierre Rebours © 2000-2001. Terms of Use.