pion  5.0.6
hash_map.hpp
1 // ---------------------------------------------------------------------
2 // pion: a Boost C++ framework for building lightweight HTTP interfaces
3 // ---------------------------------------------------------------------
4 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #ifndef __PION_HASH_MAP_HEADER__
11 #define __PION_HASH_MAP_HEADER__
12 
13 #include <string>
14 #include <locale>
15 #include <boost/algorithm/string.hpp>
16 #include <boost/functional/hash.hpp>
17 #include <pion/config.hpp>
18 
19 #if defined(PION_HAVE_UNORDERED_MAP)
20  #include <unordered_map>
21 #elif defined(PION_HAVE_TR1_UNORDERED_MAP)
22  #include <tr1/unordered_map>
23 #elif defined(PION_HAVE_EXT_HASH_MAP)
24  #include <ext/hash_map>
25 #elif defined(PION_HAVE_HASH_MAP)
26  #include <hash_map>
27 #endif
28 
29 
30 namespace pion { // begin namespace pion
31 
32 #if defined(PION_HAVE_UNORDERED_MAP)
33  #define PION_HASH_MAP std::unordered_map
34  #define PION_HASH_MULTIMAP std::unordered_multimap
35  #define PION_HASH_STRING boost::hash<std::string>
36  #define PION_HASH(TYPE) boost::hash<TYPE>
37 #elif defined(PION_HAVE_TR1_UNORDERED_MAP)
38  #define PION_HASH_MAP std::tr1::unordered_map
39  #define PION_HASH_MULTIMAP std::tr1::unordered_multimap
40  #define PION_HASH_STRING boost::hash<std::string>
41  #define PION_HASH(TYPE) boost::hash<TYPE>
42 #elif defined(PION_HAVE_EXT_HASH_MAP)
43  #if __GNUC__ >= 3
44  #define PION_HASH_MAP __gnu_cxx::hash_map
45  #define PION_HASH_MULTIMAP __gnu_cxx::hash_multimap
46  #else
47  #define PION_HASH_MAP hash_map
48  #define PION_HASH_MULTIMAP hash_multimap
49  #endif
50  #define PION_HASH_STRING boost::hash<std::string>
51  #define PION_HASH(TYPE) boost::hash<TYPE>
52 #elif defined(PION_HAVE_HASH_MAP)
53  #ifdef _MSC_VER
54  #define PION_HASH_MAP stdext::hash_map
55  #define PION_HASH_MULTIMAP stdext::hash_multimap
56  #define PION_HASH_STRING stdext::hash_compare<std::string, std::less<std::string> >
57  #define PION_HASH(TYPE) stdext::hash_compare<TYPE, std::less<TYPE> >
58  #else
59  #define PION_HASH_MAP hash_map
60  #define PION_HASH_MULTIMAP hash_multimap
61  #define PION_HASH_STRING boost::hash<std::string>
62  #define PION_HASH(TYPE) boost::hash<TYPE>
63  #endif
64 #endif
65 
69  struct iequal_to
70  : std::binary_function<std::string, std::string, bool>
71  {
72  bool operator()(std::string const& x,
73  std::string const& y) const
74  {
75  return boost::algorithm::iequals(x, y, std::locale());
76  }
77  };
78 
82  struct ihash
83  : std::unary_function<std::string, std::size_t>
84  {
85  std::size_t operator()(std::string const& x) const
86  {
87  std::size_t seed = 0;
88  std::locale locale;
89 
90  for(std::string::const_iterator it = x.begin();
91  it != x.end(); ++it)
92  {
93  boost::hash_combine(seed, std::toupper(*it, locale));
94  }
95 
96  return seed;
97  }
98  };
99 
100 #if defined(_MSC_VER) && !defined(PION_HAVE_UNORDERED_MAP)
101  template<class _Ty> struct is_iless : public std::binary_function<_Ty, _Ty, bool>
103  {
105  is_iless( const std::locale& Loc=std::locale() ) : m_Loc( Loc ) {}
106 
108  bool operator()( const _Ty& Arg1, const _Ty& Arg2 ) const
109  {
110  return _Ty(boost::algorithm::to_upper_copy(Arg1, m_Loc)) < _Ty(boost::algorithm::to_upper_copy(Arg2, m_Loc));
111  }
112 
113  private:
114  std::locale m_Loc;
115  };
116 
118  struct ihash_windows : public stdext::hash_compare<std::string, is_iless<std::string> > {
119  // makes operator() with two arguments visible, otherwise it would be hidden by the operator() defined here
120  using stdext::hash_compare<std::string, is_iless<std::string> >::operator();
121 
122  inline size_t operator()(const std::string& str) const {
123  return ihash()(str);
124  }
125  };
126 
128  typedef PION_HASH_MULTIMAP<std::string, std::string, ihash_windows > ihash_multimap;
129 #else
130  typedef PION_HASH_MULTIMAP<std::string, std::string, ihash, iequal_to > ihash_multimap;
132 #endif
133 
134 
135 } // end namespace pion
136 
137 #endif