2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-03-17 15:31:17 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
* 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
|
|
|
|
|
2021-04-10 13:59:06 +00:00
|
|
|
#include <AK/StdLibExtraDetails.h>
|
|
|
|
|
2021-03-07 20:22:36 +00:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
|
2020-10-20 16:08:13 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-17 15:31:17 +00:00
|
|
|
namespace std {
|
|
|
|
|
|
|
|
// NOTE: This is in the "std" namespace since some compiler features rely on it.
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
constexpr T&& move(T& arg)
|
|
|
|
{
|
|
|
|
return static_cast<T&&>(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using std::move;
|
|
|
|
|
2021-04-16 12:03:24 +00:00
|
|
|
namespace AK::Detail {
|
|
|
|
template<typename T>
|
|
|
|
struct _RawPtr {
|
|
|
|
using Type = T*;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
namespace AK {
|
|
|
|
|
2020-08-18 18:37:27 +00:00
|
|
|
template<typename T>
|
|
|
|
auto declval() -> T;
|
|
|
|
|
2021-04-10 13:59:06 +00:00
|
|
|
template<class T>
|
|
|
|
constexpr T&& forward(RemoveReference<T>& param)
|
|
|
|
{
|
|
|
|
return static_cast<T&&>(param);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
constexpr T&& forward(RemoveReference<T>&& param) noexcept
|
|
|
|
{
|
|
|
|
static_assert(!IsLvalueReference<T>, "Can't forward an rvalue as an lvalue.");
|
|
|
|
return static_cast<T&&>(param);
|
|
|
|
}
|
|
|
|
|
2020-08-16 09:04:00 +00:00
|
|
|
template<typename T, typename SizeType = decltype(sizeof(T)), SizeType N>
|
|
|
|
constexpr SizeType array_size(T (&)[N])
|
|
|
|
{
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
template<typename T>
|
2020-10-20 16:08:13 +00:00
|
|
|
constexpr T min(const T& a, const T& b)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2020-08-06 07:58:45 +00:00
|
|
|
return b < a ? b : a;
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-20 16:08:13 +00:00
|
|
|
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>
|
2020-10-20 16:08:13 +00:00
|
|
|
constexpr T clamp(const T& value, const T& min, const T& max)
|
2020-01-20 08:54:15 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(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>
|
2020-10-20 16:08:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-04-22 09:52:26 +00:00
|
|
|
template<typename T, typename U = T>
|
2020-10-19 16:30:30 +00:00
|
|
|
constexpr T exchange(T& slot, U&& value)
|
2020-04-22 09:52:26 +00:00
|
|
|
{
|
|
|
|
T old_value = move(slot);
|
|
|
|
slot = forward<U>(value);
|
|
|
|
return old_value;
|
|
|
|
}
|
|
|
|
|
2021-04-16 12:03:24 +00:00
|
|
|
template<typename T>
|
|
|
|
using RawPtr = typename Detail::_RawPtr<T>::Type;
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-16 09:04:00 +00:00
|
|
|
using AK::array_size;
|
2019-05-28 09:53:16 +00:00
|
|
|
using AK::ceil_div;
|
2020-02-09 14:50:13 +00:00
|
|
|
using AK::clamp;
|
2020-08-18 18:37:27 +00:00
|
|
|
using AK::declval;
|
2019-05-28 09:53:16 +00:00
|
|
|
using AK::exchange;
|
|
|
|
using AK::forward;
|
2018-10-10 09:53:07 +00:00
|
|
|
using AK::max;
|
2019-05-28 09:53:16 +00:00
|
|
|
using AK::min;
|
2021-04-16 12:03:24 +00:00
|
|
|
using AK::RawPtr;
|
2019-06-07 09:46:22 +00:00
|
|
|
using AK::swap;
|