QHostAddress.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                             IP address container                              *
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 
00023 #ifndef QHOSTADDRESS_H
00024 #define QHOSTADDRESS_H
00025 
00026 #include "FXMaths.h"
00027 #include "qdictbase.h"
00028 
00029 namespace FX {
00030 
00035 class FXString;
00036 
00038 #define QHOSTADDRESS_LOCALHOST QHostAddress(0x7f000001)
00039 
00063 struct QHostAddressPrivate;
00064 class FXAPIR QHostAddress
00065 {
00066     QHostAddressPrivate *p;
00067 public:
00069     QHostAddress();
00071     QHostAddress(FXuint ip4addr);
00073     QHostAddress(const Maths::Vector<FXuchar, 16> &ip6addr);
00075     QHostAddress(const FXuchar *ip6addr);
00076     QHostAddress(const QHostAddress &o);
00077     QHostAddress &operator=(const QHostAddress &o);
00078     ~QHostAddress();
00080     bool operator!() const { return isNull(); }
00082     bool operator==(const QHostAddress &o) const;
00084     bool operator!=(const QHostAddress &o) const { return !(*this==o); }
00086     bool operator<(const QHostAddress &o) const;
00088     bool operator>(const QHostAddress &o) const;
00090     bool operator<=(const QHostAddress &o) const { return !(*this>o); }
00092     bool operator>=(const QHostAddress &o) const { return !(*this<o); }
00094     QHostAddress operator&(const QHostAddress &o) const;
00096     QHostAddress operator|(const QHostAddress &o) const;
00098     QHostAddress operator^(const QHostAddress &o) const;
00099 
00101     void setAddress(FXuint ip4addr);
00103     void setAddress(const Maths::Vector<FXuchar, 16> &ip6addr);
00105     void setAddress(const FXuchar *ip6addr);
00111     bool setAddress(const FXString &addr);
00113     bool isNull() const;
00115     bool isIp4Addr() const;
00119     FXuint ip4Addr() const;
00121     bool isIp6Addr() const;
00125     const Maths::Vector<FXuchar, 16> &ip6Addr() const;
00129     const FXuchar *ip6AddrData() const;
00134     FXString toString() const;
00136     bool isLocalMachine() const;
00137 
00139     friend FXAPIR FXStream &operator<<(FXStream &s, const QHostAddress &i);
00141     friend FXAPIR FXStream &operator>>(FXStream &s, QHostAddress &i);
00142 };
00143 
00144 template<class type> class QHostAddressDict : public QDictBase<QHostAddress, type>
00145 {
00146     typedef QDictBase<QHostAddress, type> Base;
00147     FXuint hash(const QHostAddress &a) const
00148     {
00149         if(a.isIp4Addr()) return a.ip4Addr();
00150         const FXuchar *txt=a.ip6AddrData();
00151         const FXuchar *end=txt+16;
00152         FXuint h=0;
00153         for(; end>=txt; end--)
00154             h=(h & 0xffffff80)^(h<<8)^(*end);
00155         return h;
00156     }
00157 public:
00158     enum { HasSlowKeyCompare=true };
00160     explicit QHostAddressDict(int size=13, bool wantAutoDel=false)
00161         : Base(size, wantAutoDel)
00162     {
00163     }
00164     QHostAddressDict(const QHostAddressDict<type> &o) : Base(o) { }
00165     ~QHostAddressDict() { Base::clear(); }
00166     FXADDMOVEBASECLASS(QHostAddressDict, Base)
00168     void insert(const QHostAddress &k, const type *d)
00169     {
00170         Base::insert(hash(k), k, const_cast<type *>(d));
00171     }
00173     void replace(const QHostAddress &k, const type *d)
00174     {
00175         Base::replace(hash(k), k, const_cast<type *>(d));
00176     }
00178     bool remove(const QHostAddress &k)
00179     {
00180         return Base::remove(hash(k), k);
00181     }
00183     type *take(const QHostAddress &k)
00184     {
00185         return Base::take(hash(k), k);
00186     }
00188     type *find(const QHostAddress &k) const
00189     {
00190         return Base::find(hash(k), k);
00191     }
00193     type *operator[](const QHostAddress &k) const { return find(k); }
00194 protected:
00195     virtual void deleteItem(type *d);
00196 };
00197 
00198 template<class type> inline void QHostAddressDict<type>::deleteItem(type *d)
00199 {
00200     if(Base::autoDelete())
00201     {
00202         //fxmessage("QDB delete %p\n", d);
00203         delete d;
00204     }
00205 }
00206 // Don't delete void *
00207 template<> inline void QHostAddressDict<void>::deleteItem(void *)
00208 {
00209 }
00210 
00215 template<class type> class QHostAddressDictIterator : public QDictBaseIterator<QHostAddress, type>
00216 {
00217 public:
00218     QHostAddressDictIterator() { }
00219     QHostAddressDictIterator(const QHostAddressDict<type> &d) : QDictBaseIterator<QHostAddress, type>(d) { }
00220 };
00221 
00223 template<class type> FXStream &operator<<(FXStream &s, const QHostAddressDict<type> &i)
00224 {
00225     FXuint mysize=i.count();
00226     s << mysize;
00227     for(QHostAddressDictIterator<type> it(i); it.current(); ++it)
00228     {
00229         s << it.currentKey();
00230         s << *it.current();
00231     }
00232     return s;
00233 }
00235 template<class type> FXStream &operator>>(FXStream &s, QHostAddressDict<type> &i)
00236 {
00237     FXuint mysize;
00238     s >> mysize;
00239     i.clear();
00240     QHostAddress key;
00241     for(FXuint n=0; n<mysize; n++)
00242     {
00243         type *item;
00244         FXERRHM(item=new type);
00245         s >> key;
00246         s >> *item;
00247         i.insert(key, item);
00248     }
00249     return s;
00250 }
00251 
00252 } // namespace
00253 
00254 #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