#ifndef STA_CORE_FIFO_BUFFER_TPP #define STA_CORE_FIFO_BUFFER_TPP #ifndef STA_CORE_FIFO_BUFFER_HPP # error "Internal header. Include instead" #endif // !STA_CORE_FIFO_BUFFER_HPP #include namespace sta { template FifoBuffer::FifoBuffer() : head_{buffer_}, tail_{buffer_} {} template FifoBuffer::FifoBuffer(const uint8_t * buffer, size_type size) { set(buffer, size); } template void FifoBuffer::set(const uint8_t * buffer, size_type bsize) { STA_ASSERT(bsize <= sizeof(buffer_)); STA_ASSERT(buffer != nullptr); head_ = buffer_; tail_ = buffer_ + bsize; memcpy(buffer_, buffer, bsize); } template void FifoBuffer::clear() { head_ = tail_ = buffer_; } template void FifoBuffer::pushBack(uint8_t value) { STA_ASSERT_MSG(tail_ < buffer_ + sizeof(buffer_), "Buffer overflow"); *tail_++ = value; } template void FifoBuffer::pushBack(const uint8_t * buffer, size_type bsize) { STA_ASSERT_MSG(size() + bsize <= sizeof(buffer_), "Buffer overflow"); STA_ASSERT(buffer != nullptr); memcpy(tail_, buffer, bsize); tail_ += bsize; } template void FifoBuffer::pushBack(uint8_t value, size_type count) { STA_ASSERT_MSG(size() + count <= sizeof(buffer_), "Buffer overflow"); memset(tail_, value, count); tail_ += count; } template void FifoBuffer::popFront(uint8_t * buffer, size_type bsize) { STA_ASSERT_MSG(size() >= bsize, "Not enough data"); STA_ASSERT(buffer != nullptr); memcpy(buffer, head_, bsize); head_ += bsize; } template typename FifoBuffer::size_type FifoBuffer::size() const { return (tail_ - head_); } template bool FifoBuffer::isFull() const { return (tail_ == buffer_ + sizeof(buffer_)); } template bool FifoBuffer::isEmpty() const { return (head_ == tail_); } } // namespace sta #endif // STA_CORE_FIFO_BUFFER_TPP