hashes.hh

00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2008 Ciaran McCreesh
00005  *
00006  * This file is part of the Paludis package manager. Paludis is free software;
00007  * you can redistribute it and/or modify it under the terms of the GNU General
00008  * Public License version 2, as published by the Free Software Foundation.
00009  *
00010  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
00011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00013  * details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_HASHES_HH
00021 #define PALUDIS_GUARD_PALUDIS_UTIL_HASHES_HH 1
00022 
00023 #include <paludis/util/attributes.hh>
00024 #include <paludis/util/validated-fwd.hh>
00025 #include <paludis/util/fs_entry-fwd.hh>
00026 #include <cstddef>
00027 #include <utility>
00028 #include <string>
00029 #include <tr1/type_traits>
00030 #include <tr1/memory>
00031 
00032 namespace paludis
00033 {
00034     namespace hash_internals
00035     {
00036         template <typename T_, bool is_numeric_>
00037         struct DefaultHash
00038         {
00039             static std::size_t hash(const T_ & t)
00040             {
00041                 return static_cast<std::size_t>(t);
00042             }
00043         };
00044 
00045         template <typename T_>
00046         struct DefaultHash<T_, false>
00047         {
00048             static std::size_t hash(const T_ & t)
00049             {
00050                 return t.hash();
00051             }
00052         };
00053     }
00054 
00055     template <typename T_>
00056     class Hash
00057     {
00058         public:
00059             std::size_t operator() (const T_ & t) const
00060             {
00061                 return hash_internals::DefaultHash<T_, std::tr1::is_integral<T_>::value>::hash(t);
00062             }
00063     };
00064 
00065     template <>
00066     struct PALUDIS_VISIBLE Hash<std::string>
00067     {
00068         std::size_t operator() (const std::string &) const PALUDIS_ATTRIBUTE((warn_unused_result));
00069     };
00070 
00071     template <>
00072     struct PALUDIS_VISIBLE Hash<FSEntry>
00073     {
00074         std::size_t operator() (const FSEntry &) const PALUDIS_ATTRIBUTE((warn_unused_result));
00075     };
00076 
00077     template <typename T_, typename U_>
00078     struct Hash<std::pair<T_, U_> >
00079     {
00080         std::size_t operator() (const std::pair<T_, U_> & p) const
00081         {
00082             return Hash<T_>()(p.first) ^ Hash<U_>()(p.second);
00083         }
00084     };
00085 
00086     template <typename D_, typename V_, bool b_, typename C_>
00087     struct Hash<Validated<D_, V_, b_, C_> >
00088     {
00089         std::size_t operator() (const Validated<D_, V_, b_, C_> & v) const
00090         {
00091             return Hash<D_>()(v.data());
00092         }
00093     };
00094 
00095     template <typename T_>
00096     struct Hash<const T_>
00097     {
00098         std::size_t operator() (const T_ & t) const
00099         {
00100             return Hash<T_>()(t);
00101         }
00102     };
00103 
00104     template <typename T_>
00105     struct Hash<T_ &>
00106     {
00107         std::size_t operator() (const T_ & t) const
00108         {
00109             return Hash<T_>()(t);
00110         }
00111     };
00112 
00113     template <typename T_>
00114     struct Hash<T_ *>
00115     {
00116         std::size_t operator() (const T_ * const t) const
00117         {
00118             return Hash<T_>(*t);
00119         }
00120     };
00121 
00122     template <typename T_>
00123     struct Hash<std::tr1::shared_ptr<T_> >
00124     {
00125         std::size_t operator() (const std::tr1::shared_ptr<const T_> & t) const
00126         {
00127             return Hash<T_>()(*t);
00128         }
00129     };
00130 }
00131 
00132 #endif

Generated on Mon Sep 21 10:36:08 2009 for paludis by  doxygen 1.5.4