arduino-audio-tools
QueueFromVector.h
1 #pragma once
2 #include "AudioTools/CoreAudio/AudioBasic/Collections/List.h"
3 
4 namespace audio_tools {
5 
13 template <class T>
15  public:
16  QueueFromVector(size_t size, T empty) {
17  null_value = empty;
18  resize(size);
19  };
20 
21  bool enqueue(T& data){
22  if (is_full())
23  return false;
24  vector[_end_pos++] = data;
25  return true;
26  }
27 
28  bool peek(T& data){
29  if (_end_pos <= 0 ) {
30  data = null_value;
31  _end_pos = 0;
32  return false;
33  }
34  data = vector[0];
35  return true;
36  }
37 
38  bool dequeue(T& data){
39  if (_end_pos <= 0 ) {
40  data = null_value;
41  _end_pos = 0;
42  return false;
43  }
44  // provide data at haed
45  data = vector[0];
46  // shift all data to the left by 1 position
47  memmove(&vector[0], &vector[1], (_end_pos-1)*sizeof(T));
48  vector[--_end_pos] = null_value;
49  return true;
50  }
51 
52  size_t size() {
53  return _end_pos < 0 ? 0 : _end_pos;
54  }
55 
56  bool resize(size_t size) {
57  if (!vector.resize(size)){
58  return false;
59  }
60  return clear();
61  }
62 
63  bool clear() {
64  for (int j=0;j<vector.size();j++){
65  vector[j] = null_value;
66  }
67  _end_pos = 0;
68  return true;
69  }
70 
71  bool empty() {
72  return _end_pos == 0;
73  }
74 
75  bool is_full() {
76  return _end_pos >= vector.size();
77  }
78 
79  size_t capacity() { return vector.capacity(); }
80 
81  void setAllocator(Allocator &allocator){
82  vector.setAllocator(allocator);
83  }
84 
85  protected:
86  Vector<T> vector;
87  int32_t _end_pos = 0;
88  int empty_pos = 0;
89  T null_value;
90 };
91 
92 }
Memory allocateator which uses malloc.
Definition: Allocator.h:22
FIFO Queue which is based on a Vector.
Definition: QueueFromVector.h:14
Vector implementation which provides the most important methods as defined by std::vector....
Definition: Vector.h:21
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823