MPD: util/ConstBuffer.hxx Source File

MPD 0.20.15

ConstBuffer.hxx
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2013-2014 Max Kellermann <max.kellermann@gmail.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef CONST_BUFFER_HPP
31 #define CONST_BUFFER_HPP
32
33 #include "Compiler.h"
34
35 #include <cstddef>
36
37 #ifndef NDEBUG
38 #include <assert.h>
39 #endif
40
41 template<typename T>
42 struct ConstBuffer;
43
44 template<>
45 struct ConstBuffer<void> {
46 typedef size_t size_type;
47 typedef const void *pointer_type;
48 typedef pointer_type const_pointer_type;
49 typedef pointer_type iterator;
50 typedef pointer_type const_iterator;
51
52 pointer_type data;
53 size_type size;
54
55 ConstBuffer() = default;
56
57 constexpr ConstBuffer(std::nullptr_t):data(nullptr), size(0) {}
58
59 constexpr ConstBuffer(pointer_type _data, size_type _size)
60 :data(_data), size(_size) {}
61
62 constexpr static ConstBuffer Null() {
63 return ConstBuffer(nullptr, 0);
64 }
65
66 constexpr static ConstBuffer<void> FromVoid(ConstBuffer<void> other) {
67 return other;
68 }
69
70 constexpr ConstBuffer<void> ToVoid() const {
71 return *this;
72 }
73
74 constexpr bool IsNull() const {
75 return data == nullptr;
76 }
77
78 constexpr bool IsEmpty() const {
79 return size == 0;
80 }
81 };
82
86 template<typename T>
87 struct ConstBuffer {
88 typedef size_t size_type;
89 typedef const T &reference_type;
90 typedef reference_type const_reference_type;
91 typedef const T *pointer_type;
92 typedef pointer_type const_pointer_type;
93 typedef pointer_type iterator;
94 typedef pointer_type const_iterator;
95
96 pointer_type data;
97 size_type size;
98
99 ConstBuffer() = default;
100
101 constexpr ConstBuffer(std::nullptr_t):data(nullptr), size(0) {}
102
103 constexpr ConstBuffer(pointer_type _data, size_type _size)
104 :data(_data), size(_size) {}
105
106 constexpr static ConstBuffer Null() {
107 return ConstBuffer(nullptr, 0);
108 }
109
116 #ifdef NDEBUG
117 constexpr
118 #endif
119 static ConstBuffer<T> FromVoid(ConstBuffer<void> other) {
120 static_assert(sizeof(T) > 0, "Empty base type");
121 #ifndef NDEBUG
122 assert(other.size % sizeof(T) == 0);
123 #endif
124 return ConstBuffer<T>(pointer_type(other.data),
125 other.size / sizeof(T));
126 }
127
128 constexpr ConstBuffer<void> ToVoid() const {
129 static_assert(sizeof(T) > 0, "Empty base type");
130 return ConstBuffer<void>(data, size * sizeof(T));
131 }
132
133 constexpr bool IsNull() const {
134 return data == nullptr;
135 }
136
137 constexpr bool IsEmpty() const {
138 return size == 0;
139 }
140
141 template<typename U>
142 gcc_pure
143 bool Contains(U &&u) const noexcept {
144 for (const auto &i : *this)
145 if (u == i)
146 return true;
147
148 return false;
149 }
150
151 constexpr iterator begin() const {
152 return data;
153 }
154
155 constexpr iterator end() const {
156 return data + size;
157 }
158
159 constexpr const_iterator cbegin() const {
160 return data;
161 }
162
163 constexpr const_iterator cend() const {
164 return data + size;
165 }
166
167 #ifdef NDEBUG
168 constexpr
169 #endif
170 reference_type operator[](size_type i) const {
171 #ifndef NDEBUG
172 assert(i < size);
173 #endif
174
175 return data[i];
176 }
177
182 #ifdef NDEBUG
183 constexpr
184 #endif
185 reference_type front() const {
186 #ifndef NDEBUG
187 assert(!IsEmpty());
188 #endif
189 return data[0];
190 }
191
196 #ifdef NDEBUG
197 constexpr
198 #endif
199 reference_type back() const {
200 #ifndef NDEBUG
201 assert(!IsEmpty());
202 #endif
203 return data[size - 1];
204 }
205
210 void pop_front() {
211 #ifndef NDEBUG
212 assert(!IsEmpty());
213 #endif
214
215 ++data;
216 --size;
217 }
218
223 void pop_back() {
224 #ifndef NDEBUG
225 assert(!IsEmpty());
226 #endif
227
228 --size;
229 }
230
235 reference_type shift() {
236 reference_type result = front();
237 pop_front();
238 return result;
239 }
240
241 void skip_front(size_type n) {
242 #ifndef NDEBUG
243 assert(size >= n);
244 #endif
245
246 data += n;
247 size -= n;
248 }
249
254 void MoveFront(pointer_type new_data) {
255 #ifndef NDEBUG
256 assert(IsNull() == (new_data == nullptr));
257 assert(new_data <= end());
258 #endif
259
260 size = end() - new_data;
261 data = new_data;
262 }
263 };
264
265 #endif
ConstBuffer< void >::ConstBuffer
constexpr ConstBuffer(std::nullptr_t)
Definition: ConstBuffer.hxx:57
ConstBuffer::MoveFront
void MoveFront(pointer_type new_data)
Move the front pointer to the given address, and adjust the size attribute to retain the old end addr...
Definition: ConstBuffer.hxx:254
ConstBuffer::const_iterator
pointer_type const_iterator
Definition: ConstBuffer.hxx:94
ConstBuffer< void >::IsEmpty
constexpr bool IsEmpty() const
Definition: ConstBuffer.hxx:78
ConstBuffer< void >
Definition: ConstBuffer.hxx:45
ConstBuffer::Contains
gcc_pure bool Contains(U &&u) const noexcept
Definition: ConstBuffer.hxx:143
ConstBuffer::pop_back
void pop_back()
Remove the last element (by moving the tail pointer, does not actually modify the buffer)...
Definition: ConstBuffer.hxx:223
ConstBuffer< void >::const_iterator
pointer_type const_iterator
Definition: ConstBuffer.hxx:50
ConstBuffer::FromVoid
static ConstBuffer< T > FromVoid(ConstBuffer< void > other)
Cast a ConstBuffer<void> to a ConstBuffer<T>.
Definition: ConstBuffer.hxx:119
ConstBuffer< void >::Null
static constexpr ConstBuffer Null()
Definition: ConstBuffer.hxx:62
ConstBuffer::ConstBuffer
ConstBuffer()=default
ConstBuffer::ConstBuffer
constexpr ConstBuffer(pointer_type _data, size_type _size)
Definition: ConstBuffer.hxx:103
ConstBuffer< void >::pointer_type
const void * pointer_type
Definition: ConstBuffer.hxx:47
ConstBuffer< void >::ConstBuffer
constexpr ConstBuffer(pointer_type _data, size_type _size)
Definition: ConstBuffer.hxx:59
ConstBuffer< void >::size
size_type size
Definition: ConstBuffer.hxx:53
ConstBuffer::size
size_type size
Definition: ConstBuffer.hxx:97
ConstBuffer::cend
constexpr const_iterator cend() const
Definition: ConstBuffer.hxx:163
ConstBuffer::pointer_type
const T * pointer_type
Definition: ConstBuffer.hxx:91
ConstBuffer::reference_type
const T & reference_type
Definition: ConstBuffer.hxx:89
ConstBuffer::IsNull
constexpr bool IsNull() const
Definition: ConstBuffer.hxx:133
ConstBuffer::ToVoid
constexpr ConstBuffer< void > ToVoid() const
Definition: ConstBuffer.hxx:128
Compiler.h
ConstBuffer::cbegin
constexpr const_iterator cbegin() const
Definition: ConstBuffer.hxx:159
ConstBuffer::ConstBuffer
constexpr ConstBuffer(std::nullptr_t)
Definition: ConstBuffer.hxx:101
ConstBuffer::front
reference_type front() const
Returns a reference to the first element.
Definition: ConstBuffer.hxx:185
ConstBuffer< void >::FromVoid
static constexpr ConstBuffer< void > FromVoid(ConstBuffer< void > other)
Definition: ConstBuffer.hxx:66
ConstBuffer::data
pointer_type data
Definition: ConstBuffer.hxx:96
ConstBuffer< void >::data
pointer_type data
Definition: ConstBuffer.hxx:52
ConstBuffer::pop_front
void pop_front()
Remove the first element (by moving the head pointer, does not actually modify the buffer)...
Definition: ConstBuffer.hxx:210
ConstBuffer< void >::ToVoid
constexpr ConstBuffer< void > ToVoid() const
Definition: ConstBuffer.hxx:70
ConstBuffer::operator[]
reference_type operator[](size_type i) const
Definition: ConstBuffer.hxx:170
ConstBuffer::iterator
pointer_type iterator
Definition: ConstBuffer.hxx:93
ConstBuffer::end
constexpr iterator end() const
Definition: ConstBuffer.hxx:155
ConstBuffer< void >::iterator
pointer_type iterator
Definition: ConstBuffer.hxx:49
ConstBuffer::shift
reference_type shift()
Remove the first element and return a reference to it.
Definition: ConstBuffer.hxx:235
ConstBuffer< void >::size_type
size_t size_type
Definition: ConstBuffer.hxx:46
ConstBuffer::IsEmpty
constexpr bool IsEmpty() const
Definition: ConstBuffer.hxx:137
ConstBuffer< void >::IsNull
constexpr bool IsNull() const
Definition: ConstBuffer.hxx:74
ConstBuffer::const_reference_type
reference_type const_reference_type
Definition: ConstBuffer.hxx:90
ConstBuffer
A reference to a memory area that is read-only.
Definition: FlacPcm.hxx:29
ConstBuffer::begin
constexpr iterator begin() const
Definition: ConstBuffer.hxx:151
ConstBuffer::Null
static constexpr ConstBuffer Null()
Definition: ConstBuffer.hxx:106
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
ConstBuffer< void >::const_pointer_type
pointer_type const_pointer_type
Definition: ConstBuffer.hxx:48
ConstBuffer::size_type
size_t size_type
Definition: ConstBuffer.hxx:88
ConstBuffer::skip_front
void skip_front(size_type n)
Definition: ConstBuffer.hxx:241
ConstBuffer::back
reference_type back() const
Returns a reference to the last element.
Definition: ConstBuffer.hxx:199
ConstBuffer::const_pointer_type
pointer_type const_pointer_type
Definition: ConstBuffer.hxx:92