list.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: list.h
  3. // Purpose: interface of wxList<T>
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. The wxList<T> class provides linked list functionality.
  9. This class has been rewritten to be type safe and to provide the full API of
  10. the STL std::list container and should be used like it.
  11. The exception is that wxList<T> actually stores pointers and therefore its
  12. iterators return pointers and not references to the actual objects in the list
  13. (see example below) and @e value_type is defined as @e T*.
  14. wxList<T> destroys an object after removing it only if wxList<T>::DeleteContents
  15. has been called.
  16. wxList<T> is not a real template and it requires that you declare and define
  17. each wxList<T> class in your program. This is done with @e WX_DECLARE_LIST
  18. and @e WX_DEFINE_LIST macros (see example). We hope that we'll be able to
  19. provide a proper template class providing both the STL @c std::list and the old
  20. wxList API in the future.
  21. Please refer to the STL @c std::list documentation (see http://www.cppreference.com/wiki/stl/list/start)
  22. for further information on how to use the class.
  23. Below we documented both the supported STL and the legacy API
  24. that originated from the old wxList class and which can still be used alternatively
  25. for the same class.
  26. Note that if you compile wxWidgets in STL mode (@c wxUSE_STL defined as 1)
  27. then wxList<T> will actually derive from @c std::list and just add a legacy
  28. compatibility layer for the old wxList class.
  29. @code
  30. // this part might be in a header or source (.cpp) file
  31. class MyListElement
  32. {
  33. ... // whatever
  34. };
  35. // this macro declares and partly implements MyList class
  36. WX_DECLARE_LIST(MyListElement, MyList);
  37. ...
  38. // the only requirement for the rest is to be AFTER the full declaration of
  39. // MyListElement (for WX_DECLARE_LIST forward declaration is enough), but
  40. // usually it will be found in the source file and not in the header
  41. #include <wx/listimpl.cpp>
  42. WX_DEFINE_LIST(MyList);
  43. MyList list;
  44. MyListElement element;
  45. list.Append(&element); // ok
  46. list.Append(17); // error: incorrect type
  47. // let's iterate over the list in STL syntax
  48. MyList::iterator iter;
  49. for (iter = list.begin(); iter != list.end(); ++iter)
  50. {
  51. MyListElement *current = *iter;
  52. ...process the current element...
  53. }
  54. // the same with the legacy API from the old wxList class
  55. MyList::compatibility_iterator node = list.GetFirst();
  56. while (node)
  57. {
  58. MyListElement *current = node->GetData();
  59. ...process the current element...
  60. node = node->GetNext();
  61. }
  62. @endcode
  63. For compatibility with previous versions wxList and wxStringList classes are
  64. still defined, but their usage is deprecated and they will disappear in the
  65. future versions completely.
  66. The use of the latter is especially discouraged as it is not only unsafe but
  67. is also much less efficient than wxArrayString class.
  68. @tparam T
  69. The type stored in the wxList nodes.
  70. @library{wxbase}
  71. @category{containers}
  72. @see wxArray<T>, wxVector<T>, wxNode<T>
  73. */
  74. template<typename T>
  75. class wxList<T>
  76. {
  77. public:
  78. /**
  79. Default constructor.
  80. */
  81. wxList<T>();
  82. /**
  83. Constructor which initialized the list with an array of @a count elements.
  84. */
  85. wxList<T>(size_t count, T* elements[]);
  86. /**
  87. Destroys the list, but does not delete the objects stored in the list
  88. unless you called DeleteContents(@true ).
  89. */
  90. ~wxList<T>();
  91. /**
  92. Appends the pointer to @a object to the list.
  93. */
  94. wxList<T>::compatibility_iterator Append(T* object);
  95. /**
  96. Clears the list.
  97. Deletes the actual objects if DeleteContents( @true ) was called previously.
  98. */
  99. void Clear();
  100. /**
  101. If @a destroy is @true, instructs the list to call @e delete
  102. on objects stored in the list whenever they are removed.
  103. The default is @false.
  104. */
  105. void DeleteContents(bool destroy);
  106. /**
  107. Deletes the given element referred to by @a iter from the list
  108. if @a iter is a valid iterator. Returns @true if successful.
  109. Deletes the actual object if DeleteContents( @true ) was called previously.
  110. */
  111. bool DeleteNode(const compatibility_iterator& iter);
  112. /**
  113. Finds the given @a object and removes it from the list, returning
  114. @true if successful.
  115. Deletes @a object if DeleteContents( @true ) was called previously.
  116. */
  117. bool DeleteObject(T* object);
  118. /**
  119. Removes element referred to be @a iter.
  120. Deletes the actual object if DeleteContents( @true ) was called previously.
  121. */
  122. void Erase(const compatibility_iterator& iter);
  123. /**
  124. Returns the iterator referring to @a object or @NULL if none found.
  125. */
  126. wxList<T>::compatibility_iterator Find(T* object) const;
  127. /**
  128. Returns the number of elements in the list.
  129. */
  130. size_t GetCount() const;
  131. /**
  132. Returns the first iterator in the list (@NULL if the list is empty).
  133. */
  134. wxList<T>::compatibility_iterator GetFirst() const;
  135. /**
  136. Returns the last iterator in the list (@NULL if the list is empty).
  137. */
  138. wxList<T>::compatibility_iterator GetLast() const;
  139. /**
  140. Returns the index of @a obj within the list or @c wxNOT_FOUND if
  141. @a obj is not found in the list.
  142. */
  143. int IndexOf(T* obj) const;
  144. /**
  145. Inserts @a object at the beginning of the list.
  146. */
  147. wxList<T>::compatibility_iterator Insert(T* object);
  148. /**
  149. Inserts @a object at @a position.
  150. */
  151. wxList<T>::compatibility_iterator Insert(size_t position,
  152. T* object);
  153. /**
  154. Inserts @a object before the object referred to be @a iter.
  155. */
  156. wxList<T>::compatibility_iterator Insert(compatibility_iterator iter,
  157. T* object);
  158. /**
  159. Returns @true if the list is empty, @false otherwise.
  160. */
  161. bool IsEmpty() const;
  162. /**
  163. Returns the iterator referring to the object at the given
  164. @a index in the list.
  165. */
  166. wxList<T>::compatibility_iterator Item(size_t index) const;
  167. /**
  168. Check if the object is present in the list.
  169. @see Find()
  170. */
  171. bool Member(T* object) const;
  172. /**
  173. @deprecated This function is deprecated, use Item() instead.
  174. */
  175. wxList<T>::compatibility_iterator Nth(int n) const;
  176. /**
  177. @deprecated This function is deprecated, use wxList::GetCount instead.
  178. Returns the number of elements in the list.
  179. */
  180. int Number() const;
  181. /**
  182. Allows the sorting of arbitrary lists by giving a function to compare
  183. two list elements. We use the system @b qsort function for the actual
  184. sorting process.
  185. */
  186. void Sort(wxSortCompareFunction compfunc);
  187. /**
  188. Clears the list and item from @a first to @a last from another list to it.
  189. */
  190. void assign(const_iterator first, const const_iterator& last);
  191. /**
  192. Clears the list and adds @a n items with value @a v to it.
  193. */
  194. void assign(size_type n, const_reference v = value_type());
  195. /**
  196. Returns the last item of the list.
  197. */
  198. reference back();
  199. /**
  200. Returns the last item of the list as a const reference.
  201. */
  202. const_reference back() const;
  203. /**
  204. Returns an iterator pointing to the beginning of the list.
  205. */
  206. iterator begin();
  207. /**
  208. Returns a const iterator pointing to the beginning of the list.
  209. */
  210. const_iterator begin() const;
  211. /**
  212. Removes all items from the list.
  213. */
  214. void clear();
  215. /**
  216. Returns @e @true if the list is empty.
  217. */
  218. bool empty() const;
  219. /**
  220. Returns a const iterator pointing at the end of the list.
  221. */
  222. const_iterator end() const;
  223. /**
  224. Returns a iterator pointing at the end of the list.
  225. */
  226. iterator end() const;
  227. /**
  228. Erases the given item
  229. */
  230. iterator erase(const iterator& it);
  231. /**
  232. Erases the items from @e first to @e last.
  233. */
  234. iterator erase(const iterator& first,
  235. const iterator& last);
  236. /**
  237. Returns the first item in the list.
  238. */
  239. reference front() const;
  240. /**
  241. Returns the first item in the list as a const reference.
  242. */
  243. const_reference front() const;
  244. /**
  245. Inserts an item at the head of the list
  246. */
  247. iterator insert(const iterator& it);
  248. /**
  249. Inserts an item at the given position
  250. */
  251. void insert(const iterator& it, size_type n);
  252. /**
  253. Inserts several items at the given position.
  254. */
  255. void insert(const iterator& it, const_iterator first,
  256. const const_iterator& last);
  257. /**
  258. Returns the largest possible size of the list.
  259. */
  260. size_type max_size() const;
  261. /**
  262. Removes the last item from the list.
  263. */
  264. void pop_back();
  265. /**
  266. Removes the first item from the list.
  267. */
  268. void pop_front();
  269. /**
  270. Adds an item to end of the list.
  271. */
  272. void push_back(const_reference v = value_type());
  273. /**
  274. Adds an item to the front of the list.
  275. */
  276. void push_front(const_reference v = value_type());
  277. /**
  278. Returns a reverse iterator pointing to the beginning of the
  279. reversed list.
  280. */
  281. reverse_iterator rbegin();
  282. /**
  283. Returns a const reverse iterator pointing to the beginning of the
  284. reversed list.
  285. */
  286. const_reverse_iterator rbegin() const;
  287. /**
  288. Removes an item from the list.
  289. */
  290. void remove(const_reference v);
  291. /**
  292. Returns a reverse iterator pointing to the end of the reversed list.
  293. */
  294. reverse_iterator rend();
  295. /**
  296. Returns a const reverse iterator pointing to the end of the reversed list.
  297. */
  298. const_reverse_iterator rend() const;
  299. /**
  300. Resizes the list.
  301. If the list is longer than @a n, then items are removed until the list
  302. becomes long @a n.
  303. If the list is shorter than @a n items with the value @a v are appended
  304. to the list until the list becomes long @a n.
  305. */
  306. void resize(size_type n, value_type v = value_type());
  307. /**
  308. Reverses the list.
  309. */
  310. void reverse();
  311. /**
  312. Returns the size of the list.
  313. */
  314. size_type size() const;
  315. /**
  316. Returns a wxVector holding the list elements.
  317. @since 2.9.5
  318. */
  319. wxVector<T> AsVector() const;
  320. };
  321. /**
  322. wxNode<T> is the node structure used in linked lists (see wxList) and derived
  323. classes. You should never use wxNode<T> class directly, however, because it
  324. works with untyped (@c void *) data and this is unsafe.
  325. Use wxNode<T>-derived classes which are automatically defined by WX_DECLARE_LIST
  326. and WX_DEFINE_LIST macros instead as described in wxList documentation
  327. (see example there).
  328. Also note that although there is a class called wxNode, it is defined for backwards
  329. compatibility only and usage of this class is strongly deprecated.
  330. In the documentation below, the type @c T should be thought of as a
  331. "template" parameter: this is the type of data stored in the linked list or,
  332. in other words, the first argument of WX_DECLARE_LIST macro. Also, wxNode is
  333. written as wxNodeT even though it isn't really a template class -- but it
  334. helps to think of it as if it were.
  335. @tparam T
  336. The type stored in the wxNode.
  337. @library{wxbase}
  338. @category{data}
  339. @see wxList<T>, wxHashTable
  340. */
  341. template<typename T>
  342. class wxNode<T>
  343. {
  344. public:
  345. /**
  346. Retrieves the client data pointer associated with the node.
  347. */
  348. T* GetData() const;
  349. /**
  350. Retrieves the next node or @NULL if this node is the last one.
  351. */
  352. wxNode<T>* GetNext() const;
  353. /**
  354. Retrieves the previous node or @NULL if this node is the first one in the list.
  355. */
  356. wxNode<T>* GetPrevious();
  357. /**
  358. Returns the zero-based index of this node within the list. The return value
  359. will be @c wxNOT_FOUND if the node has not been added to a list yet.
  360. */
  361. int IndexOf();
  362. /**
  363. Sets the data associated with the node (usually the pointer will have been
  364. set when the node was created).
  365. */
  366. void SetData(T* data);
  367. };