GME  13
MgaMap.h
Go to the documentation of this file.
00001 // MgaMap.h: interface for the CMgaMap class.
00002 //
00004 
00005 #if !defined(AFX_MGAMAP_H__4FB2B383_49D6_4C83_AB80_63DFF3C45380__INCLUDED_)
00006 #define AFX_MGAMAP_H__4FB2B383_49D6_4C83_AB80_63DFF3C45380__INCLUDED_
00007 
00008 #if _MSC_VER > 1000
00009 #pragma once
00010 #endif // _MSC_VER > 1000
00011 
00012 
00013 #include "MgaObjectProxy.h"
00014 #include "AfxColl.h"
00015 
00016 
00017 
00018 template<class ObjectProxy>
00019 class CMgaMap
00020 {
00021         class CMgaObjectProxyMapItem : public ObjectProxy
00022         {
00023         public:
00024                 CMgaObjectProxyMapItem(CMgaObjectProxy& rSourceMgaObjectProxy, HTREEITEM hTreeItem) : ObjectProxy(rSourceMgaObjectProxy),m_hTreeItem(hTreeItem) {};
00025                 virtual ~CMgaObjectProxyMapItem() {}
00026                 HTREEITEM m_hTreeItem;
00027         };
00028 public:
00029         typedef CMap<HTREEITEM,HTREEITEM,LPUNKNOWN,LPUNKNOWN> CMapItem2Object;
00030         typedef CMap<LPUNKNOWN,LPUNKNOWN,CMgaObjectProxyMapItem*,CMgaObjectProxyMapItem*> CMapObject2Item;
00031 
00032         CMgaMap() {}
00033 
00034         virtual ~CMgaMap() {
00035                 DestroyMap();
00036         }
00037 
00038         void DestroyMap()
00039         {
00040                 // Remove elements from object map and delete the object
00041                 POSITION pos=m_MapObject2Item.GetStartPosition();
00042                 CMgaObjectProxyMapItem* pObject;
00043                 LPUNKNOWN pUnknown;
00044 
00045                 // Remove the TreeItem handles and LPUNKNOWNs from the item map
00046                 m_MapItem2Object.RemoveAll();
00047 
00048 
00049                 while(pos!=NULL)
00050                 {
00051                         m_MapObject2Item.GetNextAssoc(pos,pUnknown,pObject);
00052                         m_MapObject2Item.RemoveKey(pUnknown);
00053                         delete pObject;
00054                         pUnknown->Release();
00055                 }
00056 
00057         }
00058 
00059         // Retreives the corresponding TreeItem handle from an Object Proxy
00060         BOOL LookupTreeItem(CMgaObjectProxy MgaObjectProxy, HTREEITEM &hTreeItem)
00061         {
00062                 return LookupTreeItem(MgaObjectProxy.m_pMgaObject, hTreeItem);
00063         }
00064 
00065 
00066         // Retreives the Object Proxy from the corresponding TreeItem handle
00067         BOOL LookupObjectProxy(HTREEITEM hTreeItem, ObjectProxy& rObjectProxy)
00068         {
00069                 CMgaObjectProxyMapItem* pMgaObjectProxyMapItem;
00070                 LPUNKNOWN pUnknown;
00071         
00072                 BOOL bResult=m_MapItem2Object.Lookup(hTreeItem,pUnknown);
00073         
00074                 if(bResult)
00075                 {
00076                         if( (bResult=m_MapObject2Item.Lookup(pUnknown,pMgaObjectProxyMapItem)) )
00077                         {
00078                                 rObjectProxy=*pMgaObjectProxyMapItem;
00079                         }
00080                 }
00081                 return  bResult;
00082         }
00083 
00084 
00085         // Removes a tree item maintaining the consistency between the two maps
00086         BOOL RemoveTreeItem(HTREEITEM hTreeItem)
00087         {
00088                 LPUNKNOWN pUnknown;
00089 
00090                 if(!m_MapItem2Object.Lookup(hTreeItem,pUnknown))
00091                 {
00092                         return FALSE;
00093                 }
00094                 else
00095                 {
00096                         m_MapItem2Object.RemoveKey(hTreeItem);
00097 
00098                         CMgaObjectProxyMapItem* pObjectProxyMapItem;
00099                         if(m_MapObject2Item.Lookup(pUnknown,pObjectProxyMapItem))
00100                         {
00101                                 m_MapObject2Item.RemoveKey(pUnknown);                   
00102                                 // Free COM object
00103                                 pUnknown->Release();
00104                                 delete pObjectProxyMapItem;
00105                         }
00106                         return TRUE;
00107                 }
00108 
00109         }
00110 
00111 
00112         // Removes an Object Proxy maintaining the consistency between the two maps
00113         BOOL RemoveObjectProxy(CMgaObjectProxy MgaObjectProxy)
00114         {       
00115                 CMgaObjectProxyMapItem* pObjectProxyMapItem;
00116 
00117                 CComPtr<IUnknown> pUnk;
00118                 MgaObjectProxy.m_pMgaObject->QueryInterface(IID_IUnknown, (void**)&pUnk);
00119                 if(!m_MapObject2Item.Lookup(pUnk, pObjectProxyMapItem))
00120                 {
00121                         return FALSE;
00122                 }
00123                 else
00124                 {
00125                         m_MapObject2Item.RemoveKey(pObjectProxyMapItem->m_pMgaObject);
00126                         m_MapItem2Object.RemoveKey(pObjectProxyMapItem->m_hTreeItem);
00127                 
00128                         // Free COM object
00129                         pObjectProxyMapItem->m_pMgaObject->Release();
00130                         delete pObjectProxyMapItem;
00131                         return TRUE;
00132                 }
00133         }
00134 
00135 
00136         // Adds a new TreeItem Handle - ObjectProxy pair to the map
00137         ObjectProxy& AddEntry(HTREEITEM hTreeItem, ObjectProxy MgaObjectProxy)
00138         {
00139 
00140         // Removing the elements from the maps with the keys, because CMap::SetAt would overwrite
00141         // the value belonging to the already existing key 
00142         // resulting an inconsistency with the other map. 
00143                 RemoveTreeItem(hTreeItem);
00144                 RemoveObjectProxy(MgaObjectProxy);
00145         
00146                 // Increase reference counter for this map
00147                 IUnknown* pUnk = MgaObjectProxy.m_pMgaObject;
00148                 MgaObjectProxy.m_pMgaObject = NULL;
00149                 pUnk->QueryInterface(IID_IUnknown, (void**)&MgaObjectProxy.m_pMgaObject);
00150 
00151                 m_MapItem2Object.SetAt(hTreeItem,MgaObjectProxy.m_pMgaObject);
00152 
00153                 CMgaObjectProxyMapItem* pMgaObjectProxyMapItem= new CMgaObjectProxyMapItem(MgaObjectProxy, hTreeItem);
00154                 m_MapObject2Item.SetAt(MgaObjectProxy.m_pMgaObject,pMgaObjectProxyMapItem);
00155 
00156                 return *pMgaObjectProxyMapItem;
00157         }
00158 
00159 
00160         // Low level function for efficiency: if one wants IUnknown pointer
00161         // he can get it with one lookup call
00162         BOOL LookupObjectUnknown(HTREEITEM hTreeItem, LPUNKNOWN &pUnknown)
00163         {
00164 
00165                 return m_MapItem2Object.Lookup(hTreeItem,pUnknown);
00166         }
00167 
00168 
00169         _COM_SMARTPTR_TYPEDEF(IMgaObject, __uuidof(IMgaObject));
00170 
00171         BOOL SearchTreeItem(BSTR Id, HTREEITEM &hTreeItem, IUnknown* &punk)
00172         {
00173                 // read all looking for the ID
00174 
00175            LPUNKNOWN    nKey;
00176            CMgaObjectProxyMapItem *     pt;
00177            POSITION pos = m_MapObject2Item.GetStartPosition();
00178            bool found = false;
00179            while (pos != NULL)
00180            {
00181                   m_MapObject2Item.GetNextAssoc( pos, nKey, pt);
00182                   IMgaObjectPtr mgao(nKey);
00183                   IUnknownPtr punkptr(nKey);
00184 
00185                   punk = punkptr;
00186                   _bstr_t boid;
00187                   COMTHROW(mgao->get_ID(boid.GetAddress()));
00188                   _bstr_t bid = Id;
00189                   if (bid == boid)
00190                   {
00191                           found = true;
00192                           break;
00193                   }
00194            }
00195 
00196                 if(found) 
00197                         hTreeItem = pt->m_hTreeItem;
00198 
00199                 return (found);
00200         }
00201 
00202 
00203         BOOL LookupTreeItem(LPUNKNOWN pUnknown, HTREEITEM &hTreeItem)
00204         {
00205                 CComPtr<IUnknown> pUnk;
00206                 pUnknown->QueryInterface(IID_IUnknown, (void**)&pUnk);
00207                 CMgaObjectProxyMapItem* pMgaObjectProxyMapItem;
00208 
00209                 BOOL bResult=m_MapObject2Item.Lookup(pUnk,pMgaObjectProxyMapItem);
00210 
00211                 if (bResult)
00212                         hTreeItem=pMgaObjectProxyMapItem->m_hTreeItem;
00213 
00214                 return bResult;
00215         }
00216 
00217 
00218 
00219         BOOL bIsInMap(LPUNKNOWN pUnknown)
00220         {
00221                 CComPtr<IUnknown> pUnk;
00222                 pUnknown->QueryInterface(IID_IUnknown, (void**)&pUnk);
00223 
00224                 CMgaObjectProxyMapItem* pMgaObjectProxyMapItem;
00225                 return m_MapObject2Item.Lookup(pUnk,pMgaObjectProxyMapItem);
00226         }
00227 
00228         BOOL bIsInMap(HTREEITEM hTreeItem)
00229         {
00230                 LPUNKNOWN pUnknown;
00231                 return m_MapItem2Object.Lookup(hTreeItem,pUnknown);
00232         }
00233 
00234 
00235 
00236         // Dump function
00237 #ifdef _DEBUG
00238         void Dump(CDumpContext& dc ) const
00239         {
00240                 return;
00241                 dc<<"______________________ MgaMap Dump begin _______________________";
00242                 // Dumping the Item2Object Map
00243                 dc<<"\n\nMapItem2Object:\n";
00244         
00245                 HTREEITEM hItem;
00246                 LPUNKNOWN pUnknown;
00247 
00248                 POSITION pos = m_MapItem2Object.GetStartPosition();
00249                 while(pos!=NULL)
00250                 {
00251                         m_MapItem2Object.GetNextAssoc(pos,hItem,pUnknown);
00252                         CString strOut;
00253                         strOut.Format(_T(" %x  |  %x\n"),hItem, pUnknown);
00254                         dc<<strOut;
00255                 }
00256                 dc<<"\n";
00257 
00258                 // Dumping Object2Item Map
00259                 dc<<"\n\nMapObject2Item:\n";
00260         
00261 
00262                 CMgaObjectProxyMapItem* pMgaObjectProxyMapItem;
00263 
00264 
00265                 pos = m_MapObject2Item.GetStartPosition();
00266                 while(pos!=NULL)
00267                 {
00268                         m_MapObject2Item.GetNextAssoc(pos,pUnknown,pMgaObjectProxyMapItem);
00269                         CString strOut;
00270                         strOut.Format(_T(" %x  |  %x  |  %d\n"), pUnknown, pMgaObjectProxyMapItem->m_hTreeItem,pMgaObjectProxyMapItem->m_TypeInfo);
00271                         dc<<strOut;
00272                 }
00273                 dc<<"\n";
00274 
00275                 dc<<"______________________ MgaMap Dump end _______________________\n\n";
00276         }
00277 #endif
00278 
00279 private:
00280         CMapObject2Item m_MapObject2Item;
00281         CMapItem2Object m_MapItem2Object; 
00282 
00283 };
00284 
00285 #endif // !defined(AFX_MGAMAP_H__4FB2B383_49D6_4C83_AB80_63DFF3C45380__INCLUDED_)