GME  13
MgaCoreObj.h
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 #pragma once
00006 
00007 #include "MgaGeneric.h"
00008 
00009 class CoreAttr;  // defined  below
00010 class CoreObjs;
00011 
00012 
00013 
00014 // ----------------------------------------
00015 // CoreObj: supersmart ptr for ICoreObject
00016 // ----------------------------------------
00017 class CoreObj : public CComPtr<ICoreObject> {
00018 public:
00019         CoreObj() {;};
00020         CoreObj(ICoreObject *ptr) : CComPtr<ICoreObject>(ptr) { }
00021 
00022         // Create this object (it must be empty)
00023         // In both versions 'c' is used to access the core project.
00024         void Create(ICoreObject *c, metaid_type mm);
00025         void Create(ICoreProject *c, metaid_type mm);
00026 
00027         explicit CoreObj( IMgaObject *ptr);
00028 
00029         inline CoreAttr operator[](int  id) const;
00030         inline CoreAttr operator[](attrid_type  id) const;
00031 
00032         void operator= (ICoreObject *s) {
00033                 CComPtr<ICoreObject>::operator=(s);
00034         }
00035 
00036         void operator <<= (CoreObj &s) {
00037                 Attach(s.Detach());
00038         }
00039 
00040         CoreObj *operator &()  {
00041                 return this;
00042         }
00043         bool IsEqualObject(IUnknown* pOther) const {
00044                 if (p == NULL && pOther == NULL)
00045                         return true; // They are both NULL objects
00046 
00047                 if (p == NULL || pOther == NULL)
00048                         return false; // One is NULL the other is not
00049 
00050                 CComPtr<IUnknown> punk1;
00051                 CComPtr<IUnknown> punk2;
00052                 p->QueryInterface(IID_IUnknown, (void**)&punk1);
00053                 pOther->QueryInterface(IID_IUnknown, (void**)&punk2);
00054                 return punk1 == punk2;
00055         }
00056 
00057         ICoreObject *&ComPtr() { ASSERT(!p); return p; }
00058 
00059         metaid_type GetMetaID() const {
00060                                 metaid_type t;
00061                                 CComPtr<ICoreMetaObject> mmo;
00062                                 COMTHROW((*this)->get_MetaObject(&mmo));
00063                                 COMTHROW(mmo->get_MetaID(&t));
00064                                 return t;
00065         }
00066 
00067         objid_type GetObjID() const {
00068                                 objid_type t;
00069                                 COMTHROW((*this)->get_ObjID(&t));
00070                                 return t;
00071         }
00072 
00073         bool IsDeleted() const {
00074                                 VARIANT_BOOL pp;
00075                                 if((*this)->get_IsDeleted(&pp) != S_OK)
00076                                         return true;
00077                                 return pp ? true:false;
00078         }
00079 
00080         bool IsContainer() const { metaid_type s = GetMetaID(); return s == DTID_MODEL || s == DTID_FOLDER; }
00081         bool IsFCO() const { metaid_type s = GetMetaID(); return s >= DTID_MODEL && s <= DTID_SET; }
00082         bool IsRootFCO() const;
00083         bool IsRootFolder() const;
00084         bool IsObject()  const { metaid_type s = GetMetaID(); return s >= DTID_MODEL && s <= DTID_FOLDER; }
00085         bool IsSubObject() const { metaid_type s = GetMetaID(); return s >= DTID_FOLDER; }
00086 
00087         // if the object is a subobject, get the CoreObj of the object it belongs to
00088         CoreObj GetMgaObj();
00089 
00090         // get the master (if any) for a subobj (REFERENCE, SETMEMBER, CONNROLE, CONNROLESEG)
00091         // the master is the corresponding subobject in the immediate base FCO
00092         // for references, master is an object (if any)
00093         CoreObj GetMaster(int offset = 1);
00094 
00095         // FOLLOWS a chain (like ATTRID_FCOPARENT) returns NULL if list is shorter
00096         CoreObj FollowChain(attrid_type id, int offset);  
00097 
00098         // this object (an FCO) has outgoing inheritance relationships, 
00099         // but its FCO parent (if any) has none.
00100         bool IsRootOfDeriv() const;  
00101         
00102 };
00103 
00104 
00105 
00106 // ----------------------------------------
00107 // CoreObjs: supersmart ptr for ICoreObjects
00108 // ----------------------------------------
00109 class CoreObjs : public CComPtr<ICoreObjects> {
00110 public:
00111         CoreObjs() {;};
00112         // sor ascending by use for CONNROLESEG-s only
00113         void Sort();
00114         long Count() const {
00115                 long l; 
00116                 COMTHROW(p->get_Count(&l));
00117                 return l;
00118         }
00119 
00120 private:
00121         friend CoreAttr;
00122 
00123         explicit CoreObjs( IDispatch *ptr) {
00124                 COMTHROW(ptr->QueryInterface(_uuidof(p),(void**)&p));
00125                 ASSERT(p);
00126         }
00127 };
00128 
00129 
00130 // ----------------------------------------
00131 // CoreAttribute: Represents an attribute within a coreobject 
00132 // This is intended to be used as a short-lived object (does not store references)
00133 // ----------------------------------------
00134 class CoreAttr {
00135         attrid_type i;
00136 protected:
00137         CoreAttr(int attrid) {
00138                 i = (short)attrid;
00139         };
00140         ICoreObject *p;   // not a smart pointer  
00141 public:
00142         CoreAttr(int attrid, ICoreObject *ptr) {
00143                 i = (short)attrid; p = ptr;
00144         };
00145 
00146 
00147         void operator= (const CoreAttr &s) {
00148                 CComVariant k(s);
00149                 COMTHROW(p->put_AttributeValue(i,k));
00150         }
00151 
00152         template <class C> 
00153         const C& operator= (const C &s) { //const removed
00154                 CComVariant k(s);
00155                 COMTHROW(p->put_AttributeValue(i,k));
00156                 return s;
00157         };
00158 
00159         operator CComBSTR() const {
00160                 CComBSTR v;                             // It will be optimized by the compiler!!!!
00161                 CComVariant k;
00162                 COMTHROW(p->get_AttributeValue(i,&k));
00163                 ASSERT(k.vt == VT_BSTR);
00164                 k.vt = VT_EMPTY;
00165                 v.Attach(k.bstrVal);
00166                 return v;
00167         }; 
00168 
00169         operator long() const {
00170                 CComVariant k;
00171                 COMTHROW(p->get_AttributeValue(i,&k));
00172                 ASSERT(k.vt == VT_I4);
00173                 return k.lVal;
00174         }; 
00175 
00176         operator CComVariant() const {
00177                 CComVariant k;
00178                 COMTHROW(p->get_AttributeValue(i,&k));
00179                 return k;
00180         }; 
00181         inline operator CoreObj () const {
00182                 CComVariant k;
00183                 COMTHROW(p->get_AttributeValue(i,&k));
00184                 if(k.vt != VT_DISPATCH) COMTHROW(E_MGA_MODULE_INCOMPATIBILITY);
00185                 CoreObj rr;
00186                 if(k.pdispVal) COMTHROW(k.pdispVal->QueryInterface(_uuidof(ICoreObject), (void **)&rr));
00187                 return rr;
00188         }; 
00189         inline operator CoreObjs () const {
00190                 CComVariant k;
00191                 COMTHROW(p->get_AttributeValue(i,&k));
00192                 if(k.vt != VT_DISPATCH) COMTHROW(E_MGA_MODULE_INCOMPATIBILITY);
00193                 ASSERT(k.pdispVal);
00194                 return CoreObjs(k.pdispVal);
00195         }; 
00196 };
00197 
00198 inline CoreAttr CoreObj::operator[](int  id) const {
00199                 return CoreAttr(id, p);
00200 };
00201 
00202 inline CoreAttr CoreObj::operator[](attrid_type  id) const {
00203                 return CoreAttr(id, p);
00204 };
00205 
00206 inline bool CoreObj::IsRootFCO() const { return ((*this)[ATTRID_ROLEMETA] == METAREF_NULL); }
00207 inline bool CoreObj::IsRootFolder() const { return this->GetMetaID() == DTID_ROOT; }
00208 
00209 
00210 // ----------------------------------------
00211 // Non-member CoreObj functions
00212 // they are non-members mostly for historical reasons
00213 // ----------------------------------------
00214 
00215 metaid_type GetMetaID(const CoreObj &);
00216 
00217 inline CoreObj &Clr(CoreObj &x) { x.Release(); return x; }
00218 
00219 // get rootFCO with (optional) level
00220 void GetRootFCO(CoreObj &fco,  /* out */ CoreObj &rootFCO, int *level = NULL);
00221 
00222 // get the parent (and its distance from FCO) that is the root of current outgoing inheritance relationships
00223 // if nothing is derived from fco, return NULL for rootFCO and -1 for level
00224 void GetRootOfDeriv(CoreObj &fco,  /* out */ CoreObj &rootFCO, int *level = NULL);
00225 
00226 // check if FCO is contained by parentFCO, also true if parent == fco
00227 // level is only assigned if return value is true;
00228 bool IsContained(CoreObj &fco, CoreObj &parentFCO, int *level = NULL);
00229 bool IsFolderContained(CoreObj &fold, CoreObj &parentFold, int *level = NULL);
00230 
00231 // get the derived equivalent of 'objinbase' in 'subtype'  
00232 // level is the generation distance; not calculated if supplied by caller
00233 void GetDerivedEquivalent(CoreObj const &objinbase, CoreObj const &subtype, 
00234                                                   /*out*/ CoreObj &objinsubtype, int level = -1);
00235 // check if the original master of this relation is internal or external
00236 // does not check the internality of references whithin connections!!!!!!
00237 bool IsInternalRelation(CoreObj src);
00238