/src/qt3support/tools/q3valuelist.qdoc

https://bitbucket.org/ultra_iter/qt-vtl · Unknown · 555 lines · 414 code · 141 blank · 0 comment · 0 complexity · ef57d038ebf0cf6579ffb95dadff9f68 MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the documentation of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:FDL$
  10. ** GNU Free Documentation License
  11. ** Alternatively, this file may be used under the terms of the GNU Free
  12. ** Documentation License version 1.3 as published by the Free Software
  13. ** Foundation and appearing in the file included in the packaging of
  14. ** this file.
  15. **
  16. ** Other Usage
  17. ** Alternatively, this file may be used in accordance with the terms
  18. ** and conditions contained in a signed written agreement between you
  19. ** and Nokia.
  20. **
  21. **
  22. **
  23. **
  24. ** $QT_END_LICENSE$
  25. **
  26. ****************************************************************************/
  27. /*!
  28. \class Q3ValueList
  29. \brief The Q3ValueList class is a value-based template class that
  30. provides lists.
  31. \compat
  32. Q3ValueList is a Qt implementation of an STL-like list container.
  33. It can be used in your application if the standard \c list is not
  34. available for your target platforms.
  35. Q3ValueList\<T\> defines a template instance to create a list of
  36. values that all have the class T. Note that Q3ValueList does not
  37. store pointers to the members of the list; it holds a copy of
  38. every member. This is why these kinds of classes are called "value
  39. based"; Q3PtrList and Q3Dict are "pointer based".
  40. Q3ValueList contains and manages a collection of objects of type T
  41. and provides iterators that allow the contained objects to be
  42. addressed. Q3ValueList owns the contained items. For more relaxed
  43. ownership semantics, see Q3PtrCollection and friends which are
  44. pointer-based containers.
  45. Some classes cannot be used within a Q3ValueList, for example, all
  46. classes derived from QObject and thus all classes that implement
  47. widgets. Only values can be used in a Q3ValueList. To qualify as a
  48. value the class must provide:
  49. \list
  50. \i a copy constructor;
  51. \i an assignment operator;
  52. \i a default constructor, i.e. a constructor that does not take any arguments.
  53. \endlist
  54. Note that C++ defaults to field-by-field assignment operators and
  55. copy constructors if no explicit version is supplied. In many
  56. cases this is sufficient.
  57. In addition, some compilers (e.g. Sun CC) might require that the
  58. class provides an equality operator (operator==()).
  59. Q3ValueList's function naming is consistent with the other Qt
  60. classes (e.g. count(), isEmpty()). Q3ValueList also provides extra
  61. functions for compatibility with STL algorithms, such as size()
  62. and empty(). Programmers already familiar with the STL \c list may
  63. prefer to use the STL-compatible functions.
  64. Example:
  65. \snippet doc/src/snippets/code/doc_src_q3valuelist.cpp 0
  66. Notice that the latest changes to Mary's salary did not affect the
  67. value in the list because the list created a copy of Mary's entry.
  68. There are several ways to find items in the list. The begin() and
  69. end() functions return iterators to the beginning and end of the
  70. list. The advantage of getting an iterator is that you can move
  71. forward or backward from this position by
  72. incrementing/decrementing the iterator. The iterator returned by
  73. end() points to the item which is one \e past the last item in the
  74. container. The past-the-end iterator is still associated with the
  75. list it belongs to, however it is \e not dereferenceable;
  76. operator*() will not return a well-defined value. If the list is
  77. empty(), the iterator returned by begin() will equal the iterator
  78. returned by end().
  79. It is safe to have multiple iterators a the list at the same
  80. time. If some member of the list is removed, only iterators
  81. pointing to the removed member become invalid. Inserting into the
  82. list does not invalidate any iterator. For convenience, the
  83. function last() returns a reference to the last item in the list,
  84. and first() returns a reference to the first item. If the
  85. list is empty(), both last() and first() have undefined behavior
  86. (your application will crash or do unpredictable things). Use
  87. last() and first() with caution, for example:
  88. \snippet doc/src/snippets/code/doc_src_q3valuelist.cpp 1
  89. Because Q3ValueList is value-based there is no need to be careful
  90. about deleting items in the list. The list holds its own copies
  91. and will free them if the corresponding member or the list itself
  92. is deleted. You can force the list to free all of its items with
  93. clear().
  94. Q3ValueList is shared implicitly, which means it can be copied in
  95. constant time, i.e. O(1). If multiple Q3ValueList instances share
  96. the same data and one needs to modify its contents, this modifying
  97. instance makes a copy and modifies its private copy; therefore it
  98. does not affect the other instances; this takes O(n) time. This is
  99. often called "copy on write". If a Q3ValueList is being used in a
  100. multi-threaded program, you must protect all access to the list.
  101. See \l QMutex.
  102. There are several ways to insert items into the list. The
  103. prepend() and append() functions insert items at the beginning and
  104. the end of the list respectively. The insert() function comes in
  105. several flavors and can be used to add one or more items at
  106. specific positions within the list.
  107. Items can also be removed from the list in several ways. There
  108. are several variants of the remove() function, which removes a
  109. specific item from the list. The remove() function will find and
  110. remove items according to a specific item value.
  111. \sa Q3ValueListIterator
  112. */
  113. /*! \typedef Q3ValueList::iterator
  114. The list's iterator type, Q3ValueListIterator. */
  115. /*! \typedef Q3ValueList::const_iterator
  116. The list's const iterator type, Q3ValueListConstIterator. */
  117. /*! \typedef Q3ValueList::value_type
  118. The type of the object stored in the list, T. */
  119. /*! \typedef Q3ValueList::pointer
  120. The pointer to T type. */
  121. /*! \typedef Q3ValueList::const_pointer
  122. The const pointer to T type. */
  123. /*! \typedef Q3ValueList::reference
  124. The reference to T type. */
  125. /*! \typedef Q3ValueList::const_reference
  126. The const reference to T type. */
  127. /*! \typedef Q3ValueList::size_type
  128. An unsigned integral type, used to represent various sizes. */
  129. /*! \typedef Q3ValueList::difference_type
  130. \internal
  131. */
  132. /*!
  133. \fn Q3ValueList::Q3ValueList()
  134. Constructs an empty list.
  135. */
  136. /*!
  137. \fn Q3ValueList::Q3ValueList( const Q3ValueList<T>& l )
  138. \fn Q3ValueList::Q3ValueList( const QList<T>& l )
  139. \fn Q3ValueList::Q3ValueList( const QLinkedList<T>& l )
  140. Constructs a copy of \a l.
  141. */
  142. /*!
  143. \fn Q3ValueList::Q3ValueList( const std::list<T>& l )
  144. Contructs a copy of \a l.
  145. This constructor is provided for compatibility with STL
  146. containers.
  147. */
  148. /*!
  149. \fn Q3ValueList::~Q3ValueList()
  150. Destroys the list. References to the values in the list and all
  151. iterators of this list become invalidated. Note that it is
  152. impossible for an iterator to check whether or not it is valid:
  153. Q3ValueList is highly tuned for performance, not for error
  154. checking.
  155. */
  156. /*!
  157. \fn bool Q3ValueList::operator== ( const Q3ValueList<T>& l ) const
  158. Compares both lists.
  159. Returns TRUE if this list and \a l are equal; otherwise returns
  160. FALSE.
  161. */
  162. /*!
  163. \fn bool Q3ValueList::operator== ( const std::list<T>& l ) const
  164. \overload
  165. Returns TRUE if this list and \a l are equal; otherwise returns
  166. FALSE.
  167. This operator is provided for compatibility with STL containers.
  168. */
  169. /*!
  170. \fn Q3ValueList<T>& Q3ValueList::operator= ( const Q3ValueList<T>& l )
  171. Assigns \a l to this list and returns a reference to this list.
  172. All iterators of the current list become invalidated by this
  173. operation. The cost of such an assignment is O(1) since Q3ValueList
  174. is implicitly shared.
  175. */
  176. /*!
  177. \fn Q3ValueList<T>& Q3ValueList::operator= ( const QList<T>& l )
  178. Assigns \a l to this list and returns a reference to this list.
  179. All iterators of the current list become invalidated by this
  180. operation.
  181. */
  182. /*!
  183. \fn Q3ValueList<T>& Q3ValueList::operator= ( const std::list<T>& l )
  184. \overload
  185. Assigns the contents of \a l to the list.
  186. All iterators of the current list become invalidated by this
  187. operation.
  188. */
  189. /*!
  190. \fn bool Q3ValueList::operator!= ( const Q3ValueList<T>& l ) const
  191. Compares both lists.
  192. Returns TRUE if this list and \a l are unequal; otherwise returns
  193. FALSE.
  194. */
  195. /*!
  196. \fn iterator Q3ValueList::insert( typename Q3ValueList<T>::Iterator it, const T& x )
  197. Inserts the value \a x in front of the item pointed to by the
  198. iterator, \a it.
  199. Returns an iterator pointing at the inserted item.
  200. \sa append(), prepend()
  201. */
  202. /*!
  203. \fn uint Q3ValueList::remove( const T& x )
  204. \overload
  205. Removes all items that have value \a x and returns the number of
  206. removed items.
  207. */
  208. /*!
  209. \fn QDataStream& operator>>( QDataStream& s, Q3ValueList<T>& l )
  210. \relates Q3ValueList
  211. Reads a list, \a l, from the stream \a s. The type T stored in the
  212. list must implement the streaming operator.
  213. */
  214. /*!
  215. \fn QDataStream& operator<<( QDataStream& s, const Q3ValueList<T>& l )
  216. \overload
  217. \relates Q3ValueList
  218. Writes a list, \a l, to the stream \a s. The type T stored in the
  219. list must implement the streaming operator.
  220. */
  221. /*!
  222. \fn void Q3ValueList::insert( typename Q3ValueList<T>::Iterator pos,
  223. typename Q3ValueList<T>::size_type n, const T& x )
  224. \overload
  225. Inserts \a n copies of \a x before position \a pos.
  226. */
  227. /*!
  228. \fn Q3ValueList<T>& Q3ValueList::operator<< ( const T& x )
  229. Adds the value \a x to the end of the list.
  230. Returns a reference to the list.
  231. */
  232. /*!
  233. \fn const T& Q3ValueList::operator[] ( typename Q3ValueList<T>::size_type i ) const
  234. Returns a const reference to the item with index \a i in the list.
  235. It is up to you to check whether this item really exists. You can
  236. do that easily with the count() function. However this operator
  237. does not check whether \a i is in range and will deliver undefined
  238. results if it does not exist.
  239. \warning This function uses a linear search and can be extremely
  240. slow for large lists. Q3ValueList is not optimized for random item
  241. access. If you need random access use a different container, such
  242. as Q3ValueVector.
  243. */
  244. /*!
  245. \fn T& Q3ValueList::operator[] ( typename Q3ValueList<T>::size_type i )
  246. \overload
  247. Returns a non-const reference to the item with index \a i.
  248. */
  249. /*!
  250. \fn const_iterator Q3ValueList::at( typename Q3ValueList<T>::size_type i ) const
  251. Returns an iterator pointing to the item at position \a i in the
  252. list, or an undefined value if the index is out of range.
  253. \warning This function uses a linear search and can be extremely
  254. slow for large lists. Q3ValueList is not optimized for random item
  255. access. If you need random access use a different container, such
  256. as Q3ValueVector.
  257. */
  258. /*!
  259. \fn iterator Q3ValueList::at( typename Q3ValueList<T>::size_type i )
  260. \overload
  261. Returns an iterator pointing to the item at position \a i in the
  262. list, or an undefined value if the index is out of range.
  263. */
  264. /*!
  265. \fn iterator Q3ValueList::fromLast()
  266. \overload
  267. Returns an iterator to the last item in the list, or end() if
  268. there is no last item.
  269. Use the end() function instead. For example:
  270. \snippet doc/src/snippets/code/doc_src_q3valuelist.cpp 2
  271. */
  272. /*!
  273. \fn const_iterator Q3ValueList::fromLast() const
  274. Returns an iterator to the last item in the list, or end() if
  275. there is no last item.
  276. Use the end() function instead. For example:
  277. \snippet doc/src/snippets/code/doc_src_q3valuelist.cpp 3
  278. */
  279. /*!
  280. \fn Q3ValueList<T> Q3ValueList::operator+( const Q3ValueList<T>& l ) const
  281. Creates a new list and fills it with the items of this list. Then
  282. the items of \a l are appended. Returns the new list.
  283. */
  284. /*!
  285. \fn Q3ValueList<T>& Q3ValueList::operator+= ( const Q3ValueList<T>& l )
  286. Appends the items of \a l to this list. Returns a reference to
  287. this list.
  288. */
  289. /*!
  290. \fn Q3ValueList<T>& Q3ValueList::operator+= ( const T& x )
  291. \overload
  292. Appends the value \a x to the list. Returns a reference to the
  293. list.
  294. */
  295. /*!
  296. \fn iterator Q3ValueList::append( const T& x )
  297. Inserts \a x at the end of the list.
  298. \sa insert(), prepend()
  299. */
  300. /*!
  301. \fn iterator Q3ValueList::prepend( const T& x )
  302. Inserts \a x at the beginning of the list.
  303. \sa insert(), append()
  304. */
  305. /*!
  306. \fn iterator Q3ValueList::remove( typename Q3ValueList<T>::Iterator it )
  307. Removes the item pointed to by \a it from the list. No iterators
  308. other than \a it or other iterators pointing at the same item as
  309. \a it are invalidated. Returns an iterator to the next item after
  310. \a it, or end() if there is no such item.
  311. \sa clear()
  312. */
  313. /*!
  314. \fn uint Q3ValueList::contains( const T& x ) const
  315. Returns the number of occurrences of the value \a x in the list.
  316. */
  317. /*!
  318. \class Q3ValueListIterator
  319. \brief The Q3ValueListIterator class provides an iterator for Q3ValueList.
  320. \compat
  321. An iterator is a class for accessing the items of a container
  322. class: a generalization of the index in an array. A pointer
  323. into a "const char *" and an index into an "int[]" are both
  324. iterators, and the general idea is to provide that functionality
  325. for any data structure.
  326. The Q3ValueListIterator class is an iterator for Q3ValueList
  327. instantiations. You can create the appropriate iterator type by
  328. using the \c iterator typedef provided by Q3ValueList.
  329. The only way to access the items in a Q3ValueList is to use an
  330. iterator.
  331. Example (see Q3ValueList for the complete code):
  332. \snippet doc/src/snippets/code/doc_src_q3valuelist.cpp 4
  333. Q3ValueList is highly optimized for performance and memory usage.
  334. This means that you must be careful: Q3ValueList does not know
  335. about all its iterators and the iterators don't know to which list
  336. they belong. This makes things very fast, but if you're not
  337. careful, you can get spectacular bugs. Always make sure iterators
  338. are valid before dereferencing them or using them as parameters to
  339. generic algorithms in the STL.
  340. Using an invalid iterator is undefined (your application will
  341. probably crash). Many Qt functions return const value lists; to
  342. iterate over these you should make a copy and iterate over the
  343. copy.
  344. For every Iterator there is a ConstIterator. When accessing a
  345. Q3ValueList in a const environment or if the reference or pointer
  346. to the list is itself const, then you must use the ConstIterator.
  347. Its semantics are the same as the Iterator, but it only returns
  348. const references.
  349. \sa Q3ValueList, Q3ValueListConstIterator
  350. */
  351. /*!
  352. \fn Q3ValueListIterator::Q3ValueListIterator()
  353. Constructs an unitialized iterator.
  354. */
  355. /*!
  356. \fn Q3ValueListIterator::Q3ValueListIterator(const Q3ValueListIterator &o)
  357. \fn Q3ValueListIterator::Q3ValueListIterator(const typename QLinkedList<T>::iterator &o)
  358. Constucts a copy of iterator \a o.
  359. */
  360. /*!
  361. \class Q3ValueListConstIterator
  362. \brief The Q3ValueListConstIterator class provides a const iterator
  363. for Q3ValueList.
  364. \compat
  365. In contrast to Q3ValueListIterator, this class is used to iterate
  366. over a const list. It does not allow modification of the values of
  367. the list since that would break const semantics.
  368. You can create the appropriate const iterator type by using the \c
  369. const_iterator typedef provided by Q3ValueList.
  370. For more information on Q3ValueList iterators, see
  371. Q3ValueListIterator.
  372. \sa Q3ValueListIterator, Q3ValueList
  373. */
  374. /*!
  375. \fn Q3ValueListConstIterator::Q3ValueListConstIterator()
  376. Constructs an unitialized iterator.
  377. */
  378. /*!
  379. \fn Q3ValueListConstIterator::Q3ValueListConstIterator(const Q3ValueListConstIterator &o)
  380. \fn Q3ValueListConstIterator::Q3ValueListConstIterator(const typename QLinkedList<T>::const_iterator &o)
  381. \fn Q3ValueListConstIterator::Q3ValueListConstIterator(const typename QLinkedList<T>::iterator &o)
  382. Constructs a copy of iterator \a o.
  383. */
  384. /*!
  385. \typedef Q3ValueList::Iterator
  386. This iterator is an instantiation of Q3ValueListIterator for the
  387. same type as this Q3ValueList. In other words, if you instantiate
  388. Q3ValueList<int>, Iterator is a Q3ValueListIterator<int>. Several
  389. member function use it, such as Q3ValueList::begin(), which returns
  390. an iterator pointing to the first item in the list.
  391. Functionally, this is almost the same as ConstIterator. The only
  392. difference is that you cannot use ConstIterator for non-const
  393. operations, and that the compiler can often generate better code
  394. if you use ConstIterator.
  395. \sa Q3ValueListIterator ConstIterator
  396. */
  397. /*!
  398. \typedef Q3ValueList::ConstIterator
  399. This iterator is an instantiation of Q3ValueListConstIterator for
  400. the same type as this Q3ValueList. In other words, if you
  401. instantiate Q3ValueList<int>, ConstIterator is a
  402. Q3ValueListConstIterator<int>. Several member function use it, such
  403. as Q3ValueList::begin(), which returns an iterator pointing to the
  404. first item in the list.
  405. Functionally, this is almost the same as Iterator. The only
  406. difference is you cannot use ConstIterator for non-const
  407. operations, and that the compiler can often generate better code
  408. if you use ConstIterator.
  409. \sa Q3ValueListIterator Iterator
  410. */
  411. /*!
  412. \fn Q3ValueList::operator QList<T>() const
  413. Automatically converts a Q3ValueList<T> into a QList<T>.
  414. */