semf
lastinbuffer.h
Go to the documentation of this file.
1
10#ifndef SEMF_UTILS_CORE_BUFFER_LASTINBUFFER_H_
11#define SEMF_UTILS_CORE_BUFFER_LASTINBUFFER_H_
12
14
15namespace semf
16{
38template <typename T>
39class LastInBuffer : public Buffer<T>
40{
41public:
42 using Buffer<T>::Buffer;
43 virtual ~LastInBuffer() = default;
44
49 virtual void put(T data);
54 void fill(T data);
60 virtual T& operator[](size_t pos) const;
65 void setPos(size_t pos);
70 size_t pos() const;
71
72private:
74 size_t m_pos = 0;
75};
76
77template <typename T>
79{
81 this->data()[m_pos++] = data;
82 m_pos %= this->size();
84}
85
86template <typename T>
88{
89 for (size_t i = 0; i < this->size(); i++)
90 {
92 this->data()[i] = data;
94 }
95}
96
97template <typename T>
98T& LastInBuffer<T>::operator[](size_t pos) const
99{
100 pos %= this->size();
101
103 T& data = m_pos >= pos + 1 ? this->data()[m_pos - (pos + 1)] : this->data()[(this->size() + m_pos) - (pos + 1)];
105 return data;
106}
107
108template <typename T>
110{
111 m_pos = pos % this->size();
112}
113
114template <typename T>
116{
117 return m_pos;
118}
119} /* namespace semf */
120#endif /* SEMF_UTILS_CORE_BUFFER_LASTINBUFFER_H_ */
Buffer is a base class for buffer implementations.
Definition: buffer.h:42
T * data() const
Returns the pointer to the first element of the data array.
Definition: buffer.h:139
Buffer()=default
The LastInBuffer class implements a kind of circular buffer.
Definition: lastinbuffer.h:40
void setPos(size_t pos)
Sets the write position of the buffer.
Definition: lastinbuffer.h:109
size_t pos() const
Returns the current write position.
Definition: lastinbuffer.h:115
virtual void put(T data)
Writes data in the buffer.
Definition: lastinbuffer.h:78
virtual ~LastInBuffer()=default
virtual T & operator[](size_t pos) const
Returns the entry from a specific position in the buffer.
Definition: lastinbuffer.h:98
void fill(T data)
Fills the whole buffer with the same object content.
Definition: lastinbuffer.h:87