view: add read_array

This commit is contained in:
Danny Robson 2022-01-25 15:54:16 +10:00
parent f2ce328e1e
commit af3188bd16

View File

@ -938,6 +938,30 @@ namespace cruft {
} }
/// Copy N elements of ValueT from a view over WordT into a supplied
/// std::array.
///
/// Throws if there is insufficient data in the src view.
template <
typename ValueT,
typename WordT,
std::size_t N,
typename = std::enable_if_t<sizeof (ValueT) % sizeof(WordT) == 0>
>
std::array<ValueT, N>&
read_array (view<WordT*> &src, std::array<ValueT, N> &dst)
{
static_assert (std::is_trivially_copyable_v<ValueT>);
if (unlikely (src.size () * sizeof (WordT) < sizeof (dst)))
throw std::runtime_error ("insufficient data for extraction");
memcpy (dst.data (), src.data(), N * sizeof (ValueT));
src = src.consume (sizeof (ValueT) / sizeof (WordT) * N);
return dst;
}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
template < template <
typename ValueT, typename ValueT,