00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef QPTRDICT_H
00023 #define QPTRDICT_H
00024
00025 #include "qdictbase.h"
00026
00027 namespace FX {
00028
00038 template<class type> class QPtrDict : public QDictBase<FXuval, type>
00039 {
00040 typedef QDictBase<FXuval, type> Base;
00041 FXuval conv(void *v) const
00042 {
00043 return reinterpret_cast<FXuval>(v);
00044 }
00045 public:
00047 explicit QPtrDict(int size=13, bool wantAutoDel=false) : Base(size, wantAutoDel) { }
00048 ~QPtrDict() { Base::clear(); }
00049 FXADDMOVEBASECLASS(QPtrDict, Base)
00051 void insert(void *k, const type *d)
00052 {
00053 FXuval k_=conv(k);
00054 Base::insert((FXuint) k_, k_, const_cast<type *>(d));
00055 }
00057 void replace(void *k, const type *d)
00058 {
00059 FXuval k_=conv(k);
00060 Base::replace((FXuint) k_, k_, const_cast<type *>(d));
00061 }
00063 bool remove(void *k)
00064 {
00065 FXuval k_=conv(k);
00066 return Base::remove((FXuint) k_, k_);
00067 }
00069 type *take(void *k)
00070 {
00071 FXuval k_=conv(k);
00072 return Base::take((FXuint) k_, k_);
00073 }
00075 type *find(void *k) const
00076 {
00077 FXuval k_=conv(k);
00078 return Base::find((FXuint) k_, k_);
00079 }
00081 type *operator[](void *k) const { return find(k); }
00082 protected:
00083 virtual void deleteItem(type *d);
00084 };
00085
00086 template<class type> inline void QPtrDict<type>::deleteItem(type *d)
00087 {
00088 if(Base::autoDelete())
00089 {
00090
00091 delete d;
00092 }
00093 }
00094
00095 template<> inline void QPtrDict<void>::deleteItem(void *)
00096 {
00097 }
00098
00102 template<class type> class QPtrDictIterator : public QDictBaseIterator<FXuval, type>
00103 {
00104 public:
00105 QPtrDictIterator() { }
00106 QPtrDictIterator(const QPtrDict<type> &d) : QDictBaseIterator<FXuval, type>(d) { }
00107 };
00108
00110 template<class type> FXStream &operator<<(FXStream &s, const QPtrDict<type> &i)
00111 {
00112 FXuint mysize=i.count();
00113 s << mysize;
00114 for(QPtrDictIterator<type> it(i); it.current(); ++it)
00115 {
00116 s << (FXulong) it.currentKey();
00117 s << *it.current();
00118 }
00119 return s;
00120 }
00122 template<class type> FXStream &operator>>(FXStream &s, QPtrDict<type> &i)
00123 {
00124 FXuint mysize;
00125 s >> mysize;
00126 i.clear();
00127 FXulong key;
00128 for(FXuint n=0; n<mysize; n++)
00129 {
00130 type *item;
00131 FXERRHM(item=new type);
00132 s >> key;
00133 s >> *item;
00134 i.insert((void *)(FXuval) key, item);
00135 }
00136 return s;
00137 }
00138
00139 }
00140
00141 #endif