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.

338 lines
7.4 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. props.h
  5. Abstract:
  6. This module contains the definition of the property search class
  7. Author:
  8. Keith Lau (keithlau@microsoft.com)
  9. Revision History:
  10. keithlau 07/05/97 created
  11. --*/
  12. #ifndef _PROPS_H_
  13. #define _PROPS_H_
  14. // Define a generic accessor function to access properties
  15. typedef HRESULT (*GET_ACCESSOR_FUNCTION)( LPSTR pszName,
  16. LPVOID pContext,
  17. LPVOID pCacheData,
  18. LPVOID pvBuffer,
  19. LPDWORD pdwBufferLen);
  20. typedef HRESULT (*SET_ACCESSOR_FUNCTION)( LPSTR pszName,
  21. LPVOID pCacheData,
  22. LPVOID pvBuffer,
  23. DWORD dwBufferLen,
  24. DWORD ptPropertyType);
  25. typedef HRESULT (*COMMIT_ACCESSOR_FUNCTION)(LPSTR pszName,
  26. LPVOID pContext,
  27. LPVOID pCacheData);
  28. typedef HRESULT (*INVALIDATE_ACCESSOR_FUNCTION)(LPSTR pszName,
  29. LPVOID pCacheData,
  30. DWORD ptPropertyType);
  31. // Define the property item structure. We can hash this if
  32. // we want in the future
  33. typedef struct _PROPERTY_ITEM
  34. {
  35. LPSTR pszName;
  36. DWORD ptBaseType;
  37. DWORD fAccess;
  38. DWORD dwIndex;
  39. GET_ACCESSOR_FUNCTION pfnGetAccessor;
  40. SET_ACCESSOR_FUNCTION pfnSetAccessor;
  41. COMMIT_ACCESSOR_FUNCTION pfnCommitAccessor;
  42. INVALIDATE_ACCESSOR_FUNCTION pfnInvalidateAccessor;
  43. } PROPERTY_ITEM, *LPPROPERTY_ITEM;
  44. typedef struct _PROPERTY_DATA
  45. {
  46. LPVOID pContext;
  47. LPVOID pCacheData;
  48. } PROPERTY_DATA, *LPPROPERTY_DATA;
  49. // Define a property context
  50. typedef struct _PROP_CTXT
  51. {
  52. LPPROPERTY_ITEM pItem;
  53. BOOL fIsWideStr;
  54. LPSTR pszDefaultPropertyName;
  55. } PROP_CTXT, *LPPROP_CTXT;
  56. // Define a generic structure to define a set of properties
  57. typedef struct _PTABLE
  58. {
  59. LPPROPERTY_ITEM pProperties; // Actual property table
  60. LPPROPERTY_DATA pPropertyData; // Prop data
  61. DWORD dwProperties; // Count
  62. BOOL fIsSorted; // Sorted prop table?
  63. } PTABLE, *LPPTABLE;
  64. // =================================================================
  65. // Generic cache class
  66. //
  67. class CGenericCache
  68. {
  69. public:
  70. CGenericCache(LPVOID pvDefaultContext) {}
  71. ~CGenericCache() {}
  72. virtual LPPROPERTY_DATA GetCacheBlock() = 0;
  73. };
  74. // =================================================================
  75. // Generic property table
  76. //
  77. class CGenericPTable
  78. {
  79. public:
  80. CGenericPTable(CGenericCache *pCache) {}
  81. ~CGenericPTable() {}
  82. virtual LPPTABLE GetPTable() = 0;
  83. };
  84. // =================================================================
  85. // Definition of an in-house string structure
  86. //
  87. typedef struct _STRING_ATTR
  88. {
  89. LPSTR pszValue;
  90. DWORD dwMaxLen;
  91. } STRING_ATTR, *LPSTRING_ATTR;
  92. // Enumerated types representing type of access on property
  93. typedef enum _PROPERTY_ACCESS
  94. {
  95. PA_READ = 1,
  96. PA_WRITE = 2,
  97. PA_READ_WRITE = 3
  98. } _PROPERTY_ACCESS;
  99. // Enumerated types representing property types
  100. typedef enum _PROPERTY_TYPES
  101. {
  102. PT_NONE = 0,
  103. PT_STRING,
  104. PT_DWORD,
  105. PT_INTERFACE,
  106. PT_DEFAULT,
  107. PT_MAXPT
  108. } PROPERTY_TYPES;
  109. // =================================================================
  110. // Definition of an in-house string class, this is used for caching
  111. //
  112. class CPropertyValueString
  113. {
  114. public:
  115. CPropertyValueString()
  116. {
  117. m_pszValue = NULL;
  118. m_dwLength = 0;
  119. m_dwMaxLen = 0;
  120. m_fChanged = FALSE;
  121. }
  122. ~CPropertyValueString()
  123. {
  124. if (m_pszValue)
  125. LocalFree(m_pszValue);
  126. m_pszValue = NULL;
  127. }
  128. // Overload the assignment to abstract the implementation
  129. const CPropertyValueString& operator=(LPSTR szValue);
  130. // Users can call copy if they desire to
  131. BOOL Copy(LPSTR pszSrc, DWORD dwLength /* Optional */);
  132. void Invalidate()
  133. {
  134. if (m_pszValue)
  135. LocalFree(m_pszValue);
  136. m_pszValue = NULL;
  137. m_fChanged = FALSE;
  138. }
  139. // We make these directly accessible
  140. LPSTR m_pszValue;
  141. DWORD m_dwLength;
  142. DWORD m_dwMaxLen;
  143. BOOL m_fChanged;
  144. };
  145. // =================================================================
  146. // Definition of an in-house DWORD class, this is used for caching
  147. //
  148. class CPropertyValueDWORD
  149. {
  150. public:
  151. CPropertyValueDWORD()
  152. {
  153. m_dwValue = 0;
  154. m_fInit = FALSE;
  155. m_fChanged = TRUE;
  156. m_punkScratch = NULL;
  157. }
  158. ~CPropertyValueDWORD()
  159. {
  160. if (m_fInit && m_punkScratch)
  161. m_punkScratch->Release();
  162. m_punkScratch = NULL;
  163. }
  164. // Overload the assignment to abstract the implementation
  165. const CPropertyValueDWORD& operator=(DWORD dwValue)
  166. {
  167. m_dwValue = dwValue;
  168. m_fInit = TRUE;
  169. m_fChanged = TRUE;
  170. return(*this);
  171. }
  172. void Invalidate()
  173. {
  174. if (m_fInit && m_punkScratch)
  175. m_punkScratch->Release();
  176. m_punkScratch = NULL;
  177. m_fInit = FALSE;
  178. m_fChanged = FALSE;
  179. }
  180. // We make these directly accessible
  181. DWORD m_dwValue;
  182. BOOL m_fInit;
  183. BOOL m_fChanged;
  184. IUnknown *m_punkScratch; // HACK: for interfaces only
  185. };
  186. // Size of default scratch buffer
  187. #define DEFAULT_SCRATCH_BUFFER_SIZE 1024
  188. // =================================================================
  189. // class for searching properties
  190. //
  191. class CPropertyTable
  192. {
  193. public:
  194. CPropertyTable()
  195. {
  196. // Set up the default scratch pad
  197. m_szBuffer = m_rgcBuffer;
  198. m_cBuffer = DEFAULT_SCRATCH_BUFFER_SIZE;
  199. }
  200. BOOL Init(LPPROPERTY_ITEM pProperties,
  201. LPPROPERTY_DATA pData,
  202. DWORD dwcProperties,
  203. LPVOID pvDefaultContext,
  204. BOOL fIsSorted = FALSE)
  205. {
  206. m_pProperties = pProperties;
  207. m_pData = pData;
  208. m_dwProperties = dwcProperties;
  209. m_fIsSorted = fIsSorted;
  210. // Set up default context for properties
  211. for (DWORD i = 0; i < dwcProperties; i++)
  212. m_pData[i].pContext = pvDefaultContext;
  213. return(TRUE);
  214. }
  215. ~CPropertyTable()
  216. {
  217. // Wipe out members
  218. m_pProperties = NULL;
  219. m_dwProperties = 0;
  220. // Free the scratch buffer, if not equal to default
  221. if (m_szBuffer != m_rgcBuffer)
  222. {
  223. LocalFree((HLOCAL)m_szBuffer);
  224. }
  225. }
  226. // Method to get the property type given the property name
  227. HRESULT GetPropertyType(LPCSTR szPropertyName,
  228. LPDWORD pptPropertyType,
  229. LPPROP_CTXT pPropertyContext);
  230. HRESULT GetPropertyType(LPCWSTR wszPropertyName,
  231. LPDWORD pptPropertyType,
  232. LPPROP_CTXT pPropertyContext);
  233. // Method to retrieve the associated property item
  234. HRESULT GetProperty(LPPROP_CTXT pPropertyContext,
  235. LPVOID pvBuffer,
  236. LPDWORD pdwBufferLen);
  237. // Method to set the associated property item
  238. HRESULT SetProperty(LPCSTR szPropertyName,
  239. LPVOID pvBuffer,
  240. DWORD dwBufferLen,
  241. DWORD ptPropertyType);
  242. HRESULT SetProperty(LPCWSTR wszPropertyName,
  243. LPVOID pvBuffer,
  244. DWORD dwBufferLen,
  245. DWORD ptPropertyType);
  246. // Method to commit all changes. This must be called or
  247. // all the changes will be lost
  248. HRESULT CommitChanges();
  249. // Method to rollback changes to the initial state or the
  250. // state after the last commit, whichever is more recent
  251. HRESULT Invalidate();
  252. private:
  253. // Method to obtain a scratch buffer of the desired size,
  254. // will allocate new one if insufficient. Size in bytes.
  255. LPVOID GetScratchBuffer(DWORD dwSizeDesired);
  256. // Method to search the property table and return the associated
  257. // property item, if found
  258. LPPROPERTY_ITEM SearchForProperty(LPCSTR szPropertyName);
  259. // Pointer to property table and count of items
  260. LPPROPERTY_ITEM m_pProperties;
  261. LPPROPERTY_DATA m_pData;
  262. DWORD m_dwProperties;
  263. // TRUE if the table of properties is sorted, will use
  264. // binary search if so. Otherwise, a linear scan is performed
  265. BOOL m_fIsSorted;
  266. // Default scratch buffer, used for wide string to LPSTR
  267. // conversion
  268. CHAR m_rgcBuffer[DEFAULT_SCRATCH_BUFFER_SIZE];
  269. // Pointer to current scratch buffer, will be freed by
  270. // destructor if not equal to m_rgcBuffer
  271. LPSTR m_szBuffer;
  272. DWORD m_cBuffer;
  273. };
  274. #endif