WritableBuffer.hxx 6.67 KB
Newer Older
1
/*
2
 * Copyright 2013-2021 Max Kellermann <max.kellermann@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * 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.
 */

30 31
#ifndef WRITABLE_BUFFER_HXX
#define WRITABLE_BUFFER_HXX
32

33
#include "ConstBuffer.hxx"
34

35
#include <cassert>
36
#include <cstddef>
37

38 39 40 41 42
template<typename T>
struct WritableBuffer;

template<>
struct WritableBuffer<void> {
43
	typedef std::size_t size_type;
44 45 46 47 48
	using value_type = void;
	using pointer = void *;
	using const_pointer = const void *;
	using iterator = pointer;
	using const_iterator = const_pointer;
49

50
	pointer data;
51 52 53 54
	size_type size;

	WritableBuffer() = default;

55 56
	constexpr WritableBuffer(std::nullptr_t) noexcept
		:data(nullptr), size(0) {}
57

58
	constexpr WritableBuffer(pointer _data, size_type _size) noexcept
59 60
		:data(_data), size(_size) {}

61 62 63 64 65 66 67 68
	constexpr static WritableBuffer<void> FromVoid(WritableBuffer<void> other) noexcept {
		return other;
	}

	constexpr WritableBuffer<void> ToVoid() const noexcept {
		return *this;
	}

69 70 71 72
	constexpr operator ConstBuffer<void>() const noexcept {
		return {data, size};
	}

73
	constexpr bool IsNull() const noexcept {
74 75 76
		return data == nullptr;
	}

77
	constexpr bool operator==(std::nullptr_t) const noexcept {
78 79 80
		return data == nullptr;
	}

81
	constexpr bool operator!=(std::nullptr_t) const noexcept {
82 83 84
		return data != nullptr;
	}

85
	constexpr bool empty() const noexcept {
86 87 88 89
		return size == 0;
	}
};

90 91 92 93 94 95 96
/**
 * A reference to a memory area that is writable.
 *
 * @see ConstBuffer
 */
template<typename T>
struct WritableBuffer {
97 98 99 100 101 102 103 104
	using size_type = std::size_t;
	using value_type = T;
	using reference = T &;
	using const_reference = const T &;
	using pointer = T *;
	using const_pointer = const T *;
	using iterator = pointer;
	using const_iterator = const_pointer;
105

106
	pointer data;
107
	size_type size;
108

109
	WritableBuffer() = default;
110

111 112
	constexpr WritableBuffer(std::nullptr_t) noexcept
		:data(nullptr), size(0) {}
113

114
	constexpr WritableBuffer(pointer _data, size_type _size) noexcept
115
		:data(_data), size(_size) {}
116

117
	constexpr WritableBuffer(pointer _data, pointer _end) noexcept
118 119
		:data(_data), size(_end - _data) {}

120 121 122 123
	/**
	 * Convert array to WritableBuffer instance.
	 */
	template<size_type _size>
124
	constexpr WritableBuffer(T (&_data)[_size]) noexcept
125 126
		:data(_data), size(_size) {}

127 128 129 130
	constexpr operator ConstBuffer<T>() const noexcept {
		return {data, size};
	}

131 132 133 134
	/**
	 * Cast a WritableBuffer<void> to a WritableBuffer<T>,
	 * rounding down to the next multiple of T's size.
	 */
135
	static constexpr WritableBuffer<T> FromVoidFloor(WritableBuffer<void> other) noexcept {
136
		static_assert(sizeof(T) > 0, "Empty base type");
137
		return WritableBuffer<T>(pointer(other.data),
138 139 140
					 other.size / sizeof(T));
	}

141 142 143 144 145 146
	/**
	 * 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).
	 */
147
	static constexpr WritableBuffer<T> FromVoid(WritableBuffer<void> other) noexcept {
148
		static_assert(sizeof(T) > 0, "Empty base type");
149
		assert(other.size % sizeof(T) == 0);
150
		return FromVoidFloor(other);
151 152
	}

153
	constexpr WritableBuffer<void> ToVoid() const noexcept {
154 155 156 157
		static_assert(sizeof(T) > 0, "Empty base type");
		return WritableBuffer<void>(data, size * sizeof(T));
	}

158
	constexpr bool IsNull() const noexcept {
159 160
		return data == nullptr;
	}
161

162
	constexpr bool operator==(std::nullptr_t) const noexcept {
163 164 165
		return data == nullptr;
	}

166
	constexpr bool operator!=(std::nullptr_t) const noexcept {
167 168 169
		return data != nullptr;
	}

170
	constexpr bool empty() const noexcept {
171 172
		return size == 0;
	}
173

174
	constexpr iterator begin() const noexcept {
175 176
		return data;
	}
177

178
	constexpr iterator end() const noexcept {
179 180
		return data + size;
	}
181

182
	constexpr const_iterator cbegin() const noexcept {
183 184
		return data;
	}
185

186
	constexpr const_iterator cend() const noexcept {
187 188
		return data + size;
	}
189

190
	constexpr reference operator[](size_type i) const noexcept {
191 192 193 194
		assert(i < size);

		return data[i];
	}
195 196 197 198 199

	/**
	 * Returns a reference to the first element.  Buffer must not
	 * be empty.
	 */
200
	constexpr reference front() const noexcept {
201
		assert(!empty());
202 203 204 205 206 207 208
		return data[0];
	}

	/**
	 * Returns a reference to the last element.  Buffer must not
	 * be empty.
	 */
209
	constexpr reference back() const noexcept {
210
		assert(!empty());
211 212 213 214 215 216 217
		return data[size - 1];
	}

	/**
	 * Remove the first element (by moving the head pointer, does
	 * not actually modify the buffer).  Buffer must not be empty.
	 */
218
	constexpr void pop_front() noexcept {
219
		assert(!empty());
220 221 222 223 224 225 226 227 228

		++data;
		--size;
	}

	/**
	 * Remove the last element (by moving the tail pointer, does
	 * not actually modify the buffer).  Buffer must not be empty.
	 */
229
	constexpr void pop_back() noexcept {
230
		assert(!empty());
231 232 233 234 235 236 237 238

		--size;
	}

	/**
	 * Remove the first element and return a reference to it.
	 * Buffer must not be empty.
	 */
239
	constexpr reference shift() noexcept {
240
		reference result = front();
241 242 243
		pop_front();
		return result;
	}
244

245
	constexpr void skip_front(size_type n) noexcept {
246 247 248 249 250
		assert(size >= n);

		data += n;
		size -= n;
	}
251 252 253 254 255

	/**
	 * Move the front pointer to the given address, and adjust the
	 * size attribute to retain the old end address.
	 */
256
	void MoveFront(pointer new_data) noexcept {
257 258 259 260 261 262
		assert(IsNull() == (new_data == nullptr));
		assert(new_data <= end());

		size = end() - new_data;
		data = new_data;
	}
263 264 265 266 267

	/**
	 * Move the end pointer to the given address (by adjusting the
	 * size).
	 */
268
	void SetEnd(pointer new_end) noexcept {
269 270 271 272 273
		assert(IsNull() == (new_end == nullptr));
		assert(new_end >= begin());

		size = new_end - data;
	}
274 275 276
};

#endif