/* * Copyright (c) 2018-2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include namespace AK { template struct GenericTraits { using PeekType = T&; using ConstPeekType = T const&; static constexpr bool is_trivial() { return false; } static constexpr bool equals(T const& a, T const& b) { return a == b; } template U> static bool equals(U const& a, T const& b) { return a == b; } }; template struct Traits : public GenericTraits { }; template requires(IsIntegral) struct Traits : public GenericTraits { 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); } }; #ifndef KERNEL template requires(IsFloatingPoint) struct Traits : public GenericTraits { static constexpr bool is_trivial() { return true; } static constexpr unsigned hash(T value) { if constexpr (sizeof(T) < 8) return int_hash(bit_cast(value)); else return u64_hash(bit_cast(value)); } }; #endif template requires(IsPointer && !Detail::IsPointerOfType) struct Traits : public GenericTraits { static unsigned hash(T p) { return ptr_hash((FlatPtr)p); } static constexpr bool is_trivial() { return true; } }; template struct Traits : public GenericTraits { static unsigned hash(T value) { return Traits>::hash(to_underlying(value)); } static constexpr bool is_trivial() { return Traits>::is_trivial(); } }; template requires(Detail::IsPointerOfType) struct Traits : public GenericTraits { 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