MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

StaticFifoBuffer.hxx
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2003-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 STATIC_FIFO_BUFFER_HPP
31 #define STATIC_FIFO_BUFFER_HPP
32
33 #include "WritableBuffer.hxx"
34
35 #include <utility>
36 #include <algorithm>
37
38 #include <assert.h>
39 #include <stddef.h>
40
46 template<class T, size_t size>
47 class StaticFifoBuffer {
48 public:
49 typedef size_t size_type;
50
51 public:
52 typedef WritableBuffer<T> Range;
53
54 protected:
55 size_type head, tail;
56 T data[size];
57
58 public:
59 constexpr
60 StaticFifoBuffer():head(0), tail(0) {}
61
62 void Shift() {
63 if (head == 0)
64 return;
65
66 assert(head <= size);
67 assert(tail <= size);
68 assert(tail >= head);
69
70 std::move(data + head, data + tail, data);
71
72 tail -= head;
73 head = 0;
74 }
75
76 void Clear() {
77 head = tail = 0;
78 }
79
80 bool IsEmpty() const {
81 return head == tail;
82 }
83
84 bool IsFull() const {
85 return head == 0 && tail == size;
86 }
87
92 Range Write() {
93 if (IsEmpty())
94 Clear();
95 else if (tail == size)
96 Shift();
97
98 return Range(data + tail, size - tail);
99 }
100
105 void Append(size_type n) {
106 assert(tail <= size);
107 assert(n <= size);
108 assert(tail + n <= size);
109
110 tail += n;
111 }
112
117 Range Read() {
118 return Range(data + head, tail - head);
119 }
120
124 void Consume(size_type n) {
125 assert(tail <= size);
126 assert(head <= tail);
127 assert(n <= tail);
128 assert(head + n <= tail);
129
130 head += n;
131 }
132 };
133
134 #endif
WritableBuffer.hxx
StaticFifoBuffer::Range
WritableBuffer< T > Range
Definition: StaticFifoBuffer.hxx:52
StaticFifoBuffer::Append
void Append(size_type n)
Expands the tail of the buffer, after data has been written to the buffer returned by write()...
Definition: StaticFifoBuffer.hxx:105
StaticFifoBuffer
A first-in-first-out buffer: you can append data at the end, and read data from the beginning...
Definition: StaticFifoBuffer.hxx:47
WritableBuffer
A reference to a memory area that is writable.
Definition: Silence.hxx:27
StaticFifoBuffer::IsEmpty
bool IsEmpty() const
Definition: StaticFifoBuffer.hxx:80
StaticFifoBuffer::Shift
void Shift()
Definition: StaticFifoBuffer.hxx:62
StaticFifoBuffer::tail
size_type tail
Definition: StaticFifoBuffer.hxx:55
StaticFifoBuffer::StaticFifoBuffer
constexpr StaticFifoBuffer()
Definition: StaticFifoBuffer.hxx:60
StaticFifoBuffer::Consume
void Consume(size_type n)
Marks a chunk as consumed.
Definition: StaticFifoBuffer.hxx:124
StaticFifoBuffer::size_type
size_t size_type
Definition: StaticFifoBuffer.hxx:49
StaticFifoBuffer::head
size_type head
Definition: StaticFifoBuffer.hxx:55
StaticFifoBuffer::IsFull
bool IsFull() const
Definition: StaticFifoBuffer.hxx:84
StaticFifoBuffer::data
T data[size]
Definition: StaticFifoBuffer.hxx:56
StaticFifoBuffer::Clear
void Clear()
Definition: StaticFifoBuffer.hxx:76
StaticFifoBuffer::Read
Range Read()
Return a buffer range which may be read.
Definition: StaticFifoBuffer.hxx:117
StaticFifoBuffer::Write
Range Write()
Prepares writing.
Definition: StaticFifoBuffer.hxx:92