mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-07 00:19:27 +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
|
@ -60,24 +60,20 @@ public:
|
|||
|
||||
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];
|
||||
if (m_size == Capacity)
|
||||
slot.~T();
|
||||
|
||||
new (&slot) T(move(value));
|
||||
new (&slot) T(forward<U>(value));
|
||||
if (m_size == Capacity)
|
||||
m_head = (m_head + 1) % Capacity;
|
||||
else
|
||||
++m_size;
|
||||
}
|
||||
|
||||
void enqueue(const T& value)
|
||||
{
|
||||
enqueue(T(value));
|
||||
}
|
||||
|
||||
T dequeue()
|
||||
{
|
||||
ASSERT(!is_empty());
|
||||
|
|
Loading…
Reference in a new issue