mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-08-17 01:10:28 +03: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
Notes:
sideshowbarker
2024-07-18 22:42:25 +09:00
Author: https://github.com/ldm5180 Commit: https://github.com/SerenityOS/serenity/commit/2490fc79ad6 Pull-request: https://github.com/SerenityOS/serenity/pull/5193
@ -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 New Issue
Block a user