MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

WritableBuffer.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 WRITABLE_BUFFER_HPP
31 #define WRITABLE_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 WritableBuffer;
43
44 template<>
45 struct WritableBuffer<void> {
46 typedef size_t size_type;
47 typedef void *pointer_type;
48 typedef const void *const_pointer_type;
49 typedef pointer_type iterator;
50 typedef const_pointer_type const_iterator;
51
52 pointer_type data;
53 size_type size;
54
55 WritableBuffer() = default;
56
57 constexpr WritableBuffer(std::nullptr_t):data(nullptr), size(0) {}
58
59 constexpr WritableBuffer(pointer_type _data, size_type _size)
60 :data(_data), size(_size) {}
61
62 constexpr static WritableBuffer Null() {
63 return { nullptr, 0 };
64 }
65
66 constexpr bool IsNull() const {
67 return data == nullptr;
68 }
69
70 constexpr bool IsEmpty() const {
71 return size == 0;
72 }
73 };
74
80 template<typename T>
81 struct WritableBuffer {
82 typedef size_t size_type;
83 typedef T &reference_type;
84 typedef const T &const_reference_type;
85 typedef T *pointer_type;
86 typedef const T *const_pointer_type;
87 typedef pointer_type iterator;
88 typedef const_pointer_type const_iterator;
89
90 pointer_type data;
91 size_type size;
92
93 WritableBuffer() = default;
94
95 constexpr WritableBuffer(std::nullptr_t):data(nullptr), size(0) {}
96
97 constexpr WritableBuffer(pointer_type _data, size_type _size)
98 :data(_data), size(_size) {}
99
100 constexpr static WritableBuffer Null() {
101 return { nullptr, 0 };
102 }
103
110 #ifdef NDEBUG
111 constexpr
112 #endif
113 static WritableBuffer<T> FromVoid(WritableBuffer<void> other) {
114 static_assert(sizeof(T) > 0, "Empty base type");
115 #ifndef NDEBUG
116 assert(other.size % sizeof(T) == 0);
117 #endif
118 return WritableBuffer<T>(pointer_type(other.data),
119 other.size / sizeof(T));
120 }
121
122 constexpr WritableBuffer<void> ToVoid() const {
123 static_assert(sizeof(T) > 0, "Empty base type");
124 return WritableBuffer<void>(data, size * sizeof(T));
125 }
126
127 constexpr bool IsNull() const {
128 return data == nullptr;
129 }
130
131 constexpr bool IsEmpty() const {
132 return size == 0;
133 }
134
135 constexpr iterator begin() const {
136 return data;
137 }
138
139 constexpr iterator end() const {
140 return data + size;
141 }
142
143 constexpr const_iterator cbegin() const {
144 return data;
145 }
146
147 constexpr const_iterator cend() const {
148 return data + size;
149 }
150
151 #ifdef NDEBUG
152 constexpr
153 #endif
154 reference_type operator[](size_type i) const {
155 #ifndef NDEBUG
156 assert(i < size);
157 #endif
158
159 return data[i];
160 }
161
166 #ifdef NDEBUG
167 constexpr
168 #endif
169 reference_type front() const {
170 #ifndef NDEBUG
171 assert(!IsEmpty());
172 #endif
173 return data[0];
174 }
175
180 #ifdef NDEBUG
181 constexpr
182 #endif
183 reference_type back() const {
184 #ifndef NDEBUG
185 assert(!IsEmpty());
186 #endif
187 return data[size - 1];
188 }
189
194 void pop_front() {
195 assert(!IsEmpty());
196
197 ++data;
198 --size;
199 }
200
205 void pop_back() {
206 assert(!IsEmpty());
207
208 --size;
209 }
210
215 reference_type shift() {
216 reference_type result = front();
217 pop_front();
218 return result;
219 }
220
221 void skip_front(size_type n) {
222 #ifndef NDEBUG
223 assert(size >= n);
224 #endif
225
226 data += n;
227 size -= n;
228 }
229 };
230
231 #endif
WritableBuffer< void >::WritableBuffer
constexpr WritableBuffer(std::nullptr_t)
Definition: WritableBuffer.hxx:57
WritableBuffer< void >::data
pointer_type data
Definition: WritableBuffer.hxx:52
WritableBuffer::FromVoid
static WritableBuffer< T > FromVoid(WritableBuffer< void > other)
Cast a WritableBuffer to a WritableBuffer.
Definition: WritableBuffer.hxx:113
WritableBuffer< void >::const_iterator
const_pointer_type const_iterator
Definition: WritableBuffer.hxx:50
WritableBuffer::cbegin
constexpr const_iterator cbegin() const
Definition: WritableBuffer.hxx:143
WritableBuffer::reference_type
T & reference_type
Definition: WritableBuffer.hxx:83
WritableBuffer::iterator
pointer_type iterator
Definition: WritableBuffer.hxx:87
WritableBuffer
A reference to a memory area that is writable.
Definition: Silence.hxx:27
WritableBuffer::pop_front
void pop_front()
Remove the first element (by moving the head pointer, does not actually modify the buffer)...
Definition: WritableBuffer.hxx:194
WritableBuffer::back
reference_type back() const
Returns a reference to the last element.
Definition: WritableBuffer.hxx:183
WritableBuffer::pointer_type
T * pointer_type
Definition: WritableBuffer.hxx:85
WritableBuffer::shift
reference_type shift()
Remove the first element and return a reference to it.
Definition: WritableBuffer.hxx:215
WritableBuffer< void >::const_pointer_type
const void * const_pointer_type
Definition: WritableBuffer.hxx:48
WritableBuffer< void >::pointer_type
void * pointer_type
Definition: WritableBuffer.hxx:47
WritableBuffer::const_reference_type
const T & const_reference_type
Definition: WritableBuffer.hxx:84
WritableBuffer::WritableBuffer
constexpr WritableBuffer(std::nullptr_t)
Definition: WritableBuffer.hxx:95
WritableBuffer::pop_back
void pop_back()
Remove the last element (by moving the tail pointer, does not actually modify the buffer)...
Definition: WritableBuffer.hxx:205
WritableBuffer::skip_front
void skip_front(size_type n)
Definition: WritableBuffer.hxx:221
Compiler.h
WritableBuffer< void >::IsNull
constexpr bool IsNull() const
Definition: WritableBuffer.hxx:66
WritableBuffer::front
reference_type front() const
Returns a reference to the first element.
Definition: WritableBuffer.hxx:169
WritableBuffer::end
constexpr iterator end() const
Definition: WritableBuffer.hxx:139
WritableBuffer::WritableBuffer
constexpr WritableBuffer(pointer_type _data, size_type _size)
Definition: WritableBuffer.hxx:97
WritableBuffer< void >
Definition: WritableBuffer.hxx:45
WritableBuffer::ToVoid
constexpr WritableBuffer< void > ToVoid() const
Definition: WritableBuffer.hxx:122
WritableBuffer::IsNull
constexpr bool IsNull() const
Definition: WritableBuffer.hxx:127
WritableBuffer::data
pointer_type data
Definition: WritableBuffer.hxx:90
WritableBuffer::const_iterator
const_pointer_type const_iterator
Definition: WritableBuffer.hxx:88
WritableBuffer< void >::size
size_type size
Definition: WritableBuffer.hxx:53
WritableBuffer::Null
static constexpr WritableBuffer Null()
Definition: WritableBuffer.hxx:100
WritableBuffer< void >::IsEmpty
constexpr bool IsEmpty() const
Definition: WritableBuffer.hxx:70
WritableBuffer::size
size_type size
Definition: WritableBuffer.hxx:91
WritableBuffer::cend
constexpr const_iterator cend() const
Definition: WritableBuffer.hxx:147
WritableBuffer< void >::iterator
pointer_type iterator
Definition: WritableBuffer.hxx:49
WritableBuffer< void >::size_type
size_t size_type
Definition: WritableBuffer.hxx:46
WritableBuffer::begin
constexpr iterator begin() const
Definition: WritableBuffer.hxx:135
WritableBuffer< void >::Null
static constexpr WritableBuffer Null()
Definition: WritableBuffer.hxx:62
WritableBuffer< void >::WritableBuffer
constexpr WritableBuffer(pointer_type _data, size_type _size)
Definition: WritableBuffer.hxx:59
WritableBuffer::operator[]
reference_type operator[](size_type i) const
Definition: WritableBuffer.hxx:154
WritableBuffer::IsEmpty
constexpr bool IsEmpty() const
Definition: WritableBuffer.hxx:131
WritableBuffer::const_pointer_type
const T * const_pointer_type
Definition: WritableBuffer.hxx:86
WritableBuffer::size_type
size_t size_type
Definition: WritableBuffer.hxx:82
WritableBuffer::WritableBuffer
WritableBuffer()=default