Eneboo - Documentación para desarrolladores
src/qt/include/qvaluelist.h
Ir a la documentación de este archivo.
00001 /****************************************************************************
00002 ** $Id: qt/qvaluelist.h   3.3.8   edited Jan 11 14:38 $
00003 **
00004 ** Definition of QValueList class
00005 **
00006 ** Created : 990406
00007 **
00008 ** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
00009 **
00010 ** This file is part of the tools module of the Qt GUI Toolkit.
00011 **
00012 ** This file may be distributed under the terms of the Q Public License
00013 ** as defined by Trolltech ASA of Norway and appearing in the file
00014 ** LICENSE.QPL included in the packaging of this file.
00015 **
00016 ** This file may be distributed and/or modified under the terms of the
00017 ** GNU General Public License version 2 as published by the Free Software
00018 ** Foundation and appearing in the file LICENSE.GPL included in the
00019 ** packaging of this file.
00020 **
00021 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
00022 ** licenses may use this file in accordance with the Qt Commercial License
00023 ** Agreement provided with the Software.
00024 **
00025 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00026 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00027 **
00028 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
00029 **   information about Qt Commercial License Agreements.
00030 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
00031 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00032 **
00033 ** Contact info@trolltech.com if any conditions of this licensing are
00034 ** not clear to you.
00035 **
00036 **********************************************************************/
00037 
00038 #ifndef QVALUELIST_H
00039 #define QVALUELIST_H
00040 
00041 #ifndef QT_H
00042 #include "qtl.h"
00043 #include "qshared.h"
00044 #include "qdatastream.h"
00045 #endif // QT_H
00046 
00047 #ifndef QT_NO_STL
00048 #include <iterator>
00049 #include <list>
00050 #endif
00051 
00052 //#define QT_CHECK_VALUELIST_RANGE
00053 
00054 #if defined(Q_CC_MSVC)
00055 #pragma warning(disable:4284) // "return type for operator -> is not a UDT"
00056 #endif
00057 
00058 template <class T>
00059 class QValueListNode
00060 {
00061 public:
00062     QValueListNode( const T& t ) : data( t ) { }
00063     QValueListNode() { }
00064 #if defined(Q_TEMPLATEDLL)
00065     // Workaround MS bug in memory de/allocation in DLL vs. EXE
00066     virtual ~QValueListNode() { }
00067 #endif
00068 
00069     QValueListNode<T>* next;
00070     QValueListNode<T>* prev;
00071     T data;
00072 };
00073 
00074 template<class T>
00075 class QValueListIterator
00076 {
00077  public:
00081     typedef QValueListNode<T>* NodePtr;
00082 #ifndef QT_NO_STL
00083     typedef std::bidirectional_iterator_tag  iterator_category;
00084 #endif
00085     typedef T        value_type;
00086     typedef size_t size_type;
00087 #ifndef QT_NO_STL
00088     typedef ptrdiff_t  difference_type;
00089 #else
00090     typedef int difference_type;
00091 #endif
00092     typedef T*   pointer;
00093     typedef T& reference;
00094 
00098     NodePtr node;
00099 
00103     QValueListIterator() : node( 0 ) {}
00104     QValueListIterator( NodePtr p ) : node( p ) {}
00105     QValueListIterator( const QValueListIterator<T>& it ) : node( it.node ) {}
00106 
00107     bool operator==( const QValueListIterator<T>& it ) const { return node == it.node; }
00108     bool operator!=( const QValueListIterator<T>& it ) const { return node != it.node; }
00109     const T& operator*() const { return node->data; }
00110     T& operator*() { return node->data; }
00111     // UDT for T = x*
00112     // T* operator->() const { return &node->data; }
00113 
00114     QValueListIterator<T>& operator++() {
00115         node = node->next;
00116         return *this;
00117     }
00118 
00119     QValueListIterator<T> operator++(int) {
00120         QValueListIterator<T> tmp = *this;
00121         node = node->next;
00122         return tmp;
00123     }
00124 
00125     QValueListIterator<T>& operator--() {
00126         node = node->prev;
00127         return *this;
00128     }
00129 
00130     QValueListIterator<T> operator--(int) {
00131         QValueListIterator<T> tmp = *this;
00132         node = node->prev;
00133         return tmp;
00134     }
00135 
00136     QValueListIterator<T>& operator+=( int j ) {
00137         while ( j-- )
00138             node = node->next;
00139         return *this;
00140     }
00141 
00142     QValueListIterator<T>& operator-=( int j ) {
00143         while ( j-- )
00144             node = node->prev;
00145         return *this;
00146     }
00147 
00148 };
00149 
00150 template<class T>
00151 class QValueListConstIterator
00152 {
00153  public:
00157     typedef QValueListNode<T>* NodePtr;
00158 #ifndef QT_NO_STL
00159     typedef std::bidirectional_iterator_tag  iterator_category;
00160 #endif
00161     typedef T        value_type;
00162     typedef size_t size_type;
00163 #ifndef QT_NO_STL
00164     typedef ptrdiff_t  difference_type;
00165 #else
00166     typedef int difference_type;
00167 #endif
00168     typedef const T*   pointer;
00169     typedef const T& reference;
00170 
00174     NodePtr node;
00175 
00179     QValueListConstIterator() : node( 0 ) {}
00180     QValueListConstIterator( NodePtr p ) : node( p ) {}
00181     QValueListConstIterator( const QValueListConstIterator<T>& it ) : node( it.node ) {}
00182     QValueListConstIterator( const QValueListIterator<T>& it ) : node( it.node ) {}
00183 
00184     bool operator==( const QValueListConstIterator<T>& it ) const { return node == it.node; }
00185     bool operator!=( const QValueListConstIterator<T>& it ) const { return node != it.node; }
00186     const T& operator*() const { return node->data; }
00187     // UDT for T = x*
00188     // const T* operator->() const { return &node->data; }
00189 
00190     QValueListConstIterator<T>& operator++() {
00191         node = node->next;
00192         return *this;
00193     }
00194 
00195     QValueListConstIterator<T> operator++(int) {
00196         QValueListConstIterator<T> tmp = *this;
00197         node = node->next;
00198         return tmp;
00199     }
00200 
00201     QValueListConstIterator<T>& operator--() {
00202         node = node->prev;
00203         return *this;
00204     }
00205 
00206     QValueListConstIterator<T> operator--(int) {
00207         QValueListConstIterator<T> tmp = *this;
00208         node = node->prev;
00209         return tmp;
00210     }
00211 };
00212 
00213 template <class T>
00214 class QValueListPrivate : public QShared
00215 {
00216 public:
00220     typedef QValueListIterator<T> Iterator;
00221     typedef QValueListConstIterator<T> ConstIterator;
00222     typedef QValueListNode<T> Node;
00223     typedef QValueListNode<T>* NodePtr;
00224     typedef size_t size_type;
00225 
00229     QValueListPrivate();
00230     QValueListPrivate( const QValueListPrivate<T>& _p );
00231 
00232     void derefAndDelete() // ### hack to get around hp-cc brain damage
00233     {
00234         if ( deref() )
00235             delete this;
00236     }
00237 
00238 #if defined(Q_TEMPLATEDLL)
00239     // Workaround MS bug in memory de/allocation in DLL vs. EXE
00240     virtual
00241 #endif
00242     ~QValueListPrivate();
00243 
00244     Iterator insert( Iterator it, const T& x );
00245     Iterator remove( Iterator it );
00246     NodePtr find( NodePtr start, const T& x ) const;
00247     int findIndex( NodePtr start, const T& x ) const;
00248     uint contains( const T& x ) const;
00249     uint remove( const T& x );
00250     NodePtr at( size_type i ) const;
00251     void clear();
00252 
00253     NodePtr node;
00254     size_type nodes;
00255 };
00256 
00257 template <class T>
00258 Q_INLINE_TEMPLATES QValueListPrivate<T>::QValueListPrivate()
00259 {
00260     node = new Node; node->next = node->prev = node; nodes = 0;
00261 }
00262 
00263 template <class T>
00264 Q_INLINE_TEMPLATES QValueListPrivate<T>::QValueListPrivate( const QValueListPrivate<T>& _p )
00265     : QShared()
00266 {
00267     node = new Node; node->next = node->prev = node; nodes = 0;
00268     Iterator b( _p.node->next );
00269     Iterator e( _p.node );
00270     Iterator i( node );
00271     while( b != e )
00272         insert( i, *b++ );
00273 }
00274 
00275 template <class T>
00276 Q_INLINE_TEMPLATES QValueListPrivate<T>::~QValueListPrivate() {
00277     NodePtr p = node->next;
00278     while( p != node ) {
00279         NodePtr x = p->next;
00280         delete p;
00281         p = x;
00282     }
00283     delete node;
00284 }
00285 
00286 template <class T>
00287 Q_INLINE_TEMPLATES Q_TYPENAME QValueListPrivate<T>::Iterator QValueListPrivate<T>::insert( Q_TYPENAME QValueListPrivate<T>::Iterator it, const T& x )
00288 {
00289     NodePtr p = new Node( x );
00290     p->next = it.node;
00291     p->prev = it.node->prev;
00292     it.node->prev->next = p;
00293     it.node->prev = p;
00294     nodes++;
00295     return p;
00296 }
00297 
00298 template <class T>
00299 Q_INLINE_TEMPLATES Q_TYPENAME QValueListPrivate<T>::Iterator QValueListPrivate<T>::remove( Q_TYPENAME QValueListPrivate<T>::Iterator it )
00300 {
00301     Q_ASSERT ( it.node != node );
00302     NodePtr next = it.node->next;
00303     NodePtr prev = it.node->prev;
00304     prev->next = next;
00305     next->prev = prev;
00306     delete it.node;
00307     nodes--;
00308     return Iterator( next );
00309 }
00310 
00311 template <class T>
00312 Q_INLINE_TEMPLATES Q_TYPENAME QValueListPrivate<T>::NodePtr QValueListPrivate<T>::find( Q_TYPENAME QValueListPrivate<T>::NodePtr start, const T& x ) const
00313 {
00314     ConstIterator first( start );
00315     ConstIterator last( node );
00316     while( first != last) {
00317         if ( *first == x )
00318             return first.node;
00319         ++first;
00320     }
00321     return last.node;
00322 }
00323 
00324 template <class T>
00325 Q_INLINE_TEMPLATES int QValueListPrivate<T>::findIndex( Q_TYPENAME QValueListPrivate<T>::NodePtr start, const T& x ) const
00326 {
00327     ConstIterator first( start );
00328     ConstIterator last( node );
00329     int pos = 0;
00330     while( first != last) {
00331         if ( *first == x )
00332             return pos;
00333         ++first;
00334         ++pos;
00335     }
00336     return -1;
00337 }
00338 
00339 template <class T>
00340 Q_INLINE_TEMPLATES uint QValueListPrivate<T>::contains( const T& x ) const
00341 {
00342     uint result = 0;
00343     Iterator first = Iterator( node->next );
00344     Iterator last = Iterator( node );
00345     while( first != last) {
00346         if ( *first == x )
00347             ++result;
00348         ++first;
00349     }
00350     return result;
00351 }
00352 
00353 template <class T>
00354 Q_INLINE_TEMPLATES uint QValueListPrivate<T>::remove( const T& _x )
00355 {
00356     const T x = _x;
00357     uint result = 0;
00358     Iterator first = Iterator( node->next );
00359     Iterator last = Iterator( node );
00360     while( first != last) {
00361         if ( *first == x ) {
00362             first = remove( first );
00363             ++result;
00364         } else
00365             ++first;
00366     }
00367     return result;
00368 }
00369 
00370 template <class T>
00371 Q_INLINE_TEMPLATES Q_TYPENAME QValueListPrivate<T>::NodePtr QValueListPrivate<T>::at( size_type i ) const
00372 {
00373     Q_ASSERT( i <= nodes );
00374     NodePtr p = node->next;
00375     for( size_type x = 0; x < i; ++x )
00376         p = p->next;
00377     return p;
00378 }
00379 
00380 template <class T>
00381 Q_INLINE_TEMPLATES void QValueListPrivate<T>::clear()
00382 {
00383     nodes = 0;
00384     NodePtr p = node->next;
00385     while( p != node ) {
00386         NodePtr next = p->next;
00387         delete p;
00388         p = next;
00389     }
00390     node->next = node->prev = node;
00391 }
00392 
00393 #ifdef QT_CHECK_RANGE
00394 # if !defined( QT_NO_DEBUG ) && defined( QT_CHECK_VALUELIST_RANGE )
00395 #  define QT_CHECK_INVALID_LIST_ELEMENT if ( empty() ) qWarning( "QValueList: Warning invalid element" )
00396 #  define QT_CHECK_INVALID_LIST_ELEMENT_FATAL Q_ASSERT( !empty() );
00397 # else
00398 #  define QT_CHECK_INVALID_LIST_ELEMENT
00399 #  define QT_CHECK_INVALID_LIST_ELEMENT_FATAL
00400 # endif
00401 #else
00402 # define QT_CHECK_INVALID_LIST_ELEMENT
00403 # define QT_CHECK_INVALID_LIST_ELEMENT_FATAL
00404 #endif
00405 
00406 template <class T> class QDeepCopy;
00407 
00408 template <class T>
00409 class QValueList
00410 {
00411 public:
00415     typedef QValueListIterator<T> iterator;
00416     typedef QValueListConstIterator<T> const_iterator;
00417     typedef T value_type;
00418     typedef value_type* pointer;
00419     typedef const value_type* const_pointer;
00420     typedef value_type& reference;
00421     typedef const value_type& const_reference;
00422     typedef size_t size_type;
00423 #ifndef QT_NO_STL
00424     typedef ptrdiff_t  difference_type;
00425 #else
00426     typedef int difference_type;
00427 #endif
00428 
00432     QValueList() { sh = new QValueListPrivate<T>; }
00433     QValueList( const QValueList<T>& l ) { sh = l.sh; sh->ref(); }
00434 #ifndef QT_NO_STL
00435     QValueList( const std::list<T>& l )
00436     {
00437         sh = new QValueListPrivate<T>;
00438         qCopy( l.begin(), l.end(), std::back_inserter( *this ) );
00439     }
00440 #endif
00441     ~QValueList() { sh->derefAndDelete(); }
00442 
00443     QValueList<T>& operator= ( const QValueList<T>& l )
00444     {
00445         l.sh->ref();
00446         sh->derefAndDelete();
00447         sh = l.sh;
00448         return *this;
00449     }
00450 #ifndef QT_NO_STL
00451     QValueList<T>& operator= ( const std::list<T>& l )
00452     {
00453         detach();
00454         qCopy( l.begin(), l.end(), std::back_inserter( *this ) );
00455         return *this;
00456     }
00457     bool operator== ( const std::list<T>& l ) const
00458     {
00459         if ( size() != l.size() )
00460             return FALSE;
00461         const_iterator it2 = begin();
00462 #if !defined(Q_CC_MIPS)
00463         typename
00464 #endif
00465         std::list<T>::const_iterator it = l.begin();
00466         for ( ; it2 != end(); ++it2, ++it )
00467         if ( !((*it2) == (*it)) )
00468             return FALSE;
00469         return TRUE;
00470     }
00471 #endif
00472     bool operator== ( const QValueList<T>& l ) const;
00473     bool operator!= ( const QValueList<T>& l ) const { return !( *this == l ); }
00474     iterator begin() { detach(); return iterator( sh->node->next ); }
00475     const_iterator begin() const { return const_iterator( sh->node->next ); }
00476     const_iterator constBegin() const { return const_iterator( sh->node->next ); }
00477     iterator end() { detach(); return iterator( sh->node ); }
00478     const_iterator end() const { return const_iterator( sh->node ); }
00479     const_iterator constEnd() const { return const_iterator( sh->node ); }
00480     iterator insert( iterator it, const T& x ) { detach(); return sh->insert( it, x ); }
00481     uint remove( const T& x ) { detach(); return sh->remove( x ); }
00482     void clear();
00483 
00484     // ### 4.0: move out of class
00485     QValueList<T>& operator<< ( const T& x )
00486     {
00487         append( x );
00488         return *this;
00489     }
00490 
00491     size_type size() const { return sh->nodes; }
00492     bool empty() const { return sh->nodes == 0; }
00493     void push_front( const T& x ) { detach(); sh->insert( begin(), x ); }
00494     void push_back( const T& x ) { detach(); sh->insert( end(), x ); }
00495     iterator erase( iterator pos ) { detach(); return sh->remove( pos ); }
00496     iterator erase( iterator first, iterator last );
00497     reference front() { QT_CHECK_INVALID_LIST_ELEMENT_FATAL; return *begin(); }
00498     const_reference front() const { QT_CHECK_INVALID_LIST_ELEMENT_FATAL; return *begin(); }
00499     reference back() { QT_CHECK_INVALID_LIST_ELEMENT_FATAL; return *(--end()); }
00500     const_reference back() const { QT_CHECK_INVALID_LIST_ELEMENT_FATAL; return *(--end()); }
00501     void pop_front() { QT_CHECK_INVALID_LIST_ELEMENT; erase( begin() ); }
00502     void pop_back() {
00503         QT_CHECK_INVALID_LIST_ELEMENT;
00504         iterator tmp = end();
00505         erase( --tmp );
00506     }
00507     void insert( iterator pos, size_type n, const T& x );
00508     // Some compilers (incl. vc++) would instantiate this function even if
00509     // it is not used; this would constrain QValueList to classes that provide
00510     // an operator<
00511     /*
00512     void sort()
00513     {
00514         qHeapSort( *this );
00515     }
00516     */
00517 
00518     QValueList<T> operator+ ( const QValueList<T>& l ) const;
00519     QValueList<T>& operator+= ( const QValueList<T>& l );
00520 
00521     iterator fromLast() { detach(); return iterator( sh->node->prev ); }
00522     const_iterator fromLast() const { return const_iterator( sh->node->prev ); }
00523 
00524     bool isEmpty() const { return ( sh->nodes == 0 ); }
00525 
00526     iterator append( const T& x ) { detach(); return sh->insert( end(), x ); }
00527     iterator prepend( const T& x ) { detach(); return sh->insert( begin(), x ); }
00528 
00529     iterator remove( iterator it ) { detach(); return sh->remove( it ); }
00530 
00531     T& first() { QT_CHECK_INVALID_LIST_ELEMENT; detach(); return sh->node->next->data; }
00532     const T& first() const { QT_CHECK_INVALID_LIST_ELEMENT; return sh->node->next->data; }
00533     T& last() { QT_CHECK_INVALID_LIST_ELEMENT; detach(); return sh->node->prev->data; }
00534     const T& last() const { QT_CHECK_INVALID_LIST_ELEMENT; return sh->node->prev->data; }
00535 
00536     T& operator[] ( size_type i ) { QT_CHECK_INVALID_LIST_ELEMENT; detach(); return sh->at(i)->data; }
00537     const T& operator[] ( size_type i ) const { QT_CHECK_INVALID_LIST_ELEMENT; return sh->at(i)->data; }
00538     iterator at( size_type i ) { QT_CHECK_INVALID_LIST_ELEMENT; detach(); return iterator( sh->at(i) ); }
00539     const_iterator at( size_type i ) const { QT_CHECK_INVALID_LIST_ELEMENT; return const_iterator( sh->at(i) ); }
00540     iterator find ( const T& x ) { detach(); return iterator( sh->find( sh->node->next, x) ); }
00541     const_iterator find ( const T& x ) const { return const_iterator( sh->find( sh->node->next, x) ); }
00542     iterator find ( iterator it, const T& x ) { detach(); return iterator( sh->find( it.node, x ) ); }
00543     const_iterator find ( const_iterator it, const T& x ) const { return const_iterator( sh->find( it.node, x ) ); }
00544     int findIndex( const T& x ) const { return sh->findIndex( sh->node->next, x) ; }
00545     size_type contains( const T& x ) const { return sh->contains( x ); }
00546 
00547     size_type count() const { return sh->nodes; }
00548 
00549     QValueList<T>& operator+= ( const T& x )
00550     {
00551         append( x );
00552         return *this;
00553     }
00554     typedef QValueListIterator<T> Iterator;
00555     typedef QValueListConstIterator<T> ConstIterator;
00556     typedef T ValueType;
00557 
00558 protected:
00562     void detach() { if ( sh->count > 1 ) detachInternal(); }
00563 
00567     QValueListPrivate<T>* sh;
00568 
00569 private:
00570     void detachInternal();
00571 
00572     friend class QDeepCopy< QValueList<T> >;
00573 };
00574 
00575 template <class T>
00576 Q_INLINE_TEMPLATES bool QValueList<T>::operator== ( const QValueList<T>& l ) const
00577 {
00578     if ( size() != l.size() )
00579         return FALSE;
00580     const_iterator it2 = begin();
00581     const_iterator it = l.begin();
00582     for( ; it != l.end(); ++it, ++it2 )
00583         if ( !( *it == *it2 ) )
00584             return FALSE;
00585     return TRUE;
00586 }
00587 
00588 template <class T>
00589 Q_INLINE_TEMPLATES void QValueList<T>::clear()
00590 {
00591     if ( sh->count == 1 ) sh->clear(); else { sh->deref(); sh = new QValueListPrivate<T>; }
00592 }
00593 
00594 template <class T>
00595 Q_INLINE_TEMPLATES Q_TYPENAME QValueList<T>::iterator QValueList<T>::erase( Q_TYPENAME QValueList<T>::iterator first, Q_TYPENAME QValueList<T>::iterator last )
00596 {
00597     while ( first != last )
00598         erase( first++ );
00599     return last;
00600 }
00601 
00602 
00603 template <class T>
00604 Q_INLINE_TEMPLATES void QValueList<T>::insert( Q_TYPENAME QValueList<T>::iterator pos, size_type n, const T& x )
00605 {
00606     for ( ; n > 0; --n )
00607         insert( pos, x );
00608 }
00609 
00610 template <class T>
00611 Q_INLINE_TEMPLATES QValueList<T> QValueList<T>::operator+ ( const QValueList<T>& l ) const
00612 {
00613     QValueList<T> l2( *this );
00614     for( const_iterator it = l.begin(); it != l.end(); ++it )
00615         l2.append( *it );
00616     return l2;
00617 }
00618 
00619 template <class T>
00620 Q_INLINE_TEMPLATES QValueList<T>& QValueList<T>::operator+= ( const QValueList<T>& l )
00621 {
00622     QValueList<T> copy = l;
00623     for( const_iterator it = copy.begin(); it != copy.end(); ++it )
00624         append( *it );
00625     return *this;
00626 }
00627 
00628 template <class T>
00629 Q_INLINE_TEMPLATES void QValueList<T>::detachInternal()
00630 {
00631     sh->deref(); sh = new QValueListPrivate<T>( *sh );
00632 }
00633 
00634 #ifndef QT_NO_DATASTREAM
00635 template <class T>
00636 Q_INLINE_TEMPLATES QDataStream& operator>>( QDataStream& s, QValueList<T>& l )
00637 {
00638     l.clear();
00639     Q_UINT32 c;
00640     s >> c;
00641     for( Q_UINT32 i = 0; i < c; ++i )
00642     {
00643         T t;
00644         s >> t;
00645         l.append( t );
00646         if ( s.atEnd() )
00647             break;
00648     }
00649     return s;
00650 }
00651 
00652 template <class T>
00653 Q_INLINE_TEMPLATES QDataStream& operator<<( QDataStream& s, const QValueList<T>& l )
00654 {
00655     s << (Q_UINT32)l.size();
00656     QValueListConstIterator<T> it = l.begin();
00657     for( ; it != l.end(); ++it )
00658         s << *it;
00659     return s;
00660 }
00661 #endif // QT_NO_DATASTREAM
00662 
00663 #define Q_DEFINED_QVALUELIST
00664 #define Q_DEFINED_QVALUELIST
00665 #include "qwinexport.h"
00666 #endif // QVALUELIST_H
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Propiedades Amigas 'defines'