view: add write consuming operations
This commit is contained in:
parent
37b48341bb
commit
56828688b5
50
view.hpp
50
view.hpp
@ -806,6 +806,8 @@ namespace cruft {
|
||||
///
|
||||
/// in contrast to 'extract' this will always copy the bytes out from the
|
||||
/// view, making the operation alignment safe.
|
||||
///
|
||||
/// It is safe to use this on untrusted input.
|
||||
template <
|
||||
typename ValueT,
|
||||
typename WordT,
|
||||
@ -828,6 +830,54 @@ namespace cruft {
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <
|
||||
typename ValueT,
|
||||
typename WordT,
|
||||
typename = std::enable_if_t<sizeof (ValueT) % sizeof (WordT) == 0>
|
||||
>
|
||||
view<WordT*>
|
||||
write [[nodiscard]] (view<WordT*> const &dst, ValueT const &src)
|
||||
{
|
||||
static_assert (std::is_trivially_copyable_v<ValueT>);
|
||||
|
||||
if (unlikely (sizeof (ValueT) > dst.size () * sizeof (WordT)))
|
||||
throw std::runtime_error ("insufficient data for extraction");
|
||||
|
||||
memcpy (dst.data (), &src, sizeof (ValueT));
|
||||
return {
|
||||
dst.begin () + sizeof (ValueT) / sizeof (WordT),
|
||||
dst.end (),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
template <
|
||||
typename ValueT,
|
||||
typename WordT,
|
||||
typename = std::enable_if_t<sizeof (ValueT) % sizeof (WordT) == 0>
|
||||
>
|
||||
view<WordT*>
|
||||
write [[nodiscard]] (view<WordT*> const &dst, cruft::view<ValueT const*> src)
|
||||
{
|
||||
static_assert (std::is_trivially_copyable_v<ValueT>);
|
||||
|
||||
if (unlikely (src.size () * sizeof (ValueT) > dst.size () * sizeof (WordT)))
|
||||
throw std::runtime_error ("insufficient data for extraction");
|
||||
|
||||
memcpy (
|
||||
dst.data (),
|
||||
src.data (),
|
||||
src.size () * sizeof (ValueT)
|
||||
);
|
||||
|
||||
return {
|
||||
dst.begin () + (sizeof (ValueT) / sizeof (WordT)) * src.size (),
|
||||
dst.end (),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
/// Tests whether an iterator falls within a given view.
|
||||
template <typename IteratorT>
|
||||
|
Loading…
Reference in New Issue
Block a user