RingBuffer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef CORE_RINGBUFFER_H
  2. #define CORE_RINGBUFFER_H
  3. #include "utils/ArrayString.h"
  4. namespace Core {
  5. template<typename T, int N>
  6. class RingBuffer final {
  7. static_assert(N > 0, "RingBuffer size must be positive");
  8. struct alignas(T) Aligned {
  9. char data[sizeof(T)];
  10. };
  11. Aligned data[static_cast<unsigned int>(N)];
  12. int writeIndex;
  13. int readIndex;
  14. int values;
  15. public:
  16. RingBuffer() : writeIndex(0), readIndex(0), values(0) {
  17. }
  18. RingBuffer(const RingBuffer& other) : RingBuffer() {
  19. copy(other);
  20. }
  21. RingBuffer(RingBuffer&& other) : RingBuffer() {
  22. move(Core::move(other));
  23. other.clear();
  24. }
  25. ~RingBuffer() {
  26. clear();
  27. }
  28. RingBuffer& operator=(const RingBuffer& other) {
  29. if(&other != this) {
  30. clear();
  31. copy(other);
  32. }
  33. return *this;
  34. }
  35. RingBuffer& operator=(RingBuffer&& other) {
  36. if(&other != this) {
  37. clear();
  38. move(Core::move(other));
  39. other.clear();
  40. }
  41. return *this;
  42. }
  43. // returns a nullptr on error and calls the error callback
  44. template<typename... Args>
  45. check_return T* add(Args&&... args) {
  46. if(getLength() >= N) {
  47. CORE_ERROR(Error::CAPACITY_REACHED);
  48. return nullptr;
  49. }
  50. T* t = new(data + writeIndex) T(Core::forward<Args>(args)...);
  51. writeIndex = (writeIndex + 1) % N;
  52. values++;
  53. return t;
  54. }
  55. T& operator[](int index) {
  56. return reinterpret_cast<T*>(data)[(index + readIndex) % N];
  57. }
  58. const T& operator[](int index) const {
  59. return reinterpret_cast<const T*>(data)[(index + readIndex) % N];
  60. }
  61. int getLength() const {
  62. return values;
  63. }
  64. bool canRemove() const {
  65. return getLength() > 0;
  66. }
  67. void clear() {
  68. for(int i = 0; i < getLength(); i++) {
  69. (*this)[i].~T();
  70. }
  71. writeIndex = 0;
  72. readIndex = 0;
  73. values = 0;
  74. }
  75. // returns true on error and calls the error callback
  76. check_return bool remove() {
  77. if(!canRemove()) {
  78. return CORE_ERROR(Error::NOT_FOUND);
  79. }
  80. values--;
  81. (*this)[0].~T();
  82. readIndex = (readIndex + 1) % N;
  83. return false;
  84. }
  85. // returns true on error and calls the error callback
  86. template<int L>
  87. check_return bool toString(ArrayString<L>& s) const {
  88. if(s.append("[")) {
  89. return true;
  90. }
  91. for(int i = 0; i < getLength() - 1; i++) {
  92. if(s.append((*this)[i]) || s.append(", ")) {
  93. return true;
  94. }
  95. }
  96. if(getLength() > 0 && s.append((*this)[getLength() - 1])) {
  97. return true;
  98. }
  99. return s.append("]");
  100. }
  101. private:
  102. void copy(const RingBuffer& other) {
  103. for(int i = 0; i < other.getLength(); i++) {
  104. (void)add(other[i]);
  105. }
  106. }
  107. void move(RingBuffer&& other) {
  108. for(int i = 0; i < other.getLength(); i++) {
  109. (void)add(Core::move(other[i]));
  110. }
  111. }
  112. };
  113. }
  114. #endif