qintdict.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                          Q I n t D i c t   T h u n k                          *
00004 *                                                                               *
00005 *********************************************************************************
00006 *        Copyright (C) 2003 by Niall Douglas.   All Rights Reserved.            *
00007 *       NOTE THAT I DO NOT PERMIT ANY OF MY CODE TO BE PROMOTED TO THE GPL      *
00008 *********************************************************************************
00009 * This code is free software; you can redistribute it and/or modify it under    *
00010 * the terms of the GNU Library General Public License v2.1 as published by the  *
00011 * Free Software Foundation EXCEPT that clause 3 does not apply ie; you may not  *
00012 * "upgrade" this code to the GPL without my prior written permission.           *
00013 * Please consult the file "License_Addendum2.txt" accompanying this file.       *
00014 *                                                                               *
00015 * This code is distributed in the hope that it will be useful,                  *
00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                          *
00018 *********************************************************************************
00019 * $Id:                                                                          *
00020 ********************************************************************************/
00021 
00022 #ifndef QINTDICT_H
00023 #define QINTDICT_H
00024 
00025 #include "qdictbase.h"
00026 
00027 namespace FX {
00028 
00039 template<class type> class QIntDict : public QDictBase<FXint, type>
00040 {
00041 public:
00043     explicit QIntDict(int size=13, bool wantAutoDel=false) : QDictBase<FXint, type>(size, wantAutoDel) { }
00044     ~QIntDict() { QDictBase<FXint, type>::clear(); }
00046     void insert(FXint k, const type *d)
00047     {
00048         QDictBase<FXint, type>::insert(k, k, const_cast<type *>(d));
00049     }
00051     void replace(FXint k, const type *d)
00052     {
00053         QDictBase<FXint, type>::replace(k, k, const_cast<type *>(d));
00054     }
00056     bool remove(FXint k)
00057     {
00058         return QDictBase<FXint, type>::remove(k, k);
00059     }
00061     type *take(FXint k)
00062     {
00063         return QDictBase<FXint, type>::take(k, k);
00064     }
00066     type *find(FXint k) const
00067     {
00068         return QDictBase<FXint, type>::find(k, k);
00069     }
00071     type *operator[](FXint k) const { return find(k); }
00072 protected:
00073     virtual void deleteItem(type *d);
00074 };
00075 
00076 template<class type> inline void QIntDict<type>::deleteItem(type *d)
00077 {
00078     if(QDictBase<FXint, type>::autoDelete())
00079     {
00080         //fxmessage("QDB delete %p\n", d);
00081         delete d;
00082     }
00083 }
00084 // Don't delete void *
00085 template<> inline void QIntDict<void>::deleteItem(void *)
00086 {
00087 }
00088 
00093 template<class type> class QIntDictIterator : public QDictBaseIterator<FXint, type>
00094 {
00095 public:
00096     QIntDictIterator() { }
00097     QIntDictIterator(const QIntDict<type> &d) : QDictBaseIterator<FXint, type>(d) { }
00098 };
00099 
00101 template<class type> FXStream &operator<<(FXStream &s, const QIntDict<type> &i)
00102 {
00103     FXuint mysize=i.count();
00104     s << mysize;
00105     for(QIntDictIterator<type> it(i); it.current(); ++it)
00106     {
00107         s << (FXlong) it.currentKey();
00108         s << *it.current();
00109     }
00110     return s;
00111 }
00113 template<class type> FXStream &operator>>(FXStream &s, QIntDict<type> &i)
00114 {
00115     FXuint mysize;
00116     s >> mysize;
00117     i.clear();
00118     FXlong key;
00119     for(FXuint n=0; n<mysize; n++)
00120     {
00121         type *item;
00122         FXERRHM(item=new type);
00123         s >> key;
00124         s >> *item;
00125         i.insert((FXint) key, item);
00126     }
00127     return s;
00128 }
00129 
00130 } // namespace
00131 
00132 #endif

(C) 2002-2009 Niall Douglas. Some parts (C) to assorted authors.
Generated on Fri Nov 20 18:31:25 2009 for TnFOX by doxygen v1.4.7