29 #ifndef ICL_CORE_T_RING_BUFFER_H_INCLUDED 30 #define ICL_CORE_T_RING_BUFFER_H_INCLUDED 48 static const size_type cDEFAULT_CAPACITY = 32;
52 : m_buffer(capacity+1), m_write(0), m_read(0)
57 : m_buffer(other.m_buffer), m_write(other.m_write), m_read(other.m_read)
70 void Clear() { m_write = m_read = 0; }
77 void Write(
const T& val,
bool overwrite=
false)
79 size_type new_write_pos = m_write+1;
80 if (new_write_pos >= m_buffer.size())
84 if (new_write_pos == m_read)
92 throw std::out_of_range(
"tRingBuffer::Write: capacity exceeded");
95 m_buffer[m_write] = val;
96 m_write = new_write_pos;
104 const T&
At(size_type pos)
const 109 if (pos >= m_buffer.size())
111 pos -= m_buffer.size();
113 return m_buffer[pos];
117 throw std::out_of_range(
"tRingBuffer::Peek: out of range");
131 if (pos >= m_buffer.size())
133 pos -= m_buffer.size();
135 return m_buffer[pos];
139 throw std::out_of_range(
"tRingBuffer::Peek: out of range");
146 if (m_write == m_read)
148 throw std::out_of_range(
"tRingBuffer::Skip: buffer empty");
151 if (m_read >= m_buffer.size())
162 if (m_write == m_read)
164 throw std::out_of_range(
"tRingBuffer::Read: buffer empty");
166 size_type read_pos = m_read;
168 if (m_read >= m_buffer.size())
172 return m_buffer[read_pos];
178 if (m_write >= m_read)
180 return m_write-m_read;
184 return m_write+m_buffer.size()-m_read;
189 size_type
Capacity()
const {
return m_buffer.size()-1; }
198 size_type old_size = Size();
199 size_type new_size = (capacity < old_size) ? capacity : old_size;
200 std::vector<T> old_buffer(m_buffer);
201 size_type old_read = m_read;
204 m_buffer.resize(capacity+1);
206 old_read += old_size-new_size;
207 if (old_read >= old_buffer.size())
209 old_read -= old_buffer.size();
212 for (size_type i=0; i<new_size; i++)
214 m_buffer[i] = old_buffer[old_read];
216 if (old_read >= old_buffer.size())
tRingBuffer(const tRingBuffer< T > &other)
Copy constructor.
size_type Capacity() const
Returns the capacity of the ring buffer.
#define ICL_CORE_VC_DEPRECATE
void SetCapacity(size_type capacity)
Changes the capacity of the ring buffer.
A simple ring buffer implementation based on std::vector.
tRingBuffer(size_type capacity=cDEFAULT_CAPACITY)
Default constructor.
Contains macros to deprecate classes, types, functions and variables.
T Read()
Removes an element from the ring buffer provided there is one present.
const T & At(size_type pos) const
Read an arbitrary element from the ring buffer without removing it.
void Clear()
Clears the ring buffer.
void Skip()
Removes an element from the ring buffer without returning it.
size_type Size() const
Returns the current number of elements in the ring buffer.
Contains Interface base classes and base types.
ICL_CORE_VC_DEPRECATE SetRelation tSetRelation ICL_CORE_GCC_DEPRECATE
T & At(size_type pos)
Access an arbitrary element in the ring buffer without removing it.
void Write(const T &val, bool overwrite=false)
Adds an element to the ring buffer provided there is room.
std::vector< T > m_buffer