Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(307)

Side by Side Diff: base/hash_tables.h

Issue 165330: Disable RTTI and C++ exceptions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/file_path.h ('k') | breakpad/breakpad.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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:
11 // base::hash_map<int> my_map; 11 // base::hash_map<int> my_map;
12 // base::hash_set<int> my_set; 12 // base::hash_set<int> my_set;
13 // 13 //
14 14
15 #ifndef BASE_HASH_TABLES_H__ 15 #ifndef BASE_HASH_TABLES_H_
16 #define BASE_HASH_TABLES_H__ 16 #define BASE_HASH_TABLES_H_
17 17
18 #include "build/build_config.h" 18 #include "build/build_config.h"
19 19
20 #include "base/string16.h" 20 #include "base/string16.h"
21 21
22 #if defined(COMPILER_MSVC) 22 #if defined(COMPILER_MSVC)
23 #include <hash_map> 23 #include <hash_map>
24 #include <hash_set> 24 #include <hash_set>
25 namespace base { 25 namespace base {
26 using stdext::hash_map; 26 using stdext::hash_map;
27 using stdext::hash_set; 27 using stdext::hash_set;
28 } 28 }
29 #elif defined(COMPILER_GCC) 29 #elif defined(COMPILER_GCC)
30 // This is a hack to disable the gcc 4.4 warning about hash_map and hash_set 30 // This is a hack to disable the gcc 4.4 warning about hash_map and hash_set
31 // being deprecated. We can get rid of this when we upgrade to VS2008 and we 31 // being deprecated. We can get rid of this when we upgrade to VS2008 and we
32 // can use <tr1/unordered_map> and <tr1/unordered_set>. 32 // can use <tr1/unordered_map> and <tr1/unordered_set>.
33 #ifdef __DEPRECATED 33 #ifdef __DEPRECATED
34 #define CHROME_OLD__DEPRECATED __DEPRECATED 34 #define CHROME_OLD__DEPRECATED __DEPRECATED
35 #undef __DEPRECATED 35 #undef __DEPRECATED
36 #endif 36 #endif
37 37
38 #include <ext/hash_map> 38 #include <ext/hash_map>
39 #include <ext/hash_set> 39 #include <ext/hash_set>
40 #include <string>
40 41
41 #ifdef CHROME_OLD__DEPRECATED 42 #ifdef CHROME_OLD__DEPRECATED
42 #define __DEPRECATED CHROME_OLD__DEPRECATED 43 #define __DEPRECATED CHROME_OLD__DEPRECATED
43 #undef CHROME_OLD__DEPRECATED 44 #undef CHROME_OLD__DEPRECATED
44 #endif 45 #endif
45 46
46 #include <tr1/functional>
47 namespace base { 47 namespace base {
48 using __gnu_cxx::hash_map; 48 using __gnu_cxx::hash_map;
49 using __gnu_cxx::hash_set; 49 using __gnu_cxx::hash_set;
50 } 50 } // namespace base
51
52 namespace __gnu_cxx {
53
54 // The GNU C++ library provides identiy hash functions for many integral types,
55 // but not for |long long|. This hash function will truncate if |size_t| is
56 // narrower than |long long|. This is probably good enough for what we will
57 // use it for.
58
59 #define DEFINE_TRIVIAL_HASH(integral_type) \
60 template<> \
61 struct hash<integral_type> { \
62 std::size_t operator()(integral_type value) const { \
63 return static_cast<std::size_t>(value); \
64 } \
65 }
66
67 DEFINE_TRIVIAL_HASH(long long);
68 DEFINE_TRIVIAL_HASH(unsigned long long);
69
70 #undef DEFINE_TRIVIAL_HASH
51 71
52 // Implement string hash functions so that strings of various flavors can 72 // Implement string hash functions so that strings of various flavors can
53 // be used as keys in STL maps and sets. 73 // be used as keys in STL maps and sets. The hash algorithm comes from the
54 namespace __gnu_cxx { 74 // GNU C++ library, in <tr1/functional>. It is duplicated here because GCC
75 // versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI
76 // is disabled, as it is in our build.
55 77
56 template<> 78 #define DEFINE_STRING_HASH(string_type) \
57 struct hash<wchar_t*> { 79 template<> \
58 size_t operator()(const wchar_t* s) const { 80 struct hash<string_type> { \
59 return std::tr1::hash<const wchar_t*>()(s); 81 std::size_t operator()(const string_type& s) const { \
60 } 82 std::size_t result = 0; \
61 }; 83 for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \
84 result = (result * 131) + *i; \
85 return result; \
86 } \
87 }
62 88
63 template<> 89 DEFINE_STRING_HASH(std::string);
64 struct hash<const wchar_t*> { 90 DEFINE_STRING_HASH(std::wstring);
65 size_t operator()(const wchar_t* s) const {
66 return std::tr1::hash<const wchar_t*>()(s);
67 }
68 };
69
70 template<>
71 struct hash<std::wstring> {
72 size_t operator()(const std::wstring& s) const {
73 return std::tr1::hash<std::wstring>()(s);
74 }
75 };
76
77 template<>
78 struct hash<const std::wstring> {
79 size_t operator()(const std::wstring& s) const {
80 return std::tr1::hash<std::wstring>()(s);
81 }
82 };
83
84 template<>
85 struct hash<std::string> {
86 size_t operator()(const std::string& s) const {
87 return std::tr1::hash<std::string>()(s);
88 }
89 };
90
91 template<>
92 struct hash<const std::string> {
93 size_t operator()(const std::string& s) const {
94 return std::tr1::hash<std::string>()(s);
95 }
96 };
97
98 template<>
99 struct hash<long long> {
100 size_t operator()(long long i) const {
101 return std::tr1::hash<long>()((long) i);
102 }
103 };
104 91
105 #if defined(WCHAR_T_IS_UTF32) 92 #if defined(WCHAR_T_IS_UTF32)
106 template<> 93 // If string16 and std::wstring are not the same thing, provide a
107 struct hash<string16> { 94 // specialization for string16.
108 size_t operator()(const string16& s) const { 95 DEFINE_STRING_HASH(string16);
109 // This comes from GNU libstdc++, but the types have been changed to 96 #endif // WCHAR_T_IS_UTF32
110 // make it compile. The lib only defines the hash for string and wstring.
111 std::size_t result = 0;
112 for (string16::const_iterator i = s.begin(); i != s.end(); ++i)
113 result = (result * 131) + *i;
114 return result;
115 }
116 };
117 97
118 template<> 98 #undef DEFINE_STRING_HASH
119 struct hash<const string16> {
120 size_t operator()(const string16& s) const {
121 // This comes from GNU libstdc++, but the types have been changed to
122 // make it compile. The lib only defines the hash for string and wstring.
123 std::size_t result = 0;
124 for (string16::const_iterator i = s.begin(); i != s.end(); ++i)
125 result = (result * 131) + *i;
126 return result;
127 }
128 };
129 #endif
130 99
131 } 100 } // namespace __gnu_cxx
132 101
133 #endif 102 #endif // COMPILER
134 103
135 #endif // BASE_HASH_TABLES_H__ 104 #endif // BASE_HASH_TABLES_H_
OLDNEW
« no previous file with comments | « base/file_path.h ('k') | breakpad/breakpad.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698