2013-10-15 10:28:52 +02:00
|
|
|
/*
|
2020-01-03 15:38:02 +01:00
|
|
|
* Copyright 2013-2020 Max Kellermann <max.kellermann@gmail.com>
|
2013-10-15 10:28:52 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2017-09-13 10:35:11 +02:00
|
|
|
#ifndef WRITABLE_BUFFER_HXX
|
|
|
|
#define WRITABLE_BUFFER_HXX
|
2013-10-15 10:28:52 +02:00
|
|
|
|
2018-08-20 15:36:50 +02:00
|
|
|
#include "ConstBuffer.hxx"
|
2013-10-15 10:28:52 +02:00
|
|
|
#include "Compiler.h"
|
|
|
|
|
2013-12-15 23:01:01 +01:00
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2013-12-15 23:01:01 +01:00
|
|
|
#endif
|
2013-10-15 10:28:52 +02:00
|
|
|
|
2014-03-01 07:36:59 +01:00
|
|
|
template<typename T>
|
|
|
|
struct WritableBuffer;
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct WritableBuffer<void> {
|
2020-01-03 15:38:02 +01:00
|
|
|
typedef std::size_t size_type;
|
2017-07-03 17:00:53 +02:00
|
|
|
typedef void value_type;
|
2020-01-03 15:49:29 +01:00
|
|
|
typedef void *pointer;
|
|
|
|
typedef const void *const_pointer;
|
|
|
|
typedef pointer iterator;
|
|
|
|
typedef const_pointer const_iterator;
|
2014-03-01 07:36:59 +01:00
|
|
|
|
2020-01-03 15:49:29 +01:00
|
|
|
pointer data;
|
2014-03-01 07:36:59 +01:00
|
|
|
size_type size;
|
|
|
|
|
|
|
|
WritableBuffer() = default;
|
|
|
|
|
2020-01-03 15:39:19 +01:00
|
|
|
constexpr WritableBuffer(std::nullptr_t) noexcept
|
|
|
|
:data(nullptr), size(0) {}
|
2014-03-01 07:36:59 +01:00
|
|
|
|
2020-01-03 15:49:29 +01:00
|
|
|
constexpr WritableBuffer(pointer _data, size_type _size) noexcept
|
2014-03-01 07:36:59 +01:00
|
|
|
:data(_data), size(_size) {}
|
|
|
|
|
2018-08-20 15:36:50 +02:00
|
|
|
constexpr operator ConstBuffer<void>() const noexcept {
|
|
|
|
return {data, size};
|
|
|
|
}
|
|
|
|
|
2020-01-03 15:39:19 +01:00
|
|
|
constexpr bool IsNull() const noexcept {
|
2014-03-01 07:36:59 +01:00
|
|
|
return data == nullptr;
|
|
|
|
}
|
|
|
|
|
2020-01-03 15:39:19 +01:00
|
|
|
constexpr bool operator==(std::nullptr_t) const noexcept {
|
2017-09-21 20:34:36 +02:00
|
|
|
return data == nullptr;
|
|
|
|
}
|
|
|
|
|
2020-01-03 15:39:19 +01:00
|
|
|
constexpr bool operator!=(std::nullptr_t) const noexcept {
|
2017-09-21 20:34:36 +02:00
|
|
|
return data != nullptr;
|
|
|
|
}
|
|
|
|
|
2020-01-03 15:39:19 +01:00
|
|
|
constexpr bool empty() const noexcept {
|
2014-03-01 07:36:59 +01:00
|
|
|
return size == 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-15 10:28:52 +02:00
|
|
|
/**
|
|
|
|
* A reference to a memory area that is writable.
|
|
|
|
*
|
|
|
|
* @see ConstBuffer
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
struct WritableBuffer {
|
2020-01-03 15:38:02 +01:00
|
|
|
typedef std::size_t size_type;
|
2017-07-03 17:00:53 +02:00
|
|
|
typedef T value_type;
|
2020-01-12 14:39:54 +01:00
|
|
|
typedef T &reference;
|
|
|
|
typedef const T &const_reference;
|
2020-01-03 15:49:29 +01:00
|
|
|
typedef T *pointer;
|
|
|
|
typedef const T *const_pointer;
|
|
|
|
typedef pointer iterator;
|
|
|
|
typedef const_pointer const_iterator;
|
2013-10-15 10:28:52 +02:00
|
|
|
|
2020-01-03 15:49:29 +01:00
|
|
|
pointer data;
|
2013-12-15 22:58:24 +01:00
|
|
|
size_type size;
|
2013-10-15 10:28:52 +02:00
|
|
|
|
2013-12-15 22:58:24 +01:00
|
|
|
WritableBuffer() = default;
|
2013-10-15 10:28:52 +02:00
|
|
|
|
2020-01-03 15:39:19 +01:00
|
|
|
constexpr WritableBuffer(std::nullptr_t) noexcept
|
|
|
|
:data(nullptr), size(0) {}
|
2013-12-15 23:01:01 +01:00
|
|
|
|
2020-01-03 15:49:29 +01:00
|
|
|
constexpr WritableBuffer(pointer _data, size_type _size) noexcept
|
2013-12-15 22:58:24 +01:00
|
|
|
:data(_data), size(_size) {}
|
2013-10-15 10:28:52 +02:00
|
|
|
|
2020-01-03 15:49:29 +01:00
|
|
|
constexpr WritableBuffer(pointer _data, pointer _end) noexcept
|
2017-11-15 22:03:44 +01:00
|
|
|
:data(_data), size(_end - _data) {}
|
|
|
|
|
2017-04-12 13:09:11 +02:00
|
|
|
/**
|
|
|
|
* Convert array to WritableBuffer instance.
|
|
|
|
*/
|
|
|
|
template<size_type _size>
|
2020-01-03 15:39:19 +01:00
|
|
|
constexpr WritableBuffer(T (&_data)[_size]) noexcept
|
2017-04-12 13:09:11 +02:00
|
|
|
:data(_data), size(_size) {}
|
|
|
|
|
2018-08-20 15:36:50 +02:00
|
|
|
constexpr operator ConstBuffer<T>() const noexcept {
|
|
|
|
return {data, size};
|
|
|
|
}
|
|
|
|
|
2017-09-21 21:45:39 +02:00
|
|
|
/**
|
|
|
|
* Cast a WritableBuffer<void> to a WritableBuffer<T>,
|
|
|
|
* rounding down to the next multiple of T's size.
|
|
|
|
*/
|
2020-01-03 15:39:19 +01:00
|
|
|
static constexpr WritableBuffer<T> FromVoidFloor(WritableBuffer<void> other) noexcept {
|
2017-09-21 21:45:39 +02:00
|
|
|
static_assert(sizeof(T) > 0, "Empty base type");
|
2020-01-03 15:49:29 +01:00
|
|
|
return WritableBuffer<T>(pointer(other.data),
|
2017-09-21 21:45:39 +02:00
|
|
|
other.size / sizeof(T));
|
|
|
|
}
|
|
|
|
|
2013-12-15 23:01:01 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* sizeof(T).
|
|
|
|
*/
|
|
|
|
#ifdef NDEBUG
|
|
|
|
constexpr
|
|
|
|
#endif
|
2020-01-03 15:39:19 +01:00
|
|
|
static WritableBuffer<T> FromVoid(WritableBuffer<void> other) noexcept {
|
2017-11-10 19:43:17 +01:00
|
|
|
static_assert(sizeof(T) > 0, "Empty base type");
|
2013-12-15 23:01:01 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(other.size % sizeof(T) == 0);
|
|
|
|
#endif
|
2017-09-21 21:45:39 +02:00
|
|
|
return FromVoidFloor(other);
|
2013-12-15 23:01:01 +01:00
|
|
|
}
|
|
|
|
|
2020-01-03 15:39:19 +01:00
|
|
|
constexpr WritableBuffer<void> ToVoid() const noexcept {
|
2013-12-15 23:01:01 +01:00
|
|
|
static_assert(sizeof(T) > 0, "Empty base type");
|
|
|
|
return WritableBuffer<void>(data, size * sizeof(T));
|
|
|
|
}
|
|
|
|
|
2020-01-03 15:39:19 +01:00
|
|
|
constexpr bool IsNull() const noexcept {
|
2013-12-15 22:58:24 +01:00
|
|
|
return data == nullptr;
|
|
|
|
}
|
2013-10-15 10:28:52 +02:00
|
|
|
|
2020-01-03 15:39:19 +01:00
|
|
|
constexpr bool operator==(std::nullptr_t) const noexcept {
|
2017-09-21 20:34:36 +02:00
|
|
|
return data == nullptr;
|
|
|
|
}
|
|
|
|
|
2020-01-03 15:39:19 +01:00
|
|
|
constexpr bool operator!=(std::nullptr_t) const noexcept {
|
2017-09-21 20:34:36 +02:00
|
|
|
return data != nullptr;
|
|
|
|
}
|
|
|
|
|
2020-01-03 15:39:19 +01:00
|
|
|
constexpr bool empty() const noexcept {
|
2013-12-15 22:58:24 +01:00
|
|
|
return size == 0;
|
|
|
|
}
|
2013-10-15 10:28:52 +02:00
|
|
|
|
2020-01-03 15:39:19 +01:00
|
|
|
constexpr iterator begin() const noexcept {
|
2013-12-15 22:58:24 +01:00
|
|
|
return data;
|
|
|
|
}
|
2013-10-15 10:28:52 +02:00
|
|
|
|
2020-01-03 15:39:19 +01:00
|
|
|
constexpr iterator end() const noexcept {
|
2013-12-15 22:58:24 +01:00
|
|
|
return data + size;
|
|
|
|
}
|
2013-10-15 10:28:52 +02:00
|
|
|
|
2020-01-03 15:39:19 +01:00
|
|
|
constexpr const_iterator cbegin() const noexcept {
|
2013-12-15 22:58:24 +01:00
|
|
|
return data;
|
|
|
|
}
|
2013-10-15 10:28:52 +02:00
|
|
|
|
2020-01-03 15:39:19 +01:00
|
|
|
constexpr const_iterator cend() const noexcept {
|
2013-12-15 22:58:24 +01:00
|
|
|
return data + size;
|
|
|
|
}
|
2014-03-01 07:25:17 +01:00
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
constexpr
|
|
|
|
#endif
|
2020-01-12 14:39:54 +01:00
|
|
|
reference operator[](size_type i) const noexcept {
|
2014-03-01 07:25:17 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(i < size);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return data[i];
|
|
|
|
}
|
2014-04-24 09:46:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a reference to the first element. Buffer must not
|
|
|
|
* be empty.
|
|
|
|
*/
|
|
|
|
#ifdef NDEBUG
|
|
|
|
constexpr
|
|
|
|
#endif
|
2020-01-12 14:39:54 +01:00
|
|
|
reference front() const noexcept {
|
2014-04-24 09:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2017-11-10 19:24:33 +01:00
|
|
|
assert(!empty());
|
2014-04-24 09:46:52 +02:00
|
|
|
#endif
|
|
|
|
return data[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a reference to the last element. Buffer must not
|
|
|
|
* be empty.
|
|
|
|
*/
|
|
|
|
#ifdef NDEBUG
|
|
|
|
constexpr
|
|
|
|
#endif
|
2020-01-12 14:39:54 +01:00
|
|
|
reference back() const noexcept {
|
2014-04-24 09:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2017-11-10 19:24:33 +01:00
|
|
|
assert(!empty());
|
2014-04-24 09:46:52 +02:00
|
|
|
#endif
|
|
|
|
return data[size - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the first element (by moving the head pointer, does
|
|
|
|
* not actually modify the buffer). Buffer must not be empty.
|
|
|
|
*/
|
2020-01-03 15:39:19 +01:00
|
|
|
void pop_front() noexcept {
|
2017-11-10 19:24:33 +01:00
|
|
|
assert(!empty());
|
2014-04-24 09:46:52 +02:00
|
|
|
|
|
|
|
++data;
|
|
|
|
--size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the last element (by moving the tail pointer, does
|
|
|
|
* not actually modify the buffer). Buffer must not be empty.
|
|
|
|
*/
|
2020-01-03 15:39:19 +01:00
|
|
|
void pop_back() noexcept {
|
2017-11-10 19:24:33 +01:00
|
|
|
assert(!empty());
|
2014-04-24 09:46:52 +02:00
|
|
|
|
|
|
|
--size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the first element and return a reference to it.
|
|
|
|
* Buffer must not be empty.
|
|
|
|
*/
|
2020-01-12 14:39:54 +01:00
|
|
|
reference shift() noexcept {
|
|
|
|
reference result = front();
|
2014-04-24 09:46:52 +02:00
|
|
|
pop_front();
|
|
|
|
return result;
|
|
|
|
}
|
2016-08-16 08:46:30 +02:00
|
|
|
|
2020-01-03 15:39:19 +01:00
|
|
|
void skip_front(size_type n) noexcept {
|
2016-08-16 08:46:30 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(size >= n);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
data += n;
|
|
|
|
size -= n;
|
|
|
|
}
|
2017-07-05 17:10:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the front pointer to the given address, and adjust the
|
|
|
|
* size attribute to retain the old end address.
|
|
|
|
*/
|
2020-01-03 15:49:29 +01:00
|
|
|
void MoveFront(pointer new_data) noexcept {
|
2017-07-05 17:10:59 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(IsNull() == (new_data == nullptr));
|
|
|
|
assert(new_data <= end());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
size = end() - new_data;
|
|
|
|
data = new_data;
|
|
|
|
}
|
2018-08-21 08:20:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the end pointer to the given address (by adjusting the
|
|
|
|
* size).
|
|
|
|
*/
|
2020-01-03 15:49:29 +01:00
|
|
|
void SetEnd(pointer new_end) noexcept {
|
2018-08-21 08:20:17 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(IsNull() == (new_end == nullptr));
|
|
|
|
assert(new_end >= begin());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
size = new_end - data;
|
|
|
|
}
|
2013-10-15 10:28:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|