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

This commit is contained in:
Max Kellermann 2017-09-21 21:45:39 +02:00
parent e5c9b4cd75
commit 3e5ce3c92c
2 changed files with 22 additions and 6 deletions

View File

@ -124,6 +124,16 @@ struct ConstBuffer {
return ConstBuffer(nullptr, 0);
}
/**
* 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",
@ -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 {

View File

@ -118,6 +118,16 @@ struct WritableBuffer {
return { nullptr, 0 };
}
/**
* 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",
@ -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 {