Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
3.7 KiB

  1. #ifndef DLGLOGIC_H
  2. #define DLGLOGIC_H
  3. #include "hwcmmn.h"
  4. #include <dpa.h>
  5. // DL: Data Logic
  6. class CDataImpl : public CRefCounted
  7. {
  8. public:
  9. CDataImpl();
  10. virtual ~CDataImpl();
  11. void _SetDirty(BOOL fDirty);
  12. BOOL IsDirty();
  13. void SetDeleted(BOOL fDeleted);
  14. BOOL IsDeleted();
  15. void SetNew(BOOL fNew);
  16. BOOL IsNew();
  17. // This will be called by clients just before "IsDirty" is called. The
  18. // implementation should call _SetDirty with the appropriate dirty status.
  19. virtual void UpdateDirty() PURE;
  20. // This should also reset the state of the object to a non-dirty state
  21. virtual HRESULT CommitChangesToStorage();
  22. virtual HRESULT AddToStorage();
  23. virtual HRESULT DeleteFromStorage();
  24. private:
  25. BOOL _fDirty;
  26. BOOL _fDeleted;
  27. BOOL _fNew;
  28. };
  29. // TData is usually derived from CDataImpl
  30. template<typename TData>
  31. class CDLUIData
  32. {
  33. public:
  34. HRESULT InitData(TData* pdata);
  35. TData* GetData();
  36. CDLUIData();
  37. virtual ~CDLUIData();
  38. private:
  39. TData* _pdata;
  40. };
  41. template<typename TData>
  42. class CDLManager
  43. {
  44. public:
  45. ~CDLManager();
  46. HRESULT AddDataObject(TData* pdata);
  47. virtual HRESULT Commit();
  48. BOOL IsDirty();
  49. protected:
  50. CDPA<TData>* _pdpaData;
  51. };
  52. // Implementations
  53. template<typename TData>
  54. HRESULT CDLUIData<TData>::InitData(TData* pdata)
  55. {
  56. ASSERT(pdata);
  57. pdata->AddRef();
  58. _pdata = pdata;
  59. return S_OK;
  60. }
  61. template<typename TData>
  62. TData* CDLUIData<TData>::GetData()
  63. {
  64. ASSERT(_pdata);
  65. _pdata->AddRef();
  66. return _pdata;
  67. }
  68. template<typename TData>
  69. CDLManager<TData>::~CDLManager()
  70. {
  71. if (_pdpaData)
  72. {
  73. _pdpaData->Destroy();
  74. delete _pdpaData;
  75. }
  76. }
  77. template<typename TData>
  78. HRESULT CDLManager<TData>::AddDataObject(TData* pdata)
  79. {
  80. HRESULT hr = S_OK;
  81. if (!_pdpaData)
  82. {
  83. _pdpaData = new CDPA<TData>(DPA_Create(4));
  84. if (!_pdpaData)
  85. {
  86. hr = E_OUTOFMEMORY;
  87. }
  88. }
  89. if (SUCCEEDED(hr))
  90. {
  91. if (-1 == _pdpaData->AppendPtr(pdata))
  92. {
  93. hr = E_FAIL;
  94. }
  95. }
  96. return hr;
  97. }
  98. template<typename TData>
  99. CDLUIData<TData>::CDLUIData()
  100. {}
  101. template<typename TData>
  102. CDLUIData<TData>::~CDLUIData()
  103. {
  104. if (_pdata)
  105. {
  106. _pdata->Release();
  107. }
  108. }
  109. template<typename TData>
  110. HRESULT CDLManager<TData>::Commit()
  111. {
  112. HRESULT hr = S_FALSE;
  113. if (_pdpaData)
  114. {
  115. int c = _pdpaData->GetPtrCount();
  116. for (int i = 0; SUCCEEDED(hr) && (i < c); ++i)
  117. {
  118. TData* pdata = _pdpaData->GetPtr(i);
  119. if (pdata)
  120. {
  121. pdata->UpdateDirty();
  122. if (pdata->IsDeleted())
  123. {
  124. hr = pdata->DeleteFromStorage();
  125. }
  126. else
  127. {
  128. if (pdata->IsNew())
  129. {
  130. hr = pdata->AddToStorage();
  131. }
  132. else
  133. {
  134. if (pdata->IsDirty())
  135. {
  136. hr = pdata->CommitChangesToStorage();
  137. }
  138. }
  139. }
  140. }
  141. }
  142. }
  143. return hr;
  144. }
  145. template<typename TData>
  146. BOOL CDLManager<TData>::IsDirty()
  147. {
  148. BOOL fDirty = FALSE;
  149. if (_pdpaData)
  150. {
  151. int c = _pdpaData->GetPtrCount();
  152. for (int i = 0; !fDirty && (i < c); ++i)
  153. {
  154. TData* pdata = _pdpaData->GetPtr(i);
  155. if (pdata)
  156. {
  157. pdata->UpdateDirty();
  158. if (pdata->IsDirty())
  159. {
  160. fDirty = TRUE;
  161. }
  162. }
  163. }
  164. }
  165. return fDirty;
  166. }
  167. #endif //DLGLOGIC_H