diff --git a/include/etl/circular_buffer.h b/include/etl/circular_buffer.h index 2ae9fc72..006df3bb 100644 --- a/include/etl/circular_buffer.h +++ b/include/etl/circular_buffer.h @@ -83,7 +83,7 @@ namespace etl //************************************************************************* size_type size() const { - return (in >= out) ? in - out : BUFFER_SIZE - (out - in); + return (in >= out) ? in - out : buffer_size - (out - in); } //************************************************************************* @@ -98,7 +98,7 @@ namespace etl size_t i = in; ++i; - if (i == BUFFER_SIZE) ETL_UNLIKELY + if (i == buffer_size) ETL_UNLIKELY { i = 0U; } @@ -115,20 +115,20 @@ namespace etl //************************************************************************* size_type max_size() const { - return BUFFER_SIZE - 1U; + return buffer_size - 1U; } //************************************************************************* size_type capacity() const { - return BUFFER_SIZE - 1U; + return buffer_size - 1U; } protected: //************************************************************************* - circular_buffer_base(size_type BUFFER_SIZE_) - : BUFFER_SIZE(BUFFER_SIZE_) + circular_buffer_base(size_type buffer_size_) + : buffer_size(buffer_size_) , in(0U) , out(0U) { @@ -138,7 +138,7 @@ namespace etl void increment_in() { ++in; - if (in == BUFFER_SIZE) ETL_UNLIKELY + if (in == buffer_size) ETL_UNLIKELY { in = 0U; } @@ -148,13 +148,13 @@ namespace etl void increment_out() { ++out; - if (out == BUFFER_SIZE) ETL_UNLIKELY + if (out == buffer_size) ETL_UNLIKELY { out = 0U; } } - const size_type BUFFER_SIZE; + size_type buffer_size; size_type in; ///< Index to the next write. size_type out; ///< Index to the next read. ETL_DECLARE_DEBUG_COUNT; ///< Internal debugging. @@ -230,7 +230,7 @@ namespace etl //************************************************************************* pointer operator ->() const { - return picb->pbuffer[current]; + return &picb->pbuffer[current]; } //************************************************************************* @@ -241,7 +241,7 @@ namespace etl ++current; // Did we reach the end of the buffer? - if (current == picb->BUFFER_SIZE) + if (current == picb->buffer_size) { current = 0U; } @@ -269,7 +269,7 @@ namespace etl // Are we at the end of the buffer? if (current == 0U) { - current = picb->BUFFER_SIZE - 1; + current = picb->buffer_size - 1; } else { @@ -296,8 +296,8 @@ namespace etl //************************************************************************* iterator& operator +=(int n) { - current += size_type(picb->BUFFER_SIZE + n); - current %= picb->BUFFER_SIZE; + current += size_type(picb->buffer_size + n); + current %= picb->buffer_size; return (*this); } @@ -407,7 +407,7 @@ namespace etl { if (index < firstIndex) { - return picb->BUFFER_SIZE + current - firstIndex; + return picb->buffer_size + current - firstIndex; } else { @@ -501,7 +501,7 @@ namespace etl //************************************************************************* const_pointer operator ->() const { - return picb->pbuffer[current]; + return &(picb->pbuffer[current]); } //************************************************************************* @@ -512,7 +512,7 @@ namespace etl ++current; // Did we reach the end of the buffer? - if (current == picb->BUFFER_SIZE) + if (current == picb->buffer_size) { current = 0U; } @@ -540,7 +540,7 @@ namespace etl // Are we at the end of the buffer? if (current == 0U) { - current = picb->BUFFER_SIZE - 1; + current = picb->buffer_size - 1; } else { @@ -567,8 +567,8 @@ namespace etl //************************************************************************* const_iterator& operator +=(int n) { - current += size_type(picb->BUFFER_SIZE + n); - current %= picb->BUFFER_SIZE; + current += size_type(picb->buffer_size + n); + current %= picb->buffer_size; return (*this); } @@ -820,7 +820,7 @@ namespace etl { ETL_ASSERT(!empty(), ETL_ERROR(circular_buffer_empty)); - return pbuffer[in == 0U ? BUFFER_SIZE - 1 : in - 1U]; + return pbuffer[in == 0U ? buffer_size - 1 : in - 1U]; } //************************************************************************* @@ -831,7 +831,7 @@ namespace etl { ETL_ASSERT(!empty(), ETL_ERROR(circular_buffer_empty)); - return pbuffer[in == 0U ? BUFFER_SIZE - 1 : in - 1U]; + return pbuffer[in == 0U ? buffer_size - 1 : in - 1U]; } //************************************************************************* @@ -839,7 +839,7 @@ namespace etl //************************************************************************* reference operator [](size_t index) { - return pbuffer[(out + index) % BUFFER_SIZE]; + return pbuffer[(out + index) % buffer_size]; } //************************************************************************* @@ -848,7 +848,7 @@ namespace etl //************************************************************************* const_reference operator [](size_t index) const { - return pbuffer[(out + index) % BUFFER_SIZE]; + return pbuffer[(out + index) % buffer_size]; } //************************************************************************* @@ -1025,7 +1025,7 @@ namespace etl { const difference_type index = other.get_index(); const difference_type reference_index = other.container().out; - const size_t buffer_size = other.container().BUFFER_SIZE; + const size_t buffer_size = other.container().buffer_size; if (index < reference_index) { @@ -1294,13 +1294,18 @@ namespace etl //************************************************************************* /// Swap with another circular buffer //************************************************************************* - void swap(circular_buffer_ext& other) + void swap(circular_buffer_ext& other) ETL_NOEXCEPT { using ETL_OR_STD::swap; // Allow ADL swap(this->in, other.in); swap(this->out, other.out); swap(this->pbuffer, other.pbuffer); + swap(this->buffer_size, other.buffer_size); + +#if defined(ETL_DEBUG_COUNT) + swap(this->etl_debug_count, other.etl_debug_count); +#endif } //************************************************************************* diff --git a/include/etl/debug_count.h b/include/etl/debug_count.h index aa81274f..5b6bd5be 100644 --- a/include/etl/debug_count.h +++ b/include/etl/debug_count.h @@ -31,24 +31,27 @@ SOFTWARE. #ifndef ETL_DEBUG_COUNT_INCLUDED #define ETL_DEBUG_COUNT_INCLUDED -#include #include +#include -#include "platform.h" #include "atomic.h" +#include "platform.h" ///\defgroup debug_count debug count ///\ingroup utilities #if defined(ETL_DEBUG_COUNT) -#define ETL_DECLARE_DEBUG_COUNT etl::debug_count etl_debug_count; -#define ETL_INCREMENT_DEBUG_COUNT ++etl_debug_count; -#define ETL_DECREMENT_DEBUG_COUNT --etl_debug_count; -#define ETL_ADD_DEBUG_COUNT(n) etl_debug_count += (n); -#define ETL_SUBTRACT_DEBUG_COUNT(n) etl_debug_count -= (n); -#define ETL_RESET_DEBUG_COUNT etl_debug_count.clear(); -#define ETL_OBJECT_RESET_DEBUG_COUNT(object) object.etl_debug_count.clear(); + #define ETL_DECLARE_DEBUG_COUNT etl::debug_count etl_debug_count; + #define ETL_SET_DEBUG_COUNT(n) etl_debug_count.set(n) + #define ETL_GET_DEBUG_COUNT etl_debug_count.get() + #define ETL_INCREMENT_DEBUG_COUNT ++etl_debug_count; + #define ETL_DECREMENT_DEBUG_COUNT --etl_debug_count; + #define ETL_ADD_DEBUG_COUNT(n) etl_debug_count += (n); + #define ETL_SUBTRACT_DEBUG_COUNT(n) etl_debug_count -= (n); + #define ETL_RESET_DEBUG_COUNT etl_debug_count.clear(); + #define ETL_OBJECT_RESET_DEBUG_COUNT(object) object.etl_debug_count.clear(); + #define ETL_OBJECT_GET_DEBUG_COUNT(object) object.etl_debug_count.get() namespace etl { @@ -62,7 +65,6 @@ namespace etl class debug_count { public: - debug_count() : count(0) { @@ -73,13 +75,13 @@ namespace etl assert(count == 0); } - debug_count& operator ++() + debug_count& operator++() { ++count; return *this; } - debug_count& operator --() + debug_count& operator--() { --count; assert(count >= 0); @@ -87,48 +89,85 @@ namespace etl } template - debug_count& operator +=(T n) + debug_count& operator+=(T n) { count += int32_t(n); return *this; } template - debug_count& operator -=(T n) + debug_count& operator-=(T n) { count -= int32_t(n); return *this; } - operator int32_t() + debug_count& operator=(const debug_count& other) + { + count.store(other.count.load()); + + return *this; + } + + #if ETL_HAS_ATOMIC + void swap(debug_count& other) ETL_NOEXCEPT // NOT ATOMIC + { + int32_t temp = other.count.load(); + other.count.store(count.load()); + count.store(temp); + } + #else + void swap(debug_count& other) ETL_NOEXCEPT + { + swap(count, other.count); + } + #endif + + operator int32_t() const { return count; } + int32_t get() const + { + return int32_t(count); + } + + void set(int32_t n) + { + count = n; + } + void clear() { count = 0; } + friend static void swap(etl::debug_count& lhs, etl::debug_count& rhs) + { + lhs.swap(rhs); + } + private: - -#if ETL_HAS_ATOMIC + #if ETL_HAS_ATOMIC etl::atomic_int32_t count; -#else + #else int32_t count; -#endif + #endif }; - -} +} // namespace etl #else #define ETL_DECLARE_DEBUG_COUNT + #define ETL_SET_DEBUG_COUNT(n) + #define ETL_GET_DEBUG_COUNT #define ETL_INCREMENT_DEBUG_COUNT #define ETL_DECREMENT_DEBUG_COUNT #define ETL_ADD_DEBUG_COUNT(n) #define ETL_SUBTRACT_DEBUG_COUNT(n) #define ETL_RESET_DEBUG_COUNT #define ETL_OBJECT_RESET_DEBUG_COUNT(object) -#endif // ETL_DEBUG_COUNT + #define ETL_OBJECT_GET_DEBUG_COUNT(object) +#endif // ETL_DEBUG_COUNT #endif diff --git a/test/test_circular_buffer.cpp b/test/test_circular_buffer.cpp index 9eba942c..7f59080a 100644 --- a/test/test_circular_buffer.cpp +++ b/test/test_circular_buffer.cpp @@ -74,7 +74,6 @@ namespace Data data = { Ndc("0"), Ndc("1"), Ndc("2"), Ndc("3"), Ndc("4"), Ndc("5"), Ndc("6"), Ndc("7"), Ndc("8"), Ndc("9") }; Compare compare = { Ndc("0"), Ndc("1"), Ndc("2"), Ndc("3"), Ndc("4"), Ndc("5"), Ndc("6"), Ndc("7"), Ndc("8"), Ndc("9") }; - CHECK(data.begin() != data.end()); CHECK(data.cbegin() != data.cend()); CHECK_EQUAL(compare.size(), data.size()); @@ -188,6 +187,48 @@ namespace CHECK(isEqual); } + //************************************************************************* + TEST(test_iterator_to_pointer_operator) + { + Compare test{ Ndc("0"), Ndc("1"), Ndc("2"), Ndc("3"), Ndc("4"), Ndc("5"), Ndc("6"), Ndc("7"), Ndc("8"), Ndc("9") }; + Data data; + data.push(test.begin(), test.end()); + + Data::iterator itr = data.begin(); + + CHECK_EQUAL(test[0].value, (itr++)->value); + CHECK_EQUAL(test[1].value, (itr++)->value); + CHECK_EQUAL(test[2].value, (itr++)->value); + CHECK_EQUAL(test[3].value, (itr++)->value); + CHECK_EQUAL(test[4].value, (itr++)->value); + CHECK_EQUAL(test[5].value, (itr++)->value); + CHECK_EQUAL(test[6].value, (itr++)->value); + CHECK_EQUAL(test[7].value, (itr++)->value); + CHECK_EQUAL(test[8].value, (itr++)->value); + CHECK_EQUAL(test[9].value, (itr++)->value); + } + + //************************************************************************* + TEST(test_const_iterator_to_pointer_operator) + { + Compare test{ Ndc("0"), Ndc("1"), Ndc("2"), Ndc("3"), Ndc("4"), Ndc("5"), Ndc("6"), Ndc("7"), Ndc("8"), Ndc("9") }; + Data data; + data.push(test.begin(), test.end()); + + Data::const_iterator itr = data.begin(); + + CHECK_EQUAL(test[0].value, (itr++)->value); + CHECK_EQUAL(test[1].value, (itr++)->value); + CHECK_EQUAL(test[2].value, (itr++)->value); + CHECK_EQUAL(test[3].value, (itr++)->value); + CHECK_EQUAL(test[4].value, (itr++)->value); + CHECK_EQUAL(test[5].value, (itr++)->value); + CHECK_EQUAL(test[6].value, (itr++)->value); + CHECK_EQUAL(test[7].value, (itr++)->value); + CHECK_EQUAL(test[8].value, (itr++)->value); + CHECK_EQUAL(test[9].value, (itr++)->value); + } + //************************************************************************* TEST(test_push_full_range_reverse_iterator) { @@ -894,18 +935,19 @@ namespace //************************************************************************* TEST(test_swap) { - // Over-write by 3 - Compare input{ Ndc("0"), Ndc("1"), Ndc("2"), Ndc("3"), Ndc("4"), Ndc("5"), Ndc("6"), Ndc("7"), Ndc("8"), Ndc("9"), Ndc("10"), Ndc("11"), Ndc("12") }; - Compare output{ Ndc("0"), Ndc("1"), Ndc("2"), Ndc("3"), Ndc("4"), Ndc("5"), Ndc("6"), Ndc("7"), Ndc("8"), Ndc("9"), Ndc("10"), Ndc("11"), Ndc("12") }; + Compare input1{ Ndc("0"), Ndc("1"), Ndc("2"), Ndc("3"), Ndc("4") }; + Compare input2{ Ndc("7"), Ndc("8"), Ndc("9"), Ndc("10"), Ndc("11"), Ndc("12") }; Data data1; Data data2; - data1.push(input.begin(), input.end()); - data2.push(input.rbegin(), input.rend()); + data1.push(input1.begin(), input1.end()); + data2.push(input2.begin(), input2.end()); swap(data1, data2); - CHECK(std::equal(output.rbegin() + 3, output.rend(), data1.begin())); - CHECK(std::equal(output.begin() + 3, output.end(), data2.begin())); + CHECK_EQUAL(input1.size(), data2.size()); + CHECK_EQUAL(input2.size(), data1.size()); + CHECK(std::equal(input1.begin(), input1.end(), data2.begin())); + CHECK(std::equal(input2.begin(), input2.end(), data1.begin())); } //************************************************************************* diff --git a/test/test_circular_buffer_external_buffer.cpp b/test/test_circular_buffer_external_buffer.cpp index 1fac2142..1c1d08aa 100644 --- a/test/test_circular_buffer_external_buffer.cpp +++ b/test/test_circular_buffer_external_buffer.cpp @@ -197,6 +197,48 @@ namespace CHECK(isEqual); } + //************************************************************************* + TEST(test_iterator_to_pointer_operator) + { + Compare test{ Ndc("0"), Ndc("1"), Ndc("2"), Ndc("3"), Ndc("4"), Ndc("5"), Ndc("6"), Ndc("7"), Ndc("8"), Ndc("9") }; + Data data(buffer1.raw, SIZE); + data.push(test.begin(), test.end()); + + Data::iterator itr = data.begin(); + + CHECK_EQUAL(test[0].value, (itr++)->value); + CHECK_EQUAL(test[1].value, (itr++)->value); + CHECK_EQUAL(test[2].value, (itr++)->value); + CHECK_EQUAL(test[3].value, (itr++)->value); + CHECK_EQUAL(test[4].value, (itr++)->value); + CHECK_EQUAL(test[5].value, (itr++)->value); + CHECK_EQUAL(test[6].value, (itr++)->value); + CHECK_EQUAL(test[7].value, (itr++)->value); + CHECK_EQUAL(test[8].value, (itr++)->value); + CHECK_EQUAL(test[9].value, (itr++)->value); + } + + //************************************************************************* + TEST(test_const_iterator_to_pointer_operator) + { + Compare test{ Ndc("0"), Ndc("1"), Ndc("2"), Ndc("3"), Ndc("4"), Ndc("5"), Ndc("6"), Ndc("7"), Ndc("8"), Ndc("9") }; + Data data(buffer1.raw, SIZE); + data.push(test.begin(), test.end()); + + Data::const_iterator itr = data.begin(); + + CHECK_EQUAL(test[0].value, (itr++)->value); + CHECK_EQUAL(test[1].value, (itr++)->value); + CHECK_EQUAL(test[2].value, (itr++)->value); + CHECK_EQUAL(test[3].value, (itr++)->value); + CHECK_EQUAL(test[4].value, (itr++)->value); + CHECK_EQUAL(test[5].value, (itr++)->value); + CHECK_EQUAL(test[6].value, (itr++)->value); + CHECK_EQUAL(test[7].value, (itr++)->value); + CHECK_EQUAL(test[8].value, (itr++)->value); + CHECK_EQUAL(test[9].value, (itr++)->value); + } + //************************************************************************* TEST(test_push_full_range_reverse_iterator) { @@ -903,18 +945,22 @@ namespace //************************************************************************* TEST(test_swap) { - // Over-write by 3 - Compare input{ Ndc("0"), Ndc("1"), Ndc("2"), Ndc("3"), Ndc("4"), Ndc("5"), Ndc("6"), Ndc("7"), Ndc("8"), Ndc("9"), Ndc("10"), Ndc("11"), Ndc("12") }; - Compare output{ Ndc("0"), Ndc("1"), Ndc("2"), Ndc("3"), Ndc("4"), Ndc("5"), Ndc("6"), Ndc("7"), Ndc("8"), Ndc("9"), Ndc("10"), Ndc("11"), Ndc("12") }; - Data data1(buffer1.raw, SIZE); - Data data2(buffer2.raw, SIZE); - data1.push(input.begin(), input.end()); - data2.push(input.rbegin(), input.rend()); + etl::uninitialized_buffer_of buffer1; + etl::uninitialized_buffer_of buffer2; + + Compare input1{ Ndc("0"), Ndc("1"), Ndc("2"), Ndc("3"), Ndc("4") }; + Compare input2{ Ndc("7"), Ndc("8"), Ndc("9"), Ndc("10"), Ndc("11"), Ndc("12") }; + Data data1(buffer1.raw, 5U); + Data data2(buffer2.raw, 6U); + data1.push(input1.begin(), input1.end()); + data2.push(input2.begin(), input2.end()); swap(data1, data2); - CHECK(std::equal(output.rbegin() + 3, output.rend(), data1.begin())); - CHECK(std::equal(output.begin() + 3, output.end(), data2.begin())); + CHECK_EQUAL(input1.size(), data2.size()); + CHECK_EQUAL(input2.size(), data1.size()); + CHECK(std::equal(input1.begin(), input1.end(), data2.begin())); + CHECK(std::equal(input2.begin(), input2.end(), data1.begin())); } //************************************************************************* diff --git a/test/vs2019/etl.vcxproj b/test/vs2019/etl.vcxproj index 5d0aa29e..9a1a1e69 100644 --- a/test/vs2019/etl.vcxproj +++ b/test/vs2019/etl.vcxproj @@ -2655,6 +2655,62 @@ + + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + + + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true true @@ -2756,6 +2812,62 @@ + + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + + + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true true @@ -11975,6 +12087,7 @@ + @@ -11986,6 +12099,62 @@ + + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + + + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true true diff --git a/test/vs2019/etl.vcxproj.filters b/test/vs2019/etl.vcxproj.filters index c64a775c..d7fccd5f 100644 --- a/test/vs2019/etl.vcxproj.filters +++ b/test/vs2019/etl.vcxproj.filters @@ -217,9 +217,6 @@ {de372ec9-d193-4956-871f-065414a9d3be} - - {1c718339-9f2d-4c83-a39e-66b7300df135} - @@ -1308,6 +1305,12 @@ Tests\Error Handler\Log Errors + + Tests\Error Handler\Exceptions + + + Tests\Error Handler\Exceptions_And_Log_Errors + @@ -3305,6 +3308,12 @@ Tests\Error Handler\Log Errors + + Tests\Error Handler\Exceptions + + + Tests\Error Handler\Exceptions_And_Log_Errors + @@ -3409,6 +3418,9 @@ Tests\Scripts + + Tests\Scripts + @@ -3447,6 +3459,12 @@ Tests\Error Handler\Log Errors + + Tests\Error Handler\Exceptions + + + Tests\Error Handler\Exceptions_And_Log_Errors +