2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-16 04:01:18 +03:00
|
|
|
#include <AK/Forward.h>
|
2020-02-10 13:55:34 +03:00
|
|
|
#include <AK/HashFunctions.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
2019-06-29 20:14:03 +03:00
|
|
|
struct GenericTraits {
|
2019-08-14 12:47:38 +03:00
|
|
|
using PeekType = T;
|
2019-08-07 16:05:10 +03:00
|
|
|
static constexpr bool is_trivial() { return false; }
|
2019-06-29 20:14:03 +03:00
|
|
|
static bool equals(const T& a, const T& b) { return a == b; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct Traits : public GenericTraits<T> {
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2019-06-29 20:14:03 +03:00
|
|
|
struct Traits<int> : public GenericTraits<int> {
|
2019-08-07 12:53:21 +03:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2019-01-31 19:31:23 +03:00
|
|
|
static unsigned hash(int i) { return int_hash(i); }
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2019-06-29 20:14:03 +03:00
|
|
|
struct Traits<unsigned> : public GenericTraits<unsigned> {
|
2019-08-07 12:53:21 +03:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2019-01-31 19:31:23 +03:00
|
|
|
static unsigned hash(unsigned u) { return int_hash(u); }
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
2020-11-29 11:46:37 +03:00
|
|
|
template<>
|
|
|
|
struct Traits<u8> : public GenericTraits<u8> {
|
|
|
|
static constexpr bool is_trivial() { return true; }
|
|
|
|
static unsigned hash(u8 u) { return int_hash(u); }
|
|
|
|
};
|
|
|
|
|
2019-03-13 18:05:56 +03:00
|
|
|
template<>
|
2019-07-03 22:17:35 +03:00
|
|
|
struct Traits<u16> : public GenericTraits<u16> {
|
2019-08-07 12:53:21 +03:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2019-07-03 22:17:35 +03:00
|
|
|
static unsigned hash(u16 u) { return int_hash(u); }
|
2019-03-13 18:05:56 +03:00
|
|
|
};
|
|
|
|
|
2020-01-03 09:56:33 +03:00
|
|
|
template<>
|
|
|
|
struct Traits<u64> : public GenericTraits<u64> {
|
|
|
|
static constexpr bool is_trivial() { return true; }
|
|
|
|
static unsigned hash(u64 u) { return u64_hash(u); }
|
|
|
|
};
|
|
|
|
|
2019-08-07 12:53:21 +03:00
|
|
|
template<>
|
|
|
|
struct Traits<char> : public GenericTraits<char> {
|
|
|
|
static constexpr bool is_trivial() { return true; }
|
|
|
|
static unsigned hash(char c) { return int_hash(c); }
|
|
|
|
};
|
|
|
|
|
2018-10-11 17:52:30 +03:00
|
|
|
template<typename T>
|
2019-08-14 12:47:38 +03:00
|
|
|
struct Traits<T*> : public GenericTraits<T*> {
|
2018-10-27 10:33:24 +03:00
|
|
|
static unsigned hash(const T* p)
|
|
|
|
{
|
2019-06-22 22:21:57 +03:00
|
|
|
return int_hash((unsigned)(__PTRDIFF_TYPE__)p);
|
2018-10-27 10:33:24 +03:00
|
|
|
}
|
2019-08-07 12:53:21 +03:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2019-06-29 20:14:03 +03:00
|
|
|
static bool equals(const T* a, const T* b) { return a == b; }
|
2018-10-11 17:52:30 +03:00
|
|
|
};
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
2020-02-16 04:01:18 +03:00
|
|
|
|
|
|
|
using AK::GenericTraits;
|
|
|
|
using AK::Traits;
|