diff --git a/Source/Core/Common/FixedSizeQueue.h b/Source/Core/Common/FixedSizeQueue.h index 267b3d8c4b..149ee9846b 100644 --- a/Source/Core/Common/FixedSizeQueue.h +++ b/Source/Core/Common/FixedSizeQueue.h @@ -6,6 +6,7 @@ #include #include +#include #include // STL-look-a-like interface, but name is mixed case to distinguish it clearly from the @@ -19,6 +20,9 @@ class FixedSizeQueue public: void clear() { + if constexpr (!std::is_trivial_v) + storage = {}; + head = 0; tail = 0; count = 0; @@ -26,31 +30,47 @@ public: void push(T t) { + if (count == N) + head = (head + 1) % N; + else + count++; + storage[tail] = std::move(t); - tail++; - if (tail == N) - tail = 0; - count++; + tail = (tail + 1) % N; + } + + template + void emplace(Args&&... args) + { + if (count == N) + head = (head + 1) % N; + else + count++; + + storage[tail] = T(std::forward(args)...); + tail = (tail + 1) % N; } void pop() { - head++; - if (head == N) - head = 0; + if constexpr (!std::is_trivial_v) + storage[head] = {}; + + head = (head + 1) % N; count--; } T pop_front() { - T& temp = storage[head]; + T temp = std::move(front()); pop(); - return std::move(temp); + return temp; } - T& front() { return storage[head]; } - const T& front() const { return storage[head]; } - size_t size() const { return count; } + T& front() noexcept { return storage[head]; } + const T& front() const noexcept { return storage[head]; } + size_t size() const noexcept { return count; } + bool empty() const noexcept { return size() == 0; } private: std::array storage; diff --git a/Source/UnitTests/Common/FixedSizeQueueTest.cpp b/Source/UnitTests/Common/FixedSizeQueueTest.cpp index 1461ad672f..a6f6d7e833 100644 --- a/Source/UnitTests/Common/FixedSizeQueueTest.cpp +++ b/Source/UnitTests/Common/FixedSizeQueueTest.cpp @@ -31,3 +31,85 @@ TEST(FixedSizeQueue, Simple) EXPECT_EQ(0u, q.size()); } + +TEST(FixedSizeQueue, RingBuffer) +{ + // Testing if queue works when used as a ring buffer + FixedSizeQueue q; + + EXPECT_EQ(0u, q.size()); + + q.push(0); + q.push(1); + q.push(2); + q.push(3); + q.push(4); + q.push(5); + + EXPECT_EQ(5u, q.size()); + EXPECT_EQ(1, q.pop_front()); + + EXPECT_EQ(4u, q.size()); +} + +// Local classes cannot have static fields, +// therefore this has to be declared in global scope. +class NonTrivialTypeTestData +{ +public: + static inline int num_objects = 0; + static inline int total_constructed = 0; + static inline int default_constructed = 0; + static inline int total_destructed = 0; + + NonTrivialTypeTestData() + { + num_objects++; + total_constructed++; + default_constructed++; + } + + NonTrivialTypeTestData(int /*val*/) + { + num_objects++; + total_constructed++; + } + + ~NonTrivialTypeTestData() + { + num_objects--; + total_destructed++; + } +}; + +TEST(FixedSizeQueue, NonTrivialTypes) +{ + // Testing if construction/destruction of non-trivial types happens as expected + FixedSizeQueue q; + + EXPECT_EQ(0u, q.size()); + + EXPECT_EQ(2, NonTrivialTypeTestData::num_objects); + EXPECT_EQ(2, NonTrivialTypeTestData::total_constructed); + EXPECT_EQ(2, NonTrivialTypeTestData::default_constructed); + EXPECT_EQ(0, NonTrivialTypeTestData::total_destructed); + + q.emplace(4); + q.emplace(6); + q.emplace(8); + + EXPECT_EQ(2, NonTrivialTypeTestData::num_objects); + EXPECT_EQ(2 + 3, NonTrivialTypeTestData::total_constructed); + EXPECT_EQ(2, NonTrivialTypeTestData::default_constructed); + EXPECT_EQ(3, NonTrivialTypeTestData::total_destructed); + EXPECT_EQ(2u, q.size()); + + q.pop(); + q.pop(); + + EXPECT_EQ(2, NonTrivialTypeTestData::num_objects); + EXPECT_EQ(2 + 3 + 2, NonTrivialTypeTestData::total_constructed); + EXPECT_EQ(2 + 2, NonTrivialTypeTestData::default_constructed); + EXPECT_EQ(3 + 2, NonTrivialTypeTestData::total_destructed); + EXPECT_EQ(0u, q.size()); +}