GME  13
CoreAttribute.h
Go to the documentation of this file.
00001 
00002 #ifndef MGA_COREATTRIBUTE_H
00003 #define MGA_COREATTRIBUTE_H
00004 
00005 #include "StdAfx.h"
00006 
00007 #include "CoreTransactionItem.h"
00008 #include "CoreMetaAttribute.h"
00009 #include "CoreObject.h"
00010 #include "CoreDictionaryAttributeValue.h"
00011 
00012 #include <list>
00013 #include <set>
00014 
00015 
00016 class CCoreProject;
00017 class CCoreLockAttribute;
00018 class CCoreCollectionAttribute;
00019 class CCoreTerritory;
00020 class CCoreMetaAttribute;
00021 
00022 class CCoreAttributeErrorTearOff : 
00023         public ISupportErrorInfoImpl<&__uuidof(ICoreAttribute)>,
00024         public CComTearOffObjectBase<CCoreAttribute>
00025 {
00026 BEGIN_COM_MAP(CCoreAttributeErrorTearOff)
00027         COM_INTERFACE_ENTRY(ISupportErrorInfo)
00028 END_COM_MAP()
00029 };
00030 
00031 // --------------------------- CCoreAttribute
00032 #pragma pack(push)
00033 #pragma pack(1)
00034 
00035 class ATL_NO_VTABLE CCoreAttribute : 
00036         public CComObjectRootEx<CComSingleThreadModel>,
00037         public IDispatchImpl<ICoreAttribute, &__uuidof(ICoreAttribute), &__uuidof(__MGACoreLib)>
00038 {
00039 public:
00040         CCoreAttribute();
00041         virtual ~CCoreAttribute();
00042 
00043 #ifdef DEBUG
00044         char footprint[4];
00045 #endif
00046 
00047         static void Create(CCoreObject *object, CCoreMetaAttribute *metaattribute);
00048 
00049 DECLARE_GET_CONTROLLING_UNKNOWN()
00050 
00051 BEGIN_COM_MAP(CCoreAttribute)
00052         COM_INTERFACE_ENTRY(ICoreAttribute)
00053         COM_INTERFACE_ENTRY(IDispatch)
00054         COM_INTERFACE_ENTRY_TEAR_OFF(IID_ISupportErrorInfo, CCoreAttributeErrorTearOff)
00055 END_COM_MAP()
00056 
00057 // ------- COM methods
00058 
00059 public:
00060         STDMETHOD(get_Object)(ICoreObject **p);
00061         STDMETHOD(get_MetaAttribute)(ICoreMetaAttribute **p);
00062 
00063 // ------- Properties
00064 
00065 protected:
00066         CComObjPtr<CCoreMetaAttribute> metaattribute;
00067 
00068         typedef unsigned char status_type;
00069         status_type status;
00070 
00071 // ------- Methods
00072 
00073 public:
00074         // CCoreAttributes are always used as CComPartObjects
00075         // where the m_pOuterUnknown points the owner
00076         CCoreObject *GetObject() const NOTHROW 
00077         {
00078                 ASSERT( m_pOuterUnknown ); 
00079                 return CastToObject(m_pOuterUnknown); 
00080         }
00081 
00082         CCoreMetaAttribute *GetMetaAttribute() const NOTHROW { ASSERT( metaattribute ); return metaattribute; }
00083         attrid_type GetAttrID() const NOTHROW { ASSERT( metaattribute ); return metaattribute->attrid; }
00084         
00085         virtual valtype_type GetValType() const NOTHROW = 0;
00086         virtual CCoreLockAttribute *GetLockAttr() const;
00087 
00088         ICoreStorage *SetStorageThisAttribute();
00089 
00090         // if type mismatch then returns false
00091         virtual bool DoesMatch(bool do_load, const VARIANT &v) { return false; }
00092 
00093 #ifdef _ATL_DEBUG_INTERFACES
00094         HRESULT FinalConstruct()
00095         {
00096                 m_pOuterUnknown->AddRef();
00097                 return S_OK;
00098         }
00099 #endif
00100 
00101 // ------- Inline
00102 
00103 public:
00104         CCoreProject *GetProject() const NOTHROW;
00105         CCoreTerritory *GetTerritory() const NOTHROW;
00106 
00107 // ------- Status
00108 
00109 enum COREATTRIBUTE_LOCK_STATUS {
00110         COREATTRIBUTE_DIRTY = 0x0001,
00111         COREATTRIBUTE_LOCKGROUP_LOADED = 0x0002,
00112         COREATTRIBUTE_COLL_UPTODATE = 0x0004,
00113         COREATTRIBUTE_LOCK_CLOSED = 0x0008
00114 };
00115 
00116 public:
00117         void SetStatusFlag(status_type flags) NOTHROW {
00118                 status |= flags;
00119         }
00120         void ResetStatusFlag(status_type flags) NOTHROW {
00121                 status &= ~flags;
00122         }
00123 
00124         void ChangeStatusFlag(status_type flag, bool set) NOTHROW {
00125                 if(set)
00126                         status |= flag;
00127                 else
00128                         status &= ~flag;
00129         }
00130         bool GetStatusFlag(status_type flag) const NOTHROW { 
00131                 return (status & flag) != 0; 
00132         }
00133 
00134 public:
00135         bool InTransaction() const NOTHROW;
00136         bool InWriteTransaction() const NOTHROW;
00137         bool IsZombie() const NOTHROW;
00138 
00139         bool IsDirty() const NOTHROW { 
00140                 return GetStatusFlag(COREATTRIBUTE_DIRTY);
00141         }
00142         void SetDirty() NOTHROW { 
00143                 SetStatusFlag(COREATTRIBUTE_DIRTY); 
00144         }
00145         void ResetDirty() NOTHROW { 
00146                 ResetStatusFlag(COREATTRIBUTE_DIRTY);
00147         }
00148         void ChangeDirty(bool dirty) { 
00149                 ChangeStatusFlag(COREATTRIBUTE_DIRTY, dirty); 
00150         }
00151 
00152 // ------- FinalTrItem
00153 
00154 public:
00155         virtual void AbortFinalTransaction() NOTHROW { }
00156         virtual void CommitFinalTransaction() { }
00157         virtual void CommitFinalTransactionFinish(bool undo) NOTHROW { }
00158 
00159 // ------- Storage
00160 
00161 public:
00162         virtual void Load() { }
00163         virtual void Unload() NOTHROW { }
00164 
00165         virtual void FillAfterCreateObject() NOTHROW { }
00166 
00167 // ------- Debug
00168 
00169 #ifdef _DEBUG
00170 
00171 public:
00172         virtual void Dump();
00173 #endif
00174 };
00175 
00176 #pragma pack(pop)
00177 
00178 // --------------------------- CCoreLockAttribute
00179 
00180 class ATL_NO_VTABLE CCoreLockAttribute :
00181         public CCoreAttribute
00182 {
00183 public:
00184         CCoreLockAttribute();
00185 #ifdef _DEBUG
00186         virtual ~CCoreLockAttribute();
00187 #endif
00188 
00189 protected:
00190         typedef unsigned short lock_count_type;
00191         locking_type original_locking;  // local locking when enterging dirty state
00192 
00193         lock_count_type read_count;
00194         lock_count_type write_count;
00195 
00196         lockval_type others_lockval;    // original lockval minus local locking
00197 
00198 public:
00199         STDMETHOD(get_Value)(VARIANT *p);
00200         STDMETHOD(put_Value)(VARIANT p);
00201         STDMETHOD(get_LoadedValue)(VARIANT *p) { return E_NOTIMPL; }
00202         STDMETHOD(get_PreviousValue)(VARIANT *p) { return E_NOTIMPL; }
00203         STDMETHOD(get_PeerLockValue)(locking_type *p);
00204 
00205         virtual valtype_type GetValType() const NOTHROW { return VALTYPE_LOCK; }
00206         virtual CCoreLockAttribute *GetLockAttr() const NOTHROW { return NULL; }
00207 
00208         void RegisterLockTry(locking_type unreg, locking_type reg);
00209         void RegisterLockDo(locking_type unreg, locking_type reg) NOTHROW;
00210 
00211         bool IsLoaded() const NOTHROW { return IsDirty() || read_count > 0 || write_count > 0; }
00212         bool IsEmpty() const NOTHROW { return read_count == 0 && write_count == 0; }
00213 
00214         void ControlLockGroup();
00215         void ControlLockGroupDo() NOTHROW;
00216 
00217         virtual void Load();
00218         virtual void Unload() NOTHROW;
00219         void Save();
00220 
00221         virtual void FillAfterCreateObject() NOTHROW;
00222 
00223 // ------- FinalTrItem
00224 
00225 public:
00226         virtual void AbortFinalTransaction() NOTHROW { Unload(); }
00227         virtual void CommitFinalTransaction();
00228         virtual void CommitFinalTransactionFinish(bool undo) NOTHROW { Unload(); }
00229 
00230 // ------- Locking
00231 
00232 public:
00233 #ifdef _DEBUG
00234         lockval_type CalcLock(locking_type locking, lockval_type lockval);
00235         lockval_type CalcUnlock(locking_type locking, lockval_type lockval);
00236 #else
00237         static lockval_type CalcLock(locking_type locking, lockval_type lockval);
00238         static lockval_type CalcUnlock(locking_type locking, lockval_type lockval);
00239 #endif
00240         static locking_type CalcLocking(lockval_type lockval);
00241         static locking_type CombineLock(lock_count_type read_count,
00242                 lock_count_type write_count) NOTHROW;
00243 
00244 // ------- Debug
00245 
00246 #ifdef _DEBUG
00247 
00248 public:
00249         virtual void Dump();
00250 
00251 #endif
00252 };
00253 
00254 // --------------------------- CCoreDataAttrBase
00255 
00256 template<class DATA>
00257 class ATL_NO_VTABLE CCoreDataAttrBase :
00258         public CCoreAttribute
00259 {
00260 // ------- Properties
00261 
00262 public:
00263         typedef DATA value_type;
00264 
00265         typedef core::list<value_type> values_type;
00266         typedef typename values_type::iterator values_iterator;
00267 
00268 protected:
00269         values_type values;
00270 
00271 // ------- CopyTo
00272 
00273 public:
00274         void UserCopyTo(const DATA &a, VARIANT *v) { ::CopyTo(a, v); }
00275         void StorageCopyTo(const DATA &a, VARIANT *v) { ::CopyTo(a, v); }
00276 
00277 // ------- Low level
00278 
00279 public:
00280         void LockSelfTry();
00281         void LockSelfCancel() NOTHROW;
00282         void UnlockSelfTry();
00283         void UnlockSelfCancel() NOTHROW;
00284         void UnlockSelfDo() NOTHROW;
00285 
00286 // ------- Medium level
00287 
00288 public:
00289         void ChangeFrontValue(VARIANT &v);
00290         DATA CreateValue();
00291         void InsertFrontValue(VARIANT &v);
00292 
00293         void SpliceValue(values_iterator before, values_iterator pos) NOTHROW;
00294 
00295         void RemoveValueTry(values_iterator pos);
00296         void RemoveValueCancel(values_iterator pos) NOTHROW;
00297         void RemoveValueFinish(values_iterator pos) NOTHROW;
00298         void RemoveValue(values_iterator pos);
00299         void RemoveValueDo(values_iterator pos) NOTHROW;
00300 
00301 // ------- Methods
00302 
00303 public:
00304         bool IsLoaded() const NOTHROW { return !values.empty(); }
00305 
00306         // does not load, only compares
00307         static bool DoesMatchBase(const value_type &a, const VARIANT &v) NOTHROW;
00308 
00309 // ------- Debug
00310 
00311 #ifdef _DEBUG
00312 
00313 public:
00314         virtual void Dump() { }
00315 
00316 #endif
00317 };
00318 
00319 // --------------------------- CCorePointerAttrBase
00320 
00321 class ATL_NO_VTABLE CCorePointerAttrBase :
00322         public CCoreAttribute
00323 {
00324 public:
00325         CCorePointerAttrBase();
00326 #ifdef _DEBUG
00327         virtual ~CCorePointerAttrBase();
00328 #endif
00329 
00330 // ------- Properties
00331 
00332 public:
00333         typedef CComObjPtr<CCoreCollectionAttribute> value_type;
00334 
00335         typedef core::list<value_type> values_type;
00336         typedef values_type::iterator values_iterator;
00337 
00338         typedef std::set< CCoreObject*, ptr_compare<CCoreObject> > objects_type;
00339         typedef objects_type::iterator objects_iterator;
00340 
00341 protected:
00342         bool isEmpty;
00343         values_type values;
00344         objects_iterator backref;
00345 
00346 // ------- CopyTo
00347 
00348 public:
00349         void UserCopyTo(CCoreCollectionAttribute* const p, VARIANT *v);
00350         void UserCopyTo(const VARIANT &v, CComObjPtr<CCoreCollectionAttribute> &p);
00351 
00352         void StorageCopyTo(CCoreCollectionAttribute* const p, VARIANT *v);
00353         void StorageCopyTo(const VARIANT &v, CComObjPtr<CCoreCollectionAttribute> &p);
00354 
00355 // ------- Low level
00356 
00357 public:
00358         void InsertIntoCollection() NOTHROW;
00359         void RemoveFromCollection() NOTHROW;
00360         void CollectionNotUpToDate() NOTHROW;
00361 
00362         void LockCollectionTry(CCoreCollectionAttribute *p);
00363         void LockCollectionCancel(CCoreCollectionAttribute *p) NOTHROW;
00364         void UnlockCollectionTry(CCoreCollectionAttribute *p);
00365         void UnlockCollectionCancel(CCoreCollectionAttribute *p) NOTHROW;
00366         void UnlockCollectionDo(CCoreCollectionAttribute *p) NOTHROW;
00367 
00368         void LockSelfTry();
00369         void LockSelfCancel() NOTHROW;
00370         void UnlockSelfTry();
00371         void UnlockSelfCancel() NOTHROW;
00372         void UnlockSelfDo() NOTHROW;
00373 
00374 // ------- Medium level
00375 
00376 public:
00377         void ChangeFrontValue(VARIANT &v);
00378         void InsertFrontValue(VARIANT &v);
00379 
00380         void SpliceValue(values_iterator before, values_iterator pos) NOTHROW;
00381 
00382         void RemoveValueTry(values_iterator pos);
00383         void RemoveValueCancel(values_iterator pos) NOTHROW;
00384         void RemoveValueFinish(values_iterator pos) NOTHROW;
00385         void RemoveValue(values_iterator pos);
00386         void RemoveValueDo(values_iterator pos) NOTHROW;
00387 
00388 // ------- Methods
00389 
00390 public:
00391         bool IsEmpty() const NOTHROW { return isEmpty; } // used to be: return backref == objects_iterator(NULL);
00392         bool IsLoaded() const NOTHROW { return !values.empty(); }
00393 
00394         // does not load, only compares
00395         static bool DoesMatchBase(const value_type &a, const VARIANT &v) NOTHROW;
00396 };
00397 
00398 // --------------------------- CCoreDataAttribute
00399 
00400 template<class BASE, const int VALTYPE>
00401 class ATL_NO_VTABLE CCoreDataAttribute : 
00402         public /*typename*/ BASE,//z!
00403         // public CCoreTransactionItem,
00404         public CCoreUndoItem
00405 {
00406 public:
00407         typedef typename BASE::value_type value_type;
00408 
00409 // ------- Methods
00410 
00411 public:
00412         virtual valtype_type GetValType() const NOTHROW { return VALTYPE; }
00413 
00414         STDMETHODIMP get_Value(VARIANT *p);
00415         STDMETHODIMP put_Value(VARIANT p);
00416         STDMETHODIMP get_LoadedValue(VARIANT *p);
00417         STDMETHODIMP get_PreviousValue(VARIANT *p);
00418 
00419         const value_type &GetValue() const { ASSERT( !values.empty() ); return values.front(); }
00420 
00421         // if type mismatch then returns false
00422         virtual bool DoesMatch(bool do_load, const VARIANT &v);
00423 
00424 // ------- Storage
00425 
00426 public:
00427         virtual void Load();
00428         virtual void Unload() NOTHROW;
00429         void Save(value_type &value);
00430 
00431         virtual void FillAfterCreateObject() NOTHROW;
00432 
00433 // ------- NestedTrItem
00434 
00435 public:
00436         virtual bool IsDirty() const NOTHROW { return CCoreAttribute::IsDirty(); }
00437         virtual void ChangeDirty(bool dirty) NOTHROW { CCoreAttribute::ChangeDirty(dirty); }
00438 
00439         virtual void AbortNestedTransaction() NOTHROW;
00440         virtual void DiscardPreviousValue() NOTHROW;
00441 
00442 // ------- FinalTrItem
00443 
00444 public:
00445         virtual void AbortFinalTransaction() NOTHROW;
00446         virtual void CommitFinalTransaction();
00447         virtual void CommitFinalTransactionFinish(bool undo) NOTHROW;
00448 
00449 // ------- UndoItem
00450 
00451 public:
00452         virtual void UndoTransaction();
00453         virtual void UndoTransactionFinish() NOTHROW;
00454 
00455         virtual void RedoTransaction();
00456         virtual void RedoTransactionFinish() NOTHROW;
00457 
00458         virtual void DiscardLastItem();
00459         virtual void DiscardLastItemFinish() NOTHROW;
00460         virtual void DiscardLastItemCancel() NOTHROW;
00461 };
00462 
00463 // --------------------------- CCoreCollectionAttribute
00464 
00465 class ATL_NO_VTABLE CCoreCollectionAttribute :
00466         public CCoreAttribute
00467 {
00468 #ifdef _DEBUG
00469 public:
00470         ~CCoreCollectionAttribute();
00471 #endif
00472 
00473 public:
00474         typedef std::set< CCoreObject*, ptr_compare<CCoreObject> > objects_type;
00475         typedef objects_type::iterator objects_iterator;
00476 
00477 public:
00478         objects_type collection;
00479 
00480         objects_type &GetCollection() NOTHROW { return collection; }
00481 
00482         virtual valtype_type GetValType() const NOTHROW { return VALTYPE_COLLECTION; }
00483 
00484 public:
00485         STDMETHOD(get_Value)(VARIANT *p);
00486         STDMETHOD(put_Value)(VARIANT p);
00487         STDMETHOD(get_LoadedValue)(VARIANT *p);
00488         STDMETHOD(get_PreviousValue)(VARIANT *p) { return E_NOTIMPL; }
00489 
00490 // ------ Low level
00491 
00492 public:
00493         void CopyCollectionFromMemory(VARIANT &v);
00494         void CopyCollectionFromStorage(VARIANT &v);
00495         bool IsEmptyFromStorage();
00496 
00497         inline void NotUpToDate() NOTHROW { ResetStatusFlag(COREATTRIBUTE_COLL_UPTODATE); }
00498 };
00499 
00500 #endif//MGA_COREATTRIBUTE_H