MPD  0.20.18
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;
51 
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;
91  typedef const T *pointer_type;
95 
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
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
171 #ifndef NDEBUG
172  assert(i < size);
173 #endif
174 
175  return data[i];
176  }
177 
182 #ifdef NDEBUG
183  constexpr
184 #endif
186 #ifndef NDEBUG
187  assert(!IsEmpty());
188 #endif
189  return data[0];
190  }
191 
196 #ifdef NDEBUG
197  constexpr
198 #endif
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 
236  reference_type result = front();
237  pop_front();
238  return result;
239  }
240 
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
constexpr ConstBuffer(std::nullptr_t)
Definition: ConstBuffer.hxx:57
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...
pointer_type const_iterator
Definition: ConstBuffer.hxx:94
constexpr bool IsEmpty() const
Definition: ConstBuffer.hxx:78
gcc_pure bool Contains(U &&u) const noexcept
void pop_back()
Remove the last element (by moving the tail pointer, does not actually modify the buffer)...
pointer_type const_iterator
Definition: ConstBuffer.hxx:50
static ConstBuffer< T > FromVoid(ConstBuffer< void > other)
Cast a ConstBuffer<void> to a ConstBuffer<T>.
static constexpr ConstBuffer Null()
Definition: ConstBuffer.hxx:62
ConstBuffer()=default
constexpr ConstBuffer(pointer_type _data, size_type _size)
const void * pointer_type
Definition: ConstBuffer.hxx:47
constexpr ConstBuffer(pointer_type _data, size_type _size)
Definition: ConstBuffer.hxx:59
size_type size
Definition: ConstBuffer.hxx:97
constexpr const_iterator cend() const
const T * pointer_type
Definition: ConstBuffer.hxx:91
const T & reference_type
Definition: ConstBuffer.hxx:89
constexpr bool IsNull() const
constexpr ConstBuffer< void > ToVoid() const
constexpr const_iterator cbegin() const
constexpr ConstBuffer(std::nullptr_t)
reference_type front() const
Returns a reference to the first element.
static constexpr ConstBuffer< void > FromVoid(ConstBuffer< void > other)
Definition: ConstBuffer.hxx:66
pointer_type data
Definition: ConstBuffer.hxx:96
pointer_type data
Definition: ConstBuffer.hxx:52
void pop_front()
Remove the first element (by moving the head pointer, does not actually modify the buffer)...
constexpr ConstBuffer< void > ToVoid() const
Definition: ConstBuffer.hxx:70
reference_type operator[](size_type i) const
pointer_type iterator
Definition: ConstBuffer.hxx:93
constexpr iterator end() const
pointer_type iterator
Definition: ConstBuffer.hxx:49
reference_type shift()
Remove the first element and return a reference to it.
constexpr bool IsEmpty() const
constexpr bool IsNull() const
Definition: ConstBuffer.hxx:74
reference_type const_reference_type
Definition: ConstBuffer.hxx:90
A reference to a memory area that is read-only.
Definition: FlacPcm.hxx:29
constexpr iterator begin() const
static constexpr ConstBuffer Null()
#define gcc_pure
Definition: Compiler.h:116
pointer_type const_pointer_type
Definition: ConstBuffer.hxx:48
size_t size_type
Definition: ConstBuffer.hxx:88
void skip_front(size_type n)
reference_type back() const
Returns a reference to the last element.
pointer_type const_pointer_type
Definition: ConstBuffer.hxx:92