view: use a temporary buffer for cruft::read
This commit is contained in:
parent
b9c0716c66
commit
a94ca358a5
21
view.hpp
21
view.hpp
@ -701,24 +701,17 @@ namespace cruft {
|
|||||||
ValueT
|
ValueT
|
||||||
read (view<WordT*> &buffer)
|
read (view<WordT*> &buffer)
|
||||||
{
|
{
|
||||||
// we disable the class-memaccess warning so that we can memcpy into
|
// We're going to use memcpy which requires that the type is
|
||||||
// types that we know are safe but the compiler will complain about.
|
// trivially copyable.
|
||||||
// this occurs commonly with oddly packed structures, eg anything
|
static_assert (std::is_trivially_copyable_v<ValueT>);
|
||||||
// that uses gnu::packed
|
|
||||||
#if defined(COMPILER_GCC)
|
|
||||||
#pragma GCC diagnostic push
|
|
||||||
#pragma GCC diagnostic ignored "-Wclass-memaccess"
|
|
||||||
#endif
|
|
||||||
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");
|
||||||
|
|
||||||
ValueT res;
|
std::aligned_storage_t<sizeof(ValueT),alignof(ValueT)> bytes;
|
||||||
memcpy (&res, buffer.data (), sizeof (ValueT));
|
memcpy (&bytes, buffer.data (), sizeof (ValueT));
|
||||||
buffer = buffer.consume (sizeof (ValueT) / sizeof (WordT));
|
buffer = buffer.consume (sizeof (ValueT) / sizeof (WordT));
|
||||||
return res;
|
return *reinterpret_cast<ValueT const*> (&bytes);
|
||||||
#if defined(COMPILER_GCC)
|
|
||||||
#pragma GCC diagnostic pop
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user