view: allow extract with mutable pointers

This commit is contained in:
Danny Robson 2020-01-01 12:38:22 +11:00
parent 8da3cc3293
commit f404acfda0

View File

@ -725,18 +725,23 @@ namespace cruft {
template < template <
typename ValueT, typename ValueT,
typename WordT, typename WordT,
// Only allow calls if the value is a multiple of the word size. It's typename = std::enable_if_t<
// useful to allow non-unit words for areas like TCP/IP which tend to // Only allow calls if the value is a multiple of the word size.
// operate on u16 words. // It's useful to allow non-unit words for areas like TCP/IP which
typename = std::enable_if_t<sizeof (ValueT) % sizeof (WordT) == 0> // tend to include protocols that utilise u16 words.
sizeof (ValueT) % sizeof (WordT) == 0 &&
// If WordT is const then ValueT must be const. Otherwise the user
// is free to choose.
std::is_const_v<WordT> ? std::is_const_v<ValueT> : true
>
> >
ValueT const& ValueT&
extract (view<WordT const*> &buffer) extract (view<WordT*> &buffer)
{ {
if (unlikely (sizeof (ValueT) > buffer.size () * sizeof (WordT))) if (unlikely (sizeof (ValueT) > buffer.size () * sizeof (WordT)))
throw std::runtime_error ("insufficient data for extraction"); throw std::runtime_error ("insufficient data for extraction");
auto const ptr = cast::alignment<ValueT const*> (buffer.data ()); auto ptr = cast::alignment<ValueT*> (buffer.data ());
buffer = buffer.consume (sizeof (ValueT) / sizeof (WordT)); buffer = buffer.consume (sizeof (ValueT) / sizeof (WordT));
return *ptr; return *ptr;
} }