MPD  0.20.6
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>
48 public:
49  typedef size_t size_type;
50 
51 public:
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< T > Range
void Append(size_type n)
Expands the tail of the buffer, after data has been written to the buffer returned by write()...
A first-in-first-out buffer: you can append data at the end, and read data from the beginning...
A reference to a memory area that is writable.
Definition: Silence.hxx:27
bool IsEmpty() const
constexpr StaticFifoBuffer()
void Consume(size_type n)
Marks a chunk as consumed.
bool IsFull() const
Range Read()
Return a buffer range which may be read.
Range Write()
Prepares writing.