view: allow multibyte casts in make_byte_view

This commit is contained in:
Danny Robson 2018-12-17 14:45:09 +11:00
parent cc2d11f102
commit 60bd71a57b

View File

@ -576,32 +576,31 @@ namespace cruft {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// calculates a byte oriented view over an arbitrary type /// Calculates a word oriented view over an arbitrary type
// ///
// useful for passing in memory structures to file descriptors and the /// Useful for passing in memory structures to file descriptors and the
// like. but the consequences of endian conversion is on the user... /// like. but the consequences of endian conversion is on the user...
// ///
// we have to be careful that rval-references and other temporaries aren't /// We have to be careful that rval-references and other temporaries aren't
// accepted in this signature. /// accepted in this signature.
template < template <
typename ByteT = std::byte, typename WordT = std::byte,
typename T typename T
> >
auto auto
make_byte_view (T &t) make_byte_view (T &t)
{ {
static_assert (std::is_integral_v<ByteT>); static_assert (sizeof (T) % sizeof (WordT) == 0);
static_assert (sizeof (T) % sizeof (ByteT) == 0);
using cursor_type = std::conditional_t< using cursor_type = std::conditional_t<
std::is_const_v<T>, std::is_const_v<T>,
ByteT const*, WordT const*,
ByteT* WordT*
>; >;
return view { return view {
cast::alignment<cursor_type> (&t), cast::alignment<cursor_type> (&t),
sizeof (T) / sizeof (ByteT) sizeof (T) / sizeof (WordT)
}; };
} }