util/{Const,Writable}Buffer: enable constexpr on more methods

This commit is contained in:
Max Kellermann
2021-01-15 15:19:06 +01:00
committed by Max Kellermann
parent c44a7b2705
commit 17858143b3
2 changed files with 16 additions and 38 deletions

View File

@@ -137,10 +137,7 @@ struct ConstBuffer {
* 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 constexpr static ConstBuffer<T> FromVoid(ConstBuffer<void> other) noexcept {
constexpr
#endif
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 #ifndef NDEBUG
assert(other.size % sizeof(T) == 0); assert(other.size % sizeof(T) == 0);
@@ -170,8 +167,7 @@ struct ConstBuffer {
} }
template<typename U> template<typename U>
gcc_pure constexpr bool Contains(U &&u) const noexcept {
bool Contains(U &&u) const noexcept {
for (const auto &i : *this) for (const auto &i : *this)
if (u == i) if (u == i)
return true; return true;
@@ -195,10 +191,7 @@ struct ConstBuffer {
return data + size; return data + size;
} }
#ifdef NDEBUG constexpr reference operator[](size_type i) const noexcept {
constexpr
#endif
reference operator[](size_type i) const noexcept {
#ifndef NDEBUG #ifndef NDEBUG
assert(i < size); assert(i < size);
#endif #endif
@@ -210,10 +203,7 @@ struct ConstBuffer {
* Returns a reference to the first element. Buffer must not * Returns a reference to the first element. Buffer must not
* be empty. * be empty.
*/ */
#ifdef NDEBUG constexpr reference front() const noexcept {
constexpr
#endif
reference front() const noexcept {
#ifndef NDEBUG #ifndef NDEBUG
assert(!empty()); assert(!empty());
#endif #endif
@@ -224,10 +214,7 @@ struct ConstBuffer {
* Returns a reference to the last element. Buffer must not * Returns a reference to the last element. Buffer must not
* be empty. * be empty.
*/ */
#ifdef NDEBUG constexpr reference back() const noexcept {
constexpr
#endif
reference back() const noexcept {
#ifndef NDEBUG #ifndef NDEBUG
assert(!empty()); assert(!empty());
#endif #endif
@@ -238,7 +225,7 @@ struct ConstBuffer {
* Remove the first element (by moving the head pointer, does * Remove the first element (by moving the head pointer, does
* not actually modify the buffer). Buffer must not be empty. * not actually modify the buffer). Buffer must not be empty.
*/ */
void pop_front() noexcept { constexpr void pop_front() noexcept {
#ifndef NDEBUG #ifndef NDEBUG
assert(!empty()); assert(!empty());
#endif #endif
@@ -251,7 +238,7 @@ struct ConstBuffer {
* Remove the last element (by moving the tail pointer, does * Remove the last element (by moving the tail pointer, does
* not actually modify the buffer). Buffer must not be empty. * not actually modify the buffer). Buffer must not be empty.
*/ */
void pop_back() noexcept { constexpr void pop_back() noexcept {
#ifndef NDEBUG #ifndef NDEBUG
assert(!empty()); assert(!empty());
#endif #endif
@@ -263,13 +250,13 @@ struct ConstBuffer {
* Remove the first element and return a reference to it. * Remove the first element and return a reference to it.
* Buffer must not be empty. * Buffer must not be empty.
*/ */
reference shift() noexcept { constexpr reference shift() noexcept {
reference result = front(); reference result = front();
pop_front(); pop_front();
return result; return result;
} }
void skip_front(size_type n) noexcept { constexpr void skip_front(size_type n) noexcept {
#ifndef NDEBUG #ifndef NDEBUG
assert(size >= n); assert(size >= n);
#endif #endif

View File

@@ -188,10 +188,7 @@ struct WritableBuffer {
return data + size; return data + size;
} }
#ifdef NDEBUG constexpr reference operator[](size_type i) const noexcept {
constexpr
#endif
reference operator[](size_type i) const noexcept {
#ifndef NDEBUG #ifndef NDEBUG
assert(i < size); assert(i < size);
#endif #endif
@@ -203,10 +200,7 @@ struct WritableBuffer {
* Returns a reference to the first element. Buffer must not * Returns a reference to the first element. Buffer must not
* be empty. * be empty.
*/ */
#ifdef NDEBUG constexpr reference front() const noexcept {
constexpr
#endif
reference front() const noexcept {
#ifndef NDEBUG #ifndef NDEBUG
assert(!empty()); assert(!empty());
#endif #endif
@@ -217,10 +211,7 @@ struct WritableBuffer {
* Returns a reference to the last element. Buffer must not * Returns a reference to the last element. Buffer must not
* be empty. * be empty.
*/ */
#ifdef NDEBUG constexpr reference back() const noexcept {
constexpr
#endif
reference back() const noexcept {
#ifndef NDEBUG #ifndef NDEBUG
assert(!empty()); assert(!empty());
#endif #endif
@@ -231,7 +222,7 @@ struct WritableBuffer {
* Remove the first element (by moving the head pointer, does * Remove the first element (by moving the head pointer, does
* not actually modify the buffer). Buffer must not be empty. * not actually modify the buffer). Buffer must not be empty.
*/ */
void pop_front() noexcept { constexpr void pop_front() noexcept {
assert(!empty()); assert(!empty());
++data; ++data;
@@ -242,7 +233,7 @@ struct WritableBuffer {
* Remove the last element (by moving the tail pointer, does * Remove the last element (by moving the tail pointer, does
* not actually modify the buffer). Buffer must not be empty. * not actually modify the buffer). Buffer must not be empty.
*/ */
void pop_back() noexcept { constexpr void pop_back() noexcept {
assert(!empty()); assert(!empty());
--size; --size;
@@ -252,13 +243,13 @@ struct WritableBuffer {
* Remove the first element and return a reference to it. * Remove the first element and return a reference to it.
* Buffer must not be empty. * Buffer must not be empty.
*/ */
reference shift() noexcept { constexpr reference shift() noexcept {
reference result = front(); reference result = front();
pop_front(); pop_front();
return result; return result;
} }
void skip_front(size_type n) noexcept { constexpr void skip_front(size_type n) noexcept {
#ifndef NDEBUG #ifndef NDEBUG
assert(size >= n); assert(size >= n);
#endif #endif