24#ifndef COMM_BUFFERS_H_
25#define COMM_BUFFERS_H_
35typedef unsigned __int8 uint8_t;
42 BufferError(
const std::string& why) : logic_error(why) {};
49 void Put(
const std::string line);
55 bool Get(std::string& line);
58 std::deque<std::string> m_buffer;
74 LineBuffer() : m_line_count(0), m_last_ch(
'\n') {}
77 std::vector<uint8_t> m_buff;
100 std::deque<std::string> m_lines;
101 std::vector<uint8_t> m_line;
102 enum class State { PrefixWait, Data, CsDigit1, CsDigit2 } m_state;
110 : m_buf(std::unique_ptr<T[]>(
new T[size])),
118 std::lock_guard<std::mutex> lock(m_mutex);
125 size_t Capacity() const noexcept {
return m_max_size; }
129 std::lock_guard<std::mutex> lock(m_mutex);
130 size_t size = m_max_size;
132 size = m_head >= m_tail ? m_head - m_tail : m_head + m_max_size - m_tail;
138 std::lock_guard<std::mutex> lock(m_mutex);
139 return (!m_full && (m_head == m_tail));
144 std::lock_guard<std::mutex> lock(m_mutex);
153 std::lock_guard<std::mutex> lock(m_mutex);
154 if (m_full)
return false;
161 std::lock_guard<std::mutex> lock(m_mutex);
162 if (m_full)
throw BufferError(
"Put(): full buffer");
168 std::lock_guard<std::mutex> lock(m_mutex);
169 if (!m_full && m_head == m_tail)
throw BufferError(
"Get(): empty buffer");
177 bool Get(T& item)
noexcept {
178 std::lock_guard<std::mutex> lock(m_mutex);
179 if (!m_full && m_head == m_tail)
return false;
184 const T& Peek()
const {
185 std::lock_guard<std::mutex> lock(m_mutex);
186 if (!m_full && m_head == m_tail)
throw BufferError(
"Peek(): empty buffer");
187 return m_buf[m_tail];
190 bool Peek(T& item)
const noexcept {
191 std::lock_guard<std::mutex> lock(m_mutex);
192 if (!m_full && m_head == m_tail)
return false;
193 item = m_buf[m_tail];
198 mutable std::mutex m_mutex;
199 std::unique_ptr<T[]> m_buf;
200 const size_t m_max_size;
205 void DoPut(
const T& item) {
206 m_buf[m_head] = item;
207 if (m_full) m_tail = (m_tail + 1) % m_max_size;
208 m_head = (m_head + 1) % m_max_size;
209 m_full = m_head == m_tail;
214 auto val = m_buf[m_tail];
216 m_tail = (m_tail + 1) % m_max_size;
Fixed size, synchronized FIFO buffer.
bool IsEmpty() const noexcept
Return true if buffer is empty.
void Reset() noexcept
Reset internal state, ditch possible contained data.
size_t Capacity() const noexcept
Return buffer max size.
size_t Size() const noexcept
Return actual size.
bool Get(T &item) noexcept
Retrieve item from buffer without throwing exceptions.
T Get()
Get item from buff; throw BufferError if empty.
bool SafePut(const T &item) noexcept
Add item to buffer without throwing exceptions.
void Put(const T &item)
Add item to buffer; throw BufferError if full.
bool IsFull() const noexcept
Return true if buffer is full.
Assembles input characters to lines.
void Put(uint8_t ch)
Add a single character.
std::vector< uint8_t > GetLine()
Retrieve a line from buffer, return empty line if none available.
bool HasLine() const
Return true if a line is available to be returned by GetLine().
Assemble characters to NMEA0183 sentences.
bool HasSentence() const
Return true if a sentence is available to be returned by GetSentence()
std::string GetSentence()
Retrieve a sentence from buffer.
void Put(uint8_t ch)
Add a single character, possibly making a sentence available.
Synchronized buffer for outbound, line oriented data.
void Put(const std::string line)
Insert line in buffer.
bool Get(std::string &line)
Retrieve a line in buffer.