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

hyp.std.smart_pointer.h

00001 /*
00002 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
00003  Hyperion Pattern.
00004  Hyperion Pattern is a part of the Hyperion Project.
00005 ----------------------------------------------------------
00006  Copyright (c) 2001 Pierre Rebours.
00007 
00008  This library is free software; you can redistribute it and/or
00009  modify it under the terms of the GNU Lesser General Public
00010  License as published by the Free Software Foundation; either
00011  version 2.1 of the License, or (at your option) any later version.
00012 
00013  This library is distributed in the hope that it will be useful,
00014  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  
00017  See hyp.license.txt file for more information about the license.
00018  
00019  Web: http://lagrandeporte.multimania.com/hyperion
00020  Email: amtycho@yahoo.fr
00021 ----------------------------------------------------------
00022  CVS
00023  $Log$
00024 
00025  Creation 05/07/2000 PR
00026  Update 28/07/00 CreateInstance added PR
00027  Acknowledgements Dale Rogerson
00028 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
00029 */
00030 
00031 
00032 #ifndef _hypstd_smart_pointeur_h_
00033 #define _hypstd_smart_pointeur_h_
00034 
00035 #ifndef hyp_KERNEL_INSIDE
00036 #error Inclusion not allowed (include hyp.kernel.h instead)
00037 #endif
00038 
00045 template<class T, const hypIID* piid>
00046 class IPtr
00047 {
00048         T* m_pI;        
00049 public:
00050         IPtr() :
00051         m_pI(0) {
00052         
00053         }
00054 
00055 
00056         IPtr(T* lp) :
00057         m_pI(lp) {
00058                 if(m_pI!=0) {
00059                         m_pI->AddRef();
00060                 }
00061         }
00062 
00063         IPtr(IUnknown* pI) :
00064         m_pI(0) {
00065                 m_pI=0;
00066                 if(pI!=0) {
00067                         pI->QueryInterface(*piid,(void**)&m_pI);
00068                 }
00069         }
00070 
00071         ~IPtr() {
00072                 Release();
00073         }
00074 
00076         void Release() {
00077                 if(m_pI!=0) {
00078                         T* pOld=m_pI;
00079                         m_pI=0;
00080                         pOld->Release();
00081                 }
00082         }
00083 
00084         inline operator T*() {
00085                 return m_pI;
00086         }
00087 
00088         inline operator T*() const {
00089                 return m_pI;
00090         }
00091 
00092         inline const T* get() const {
00093                 return m_pI;
00094         }
00095 
00096         inline T& operator*()   {
00097                 hyp_ASSERT(m_pI);
00098                 return *m_pI;
00099         }
00100         
00101         inline T** operator&() {
00102                 hyp_ASSERT(m_pI==0);
00103                 return &m_pI;
00104         }
00105         
00106         inline T* operator->() {
00107                 hyp_ASSERT(m_pI);
00108                 return m_pI;
00109         }
00110 
00112         T* operator=(T* pI)     {
00113                 if(m_pI!=pI) {
00114                         IUnknown* pOld=m_pI;
00115                         m_pI=pI;
00116 
00117                         if(m_pI!=0) {
00118                                 m_pI->AddRef();
00119                         }
00120                         if(pOld!=0) {
00121                                 pOld->Release();
00122                         }
00123                 }
00124                 return m_pI;
00125         }
00126 
00128         T* operator=(IUnknown* pI) {
00129                 IUnknown* pOld=m_pI;
00130                 m_pI=0;
00131 
00132                 if(pI!=0) {
00133                         com_result hr=pI->QueryInterface(*piid,(void**)&m_pI);
00134                         if(hr!=com_ok) {
00135                                 m_pI=0;
00136                                 //FIXTODO : lancement d'exeption????
00137                                 hyp_ASSERT(0);
00138                         }
00139                 }
00140 
00141                 if(pOld!=0) {
00142                         pOld->Release();
00143                 }
00144                 return m_pI;
00145         }
00146 
00147         inline bool operator!() {
00148                 return (m_pI==0)? true:false;
00149         }
00150 
00151         inline const hypIID& iid() {
00152                 return *piid;
00153         }
00154 
00156         void CreateInstance(const hypCLSID& clsid,IUnknown* pI,long clsctx = INPROC_SERVER) {
00157                 Release();
00158                 if(Failed(ker::CreateInstance(clsid,pI,clsctx,*piid,(void**)&m_pI))) {
00159                         hyp_THROW_COM(com_false);
00160                 }
00161         }
00162 };
00163 
00164 
00165 
00170 class IPtrUnknown
00171 {
00172         IUnknown* m_pI;
00173 public:
00174         IPtrUnknown() :
00175         m_pI(0) {
00176                 
00177         }
00178 
00179         IPtrUnknown(IUnknown* lp) {
00180                 m_pI=lp;
00181                 if(m_pI!=0) {
00182                         m_pI->AddRef();
00183                 }
00184         }
00185 
00186         ~IPtrUnknown() { 
00187                 Release();
00188         }
00189 
00190         void Release() {
00191                 if (m_pI) {
00192                         IUnknown* pOld = m_pI ;
00193                         m_pI = 0;
00194                         pOld->Release() ;
00195                 }
00196         }
00197 
00198         operator IUnknown*(){
00199                 return static_cast<IUnknown*>(m_pI);
00200         }
00201 
00202         IUnknown& operator*() {
00203                 hyp_ASSERT(m_pI);
00204                 return *m_pI;
00205         }
00206 
00207         IUnknown** operator&() {
00208                 hyp_ASSERT(m_pI==0);
00209                 return &m_pI;
00210         }
00211         
00212         IUnknown* operator->() {
00213                 hyp_ASSERT(m_pI);
00214                 return m_pI;
00215         }
00216 
00217         IUnknown* operator=(IUnknown* pI) {             
00218                 if(m_pI!=pI) {
00219                         IUnknown* pOld=m_pI;
00220                         m_pI=pI;
00221                         if(m_pI!=0) {
00222                                 m_pI->AddRef();
00223                         }
00224                         if(pOld!=0) {
00225                                 pOld->Release() ;
00226                         }
00227                 }
00228                 return m_pI;
00229         }
00230 
00231         bool operator!() {
00232                 return (m_pI==0) ? true : false;
00233         }
00234 
00235         inline const hypIID& iid() {
00236                 return IID_hypIUnknown;
00237         }
00238 
00239         com_result CreateInstance(const hypCLSID& clsid,IUnknown* pI,long clsctx = INPROC_SERVER) {
00240                 Release();
00241                 return ker::CreateInstance(clsid,pI,clsctx,IID_hypIUnknown,(void**)&m_pI);
00242         }
00243 };
00244 
00245 
00250 template <class I, const hypGUID* pGUID>
00251 I* interface_cast(IUnknown* pIUnknown)
00252 {
00253         I* pI=0;
00254         com_result hr=pIUnknown->QueryInterface(*pGUID,(void**)&pI);
00255         hyp_ASSERT(hr==com_ok);
00256         return pI;
00257 }
00258 
00259 
00260 #endif  

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