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

Side by Side Diff: third_party/WebKit/Source/wtf/HashTraits.h

Issue 2687923002: WTF: Support enums in DefaultHash and HashTraits by casting to integral types. (Closed)
Patch Set: . Created 3 years, 10 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
« no previous file with comments | « third_party/WebKit/Source/wtf/HashFunctions.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All rights 2 * Copyright (C) 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All rights
3 * reserved. 3 * reserved.
4 * 4 *
5 * This library is free software; you can redistribute it and/or 5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public 6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either 7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version. 8 * version 2 of the License, or (at your option) any later version.
9 * 9 *
10 * This library is distributed in the hope that it will be useful, 10 * This library is distributed in the hope that it will be useful,
(...skipping 17 matching lines...) Expand all
28 #include "wtf/StdLibExtras.h" 28 #include "wtf/StdLibExtras.h"
29 #include "wtf/TypeTraits.h" 29 #include "wtf/TypeTraits.h"
30 #include <limits> 30 #include <limits>
31 #include <memory> 31 #include <memory>
32 #include <string.h> // For memset. 32 #include <string.h> // For memset.
33 #include <type_traits> 33 #include <type_traits>
34 #include <utility> 34 #include <utility>
35 35
36 namespace WTF { 36 namespace WTF {
37 37
38 template <bool isInteger, typename T> 38 template <bool isInteger, bool isEnum, typename T>
39 struct GenericHashTraitsBase; 39 struct GenericHashTraitsBase;
40 template <typename T> 40 template <typename T>
41 struct HashTraits; 41 struct HashTraits;
42 42
43 enum ShouldWeakPointersBeMarkedStrongly { 43 enum ShouldWeakPointersBeMarkedStrongly {
44 WeakPointersActStrong, 44 WeakPointersActStrong,
45 WeakPointersActWeak 45 WeakPointersActWeak
46 }; 46 };
47 47
48 template <typename T> 48 template <typename T>
49 struct GenericHashTraitsBase<false, T> { 49 struct GenericHashTraitsBase<false, false, T> {
50 // The emptyValueIsZero flag is used to optimize allocation of empty hash 50 // The emptyValueIsZero flag is used to optimize allocation of empty hash
51 // tables with zeroed memory. 51 // tables with zeroed memory.
52 static const bool emptyValueIsZero = false; 52 static const bool emptyValueIsZero = false;
53 53
54 // The hasIsEmptyValueFunction flag allows the hash table to automatically 54 // The hasIsEmptyValueFunction flag allows the hash table to automatically
55 // generate code to check for the empty value when it can be done with the 55 // generate code to check for the empty value when it can be done with the
56 // equality operator, but allows custom functions for cases like String that 56 // equality operator, but allows custom functions for cases like String that
57 // need them. 57 // need them.
58 static const bool hasIsEmptyValueFunction = false; 58 static const bool hasIsEmptyValueFunction = false;
59 59
(...skipping 21 matching lines...) Expand all
81 template <typename U = void> 81 template <typename U = void>
82 struct NeedsToForbidGCOnMove { 82 struct NeedsToForbidGCOnMove {
83 // TODO(yutak): Consider using of std:::is_trivially_move_constructible 83 // TODO(yutak): Consider using of std:::is_trivially_move_constructible
84 // when it is accessible. 84 // when it is accessible.
85 static const bool value = !std::is_pod<T>::value; 85 static const bool value = !std::is_pod<T>::value;
86 }; 86 };
87 87
88 static const WeakHandlingFlag weakHandlingFlag = 88 static const WeakHandlingFlag weakHandlingFlag =
89 IsWeak<T>::value ? WeakHandlingInCollections 89 IsWeak<T>::value ? WeakHandlingInCollections
90 : NoWeakHandlingInCollections; 90 : NoWeakHandlingInCollections;
91
92 static T emptyValue() { return T(); }
91 }; 93 };
92 94
93 // Default integer traits disallow both 0 and -1 as keys (max value instead of 95 // Default integer traits disallow both 0 and -1 as keys (max value instead of
94 // -1 for unsigned). 96 // -1 for unsigned).
95 template <typename T> 97 template <typename T>
96 struct GenericHashTraitsBase<true, T> : GenericHashTraitsBase<false, T> { 98 struct GenericHashTraitsBase<true, false, T>
99 : GenericHashTraitsBase<false, false, T> {
100 static_assert(std::is_integral<T>::value, "These traits are for integers");
97 static const bool emptyValueIsZero = true; 101 static const bool emptyValueIsZero = true;
98 static void constructDeletedValue(T& slot, bool) { 102 static void constructDeletedValue(T& slot, bool) {
99 slot = static_cast<T>(-1); 103 slot = static_cast<T>(-1);
100 } 104 }
101 static bool isDeletedValue(T value) { return value == static_cast<T>(-1); } 105 static bool isDeletedValue(T value) { return value == static_cast<T>(-1); }
102 }; 106 };
103 107
108 // Default enum traits disallow both -1 and -2 as keys (max and max-1 for
109 // enums with unsigned underlying types).
104 template <typename T> 110 template <typename T>
105 struct GenericHashTraits 111 struct GenericHashTraitsBase<false, true, T>
106 : GenericHashTraitsBase<std::is_integral<T>::value, T> { 112 : GenericHashTraitsBase<false, false, T> {
113 static_assert(std::is_enum<T>::value, "These traits are for enums");
114 static T emptyValue() { return static_cast<T>(-1); }
115 static void constructDeletedValue(T& slot, bool) {
116 slot = static_cast<T>(-2);
117 }
118 static bool isDeletedValue(T value) { return value == static_cast<T>(-2); }
119 };
120
121 template <typename T>
122 struct GenericHashTraits : GenericHashTraitsBase<std::is_integral<T>::value,
123 std::is_enum<T>::value,
124 T> {
107 typedef T TraitType; 125 typedef T TraitType;
108 typedef T EmptyValueType; 126 typedef T EmptyValueType;
109 127
110 static T emptyValue() { return T(); }
111
112 // Type for functions that do not take ownership, such as contains. 128 // Type for functions that do not take ownership, such as contains.
113 typedef const T& PeekInType; 129 typedef const T& PeekInType;
114 typedef T* IteratorGetType; 130 typedef T* IteratorGetType;
115 typedef const T* IteratorConstGetType; 131 typedef const T* IteratorConstGetType;
116 typedef T& IteratorReferenceType; 132 typedef T& IteratorReferenceType;
117 typedef const T& IteratorConstReferenceType; 133 typedef const T& IteratorConstReferenceType;
118 static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { 134 static IteratorReferenceType getToReferenceConversion(IteratorGetType x) {
119 return *x; 135 return *x;
120 } 136 }
121 static IteratorConstReferenceType getToReferenceConstConversion( 137 static IteratorConstReferenceType getToReferenceConstConversion(
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 }; 436 };
421 437
422 } // namespace WTF 438 } // namespace WTF
423 439
424 using WTF::HashTraits; 440 using WTF::HashTraits;
425 using WTF::PairHashTraits; 441 using WTF::PairHashTraits;
426 using WTF::NullableHashTraits; 442 using WTF::NullableHashTraits;
427 using WTF::SimpleClassHashTraits; 443 using WTF::SimpleClassHashTraits;
428 444
429 #endif // WTF_HashTraits_h 445 #endif // WTF_HashTraits_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/HashFunctions.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698