MPD  0.20.6
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
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
constexpr WritableBuffer(std::nullptr_t)
static WritableBuffer< T > FromVoid(WritableBuffer< void > other)
Cast a WritableBuffer to a WritableBuffer.
const_pointer_type const_iterator
constexpr const_iterator cbegin() const
pointer_type iterator
A reference to a memory area that is writable.
Definition: Silence.hxx:27
void pop_front()
Remove the first element (by moving the head pointer, does not actually modify the buffer)...
reference_type back() const
Returns a reference to the last element.
reference_type shift()
Remove the first element and return a reference to it.
const void * const_pointer_type
const T & const_reference_type
constexpr WritableBuffer(std::nullptr_t)
void pop_back()
Remove the last element (by moving the tail pointer, does not actually modify the buffer)...
void skip_front(size_type n)
constexpr bool IsNull() const
reference_type front() const
Returns a reference to the first element.
constexpr iterator end() const
constexpr WritableBuffer(pointer_type _data, size_type _size)
constexpr WritableBuffer< void > ToVoid() const
constexpr bool IsNull() const
pointer_type data
const_pointer_type const_iterator
static constexpr WritableBuffer Null()
constexpr bool IsEmpty() const
constexpr const_iterator cend() const
constexpr iterator begin() const
static constexpr WritableBuffer Null()
constexpr WritableBuffer(pointer_type _data, size_type _size)
reference_type operator[](size_type i) const
constexpr bool IsEmpty() const
const T * const_pointer_type
WritableBuffer()=default