@@ -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 */
7980template <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
0 commit comments