1 /***************************************************************************
2 copyright : (C) 2002 - 2008 by Scott Wheeler
3 email : wheeler@kde.org
4 ***************************************************************************/
6 /***************************************************************************
7 * This library is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU Lesser General Public License version *
9 * 2.1 as published by the Free Software Foundation. *
11 * This library is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the Free Software *
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
21 * Alternatively, this file is available under the Mozilla Public *
22 * License Version 1.1. You may obtain a copy of the License at *
23 * http://www.mozilla.org/MPL/ *
24 ***************************************************************************/
35 //! A generic, implicitly shared list.
38 * This is basic generic list that's somewhere between a std::list and a
39 * QValueList. This class is implicitly shared. For example:
43 * TagLib::List<int> l = someOtherIntList;
47 * The above example is very cheap. This also makes lists suitable for the
48 * return types of functions. The above example will just copy a pointer rather
49 * than copying the data in the list. When your \e shared list's data changes,
50 * only \e then will the data be copied.
53 template <class T> class List
56 #ifndef DO_NOT_DOCUMENT
57 typedef typename std::list<T>::iterator Iterator;
58 typedef typename std::list<T>::const_iterator ConstIterator;
62 * Constructs an empty list.
67 * Make a shallow, implicitly shared, copy of \a l. Because this is
68 * implicitly shared, this method is lightweight and suitable for
69 * pass-by-value usage.
71 List(const List<T> &l);
74 * Destroys this List instance. If auto deletion is enabled and this list
75 * contains a pointer type all of the memebers are also deleted.
80 * Returns an STL style iterator to the beginning of the list. See
81 * std::list::const_iterator for the semantics.
86 * Returns an STL style constant iterator to the beginning of the list. See
87 * std::list::iterator for the semantics.
89 ConstIterator begin() const;
92 * Returns an STL style iterator to the end of the list. See
93 * std::list::iterator for the semantics.
98 * Returns an STL style constant iterator to the end of the list. See
99 * std::list::const_iterator for the semantics.
101 ConstIterator end() const;
104 * Inserts a copy of \a value before \a it.
106 Iterator insert(Iterator it, const T &value);
109 * Inserts the \a value into the list. This assumes that the list is
110 * currently sorted. If \a unique is true then the value will not
111 * be inserted if it is already in the list.
113 List<T> &sortedInsert(const T &value, bool unique = false);
116 * Appends \a item to the end of the list and returns a reference to the
119 List<T> &append(const T &item);
122 * Appends all of the values in \a l to the end of the list and returns a
123 * reference to the list.
125 List<T> &append(const List<T> &l);
128 * Prepends \a item to the beginning list and returns a reference to the
131 List<T> &prepend(const T &item);
134 * Prepends all of the items in \a l to the beginning list and returns a
135 * reference to the list.
137 List<T> &prepend(const List<T> &l);
140 * Clears the list. If auto deletion is enabled and this list contains a
141 * pointer type the members are also deleted.
143 * \see setAutoDelete()
148 * Returns the number of elements in the list.
151 bool isEmpty() const;
154 * Find the first occurrence of \a value.
156 Iterator find(const T &value);
159 * Find the first occurrence of \a value.
161 ConstIterator find(const T &value) const;
164 * Returns true if the list contains \a value.
166 bool contains(const T &value) const;
169 * Erase the item at \a it from the list.
171 Iterator erase(Iterator it);
174 * Returns a reference to the first item in the list.
176 const T &front() const;
179 * Returns a reference to the first item in the list.
184 * Returns a reference to the last item in the list.
186 const T &back() const;
189 * Returns a reference to the last item in the list.
194 * Auto delete the members of the list when the last reference to the list
195 * passes out of scope. This will have no effect on lists which do not
196 * contain a pointer type.
198 * \note This relies on partial template instantiation -- most modern C++
199 * compilers should now support this.
201 void setAutoDelete(bool autoDelete);
204 * Returns a reference to item \a i in the list.
206 * \warning This method is slow. Use iterators to loop through the list.
208 T &operator[](uint i);
211 * Returns a const reference to item \a i in the list.
213 * \warning This method is slow. Use iterators to loop through the list.
215 const T &operator[](uint i) const;
218 * Make a shallow, implicitly shared, copy of \a l. Because this is
219 * implicitly shared, this method is lightweight and suitable for
220 * pass-by-value usage.
222 List<T> &operator=(const List<T> &l);
225 * Compares this list with \a l and returns true if all of the elements are
228 bool operator==(const List<T> &l) const;
232 * If this List is being shared via implicit sharing, do a deep copy of the
233 * data and separate from the shared members. This should be called by all
234 * non-const subclass members.
239 #ifndef DO_NOT_DOCUMENT
240 template <class TP> class ListPrivate;
247 // Since GCC doesn't support the "export" keyword, we have to include the