serenity/AK/Traits.h

86 lines
2.5 KiB
C
Raw Normal View History

/*
2022-04-10 17:17:47 +00:00
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
2022-04-10 17:17:47 +00:00
#include <AK/BitCast.h>
#include <AK/Concepts.h>
#include <AK/Forward.h>
#include <AK/HashFunctions.h>
2021-11-07 13:37:05 +00:00
#include <AK/StringHash.h>
namespace AK {
template<typename T>
struct GenericTraits {
using PeekType = T&;
using ConstPeekType = T const&;
static constexpr bool is_trivial() { return false; }
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; }
template<Concepts::HashCompatible<T> U>
static bool equals(U const& a, T const& b) { return a == b; }
};
template<typename T>
struct Traits : public GenericTraits<T> {
};
template<Integral T>
struct Traits<T> : public GenericTraits<T> {
static constexpr bool is_trivial() { return true; }
static constexpr bool is_trivially_serializable() { return true; }
static constexpr unsigned hash(T value)
{
if constexpr (sizeof(T) < 8)
return int_hash(value);
else
return u64_hash(value);
}
};
#ifndef KERNEL
template<FloatingPoint T>
struct Traits<T> : public GenericTraits<T> {
static constexpr bool is_trivial() { return true; }
static constexpr bool is_trivially_serializable() { return true; }
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> {
2023-03-07 14:28:21 +00:00
static unsigned hash(T p) { return ptr_hash(p); }
static constexpr bool is_trivial() { return true; }
2018-10-11 14:52:30 +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(); }
static constexpr bool is_trivially_serializable() { return Traits<UnderlyingType<T>>::is_trivially_serializable(); }
};
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; }
};
}
#if USING_AK_GLOBALLY
using AK::GenericTraits;
using AK::Traits;
#endif