GME  13
MetaUtilities.cpp
Go to the documentation of this file.
00001 
00002 #include "stdafx.h"
00003 
00004 // --------------------------- CCoreObjectPtr
00005 
00006 void CCoreObjectPtr::GetCollectionValue(attrid_type attrid, coreobjects_type &ret) const
00007 {
00008         ASSERT( sizeof(CCoreObjectPtr) == sizeof(ICoreObject*) );
00009 
00010         ASSERT( p != NULL );
00011         ASSERT( ret.empty() );
00012 
00013         CComVariant v;
00014         COMTHROW( p->get_LoadedAttrValue(attrid + ATTRID_COLLECTION, PutOut(v)) );
00015         ASSERT( v.vt == VT_DISPATCH );
00016 
00017         CComObjPtr<ICoreObjects> coll;
00018         COMTHROW( ::QueryInterface(v.pdispVal, coll) );
00019         ASSERT( coll != NULL );
00020 
00021         long count = 0;
00022         COMTHROW( coll->get_Count(&count) );
00023         ASSERT( count >= 0 );
00024 
00025         if(count!=0)
00026         {
00027                 // Used to be: ret.insert(ret.begin(), count, CCoreObjectPtr());
00028                 ret.resize(count);
00029                 COMTHROW( coll->GetAll(count, (ICoreObject**)&ret[0]) );
00030         }
00031 }
00032 
00033 void CCoreObjectPtr::GetStringValue(attrid_type attrid, BSTR *ret) const
00034 {
00035         ASSERT( p != NULL );
00036         ASSERT( ret != NULL && *ret == NULL );
00037 
00038         CComVariant v;
00039         COMTHROW( p->get_LoadedAttrValue(attrid, PutOut(v)) );
00040         ASSERT( v.vt == VT_BSTR );
00041 
00042         MoveTo(&v.bstrVal, ret);
00043 }
00044 
00045 void CCoreObjectPtr::GetVariantValue(attrid_type attrid, VARIANT *ret) const
00046 {
00047         ASSERT( p != NULL );
00048         ASSERT( ret != NULL && ret->vt == VT_EMPTY );
00049 
00050         COMTHROW( p->get_LoadedAttrValue(attrid, ret) );
00051 }
00052 
00053 long CCoreObjectPtr::GetLongValue(attrid_type attrid) const
00054 {
00055         ASSERT( p != NULL );
00056 
00057         CComVariant v;
00058         COMTHROW( p->get_LoadedAttrValue(attrid, PutOut(v)) );
00059         ASSERT( v.vt == VT_I4 );
00060 
00061         return v.lVal;
00062 }
00063 
00064 VARIANT_BOOL CCoreObjectPtr::GetBoolValue(attrid_type attrid) const
00065 {
00066         ASSERT( p != NULL );
00067 
00068         CComVariant v;
00069         COMTHROW( p->get_LoadedAttrValue(attrid, PutOut(v)) );
00070         ASSERT( v.vt == VT_I4 );
00071 
00072         return v.lVal ? VARIANT_TRUE : VARIANT_FALSE;
00073 }
00074 
00075 void CCoreObjectPtr::GetPointerValue(attrid_type attrid, CCoreObjectPtr &ret) const
00076 {
00077         ASSERT( ret == NULL );
00078 
00079         CComVariant v;
00080         COMTHROW( p->get_LoadedAttrValue(attrid, PutOut(v)) );
00081         ASSERT( v.vt == VT_DISPATCH );
00082 
00083         if( v.pdispVal != NULL )
00084                 COMTHROW( ::QueryInterface(v.pdispVal, PutOut(ret)) );
00085 }
00086 
00087 metaid_type CCoreObjectPtr::GetMetaID() const
00088 {
00089         ASSERT( p != NULL );
00090 
00091         CComObjPtr<ICoreMetaObject> metaobject;
00092         COMTHROW( p->get_MetaObject(PutOut(metaobject)) );
00093 
00094         metaid_type metaid;
00095         COMTHROW( metaobject->get_MetaID(&metaid) );
00096 
00097         return metaid;
00098 }
00099 
00100 objid_type CCoreObjectPtr::GetObjID() const
00101 {
00102         ASSERT( p != NULL );
00103 
00104         objid_type objid;
00105         COMTHROW( p->get_ObjID(&objid) );
00106 
00107         return objid;
00108 }
00109 
00110 // ------- Load
00111 
00112 void CCoreObjectPtr::LoadCollectionValue(attrid_type attrid, coreobjects_type &ret)
00113 {
00114         ASSERT( sizeof(CCoreObjectPtr) == sizeof(ICoreObject*) );
00115 
00116         ASSERT( p != NULL );
00117         ASSERT( ret.empty() );
00118 
00119         CComVariant v;
00120         COMTHROW( p->get_AttributeValue(attrid + ATTRID_COLLECTION, PutOut(v)) );
00121         ASSERT( v.vt == VT_DISPATCH );
00122 
00123         CComObjPtr<ICoreObjects> coll;
00124         COMTHROW( ::QueryInterface(v.pdispVal, coll) );
00125         ASSERT( coll != NULL );
00126 
00127         long count = 0;
00128         COMTHROW( coll->get_Count(&count) );
00129         ASSERT( count >= 0 );
00130         
00131         if(count!=0)
00132         {
00133                 ret.insert(ret.begin(), count, CCoreObjectPtr());
00134                 COMTHROW( coll->GetAll(count, (ICoreObject**)&ret[0]) );
00135         }
00136 }
00137 
00138 // ------- Put
00139 
00140 void CCoreObjectPtr::PutStringValue(attrid_type attrid, BSTR a)
00141 {
00142         ASSERT( p != NULL );
00143 
00144         COMTHROW( p->put_AttributeValue(attrid, PutInVariant(a)) );
00145 }
00146 
00147 void CCoreObjectPtr::PutVariantValue(attrid_type attrid, VARIANT a)
00148 {
00149         ASSERT( p != NULL );
00150 
00151         COMTHROW( p->put_AttributeValue(attrid, a) );
00152 }
00153 
00154 void CCoreObjectPtr::PutLongValue(attrid_type attrid, long a)
00155 {
00156         ASSERT( p != NULL );
00157 
00158         COMTHROW( p->put_AttributeValue(attrid, PutInVariant(a)) );
00159 }
00160 
00161 void CCoreObjectPtr::PutBoolValue(attrid_type attrid, VARIANT_BOOL a)
00162 {
00163         ASSERT( p != NULL );
00164 
00165         COMTHROW( p->put_AttributeValue(attrid, PutInVariant(a)) );
00166 }
00167 
00168 void CCoreObjectPtr::PutPointerValue(attrid_type attrid, ICoreObject *a)
00169 {
00170         ASSERT( p != NULL );
00171 
00172         COMTHROW( p->put_AttributeValue(attrid, PutInVariant(a)) );
00173 }
00174 
00175 void CCoreObjectPtr::PutLockValue(attrid_type attrid, locking_type locking)
00176 {
00177         ASSERT( p != NULL );
00178 
00179         COMTHROW( p->put_AttributeValue(attrid, PutInVariant(locking)) );
00180 }
00181 
00182 // --------------------------- ComGetAttrValue
00183 
00184 HRESULT ComGetAttrValue(IUnknown *self, attrid_type attrid, BSTR *p)
00185 {
00186         CHECK_OUT(p);
00187 
00188         COMTRY
00189         {
00190                 ASSERT( self != NULL );
00191                 CCoreObjectPtr me(self);
00192                 me.GetStringValue(attrid, p);
00193         }
00194         COMCATCH(;)
00195 }
00196 
00197 HRESULT ComGetAttrValue(IUnknown *self, attrid_type attrid, long *p)
00198 {
00199         CHECK_OUT(p);
00200 
00201         COMTRY
00202         {
00203                 ASSERT( self != NULL );
00204                 CCoreObjectPtr me(self);
00205                 *p = me.GetLongValue(attrid);
00206         }
00207         COMCATCH(;)
00208 }
00209 
00210 HRESULT ComGetAttrValue(IUnknown *self, attrid_type attrid, VARIANT *p)
00211 {
00212         CHECK_OUT(p);
00213 
00214         COMTRY
00215         {
00216                 ASSERT( self != NULL );
00217                 CCoreObjectPtr me(self);
00218                 me.GetVariantValue(attrid, p);
00219         }
00220         COMCATCH(;)
00221 }
00222 
00223 HRESULT ComGetDisplayedName(IUnknown *self, attrid_type dispattrid, attrid_type nameattrid, BSTR *p)
00224 {
00225         CHECK_OUT(p);
00226 
00227         COMTRY
00228         {
00229                 ASSERT( self != NULL );
00230                 CCoreObjectPtr me(self);
00231 
00232                 CComVariant v;
00233                 COMTHROW( me->get_LoadedAttrValue(dispattrid, PutOut(v)) );
00234                 ASSERT( v.vt == VT_BSTR );
00235 
00236                 std::string dispname;
00237                 CopyTo(v, dispname);
00238 
00239                 if( dispname.empty() )
00240                 {
00241                         v.Clear();
00242 
00243                         COMTHROW( me->get_LoadedAttrValue(nameattrid, PutOut(v)) );
00244                         ASSERT( v.vt == VT_BSTR );
00245                 }
00246 
00247                 MoveTo(&v.bstrVal, p);
00248         }
00249         COMCATCH(;)
00250 }
00251 
00252 HRESULT ComGetObjType(IUnknown *self, objtype_enum *p)
00253 {
00254         CHECK_OUT(p);
00255 
00256         COMTRY
00257         {
00258                 ASSERT( self != NULL );
00259                 CCoreObjectPtr me(self);
00260 
00261                 metaid_type metaid = me.GetMetaID();
00262 
00263                 if( metaid == METAID_METAPROJECT )
00264                         *p = OBJTYPE_FOLDER;
00265                 else
00266                         *p = (objtype_enum)(metaid - METAID_METABASE);
00267         }
00268         COMCATCH(;)
00269 }
00270 
00271 // --------------------------- ComPutAttrValue
00272 
00273 HRESULT ComPutAttrValue(IUnknown *self, attrid_type attrid, BSTR p)
00274 {
00275         COMTRY
00276         {
00277                 ASSERT( self != NULL );
00278                 CCoreObjectPtr me(self);
00279                 me.PutStringValue(attrid, p);
00280         }
00281         COMCATCH(;)
00282 }
00283 
00284 HRESULT ComPutAttrValue(IUnknown *self, attrid_type attrid, long p)
00285 {
00286         COMTRY
00287         {
00288                 ASSERT( self != NULL );
00289                 CCoreObjectPtr me(self);
00290                 me.PutLongValue(attrid, p);
00291         }
00292         COMCATCH(;)
00293 }
00294 
00295 HRESULT ComPutAttrValue(IUnknown *self, attrid_type attrid, VARIANT p)
00296 {
00297         COMTRY
00298         {
00299                 ASSERT( self != NULL );
00300                 CCoreObjectPtr me(self);
00301                 me.PutVariantValue(attrid, p);
00302         }
00303         COMCATCH(;)
00304 }
00305 
00306 HRESULT ComPutPointerValue(IUnknown *self, attrid_type attrid, IDispatch *p)
00307 {
00308         COMTRY
00309         {
00310                 ASSERT( self != NULL );
00311                 CCoreObjectPtr me(self);
00312 
00313                 CCoreObjectPtr other(p);
00314 
00315                 me.PutPointerValue(attrid, other);
00316         }
00317         COMCATCH(;)
00318 }
00319 
00320 // --------------------------- Others
00321 
00322 void ChangeAttrValueType(CComVariant &v, attval_enum attval)
00323 {
00324         ASSERT( v.vt == VT_BSTR );
00325 
00326         switch(attval)
00327         {
00328         case ATTVAL_STRING:
00329         case ATTVAL_ENUM:
00330         case ATTVAL_DYNAMIC:
00331                 return;
00332 
00333         case ATTVAL_INTEGER:
00334                 if( FAILED(v.ChangeType(VT_I4, &v)) )
00335                         v = (int)0;
00336 
00337                 ASSERT( v.vt == VT_I4 );
00338                 break;
00339 
00340         case ATTVAL_DOUBLE:
00341                 if( FAILED(v.ChangeType(VT_R8, &v)) )
00342                         v = (double)0.0;
00343 
00344                 ASSERT( v.vt == VT_R8 );
00345                 break;
00346 
00347         case ATTVAL_BOOLEAN:
00348                 if( FAILED(v.ChangeType(VT_BOOL, &v)) )
00349                         v = (VARIANT_BOOL)(VARIANT_FALSE);
00350 
00351                 ASSERT( v.vt == VT_BOOL );
00352                 break;
00353 
00354         default:
00355                 HR_THROW(E_ATTVALTYPE);
00356         }
00357 }
00358 
00359 HRESULT ComDeleteObject(IUnknown *self)
00360 {
00361         COMTRY
00362         {
00363                 ASSERT( self != NULL );
00364                 CCoreObjectPtr me(self);
00365                 COMTHROW( me->Delete() );
00366         }
00367         COMCATCH(;)
00368 }
00369 
00370 CComBSTR truncateName( IUnknown *p_bs, CComBSTR pIn)
00371 {
00372         CComBSTR truncated_name;
00373         CComBSTR nmspc;
00374         CComQIPtr<IMgaMetaBase> bs( p_bs);
00375         if( bs)
00376         {
00377                 CComPtr<IMgaMetaProject> pr;
00378                 COMTHROW( bs->get_MetaProject( &pr));
00379                 COMTHROW( pr->GetNmspc( &nmspc)); // fetch namespace into m_nmspc
00380         }
00381         if( nmspc && nmspc.Length() > 0) // some namespace is set
00382         {
00383                 bool nm_match = false;
00384                 COMTHROW(nmspc.Append( L"::"));
00385                 unsigned int nms_len = nmspc.Length();
00386                 unsigned int pin_len = pIn.Length();
00387                 if( nms_len < pin_len
00388                         && 0 == wcsncmp( pIn.m_str, nmspc.m_str, (size_t) nms_len))
00389                 {
00390                         unsigned int i = nms_len;
00391                         while( i < pin_len)
00392                         {
00393                                 COMTHROW(truncated_name.Append( pIn[i]));
00394                                 ++i;
00395                         }
00396 
00397                         return truncated_name;
00398                 }
00399         }
00400 
00401         return pIn;
00402 }