GME  13
MgaMetaRegNode.cpp
Go to the documentation of this file.
00001 
00002 #include "stdafx.h"
00003 #include "MgaMetaRegNode.h"
00004 
00005 // --------------------------- CMgaMetaRegNodes
00006 
00007 HRESULT CMgaMetaRegNodes::get_RegistryNode(IUnknown *me, BSTR bpath, IMgaMetaRegNode **p)
00008 {
00009         ASSERT( me != NULL );
00010 
00011         CHECK_OUT(p);
00012 
00013         COMTRY
00014         {
00015                 CCoreObjectPtr self(me);
00016 
00017                 std::wstring path = bpath ? bpath : L"";
00018 
00019                 CComObjPtr<ICoreProject> coreproject;
00020                 COMTHROW( self->get_Project(PutOut(coreproject)) );
00021                 ASSERT( coreproject != NULL );
00022 
00023                 for(;;)
00024                 {
00025                         size_t pos = path.find(L'/');//pos will be either in range 0..len-1 or will be equal with string::npos 
00026                         std::wstring name(path, 0, pos);
00027 
00028                         CComBstrObj bname(name.c_str());
00029 
00030                         CCoreObjectPtr node;
00031 
00032                         coreobjects_type coreobjects;
00033                         self.GetCollectionValue(ATTRID_REGNODES_COLL, coreobjects);
00034 
00035                         coreobjects_iterator i = coreobjects.begin();
00036                         coreobjects_iterator e = coreobjects.end();
00037                         while( i != e )
00038                         {
00039                                 CComBstrObj bstr;
00040                                 i->GetStringValue(ATTRID_NAME, PutOut(bstr));
00041 
00042                                 if( bname == bstr )
00043                                 {
00044                                         node = *i;
00045                                         break;
00046                                 }
00047 
00048                                 ++i;
00049                         }
00050 
00051                         if( node == NULL )
00052                         {
00053                                 HRESULT hr = coreproject->CreateObject(METAID_METAREGNODE, PutOut(node));
00054 
00055                                 if( hr == E_TRANSACTION )
00056                                         COMRETURN(E_NOTFOUND);
00057                                 COMTHROW(hr);
00058 
00059                                 ASSERT( node != NULL );
00060 
00061                                 node.PutPointerValue(ATTRID_REGNODES_COLL, self);
00062                                 node.PutStringValue(ATTRID_NAME, PutInBstr(name));
00063                         }
00064 
00065                         ASSERT( node != NULL );
00066 
00067                         if( pos == std::string::npos) //<-- correct test, instead of "if( (unsigned int) pos >= path.length() )"
00068                         {
00069                                 COMTHROW( ::QueryInterface(node, p) );
00070                                 return S_OK;
00071                         }
00072 
00073                         self = node;
00074                         path.erase(0, pos+1);
00075                 }
00076         }
00077         COMCATCH(;)
00078 }
00079 
00080 HRESULT CMgaMetaRegNodes::get_RegistryValue(IUnknown *me, BSTR path, BSTR *p)
00081 {
00082         CComObjPtr<IMgaMetaRegNode> node;
00083 
00084         HRESULT hr;
00085         if( FAILED(hr = get_RegistryNode(me, path, PutOut(node))) )
00086         {
00087                 if( hr == E_NOTFOUND )
00088                         hr = S_OK;
00089 
00090                 return hr;
00091         }
00092 
00093         ASSERT( node != NULL );
00094         return node->get_Value(p);
00095 }
00096 
00097 HRESULT CMgaMetaRegNodes::put_RegistryValue(IUnknown *me, BSTR path, BSTR p)
00098 {
00099         CComObjPtr<IMgaMetaRegNode> node;
00100 
00101         HRESULT hr;
00102         if( FAILED(hr = get_RegistryNode(me, path, PutOut(node))) )
00103         {
00104                 if( hr == E_NOTFOUND )
00105                         hr = S_OK;
00106 
00107                 return hr;
00108         }
00109 
00110         ASSERT( node != NULL );
00111         return node->put_Value(p);
00112 }
00113 
00114 // --------------------------- CMgaMetaRegNode
00115 
00116 STDMETHODIMP CMgaMetaRegNode::Delete()
00117 {
00118         COMTRY
00119         {
00120                 CCoreObjectPtr self(GetUnknown());
00121         
00122                 self.PutLockValue(ATTRID_LOCK, LOCKING_EXCLUSIVE);
00123 
00124                 coreobjects_type coreobjects;
00125                 self.LoadCollectionValue(ATTRID_REGNODES_COLL, coreobjects);
00126 
00127                 coreobjects_iterator i = coreobjects.begin();
00128                 coreobjects_iterator e = coreobjects.end();
00129                 while( i != e )
00130                 {
00131                         CComObjPtr<IMgaMetaRegNode> node;
00132                         COMTHROW( ::QueryInterface(*i, node) );
00133 
00134                         COMTHROW( node->Delete() );
00135 
00136                         ++i;
00137                 }
00138 
00139                 COMTHROW( self->Delete() );
00140         }
00141         COMCATCH(;)
00142 }
00143 
00144 // ------- Traverse
00145 
00146 void CMgaMetaRegNode::Traverse(CMgaMetaProject *metaproject, CCoreObjectPtr &me)
00147 {
00148         ASSERT( metaproject != NULL );
00149         ASSERT( me != NULL );
00150 
00151         TraverseCollection(metaproject, me, ATTRID_REGNODES_COLL);
00152 }