mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-11-22 01:12:28 +00:00
Common/BitUtils: Add overloads of AsU8Span/AsWritableU8Span that handle conversions from contiguous ranges.
This commit is contained in:
parent
405baed805
commit
b98acb9a37
@ -255,15 +255,33 @@ T ExpandValue(T value, size_t left_shift_amount)
|
||||
(T(-ExtractBit<0>(value)) >> (BitSize<T>() - left_shift_amount));
|
||||
}
|
||||
|
||||
// Convert a contiguous range of a trivially-copyable type to a `span<const u8>`
|
||||
template <std::ranges::contiguous_range T>
|
||||
requires(std::is_trivially_copyable_v<std::ranges::range_value_t<T>>)
|
||||
constexpr auto AsU8Span(const T& range)
|
||||
{
|
||||
return std::span{reinterpret_cast<const u8*>(range.data()), range.size() * sizeof(*range.data())};
|
||||
}
|
||||
|
||||
// Convert a contiguous range of a non-const trivially-copyable type to a `span<u8>`
|
||||
template <std::ranges::contiguous_range T>
|
||||
requires(std::is_trivially_copyable_v<std::ranges::range_value_t<T>>)
|
||||
constexpr auto AsWritableU8Span(T& range)
|
||||
{
|
||||
return std::span{reinterpret_cast<u8*>(range.data()), range.size() * sizeof(*range.data())};
|
||||
}
|
||||
|
||||
// Convert a trivially-copyable object to a `span<const u8>`
|
||||
template <typename T>
|
||||
requires(std::is_trivially_copyable_v<T>)
|
||||
requires(!std::ranges::contiguous_range<T> && std::is_trivially_copyable_v<T>)
|
||||
constexpr auto AsU8Span(const T& obj)
|
||||
{
|
||||
return std::span{reinterpret_cast<const u8*>(std::addressof(obj)), sizeof(obj)};
|
||||
}
|
||||
|
||||
// Convert a non-const trivially-copyable object to a `span<u8>`
|
||||
template <typename T>
|
||||
requires(std::is_trivially_copyable_v<T>)
|
||||
requires(!std::ranges::contiguous_range<T> && std::is_trivially_copyable_v<T>)
|
||||
constexpr auto AsWritableU8Span(T& obj)
|
||||
{
|
||||
return std::span{reinterpret_cast<u8*>(std::addressof(obj)), sizeof(obj)};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user