include/wipal/wifi/frame/unique_id/unique_id.hxx

00001 /*
00002  * WiPal - A library and a set of tools to manipulate wireless traces.
00003  * Copyright (C) 2007  Universite Pierre et Marie Curie - Paris 6
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018  * MA  02110-1301  USA
00019  *
00020  * Author: Thomas Claveirole <thomas.claveirole@lip6.fr>
00021  */
00022 #ifndef WIFI_FRAME_UNIQUE_ID_HXX_
00023 # define WIFI_FRAME_UNIQUE_ID_HXX_
00024 
00025 # include "unique_id.hh"
00026 
00027 # include <wipal/phy/prism_header.hh>
00028 
00029 namespace wifi
00030 {
00031 
00032   namespace frame
00033   {
00034 
00035     namespace internals
00036     {
00037 
00038       template <class Tag>
00039       bool
00040       empty<Tag>::operator < (const empty&) const
00041       {
00042         return false;
00043       }
00044 
00045       template <class Tag>
00046       bool
00047       empty<Tag>::operator == (const empty&) const
00048       {
00049         return true;
00050       }
00051 
00052       template <class Tag>
00053       bool
00054       empty<Tag>::operator != (const empty&) const
00055       {
00056         return false;
00057       }
00058 
00059       template <class T, class Tag>
00060       adapter<T, Tag>::adapter(const T& v): v_ (v)
00061       {
00062       }
00063 
00064       template <class T, class Tag>
00065       bool
00066       adapter<T, Tag>::operator < (const adapter& rhs) const
00067       {
00068         return v_ < rhs.v_;
00069       }
00070 
00071       template <class T, class Tag>
00072       bool
00073       adapter<T, Tag>::operator == (const adapter& rhs) const
00074       {
00075         return v_ == rhs.v_;
00076       }
00077 
00078       template <class T, class Tag>
00079       bool
00080       adapter<T, Tag>::operator != (const adapter& rhs) const
00081       {
00082         return not (*this == rhs);
00083       }
00084 
00085       template <class T, class Tag>
00086       const T&
00087       adapter<T, Tag>::value() const
00088       {
00089         return v_;
00090       }
00091 
00092       template <class T, class Tag>
00093       T&
00094       adapter<T, Tag>::value()
00095       {
00096         return v_;
00097       }
00098 
00099     } // End of namespace wifi::frame::internals.
00100 
00101     /*----------------------.
00102     | uniquely_identifiable |
00103     `----------------------*/
00104 
00105     template <class Id1, class Id2, class Id3, class Id4>
00106     unique_id<Id1, Id2, Id3, Id4>::unique_id(unsigned   frame_id,
00107                                              const Id1& id1,
00108                                              const Id2& id2,
00109                                              const Id3& id3,
00110                                              const Id4& id4):
00111       Id1 (id1),
00112       Id2 (id2),
00113       Id3 (id3),
00114       Id4 (id4),
00115       frame_id_ (frame_id)
00116     {
00117     }
00118 
00119     template <class Id1, class Id2, class Id3, class Id4>
00120     bool
00121     unique_id<Id1, Id2, Id3, Id4>::operator < (const unique_id& rhs) const
00122     {
00123       const Id1&   l1 = id1(),   r1 = rhs.id1();
00124       const Id2&   l2 = id2(),   r2 = rhs.id2();
00125       const Id3&   l3 = id3(),   r3 = rhs.id3();
00126       const Id4&   l4 = id4(),   r4 = rhs.id4();
00127 
00128       if (l1 != r1)
00129         return l1 < r1;
00130       if (l2 != r2)
00131         return l2 < r2;
00132       if (l3 != r3)
00133         return l3 < r3;
00134       return l4 < r4;
00135     }
00136 
00137     template <class Id1, class Id2, class Id3, class Id4>
00138     bool
00139     unique_id<Id1, Id2, Id3, Id4>::operator == (const unique_id& rhs) const
00140     {
00141       return (id1() == rhs.id1() and id2() == rhs.id2() and
00142               id3() == rhs.id3() and id4() == rhs.id4());
00143     }
00144 
00145     template <class Id1, class Id2, class Id3, class Id4>
00146     bool
00147     unique_id<Id1, Id2, Id3, Id4>::operator != (const unique_id& rhs) const
00148     {
00149       return not (*this == rhs);
00150     }
00151 
00152     template <class Id1, class Id2, class Id3, class Id4>
00153     std::ostream&
00154     unique_id<Id1, Id2, Id3, Id4>::print(std::ostream& os) const
00155     {
00156       return os << frame_id_;
00157     }
00158 
00159     template <class Id1, class Id2, class Id3, class Id4>
00160     unsigned
00161     unique_id<Id1, Id2, Id3, Id4>::frame_id() const
00162     {
00163       return frame_id_;
00164     }
00165 
00166     template <class Id1, class Id2, class Id3, class Id4>
00167     const Id1&
00168     unique_id<Id1, Id2, Id3, Id4>::id1() const
00169     {
00170       return static_cast<const Id1&> (*this);
00171     }
00172 
00173     template <class Id1, class Id2, class Id3, class Id4>
00174     const Id2&
00175     unique_id<Id1, Id2, Id3, Id4>::id2() const
00176     {
00177       return static_cast<const Id2&> (*this);
00178     }
00179 
00180     template <class Id1, class Id2, class Id3, class Id4>
00181     const Id3&
00182     unique_id<Id1, Id2, Id3, Id4>::id3() const
00183     {
00184       return static_cast<const Id3&> (*this);
00185     }
00186 
00187     template <class Id1, class Id2, class Id3, class Id4>
00188     const Id4&
00189     unique_id<Id1, Id2, Id3, Id4>::id4() const
00190     {
00191       return static_cast<const Id4&> (*this);
00192     }
00193 
00194     template <class Id1, class Id2, class Id3, class Id4>
00195     std::ostream&
00196     operator << (std::ostream& os, const unique_id<Id1, Id2, Id3, Id4>& ui)
00197     {
00198       return ui.print(os);
00199     }
00200 
00201     namespace internals
00202     {
00203 
00204       inline
00205       bool
00206       make_unique_id_handle_prism(const void*&                    frame,
00207                                   size_t&                         caplen,
00208                                   const pcapxx::frame_descriptor& desc)
00209       {
00210         const pcapxx::pkthdr*           pcap = desc.pcap_header().get();
00211         const prism::header* const      prism =
00212           reinterpret_cast<const prism::header*> (desc.bytes().get());
00213 
00214         if (pcap->caplen <= sizeof (prism::header))
00215           return false;
00216 
00217         frame = prism + 1;
00218         caplen = pcap->caplen - sizeof (*prism);
00219         return true;
00220       }
00221 
00222     } // End of namespace wifi::frame::internals.
00223 
00224 
00225   } // End of namespace wifi::frame.
00226 
00227 } // End of namespace wifi.
00228 
00229 HASH_NAMESPACE_BEGIN
00230 
00231   template <class Tag>
00232   size_t
00233   hash< wifi::frame::internals::empty<Tag> >::
00234   operator () (const wifi::frame::internals::empty<Tag>&) const
00235   {
00236     return 0;
00237   }
00238 
00239   template <class T, class Tag>
00240   size_t
00241   hash< wifi::frame::internals::adapter<T, Tag> >::
00242   operator () (const wifi::frame::internals::adapter<T, Tag>& a) const
00243   {
00244     return hash<T>::operator () (a.value());
00245   }
00246 
00247   template <class Id1, class Id2, class Id3, class Id4>
00248   size_t
00249   hash< wifi::frame::unique_id<Id1, Id2, Id3, Id4> >::
00250   operator () (const wifi::frame::unique_id<Id1, Id2, Id3, Id4>& i) const
00251   {
00252     return (hash<Id1>::operator () (i.id1()) ^
00253             hash<Id2>::operator () (i.id2()) ^
00254             hash<Id3>::operator () (i.id3()) ^
00255             hash<Id4>::operator () (i.id4()));
00256   }
00257 
00258 HASH_NAMESPACE_END
00259 
00260 #endif // ! WIFI_FRAME_UNIQUE_ID_HXX_

Generated on Tue Jan 15 19:32:31 2008 for wipal by  doxygen 1.5.4