Skyward boardcore
Loading...
Searching...
No Matches
contiguous_queue.h
Go to the documentation of this file.
1/* Copyright (c) 2017 Skyward Experimental Rocketry
2 * Author: Federico Terraneo
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <stdexcept>
26
27namespace Boardcore
28{
29
45template <typename T, unsigned N>
47{
48public:
53
62 T* addEnd() { return elements + size; }
63
73 T* removeEnd() { return elements; }
74
84 const T* removeEnd() const { return elements; }
85
89 unsigned int availableToAdd() const { return N - size; }
90
94 unsigned int availableToRemove() const { return size; }
95
104 void added(unsigned int n)
105 {
106 if (size + n > N)
107 throw std::range_error("ContiguousBuffer::added");
108 size += n;
109 }
110
119 void removed(unsigned int n)
120 {
121 if (n > size)
122 throw std::range_error("ContiguousBuffer::removed");
123 if (n == 0)
124 return;
125 for (unsigned int i = 0; i < size - n; i++)
126 elements[i] = std::move(elements[i + n]);
127 size -= n;
128 }
129
130private:
131 ContiguousQueue(const ContiguousQueue&) = delete;
132 ContiguousQueue& operator=(const ContiguousQueue&) = delete;
133
134 T elements[N] = {0};
135 unsigned int size = 0;
136};
137
138} // namespace Boardcore
unsigned int availableToAdd() const
void removed(unsigned int n)
void added(unsigned int n)
unsigned int availableToRemove() const
This file includes all the types the logdecoder script will decode.