wxlist.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. //------------------------------------------------------------------------------
  2. // File: WXList.cpp
  3. //
  4. // Desc: DirectShow base classes - implements a non-MFC based generic list
  5. // template class.
  6. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  7. //------------------------------------------------------------------------------
  8. #include <pjmedia-videodev/config.h>
  9. #if defined(PJMEDIA_VIDEO_DEV_HAS_DSHOW) && PJMEDIA_VIDEO_DEV_HAS_DSHOW != 0
  10. /* A generic list of pointers to objects.
  11. Objectives: avoid using MFC libraries in ndm kernel mode and
  12. provide a really useful list type.
  13. The class is thread safe in that separate threads may add and
  14. delete items in the list concurrently although the application
  15. must ensure that constructor and destructor access is suitably
  16. synchronised.
  17. The list name must not conflict with MFC classes as an
  18. application may use both
  19. The nodes form a doubly linked, NULL terminated chain with an anchor
  20. block (the list object per se) holding pointers to the first and last
  21. nodes and a count of the nodes.
  22. There is a node cache to reduce the allocation and freeing overhead.
  23. It optionally (determined at construction time) has an Event which is
  24. set whenever the list becomes non-empty and reset whenever it becomes
  25. empty.
  26. It optionally (determined at construction time) has a Critical Section
  27. which is entered during the important part of each operation. (About
  28. all you can do outside it is some parameter checking).
  29. The node cache is a repository of nodes that are NOT in the list to speed
  30. up storage allocation. Each list has its own cache to reduce locking and
  31. serialising. The list accesses are serialised anyway for a given list - a
  32. common cache would mean that we would have to separately serialise access
  33. of all lists within the cache. Because the cache only stores nodes that are
  34. not in the list, releasing the cache does not release any list nodes. This
  35. means that list nodes can be copied or rechained from one list to another
  36. without danger of creating a dangling reference if the original cache goes
  37. away.
  38. Questionable design decisions:
  39. 1. Retaining the warts for compatibility
  40. 2. Keeping an element count -i.e. counting whenever we do anything
  41. instead of only when we want the count.
  42. 3. Making the chain pointers NULL terminated. If the list object
  43. itself looks just like a node and the list is kept as a ring then
  44. it reduces the number of special cases. All inserts look the same.
  45. */
  46. #include <streams.h>
  47. /* set cursor to the position of each element of list in turn */
  48. #define INTERNALTRAVERSELIST(list, cursor) \
  49. for ( cursor = (list).GetHeadPositionI() \
  50. ; cursor!=NULL \
  51. ; cursor = (list).Next(cursor) \
  52. )
  53. /* set cursor to the position of each element of list in turn
  54. in reverse order
  55. */
  56. #define INTERNALREVERSETRAVERSELIST(list, cursor) \
  57. for ( cursor = (list).GetTailPositionI() \
  58. ; cursor!=NULL \
  59. ; cursor = (list).Prev(cursor) \
  60. )
  61. /* Constructor calls a separate initialisation function that
  62. creates a node cache, optionally creates a lock object
  63. and optionally creates a signaling object.
  64. By default we create a locking object, a DEFAULTCACHE sized
  65. cache but no event object so the list cannot be used in calls
  66. to WaitForSingleObject
  67. */
  68. CBaseList::CBaseList(__in_opt LPCTSTR pName, // Descriptive list name
  69. INT iItems) : // Node cache size
  70. #ifdef DEBUG
  71. CBaseObject(pName),
  72. #endif
  73. m_pFirst(NULL),
  74. m_pLast(NULL),
  75. m_Count(0),
  76. m_Cache(iItems)
  77. {
  78. } // constructor
  79. CBaseList::CBaseList(__in_opt LPCTSTR pName) : // Descriptive list name
  80. #ifdef DEBUG
  81. CBaseObject(pName),
  82. #endif
  83. m_pFirst(NULL),
  84. m_pLast(NULL),
  85. m_Count(0),
  86. m_Cache(DEFAULTCACHE)
  87. {
  88. } // constructor
  89. #ifdef UNICODE
  90. CBaseList::CBaseList(__in_opt LPCSTR pName, // Descriptive list name
  91. INT iItems) : // Node cache size
  92. #ifdef DEBUG
  93. CBaseObject(pName),
  94. #endif
  95. m_pFirst(NULL),
  96. m_pLast(NULL),
  97. m_Count(0),
  98. m_Cache(iItems)
  99. {
  100. } // constructor
  101. CBaseList::CBaseList(__in_opt LPCSTR pName) : // Descriptive list name
  102. #ifdef DEBUG
  103. CBaseObject(pName),
  104. #endif
  105. m_pFirst(NULL),
  106. m_pLast(NULL),
  107. m_Count(0),
  108. m_Cache(DEFAULTCACHE)
  109. {
  110. } // constructor
  111. #endif
  112. /* The destructor enumerates all the node objects in the list and
  113. in the cache deleting each in turn. We do not do any processing
  114. on the objects that the list holds (i.e. points to) so if they
  115. represent interfaces for example the creator of the list should
  116. ensure that each of them is released before deleting us
  117. */
  118. CBaseList::~CBaseList()
  119. {
  120. /* Delete all our list nodes */
  121. RemoveAll();
  122. } // destructor
  123. /* Remove all the nodes from the list but don't do anything
  124. with the objects that each node looks after (this is the
  125. responsibility of the creator).
  126. Aa a last act we reset the signalling event
  127. (if available) to indicate to clients that the list
  128. does not have any entries in it.
  129. */
  130. void CBaseList::RemoveAll()
  131. {
  132. /* Free up all the CNode objects NOTE we don't bother putting the
  133. deleted nodes into the cache as this method is only really called
  134. in serious times of change such as when we are being deleted at
  135. which point the cache will be deleted anway */
  136. CNode *pn = m_pFirst;
  137. while (pn) {
  138. CNode *op = pn;
  139. pn = pn->Next();
  140. delete op;
  141. }
  142. /* Reset the object count and the list pointers */
  143. m_Count = 0;
  144. m_pFirst = m_pLast = NULL;
  145. } // RemoveAll
  146. /* Return a position enumerator for the entire list.
  147. A position enumerator is a pointer to a node object cast to a
  148. transparent type so all we do is return the head/tail node
  149. pointer in the list.
  150. WARNING because the position is a pointer to a node there is
  151. an implicit assumption for users a the list class that after
  152. deleting an object from the list that any other position
  153. enumerators that you have may be invalid (since the node
  154. may be gone).
  155. */
  156. __out_opt POSITION CBaseList::GetHeadPositionI() const
  157. {
  158. return (POSITION) m_pFirst;
  159. } // GetHeadPosition
  160. __out_opt POSITION CBaseList::GetTailPositionI() const
  161. {
  162. return (POSITION) m_pLast;
  163. } // GetTailPosition
  164. /* Get the number of objects in the list,
  165. Get the lock before accessing the count.
  166. Locking may not be entirely necessary but it has the side effect
  167. of making sure that all operations are complete before we get it.
  168. So for example if a list is being added to this list then that
  169. will have completed in full before we continue rather than seeing
  170. an intermediate albeit valid state
  171. */
  172. int CBaseList::GetCountI() const
  173. {
  174. return m_Count;
  175. } // GetCount
  176. /* Return the object at rp, update rp to the next object from
  177. the list or NULL if you have moved over the last object.
  178. You may still call this function once we return NULL but
  179. we will continue to return a NULL position value
  180. */
  181. __out void *CBaseList::GetNextI(__inout POSITION& rp) const
  182. {
  183. /* have we reached the end of the list */
  184. if (rp == NULL) {
  185. return NULL;
  186. }
  187. /* Lock the object before continuing */
  188. void *pObject;
  189. /* Copy the original position then step on */
  190. CNode *pn = (CNode *) rp;
  191. ASSERT(pn != NULL);
  192. rp = (POSITION) pn->Next();
  193. /* Get the object at the original position from the list */
  194. pObject = pn->GetData();
  195. // ASSERT(pObject != NULL); // NULL pointers in the list are allowed.
  196. return pObject;
  197. } //GetNext
  198. /* Return the object at p.
  199. Asking for the object at NULL ASSERTs then returns NULL
  200. The object is NOT locked. The list is not being changed
  201. in any way. If another thread is busy deleting the object
  202. then locking would only result in a change from one bad
  203. behaviour to another.
  204. */
  205. __out_opt void *CBaseList::GetI(__in_opt POSITION p) const
  206. {
  207. if (p == NULL) {
  208. return NULL;
  209. }
  210. CNode * pn = (CNode *) p;
  211. void *pObject = pn->GetData();
  212. // ASSERT(pObject != NULL); // NULL pointers in the list are allowed.
  213. return pObject;
  214. } //Get
  215. __out void *CBaseList::GetValidI(__in POSITION p) const
  216. {
  217. CNode * pn = (CNode *) p;
  218. void *pObject = pn->GetData();
  219. // ASSERT(pObject != NULL); // NULL pointers in the list are allowed.
  220. return pObject;
  221. } //Get
  222. /* Return the first position in the list which holds the given pointer.
  223. Return NULL if it's not found.
  224. */
  225. __out_opt POSITION CBaseList::FindI( __in void * pObj) const
  226. {
  227. POSITION pn;
  228. INTERNALTRAVERSELIST(*this, pn){
  229. if (GetI(pn)==pObj) {
  230. return pn;
  231. }
  232. }
  233. return NULL;
  234. } // Find
  235. /* Remove the first node in the list (deletes the pointer to its object
  236. from the list, does not free the object itself).
  237. Return the pointer to its object or NULL if empty
  238. */
  239. __out_opt void *CBaseList::RemoveHeadI()
  240. {
  241. /* All we do is get the head position and ask for that to be deleted.
  242. We could special case this since some of the code path checking
  243. in Remove() is redundant as we know there is no previous
  244. node for example but it seems to gain little over the
  245. added complexity
  246. */
  247. return RemoveI((POSITION)m_pFirst);
  248. } // RemoveHead
  249. /* Remove the last node in the list (deletes the pointer to its object
  250. from the list, does not free the object itself).
  251. Return the pointer to its object or NULL if empty
  252. */
  253. __out_opt void *CBaseList::RemoveTailI()
  254. {
  255. /* All we do is get the tail position and ask for that to be deleted.
  256. We could special case this since some of the code path checking
  257. in Remove() is redundant as we know there is no previous
  258. node for example but it seems to gain little over the
  259. added complexity
  260. */
  261. return RemoveI((POSITION)m_pLast);
  262. } // RemoveTail
  263. /* Remove the pointer to the object in this position from the list.
  264. Deal with all the chain pointers
  265. Return a pointer to the object removed from the list.
  266. The node object that is freed as a result
  267. of this operation is added to the node cache where
  268. it can be used again.
  269. Remove(NULL) is a harmless no-op - but probably is a wart.
  270. */
  271. __out_opt void *CBaseList::RemoveI(__in_opt POSITION pos)
  272. {
  273. /* Lock the critical section before continuing */
  274. // ASSERT (pos!=NULL); // Removing NULL is to be harmless!
  275. if (pos==NULL) return NULL;
  276. CNode *pCurrent = (CNode *) pos;
  277. ASSERT(pCurrent != NULL);
  278. /* Update the previous node */
  279. CNode *pNode = pCurrent->Prev();
  280. if (pNode == NULL) {
  281. m_pFirst = pCurrent->Next();
  282. } else {
  283. pNode->SetNext(pCurrent->Next());
  284. }
  285. /* Update the following node */
  286. pNode = pCurrent->Next();
  287. if (pNode == NULL) {
  288. m_pLast = pCurrent->Prev();
  289. } else {
  290. pNode->SetPrev(pCurrent->Prev());
  291. }
  292. /* Get the object this node was looking after */
  293. void *pObject = pCurrent->GetData();
  294. // ASSERT(pObject != NULL); // NULL pointers in the list are allowed.
  295. /* Try and add the node object to the cache -
  296. a NULL return code from the cache means we ran out of room.
  297. The cache size is fixed by a constructor argument when the
  298. list is created and defaults to DEFAULTCACHE.
  299. This means that the cache will have room for this many
  300. node objects. So if you have a list of media samples
  301. and you know there will never be more than five active at
  302. any given time of them for example then override the default
  303. constructor
  304. */
  305. m_Cache.AddToCache(pCurrent);
  306. /* If the list is empty then reset the list event */
  307. --m_Count;
  308. ASSERT(m_Count >= 0);
  309. return pObject;
  310. } // Remove
  311. /* Add this object to the tail end of our list
  312. Return the new tail position.
  313. */
  314. __out_opt POSITION CBaseList::AddTailI(__in void *pObject)
  315. {
  316. /* Lock the critical section before continuing */
  317. CNode *pNode;
  318. // ASSERT(pObject); // NULL pointers in the list are allowed.
  319. /* If there is a node objects in the cache then use
  320. that otherwise we will have to create a new one */
  321. pNode = (CNode *) m_Cache.RemoveFromCache();
  322. if (pNode == NULL) {
  323. pNode = new CNode;
  324. }
  325. /* Check we have a valid object */
  326. if (pNode == NULL) {
  327. return NULL;
  328. }
  329. /* Initialise all the CNode object
  330. just in case it came from the cache
  331. */
  332. pNode->SetData(pObject);
  333. pNode->SetNext(NULL);
  334. pNode->SetPrev(m_pLast);
  335. if (m_pLast == NULL) {
  336. m_pFirst = pNode;
  337. } else {
  338. m_pLast->SetNext(pNode);
  339. }
  340. /* Set the new last node pointer and also increment the number
  341. of list entries, the critical section is unlocked when we
  342. exit the function
  343. */
  344. m_pLast = pNode;
  345. ++m_Count;
  346. return (POSITION) pNode;
  347. } // AddTail(object)
  348. /* Add this object to the head end of our list
  349. Return the new head position.
  350. */
  351. __out_opt POSITION CBaseList::AddHeadI(__in void *pObject)
  352. {
  353. CNode *pNode;
  354. // ASSERT(pObject); // NULL pointers in the list are allowed.
  355. /* If there is a node objects in the cache then use
  356. that otherwise we will have to create a new one */
  357. pNode = (CNode *) m_Cache.RemoveFromCache();
  358. if (pNode == NULL) {
  359. pNode = new CNode;
  360. }
  361. /* Check we have a valid object */
  362. if (pNode == NULL) {
  363. return NULL;
  364. }
  365. /* Initialise all the CNode object
  366. just in case it came from the cache
  367. */
  368. pNode->SetData(pObject);
  369. /* chain it in (set four pointers) */
  370. pNode->SetPrev(NULL);
  371. pNode->SetNext(m_pFirst);
  372. if (m_pFirst == NULL) {
  373. m_pLast = pNode;
  374. } else {
  375. m_pFirst->SetPrev(pNode);
  376. }
  377. m_pFirst = pNode;
  378. ++m_Count;
  379. return (POSITION) pNode;
  380. } // AddHead(object)
  381. /* Add all the elements in *pList to the tail of this list.
  382. Return TRUE if it all worked, FALSE if it didn't.
  383. If it fails some elements may have been added.
  384. */
  385. BOOL CBaseList::AddTail(__in CBaseList *pList)
  386. {
  387. /* lock the object before starting then enumerate
  388. each entry in the source list and add them one by one to
  389. our list (while still holding the object lock)
  390. Lock the other list too.
  391. */
  392. POSITION pos = pList->GetHeadPositionI();
  393. while (pos) {
  394. if (NULL == AddTailI(pList->GetNextI(pos))) {
  395. return FALSE;
  396. }
  397. }
  398. return TRUE;
  399. } // AddTail(list)
  400. /* Add all the elements in *pList to the head of this list.
  401. Return TRUE if it all worked, FALSE if it didn't.
  402. If it fails some elements may have been added.
  403. */
  404. BOOL CBaseList::AddHead(__in CBaseList *pList)
  405. {
  406. /* lock the object before starting then enumerate
  407. each entry in the source list and add them one by one to
  408. our list (while still holding the object lock)
  409. Lock the other list too.
  410. To avoid reversing the list, traverse it backwards.
  411. */
  412. POSITION pos;
  413. INTERNALREVERSETRAVERSELIST(*pList, pos) {
  414. if (NULL== AddHeadI(pList->GetValidI(pos))){
  415. return FALSE;
  416. }
  417. }
  418. return TRUE;
  419. } // AddHead(list)
  420. /* Add the object after position p
  421. p is still valid after the operation.
  422. AddAfter(NULL,x) adds x to the start - same as AddHead
  423. Return the position of the new object, NULL if it failed
  424. */
  425. __out_opt POSITION CBaseList::AddAfterI(__in_opt POSITION pos, __in void * pObj)
  426. {
  427. if (pos==NULL)
  428. return AddHeadI(pObj);
  429. /* As someone else might be furkling with the list -
  430. Lock the critical section before continuing
  431. */
  432. CNode *pAfter = (CNode *) pos;
  433. ASSERT(pAfter != NULL);
  434. if (pAfter==m_pLast)
  435. return AddTailI(pObj);
  436. /* set pnode to point to a new node, preferably from the cache */
  437. CNode *pNode = (CNode *) m_Cache.RemoveFromCache();
  438. if (pNode == NULL) {
  439. pNode = new CNode;
  440. }
  441. /* Check we have a valid object */
  442. if (pNode == NULL) {
  443. return NULL;
  444. }
  445. /* Initialise all the CNode object
  446. just in case it came from the cache
  447. */
  448. pNode->SetData(pObj);
  449. /* It is to be added to the middle of the list - there is a before
  450. and after node. Chain it after pAfter, before pBefore.
  451. */
  452. CNode * pBefore = pAfter->Next();
  453. ASSERT(pBefore != NULL);
  454. /* chain it in (set four pointers) */
  455. pNode->SetPrev(pAfter);
  456. pNode->SetNext(pBefore);
  457. pBefore->SetPrev(pNode);
  458. pAfter->SetNext(pNode);
  459. ++m_Count;
  460. return (POSITION) pNode;
  461. } // AddAfter(object)
  462. BOOL CBaseList::AddAfter(__in_opt POSITION p, __in CBaseList *pList)
  463. {
  464. POSITION pos;
  465. INTERNALTRAVERSELIST(*pList, pos) {
  466. /* p follows along the elements being added */
  467. p = AddAfterI(p, pList->GetValidI(pos));
  468. if (p==NULL) return FALSE;
  469. }
  470. return TRUE;
  471. } // AddAfter(list)
  472. /* Mirror images:
  473. Add the element or list after position p.
  474. p is still valid after the operation.
  475. AddBefore(NULL,x) adds x to the end - same as AddTail
  476. */
  477. __out_opt POSITION CBaseList::AddBeforeI(__in_opt POSITION pos, __in void * pObj)
  478. {
  479. if (pos==NULL)
  480. return AddTailI(pObj);
  481. /* set pnode to point to a new node, preferably from the cache */
  482. CNode *pBefore = (CNode *) pos;
  483. ASSERT(pBefore != NULL);
  484. if (pBefore==m_pFirst)
  485. return AddHeadI(pObj);
  486. CNode * pNode = (CNode *) m_Cache.RemoveFromCache();
  487. if (pNode == NULL) {
  488. pNode = new CNode;
  489. }
  490. /* Check we have a valid object */
  491. if (pNode == NULL) {
  492. return NULL;
  493. }
  494. /* Initialise all the CNode object
  495. just in case it came from the cache
  496. */
  497. pNode->SetData(pObj);
  498. /* It is to be added to the middle of the list - there is a before
  499. and after node. Chain it after pAfter, before pBefore.
  500. */
  501. CNode * pAfter = pBefore->Prev();
  502. ASSERT(pAfter != NULL);
  503. /* chain it in (set four pointers) */
  504. pNode->SetPrev(pAfter);
  505. pNode->SetNext(pBefore);
  506. pBefore->SetPrev(pNode);
  507. pAfter->SetNext(pNode);
  508. ++m_Count;
  509. return (POSITION) pNode;
  510. } // Addbefore(object)
  511. BOOL CBaseList::AddBefore(__in_opt POSITION p, __in CBaseList *pList)
  512. {
  513. POSITION pos;
  514. INTERNALREVERSETRAVERSELIST(*pList, pos) {
  515. /* p follows along the elements being added */
  516. p = AddBeforeI(p, pList->GetValidI(pos));
  517. if (p==NULL) return FALSE;
  518. }
  519. return TRUE;
  520. } // AddBefore(list)
  521. /* Split *this after position p in *this
  522. Retain as *this the tail portion of the original *this
  523. Add the head portion to the tail end of *pList
  524. Return TRUE if it all worked, FALSE if it didn't.
  525. e.g.
  526. foo->MoveToTail(foo->GetHeadPosition(), bar);
  527. moves one element from the head of foo to the tail of bar
  528. foo->MoveToTail(NULL, bar);
  529. is a no-op
  530. foo->MoveToTail(foo->GetTailPosition, bar);
  531. concatenates foo onto the end of bar and empties foo.
  532. A better, except excessively long name might be
  533. MoveElementsFromHeadThroughPositionToOtherTail
  534. */
  535. BOOL CBaseList::MoveToTail
  536. (__in_opt POSITION pos, __in CBaseList *pList)
  537. {
  538. /* Algorithm:
  539. Note that the elements (including their order) in the concatenation
  540. of *pList to the head of *this is invariant.
  541. 1. Count elements to be moved
  542. 2. Join *pList onto the head of this to make one long chain
  543. 3. Set first/Last pointers in *this and *pList
  544. 4. Break the chain at the new place
  545. 5. Adjust counts
  546. 6. Set/Reset any events
  547. */
  548. if (pos==NULL) return TRUE; // no-op. Eliminates special cases later.
  549. /* Make cMove the number of nodes to move */
  550. CNode * p = (CNode *)pos;
  551. int cMove = 0; // number of nodes to move
  552. while(p!=NULL) {
  553. p = p->Prev();
  554. ++cMove;
  555. }
  556. /* Join the two chains together */
  557. if (pList->m_pLast!=NULL)
  558. pList->m_pLast->SetNext(m_pFirst);
  559. if (m_pFirst!=NULL)
  560. m_pFirst->SetPrev(pList->m_pLast);
  561. /* set first and last pointers */
  562. p = (CNode *)pos;
  563. if (pList->m_pFirst==NULL)
  564. pList->m_pFirst = m_pFirst;
  565. m_pFirst = p->Next();
  566. if (m_pFirst==NULL)
  567. m_pLast = NULL;
  568. pList->m_pLast = p;
  569. /* Break the chain after p to create the new pieces */
  570. if (m_pFirst!=NULL)
  571. m_pFirst->SetPrev(NULL);
  572. p->SetNext(NULL);
  573. /* Adjust the counts */
  574. m_Count -= cMove;
  575. pList->m_Count += cMove;
  576. return TRUE;
  577. } // MoveToTail
  578. /* Mirror image of MoveToTail:
  579. Split *this before position p in *this.
  580. Retain in *this the head portion of the original *this
  581. Add the tail portion to the start (i.e. head) of *pList
  582. Return TRUE if it all worked, FALSE if it didn't.
  583. e.g.
  584. foo->MoveToHead(foo->GetTailPosition(), bar);
  585. moves one element from the tail of foo to the head of bar
  586. foo->MoveToHead(NULL, bar);
  587. is a no-op
  588. foo->MoveToHead(foo->GetHeadPosition, bar);
  589. concatenates foo onto the start of bar and empties foo.
  590. */
  591. BOOL CBaseList::MoveToHead
  592. (__in_opt POSITION pos, __in CBaseList *pList)
  593. {
  594. /* See the comments on the algorithm in MoveToTail */
  595. if (pos==NULL) return TRUE; // no-op. Eliminates special cases later.
  596. /* Make cMove the number of nodes to move */
  597. CNode * p = (CNode *)pos;
  598. int cMove = 0; // number of nodes to move
  599. while(p!=NULL) {
  600. p = p->Next();
  601. ++cMove;
  602. }
  603. /* Join the two chains together */
  604. if (pList->m_pFirst!=NULL)
  605. pList->m_pFirst->SetPrev(m_pLast);
  606. if (m_pLast!=NULL)
  607. m_pLast->SetNext(pList->m_pFirst);
  608. /* set first and last pointers */
  609. p = (CNode *)pos;
  610. if (pList->m_pLast==NULL)
  611. pList->m_pLast = m_pLast;
  612. m_pLast = p->Prev();
  613. if (m_pLast==NULL)
  614. m_pFirst = NULL;
  615. pList->m_pFirst = p;
  616. /* Break the chain after p to create the new pieces */
  617. if (m_pLast!=NULL)
  618. m_pLast->SetNext(NULL);
  619. p->SetPrev(NULL);
  620. /* Adjust the counts */
  621. m_Count -= cMove;
  622. pList->m_Count += cMove;
  623. return TRUE;
  624. } // MoveToHead
  625. /* Reverse the order of the [pointers to] objects in *this
  626. */
  627. void CBaseList::Reverse()
  628. {
  629. /* algorithm:
  630. The obvious booby trap is that you flip pointers around and lose
  631. addressability to the node that you are going to process next.
  632. The easy way to avoid this is do do one chain at a time.
  633. Run along the forward chain,
  634. For each node, set the reverse pointer to the one ahead of us.
  635. The reverse chain is now a copy of the old forward chain, including
  636. the NULL termination.
  637. Run along the reverse chain (i.e. old forward chain again)
  638. For each node set the forward pointer of the node ahead to point back
  639. to the one we're standing on.
  640. The first node needs special treatment,
  641. it's new forward pointer is NULL.
  642. Finally set the First/Last pointers
  643. */
  644. CNode * p;
  645. // Yes we COULD use a traverse, but it would look funny!
  646. p = m_pFirst;
  647. while (p!=NULL) {
  648. CNode * q;
  649. q = p->Next();
  650. p->SetNext(p->Prev());
  651. p->SetPrev(q);
  652. p = q;
  653. }
  654. p = m_pFirst;
  655. m_pFirst = m_pLast;
  656. m_pLast = p;
  657. #if 0 // old version
  658. if (m_pFirst==NULL) return; // empty list
  659. if (m_pFirst->Next()==NULL) return; // single node list
  660. /* run along forward chain */
  661. for ( p = m_pFirst
  662. ; p!=NULL
  663. ; p = p->Next()
  664. ){
  665. p->SetPrev(p->Next());
  666. }
  667. /* special case first element */
  668. m_pFirst->SetNext(NULL); // fix the old first element
  669. /* run along new reverse chain i.e. old forward chain again */
  670. for ( p = m_pFirst // start at the old first element
  671. ; p->Prev()!=NULL // while there's a node still to be set
  672. ; p = p->Prev() // work in the same direction as before
  673. ){
  674. p->Prev()->SetNext(p);
  675. }
  676. /* fix forward and reverse pointers
  677. - the triple XOR swap would work but all the casts look hideous */
  678. p = m_pFirst;
  679. m_pFirst = m_pLast;
  680. m_pLast = p;
  681. #endif
  682. } // Reverse
  683. #endif /* PJMEDIA_VIDEO_DEV_HAS_DSHOW */