2 * Copyright (C) 2003-2010 Max Kellermann <max@duempel.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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
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.
30 #ifndef XCSOAR_FIFO_BUFFER_HPP
31 #define XCSOAR_FIFO_BUFFER_HPP
40 * A first-in-first-out buffer: you can append data at the end, and
41 * read data from the beginning. This class automatically shifts the
42 * buffer as needed. It is not thread safe.
44 template<class T
, size_t size
>
47 typedef size_t size_type
;
58 Range(T
*_data
, size_type _length
):data(_data
), length(_length
) {}
61 bool IsEmpty() const {
72 FifoBuffer():head(0), tail(0) {}
83 std::move(data
+ head
, data
+ tail
, data
);
94 bool IsEmpty() const {
99 return head
== 0 && tail
== size
;
103 * Prepares writing. Returns a buffer range which may be written.
104 * When you are finished, call append().
108 return Range(data
+ tail
, size
- tail
);
112 * Expands the tail of the buffer, after data has been written to
113 * the buffer returned by write().
115 void Append(size_type n
) {
116 assert(tail
<= size
);
118 assert(tail
+ n
<= size
);
124 * Return a buffer range which may be read. The buffer pointer is
125 * writable, to allow modifications while parsing.
128 return Range(data
+ head
, tail
- head
);
132 * Marks a chunk as consumed.
134 void Consume(size_type n
) {
135 assert(tail
<= size
);
136 assert(head
<= tail
);
138 assert(head
+ n
<= tail
);