| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_BASE_UTILITY_HPP |
| 4 |
#define MPT_BASE_UTILITY_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
|
| 11 |
#if MPT_CXX_BEFORE(20) || MPT_LIBCXX_LLVM_BEFORE(13000) |
| 12 |
#include "mpt/base/saturate_cast.hpp" |
| 13 |
#include "mpt/base/saturate_round.hpp" |
| 14 |
#endif |
| 15 |
|
| 16 |
#if MPT_CXX_BEFORE(23) && !MPT_COMPILER_MSVC && !MPT_COMPILER_GCC && !MPT_COMPILER_CLANG |
| 17 |
#include <exception> |
| 18 |
#endif |
| 19 |
#include <new> |
| 20 |
#include <type_traits> |
| 21 |
#include <utility> |
| 22 |
|
| 23 |
#include <cstddef> |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
namespace mpt { |
| 28 |
inline namespace MPT_INLINE_NS { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
template <typename Tdst, typename Tsrc> |
| 33 |
MPT_CONSTEXPRINLINE Tdst c_cast(Tsrc && x) { |
| 34 |
return (Tdst)std::forward<Tsrc>(x); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
template <typename Tdst, typename Tsrc> |
| 40 |
MPT_CONSTEXPRINLINE Tdst function_pointer_cast(Tsrc f) { |
| 41 |
#if !defined(MPT_LIBCXX_QUIRK_INCOMPLETE_IS_FUNCTION) |
| 42 |
// MinGW64 std::is_function is always false for non __cdecl functions. |
| 43 |
// Issue is similar to <https://connect.microsoft.com/VisualStudio/feedback/details/774720/stl-is-function-bug>. |
| 44 |
static_assert(std::is_pointer<typename std::remove_cv<Tsrc>::type>::value); |
| 45 |
static_assert(std::is_pointer<typename std::remove_cv<Tdst>::type>::value); |
| 46 |
static_assert(std::is_function<typename std::remove_pointer<typename std::remove_cv<Tsrc>::type>::type>::value); |
| 47 |
static_assert(std::is_function<typename std::remove_pointer<typename std::remove_cv<Tdst>::type>::type>::value); |
| 48 |
#endif |
| 49 |
#if (MPT_CLANG_AT_LEAST(19, 0, 0) && !MPT_OS_ANDROID) || MPT_CLANG_AT_LEAST(20, 0, 0) |
| 50 |
#pragma clang diagnostic push |
| 51 |
#pragma clang diagnostic ignored "-Wcast-function-type-mismatch" |
| 52 |
#endif |
| 53 |
return reinterpret_cast<Tdst>(f); |
| 54 |
#if (MPT_CLANG_AT_LEAST(19, 0, 0) && !MPT_OS_ANDROID) || MPT_CLANG_AT_LEAST(20, 0, 0) |
| 55 |
#pragma clang diagnostic pop |
| 56 |
#endif |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#if MPT_CXX_AT_LEAST(20) && !MPT_LIBCXX_LLVM_BEFORE(13000) |
| 62 |
|
| 63 |
using std::in_range; |
| 64 |
|
| 65 |
#else |
| 66 |
|
| 67 |
namespace detail { |
| 68 |
|
| 69 |
template <typename Tdst, typename Tsrc> |
| 70 |
constexpr Tdst saturate_cast(Tsrc src) noexcept { |
| 71 |
return mpt::saturate_cast<Tdst>(src); |
| 72 |
} |
| 73 |
|
| 74 |
template <typename Tdst> |
| 75 |
constexpr Tdst saturate_cast(double src) { |
| 76 |
return mpt::saturate_trunc<Tdst>(src); |
| 77 |
} |
| 78 |
|
| 79 |
template <typename Tdst> |
| 80 |
constexpr Tdst saturate_cast(float src) { |
| 81 |
return mpt::saturate_trunc<Tdst>(src); |
| 82 |
} |
| 83 |
|
| 84 |
} // namespace detail |
| 85 |
|
| 86 |
// Returns true iff Tdst can represent the value val. |
| 87 |
// Use as if(mpt::in_range<uint8>(-1)). |
| 88 |
template <typename Tdst, typename Tsrc> |
| 89 |
constexpr bool in_range(Tsrc val) { |
| 90 |
return (static_cast<Tsrc>(mpt::detail::saturate_cast<Tdst>(val)) == val); |
| 91 |
} |
| 92 |
|
| 93 |
#endif |
| 94 |
|
| 95 |
|
| 96 |
#if MPT_CXX_AT_LEAST(23) |
| 97 |
|
| 98 |
using std::to_underlying; |
| 99 |
|
| 100 |
#else // !C++23 |
| 101 |
|
| 102 |
template <typename T> |
| 103 |
constexpr std::underlying_type_t<T> to_underlying(T value) noexcept { |
| 104 |
return static_cast<typename std::underlying_type<T>::type>(value); |
| 105 |
} |
| 106 |
|
| 107 |
#endif // C++23 |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
template <typename T> |
| 112 |
struct value_initializer { |
| 113 |
inline void operator()(T & x) { |
| 114 |
x = T{}; |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
template <typename T, std::size_t N> |
| 119 |
struct value_initializer<T[N]> { |
| 120 |
inline void operator()(T (&a)[N]) { |
| 121 |
for (auto & e : a) { |
| 122 |
value_initializer<T>{}(e); |
| 123 |
} |
| 124 |
} |
| 125 |
}; |
| 126 |
|
| 127 |
template <typename T> |
| 128 |
inline void reset(T & x) { |
| 129 |
value_initializer<T>{}(x); |
| 130 |
} |
| 131 |
|
| 132 |
template <typename T, typename... Targs> |
| 133 |
void reset(T & x, Targs &&... args) { |
| 134 |
x = T{std::forward<Targs>(args)...}; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
template <typename T, typename... Targs> |
| 140 |
void reconstruct(T & x, Targs &&... args) { |
| 141 |
x.~T(); |
| 142 |
new (&x) T{std::forward<Targs>(args)...}; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
#if MPT_CXX_AT_LEAST(20) && !MPT_LIBCXX_LLVM_BEFORE(13000) |
| 148 |
|
| 149 |
using std::cmp_equal; |
| 150 |
using std::cmp_greater; |
| 151 |
using std::cmp_greater_equal; |
| 152 |
using std::cmp_less; |
| 153 |
using std::cmp_less_equal; |
| 154 |
using std::cmp_not_equal; |
| 155 |
|
| 156 |
#else |
| 157 |
|
| 158 |
template <typename Ta, typename Tb> |
| 159 |
constexpr bool cmp_equal(Ta a, Tb b) noexcept { |
| 160 |
using UTa = typename std::make_unsigned<Ta>::type; |
| 161 |
using UTb = typename std::make_unsigned<Tb>::type; |
| 162 |
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) { |
| 163 |
return a == b; |
| 164 |
} else if constexpr (std::is_signed<Ta>::value) { |
| 165 |
return (a < 0) ? false : static_cast<UTa>(a) == b; |
| 166 |
} else { |
| 167 |
return (b < 0) ? false : a == static_cast<UTb>(b); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
template <typename Ta, typename Tb> |
| 172 |
constexpr bool cmp_not_equal(Ta a, Tb b) noexcept { |
| 173 |
using UTa = typename std::make_unsigned<Ta>::type; |
| 174 |
using UTb = typename std::make_unsigned<Tb>::type; |
| 175 |
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) { |
| 176 |
return a != b; |
| 177 |
} else if constexpr (std::is_signed<Ta>::value) { |
| 178 |
return (a < 0) ? true : static_cast<UTa>(a) != b; |
| 179 |
} else { |
| 180 |
return (b < 0) ? true : a != static_cast<UTb>(b); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
template <typename Ta, typename Tb> |
| 185 |
constexpr bool cmp_less(Ta a, Tb b) noexcept { |
| 186 |
using UTa = typename std::make_unsigned<Ta>::type; |
| 187 |
using UTb = typename std::make_unsigned<Tb>::type; |
| 188 |
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) { |
| 189 |
return a < b; |
| 190 |
} else if constexpr (std::is_signed<Ta>::value) { |
| 191 |
return (a < 0) ? true : static_cast<UTa>(a) < b; |
| 192 |
} else { |
| 193 |
return (b < 0) ? false : a < static_cast<UTb>(b); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
template <typename Ta, typename Tb> |
| 198 |
constexpr bool cmp_greater(Ta a, Tb b) noexcept { |
| 199 |
using UTa = typename std::make_unsigned<Ta>::type; |
| 200 |
using UTb = typename std::make_unsigned<Tb>::type; |
| 201 |
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) { |
| 202 |
return a > b; |
| 203 |
} else if constexpr (std::is_signed<Ta>::value) { |
| 204 |
return (a < 0) ? false : static_cast<UTa>(a) > b; |
| 205 |
} else { |
| 206 |
return (b < 0) ? true : a > static_cast<UTb>(b); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
template <typename Ta, typename Tb> |
| 211 |
constexpr bool cmp_less_equal(Ta a, Tb b) noexcept { |
| 212 |
using UTa = typename std::make_unsigned<Ta>::type; |
| 213 |
using UTb = typename std::make_unsigned<Tb>::type; |
| 214 |
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) { |
| 215 |
return a <= b; |
| 216 |
} else if constexpr (std::is_signed<Ta>::value) { |
| 217 |
return (a < 0) ? true : static_cast<UTa>(a) <= b; |
| 218 |
} else { |
| 219 |
return (b < 0) ? false : a <= static_cast<UTb>(b); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
template <typename Ta, typename Tb> |
| 224 |
constexpr bool cmp_greater_equal(Ta a, Tb b) noexcept { |
| 225 |
using UTa = typename std::make_unsigned<Ta>::type; |
| 226 |
using UTb = typename std::make_unsigned<Tb>::type; |
| 227 |
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) { |
| 228 |
return a >= b; |
| 229 |
} else if constexpr (std::is_signed<Ta>::value) { |
| 230 |
return (a < 0) ? false : static_cast<UTa>(a) >= b; |
| 231 |
} else { |
| 232 |
return (b < 0) ? true : a >= static_cast<UTb>(b); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
#endif |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
#if MPT_CXX_AT_LEAST(23) && !MPT_LIBCXX_GNU_BEFORE(12) |
| 241 |
|
| 242 |
using std::unreachable; |
| 243 |
|
| 244 |
#else // !C++23 |
| 245 |
|
| 246 |
[[noreturn]] inline void unreachable() { |
| 247 |
#if MPT_COMPILER_MSVC |
| 248 |
__assume(false); |
| 249 |
#elif MPT_COMPILER_GCC || MPT_COMPILER_CLANG |
| 250 |
__builtin_unreachable(); |
| 251 |
#else |
| 252 |
std::terminate(); |
| 253 |
#endif |
| 254 |
} |
| 255 |
|
| 256 |
#endif // C++23 |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
} // namespace MPT_INLINE_NS |
| 261 |
} // namespace mpt |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
#endif // MPT_BASE_UTILITY_HPP |