Merge pull request #8337 from CookiePLMonster/log-widget-improvements

Log widget improvements
This commit is contained in:
Connor McLaughlin 2019-10-01 23:42:16 +10:00 committed by GitHub
commit b319f823bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 195 additions and 97 deletions

View file

@ -6,6 +6,7 @@
#include <array>
#include <cstddef>
#include <type_traits>
#include <utility>
// 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<T>)
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 <class... Args>
void emplace(Args&&... args)
{
if (count == N)
head = (head + 1) % N;
else
count++;
storage[tail] = T(std::forward<Args>(args)...);
tail = (tail + 1) % N;
}
void pop()
{
head++;
if (head == N)
head = 0;
if constexpr (!std::is_trivial_v<T>)
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<T, N> storage;