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 <
typename ValueT,
typename WordT,
// Only allow calls if the value is a multiple of the word size. It's
// useful to allow non-unit words for areas like TCP/IP which tend to
// operate on u16 words.
typename = std::enable_if_t<sizeof (ValueT) % sizeof (WordT) == 0>
typename = std::enable_if_t<
// Only allow calls if the value is a multiple of the word size.
// It's useful to allow non-unit words for areas like TCP/IP which
// 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&
extract (view<WordT const*> &buffer)
ValueT&
extract (view<WordT*> &buffer)
{
if (unlikely (sizeof (ValueT) > buffer.size () * sizeof (WordT)))
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));
return *ptr;
}