/* libobby - Network text editing library * Copyright (C) 2005 0x539 dev group * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef _OBBY_RING_HPP_ #define _OBBY_RING_HPP_ #include #include namespace obby { /** STL style ring container class. A ring container is a container with a * fixed size that allows to insert more elements as the container is able to * hold. Older elements will be removed in this case. */ template class ring { public: typedef std::list container_type; typedef typename container_type::size_type size_type; typedef typename container_type::iterator iterator; typedef typename container_type::const_iterator const_iterator; /** Creates a ring with n elements before wrapping around. */ ring(size_type n); /** Adds a new element to the ring. If the ring is full, the oldest * element will be overridden. */ void push_back(const value_type& val); /** Removes the last (newest) element from the ring. The ring will * shrink after this and the next call to push_back will never overwrite * an element. */ void pop_back(); /** Returns the first (oldest) element in the ring. */ const value_type& front() const; /** Returns the last (newest) element in the ring. */ const value_type& back() const; /** Checks if the ring contains any elements. If not, calls to * pop_back, front or back will not succeed. */ bool empty() const; /** Clears all contents within the ring. */ void clear(); /** Returns an STL-like iterator pointing at the first (oldest) element * in the ring. */ iterator begin(); /** Returns an STL-like iterator pointing after the * last (newest) element in the ring. */ iterator end(); /** Returns an STL-like const_iterator pointing at the first * (oldest) element in the ring. */ const_iterator begin() const; /** Returns an STL-like const_iterator pointing after * the last (newest) element in the ring. */ const_iterator end() const; private: container_type m_elems; size_type m_n; }; template ring::ring(size_type n): m_n(n) { } template void ring::push_back(const value_type& val) { m_elems.push_back(val); while(m_elems.size() > m_n) m_elems.pop_front(); } template void ring::pop_back() { m_elems.pop_back(); } template const value_type& ring::front() const { return m_elems.front(); } template const value_type& ring::back() const { return m_elems.back(); } template bool ring::empty() const { return m_elems.empty(); } template void ring::clear() { m_elems.clear(); } template typename ring::const_iterator ring::begin() const { return m_elems.begin(); } template typename ring::const_iterator ring::end() const { return m_elems.end(); } template typename ring::iterator ring::begin() { return m_elems.begin(); } template typename ring::iterator ring::end() { return m_elems.end(); } } // namespace obby #endif // _OBBY_RING_HPP_