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.

361 lines
6.4 KiB

  1. /*++
  2. Copyright (c) 1994-1998 Microsoft Corporation
  3. Module Name :
  4. objplus.h
  5. Abstract:
  6. Base object class definitions
  7. Author:
  8. Ronald Meijer (ronaldm)
  9. Project:
  10. Internet Services Manager
  11. Revision History:
  12. --*/
  13. #ifndef _COMMON_H_
  14. #define _COMMON_H_
  15. //
  16. // Forward declarations
  17. //
  18. class CObjHelper;
  19. class CObjectPlus;
  20. class CObListPlus;
  21. class CObListIter;
  22. class COMDLL CObjHelper
  23. /*++
  24. Class Description:
  25. Helper class for control of construction and API errors
  26. Public Interface:
  27. IsValid : Determine if the object is in a valid state.
  28. operator BOOL : Boolean cast to IsValid()
  29. SetDirty : Set or reset the dirty flag
  30. IsDirty : Query the dirty state of the object
  31. QueryCreationTime : Query the creation time of the object
  32. QueryAge : Query the age of the object
  33. ReportError : Query/set construction failure
  34. QueryError : Query the error code of the object
  35. QueryApiErr : Query/set API error code
  36. ResetErrors : Reset all error codes
  37. SetApiErr : Echoes the error to the caller
  38. AssertValid : Assert the object is in a valid state (debug only)
  39. --*/
  40. {
  41. protected:
  42. //
  43. // Protected constructor: Not to be declared independently.
  44. //
  45. CObjHelper();
  46. public:
  47. virtual BOOL IsValid() const;
  48. operator BOOL();
  49. //
  50. // Update the Dirty flag
  51. //
  52. void SetDirty(
  53. IN BOOL fDirty = TRUE
  54. );
  55. //
  56. // Query the Dirty flag
  57. //
  58. BOOL IsDirty() const { return m_fDirty; }
  59. //
  60. // Return the creation time of this object
  61. //
  62. DWORD QueryCreationTime() const { return m_time_created; }
  63. //
  64. // Return the elapsed time this object has been alive.
  65. //
  66. DWORD QueryAge() const;
  67. //
  68. // Query/set construction failure
  69. //
  70. void ReportError(
  71. IN LONG errInConstruction
  72. );
  73. //
  74. // Fetch construction error
  75. //
  76. LONG QueryError() const { return m_ctor_err; }
  77. //
  78. // Query/set API errors.
  79. //
  80. LONG QueryApiErr() const { return m_api_err; }
  81. //
  82. // Reset all error conditions.
  83. //
  84. void ResetErrors();
  85. //
  86. // SetApiErr() echoes the error to the caller.
  87. // for use in expressions.
  88. //
  89. LONG SetApiErr(
  90. IN LONG errApi = ERROR_SUCCESS
  91. );
  92. #ifdef _DEBUG
  93. void AssertValid() const;
  94. #endif // _DEBUG
  95. protected:
  96. LONG m_ctor_err;
  97. LONG m_api_err;
  98. DWORD m_time_created;
  99. BOOL m_fDirty;
  100. };
  101. class COMDLL CObjectPlus : public CObject, public CObjHelper
  102. /*++
  103. Class Description:
  104. Super CObject class.
  105. Public Interface:
  106. CObjectPlus : Constructor
  107. Compare : Compare one object with another
  108. --*/
  109. {
  110. public:
  111. CObjectPlus();
  112. //
  113. // Compare one object with another
  114. //
  115. virtual int Compare(
  116. IN const CObjectPlus * pob
  117. ) const;
  118. //
  119. // Define a typedef for an ordering function.
  120. //
  121. typedef int (CObjectPlus::*PCOBJPLUS_ORDER_FUNC)(
  122. IN const CObjectPlus * pobOther
  123. ) const;
  124. };
  125. class COMDLL CObListPlus : public CObList, public CObjHelper
  126. /*++
  127. Class Description:
  128. Object pointer list which optionally "owns" the objects pointed to, and
  129. with facility to sort. If the list "owns" its objects, the destructor
  130. will clean up its member objects.
  131. Public Interface:
  132. CObListPlus : Constructor
  133. ~CObListPlus : Destructor
  134. SetOwnership : Set/reset ownership bit
  135. Index : Get object by index
  136. RemoveIndex : Remove object by index
  137. Remove : Remove object
  138. RemoveAt : Remove object at position
  139. RemoveAll : Remove all objects
  140. FindElement : Find object
  141. SetAll : Set/Reset the dirty flag of all objects
  142. AddTail : Add new object to the tail of the list
  143. Sort : Sort the list elements with sorting function provided
  144. --*/
  145. {
  146. //
  147. // Constructor/Destructor
  148. //
  149. public:
  150. CObListPlus(
  151. IN int nBlockSize = 10
  152. );
  153. virtual ~CObListPlus();
  154. //
  155. // Access
  156. //
  157. public:
  158. BOOL SetOwnership(
  159. IN BOOL fOwned = TRUE
  160. );
  161. //
  162. // Return object at the given index
  163. //
  164. CObject * Index(
  165. IN int index
  166. );
  167. //
  168. // Remove item the given index
  169. //
  170. BOOL RemoveIndex(
  171. IN int index
  172. );
  173. //
  174. // Remove the given object from the list
  175. //
  176. BOOL Remove(
  177. IN CObject * pob
  178. );
  179. //
  180. // Remove the item at the given position
  181. //
  182. void RemoveAt(
  183. IN POSITION & pos
  184. );
  185. //
  186. // Remove all items from the list
  187. //
  188. void RemoveAll();
  189. int FindElement(
  190. IN CObject * pobSought
  191. ) const;
  192. //
  193. // Set all elements to dirty or clean. Return TRUE if
  194. // any element was dirty.
  195. //
  196. BOOL SetAll(
  197. IN BOOL fDirty = FALSE
  198. );
  199. //
  200. // Sort the list elements according to the
  201. // given ordering function. Return error code
  202. //
  203. DWORD Sort(
  204. IN CObjectPlus::PCOBJPLUS_ORDER_FUNC pOrderFunc
  205. );
  206. protected:
  207. static int _cdecl SortHelper(
  208. IN const void * pa,
  209. IN const void * pb
  210. );
  211. protected:
  212. BOOL m_fOwned;
  213. };
  214. class COMDLL CObListIter : public CObjectPlus
  215. /*++
  216. Class Description:
  217. Object iteration class
  218. Public Interface:
  219. CObListIter : Constructor
  220. Next : Get next object
  221. Reset : Reset the iteration index
  222. QueryPosition : Query the current iteration index
  223. SetPosition : Set the current position in the list by POSITION
  224. --*/
  225. {
  226. public:
  227. CObListIter(
  228. IN const CObListPlus & obList
  229. );
  230. CObject * Next();
  231. void Reset();
  232. POSITION QueryPosition() const { return m_pos; }
  233. void SetPosition(
  234. IN POSITION pos
  235. );
  236. protected:
  237. POSITION m_pos;
  238. const CObListPlus & m_obList;
  239. };
  240. //
  241. // Inline Expansion
  242. //
  243. // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  244. inline CObjHelper::operator BOOL()
  245. {
  246. return IsValid();
  247. }
  248. inline void CObjHelper::SetDirty(
  249. IN BOOL fDirty
  250. )
  251. {
  252. m_fDirty = fDirty;
  253. }
  254. inline void CObjHelper::ResetErrors()
  255. {
  256. m_ctor_err = m_api_err = ERROR_SUCCESS;
  257. }
  258. inline BOOL CObListPlus::SetOwnership(
  259. IN BOOL fOwned
  260. )
  261. {
  262. BOOL fOld = m_fOwned;
  263. m_fOwned = fOwned;
  264. return fOld;
  265. }
  266. inline void CObListIter::SetPosition(
  267. IN POSITION pos
  268. )
  269. {
  270. m_pos = pos;
  271. }
  272. #endif // _COMMON_H