1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 17:27:35 +00:00

AK: Defer to Traits<T> for equality comparison in container templates.

This is prep work for supporting HashMap with NonnullRefPtr<T> as values.
It's currently not possible because many HashTable functions require being
able to default-construct the value type.
This commit is contained in:
Andreas Kling 2019-06-29 19:14:03 +02:00
parent 9a7dc06567
commit d5bb98acbc
14 changed files with 61 additions and 31 deletions

View file

@ -6,25 +6,30 @@
namespace AK {
template<typename T>
struct Traits {
struct GenericTraits {
static bool equals(const T& a, const T& b) { return a == b; }
};
template<typename T>
struct Traits : public GenericTraits<T> {
};
template<>
struct Traits<int> {
struct Traits<int> : public GenericTraits<int> {
static unsigned hash(int i) { return int_hash(i); }
static void dump(int i) { kprintf("%d", i); }
};
template<>
struct Traits<unsigned> {
struct Traits<unsigned> : public GenericTraits<unsigned> {
static unsigned hash(unsigned u) { return int_hash(u); }
static void dump(unsigned u) { kprintf("%u", u); }
};
template<>
struct Traits<word> {
static unsigned hash(unsigned u) { return int_hash(u); }
static void dump(unsigned u) { kprintf("%u", u); }
struct Traits<word> : public GenericTraits<word> {
static unsigned hash(word u) { return int_hash(u); }
static void dump(word u) { kprintf("%u", u); }
};
template<typename T>
@ -34,6 +39,7 @@ struct Traits<T*> {
return int_hash((unsigned)(__PTRDIFF_TYPE__)p);
}
static void dump(const T* p) { kprintf("%p", p); }
static bool equals(const T* a, const T* b) { return a == b; }
};
}