diff options
| author | Matthew Parlane <parlane@gmail.com> | 2016-06-26 10:31:07 +1200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-06-26 10:31:07 +1200 |
| commit | 5ba3e641ea6212a3e7d7e9f443b1dba985e5d8ca (patch) | |
| tree | 32768119a90856570f7c9e92ddb477d8e97007c8 | |
| parent | 10682dbf5843bcf55cda7db4bc4e5acaee556648 (diff) | |
| parent | 9081d029e304cfeb8650dab58efc526068a4ea03 (diff) | |
Merge pull request #3933 from delroth/fixed-size-queue
FixedSizeQueue: modernize (std::array, std::move)
| -rw-r--r-- | Source/Core/Common/FixedSizeQueue.h | 32 |
1 files changed, 12 insertions, 20 deletions
diff --git a/Source/Core/Common/FixedSizeQueue.h b/Source/Core/Common/FixedSizeQueue.h index 4afd38dbe7..b8fb345fb1 100644 --- a/Source/Core/Common/FixedSizeQueue.h +++ b/Source/Core/Common/FixedSizeQueue.h @@ -4,33 +4,19 @@ #pragma once +#include <array> #include <cstddef> +#include <utility> // STL-look-a-like interface, but name is mixed case to distinguish it clearly from the // real STL classes. - +// // Not fully featured, no safety checking yet. Add features as needed. -// TODO: "inline" storage? - template <class T, int N> class FixedSizeQueue { - T* storage; - int head; - int tail; - int count; // sacrifice 4 bytes for a simpler implementation. may optimize away in the future. - - // Make copy constructor private for now. - FixedSizeQueue(FixedSizeQueue& other) {} public: - FixedSizeQueue() - { - storage = new T[N]; - clear(); - } - - ~FixedSizeQueue() { delete[] storage; } void clear() { head = 0; @@ -40,7 +26,7 @@ public: void push(T t) { - storage[tail] = t; + storage[tail] = std::move(t); tail++; if (tail == N) tail = 0; @@ -57,12 +43,18 @@ public: T pop_front() { - const T& temp = storage[head]; + T& temp = storage[head]; pop(); - return temp; + return std::move(temp); } T& front() { return storage[head]; } const T& front() const { return storage[head]; } size_t size() const { return count; } +private: + std::array<T, N> storage; + int head = 0; + int tail = 0; + // Sacrifice 4 bytes for a simpler implementation. may optimize away in the future. + int count = 0; }; |
