Skip to content

Commit 5f7ded8

Browse files
author
dalj8690
committed
Fixing the RingBuffer implementation
1 parent d911090 commit 5f7ded8

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/ring buffer/RingBuffer_c++20.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@
2222

2323

2424

25-
namespace utils
25+
namespace utils::rb
2626
{
2727
template <std::size_t N>
28-
constexpr bool is_power_of_2 = (N & (N - 1)) == 0;
28+
constexpr bool is_power_of_2 = (N > 0) and (N & (N - 1)) == 0;
2929

3030
template <typename Byte>
3131
static constexpr bool is_byte
32-
= std::is_same_v<Byte, unsigned char> || std::is_same_v<Byte, std::uint8_t> || std::is_same_v<Byte, std::byte>;
32+
= std::is_same_v<Byte, unsigned char> or std::is_same_v<Byte, std::uint8_t> or std::is_same_v<Byte, std::byte>;
3333

3434
template <typename T, std::size_t BlockSize>
35-
requires is_byte<T> && is_power_of_2<BlockSize>
35+
requires is_power_of_2<BlockSize>
3636
struct block final
3737
{
3838
std::size_t size_; // the size - number of actually stored data
@@ -54,7 +54,7 @@ namespace utils
5454
* @tparam BlockSize The size of the each slot, in elements of type T
5555
*/
5656
template <typename T, std::size_t Blocks, std::size_t BlockSize>
57-
requires is_byte<T> && is_power_of_2<Blocks>
57+
requires is_power_of_2<Blocks>
5858
class RingBuffer
5959
{
6060

@@ -90,8 +90,7 @@ namespace utils
9090
auto& block = blocks_[writeIndex_];
9191
block.size_ = written;
9292

93-
auto&& col = std::forward<Collection>(collection);
94-
std::copy(col.cbegin(), std::next(col.cbegin(), written), block.data_.begin());
93+
std::copy_n(std::begin(std::forward<Collection>(collection)), written, block.data_.begin());
9594
writeIndex_ = (writeIndex_ + 1) & MASK;
9695
}
9796

@@ -178,6 +177,7 @@ namespace utils
178177

179178
if (writeIndex_ == readIndex_) return false;
180179
std::invoke(std::forward<Func>(func), blocks_[readIndex_]);
180+
readIndex_ = (readIndex_ + 1) & MASK;
181181
} // unlock
182182

183183
writeSemaphore_.release();
@@ -255,7 +255,7 @@ namespace test {
255255
{
256256
using namespace std::chrono_literals;
257257

258-
using ring_buffer_t = utils::RingBuffer<A, 5, 10>;
258+
using ring_buffer_t = utils::rb::RingBuffer<A, 8, 16>;
259259

260260
std::shared_ptr<ring_buffer_t> ringBuffer = std::make_shared<ring_buffer_t>();
261261
std::stop_source stop;

0 commit comments

Comments
 (0)