list/node: add value size and packing assertions

This commit is contained in:
Danny Robson 2019-09-12 13:34:18 +10:00
parent 8adac4604c
commit d9292cb407

View File

@ -119,6 +119,11 @@ namespace cruft::list::node {
disjoint<ValueT>*
next (disjoint<ValueT> *obj, disjoint<ValueT> *val)
{
static_assert (
sizeof (disjoint<ValueT>) >= sizeof (obj),
"There must be enough room to store a pointer in the node"
);
memcpy (&obj->bytes, &val, sizeof (val));
return val;
}
@ -134,14 +139,36 @@ namespace cruft::list::node {
}
/// Return a reference to the node's data
template <typename ValueT>
ValueT const&
data (disjoint<ValueT> const *obj)
{
static_assert (
sizeof (ValueT) == sizeof (*obj),
"Sizes must be equal to allow pointer indexing"
);
static_assert (
alignof (ValueT) == alignof (decltype (*obj)),
"Alignment must be equal to allow pointer indexing"
);
return *reinterpret_cast<ValueT const*> (&obj->bytes);
}
/// Return a reference to the node's data
template <typename ValueT>
ValueT&
data (disjoint<ValueT> *obj)
{
static_assert (
sizeof (ValueT) >= sizeof (*obj),
"ValueT < disjoint* prevents array indexing of nodes"
sizeof (ValueT) == sizeof (*obj),
"Sizes must be equal to allow pointer indexing"
);
static_assert (
alignof (ValueT) == alignof (decltype (*obj)),
"Alignment must be equal to allow pointer indexing"
);
return *reinterpret_cast<ValueT*> (&obj->bytes);