Skip to content

Commit 655718c

Browse files
committed
cleanup CircularBuffer class, fixed #157
1 parent 90b6535 commit 655718c

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

src/CircularBuffer.hpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ template <typename T> class CircularBuffer
2424
CircularBuffer() = delete;
2525
CircularBuffer &operator=(const CircularBuffer &) = delete;
2626
CircularBuffer(CircularBuffer &other) = delete;
27-
void addItem(const T &item);
28-
T &getTop() noexcept;
27+
void addItem(const T &item); // copy overload
28+
void addItem(T &&item); // Move overload
29+
T &getFront() noexcept;
2930
void removeFirst();
30-
[[nodiscard]] bool isEmpty() const;
31+
[[nodiscard]] bool isEmpty() const noexcept;
3132
const std::deque<T> &getAll() const;
3233
void clean();
3334

@@ -50,7 +51,7 @@ template <typename T> void CircularBuffer<T>::clean()
5051
* @tparam T any type
5152
* @return TRUE or FALSE
5253
*/
53-
template <typename T> bool CircularBuffer<T>::isEmpty() const
54+
template <typename T> bool CircularBuffer<T>::isEmpty() const noexcept
5455
{
5556
return m_deque.empty();
5657
}
@@ -68,7 +69,7 @@ template <typename T> inline const std::deque<T> &CircularBuffer<T>::getAll() co
6869
* @tparam T any type
6970
* @return The first element in queue
7071
*/
71-
template <typename T> T &CircularBuffer<T>::getTop() noexcept
72+
template <typename T> T &CircularBuffer<T>::getFront() noexcept
7273
{
7374
return m_deque.front();
7475
}
@@ -78,11 +79,14 @@ template <typename T> T &CircularBuffer<T>::getTop() noexcept
7879
*/
7980
template <typename T> void CircularBuffer<T>::removeFirst()
8081
{
81-
m_deque.pop_front();
82+
if (!m_deque.empty())
83+
{
84+
m_deque.pop_front();
85+
}
8286
}
8387

8488
/**
85-
* Add new element to buffer
89+
* Add new element to buffer (using copy)
8690
* @tparam T any type
8791
* @param item the element to be inserted
8892
*/
@@ -95,4 +99,18 @@ template <typename T> void CircularBuffer<T>::addItem(const T &item)
9599
}
96100
m_deque.emplace_back(item);
97101
}
102+
103+
/**
104+
* Move element into the buffer
105+
* @tparam T any type
106+
* @param item the element to be inserted
107+
*/
108+
template <typename T> void CircularBuffer<T>::addItem(T &&item)
109+
{
110+
if (m_deque.size() >= max_capacity)
111+
{
112+
m_deque.pop_front();
113+
}
114+
m_deque.emplace_back(std::move(item));
115+
}
98116
} // namespace chk

src/managers/GameManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ void chk::GameManager::handleCellTap(const chk::PlayerPtr &hunter, const chk::Pl
314314
// Cell is Empty! Let's judge if this is SIMPLE move or ATTACK move
315315
if (!buffer.isEmpty())
316316
{
317-
const short movablePieceId = buffer.getTop();
317+
const short movablePieceId = buffer.getFront();
318318
if (!hunter->hasThisPiece(movablePieceId))
319319
{
320320
return;

src/managers/OnlineGameManager.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ inline void OnlineGameManager::handleCellTap(const chk::PlayerPtr &hunter, const
417417
// Cell is Empty! Let's judge if this is SIMPLE move or ATTACK move
418418
if (!buffer.isEmpty())
419419
{
420-
const short movablePieceId = buffer.getTop();
420+
const short movablePieceId = buffer.getFront();
421421
if (!hunter->hasThisPiece(movablePieceId))
422422
{
423423
return;

0 commit comments

Comments
 (0)