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.

501 lines
18 KiB

  1. #ifndef _HXX_CARY
  2. #define _HXX_CARY
  3. //+---------------------------------------------------------------------------
  4. //
  5. // Microsoft Windows
  6. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  7. //
  8. // File: formsary.hxx
  9. //
  10. // Contents: CADsAry* classes
  11. //
  12. // Classes: CBaseEnum
  13. // CEnumGeneric
  14. // CEnumVARIANT
  15. // CADsAry
  16. //
  17. // Functions: (none)
  18. //
  19. // History: 12-29-93 adams Created
  20. // 1-12-94 adams Renamed from cary.hxx to formsary.hxx
  21. //
  22. //----------------------------------------------------------------------------
  23. #include <noutil.hxx>
  24. class CADsAry;
  25. //+------------------------------------------------------------------------
  26. //
  27. // Macro that calculates the number of elements in a statically-defined
  28. // array.
  29. //
  30. //-------------------------------------------------------------------------
  31. #define ARRAY_SIZE(_a) (sizeof(_a) / sizeof(_a[0]))
  32. //+---------------------------------------------------------------------------
  33. //
  34. // Class: CBaseEnum (benum)
  35. //
  36. // Purpose: Base OLE enumerator class for a CADsAry.
  37. //
  38. // Interface: DECLARE_ADs_STANRARD_IUNKNOWN
  39. //
  40. // Next -- Per IEnum*
  41. // Skip -- ""
  42. // Reset -- ""
  43. // Clone -- ""
  44. // CBaseEnum -- ctor.
  45. // CBaseEnum -- ctor.
  46. // ~CBaseEnum -- dtor.
  47. // Init -- 2nd stage initialization.
  48. // Deref -- gets pointer to element.
  49. //
  50. // History: 5-15-94 adams Created
  51. //
  52. // Notes: Since there is no IEnum interface, we create a vtable
  53. // with the same layout as all IEnum interfaces. Be careful
  54. // where you put virtual function declarations!
  55. //
  56. //----------------------------------------------------------------------------
  57. class CBaseEnum : public IUnknown
  58. {
  59. public:
  60. DECLARE_ADs_STANDARD_IUNKNOWN(CBaseEnum);
  61. // IEnum methods
  62. STDMETHOD(Next) (ULONG celt, void * reelt, ULONG * pceltFetched) PURE;
  63. STDMETHOD(Skip) (ULONG celt);
  64. STDMETHOD(Reset) (void);
  65. STDMETHOD(Clone) (CBaseEnum ** ppenum) PURE;
  66. //
  67. // Ensure that vtable contains virtual destructor after other virtual methods.
  68. //
  69. virtual ~CBaseEnum(void);
  70. protected:
  71. CBaseEnum(size_t cb, REFIID iid, BOOL fAddRef, BOOL fDelete);
  72. CBaseEnum(const CBaseEnum & benum);
  73. CBaseEnum& operator=(const CBaseEnum & benum); // don't define
  74. HRESULT Init(CADsAry * pary, BOOL fCopy);
  75. void * Deref(int i);
  76. CADsAry * _pary;
  77. const IID * _piid;
  78. int _i;
  79. size_t _cb;
  80. BOOL _fAddRef;
  81. BOOL _fDelete;
  82. };
  83. //+---------------------------------------------------------------------------
  84. //
  85. // Class: CEnumGeneric (enumg)
  86. //
  87. // Purpose: OLE enumerator for class CADsAry.
  88. //
  89. // Interface: Next -- Per IEnum
  90. // Clone -- ""
  91. // Create -- Creates a new enumerator.
  92. // CEnumGeneric -- ctor.
  93. // CEnumGeneric -- ctor.
  94. //
  95. // History: 5-15-94 adams Created
  96. //
  97. //----------------------------------------------------------------------------
  98. class CEnumGeneric : public CBaseEnum
  99. {
  100. public:
  101. // IEnum methods
  102. STDMETHOD(Next) (ULONG celt, void * reelt, ULONG * pceltFetched);
  103. STDMETHOD(Clone) (CBaseEnum ** ppenum);
  104. // CEnumGeneric methods
  105. static HRESULT Create(
  106. size_t cb,
  107. CADsAry * pary,
  108. REFIID iid,
  109. BOOL fAddRef,
  110. BOOL fCopy,
  111. BOOL fDelete,
  112. CEnumGeneric ** ppenum);
  113. protected:
  114. CEnumGeneric(size_t cb, REFIID iid, BOOL fAddRef, BOOL fDelete);
  115. CEnumGeneric(const CEnumGeneric & enumg);
  116. CEnumGeneric& operator=(const CEnumGeneric & enumg); // don't define
  117. };
  118. //+---------------------------------------------------------------------------
  119. //
  120. // Class: CEnumVARIANT (enumv)
  121. //
  122. // Purpose: OLE enumerator for class CADsAry.
  123. //
  124. // Interface: Next -- Per IEnum
  125. // Clone -- ""
  126. // Create -- Creates a new enumerator.
  127. // CEnumGeneric -- ctor.
  128. // CEnumGeneric -- ctor.
  129. //
  130. // History: 5-15-94 adams Created
  131. //
  132. //----------------------------------------------------------------------------
  133. class CEnumVARIANT : public CBaseEnum
  134. {
  135. public:
  136. // IEnum methods
  137. STDMETHOD(Next) (ULONG celt, void * reelt, ULONG * pceltFetched);
  138. STDMETHOD(Clone) (CBaseEnum ** ppenum);
  139. static HRESULT Create(
  140. size_t cb,
  141. CADsAry * pary,
  142. VARTYPE vt,
  143. BOOL fCopy,
  144. BOOL fDelete,
  145. IEnumVARIANT ** ppenum);
  146. protected:
  147. CEnumVARIANT(size_t cb, VARTYPE vt, BOOL fDelete);
  148. CEnumVARIANT(const CEnumVARIANT & enumv);
  149. // don't define
  150. CEnumVARIANT& operator =(const CEnumVARIANT & enumv);
  151. VARTYPE _vt; // type of element enumerated
  152. };
  153. //+------------------------------------------------------------------------
  154. //
  155. // Class: CADsAry (ary)
  156. //
  157. // Purpose: Generic resizeable array class. Note that most of
  158. // the functionality in this class is provided in
  159. // protected members. The DECLARE_ADsPTRARY and
  160. // DECLARE_ADsDATAARY macros define concrete implementations
  161. // of the array class. In these concrete classes, public
  162. // methods are provided which delegate to the protected
  163. // methods. This allows us to avoid storing the element
  164. // size with the array.
  165. //
  166. // Interface:
  167. // CADsAry, ~CADsAry
  168. //
  169. // EnsureSize Ensures that the array is at least a certain
  170. // size, allocating more memory if necessary.
  171. // Note that the array size is measured in elements,
  172. // rather than in bytes.
  173. // Size Returns the current size of the array.
  174. // SetSize Sets the array size; EnsureSize must be called
  175. // first to reserve space if the array is growing
  176. //
  177. // operator LPVOID Allow the CADsAry class to be cast
  178. // to a (void *)
  179. //
  180. // Append Adds a new pointer to the end of the array,
  181. // growing the array if necessary. Only works
  182. // for arrays of pointers.
  183. // AppendIndirect As Append, for non-pointer arrays
  184. //
  185. // Insert Inserts a new pointer at the given index in
  186. // the array, growing the array if necessary. Any
  187. // elements at or following the index are moved
  188. // out of the way.
  189. // InsertIndirect As Insert, for non-pointer arrays
  190. //
  191. // Delete Deletes an element of the array, moving any
  192. // elements that follow it to fill
  193. // DeleteMultiple Deletes a range of elements from the array,
  194. // moving to fill
  195. // BringToFront Moves an element of the array to index 0,
  196. // shuffling elements to make room
  197. // SendToBack Moves an element to the end of the array,
  198. // shuffling elements to make room
  199. //
  200. // Find Returns the index at which a given pointer
  201. // is found
  202. // FindIndirect As Find, for non-pointer arrays
  203. //
  204. // Copy Creates a copy of the object.
  205. //
  206. // EnumElements Create an enumerator which supports the given
  207. // interface ID for the contents of the array
  208. //
  209. // EnumElements Create an IEnumVARIANT enumerator.
  210. //
  211. //
  212. // Deref Returns a pointer to an element of the array;
  213. // normally used by type-safe methods in derived
  214. // classes
  215. //
  216. // GetAlloced Get number of elements allocated
  217. //
  218. // Members: _c Current size of the array
  219. // _pv Buffer storing the elements
  220. //
  221. // Note: The CADsAry class only supports arrays of elements
  222. // whose size is less than CADsAry_MAXELEMSIZE (currently 128).
  223. //
  224. //-------------------------------------------------------------------------
  225. class CADsAry
  226. {
  227. friend CBaseEnum;
  228. friend CEnumGeneric;
  229. friend CEnumVARIANT;
  230. public:
  231. CADsAry(void)
  232. { _c = 0; _pv = NULL; }
  233. ~CADsAry();
  234. int Size(void)
  235. { return _c; }
  236. void SetSize(int c)
  237. { _c = c;}
  238. operator LPVOID(void)
  239. { return _pv; }
  240. void DeleteAll(void);
  241. protected:
  242. // Methods which are wrapped by inline subclass methods
  243. HRESULT EnsureSize(size_t cb, int c);
  244. HRESULT AppendIndirect(size_t cb, void * pv);
  245. HRESULT InsertIndirect(size_t cb, int i, void * pv);
  246. int FindIndirect(size_t cb, void **);
  247. void Delete(size_t cb, int i);
  248. void BringToFront(size_t cb, int i);
  249. void SendToBack(size_t cb, int i);
  250. HRESULT Copy(size_t cb, const CADsAry& ary, BOOL fAddRef);
  251. int GetAlloced(size_t cb)
  252. { return _pv ? LocalSize(_pv) / cb : 0; }
  253. HRESULT EnumElements(
  254. size_t cb,
  255. REFIID iid,
  256. void ** ppv,
  257. BOOL fAddRef,
  258. BOOL fCopy = TRUE,
  259. BOOL fDelete = TRUE);
  260. HRESULT EnumVARIANT(
  261. size_t cb,
  262. VARTYPE vt,
  263. IEnumVARIANT ** ppenum,
  264. BOOL fCopy = TRUE,
  265. BOOL fDelete = TRUE);
  266. // Internal helpers
  267. void * Deref(size_t cb, int i);
  268. int _c;
  269. void * _pv;
  270. NO_COPY(CADsAry);
  271. };
  272. #define CADsAry_MAXELEMSIZE 128
  273. //+---------------------------------------------------------------------------
  274. //
  275. // Member: CBaseEnum::Deref
  276. //
  277. // Synopsis: Forwards deref to _pary. Required because classes derived
  278. // from CBaseEnum are friends of CADsAry.
  279. //
  280. // History: 5-15-94 adams Created
  281. //
  282. //----------------------------------------------------------------------------
  283. inline void *
  284. CBaseEnum::Deref(int i)
  285. {
  286. // KrishnaG: commented this out while porting, MUSTRESOLVE
  287. //Assert(i >= 0);
  288. return ((BYTE *) _pary->_pv) + i * _cb;
  289. }
  290. //+------------------------------------------------------------------------
  291. //
  292. // Class: CADsPtrAry (ary)
  293. //
  294. // Purpose: Subclass used for arrays of pointers. In this case, the
  295. // element size is known. Normally, the DECLARE_ADsPTRARY
  296. // macro is used to define a specific concrete implementation
  297. // of this class, to hold a specific type of pointer.
  298. //
  299. //-------------------------------------------------------------------------
  300. class CADsPtrAry : public CADsAry
  301. {
  302. public:
  303. CADsPtrAry(void) : CADsAry( ) { ; }
  304. HRESULT EnsureSize(int c);
  305. HRESULT Append(void * pv);
  306. HRESULT Insert(int i, void * pv);
  307. int Find(void * pv);
  308. void Delete(int i);
  309. void BringToFront(int i);
  310. void SendToBack(int i);
  311. HRESULT Copy(const CADsAry& ary, BOOL fAddRef);
  312. HRESULT EnumElements(
  313. REFIID iid,
  314. void ** ppv,
  315. BOOL fAddRef,
  316. BOOL fCopy = TRUE,
  317. BOOL fDelete = TRUE);
  318. HRESULT EnumVARIANT(
  319. VARTYPE vt,
  320. IEnumVARIANT ** ppenum,
  321. BOOL fCopy = TRUE,
  322. BOOL fDelete = TRUE);
  323. };
  324. //+------------------------------------------------------------------------
  325. //
  326. // Macro: DECLARE_ADsDATAARY
  327. //
  328. // Purpose: Declares a type-safe class derived from CADsAry.
  329. //
  330. // Arguments: _Cls -- Name of new array class
  331. // _Ty -- Type of array element (e.g. FOO)
  332. // _pTy -- Type which is a pointer to _Ty (e.g. LPFOO)
  333. //
  334. // Interface: operator [] Provides a type-safe pointer to an element
  335. // of the array
  336. // operator LPFOO Allows the array to be cast to a type-safe
  337. // pointer to its first element
  338. //
  339. // In addition, all the protected members introduced by
  340. // the CADsAry class are wrapped with public inlines
  341. // which substitute the known element size for this
  342. // array type.
  343. //
  344. //-------------------------------------------------------------------------
  345. #define DECLARE_ADsDATAARY(_Cls, _Ty, _pTy) \
  346. class _Cls : public CADsAry \
  347. { \
  348. public: \
  349. _Cls(void) : CADsAry() { ; } \
  350. _Ty& operator[] (int i) { return * (_pTy) Deref(sizeof(_Ty), i); } \
  351. operator _pTy(void) { return (_pTy) _pv; } \
  352. _Cls(const _Cls &); \
  353. _Cls& operator=(const _Cls &); \
  354. \
  355. HRESULT EnsureSize(int c) \
  356. { return CADsAry::EnsureSize(sizeof(_Ty), c); } \
  357. HRESULT AppendIndirect(void * pv) \
  358. { return CADsAry::AppendIndirect(sizeof(_Ty), pv); } \
  359. HRESULT InsertIndirect(int i, void * pv) \
  360. { return CADsAry::InsertIndirect(sizeof(_Ty), i, pv); } \
  361. int FindIndirect(void ** ppv) \
  362. { return CADsAry::FindIndirect(sizeof(_Ty), ppv); } \
  363. \
  364. void Delete(int i) \
  365. { CADsAry::Delete(sizeof(_Ty), i); } \
  366. void BringToFront(int i) \
  367. { CADsAry::BringToFront(sizeof(_Ty), i); } \
  368. void SendToBack(int i) \
  369. { CADsAry::SendToBack(sizeof(_Ty), i); } \
  370. \
  371. HRESULT Copy(const CADsAry& ary, BOOL fAddRef) \
  372. { return CADsAry::Copy(sizeof(_Ty), ary, fAddRef); } \
  373. \
  374. HRESULT EnumElements( \
  375. REFIID iid, \
  376. void ** ppv, \
  377. BOOL fAddRef, \
  378. BOOL fCopy = TRUE, \
  379. BOOL fDelete = TRUE) \
  380. { return CADsAry::EnumElements(sizeof(_Ty), iid, ppv, fAddRef, fCopy, fDelete); } \
  381. \
  382. HRESULT EnumVARIANT( \
  383. VARTYPE vt, \
  384. IEnumVARIANT ** ppenum, \
  385. BOOL fCopy = TRUE, \
  386. BOOL fDelete = TRUE) \
  387. { return CADsAry::EnumVARIANT(sizeof(_Ty), vt, ppenum, fCopy, fDelete); } \
  388. };
  389. //+------------------------------------------------------------------------
  390. //
  391. // Macro: DECLARE_ADsDATAARY
  392. //
  393. // Purpose: Declares a type-safe class derived from CADsPtrAry.
  394. //
  395. // Arguments: _Cls -- Name of new array class
  396. // _Ty -- Type of array element (e.g. FOO)
  397. // _pTy -- Type which is a pointer to _Ty (e.g. LPFOO)
  398. //
  399. // Interface: operator [] Provides a type-safe pointer to an element
  400. // of the array
  401. // operator LPFOO Allows the array to be cast to a type-safe
  402. // pointer to its first element
  403. //
  404. //-------------------------------------------------------------------------
  405. #define DECLARE_ADsPTRARY(_Cls, _Ty, _pTy) \
  406. class _Cls : public CADsPtrAry \
  407. { \
  408. public: \
  409. _Cls(void) : CADsPtrAry() { ; } \
  410. _Ty& operator[] (int i) { return * (_pTy) Deref(sizeof(void *), i); } \
  411. operator _pTy(void) { return (_pTy) _pv; } \
  412. _Cls(const _Cls &); \
  413. _Cls& operator=(const _Cls &); \
  414. };
  415. //+------------------------------------------------------------------------
  416. //
  417. // Common CAry classes
  418. //
  419. //-------------------------------------------------------------------------
  420. typedef LPDISPATCH * LPLPDISPATCH;
  421. DECLARE_ADsPTRARY(CAryDisp, LPDISPATCH, LPLPDISPATCH);
  422. #endif // #ifndef _HXX_CARY