GME  13
MgaMap2.cpp
Go to the documentation of this file.
00001 // MgaMap2.cpp: implementation of the CMgaMap class.
00002 //
00003 // Maintains double map to enforce the easy mapping between a 
00004 // tree element and the corresponding mga object and vice versa
00005 // providing fast search based on the CMap hash tables and ensures the consistency
00006 // between the two inner maps.
00007 //
00008 // The first map (m_MapItem2Object) maintans the correspondence between a tree item
00009 // handle as a key and an CMgaObjectProxy type pointer as a value.
00010 // The second map (m_MapObject2Item) has an LPUNKNOWN pointer as a 
00011 // key and a tree item handle.
00012 // So the mapping can be summarized as follows:
00013 //      m_MapItem2Object: HTREEITEM -> CMgaObjectProxy
00014 //      m_MapObject2Item: LPUNKNOWN -> HTREEITEM
00015 //
00016 // 
00017 //
00018 // Tihamer Levendovszky 11/26/2001
00020 
00021 #include "stdafx.h"
00022 #include "gmeactivebrowser.h"
00023 #include "MgaMap2.h"
00024 
00025 #ifdef _DEBUG
00026 #undef THIS_FILE
00027 static char THIS_FILE[]=__FILE__;
00028 #define new DEBUG_NEW
00029 #endif
00030 
00032 // Construction/Destruction
00034 
00035 CMgaMap::CMgaMap()
00036 {
00037 
00038 }
00039 
00040 CMgaMap::~CMgaMap()
00041 {
00042         DestroyMap();
00043 }
00044 
00045 void CMgaMap::DestroyMap()
00046 {
00047         // Remove elements from object map and delete the object
00048         POSITION pos=m_MapObject2Item.GetStartPosition();
00049         CMgaObjectProxy* pObject;
00050         LPUNKNOWN pUnknown;
00051 
00052         // Remove the TreeItem handles and LPUNKNOWNs from the item map
00053         m_MapItem2Object.RemoveAll();
00054 
00055 
00056         while(pos!=NULL)
00057         {
00058                 m_MapObject2Item.GetNextAssoc(pos,pUnknown,pObject);
00059                 m_MapObject2Item.RemoveKey(pUnknown);
00060                 delete pObject;
00061                 pUnknown->Release();
00062         }
00063 
00064 }
00065 
00066 // Retreives the corresponding TreeItem handle from an Object Proxy
00067 BOOL CMgaMap::LookupTreeItem(CMgaObjectProxy MgaObjectProxy, HTREEITEM &hTreeItem)
00068 {
00069         
00070         return m_MapObject2Item.Lookup(MgaObjectProxy.m_pMgaObject,hTreeItem);
00071 
00072         
00073 }
00074 
00075 
00076 // Retreives the Object Proxy from the corresponding TreeItem handle
00077 BOOL CMgaMap::LookupObjectProxy(HTREEITEM hTreeItem,CMgaObjectProxy& rObjectProxy)
00078 {
00079         CMgaObjectProxy* pMgaObjectProxy;
00080 
00081         
00082          bResult=m_MapItem2Object.Lookup(hTreeItem,pMgaObjectProxy);
00083         
00084         if(bResult)
00085         {
00086                 rObjectProxy=*pMgaObjectProxy;
00087         }
00088         return  bResult;
00089 }
00090 
00091 
00092 // Removes a tree item maintaining the consistency between the two maps
00093 BOOL CMgaMap::RemoveTreeItem(HTREEITEM hTreeItem)
00094 {
00095         CMgaObjectProxy* pMgaObjectProxy;
00096 
00097         if(!m_MapItem2Object.Lookup(hTreeItem,pMgaObjectProxy))
00098         {
00099                 return FALSE;
00100         }
00101         else
00102         {
00103                 m_MapItem2Object.RemoveKey(hTreeItem);
00104                 
00105                 m_MapObject2Item.RemoveKey(pMgaObjectProxy->m_pMgaObject);                      
00106                 // Free COM object
00107                 pMgaObjectProxy->m_pMgaObject->Release();
00108                 delete pObjectProxy;
00109                 
00110                 return TRUE;
00111         }
00112 
00113 }
00114 
00115 
00116 // Removes an Object Proxy maintaining the consistency between the two maps
00117 BOOL CMgaMap::RemoveObjectProxy(CMgaObjectProxy MgaObjectProxy)
00118 {       
00119         HTREEITEM hTreItem;
00120 
00121         if(!m_MapObject2Item.Lookup(MgaObjectProxy.m_pMgaObject,hTreItem))
00122         {
00123                 return FALSE;
00124         }
00125         else
00126         {
00127                 m_MapObject2Item.RemoveKey(pObjectProxyMapItem->m_pMgaObject);
00128                 m_MapItem2Object.RemoveKey(hTreeItem);
00129                 
00130                 // Free COM object
00131                 pObjectProxy->m_pMgaObject->Release();
00132                 delete pObjectProxyMap;
00133                 return TRUE;
00134         }
00135 }
00136 
00137 
00138 // Adds a new TreeItem Handle - ObjectProxy pair to the map
00139 void CMgaMap::AddEntry(HTREEITEM hTreeItem, CMgaObjectProxy MgaObjectProxy)
00140 {
00141 
00142 // Removing the elements from the maps with the keys, because CMap::SetAt would overwrite
00143 // the value belonging to the already existing key 
00144 // resulting an inconsistency with the other map. 
00145         RemoveTreeItem(hTreeItem);
00146         RemoveObjectProxy(MgaObjectProxy);      
00147         
00148         // Increase reference counter for this map
00149         MgaObjectProxy.m_pMgaObject->AddRef();
00150 
00151         CMgaObjectProxy* pMgaObjectProxy= new CMgaObjectProxy(MgaObjectProxy);
00152         m_MapItem2Object.SetAt(hTreeItem,pMgaObjectProxy);
00153 
00154         
00155         m_MapObject2Item.SetAt(MgaObjectProxy.m_pMgaObject,hTreeItem);
00156 
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 CMgaMap::LookupObjectUnknown(HTREEITEM hTreeItem, LPUNKNOWN &pUnknown)
00163 {
00164         CMgaObjectProxy* pMgaObjectProxy;
00165         BOOL bResult=m_MapItem2Object.Lookup(hTreeItem,pMgaObjectProxy);
00166 
00167         if(bResult)
00168         {
00169                 pUnknown=pMgaObjectProxy->m_pMgaObject;
00170                 return TRUE;
00171         }
00172         else
00173         {
00174                 return FALSE;
00175         }
00176 }
00177 
00178 
00179 
00180 BOOL CMgaMap::LookupTreeItem(LPUNKNOWN pUnknown, HTREEITEM &hTreeItem)
00181 {
00182         
00183         return m_MapObject2Item.Lookup(pUnknown,hTreeItem);
00184 }
00185 
00186 
00187 
00188 BOOL CMgaMap::bIsInMap(LPUNKNOWN pUnknown)
00189 {
00190         HTREEITEM hTreeItem;
00191 
00192         return m_MapObject2Item.Lookup(pUnknown,hTreeItem);
00193 }
00194 
00195 BOOL CMgaMap::bIsInMap(HTREEITEM hTreeItem)
00196 {
00197         CMgaObjectProxy* pMgaObjectProxy;
00198         return m_MapItem2Object.Lookup(hTreeItem,pMgaObjectProxy);
00199 }
00200 
00201 
00202 
00203 // Dump function
00204 #ifdef _DEBUG
00205 void CMgaMap::Dump(CDumpContext& dc ) const
00206 {
00207         dc<<"______________________ MgaMap Dump begin _______________________";
00208         // Dumping the Item2Object Map
00209         dc<<"\n\nMapItem2Object:\n";
00210         
00211         HTREEITEM hItem;
00212         CMgaObjectProxy* pMgaObjectProxy;
00213 
00214         dc<<"ItemHandle  Proxy  Type";
00215         POSITION pos = m_MapItem2Object.GetStartPosition();
00216         while(pos!=NULL)
00217         {
00218                 m_MapItem2Object.GetNextAssoc(pos,hItem,pMgaObjectProxy);
00219                 CString strOut;
00220                 strOut.Format(" %x  |  %x |  %x \n",hItem, pMgaObjectProxy,pMgaObjectProxy->m_TypeInfo);
00221                 dc<<strOut;
00222         }
00223         dc<<"\n";
00224 
00225         // Dumping Object2Item Map
00226         dc<<"\n\nMapObject2Item:\n";
00227         
00228 
00229         LPUNKNOWN pUnknown;
00230 
00231         dc<<"pUnknown ItemHandle";
00232         pos = m_MapObject2Item.GetStartPosition();
00233         while(pos!=NULL)
00234         {
00235                 m_MapObject2Item.GetNextAssoc(pos,pUnknown,hItem);
00236                 CString strOut;
00237                 strOut.Format(" %x  |  %x\n", pUnknown,hTreeItem,);
00238                 dc<<strOut;
00239         }
00240         dc<<"\n";
00241 
00242         dc<<"______________________ MgaMap Dump end _______________________\n\n";
00243 }
00244 #endif