util/{Const,Writable}Buffer: always enable assertions

This commit is contained in:
Max Kellermann 2021-01-15 15:26:03 +01:00 committed by Max Kellermann
parent 17858143b3
commit 5e67443a1a
2 changed files with 3 additions and 44 deletions

View File

@ -32,11 +32,8 @@
#include "Compiler.h" #include "Compiler.h"
#include <cstddef>
#ifndef NDEBUG
#include <cassert> #include <cassert>
#endif #include <cstddef>
template<typename T> template<typename T>
struct ConstBuffer; struct ConstBuffer;
@ -139,9 +136,7 @@ struct ConstBuffer {
*/ */
constexpr static ConstBuffer<T> FromVoid(ConstBuffer<void> other) noexcept { constexpr static ConstBuffer<T> FromVoid(ConstBuffer<void> other) noexcept {
static_assert(sizeof(T) > 0, "Empty base type"); static_assert(sizeof(T) > 0, "Empty base type");
#ifndef NDEBUG
assert(other.size % sizeof(T) == 0); assert(other.size % sizeof(T) == 0);
#endif
return FromVoidFloor(other); return FromVoidFloor(other);
} }
@ -192,9 +187,7 @@ struct ConstBuffer {
} }
constexpr reference operator[](size_type i) const noexcept { constexpr reference operator[](size_type i) const noexcept {
#ifndef NDEBUG
assert(i < size); assert(i < size);
#endif
return data[i]; return data[i];
} }
@ -204,9 +197,7 @@ struct ConstBuffer {
* be empty. * be empty.
*/ */
constexpr reference front() const noexcept { constexpr reference front() const noexcept {
#ifndef NDEBUG
assert(!empty()); assert(!empty());
#endif
return data[0]; return data[0];
} }
@ -215,9 +206,7 @@ struct ConstBuffer {
* be empty. * be empty.
*/ */
constexpr reference back() const noexcept { constexpr reference back() const noexcept {
#ifndef NDEBUG
assert(!empty()); assert(!empty());
#endif
return data[size - 1]; return data[size - 1];
} }
@ -226,9 +215,7 @@ struct ConstBuffer {
* not actually modify the buffer). Buffer must not be empty. * not actually modify the buffer). Buffer must not be empty.
*/ */
constexpr void pop_front() noexcept { constexpr void pop_front() noexcept {
#ifndef NDEBUG
assert(!empty()); assert(!empty());
#endif
++data; ++data;
--size; --size;
@ -239,9 +226,7 @@ struct ConstBuffer {
* not actually modify the buffer). Buffer must not be empty. * not actually modify the buffer). Buffer must not be empty.
*/ */
constexpr void pop_back() noexcept { constexpr void pop_back() noexcept {
#ifndef NDEBUG
assert(!empty()); assert(!empty());
#endif
--size; --size;
} }
@ -257,9 +242,7 @@ struct ConstBuffer {
} }
constexpr void skip_front(size_type n) noexcept { constexpr void skip_front(size_type n) noexcept {
#ifndef NDEBUG
assert(size >= n); assert(size >= n);
#endif
data += n; data += n;
size -= n; size -= n;
@ -270,10 +253,8 @@ struct ConstBuffer {
* size attribute to retain the old end address. * size attribute to retain the old end address.
*/ */
void MoveFront(pointer new_data) noexcept { void MoveFront(pointer new_data) noexcept {
#ifndef NDEBUG
assert(IsNull() == (new_data == nullptr)); assert(IsNull() == (new_data == nullptr));
assert(new_data <= end()); assert(new_data <= end());
#endif
size = end() - new_data; size = end() - new_data;
data = new_data; data = new_data;
@ -284,10 +265,8 @@ struct ConstBuffer {
* size). * size).
*/ */
void SetEnd(pointer new_end) noexcept { void SetEnd(pointer new_end) noexcept {
#ifndef NDEBUG
assert(IsNull() == (new_end == nullptr)); assert(IsNull() == (new_end == nullptr));
assert(new_end >= begin()); assert(new_end >= begin());
#endif
size = new_end - data; size = new_end - data;
} }

View File

@ -33,11 +33,8 @@
#include "ConstBuffer.hxx" #include "ConstBuffer.hxx"
#include "Compiler.h" #include "Compiler.h"
#include <cstddef>
#ifndef NDEBUG
#include <cassert> #include <cassert>
#endif #include <cstddef>
template<typename T> template<typename T>
struct WritableBuffer; struct WritableBuffer;
@ -140,14 +137,9 @@ struct WritableBuffer {
* the assertion below ensures that the size is a multiple of * the assertion below ensures that the size is a multiple of
* sizeof(T). * sizeof(T).
*/ */
#ifdef NDEBUG static constexpr WritableBuffer<T> FromVoid(WritableBuffer<void> other) noexcept {
constexpr
#endif
static WritableBuffer<T> FromVoid(WritableBuffer<void> other) noexcept {
static_assert(sizeof(T) > 0, "Empty base type"); static_assert(sizeof(T) > 0, "Empty base type");
#ifndef NDEBUG
assert(other.size % sizeof(T) == 0); assert(other.size % sizeof(T) == 0);
#endif
return FromVoidFloor(other); return FromVoidFloor(other);
} }
@ -189,9 +181,7 @@ struct WritableBuffer {
} }
constexpr reference operator[](size_type i) const noexcept { constexpr reference operator[](size_type i) const noexcept {
#ifndef NDEBUG
assert(i < size); assert(i < size);
#endif
return data[i]; return data[i];
} }
@ -201,9 +191,7 @@ struct WritableBuffer {
* be empty. * be empty.
*/ */
constexpr reference front() const noexcept { constexpr reference front() const noexcept {
#ifndef NDEBUG
assert(!empty()); assert(!empty());
#endif
return data[0]; return data[0];
} }
@ -212,9 +200,7 @@ struct WritableBuffer {
* be empty. * be empty.
*/ */
constexpr reference back() const noexcept { constexpr reference back() const noexcept {
#ifndef NDEBUG
assert(!empty()); assert(!empty());
#endif
return data[size - 1]; return data[size - 1];
} }
@ -250,9 +236,7 @@ struct WritableBuffer {
} }
constexpr void skip_front(size_type n) noexcept { constexpr void skip_front(size_type n) noexcept {
#ifndef NDEBUG
assert(size >= n); assert(size >= n);
#endif
data += n; data += n;
size -= n; size -= n;
@ -263,10 +247,8 @@ struct WritableBuffer {
* size attribute to retain the old end address. * size attribute to retain the old end address.
*/ */
void MoveFront(pointer new_data) noexcept { void MoveFront(pointer new_data) noexcept {
#ifndef NDEBUG
assert(IsNull() == (new_data == nullptr)); assert(IsNull() == (new_data == nullptr));
assert(new_data <= end()); assert(new_data <= end());
#endif
size = end() - new_data; size = end() - new_data;
data = new_data; data = new_data;
@ -277,10 +259,8 @@ struct WritableBuffer {
* size). * size).
*/ */
void SetEnd(pointer new_end) noexcept { void SetEnd(pointer new_end) noexcept {
#ifndef NDEBUG
assert(IsNull() == (new_end == nullptr)); assert(IsNull() == (new_end == nullptr));
assert(new_end >= begin()); assert(new_end >= begin());
#endif
size = new_end - data; size = new_end - data;
} }