diff --git a/bin/main.cpp b/bin/main.cpp index 699b32f..cb106f4 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -36,4 +36,6 @@ auto main(int argc, char *argv[]) -> int hack::log()(hack::mt::algorithms::max(4, 5)); hack::log()(hack::mt::algorithms::max(c, b)); } + + hack::error()("asdfJK"); } diff --git a/src/hack/exception/exception.hpp b/src/hack/exception/exception.hpp index 2ed6281..eb7d202 100644 --- a/src/hack/exception/exception.hpp +++ b/src/hack/exception/exception.hpp @@ -5,6 +5,8 @@ #include "hack/utils/color.hpp" #include "hack/exception/title.hpp" +// NOTE +// вмесо nlohman::json можно поробовать прикрутить https://jqlang.github.io/jq namespace hack { class exception diff --git a/src/hack/patterns/ring_buffer.hpp b/src/hack/patterns/ring_buffer.hpp index 16a264e..67a4c6b 100644 --- a/src/hack/patterns/ring_buffer.hpp +++ b/src/hack/patterns/ring_buffer.hpp @@ -1,19 +1,23 @@ #pragma once -#include +#include #include #include #include namespace hack::patterns { - template + template class ring_buffer { using MUTEX = std::lock_guard; public: - explicit ring_buffer() = default; + explicit ring_buffer(int s) + { + m_size = s; + m_data.resize(m_size); + }; public: void put(T item) noexcept @@ -52,13 +56,13 @@ namespace hack::patterns if(empty()) return std::nullopt; auto val = m_data[m_tail]; - m_tail = (m_tail + 1) % BUFFER_SIZE; + m_tail = (m_tail + 1) % m_size; m_full = false; return val; } - std::array& get_src() const noexcept + std::vector& get_src() const noexcept { return m_data; } @@ -73,13 +77,13 @@ namespace hack::patterns { MUTEX lock(m_mutex); - std::size_t size; - if(!m_full) - size = (m_head >= m_tail) ? m_head - m_tail : BUFFER_SIZE - (m_tail - m_head); + std::size_t s; + if (!m_full) + s = (m_head >= m_tail) ? m_head - m_tail : m_size - (m_tail - m_head); else - size = BUFFER_SIZE; + s = m_size; - return size; + return s; } void reset() noexcept @@ -96,14 +100,14 @@ namespace hack::patterns std::size_t capacity() const noexcept { - return BUFFER_SIZE; + return m_size; } private: void head_refresh() { - if (m_full) m_tail = (m_tail + 1) % BUFFER_SIZE; - m_head = (m_head + 1) % BUFFER_SIZE; + if (m_full) m_tail = (m_tail + 1) % m_size; + m_head = (m_head + 1) % m_size; m_full = m_head == m_tail; } @@ -113,7 +117,8 @@ namespace hack::patterns std::size_t m_tail{ 0 }; private: + int m_size; mutable std::recursive_mutex m_mutex; - mutable std::array m_data; + mutable std::vector m_data; }; } diff --git a/tests/patterns/ring_buffer.cpp b/tests/patterns/ring_buffer.cpp index 34e9a7b..a210659 100644 --- a/tests/patterns/ring_buffer.cpp +++ b/tests/patterns/ring_buffer.cpp @@ -7,7 +7,7 @@ TEST_CASE("patterns") SECTION("ring buffer") { // single value - hack::patterns::ring_buffer rb1; + hack::patterns::ring_buffer rb1(10); REQUIRE(rb1.empty() == true); for (int i = 1; i < 13; ++i) rb1.put(i); @@ -36,7 +36,7 @@ TEST_CASE("patterns") // vector value std::vector v { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; - hack::patterns::ring_buffer rb2; + hack::patterns::ring_buffer rb2(10); REQUIRE(rb2.empty() == true); rb2.put(v);