Commit 3e5ce3c9 authored by Max Kellermann's avatar Max Kellermann

util/{Const,Writable}Buffer: add static method FromVoidFloor()

parent e5c9b4cd
......@@ -125,6 +125,16 @@ struct ConstBuffer {
}
/**
* Cast a ConstBuffer<void> to a ConstBuffer<T>, rounding down
* to the next multiple of T's size.
*/
static constexpr ConstBuffer<T> FromVoidFloor(ConstBuffer<void> other) {
static_assert(sizeof(T) > 0, "Empty base type");
return ConstBuffer<T>(pointer_type(other.data),
other.size / sizeof(T));
}
/**
* Cast a ConstBuffer<void> to a ConstBuffer<T>. A "void"
* buffer records its size in bytes, and when casting to "T",
* the assertion below ensures that the size is a multiple of
......@@ -134,12 +144,10 @@ struct ConstBuffer {
constexpr
#endif
static ConstBuffer<T> FromVoid(ConstBuffer<void> other) {
static_assert(sizeof(T) > 0, "Empty base type");
#ifndef NDEBUG
assert(other.size % sizeof(T) == 0);
#endif
return ConstBuffer<T>(pointer_type(other.data),
other.size / sizeof(T));
return FromVoidFloor(other);
}
constexpr ConstBuffer<void> ToVoid() const {
......
......@@ -119,6 +119,16 @@ struct WritableBuffer {
}
/**
* Cast a WritableBuffer<void> to a WritableBuffer<T>,
* rounding down to the next multiple of T's size.
*/
static constexpr WritableBuffer<T> FromVoidFloor(WritableBuffer<void> other) {
static_assert(sizeof(T) > 0, "Empty base type");
return WritableBuffer<T>(pointer_type(other.data),
other.size / sizeof(T));
}
/**
* Cast a WritableBuffer<void> to a WritableBuffer<T>. A "void"
* buffer records its size in bytes, and when casting to "T",
* the assertion below ensures that the size is a multiple of
......@@ -128,12 +138,10 @@ struct WritableBuffer {
constexpr
#endif
static WritableBuffer<T> FromVoid(WritableBuffer<void> other) {
static_assert(sizeof(T) > 0, "Empty base type");
#ifndef NDEBUG
assert(other.size % sizeof(T) == 0);
#endif
return WritableBuffer<T>(pointer_type(other.data),
other.size / sizeof(T));
return FromVoidFloor(other);
}
constexpr WritableBuffer<void> ToVoid() const {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment