vxwLstLib.h
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:10k
源码类别:

VxWorks

开发平台:

C/C++

  1. // VXWList/vxwLstLib.h - simple linked list class
  2. // Copyright 1995-1999 Wind River Systems, Inc.
  3. // modification history
  4. // --------------------
  5. // 01e,28mar99,jdi  doc: removed mention of support for Booch class.
  6. // 01d,23feb99,fle  made it refgen parsable
  7. // 01c,21feb99,jdi  added library section, checked in documentation.
  8. // 01b,03oct95,rhp  moved "Using" instructions to library man page;
  9. //                  documented constructors and member fns.
  10. // 01a,15jun95,srh  written.
  11. // DESCRIPTION
  12. // The VXWList class supports the creation and maintenance of a doubly
  13. // linked list.  The class contains pointers to the first and last
  14. // nodes in the list, and a count of the number of nodes in the list.
  15. // The nodes in the list are derived from the structure NODE, which
  16. // provides two pointers: `NODE::next' and `NODE::previous'.
  17. // Both the forward and backward chains are terminated with a NULL pointer.
  18. //
  19. // The VXWList class simply manipulates the linked-list data structures;
  20. // no kernel functions are invoked.  In particular, linked lists by themselves
  21. // provide no task synchronization or mutual exclusion.  If multiple tasks will
  22. // access a single linked list, that list must be guarded with some
  23. // mutual-exclusion mechanism (such as a mutual-exclusion semaphore).
  24. //
  25. // NON-EMPTY LIST:
  26. // .CS
  27. //    ---------             --------          --------
  28. //    | head--------------->| next----------->| next---------
  29. //    |       |             |      |          |      |      |
  30. //    |       |       ------- prev |<---------- prev |      |
  31. //    |       |       |     |      |          |      |      |
  32. //    | tail------    |     | ...  |    ----->| ...  |      |
  33. //    |       |  |    v                 |                   v
  34. //    |count=2|  |  -----               |                 -----
  35. //    ---------  |   ---                |                  ---
  36. //               |    -                 |                   -
  37. //               |                      |
  38. //               ------------------------
  39. // .CE
  40. //
  41. // EMPTY LIST:
  42. // .CS
  43. //         -----------
  44. //         |  head------------------
  45. //         |         |             |
  46. //         |  tail----------       |
  47. //         |         |     |       v
  48. //         | count=0 |   -----   -----
  49. //         -----------    ---     ---
  50. //                         -    -
  51. // .CE
  52. //
  53. // WARNINGS
  54. // Use only single inheritance!  This class is an interface to the
  55. // VxWorks library lstLib.  More sophisticated alternatives are
  56. // available in the Tools.h++ class libraries.
  57. //
  58. // EXAMPLE
  59. // The following example illustrates how to create a list by deriving
  60. // elements from NODE and putting them on a VXWList.
  61. //
  62. // .CS
  63. // class myListNode : public NODE
  64. //     {
  65. //   public:
  66. //     myListNode ()
  67. //      {
  68. //      }
  69. //   private:
  70. //     };
  71. //
  72. // VXWList      myList;
  73. // myListNode   a, b, c;
  74. //
  75. // NODE       * pEl = &c;
  76. //
  77. // void useList ()
  78. //     {
  79. //     myList.add (&a);
  80. //     myList.insert (pEl, &b);
  81. //     }
  82. // .CE
  83. //
  84. // INCLUDE FILES: vxwLstLib.h
  85. //
  86. // SECTION: 1C
  87. //
  88. #ifndef vxwLstLib_h
  89. #define vxwLstLib_h
  90. #include "vxWorks.h"
  91. #include "lstLib.h"
  92. #include "vxwErr.h"
  93. class VXWList : virtual public LIST
  94.     {
  95.   public:
  96. //_ VXWList Public Constructors
  97. ///////////////////////////////////////////////////////////////////////////////
  98. //
  99. // VXWList::VXWList - initialize a list
  100. //
  101. // This constructor initializes a list as an empty list.
  102. //
  103. // RETURNS: N/A
  104.     VXWList ()
  105. {
  106. lstInit (&list_);
  107. }
  108.     VXWList & operator = (const VXWList &);
  109. ///////////////////////////////////////////////////////////////////////////////
  110. //
  111. // VXWList::VXWList - initialize a list as a copy of another
  112. // 
  113. // This constructor builds a new list as a copy of an existing list.
  114. // 
  115. // RETURNS: N/A
  116.     VXWList (const VXWList &);
  117. ///////////////////////////////////////////////////////////////////////////////
  118. //
  119. // VXWList::~VXWList - free up a list
  120. //
  121. // This destructor frees up memory used for nodes.
  122. //
  123. // RETURNS: N/A
  124.     ~VXWList ()
  125. {
  126. lstFree (&list_);
  127. }
  128. //_ VXWList Public Member Functions
  129. ///////////////////////////////////////////////////////////////////////////////
  130. //
  131. // VXWList::add - add a node to the end of list
  132. //
  133. // This routine adds a specified node to the end of the list.
  134. //
  135. // RETURNS: N/A
  136.     void add (NODE *pNode)
  137. {
  138. lstAdd (&list_, pNode);
  139. }
  140. ///////////////////////////////////////////////////////////////////////////////
  141. //
  142. // VXWList::concat - concatenate two lists
  143. //
  144. // This routine concatenates the specified list to the end of the current list.
  145. // The specified list is left empty.  Either list (or both) can be
  146. // empty at the beginning of the operation.
  147. //
  148. // RETURNS: N/A
  149.     void concat (VXWList &aList)
  150. {
  151. lstConcat (&list_, &aList.list_);
  152. }
  153. ///////////////////////////////////////////////////////////////////////////////
  154. //
  155. // VXWList::count - report the number of nodes in a list
  156. //
  157. // This routine returns the number of nodes in a specified list.
  158. //
  159. // RETURNS:
  160. // The number of nodes in the list.
  161.     int count () const
  162. {
  163. return lstCount ((LIST *) &list_);
  164. }
  165. ///////////////////////////////////////////////////////////////////////////////
  166. //
  167. // VXWList::extract - extract a sublist from list
  168. //
  169. // This routine extracts the sublist that starts with <pStart> and ends
  170. // with <pEnd>.  It returns the extracted list.
  171. //
  172. // RETURNS: The extracted sublist.
  173.     LIST extract (NODE *pStart, NODE *pEnd)
  174. {
  175. LIST rVal;
  176. lstExtract (&list_, pStart, pEnd, &rVal);
  177. return rVal;
  178. }
  179. ///////////////////////////////////////////////////////////////////////////////
  180. //
  181. // VXWList::find - find a node in list
  182. //
  183. // This routine returns the node number of a specified node (the 
  184. // first node is 1).
  185. //
  186. // RETURNS:
  187. // The node number, or
  188. // ERROR if the node is not found.
  189.     int find (NODE *pNode) const
  190. {
  191. return lstFind ((LIST *) &list_, pNode);
  192. }
  193. ///////////////////////////////////////////////////////////////////////////////
  194. //
  195. // VXWList::first - find first node in list
  196. //
  197. // This routine finds the first node in its list.
  198. //
  199. // RETURNS
  200. // A pointer to the first node in the list, or
  201. // NULL if the list is empty.
  202.     NODE * first () const
  203. {
  204. return lstFirst ((LIST *) &list_);
  205. }
  206. ///////////////////////////////////////////////////////////////////////////////
  207. //
  208. // VXWList::get - delete and return the first node from list
  209. //
  210. // This routine gets the first node from its list, deletes the node
  211. // from the list, and returns a pointer to the node gotten.
  212. //
  213. // RETURNS
  214. // A pointer to the node gotten, or
  215. // NULL if the list is empty.
  216.     NODE * get ()
  217. {
  218. return lstGet (&list_);
  219. }
  220. ///////////////////////////////////////////////////////////////////////////////
  221. //
  222. // VXWList::insert - insert a node in list after a specified node
  223. //
  224. // This routine inserts a specified node into the list.
  225. // The new node is placed following the list node <pPrev>.
  226. // If <pPrev> is NULL, the node is inserted at the head of the list.
  227. //
  228. // RETURNS: N/A
  229.     void insert (NODE *pPrev, NODE *pNode)
  230. {
  231. lstInsert (&list_, pPrev, pNode);
  232. }
  233. ///////////////////////////////////////////////////////////////////////////////
  234. //
  235. // VXWList::last - find the last node in list
  236. //
  237. // This routine finds the last node in its list.
  238. //
  239. // RETURNS
  240. // A pointer to the last node in the list, or
  241. // NULL if the list is empty.
  242.     NODE * last () const
  243. {
  244. return lstLast ((LIST *) &list_);
  245. }
  246. ///////////////////////////////////////////////////////////////////////////////
  247. //
  248. // VXWList::next - find the next node in list
  249. //
  250. // This routine locates the node immediately following a specified node.
  251. //
  252. // RETURNS:
  253. // A pointer to the next node in the list, or
  254. // NULL if there is no next node.
  255.     NODE * next (NODE *pNode) const
  256. {
  257. return lstNext (pNode);
  258. }
  259. ///////////////////////////////////////////////////////////////////////////////
  260. //
  261. // VXWList::nStep - find a list node <nStep> steps away from a specified node
  262. //
  263. // This routine locates the node <nStep> steps away in either direction from 
  264. // a specified node.  If <nStep> is positive, it steps toward the tail.  If
  265. // <nStep> is negative, it steps toward the head.  If the number of steps is
  266. // out of range, NULL is returned.
  267. //
  268. // RETURNS:
  269. // A pointer to the node <nStep> steps away, or
  270. // NULL if the node is out of range.
  271.     NODE * nStep (NODE *pNode, int nStep) const
  272. {
  273. return lstNStep (pNode, nStep);
  274. }
  275. ///////////////////////////////////////////////////////////////////////////////
  276. //
  277. // VXWList::nth - find the Nth node in a list
  278. //
  279. // This routine returns a pointer to the node specified by a number <nodeNum>
  280. // where the first node in the list is numbered 1.
  281. // Note that the search is optimized by searching forward from the beginning
  282. // if the node is closer to the head, and searching back from the end
  283. // if it is closer to the tail.
  284. //
  285. // RETURNS:
  286. // A pointer to the Nth node, or
  287. // NULL if there is no Nth node.
  288.     NODE * nth (int nodeNum) const
  289. {
  290. return lstNth ((LIST *) &list_, nodeNum);
  291. }
  292. ///////////////////////////////////////////////////////////////////////////////
  293. //
  294. // VXWList::previous - find the previous node in list
  295. //
  296. // This routine locates the node immediately preceding the node pointed to 
  297. // by <pNode>.
  298. //
  299. // RETURNS:
  300. // A pointer to the previous node in the list, or
  301. // NULL if there is no previous node.
  302.     NODE * previous (NODE *pNode) const
  303. {
  304. return lstPrevious (pNode);
  305. }
  306. ///////////////////////////////////////////////////////////////////////////////
  307. //
  308. // VXWList::remove - delete a specified node from list
  309. //
  310. // This routine deletes a specified node from its list.
  311. //
  312. // RETURNS: N/A
  313.     void remove (NODE *pNode)
  314. {
  315. lstDelete (&list_, pNode);
  316. }
  317.   protected:
  318.     LIST list_;
  319.     };
  320. #endif /* ifndef vxwLstLib_h */