Xyris  0.5
RingBuffer< T, S > Class Template Reference

#include <RingBuffer.hpp>

Public Member Functions

 RingBuffer ()
 
int Enqueue (T val)
 
int Dequeue (T *buf)
 
Dequeue ()
 
int Peek (T *buf)
 
bool IsEmpty ()
 
bool IsFull ()
 
int Length ()
 
int Capacity ()
 
int Error ()
 

Private Attributes

data [S]
 
size_t head
 
size_t tail
 
size_t length
 
int error
 

Detailed Description

template<typename T, size_t S>
class RingBuffer< T, S >

Definition at line 19 of file RingBuffer.hpp.

Constructor & Destructor Documentation

◆ RingBuffer()

template<typename T , size_t S>
RingBuffer< T, S >::RingBuffer ( )
inlineexplicit

Initializes the circular buffer and allocates the data memory.

Definition at line 25 of file RingBuffer.hpp.

26  : head(0)
27  , tail(0)
28  , length(0)
29  , error(0)
30  {
31  // Default constructor
32  }

Member Function Documentation

◆ Capacity()

template<typename T , size_t S>
int RingBuffer< T, S >::Capacity ( )
inline

Returns the buffer capacity (in number of bytes).

Returns
int Buffer capacity in bytes

Definition at line 162 of file RingBuffer.hpp.

163  {
164  return S;
165  }

◆ Dequeue() [1/2]

template<typename T , size_t S>
T RingBuffer< T, S >::Dequeue ( )
inline

Dequeues from the circular buffer and returns the data.

Returns
T Returns dequeued data.

Definition at line 86 of file RingBuffer.hpp.

87  {
88  T val = 0;
89  // Check if the buffer is empty. If so, we can't dequeue
90  if (IsEmpty()) {
92  } else {
93  // Read out the data and decrement the position
94  val = this->data[this->tail];
95  this->tail = ((this->tail + 1) % S);
96  --this->length;
97  }
98  // Return the status code
99  return val;
100  }
+ Here is the call graph for this function:

◆ Dequeue() [2/2]

template<typename T , size_t S>
int RingBuffer< T, S >::Dequeue ( T *  buf)
inline

Dequeues from the circular buffer and writes the value to the data pointer.

Parameters
bufBuffer to contain the data
Returns
int Returns 0 on success and -1 on error.

Definition at line 63 of file RingBuffer.hpp.

64  {
65  int status = 0;
66  // Check if the buffer is empty. If so, we can't dequeue
67  if (IsEmpty()) {
68  status = -1;
70  } else {
71  // Read out the data and decrement the position
72  *buf = this->data[this->tail];
73  this->tail = ((this->tail + 1) % S);
74  --this->length;
75  }
76  // Return the status code
77  return status;
78  }
+ Here is the call graph for this function:

◆ Enqueue()

template<typename T , size_t S>
int RingBuffer< T, S >::Enqueue ( val)
inline

Writes a byte into the circular buffer.

Parameters
valData to write to the buffer

Definition at line 39 of file RingBuffer.hpp.

40  {
41  int status = 0;
42  // Check if the buffer is full. If so, we can't enqueue.
43  if (IsFull()) {
44  status = -1;
45  error = BufferFull;
46  } else {
47  // Write the data at the write index
48  this->data[this->head] = val;
49  this->head = ((this->head + 1) % S);
50  ++this->length;
51  }
52  // Return the status code
53  return status;
54  }
+ Here is the call graph for this function:

◆ Error()

template<typename T , size_t S>
int RingBuffer< T, S >::Error ( )
inline

Returns the ring buffer error code.

Returns
int Error code

Definition at line 172 of file RingBuffer.hpp.

173  {
174  return error;
175  }

◆ IsEmpty()

template<typename T , size_t S>
bool RingBuffer< T, S >::IsEmpty ( )
inline

Query whether the circular buffer is empty.

Returns
true The buffer is empty
false The buffer is not empty

Definition at line 131 of file RingBuffer.hpp.

132  {
133  return this->length == 0;
134  }
+ Here is the caller graph for this function:

◆ IsFull()

template<typename T , size_t S>
bool RingBuffer< T, S >::IsFull ( )
inline

Query whether the circular buffer is full.

Returns
true The buffer is full
false The buffer is not full

Definition at line 142 of file RingBuffer.hpp.

143  {
144  return this->length == S;
145  }
+ Here is the caller graph for this function:

◆ Length()

template<typename T , size_t S>
int RingBuffer< T, S >::Length ( )
inline

Returns the number of items (bytes) in the buffer.

Returns
int Number of bytes available for reading.

Definition at line 152 of file RingBuffer.hpp.

153  {
154  return this->length;
155  }

◆ Peek()

template<typename T , size_t S>
int RingBuffer< T, S >::Peek ( T *  buf)
inline

Grab the latest bytes of data from the buffer without removing it.

Parameters
bufBuffer to contain the data
Returns
int Returns 0 on success and -1 on error.

Definition at line 109 of file RingBuffer.hpp.

110  {
111  int status = 0;
112  // Check if the buffer is empty. If so, we can't dequeue
113  if (IsEmpty()) {
114  status = -1;
116  } else {
117  // Read out the data and don't decrement the position
118  *buf = this->data[this->tail];
119  }
120  // Return the status code
121  return status;
122  }
+ Here is the call graph for this function:

Field Documentation

◆ data

template<typename T , size_t S>
T RingBuffer< T, S >::data[S]
private

Definition at line 178 of file RingBuffer.hpp.

◆ error

template<typename T , size_t S>
int RingBuffer< T, S >::error
private

Definition at line 182 of file RingBuffer.hpp.

◆ head

template<typename T , size_t S>
size_t RingBuffer< T, S >::head
private

Definition at line 179 of file RingBuffer.hpp.

◆ length

template<typename T , size_t S>
size_t RingBuffer< T, S >::length
private

Definition at line 181 of file RingBuffer.hpp.

◆ tail

template<typename T , size_t S>
size_t RingBuffer< T, S >::tail
private

Definition at line 180 of file RingBuffer.hpp.


The documentation for this class was generated from the following file:
BufferFull
@ BufferFull
Definition: errno.hpp:19
RingBuffer::data
T data[S]
Definition: RingBuffer.hpp:178
RingBuffer::head
size_t head
Definition: RingBuffer.hpp:179
RingBuffer::error
int error
Definition: RingBuffer.hpp:182
RingBuffer::tail
size_t tail
Definition: RingBuffer.hpp:180
RingBuffer::IsEmpty
bool IsEmpty()
Query whether the circular buffer is empty.
Definition: RingBuffer.hpp:131
RingBuffer::length
size_t length
Definition: RingBuffer.hpp:181
InvalidValue
@ InvalidValue
Definition: errno.hpp:20
RingBuffer::IsFull
bool IsFull()
Query whether the circular buffer is full.
Definition: RingBuffer.hpp:142