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.

326 lines
12 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1992-1998 Microsoft Corporation
  4. //
  5. // File: ary.hxx
  6. //
  7. // Contents: CFormsAry* classes
  8. //
  9. //----------------------------------------------------------------------------
  10. #ifndef _HXX_CARY
  11. #define _HXX_CARY
  12. class CFormsAry;
  13. inline size_t
  14. MemGetSize(void *pv)
  15. {
  16. if (!pv)
  17. return 0;
  18. return (size_t)::GlobalSize(GlobalPtrHandle(pv));
  19. }
  20. inline void *
  21. MemAlloc(size_t cb)
  22. {
  23. return(GlobalAllocPtr(GMEM_MOVEABLE, cb));
  24. }
  25. inline void *
  26. MemAllocClear(size_t cb)
  27. {
  28. return(GlobalAllocPtr(GPTR, cb));
  29. }
  30. inline void
  31. MemFree(void *pv)
  32. {
  33. if (pv)
  34. GlobalFreePtr(pv);
  35. }
  36. inline HRESULT
  37. MemRealloc(void **ppv, size_t cb)
  38. {
  39. LPVOID pv = *ppv;
  40. if (pv)
  41. pv = GlobalReAllocPtr(pv, cb, GMEM_MOVEABLE);
  42. else
  43. pv = MemAlloc(cb);
  44. if (pv)
  45. {
  46. *ppv = pv;
  47. return S_OK;
  48. }
  49. return HRESULT_FROM_WIN32(::GetLastError());
  50. }
  51. #define ULREF_IN_DESTRUCTOR 256
  52. #define DECLARE_FORMS_IUNKNOWN_METHODS \
  53. STDMETHOD(QueryInterface) (REFIID riid, LPVOID * ppv); \
  54. STDMETHOD_(ULONG, AddRef) (void); \
  55. STDMETHOD_(ULONG, Release) (void);
  56. #define DECLARE_FORMS_STANDARD_IUNKNOWN(cls) \
  57. STDMETHOD(QueryInterface) (REFIID riid, LPVOID * ppv); \
  58. ULONG _ulRefs; \
  59. STDMETHOD_(ULONG, AddRef) (void) \
  60. { \
  61. return ++_ulRefs; \
  62. } \
  63. STDMETHOD_(ULONG, Release) (void) \
  64. { \
  65. if (--_ulRefs == 0) \
  66. { \
  67. _ulRefs = ULREF_IN_DESTRUCTOR; \
  68. delete this; \
  69. return 0; \
  70. } \
  71. return _ulRefs; \
  72. } \
  73. ULONG GetRefs(void) \
  74. { return _ulRefs; }
  75. //+------------------------------------------------------------------------
  76. //
  77. // Class: CFormsAry (ary)
  78. //
  79. // Purpose: Generic resizeable array class. Note that most of
  80. // the functionality in this class is provided in
  81. // protected members. The CPtrAry and CDataAry templates
  82. // define concrete implementations of the array class.
  83. // In these concrete classes, public methods are provided
  84. // which delegate to the protected methods. This allows
  85. // us to avoid storing the element size with the array.
  86. //
  87. // Interface:
  88. // CFormsAry, ~CFormsAry
  89. //
  90. // EnsureSize Ensures that the array is at least a certain
  91. // size, allocating more memory if necessary.
  92. // Note that the array size is measured in elements,
  93. // rather than in bytes.
  94. // Size Returns the current size of the array.
  95. // SetSize Sets the array size; EnsureSize must be called
  96. // first to reserve space if the array is growing
  97. //
  98. // operator void * Allow the CFormsAry class to be cast
  99. // to a (void *)
  100. //
  101. // Append Adds a new pointer to the end of the array,
  102. // growing the array if necessary. Only works
  103. // for arrays of pointers.
  104. // AppendIndirect As Append, for non-pointer arrays
  105. //
  106. // Insert Inserts a new pointer at the given index in
  107. // the array, growing the array if necessary. Any
  108. // elements at or following the index are moved
  109. // out of the way.
  110. // InsertIndirect As Insert, for non-pointer arrays
  111. //
  112. // Delete Deletes an element of the array, moving any
  113. // elements that follow it to fill
  114. // DeleteMultiple Deletes a range of elements from the array,
  115. // moving to fill
  116. // DeleteByValue Delete element matching given value.
  117. // DeleteByValueIndirect As DeleteByValue, for non-pointer arrays.
  118. // BringToFront Moves an element of the array to index 0,
  119. // shuffling elements to make room
  120. // SendToBack Moves an element to the end of the array,
  121. // shuffling elements to make room
  122. // Swap swaps two elements
  123. //
  124. // Find Returns the index at which a given pointer
  125. // is found
  126. // FindIndirect As Find, for non-pointer arrays
  127. //
  128. // CopyAppend Appends data from another array to the end
  129. // Copy Creates a copy of the object.
  130. // CopyAppendIndirect Appends data from a c-style array
  131. // CopyIndirect Creates a copy of a c-style array
  132. //
  133. // EnumElements Create an enumerator which supports the given
  134. // interface ID for the contents of the array
  135. //
  136. // EnumElements Create an IEnumVARIANT enumerator.
  137. //
  138. //
  139. // Deref Returns a pointer to an element of the array;
  140. // normally used by type-safe methods in derived
  141. // classes
  142. //
  143. // GetAlloced Get number of elements allocated
  144. //
  145. // Members: _c Current size of the array
  146. // _pv Buffer storing the elements
  147. //
  148. // Note: The CFormsAry class only supports arrays of elements
  149. // whose size is less than 128.
  150. //
  151. //-------------------------------------------------------------------------
  152. class CFormsAry
  153. {
  154. // friend class CBaseEnum;
  155. // friend class CEnumGeneric;
  156. // friend class CEnumVARIANT;
  157. public:
  158. ~CFormsAry();
  159. int Size() const { return _c; }
  160. void SetSize(int c) { _c = c;}
  161. operator void *() { return PData(); }
  162. void DeleteAll();
  163. void * Deref(size_t cb, int i);
  164. protected:
  165. // Methods which are wrapped by inline subclass methods
  166. CFormsAry() { _c = 0; PData() = 0; }
  167. HRESULT EnsureSize(size_t cb, int c);
  168. HRESULT AppendIndirect(size_t cb, void * pv);
  169. HRESULT InsertIndirect(size_t cb, int i, void * pv);
  170. int FindIndirect(size_t cb, void *);
  171. void Delete(size_t cb, int i);
  172. BOOL DeleteByValueIndirect(size_t cb, void *pv);
  173. void DeleteMultiple(size_t cb, int start, int end);
  174. void BringToFront(size_t cb, int i);
  175. void SendToBack(size_t cb, int i);
  176. void Swap(size_t cb, int i1, int i2);
  177. HRESULT CopyAppend(size_t cb, const CFormsAry& ary, BOOL fAddRef);
  178. HRESULT Copy(size_t cb, const CFormsAry& ary, BOOL fAddRef);
  179. HRESULT CopyAppendIndirect(size_t cb, int c, void * pv, BOOL fAddRef);
  180. HRESULT CopyIndirect(size_t cb, int c, void * pv, BOOL fAddRef);
  181. int GetAlloced(size_t cb)
  182. { return MemGetSize(PData()) / cb; }
  183. HRESULT EnumElements(
  184. size_t cb,
  185. REFIID iid,
  186. void ** ppv,
  187. BOOL fAddRef,
  188. BOOL fCopy = TRUE,
  189. BOOL fDelete = TRUE);
  190. HRESULT EnumVARIANT(
  191. size_t cb,
  192. VARTYPE vt,
  193. IEnumVARIANT ** ppenum,
  194. BOOL fCopy = TRUE,
  195. BOOL fDelete = TRUE);
  196. int _c;
  197. void * _pv;
  198. void * & PData() { return _pv; }
  199. };
  200. //+------------------------------------------------------------------------
  201. //
  202. // Member: CFormsAry::Deref
  203. //
  204. // Synopsis: Returns a pointer to the i'th element of the array. This
  205. // method is normally called by type-safe methods in derived
  206. // classes.
  207. //
  208. // Arguments: i
  209. //
  210. // Returns: void *
  211. //
  212. //-------------------------------------------------------------------------
  213. inline void *
  214. CFormsAry::Deref(size_t cb, int i)
  215. {
  216. MYDBGASSERT(i >= 0);
  217. MYDBGASSERT(i < GetAlloced(cb));
  218. return ((BYTE *) PData()) + i * cb;
  219. }
  220. //+---------------------------------------------------------------------------
  221. //
  222. // Class: CDataAry
  223. //
  224. // Purpose: This template class declares a concrete derived class
  225. // of CFormsAry. See CFormsAry discussion above for
  226. // documentation.
  227. //
  228. //----------------------------------------------------------------------------
  229. template <class ELEM>
  230. class CDataAry : public CFormsAry
  231. {
  232. public:
  233. CDataAry() : CFormsAry() { }
  234. operator ELEM *() { return (ELEM *)PData(); }
  235. CDataAry(const CDataAry &);
  236. CDataAry& operator=(const CDataAry &);
  237. HRESULT EnsureSize(int c)
  238. { return CFormsAry::EnsureSize(sizeof(ELEM), c); }
  239. HRESULT AppendIndirect(void * pv)
  240. { return CFormsAry::AppendIndirect(sizeof(ELEM), pv); }
  241. HRESULT InsertIndirect(int i, void * pv)
  242. { return CFormsAry::InsertIndirect(sizeof(ELEM), i, pv); }
  243. int FindIndirect(void * pv)
  244. { return CFormsAry::FindIndirect(sizeof(ELEM), pv); }
  245. void Delete(int i)
  246. { CFormsAry::Delete(sizeof(ELEM), i); }
  247. BOOL DeleteByValueIndirect(void *pv)
  248. { return CFormsAry::DeleteByValueIndirect(sizeof(ELEM), pv); }
  249. void DeleteMultiple(int start, int end)
  250. { CFormsAry::DeleteMultiple(sizeof(ELEM), start, end); }
  251. void BringToFront(int i)
  252. { CFormsAry::BringToFront(sizeof(ELEM), i); }
  253. void SendToBack(int i)
  254. { CFormsAry::SendToBack(sizeof(ELEM), i); }
  255. HRESULT CopyAppend(const CFormsAry& ary, BOOL fAddRef)
  256. { return CFormsAry::Copy(sizeof(ELEM), ary, fAddRef); }
  257. HRESULT Copy(const CFormsAry& ary, BOOL fAddRef)
  258. { return CFormsAry::Copy(sizeof(ELEM), ary, fAddRef); }
  259. HRESULT CopyAppendIndirect(int c, void * pv, BOOL fAddRef)
  260. { return CFormsAry::CopyAppendIndirect(sizeof(ELEM), c, pv, fAddRef); }
  261. HRESULT CopyIndirect(int c, void * pv, BOOL fAddRef)
  262. { return CFormsAry::CopyIndirect(sizeof(ELEM), c, pv, fAddRef); }
  263. HRESULT EnumElements(
  264. REFIID iid,
  265. void ** ppv,
  266. BOOL fAddRef,
  267. BOOL fCopy = TRUE,
  268. BOOL fDelete = TRUE)
  269. { return CFormsAry::EnumElements(sizeof(ELEM), iid, ppv, fAddRef, fCopy, fDelete); }
  270. HRESULT EnumVARIANT(
  271. VARTYPE vt,
  272. IEnumVARIANT ** ppenum,
  273. BOOL fCopy = TRUE,
  274. BOOL fDelete = TRUE)
  275. { return CFormsAry::EnumVARIANT(sizeof(ELEM), vt, ppenum, fCopy, fDelete); }
  276. };
  277. #define DECLARE_FORMSDATAARY(_Cls, _Ty, _pTy) class _Cls : public CDataAry<_Ty> { };
  278. #endif // #ifndef _HXX_CARY