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.

556 lines
19 KiB

  1. //------------------------------------------------------------------------------
  2. // File: WXList.h
  3. //
  4. // Desc: DirectShow base classes - defines a non-MFC generic template list
  5. // class.
  6. //
  7. //@@BEGIN_MSINTERNAL
  8. //
  9. // December 1994
  10. //
  11. //@@END_MSINTERNAL
  12. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  13. //------------------------------------------------------------------------------
  14. /* A generic list of pointers to objects.
  15. No storage management or copying is done on the objects pointed to.
  16. Objectives: avoid using MFC libraries in ndm kernel mode and
  17. provide a really useful list type.
  18. The class is thread safe in that separate threads may add and
  19. delete items in the list concurrently although the application
  20. must ensure that constructor and destructor access is suitably
  21. synchronised. An application can cause deadlock with operations
  22. which use two lists by simultaneously calling
  23. list1->Operation(list2) and list2->Operation(list1). So don't!
  24. The names must not conflict with MFC classes as an application
  25. may use both.
  26. */
  27. #ifndef __WXLIST__
  28. #define __WXLIST__
  29. /* A POSITION represents (in some fashion that's opaque) a cursor
  30. on the list that can be set to identify any element. NULL is
  31. a valid value and several operations regard NULL as the position
  32. "one step off the end of the list". (In an n element list there
  33. are n+1 places to insert and NULL is that "n+1-th" value).
  34. The POSITION of an element in the list is only invalidated if
  35. that element is deleted. Move operations may mean that what
  36. was a valid POSITION in one list is now a valid POSITION in
  37. a different list.
  38. Some operations which at first sight are illegal are allowed as
  39. harmless no-ops. For instance RemoveHead is legal on an empty
  40. list and it returns NULL. This allows an atomic way to test if
  41. there is an element there, and if so, get it. The two operations
  42. AddTail and RemoveHead thus implement a MONITOR (See Hoare's paper).
  43. Single element operations return POSITIONs, non-NULL means it worked.
  44. whole list operations return a BOOL. TRUE means it all worked.
  45. This definition is the same as the POSITION type for MFCs, so we must
  46. avoid defining it twice.
  47. */
  48. #ifndef __AFX_H__
  49. struct __POSITION { int unused; };
  50. typedef __POSITION* POSITION;
  51. #endif
  52. const int DEFAULTCACHE = 10; /* Default node object cache size */
  53. /* A class representing one node in a list.
  54. Each node knows a pointer to it's adjacent nodes and also a pointer
  55. to the object that it looks after.
  56. All of these pointers can be retrieved or set through member functions.
  57. */
  58. class CBaseList
  59. #ifdef DEBUG
  60. : public CBaseObject
  61. #endif
  62. {
  63. /* Making these classes inherit from CBaseObject does nothing
  64. functionally but it allows us to check there are no memory
  65. leaks in debug builds.
  66. */
  67. public:
  68. #ifdef DEBUG
  69. class CNode : public CBaseObject {
  70. #else
  71. class CNode {
  72. #endif
  73. CNode *m_pPrev; /* Previous node in the list */
  74. CNode *m_pNext; /* Next node in the list */
  75. void *m_pObject; /* Pointer to the object */
  76. public:
  77. /* Constructor - initialise the object's pointers */
  78. CNode()
  79. #ifdef DEBUG
  80. : CBaseObject(NAME("List node"))
  81. #endif
  82. {
  83. };
  84. /* Return the previous node before this one */
  85. CNode *Prev() const { return m_pPrev; };
  86. /* Return the next node after this one */
  87. CNode *Next() const { return m_pNext; };
  88. /* Set the previous node before this one */
  89. void SetPrev(CNode *p) { m_pPrev = p; };
  90. /* Set the next node after this one */
  91. void SetNext(CNode *p) { m_pNext = p; };
  92. /* Get the pointer to the object for this node */
  93. void *GetData() const { return m_pObject; };
  94. /* Set the pointer to the object for this node */
  95. void SetData(void *p) { m_pObject = p; };
  96. };
  97. class CNodeCache
  98. {
  99. public:
  100. CNodeCache(INT iCacheSize) : m_iCacheSize(iCacheSize),
  101. m_pHead(NULL),
  102. m_iUsed(0)
  103. {};
  104. ~CNodeCache() {
  105. CNode *pNode = m_pHead;
  106. while (pNode) {
  107. CNode *pCurrent = pNode;
  108. pNode = pNode->Next();
  109. delete pCurrent;
  110. }
  111. };
  112. void AddToCache(CNode *pNode)
  113. {
  114. if (m_iUsed < m_iCacheSize) {
  115. pNode->SetNext(m_pHead);
  116. m_pHead = pNode;
  117. m_iUsed++;
  118. } else {
  119. delete pNode;
  120. }
  121. };
  122. CNode *RemoveFromCache()
  123. {
  124. CNode *pNode = m_pHead;
  125. if (pNode != NULL) {
  126. m_pHead = pNode->Next();
  127. m_iUsed--;
  128. ASSERT(m_iUsed >= 0);
  129. } else {
  130. ASSERT(m_iUsed == 0);
  131. }
  132. return pNode;
  133. };
  134. private:
  135. INT m_iCacheSize;
  136. INT m_iUsed;
  137. CNode *m_pHead;
  138. };
  139. protected:
  140. CNode* m_pFirst; /* Pointer to first node in the list */
  141. CNode* m_pLast; /* Pointer to the last node in the list */
  142. LONG m_Count; /* Number of nodes currently in the list */
  143. private:
  144. CNodeCache m_Cache; /* Cache of unused node pointers */
  145. private:
  146. /* These override the default copy constructor and assignment
  147. operator for all list classes. They are in the private class
  148. declaration section so that anybody trying to pass a list
  149. object by value will generate a compile time error of
  150. "cannot access the private member function". If these were
  151. not here then the compiler will create default constructors
  152. and assignment operators which when executed first take a
  153. copy of all member variables and then during destruction
  154. delete them all. This must not be done for any heap
  155. allocated data.
  156. */
  157. CBaseList(const CBaseList &refList);
  158. CBaseList &operator=(const CBaseList &refList);
  159. public:
  160. CBaseList(TCHAR *pName,
  161. INT iItems);
  162. CBaseList(TCHAR *pName);
  163. #ifdef UNICODE
  164. CBaseList(CHAR *pName,
  165. INT iItems);
  166. CBaseList(CHAR *pName);
  167. #endif
  168. ~CBaseList();
  169. /* Remove all the nodes from *this i.e. make the list empty */
  170. void RemoveAll();
  171. /* Return a cursor which identifies the first element of *this */
  172. POSITION GetHeadPositionI() const;
  173. /* Return a cursor which identifies the last element of *this */
  174. POSITION GetTailPositionI() const;
  175. /* Return the number of objects in *this */
  176. int GetCountI() const;
  177. protected:
  178. /* Return the pointer to the object at rp,
  179. Update rp to the next node in *this
  180. but make it NULL if it was at the end of *this.
  181. This is a wart retained for backwards compatibility.
  182. GetPrev is not implemented.
  183. Use Next, Prev and Get separately.
  184. */
  185. void *GetNextI(POSITION& rp) const;
  186. /* Return a pointer to the object at p
  187. Asking for the object at NULL will return NULL harmlessly.
  188. */
  189. void *GetI(POSITION p) const;
  190. public:
  191. /* return the next / prev position in *this
  192. return NULL when going past the end/start.
  193. Next(NULL) is same as GetHeadPosition()
  194. Prev(NULL) is same as GetTailPosition()
  195. An n element list therefore behaves like a n+1 element
  196. cycle with NULL at the start/end.
  197. !!WARNING!! - This handling of NULL is DIFFERENT from GetNext.
  198. Some reasons are:
  199. 1. For a list of n items there are n+1 positions to insert
  200. These are conveniently encoded as the n POSITIONs and NULL.
  201. 2. If you are keeping a list sorted (fairly common) and you
  202. search forward for an element to insert before and don't
  203. find it you finish up with NULL as the element before which
  204. to insert. You then want that NULL to be a valid POSITION
  205. so that you can insert before it and you want that insertion
  206. point to mean the (n+1)-th one that doesn't have a POSITION.
  207. (symmetrically if you are working backwards through the list).
  208. 3. It simplifies the algebra which the methods generate.
  209. e.g. AddBefore(p,x) is identical to AddAfter(Prev(p),x)
  210. in ALL cases. All the other arguments probably are reflections
  211. of the algebraic point.
  212. */
  213. POSITION Next(POSITION pos) const
  214. {
  215. if (pos == NULL) {
  216. return (POSITION) m_pFirst;
  217. }
  218. CNode *pn = (CNode *) pos;
  219. return (POSITION) pn->Next();
  220. } //Next
  221. // See Next
  222. POSITION Prev(POSITION pos) const
  223. {
  224. if (pos == NULL) {
  225. return (POSITION) m_pLast;
  226. }
  227. CNode *pn = (CNode *) pos;
  228. return (POSITION) pn->Prev();
  229. } //Prev
  230. /* Return the first position in *this which holds the given
  231. pointer. Return NULL if the pointer was not not found.
  232. */
  233. protected:
  234. POSITION FindI( void * pObj) const;
  235. // ??? Should there be (or even should there be only)
  236. // ??? POSITION FindNextAfter(void * pObj, POSITION p)
  237. // ??? And of course FindPrevBefore too.
  238. // ??? List.Find(&Obj) then becomes List.FindNextAfter(&Obj, NULL)
  239. /* Remove the first node in *this (deletes the pointer to its
  240. object from the list, does not free the object itself).
  241. Return the pointer to its object.
  242. If *this was already empty it will harmlessly return NULL.
  243. */
  244. void *RemoveHeadI();
  245. /* Remove the last node in *this (deletes the pointer to its
  246. object from the list, does not free the object itself).
  247. Return the pointer to its object.
  248. If *this was already empty it will harmlessly return NULL.
  249. */
  250. void *RemoveTailI();
  251. /* Remove the node identified by p from the list (deletes the pointer
  252. to its object from the list, does not free the object itself).
  253. Asking to Remove the object at NULL will harmlessly return NULL.
  254. Return the pointer to the object removed.
  255. */
  256. void *RemoveI(POSITION p);
  257. /* Add single object *pObj to become a new last element of the list.
  258. Return the new tail position, NULL if it fails.
  259. If you are adding a COM objects, you might want AddRef it first.
  260. Other existing POSITIONs in *this are still valid
  261. */
  262. POSITION AddTailI(void * pObj);
  263. public:
  264. /* Add all the elements in *pList to the tail of *this.
  265. This duplicates all the nodes in *pList (i.e. duplicates
  266. all its pointers to objects). It does not duplicate the objects.
  267. If you are adding a list of pointers to a COM object into the list
  268. it's a good idea to AddRef them all it when you AddTail it.
  269. Return TRUE if it all worked, FALSE if it didn't.
  270. If it fails some elements may have been added.
  271. Existing POSITIONs in *this are still valid
  272. If you actually want to MOVE the elements, use MoveToTail instead.
  273. */
  274. BOOL AddTail(CBaseList *pList);
  275. /* Mirror images of AddHead: */
  276. /* Add single object to become a new first element of the list.
  277. Return the new head position, NULL if it fails.
  278. Existing POSITIONs in *this are still valid
  279. */
  280. protected:
  281. POSITION AddHeadI(void * pObj);
  282. public:
  283. /* Add all the elements in *pList to the head of *this.
  284. Same warnings apply as for AddTail.
  285. Return TRUE if it all worked, FALSE if it didn't.
  286. If it fails some of the objects may have been added.
  287. If you actually want to MOVE the elements, use MoveToHead instead.
  288. */
  289. BOOL AddHead(CBaseList *pList);
  290. /* Add the object *pObj to *this after position p in *this.
  291. AddAfter(NULL,x) adds x to the start - equivalent to AddHead
  292. Return the position of the object added, NULL if it failed.
  293. Existing POSITIONs in *this are undisturbed, including p.
  294. */
  295. protected:
  296. POSITION AddAfterI(POSITION p, void * pObj);
  297. public:
  298. /* Add the list *pList to *this after position p in *this
  299. AddAfter(NULL,x) adds x to the start - equivalent to AddHead
  300. Return TRUE if it all worked, FALSE if it didn't.
  301. If it fails, some of the objects may be added
  302. Existing POSITIONs in *this are undisturbed, including p.
  303. */
  304. BOOL AddAfter(POSITION p, CBaseList *pList);
  305. /* Mirror images:
  306. Add the object *pObj to this-List after position p in *this.
  307. AddBefore(NULL,x) adds x to the end - equivalent to AddTail
  308. Return the position of the new object, NULL if it fails
  309. Existing POSITIONs in *this are undisturbed, including p.
  310. */
  311. protected:
  312. POSITION AddBeforeI(POSITION p, void * pObj);
  313. public:
  314. /* Add the list *pList to *this before position p in *this
  315. AddAfter(NULL,x) adds x to the start - equivalent to AddHead
  316. Return TRUE if it all worked, FALSE if it didn't.
  317. If it fails, some of the objects may be added
  318. Existing POSITIONs in *this are undisturbed, including p.
  319. */
  320. BOOL AddBefore(POSITION p, CBaseList *pList);
  321. /* Note that AddAfter(p,x) is equivalent to AddBefore(Next(p),x)
  322. even in cases where p is NULL or Next(p) is NULL.
  323. Similarly for mirror images etc.
  324. This may make it easier to argue about programs.
  325. */
  326. /* The following operations do not copy any elements.
  327. They move existing blocks of elements around by switching pointers.
  328. They are fairly efficient for long lists as for short lists.
  329. (Alas, the Count slows things down).
  330. They split the list into two parts.
  331. One part remains as the original list, the other part
  332. is appended to the second list. There are eight possible
  333. variations:
  334. Split the list {after/before} a given element
  335. keep the {head/tail} portion in the original list
  336. append the rest to the {head/tail} of the new list.
  337. Since After is strictly equivalent to Before Next
  338. we are not in serious need of the Before/After variants.
  339. That leaves only four.
  340. If you are processing a list left to right and dumping
  341. the bits that you have processed into another list as
  342. you go, the Tail/Tail variant gives the most natural result.
  343. If you are processing in reverse order, Head/Head is best.
  344. By using NULL positions and empty lists judiciously either
  345. of the other two can be built up in two operations.
  346. The definition of NULL (see Next/Prev etc) means that
  347. degenerate cases include
  348. "move all elements to new list"
  349. "Split a list into two lists"
  350. "Concatenate two lists"
  351. (and quite a few no-ops)
  352. !!WARNING!! The type checking won't buy you much if you get list
  353. positions muddled up - e.g. use a POSITION that's in a different
  354. list and see what a mess you get!
  355. */
  356. /* Split *this after position p in *this
  357. Retain as *this the tail portion of the original *this
  358. Add the head portion to the tail end of *pList
  359. Return TRUE if it all worked, FALSE if it didn't.
  360. e.g.
  361. foo->MoveToTail(foo->GetHeadPosition(), bar);
  362. moves one element from the head of foo to the tail of bar
  363. foo->MoveToTail(NULL, bar);
  364. is a no-op, returns NULL
  365. foo->MoveToTail(foo->GetTailPosition, bar);
  366. concatenates foo onto the end of bar and empties foo.
  367. A better, except excessively long name might be
  368. MoveElementsFromHeadThroughPositionToOtherTail
  369. */
  370. BOOL MoveToTail(POSITION pos, CBaseList *pList);
  371. /* Mirror image:
  372. Split *this before position p in *this.
  373. Retain in *this the head portion of the original *this
  374. Add the tail portion to the start (i.e. head) of *pList
  375. e.g.
  376. foo->MoveToHead(foo->GetTailPosition(), bar);
  377. moves one element from the tail of foo to the head of bar
  378. foo->MoveToHead(NULL, bar);
  379. is a no-op, returns NULL
  380. foo->MoveToHead(foo->GetHeadPosition, bar);
  381. concatenates foo onto the start of bar and empties foo.
  382. */
  383. BOOL MoveToHead(POSITION pos, CBaseList *pList);
  384. /* Reverse the order of the [pointers to] objects in *this
  385. */
  386. void Reverse();
  387. /* set cursor to the position of each element of list in turn */
  388. #define TRAVERSELIST(list, cursor) \
  389. for ( cursor = (list).GetHeadPosition() \
  390. ; cursor!=NULL \
  391. ; cursor = (list).Next(cursor) \
  392. )
  393. /* set cursor to the position of each element of list in turn
  394. in reverse order
  395. */
  396. #define REVERSETRAVERSELIST(list, cursor) \
  397. for ( cursor = (list).GetTailPosition() \
  398. ; cursor!=NULL \
  399. ; cursor = (list).Prev(cursor) \
  400. )
  401. }; // end of class declaration
  402. template<class OBJECT> class CGenericList : public CBaseList
  403. {
  404. public:
  405. CGenericList(TCHAR *pName,
  406. INT iItems,
  407. BOOL bLock = TRUE,
  408. BOOL bAlert = FALSE) :
  409. CBaseList(pName, iItems) {
  410. UNREFERENCED_PARAMETER(bAlert);
  411. UNREFERENCED_PARAMETER(bLock);
  412. };
  413. CGenericList(TCHAR *pName) :
  414. CBaseList(pName) {
  415. };
  416. POSITION GetHeadPosition() const { return (POSITION)m_pFirst; }
  417. POSITION GetTailPosition() const { return (POSITION)m_pLast; }
  418. int GetCount() const { return m_Count; }
  419. OBJECT *GetNext(POSITION& rp) const { return (OBJECT *) GetNextI(rp); }
  420. OBJECT *Get(POSITION p) const { return (OBJECT *) GetI(p); }
  421. OBJECT *GetHead() const { return Get(GetHeadPosition()); }
  422. OBJECT *RemoveHead() { return (OBJECT *) RemoveHeadI(); }
  423. OBJECT *RemoveTail() { return (OBJECT *) RemoveTailI(); }
  424. OBJECT *Remove(POSITION p) { return (OBJECT *) RemoveI(p); }
  425. POSITION AddBefore(POSITION p, OBJECT * pObj) { return AddBeforeI(p, pObj); }
  426. POSITION AddAfter(POSITION p, OBJECT * pObj) { return AddAfterI(p, pObj); }
  427. POSITION AddHead(OBJECT * pObj) { return AddHeadI(pObj); }
  428. POSITION AddTail(OBJECT * pObj) { return AddTailI(pObj); }
  429. BOOL AddTail(CGenericList<OBJECT> *pList)
  430. { return CBaseList::AddTail((CBaseList *) pList); }
  431. BOOL AddHead(CGenericList<OBJECT> *pList)
  432. { return CBaseList::AddHead((CBaseList *) pList); }
  433. BOOL AddAfter(POSITION p, CGenericList<OBJECT> *pList)
  434. { return CBaseList::AddAfter(p, (CBaseList *) pList); };
  435. BOOL AddBefore(POSITION p, CGenericList<OBJECT> *pList)
  436. { return CBaseList::AddBefore(p, (CBaseList *) pList); };
  437. POSITION Find( OBJECT * pObj) const { return FindI(pObj); }
  438. }; // end of class declaration
  439. /* These define the standard list types */
  440. typedef CGenericList<CBaseObject> CBaseObjectList;
  441. typedef CGenericList<IUnknown> CBaseInterfaceList;
  442. #endif /* __WXLIST__ */