OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 | 5 |
6 // | 6 // |
7 // Deal with the differences between Microsoft and GNU implemenations | 7 // Deal with the differences between Microsoft and GNU implemenations |
8 // of hash_map. Allows all platforms to use |base::hash_map| and | 8 // of hash_map. Allows all platforms to use |base::hash_map| and |
9 // |base::hash_set|. | 9 // |base::hash_set|. |
10 // eg: | 10 // eg: |
(...skipping 11 matching lines...) Expand all Loading... | |
22 #define BASE_HASH_TABLES_H_ | 22 #define BASE_HASH_TABLES_H_ |
23 #pragma once | 23 #pragma once |
24 | 24 |
25 #include "build/build_config.h" | 25 #include "build/build_config.h" |
26 | 26 |
27 #include "base/string16.h" | 27 #include "base/string16.h" |
28 | 28 |
29 #if defined(COMPILER_MSVC) | 29 #if defined(COMPILER_MSVC) |
30 #include <hash_map> | 30 #include <hash_map> |
31 #include <hash_set> | 31 #include <hash_set> |
32 | |
33 #define BASE_HASH_NAMESPACE stdext | |
34 | |
32 namespace base { | 35 namespace base { |
33 using stdext::hash_map; | 36 using BASE_HASH_NAMESPACE::hash_map; |
34 using stdext::hash_set; | 37 using BASE_HASH_NAMESPACE::hash_set; |
35 } | 38 } |
36 #elif defined(COMPILER_GCC) | 39 #elif defined(COMPILER_GCC) |
brettw
2011/06/24 23:57:04
CAn you add blank lines around this for readabilit
michaelbai
2011/06/27 16:14:09
Done.
| |
40 #if defined(OS_ANDROID) | |
41 #define BASE_HASH_NAMESPACE std | |
42 #else | |
43 #define BASE_HASH_NAMESPACE __gnu_cxx | |
44 #endif | |
45 | |
37 // This is a hack to disable the gcc 4.4 warning about hash_map and hash_set | 46 // This is a hack to disable the gcc 4.4 warning about hash_map and hash_set |
38 // being deprecated. We can get rid of this when we upgrade to VS2008 and we | 47 // being deprecated. We can get rid of this when we upgrade to VS2008 and we |
39 // can use <tr1/unordered_map> and <tr1/unordered_set>. | 48 // can use <tr1/unordered_map> and <tr1/unordered_set>. |
40 #ifdef __DEPRECATED | 49 #ifdef __DEPRECATED |
41 #define CHROME_OLD__DEPRECATED __DEPRECATED | 50 #define CHROME_OLD__DEPRECATED __DEPRECATED |
42 #undef __DEPRECATED | 51 #undef __DEPRECATED |
43 #endif | 52 #endif |
44 | 53 |
54 #if defined(OS_ANDROID) | |
55 #include <hash_map> | |
56 #include <hash_set> | |
57 #else | |
45 #include <ext/hash_map> | 58 #include <ext/hash_map> |
46 #include <ext/hash_set> | 59 #include <ext/hash_set> |
60 #endif | |
61 | |
47 #include <string> | 62 #include <string> |
48 | 63 |
49 #ifdef CHROME_OLD__DEPRECATED | 64 #ifdef CHROME_OLD__DEPRECATED |
50 #define __DEPRECATED CHROME_OLD__DEPRECATED | 65 #define __DEPRECATED CHROME_OLD__DEPRECATED |
51 #undef CHROME_OLD__DEPRECATED | 66 #undef CHROME_OLD__DEPRECATED |
52 #endif | 67 #endif |
53 | 68 |
54 namespace base { | 69 namespace base { |
darin (slow to review)
2011/06/24 23:41:12
i guess it would be nice to not repeat this code.
michaelbai
2011/06/24 23:45:35
I thought about it before, the whole structure in
brettw
2011/06/24 23:57:04
Do we support any other compilers? In these cases
michaelbai
2011/06/27 16:14:09
Done.
| |
55 using __gnu_cxx::hash_map; | 70 using BASE_HASH_NAMESPACE::hash_map; |
56 using __gnu_cxx::hash_set; | 71 using BASE_HASH_NAMESPACE::hash_set; |
57 } // namespace base | 72 } // namespace base |
58 | 73 |
59 namespace __gnu_cxx { | 74 namespace BASE_HASH_NAMESPACE { |
60 | 75 |
76 #if !defined(OS_ANDROID) | |
61 // The GNU C++ library provides identity hash functions for many integral types, | 77 // The GNU C++ library provides identity hash functions for many integral types, |
62 // but not for |long long|. This hash function will truncate if |size_t| is | 78 // but not for |long long|. This hash function will truncate if |size_t| is |
63 // narrower than |long long|. This is probably good enough for what we will | 79 // narrower than |long long|. This is probably good enough for what we will |
64 // use it for. | 80 // use it for. |
65 | 81 |
66 #define DEFINE_TRIVIAL_HASH(integral_type) \ | 82 #define DEFINE_TRIVIAL_HASH(integral_type) \ |
67 template<> \ | 83 template<> \ |
68 struct hash<integral_type> { \ | 84 struct hash<integral_type> { \ |
69 std::size_t operator()(integral_type value) const { \ | 85 std::size_t operator()(integral_type value) const { \ |
70 return static_cast<std::size_t>(value); \ | 86 return static_cast<std::size_t>(value); \ |
71 } \ | 87 } \ |
72 } | 88 } |
73 | 89 |
74 DEFINE_TRIVIAL_HASH(long long); | 90 DEFINE_TRIVIAL_HASH(long long); |
75 DEFINE_TRIVIAL_HASH(unsigned long long); | 91 DEFINE_TRIVIAL_HASH(unsigned long long); |
76 | 92 |
77 #undef DEFINE_TRIVIAL_HASH | 93 #undef DEFINE_TRIVIAL_HASH |
94 #endif // !defined(OS_ANDROID) | |
78 | 95 |
79 // Implement string hash functions so that strings of various flavors can | 96 // Implement string hash functions so that strings of various flavors can |
80 // be used as keys in STL maps and sets. The hash algorithm comes from the | 97 // be used as keys in STL maps and sets. The hash algorithm comes from the |
81 // GNU C++ library, in <tr1/functional>. It is duplicated here because GCC | 98 // GNU C++ library, in <tr1/functional>. It is duplicated here because GCC |
82 // versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI | 99 // versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI |
83 // is disabled, as it is in our build. | 100 // is disabled, as it is in our build. |
84 | 101 |
85 #define DEFINE_STRING_HASH(string_type) \ | 102 #define DEFINE_STRING_HASH(string_type) \ |
86 template<> \ | 103 template<> \ |
87 struct hash<string_type> { \ | 104 struct hash<string_type> { \ |
88 std::size_t operator()(const string_type& s) const { \ | 105 std::size_t operator()(const string_type& s) const { \ |
89 std::size_t result = 0; \ | 106 std::size_t result = 0; \ |
90 for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \ | 107 for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \ |
91 result = (result * 131) + *i; \ | 108 result = (result * 131) + *i; \ |
92 return result; \ | 109 return result; \ |
93 } \ | 110 } \ |
94 } | 111 } |
95 | 112 |
96 DEFINE_STRING_HASH(std::string); | 113 DEFINE_STRING_HASH(std::string); |
97 DEFINE_STRING_HASH(string16); | 114 DEFINE_STRING_HASH(string16); |
98 | 115 |
99 #undef DEFINE_STRING_HASH | 116 #undef DEFINE_STRING_HASH |
100 | 117 |
101 } // namespace __gnu_cxx | 118 } // namespace BASE_HASH_NAMESPACE |
102 | 119 |
103 #endif // COMPILER | 120 #endif // COMPILER |
104 | 121 |
105 #endif // BASE_HASH_TABLES_H_ | 122 #endif // BASE_HASH_TABLES_H_ |
OLD | NEW |