2020-01-18 08:38:21 +00: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 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-05 13:54:56 +00:00
|
|
|
#define UNUSED_PARAM(x) (void)x
|
|
|
|
|
2020-03-08 11:34:33 +00:00
|
|
|
inline constexpr unsigned round_up_to_power_of_two(unsigned value, unsigned power_of_two)
|
2019-05-06 12:04:54 +00:00
|
|
|
{
|
2019-05-28 09:53:16 +00:00
|
|
|
return ((value - 1) & ~(power_of_two - 1)) + power_of_two;
|
2019-05-06 12:04:54 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
2019-06-24 08:13:28 +00:00
|
|
|
inline constexpr T min(const T& a, const T& b)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
return a < b ? a : b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2019-06-24 08:13:28 +00:00
|
|
|
inline constexpr T max(const T& a, const T& b)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
return a < b ? b : a;
|
|
|
|
}
|
|
|
|
|
2020-01-20 08:54:15 +00:00
|
|
|
template<typename T>
|
|
|
|
inline constexpr T clamp(const T& value, const T& min, const T& max)
|
|
|
|
{
|
2020-01-20 12:49:05 +00:00
|
|
|
ASSERT(max >= min);
|
2020-01-20 08:54:15 +00:00
|
|
|
if (value > max)
|
|
|
|
return max;
|
|
|
|
if (value < min)
|
|
|
|
return min;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2018-11-08 14:39:26 +00:00
|
|
|
template<typename T, typename U>
|
2019-06-24 08:13:28 +00:00
|
|
|
inline constexpr T ceil_div(T a, U b)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-11-08 14:39:26 +00:00
|
|
|
static_assert(sizeof(T) == sizeof(U));
|
2018-10-10 09:53:07 +00:00
|
|
|
T result = a / b;
|
|
|
|
if ((a % b) != 0)
|
|
|
|
++result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:43:52 +00:00
|
|
|
#ifdef __clang__
|
2019-05-28 09:53:16 +00:00
|
|
|
# pragma clang diagnostic push
|
|
|
|
# pragma clang diagnostic ignored "-Wconsumed"
|
2019-02-25 11:43:52 +00:00
|
|
|
#endif
|
2019-05-28 09:53:16 +00:00
|
|
|
template<typename T>
|
2020-01-19 09:28:58 +00:00
|
|
|
inline T&& move(T& arg)
|
2018-10-16 10:10:01 +00:00
|
|
|
{
|
|
|
|
return static_cast<T&&>(arg);
|
|
|
|
}
|
2019-02-25 11:43:52 +00:00
|
|
|
#ifdef __clang__
|
2019-05-28 09:53:16 +00:00
|
|
|
# pragma clang diagnostic pop
|
2019-02-25 11:43:52 +00:00
|
|
|
#endif
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-10-16 12:33:16 +00:00
|
|
|
template<typename T, typename U>
|
2020-01-19 09:28:58 +00:00
|
|
|
inline void swap(T& a, U& b)
|
2018-10-16 12:33:16 +00:00
|
|
|
{
|
|
|
|
U tmp = move((U&)a);
|
2019-05-28 09:53:16 +00:00
|
|
|
a = (T &&) move(b);
|
2018-10-16 12:33:16 +00:00
|
|
|
b = move(tmp);
|
|
|
|
}
|
2018-10-16 10:20:51 +00:00
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
template<bool B, class T = void>
|
2019-05-28 09:53:16 +00:00
|
|
|
struct EnableIf {
|
2018-10-17 08:55:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
2019-05-28 09:53:16 +00:00
|
|
|
struct EnableIf<true, T> {
|
2018-10-17 08:55:43 +00:00
|
|
|
typedef T Type;
|
|
|
|
};
|
|
|
|
|
2020-07-24 00:38:01 +00:00
|
|
|
template<class T>
|
|
|
|
struct AddConst {
|
|
|
|
typedef const T Type;
|
|
|
|
};
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<class T>
|
|
|
|
struct RemoveConst {
|
|
|
|
typedef T Type;
|
|
|
|
};
|
|
|
|
template<class T>
|
|
|
|
struct RemoveConst<const T> {
|
|
|
|
typedef T Type;
|
|
|
|
};
|
|
|
|
template<class T>
|
|
|
|
struct RemoveVolatile {
|
|
|
|
typedef T Type;
|
|
|
|
};
|
|
|
|
template<class T>
|
2019-06-27 14:01:24 +00:00
|
|
|
struct RemoveVolatile<volatile T> {
|
2019-05-28 09:53:16 +00:00
|
|
|
typedef T Type;
|
|
|
|
};
|
|
|
|
template<class T>
|
|
|
|
struct RemoveCV {
|
2018-10-17 08:55:43 +00:00
|
|
|
typedef typename RemoveVolatile<typename RemoveConst<T>::Type>::Type Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T, T v>
|
|
|
|
struct IntegralConstant {
|
|
|
|
static constexpr T value = v;
|
|
|
|
typedef T ValueType;
|
|
|
|
typedef IntegralConstant Type;
|
|
|
|
constexpr operator ValueType() const { return value; }
|
|
|
|
constexpr ValueType operator()() const { return value; }
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef IntegralConstant<bool, false> FalseType;
|
|
|
|
typedef IntegralConstant<bool, true> TrueType;
|
|
|
|
|
2020-04-07 12:31:49 +00:00
|
|
|
template<class T>
|
|
|
|
struct IsLvalueReference : FalseType {
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct IsLvalueReference<T&> : TrueType {
|
|
|
|
};
|
|
|
|
|
2018-10-17 08:55:43 +00:00
|
|
|
template<class T>
|
2019-05-28 09:53:16 +00:00
|
|
|
struct __IsPointerHelper : FalseType {
|
|
|
|
};
|
2018-10-17 08:55:43 +00:00
|
|
|
|
|
|
|
template<class T>
|
2019-05-28 09:53:16 +00:00
|
|
|
struct __IsPointerHelper<T*> : TrueType {
|
|
|
|
};
|
2018-10-17 08:55:43 +00:00
|
|
|
|
|
|
|
template<class T>
|
2019-05-28 09:53:16 +00:00
|
|
|
struct IsPointer : __IsPointerHelper<typename RemoveCV<T>::Type> {
|
|
|
|
};
|
2018-10-17 08:55:43 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<class>
|
|
|
|
struct IsFunction : FalseType {
|
|
|
|
};
|
2018-10-17 08:55:43 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args...)> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args..., ...)> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args...) const> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args..., ...) const> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args...) volatile> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args..., ...) volatile> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args...) const volatile> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args..., ...) const volatile> : TrueType {
|
|
|
|
};
|
2018-10-17 08:55:43 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args...)&> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args..., ...)&> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args...) const&> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args..., ...) const&> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args...) volatile&> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args..., ...) volatile&> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args...) const volatile&> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args..., ...) const volatile&> : TrueType {
|
|
|
|
};
|
2018-10-17 08:55:43 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args...) &&> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args..., ...) &&> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args...) const&&> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args..., ...) const&&> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args...) volatile&&> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args..., ...) volatile&&> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args...) const volatile&&> : TrueType {
|
|
|
|
};
|
|
|
|
template<class Ret, class... Args>
|
|
|
|
struct IsFunction<Ret(Args..., ...) const volatile&&> : TrueType {
|
|
|
|
};
|
2018-10-17 08:55:43 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<class T>
|
|
|
|
struct IsRvalueReference : FalseType {
|
|
|
|
};
|
|
|
|
template<class T>
|
|
|
|
struct IsRvalueReference<T&&> : TrueType {
|
|
|
|
};
|
2018-10-17 08:55:43 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<class T>
|
|
|
|
struct RemovePointer {
|
|
|
|
typedef T Type;
|
|
|
|
};
|
|
|
|
template<class T>
|
|
|
|
struct RemovePointer<T*> {
|
|
|
|
typedef T Type;
|
|
|
|
};
|
|
|
|
template<class T>
|
|
|
|
struct RemovePointer<T* const> {
|
|
|
|
typedef T Type;
|
|
|
|
};
|
|
|
|
template<class T>
|
|
|
|
struct RemovePointer<T* volatile> {
|
|
|
|
typedef T Type;
|
|
|
|
};
|
|
|
|
template<class T>
|
|
|
|
struct RemovePointer<T* const volatile> {
|
|
|
|
typedef T Type;
|
|
|
|
};
|
2018-10-17 08:55:43 +00:00
|
|
|
|
2019-02-20 11:28:41 +00:00
|
|
|
template<typename T, typename U>
|
|
|
|
struct IsSame {
|
2019-06-07 15:13:23 +00:00
|
|
|
enum {
|
2019-05-28 09:53:16 +00:00
|
|
|
value = 0
|
|
|
|
};
|
2019-02-20 11:28:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct IsSame<T, T> {
|
2019-06-07 15:13:23 +00:00
|
|
|
enum {
|
2019-05-28 09:53:16 +00:00
|
|
|
value = 1
|
|
|
|
};
|
2019-02-20 11:28:41 +00:00
|
|
|
};
|
|
|
|
|
2020-03-08 09:36:11 +00:00
|
|
|
template<bool condition, class TrueType, class FalseType>
|
|
|
|
struct Conditional {
|
|
|
|
typedef TrueType Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class TrueType, class FalseType>
|
|
|
|
struct Conditional<false, TrueType, FalseType> {
|
|
|
|
typedef FalseType Type;
|
|
|
|
};
|
|
|
|
|
2020-04-07 12:31:49 +00:00
|
|
|
template<typename T>
|
|
|
|
struct RemoveReference {
|
|
|
|
typedef T Type;
|
|
|
|
};
|
|
|
|
template<class T>
|
|
|
|
struct RemoveReference<T&> {
|
|
|
|
typedef T Type;
|
|
|
|
};
|
|
|
|
template<class T>
|
|
|
|
struct RemoveReference<T&&> {
|
|
|
|
typedef T Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline constexpr T&& forward(typename RemoveReference<T>::Type& param)
|
|
|
|
{
|
|
|
|
return static_cast<T&&>(param);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline constexpr T&& forward(typename RemoveReference<T>::Type&& param) noexcept
|
|
|
|
{
|
|
|
|
static_assert(!IsLvalueReference<T>::value, "Can't forward an rvalue as an lvalue.");
|
|
|
|
return static_cast<T&&>(param);
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:51:36 +00:00
|
|
|
template<typename T>
|
|
|
|
struct MakeUnsigned {
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2020-07-18 15:19:59 +00:00
|
|
|
struct MakeUnsigned<signed char> {
|
2020-04-15 14:51:36 +00:00
|
|
|
typedef unsigned char type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeUnsigned<short> {
|
|
|
|
typedef unsigned short type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeUnsigned<int> {
|
|
|
|
typedef unsigned type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeUnsigned<long> {
|
|
|
|
typedef unsigned long type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeUnsigned<long long> {
|
|
|
|
typedef unsigned long long type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeUnsigned<unsigned char> {
|
|
|
|
typedef unsigned char type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeUnsigned<unsigned short> {
|
|
|
|
typedef unsigned short type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeUnsigned<unsigned int> {
|
|
|
|
typedef unsigned type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeUnsigned<unsigned long> {
|
|
|
|
typedef unsigned long type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeUnsigned<unsigned long long> {
|
|
|
|
typedef unsigned long long type;
|
|
|
|
};
|
|
|
|
|
2020-05-23 10:39:51 +00:00
|
|
|
template<typename T>
|
|
|
|
struct MakeSigned {
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2020-07-18 15:19:59 +00:00
|
|
|
struct MakeSigned<signed char> {
|
|
|
|
typedef signed char type;
|
2020-05-23 10:39:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeSigned<short> {
|
|
|
|
typedef short type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeSigned<int> {
|
|
|
|
typedef int type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeSigned<long> {
|
|
|
|
typedef long type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeSigned<long long> {
|
|
|
|
typedef long long type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeSigned<unsigned char> {
|
|
|
|
typedef char type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeSigned<unsigned short> {
|
|
|
|
typedef short type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeSigned<unsigned int> {
|
|
|
|
typedef int type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeSigned<unsigned long> {
|
|
|
|
typedef long type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeSigned<unsigned long long> {
|
|
|
|
typedef long long type;
|
|
|
|
};
|
|
|
|
|
2020-07-24 00:38:01 +00:00
|
|
|
template<class T>
|
|
|
|
struct IsVoid : IsSame<void, typename RemoveCV<T>::Type> {
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct IsConst : FalseType {
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct IsConst<const T> : TrueType {
|
|
|
|
};
|
|
|
|
|
2020-04-22 09:52:26 +00:00
|
|
|
template<typename T, typename U = T>
|
|
|
|
inline constexpr T exchange(T& slot, U&& value)
|
|
|
|
{
|
|
|
|
T old_value = move(slot);
|
|
|
|
slot = forward<U>(value);
|
|
|
|
return old_value;
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 00:38:01 +00:00
|
|
|
using AK::AddConst;
|
2019-05-28 09:53:16 +00:00
|
|
|
using AK::ceil_div;
|
2020-02-09 14:50:13 +00:00
|
|
|
using AK::clamp;
|
2020-04-07 12:31:49 +00:00
|
|
|
using AK::Conditional;
|
2019-05-28 09:53:16 +00:00
|
|
|
using AK::exchange;
|
|
|
|
using AK::forward;
|
2020-07-24 00:38:01 +00:00
|
|
|
using AK::IsConst;
|
2019-05-28 09:53:16 +00:00
|
|
|
using AK::IsSame;
|
2020-07-24 00:38:01 +00:00
|
|
|
using AK::IsVoid;
|
2020-05-23 10:39:51 +00:00
|
|
|
using AK::MakeSigned;
|
2020-04-15 14:51:36 +00:00
|
|
|
using AK::MakeUnsigned;
|
2018-10-10 09:53:07 +00:00
|
|
|
using AK::max;
|
2019-05-28 09:53:16 +00:00
|
|
|
using AK::min;
|
2018-10-16 10:20:51 +00:00
|
|
|
using AK::move;
|
2019-06-01 12:11:31 +00:00
|
|
|
using AK::RemoveConst;
|
2019-06-07 09:46:22 +00:00
|
|
|
using AK::swap;
|