[refactor]: formatting

This commit is contained in:
2023-08-08 23:24:26 +04:00
parent a445fc44b3
commit 268103d7da
14 changed files with 495 additions and 384 deletions

View File

@@ -1,10 +1,8 @@
#pragma once
#include <cstddef>
#include "Logger.h"
template <typename T>
class RingBuffer
{
private:
#include <cstddef>
template <typename T> class RingBuffer {
private:
T* m_items; /* data */
std::size_t m_head;
std::size_t m_tail;
@@ -13,7 +11,8 @@ private:
std::size_t m_size;
void advance_pointer();
void retreat_pointer();
public:
public:
RingBuffer(std::size_t size);
~RingBuffer();
bool IsFull() { return m_is_full; }
@@ -26,8 +25,7 @@ public:
void Print();
};
template <typename T> RingBuffer<T>::RingBuffer(std::size_t size)
{
template <typename T> RingBuffer<T>::RingBuffer(std::size_t size) {
m_items = new T[size];
m_head = 0;
m_tail = 0;
@@ -36,50 +34,42 @@ template <typename T> RingBuffer<T>::RingBuffer(std::size_t size)
m_size = size;
}
template <typename T> RingBuffer<T>::~RingBuffer()
{
delete[] m_items;
}
template <typename T> RingBuffer<T>::~RingBuffer() { delete[] m_items; }
template <typename T> void RingBuffer<T>::Reset()
{
template <typename T> void RingBuffer<T>::Reset() {
m_head = 0;
m_tail = 0;
m_is_full = 0;
}
template <typename T> void RingBuffer<T>::advance_pointer()
{
template <typename T> void RingBuffer<T>::advance_pointer() {
if (m_is_full) {
m_tail++;
if (m_tail == m_size) {
m_tail = 0;
}
}
if (m_tail == m_size) {
m_tail = 0;
}
}
m_head++;
if (m_head == m_size) {
m_head = 0;
}
if (m_head == m_size) {
m_head = 0;
}
std::size_t p_is_full = m_head == m_tail ? 1 : 0;
m_is_full = p_is_full;
m_is_full = p_is_full;
}
template <typename T> void RingBuffer<T>::retreat_pointer()
{
m_is_full = 0;
template <typename T> void RingBuffer<T>::retreat_pointer() {
m_is_full = 0;
m_tail++;
if (m_tail == m_size) {
m_tail = 0;
}
if (m_tail == m_size) {
m_tail = 0;
}
}
template <typename T> void RingBuffer<T>::Write(T* data, std::size_t count)
{
template <typename T> void RingBuffer<T>::Write(T* data, std::size_t count) {
if (m_is_full || m_head + count > m_size) {
write_log("[WARN] Trying to overfill the ring buffer: \n\tIsFull:%d\n\tHead:%zu\n\tCount:%zu\n\t",
m_is_full,
m_head,
count);
write_log("[WARN] Trying to overfill the ring buffer: "
"\n\tIsFull:%d\n\tHead:%zu\n\tCount:%zu\n\t",
m_is_full, m_head, count);
return;
}
m_is_empty = 0;
@@ -88,11 +78,10 @@ template <typename T> void RingBuffer<T>::Write(T* data, std::size_t count)
m_items[m_head] = data[i];
advance_pointer();
}
//m_is_empty = m_is_full && (m_head == m_tail);
// m_is_empty = m_is_full && (m_head == m_tail);
}
template <typename T> bool RingBuffer<T>::Read(T* output, std::size_t count)
{
template <typename T> bool RingBuffer<T>::Read(T* output, std::size_t count) {
if (m_is_empty) {
write_log("[WARN] Trying to read empty buffer");
return 0;
@@ -106,26 +95,21 @@ template <typename T> bool RingBuffer<T>::Read(T* output, std::size_t count)
return 1;
}
template <typename T> std::size_t RingBuffer<T>::GetSize()
{
size_t p_size = m_size;
if(!m_is_full) {
if(m_head >= m_tail) {
p_size = (m_head - m_tail);
}
else {
p_size = (m_size + m_head - m_tail);
}
}
template <typename T> std::size_t RingBuffer<T>::GetSize() {
size_t p_size = m_size;
if (!m_is_full) {
if (m_head >= m_tail) {
p_size = (m_head - m_tail);
} else {
p_size = (m_size + m_head - m_tail);
}
}
return p_size;
return p_size;
}
template <typename T> void RingBuffer<T>::Print()
{
write_log("[INFO] The ring buffer: \n\tIsFull:%d\n\tIsEmpty:%d\n\tHead:%zu\n\tTail:%zu\n\t",
m_is_full,
m_is_empty,
m_head,
m_tail);
template <typename T> void RingBuffer<T>::Print() {
write_log("[INFO] The ring buffer: "
"\n\tIsFull:%d\n\tIsEmpty:%d\n\tHead:%zu\n\tTail:%zu\n\t",
m_is_full, m_is_empty, m_head, m_tail);
}