serenity/AK/Traits.h
Ali Mohammad Pur ad328f852b AK: Replace all explicit specialisations of Traits with a single one
This commit un-confuses the many specialisations of AK::Traits, and
makes it work correctly for all integral types.
Prior to this, `AK::Traits<size_t>` would've been instantiating the
base Traits implementation, not `Traits<u32>` or `Traits<u64>`.
2021-07-12 23:49:59 +04:30

52 lines
1.1 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Forward.h>
#include <AK/HashFunctions.h>
namespace AK {
template<typename T>
struct GenericTraits {
using PeekType = T;
using ConstPeekType = T;
static constexpr bool is_trivial() { return false; }
static constexpr bool equals(const T& a, const T& b) { return a == b; }
};
template<typename T>
struct Traits : public GenericTraits<T> {
};
template<typename T>
requires(IsIntegral<T>) struct Traits<T> : public GenericTraits<T> {
static constexpr bool is_trivial() { return true; }
static constexpr unsigned hash(T value)
{
if constexpr (sizeof(T) < 8)
return int_hash(value);
else
return u64_hash(value);
}
};
template<typename T>
struct Traits<T*> : public GenericTraits<T*> {
static unsigned hash(const T* p)
{
return int_hash((unsigned)(__PTRDIFF_TYPE__)p);
}
static constexpr bool is_trivial() { return true; }
static bool equals(const T* a, const T* b) { return a == b; }
};
}
using AK::GenericTraits;
using AK::Traits;