qptrdict.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                          Q P t r 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 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         //fxmessage("QDB delete %p\n", d);
00091         delete d;
00092     }
00093 }
00094 // Don't delete void *
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 } // namespace
00140 
00141 #endif

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