mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:47:45 +00:00
CircularQueue: Correctly pass args to enqueue
Problem: - Using regular functions rather than function templates results in the arguments not being deduced. This then requires the same function to be written multiple times and for `move` to be used rather than `forward`. Solution: - Collapse multiple function overloads to a single function template with a deduced argument. This allows the argument to be a forwarding reference and bind to either an l-value or r-value and forward the value.
This commit is contained in:
parent
a2501c3981
commit
2490fc79ad
1 changed files with 3 additions and 7 deletions
|
@ -60,24 +60,20 @@ public:
|
||||||
|
|
||||||
size_t capacity() const { return Capacity; }
|
size_t capacity() const { return Capacity; }
|
||||||
|
|
||||||
void enqueue(T&& value)
|
template<typename U = T>
|
||||||
|
void enqueue(U&& value)
|
||||||
{
|
{
|
||||||
auto& slot = elements()[(m_head + m_size) % Capacity];
|
auto& slot = elements()[(m_head + m_size) % Capacity];
|
||||||
if (m_size == Capacity)
|
if (m_size == Capacity)
|
||||||
slot.~T();
|
slot.~T();
|
||||||
|
|
||||||
new (&slot) T(move(value));
|
new (&slot) T(forward<U>(value));
|
||||||
if (m_size == Capacity)
|
if (m_size == Capacity)
|
||||||
m_head = (m_head + 1) % Capacity;
|
m_head = (m_head + 1) % Capacity;
|
||||||
else
|
else
|
||||||
++m_size;
|
++m_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void enqueue(const T& value)
|
|
||||||
{
|
|
||||||
enqueue(T(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
T dequeue()
|
T dequeue()
|
||||||
{
|
{
|
||||||
ASSERT(!is_empty());
|
ASSERT(!is_empty());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue