/* * Copyright (c) 2018-2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include template constexpr auto round_up_to_power_of_two(T value, U power_of_two) requires(IsIntegral&& IsIntegral) { return ((value - 1) & ~(power_of_two - 1)) + power_of_two; } namespace std { // NOTE: This is in the "std" namespace since some compiler features rely on it. template constexpr T&& move(T& arg) { return static_cast(arg); } } using std::move; namespace AK::Detail { template struct _RawPtr { using Type = T*; }; } namespace AK { template constexpr T&& forward(RemoveReference& param) { return static_cast(param); } template constexpr T&& forward(RemoveReference&& param) noexcept { static_assert(!IsLvalueReference, "Can't forward an rvalue as an lvalue."); return static_cast(param); } template constexpr SizeType array_size(T (&)[N]) { return N; } template constexpr T min(const T& a, const IdentityType& b) { return b < a ? b : a; } template constexpr T max(const T& a, const IdentityType& b) { return a < b ? b : a; } template constexpr T clamp(const T& value, const IdentityType& min, const IdentityType& max) { VERIFY(max >= min); if (value > max) return max; if (value < min) return min; return value; } template constexpr T ceil_div(T a, U b) { static_assert(sizeof(T) == sizeof(U)); T result = a / b; if ((a % b) != 0) ++result; return result; } template inline void swap(T& a, U& b) { U tmp = move((U&)a); a = (T &&) move(b); b = move(tmp); } template constexpr T exchange(T& slot, U&& value) { T old_value = move(slot); slot = forward(value); return old_value; } template using RawPtr = typename Detail::_RawPtr::Type; template constexpr decltype(auto) to_underlying(V value) requires(IsEnum) { return static_cast>(value); } } using AK::array_size; using AK::ceil_div; using AK::clamp; using AK::exchange; using AK::forward; using AK::max; using AK::min; using AK::RawPtr; using AK::swap; using AK::to_underlying;