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 { ...@@ -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" * Cast a ConstBuffer<void> to a ConstBuffer<T>. A "void"
* buffer records its size in bytes, and when casting to "T", * buffer records its size in bytes, and when casting to "T",
* the assertion below ensures that the size is a multiple of * the assertion below ensures that the size is a multiple of
...@@ -134,12 +144,10 @@ struct ConstBuffer { ...@@ -134,12 +144,10 @@ struct ConstBuffer {
constexpr constexpr
#endif #endif
static ConstBuffer<T> FromVoid(ConstBuffer<void> other) { static ConstBuffer<T> FromVoid(ConstBuffer<void> other) {
static_assert(sizeof(T) > 0, "Empty base type");
#ifndef NDEBUG #ifndef NDEBUG
assert(other.size % sizeof(T) == 0); assert(other.size % sizeof(T) == 0);
#endif #endif
return ConstBuffer<T>(pointer_type(other.data), return FromVoidFloor(other);
other.size / sizeof(T));
} }
constexpr ConstBuffer<void> ToVoid() const { constexpr ConstBuffer<void> ToVoid() const {
......
...@@ -119,6 +119,16 @@ struct WritableBuffer { ...@@ -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" * Cast a WritableBuffer<void> to a WritableBuffer<T>. A "void"
* buffer records its size in bytes, and when casting to "T", * buffer records its size in bytes, and when casting to "T",
* the assertion below ensures that the size is a multiple of * the assertion below ensures that the size is a multiple of
...@@ -128,12 +138,10 @@ struct WritableBuffer { ...@@ -128,12 +138,10 @@ struct WritableBuffer {
constexpr constexpr
#endif #endif
static WritableBuffer<T> FromVoid(WritableBuffer<void> other) { static WritableBuffer<T> FromVoid(WritableBuffer<void> other) {
static_assert(sizeof(T) > 0, "Empty base type");
#ifndef NDEBUG #ifndef NDEBUG
assert(other.size % sizeof(T) == 0); assert(other.size % sizeof(T) == 0);
#endif #endif
return WritableBuffer<T>(pointer_type(other.data), return FromVoidFloor(other);
other.size / sizeof(T));
} }
constexpr WritableBuffer<void> ToVoid() const { 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