src/qt3support/tools/q3gdict.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 Qt3Support module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef Q3GDICT_H
       
    43 #define Q3GDICT_H
       
    44 
       
    45 #include <Qt3Support/q3ptrcollection.h>
       
    46 #include <QtCore/qstring.h>
       
    47 
       
    48 QT_BEGIN_HEADER
       
    49 
       
    50 QT_BEGIN_NAMESPACE
       
    51 
       
    52 QT_MODULE(Qt3SupportLight)
       
    53 
       
    54 class Q3GDictIterator;
       
    55 class Q3GDItList;
       
    56 
       
    57 
       
    58 class Q3BaseBucket				// internal dict node
       
    59 {
       
    60 public:
       
    61     Q3PtrCollection::Item	 getData()			{ return data; }
       
    62     Q3PtrCollection::Item	 setData( Q3PtrCollection::Item d ) { return data = d; }
       
    63     Q3BaseBucket		*getNext()			{ return next; }
       
    64     void		 setNext( Q3BaseBucket *n)	{ next = n; }
       
    65 protected:
       
    66     Q3BaseBucket( Q3PtrCollection::Item d, Q3BaseBucket *n ) : data(d), next(n) {}
       
    67     Q3PtrCollection::Item	 data;
       
    68     Q3BaseBucket		*next;
       
    69 };
       
    70 
       
    71 class Q3StringBucket : public Q3BaseBucket
       
    72 {
       
    73 public:
       
    74     Q3StringBucket( const QString &k, Q3PtrCollection::Item d, Q3BaseBucket *n )
       
    75 	: Q3BaseBucket(d,n), key(k)		{}
       
    76     const QString  &getKey() const		{ return key; }
       
    77 private:
       
    78     QString	    key;
       
    79 };
       
    80 
       
    81 class Q3AsciiBucket : public Q3BaseBucket
       
    82 {
       
    83 public:
       
    84     Q3AsciiBucket( const char *k, Q3PtrCollection::Item d, Q3BaseBucket *n )
       
    85 	: Q3BaseBucket(d,n), key(k) {}
       
    86     const char *getKey() const { return key; }
       
    87 private:
       
    88     const char *key;
       
    89 };
       
    90 
       
    91 class Q3IntBucket : public Q3BaseBucket
       
    92 {
       
    93 public:
       
    94     Q3IntBucket( long k, Q3PtrCollection::Item d, Q3BaseBucket *n )
       
    95 	: Q3BaseBucket(d,n), key(k) {}
       
    96     long  getKey() const { return key; }
       
    97 private:
       
    98     long  key;
       
    99 };
       
   100 
       
   101 class Q3PtrBucket : public Q3BaseBucket
       
   102 {
       
   103 public:
       
   104     Q3PtrBucket( void *k, Q3PtrCollection::Item d, Q3BaseBucket *n )
       
   105 	: Q3BaseBucket(d,n), key(k) {}
       
   106     void *getKey() const { return key; }
       
   107 private:
       
   108     void *key;
       
   109 };
       
   110 
       
   111 
       
   112 class Q_COMPAT_EXPORT Q3GDict : public Q3PtrCollection	// generic dictionary class
       
   113 {
       
   114 public:
       
   115     uint	count() const	{ return numItems; }
       
   116     uint	size()	const	{ return vlen; }
       
   117     Q3PtrCollection::Item look_string( const QString& key, Q3PtrCollection::Item,
       
   118 				   int );
       
   119     Q3PtrCollection::Item look_ascii( const char *key, Q3PtrCollection::Item, int );
       
   120     Q3PtrCollection::Item look_int( long key, Q3PtrCollection::Item, int );
       
   121     Q3PtrCollection::Item look_ptr( void *key, Q3PtrCollection::Item, int );
       
   122 #ifndef QT_NO_DATASTREAM
       
   123     QDataStream &read( QDataStream & );
       
   124     QDataStream &write( QDataStream & ) const;
       
   125 #endif
       
   126 protected:
       
   127     enum KeyType { StringKey, AsciiKey, IntKey, PtrKey };
       
   128 
       
   129     Q3GDict( uint len, KeyType kt, bool cs, bool ck );
       
   130     Q3GDict( const Q3GDict & );
       
   131    ~Q3GDict();
       
   132 
       
   133     Q3GDict     &operator=( const Q3GDict & );
       
   134 
       
   135     bool	remove_string( const QString &key, Q3PtrCollection::Item item=0 );
       
   136     bool	remove_ascii( const char *key, Q3PtrCollection::Item item=0 );
       
   137     bool	remove_int( long key, Q3PtrCollection::Item item=0 );
       
   138     bool	remove_ptr( void *key, Q3PtrCollection::Item item=0 );
       
   139     Q3PtrCollection::Item take_string( const QString &key );
       
   140     Q3PtrCollection::Item take_ascii( const char *key );
       
   141     Q3PtrCollection::Item take_int( long key );
       
   142     Q3PtrCollection::Item take_ptr( void *key );
       
   143 
       
   144     void	clear();
       
   145     void	resize( uint );
       
   146 
       
   147     int		hashKeyString( const QString & );
       
   148     int		hashKeyAscii( const char * );
       
   149 
       
   150     void	statistics() const;
       
   151 
       
   152 #ifndef QT_NO_DATASTREAM
       
   153     virtual QDataStream &read( QDataStream &, Q3PtrCollection::Item & );
       
   154     virtual QDataStream &write( QDataStream &, Q3PtrCollection::Item ) const;
       
   155 #endif
       
   156 private:
       
   157     Q3BaseBucket **vec;
       
   158     uint	vlen;
       
   159     uint	numItems;
       
   160     uint	keytype	: 2;
       
   161     uint	cases	: 1;
       
   162     uint	copyk	: 1;
       
   163     Q3GDItList  *iterators;
       
   164     void	   unlink_common( int, Q3BaseBucket *, Q3BaseBucket * );
       
   165     Q3StringBucket *unlink_string( const QString &,
       
   166 				  Q3PtrCollection::Item item = 0 );
       
   167     Q3AsciiBucket  *unlink_ascii( const char *, Q3PtrCollection::Item item = 0 );
       
   168     Q3IntBucket    *unlink_int( long, Q3PtrCollection::Item item = 0 );
       
   169     Q3PtrBucket    *unlink_ptr( void *, Q3PtrCollection::Item item = 0 );
       
   170     void	init( uint, KeyType, bool, bool );
       
   171     friend class Q3GDictIterator;
       
   172 };
       
   173 
       
   174 
       
   175 class Q_COMPAT_EXPORT Q3GDictIterator			// generic dictionary iterator
       
   176 {
       
   177 friend class Q3GDict;
       
   178 public:
       
   179     Q3GDictIterator( const Q3GDict & );
       
   180     Q3GDictIterator( const Q3GDictIterator & );
       
   181     Q3GDictIterator &operator=( const Q3GDictIterator & );
       
   182    ~Q3GDictIterator();
       
   183 
       
   184     Q3PtrCollection::Item toFirst();
       
   185 
       
   186     Q3PtrCollection::Item get()	     const;
       
   187     QString	      getKeyString() const;
       
   188     const char	     *getKeyAscii()  const;
       
   189     long	      getKeyInt()    const;
       
   190     void	     *getKeyPtr()    const;
       
   191 
       
   192     Q3PtrCollection::Item operator()();
       
   193     Q3PtrCollection::Item operator++();
       
   194     Q3PtrCollection::Item operator+=(uint);
       
   195 
       
   196 protected:
       
   197     Q3GDict	     *dict;
       
   198 
       
   199 private:
       
   200     Q3BaseBucket      *curNode;
       
   201     uint	      curIndex;
       
   202 };
       
   203 
       
   204 inline Q3PtrCollection::Item Q3GDictIterator::get() const
       
   205 {
       
   206     return curNode ? curNode->getData() : 0;
       
   207 }
       
   208 
       
   209 inline QString Q3GDictIterator::getKeyString() const
       
   210 {
       
   211     return curNode ? ((Q3StringBucket*)curNode)->getKey() : QString();
       
   212 }
       
   213 
       
   214 inline const char *Q3GDictIterator::getKeyAscii() const
       
   215 {
       
   216     return curNode ? ((Q3AsciiBucket*)curNode)->getKey() : 0;
       
   217 }
       
   218 
       
   219 inline long Q3GDictIterator::getKeyInt() const
       
   220 {
       
   221     return curNode ? ((Q3IntBucket*)curNode)->getKey() : 0;
       
   222 }
       
   223 
       
   224 inline void *Q3GDictIterator::getKeyPtr() const
       
   225 {
       
   226     return curNode ? ((Q3PtrBucket*)curNode)->getKey() : 0;
       
   227 }
       
   228 
       
   229 QT_END_NAMESPACE
       
   230 
       
   231 QT_END_HEADER
       
   232 
       
   233 #endif // Q3GDICT_H