Leaked source code of windows server 2003
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.

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