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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/Source/wtf/HashFunctions.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/wtf/HashTraits.h
diff --git a/third_party/WebKit/Source/wtf/HashTraits.h b/third_party/WebKit/Source/wtf/HashTraits.h
index 6c9580a413bfe1a2da552a9b0761ed97a811b255..c4319ce235c80284cfa1fb738401502c2bb5faef 100644
--- a/third_party/WebKit/Source/wtf/HashTraits.h
+++ b/third_party/WebKit/Source/wtf/HashTraits.h
@@ -35,7 +35,7 @@
namespace WTF {
-template <bool isInteger, typename T>
+template <bool isInteger, bool isEnum, typename T>
struct GenericHashTraitsBase;
template <typename T>
struct HashTraits;
@@ -46,7 +46,7 @@ enum ShouldWeakPointersBeMarkedStrongly {
};
template <typename T>
-struct GenericHashTraitsBase<false, T> {
+struct GenericHashTraitsBase<false, false, T> {
// The emptyValueIsZero flag is used to optimize allocation of empty hash
// tables with zeroed memory.
static const bool emptyValueIsZero = false;
@@ -88,12 +88,16 @@ struct GenericHashTraitsBase<false, T> {
static const WeakHandlingFlag weakHandlingFlag =
IsWeak<T>::value ? WeakHandlingInCollections
: NoWeakHandlingInCollections;
+
+ static T emptyValue() { return T(); }
};
// Default integer traits disallow both 0 and -1 as keys (max value instead of
// -1 for unsigned).
template <typename T>
-struct GenericHashTraitsBase<true, T> : GenericHashTraitsBase<false, T> {
+struct GenericHashTraitsBase<true, false, T>
+ : GenericHashTraitsBase<false, false, T> {
+ static_assert(std::is_integral<T>::value, "These traits are for integers");
static const bool emptyValueIsZero = true;
static void constructDeletedValue(T& slot, bool) {
slot = static_cast<T>(-1);
@@ -101,14 +105,26 @@ struct GenericHashTraitsBase<true, T> : GenericHashTraitsBase<false, T> {
static bool isDeletedValue(T value) { return value == static_cast<T>(-1); }
};
+// Default enum traits disallow both -1 and -2 as keys (max and max-1 for
+// enums with unsigned underlying types).
template <typename T>
-struct GenericHashTraits
- : GenericHashTraitsBase<std::is_integral<T>::value, T> {
+struct GenericHashTraitsBase<false, true, T>
+ : GenericHashTraitsBase<false, false, T> {
+ static_assert(std::is_enum<T>::value, "These traits are for enums");
+ static T emptyValue() { return static_cast<T>(-1); }
+ static void constructDeletedValue(T& slot, bool) {
+ slot = static_cast<T>(-2);
+ }
+ static bool isDeletedValue(T value) { return value == static_cast<T>(-2); }
+};
+
+template <typename T>
+struct GenericHashTraits : GenericHashTraitsBase<std::is_integral<T>::value,
+ std::is_enum<T>::value,
+ T> {
typedef T TraitType;
typedef T EmptyValueType;
- static T emptyValue() { return T(); }
-
// Type for functions that do not take ownership, such as contains.
typedef const T& PeekInType;
typedef T* IteratorGetType;
« 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