Преглед изворни кода

AK: Add Traits for Enums

Enums can be hashed as their underlying integral type. This allows enum
keys in hash maps etc.
kleines Filmröllchen пре 3 година
родитељ
комит
f99e6507ee
1 измењених фајлова са 7 додато и 0 уклоњено
  1. 7 0
      AK/Traits.h

+ 7 - 0
AK/Traits.h

@@ -6,6 +6,7 @@
 
 #pragma once
 
+#include <AK/Concepts.h>
 #include <AK/Forward.h>
 #include <AK/HashFunctions.h>
 
@@ -41,6 +42,12 @@ requires(IsPointer<T>) struct Traits<T> : public GenericTraits<T> {
     static constexpr bool is_trivial() { return true; }
 };
 
+template<Enum T>
+struct Traits<T> : public GenericTraits<T> {
+    static unsigned hash(T value) { return Traits<UnderlyingType<T>>::hash(to_underlying(value)); }
+    static constexpr bool is_trivial() { return Traits<UnderlyingType<T>>::is_trivial(); }
+};
+
 }
 
 using AK::GenericTraits;