diff --git a/view.hpp b/view.hpp index 6073a33a..6ce7de96 100644 --- a/view.hpp +++ b/view.hpp @@ -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 + > + std::array& + read_array (view &src, std::array &dst) + { + static_assert (std::is_trivially_copyable_v); + + 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 < typename ValueT,