Blame qtools/qtl.doc

Packit Service 50c9f2
/****************************************************************************
Packit Service 50c9f2
** 
Packit Service 50c9f2
**
Packit Service 50c9f2
** Qt template library classes documentation
Packit Service 50c9f2
**
Packit Service 50c9f2
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file is part of the Qt GUI Toolkit.
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file may be distributed under the terms of the Q Public License
Packit Service 50c9f2
** as defined by Trolltech AS of Norway and appearing in the file
Packit Service 50c9f2
** LICENSE.QPL included in the packaging of this file.
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file may be distributed and/or modified under the terms of the
Packit Service 50c9f2
** GNU General Public License version 2 as published by the Free Software
Packit Service 50c9f2
** Foundation and appearing in the file LICENSE.GPL included in the
Packit Service 50c9f2
** packaging of this file.
Packit Service 50c9f2
**
Packit Service 50c9f2
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
Packit Service 50c9f2
** licenses may use this file in accordance with the Qt Commercial License
Packit Service 50c9f2
** Agreement provided with the Software.
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
Packit Service 50c9f2
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Packit Service 50c9f2
**
Packit Service 50c9f2
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
Packit Service 50c9f2
**   information about Qt Commercial License Agreements.
Packit Service 50c9f2
** See http://www.trolltech.com/qpl/ for QPL licensing information.
Packit Service 50c9f2
** See http://www.trolltech.com/gpl/ for GPL licensing information.
Packit Service 50c9f2
**
Packit Service 50c9f2
** Contact info@trolltech.com if any conditions of this licensing are
Packit Service 50c9f2
** not clear to you.
Packit Service 50c9f2
**
Packit Service 50c9f2
**********************************************************************/
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
\page qtl.html
Packit Service 50c9f2
Packit Service 50c9f2
\title Qt Template library
Packit Service 50c9f2
Packit Service 50c9f2
Thq Qt Template Library is a set of templates within Qt dealing with
Packit Service 50c9f2
containers of objects.  It provides a list of objects, a stack of
Packit Service 50c9f2
objects, a map (or dictionary) from one type to another, and
Packit Service 50c9f2
associated iterators and algorithms.
Packit Service 50c9f2
Packit Service 50c9f2
Qt also contains similar classes that deal with pointers to objects;
Packit Service 50c9f2
\l QValueList vs. \l QList, etc.  Compared to the pointer-based
Packit Service 50c9f2
templates, the QTL offers easy copying of the container, real support
Packit Service 50c9f2
for classes that e.g. require constructors, expand to much more object
Packit Service 50c9f2
code, can often be a bit faster, require that the objects stored can
Packit Service 50c9f2
be copied, and finally, have a worse record of compiler problems.
Packit Service 50c9f2
Packit Service 50c9f2
Compared to the STL, the QTL contains only the most important features
Packit Service 50c9f2
of the STL, has more regular function naming, has no platform
Packit Service 50c9f2
differences, is often a little slower and often expands to less object
Packit Service 50c9f2
code.
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
If you can not make copies of the objects you want to store you are
Packit Service 50c9f2
better off with QCollection and friends. They were designed to handle
Packit Service 50c9f2
exactly that kind of pointer semantics. This applies for example to
Packit Service 50c9f2
all classes derived from \l QObject. A QObject does not have a copy
Packit Service 50c9f2
constructor, so using it as value is impossible. You may choose be
Packit Service 50c9f2
store pointers to QObjects in a QValueList, but using QList directly
Packit Service 50c9f2
seems to be the better choice for this kind of application
Packit Service 50c9f2
domain. QList, like all other QCollection based containers, provides
Packit Service 50c9f2
far more sanity checking than a speed-optimized value
Packit Service 50c9f2
based container.
Packit Service 50c9f2
Packit Service 50c9f2
If you have objects that implement value semantics, use the Qt
Packit Service 50c9f2
template library.  Value semantics require at least
Packit Service 50c9f2
    Packit Service 50c9f2
  • a copy constructor,
  • Packit Service 50c9f2
  • an assignment operator and
  • Packit Service 50c9f2
  • a default constructor, i.e. a constructor that does not take
  • Packit Service 50c9f2
    any arguments.
    Packit Service 50c9f2
    Packit Service 50c9f2
    Note that a fast copy constructor is absolutely crucial for a good
    Packit Service 50c9f2
    overall performance of the container, since many copy operations are
    Packit Service 50c9f2
    going to happen.
    Packit Service 50c9f2
    Packit Service 50c9f2
    Examples for value based classes are QRect, QPoint, QSize and all
    Packit Service 50c9f2
    simple C++ types like int, bool or double.
    Packit Service 50c9f2
    Packit Service 50c9f2
    The Qt template library is designed for speed. Especially iterators
    Packit Service 50c9f2
    are extremely fast. On the drawback side, less error checking is done
    Packit Service 50c9f2
    than in the QCollection based containers. A template library container
    Packit Service 50c9f2
    for example does not track associated iterators. This makes certain
    Packit Service 50c9f2
    validity checks, like on removing items, impossible to perform
    Packit Service 50c9f2
    automatically.
    Packit Service 50c9f2
    Packit Service 50c9f2

    Iterators

    Packit Service 50c9f2
    Packit Service 50c9f2
    The Qt template library deals with value objects, not with pointers.
    Packit Service 50c9f2
    For that reason, there is no other way of iterating over containers
    Packit Service 50c9f2
    than using iterators. This is no disadvantage as the size of an
    Packit Service 50c9f2
    iterator matches the size of a normal pointer - 32 or 64 bits
    Packit Service 50c9f2
    depending on your CPU architecture.
    Packit Service 50c9f2
    Packit Service 50c9f2
    To iterate over a container, use a loop like this:
    Packit Service 50c9f2
    Packit Service 50c9f2
    \code
    Packit Service 50c9f2
    	typedef QValueList<int> List;
    Packit Service 50c9f2
    	List l;
    Packit Service 50c9f2
    	for( List::Iterator it = l.begin(); it != l.end(); ++it )
    Packit Service 50c9f2
    		printf("Number is %i\n",*it);
    Packit Service 50c9f2
    \endcode
    Packit Service 50c9f2
    Packit Service 50c9f2
    begin() returns the iterator pointing at the first element, while
    Packit Service 50c9f2
    end() returns an iterator that points \e after the last
    Packit Service 50c9f2
    element. end() marks an invalid position, it can never be
    Packit Service 50c9f2
    dereferenced. It's the break condition in any iteration, may it be
    Packit Service 50c9f2
    from begin() or fromLast(). For maximum speed, use increment or
    Packit Service 50c9f2
    decrement iterators with the prefix operator (++it, --it) instead of the the
    Packit Service 50c9f2
    postfix one (it++, it--), since the former is slightly faster.
    Packit Service 50c9f2
    Packit Service 50c9f2
    The same concept applies to the other container classes:
    Packit Service 50c9f2
    Packit Service 50c9f2
    \code
    Packit Service 50c9f2
    	typedef QMap<QString,QString> Map;
    Packit Service 50c9f2
    	Map map;
    Packit Service 50c9f2
    	for( Map::Iterator it = map.begin(); it != map.end(); ++it )
    Packit Service 50c9f2
    		printf("Key=%s Data=%s\n", it.key().ascii(), it.data().ascii() );
    Packit Service 50c9f2
    Packit Service 50c9f2
    	typedef QArray<int> Array;
    Packit Service 50c9f2
    	Array array;
    Packit Service 50c9f2
    	for( Array::Iterator it = array.begin(); it != array.end(); ++it )
    Packit Service 50c9f2
    		printf("Data=%i\n", *it );
    Packit Service 50c9f2
    \endcode
    Packit Service 50c9f2
    Packit Service 50c9f2
    There are two kind of iterators, the volatile iterator shown in the
    Packit Service 50c9f2
    examples above and a version that returns a const reference to its
    Packit Service 50c9f2
    current object, the ConstIterator. Const iterators are required
    Packit Service 50c9f2
    whenever the container itself is const, such as a member variable
    Packit Service 50c9f2
    inside a const function. Assigning a ConstIterator to a normal
    Packit Service 50c9f2
    Iterator is not allowed as it would violate const semantics.
    Packit Service 50c9f2
    Packit Service 50c9f2

    Algorithms

    Packit Service 50c9f2
    Packit Service 50c9f2
    The template library defines a number of algorithms that operate on
    Packit Service 50c9f2
    its containers: qHeapSort(), qBubbleSort(), qSwap() and
    Packit Service 50c9f2
    qCopy(). These algorithms are implemented as template functions.
    Packit Service 50c9f2
    Packit Service 50c9f2
    qHeapSort() and qBubbleSort() provide the well known sorting
    Packit Service 50c9f2
    algorithms. You can use them like this:
    Packit Service 50c9f2
    Packit Service 50c9f2
    \code
    Packit Service 50c9f2
    	typedef QValueList<int> List;
    Packit Service 50c9f2
    	List l;
    Packit Service 50c9f2
    	l << 42 << 100 << 1234 << 12 << 8;
    Packit Service 50c9f2
    	qHeapSort( l );
    Packit Service 50c9f2
    	
    Packit Service 50c9f2
    	List l2;
    Packit Service 50c9f2
    	l2 << 42 << 100 << 1234 << 12 << 8;
    Packit Service 50c9f2
    	List::Iterator b = l2.find( 100 );
    Packit Service 50c9f2
    	List::Iterator e = l2.find( 8 );
    Packit Service 50c9f2
    	qHeapSort( b, e );
    Packit Service 50c9f2
    Packit Service 50c9f2
    	double arr[] = { 3.2, 5.6, 8.9 };
    Packit Service 50c9f2
    	qHeapSort( arr, arr + 3 );
    Packit Service 50c9f2
    \endcode
    Packit Service 50c9f2
    Packit Service 50c9f2
    The first example sorts the entire list. The second one sorts all
    Packit Service 50c9f2
    elements enclosed in the two iterators, namely 100, 1234 and 12.  The
    Packit Service 50c9f2
    third example shows that iterators act like pointers and can be
    Packit Service 50c9f2
    treated as such.
    Packit Service 50c9f2
    Packit Service 50c9f2
    Naturally, the sorting templates won't work with const iterators.
    Packit Service 50c9f2
    Packit Service 50c9f2
    Another utility is qSwap(). It exchanges the values of two variables:
    Packit Service 50c9f2
    Packit Service 50c9f2
    \code
    Packit Service 50c9f2
    	QString second( "Einstein" );
    Packit Service 50c9f2
    	QString name( "Albert" );
    Packit Service 50c9f2
    	qSwap( second, name );
    Packit Service 50c9f2
    \endcode
    Packit Service 50c9f2
    Packit Service 50c9f2
    Another template function is qCopy(). It copies a container or a slice
    Packit Service 50c9f2
    of it to an OutputIterator, in this case a QTextOStreamIterator:
    Packit Service 50c9f2
    Packit Service 50c9f2
    \code
    Packit Service 50c9f2
    	typedef QValueList<int> List;
    Packit Service 50c9f2
    	List l;
    Packit Service 50c9f2
    	l << 100 << 200 << 300;
    Packit Service 50c9f2
    	QTextOStream str( stdout );
    Packit Service 50c9f2
    	qCopy( l, QTextOStreamIterator( str ) );
    Packit Service 50c9f2
    \endcode
    Packit Service 50c9f2
    Packit Service 50c9f2
    In addition, you can use any Qt template library iterator as the
    Packit Service 50c9f2
    OutputIterator. Just make sure that the right hand of the iterator has
    Packit Service 50c9f2
    as many elements present as you want to insert. The following example
    Packit Service 50c9f2
    illustrates this:
    Packit Service 50c9f2
    Packit Service 50c9f2
    \code
    Packit Service 50c9f2
    	QStringList l1, l2;
    Packit Service 50c9f2
    	l1 << "Weis" << "Ettrich" << "Arnt" << "Sue";
    Packit Service 50c9f2
    	l2 << "Torben" << "Matthias";
    Packit Service 50c9f2
    	qCopy( l2, l1.begin();
    Packit Service 50c9f2
    \endcode
    Packit Service 50c9f2
    Packit Service 50c9f2
    At the end of this code fragment, the List l1 contains "Torben",
    Packit Service 50c9f2
    "Matthias", "Arnt" and "Sue", with the prior contents being
    Packit Service 50c9f2
    overwritten. Another flavor of qCopy() takes three arguments to make
    Packit Service 50c9f2
    it possible to copy a slice of a container:
    Packit Service 50c9f2
    Packit Service 50c9f2
    \code
    Packit Service 50c9f2
    	typedef QValueList<int> List;
    Packit Service 50c9f2
    	List l;
    Packit Service 50c9f2
    	l << 42 << 100 << 1234 << 12 << 8;
    Packit Service 50c9f2
    	List::Iterator b = l.find( 100 );
    Packit Service 50c9f2
    	List::Iterator e = l.find( 8 );
    Packit Service 50c9f2
    	QTextOStream str( stdout );
    Packit Service 50c9f2
    	qCopy( b, e, QTextOStreamIterator( str ) );
    Packit Service 50c9f2
    \endcode
    Packit Service 50c9f2
    Packit Service 50c9f2
    If you write new algorithms, consider writing them as template
    Packit Service 50c9f2
    functions in order to make them usable with as many containers
    Packit Service 50c9f2
    possible.  In the above example, you could just as easily print out a
    Packit Service 50c9f2
    standard C++ array with qCopy():
    Packit Service 50c9f2
    Packit Service 50c9f2
    \code
    Packit Service 50c9f2
    	int arr[] = { 100, 200, 300 };
    Packit Service 50c9f2
    	QTextOStream str( stdout );
    Packit Service 50c9f2
    	qCopy( arr, arr + 3, QTextOStreamIterator( str ) );	
    Packit Service 50c9f2
    \endcode
    Packit Service 50c9f2
    Packit Service 50c9f2
    Packit Service 50c9f2

    Streaming

    Packit Service 50c9f2
    Packit Service 50c9f2
    All mentioned containers can be serialized with the respective
    Packit Service 50c9f2
    streaming operators. Here is an example.
    Packit Service 50c9f2
    Packit Service 50c9f2
    \code
    Packit Service 50c9f2
    	QDataStream str(...);
    Packit Service 50c9f2
    	QValueList<QRect> l;
    Packit Service 50c9f2
    	// ... fill the list here
    Packit Service 50c9f2
    	str << l;
    Packit Service 50c9f2
    \endcode
    Packit Service 50c9f2
    Packit Service 50c9f2
    The container can be read in again with:
    Packit Service 50c9f2
    Packit Service 50c9f2
    \code
    Packit Service 50c9f2
    	QValueList<QRect> l;
    Packit Service 50c9f2
    	str >> l;
    Packit Service 50c9f2
    \endcode
    Packit Service 50c9f2
    Packit Service 50c9f2
    The same applies to QStringList, QValueStack and QMap.
    Packit Service 50c9f2
    Packit Service 50c9f2
    */