From aacea38f9df734c2f083d866296d450971b6ada8 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 1 Sep 2023 12:49:15 +0300 Subject: [PATCH] Updated details::circular_q and added some tests for it --- include/spdlog/details/circular_q.h | 31 +++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index 90a82eef..9deade41 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -90,17 +90,26 @@ public: // Return const reference to item by index. // If index is out of range 0…size()-1, the behavior is undefined. - const T &at(size_t i) const + const T &operator[](size_t idx) const { - assert(i < size()); - return v_[(head_ + i) % max_items_]; + assert(idx < size()); + assert(max_items_ > 0); + return v_[(head_ + idx) % max_items_]; } - // Pop item from front. - // If there are no elements in the container, the behavior is undefined. + // Return reference to item by index. + T &operator[](size_t idx) + { + return const_cast(static_cast(*this)[idx]); + } + + // Pop item from front if exists. void pop_front() { - head_ = (head_ + 1) % max_items_; + if(!empty()) + { + head_ = (head_ + 1) % max_items_; + } } [[nodiscard]] bool empty() const @@ -115,7 +124,7 @@ public: { return ((tail_ + 1) % max_items_) == head_; } - return false; + return true; } [[nodiscard]] size_t overrun_counter() const @@ -145,7 +154,7 @@ public: reference operator*() const { - return cq_->v_[(cq_->head_ + index_) % cq_->max_items_]; + return cq_->operator[](index_); } pointer operator->() @@ -153,15 +162,17 @@ public: return &operator*(); } + // Prefix increment iterator &operator++() { - if (cq_ != nullptr) + if(cq_ != nullptr) { - index_ = (index_ + 1) % cq_->max_items_; + index_ = (index_ + 1); } return *this; } + // Postfix increment iterator operator++(int) { iterator retval = *this;