Aviate Audio Multiverse Library
AviateTypes.h
Go to the documentation of this file.
1 /**************************************************************************/
11 #ifndef AVIATE_AVIATETYPES_H_
12 #define AVIATE_AVIATETYPES_H_
13 
14 #include "Aviate/Aviate.h"
15 
16 namespace Aviate {
17 
18 /**************************************************************************/
21 template <class T>
23 public:
24  RingBuffer() = delete;
25 
28  RingBuffer(const size_t maxSize) : m_maxSize(maxSize) {
29  m_buffer = new T[maxSize]();
30  }
32  virtual ~RingBuffer(){
33  if (m_buffer) delete [] m_buffer;
34  }
35 
39  int push_back(T element) {
40 
41  if ( (m_head == m_tail) && (m_size > 0) ) {
42  // overflow
43  return -1;
44  }
45 
46  m_buffer[m_head] = element;
47  if (m_head < (m_maxSize-1) ) {
48  m_head++;
49  } else {
50  m_head = 0;
51  }
52  m_size++;
53 
54  return 0;
55  }
56 
59  int pop_front() {
60 
61  if (m_size == 0) {
62  // buffer is empty
63  return -1;
64  }
65  if (m_tail < m_maxSize-1) {
66  m_tail++;
67  } else {
68  m_tail = 0;
69  }
70  m_size--;
71  return 0;
72  }
73 
76  T front() const {
77  return m_buffer[m_tail];
78  }
79 
82  T back() const {
83  return m_buffer[m_head-1];
84  }
85 
89  size_t get_index_from_back(size_t offset = 0) const {
90  // the target at m_head - 1 - offset or m_maxSize + m_head -1 - offset;
91  size_t idx = (m_maxSize + m_head -1 - offset);
92 
93  if ( idx >= m_maxSize) {
94  idx -= m_maxSize;
95  }
96 
97  return idx;
98  }
99 
102  size_t size() const {
103  return m_size;
104  }
105 
108  size_t max_size() const {
109  return m_maxSize;
110  }
111 
115  T& operator[] (size_t index) {
116  return m_buffer[index];
117  }
118 
122  T at(size_t index) const {
123  return m_buffer[index];
124  }
125 
126 private:
127  size_t m_head=0;
128  size_t m_tail=0;
129  size_t m_size=0;
130  T *m_buffer = nullptr;
131  const size_t m_maxSize;
132 };
133 
134 } // namespace Aviate
135 
136 
137 #endif /* AVIATE_AVIATETYPES_H_ */
#define AVIATE_API
enable default visibility. This is used for public API functions and classes.
Definition: Aviate.h:19
Customer RingBuffer with random access.
Definition: AviateTypes.h:22
T at(size_t index) const
get the element at the specified absolute index
Definition: AviateTypes.h:122
virtual ~RingBuffer()
Destructor.
Definition: AviateTypes.h:32
T front() const
Get the element at the front of the queue.
Definition: AviateTypes.h:76
size_t max_size() const
get the maximum size the queue can hold
Definition: AviateTypes.h:108
size_t size() const
get the current size of the queue
Definition: AviateTypes.h:102
T back() const
get the element at the back of the queue
Definition: AviateTypes.h:82
size_t get_index_from_back(size_t offset=0) const
Get a previously pushed elememt.
Definition: AviateTypes.h:89
int push_back(T element)
Add an element to the back of the queue.
Definition: AviateTypes.h:39
int pop_front()
Remove the element at teh front of the queue.
Definition: AviateTypes.h:59
RingBuffer(const size_t maxSize)
Construct a RingBuffer of specified max size.
Definition: AviateTypes.h:28
The Aviate library/namespace provides the primary API for working with Multiverse.
Definition: AudioEffectWrapper.h:24