2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2022-04-10 17:17:47 +00:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-04-10 17:17:47 +00:00
|
|
|
#include <AK/BitCast.h>
|
2021-08-19 12:46:05 +00:00
|
|
|
#include <AK/Concepts.h>
|
2020-02-16 01:01:18 +00:00
|
|
|
#include <AK/Forward.h>
|
2020-02-10 10:55:34 +00:00
|
|
|
#include <AK/HashFunctions.h>
|
2021-11-07 13:37:05 +00:00
|
|
|
#include <AK/StringHash.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
2019-06-29 17:14:03 +00:00
|
|
|
struct GenericTraits {
|
2022-04-03 13:34:57 +00:00
|
|
|
using PeekType = T&;
|
|
|
|
using ConstPeekType = T const&;
|
2019-08-07 13:05:10 +00:00
|
|
|
static constexpr bool is_trivial() { return false; }
|
2023-01-14 20:13:29 +00:00
|
|
|
static constexpr bool is_trivially_serializable() { return false; }
|
2022-10-16 22:06:11 +00:00
|
|
|
static constexpr bool equals(T const& a, T const& b) { return a == b; }
|
2022-01-29 18:01:35 +00:00
|
|
|
template<Concepts::HashCompatible<T> U>
|
|
|
|
static bool equals(U const& a, T const& b) { return a == b; }
|
2019-06-29 17:14:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct Traits : public GenericTraits<T> {
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
2022-11-14 18:20:59 +00:00
|
|
|
template<Integral T>
|
|
|
|
struct Traits<T> : public GenericTraits<T> {
|
2020-01-03 06:56:33 +00:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2023-01-14 20:13:29 +00:00
|
|
|
static constexpr bool is_trivially_serializable() { return true; }
|
2021-07-12 18:08:03 +00:00
|
|
|
static constexpr unsigned hash(T value)
|
|
|
|
{
|
|
|
|
if constexpr (sizeof(T) < 8)
|
|
|
|
return int_hash(value);
|
|
|
|
else
|
|
|
|
return u64_hash(value);
|
|
|
|
}
|
2020-01-03 06:56:33 +00:00
|
|
|
};
|
|
|
|
|
2022-04-10 10:27:08 +00:00
|
|
|
#ifndef KERNEL
|
2022-11-14 18:20:59 +00:00
|
|
|
template<FloatingPoint T>
|
|
|
|
struct Traits<T> : public GenericTraits<T> {
|
2022-04-10 10:27:08 +00:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2023-01-14 20:13:29 +00:00
|
|
|
static constexpr bool is_trivially_serializable() { return true; }
|
2022-04-10 10:27:08 +00:00
|
|
|
static constexpr unsigned hash(T value)
|
|
|
|
{
|
|
|
|
if constexpr (sizeof(T) < 8)
|
|
|
|
return int_hash(bit_cast<u32>(value));
|
|
|
|
else
|
|
|
|
return u64_hash(bit_cast<u64>(value));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2018-10-11 14:52:30 +00:00
|
|
|
template<typename T>
|
2021-11-07 13:37:05 +00:00
|
|
|
requires(IsPointer<T> && !Detail::IsPointerOfType<char, T>) struct Traits<T> : public GenericTraits<T> {
|
2021-07-12 18:13:58 +00:00
|
|
|
static unsigned hash(T p) { return ptr_hash((FlatPtr)p); }
|
2019-08-07 09:53:21 +00:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2018-10-11 14:52:30 +00:00
|
|
|
};
|
|
|
|
|
2021-08-19 12:46:05 +00:00
|
|
|
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(); }
|
2023-01-14 20:13:29 +00:00
|
|
|
static constexpr bool is_trivially_serializable() { return Traits<UnderlyingType<T>>::is_trivially_serializable(); }
|
2021-08-19 12:46:05 +00:00
|
|
|
};
|
|
|
|
|
2021-11-07 13:37:05 +00:00
|
|
|
template<typename T>
|
|
|
|
requires(Detail::IsPointerOfType<char, T>) struct Traits<T> : public GenericTraits<T> {
|
|
|
|
static unsigned hash(T const value) { return string_hash(value, strlen(value)); }
|
|
|
|
static constexpr bool equals(T const a, T const b) { return strcmp(a, b); }
|
|
|
|
static constexpr bool is_trivial() { return true; }
|
|
|
|
};
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
2020-02-16 01:01:18 +00:00
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-02-16 01:01:18 +00:00
|
|
|
using AK::GenericTraits;
|
|
|
|
using AK::Traits;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|