RingBuffer.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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
  44. template<typename... Args>
  45. check_return T* add(Args&&... args) {
  46. if(getLength() >= N) {
  47. return nullptr;
  48. }
  49. T* t = new(data + writeIndex) T(Core::forward<Args>(args)...);
  50. writeIndex = (writeIndex + 1) % N;
  51. values++;
  52. return t;
  53. }
  54. T& operator[](int index) {
  55. return reinterpret_cast<T*>(data)[(index + readIndex) % N];
  56. }
  57. const T& operator[](int index) const {
  58. return reinterpret_cast<const T*>(data)[(index + readIndex) % N];
  59. }
  60. int getLength() const {
  61. return values;
  62. }
  63. bool canRemove() const {
  64. return getLength() > 0;
  65. }
  66. void clear() {
  67. for(int i = 0; i < getLength(); i++) {
  68. (*this)[i].~T();
  69. }
  70. writeIndex = 0;
  71. readIndex = 0;
  72. values = 0;
  73. }
  74. // returns true on error
  75. check_return bool remove() {
  76. if(!canRemove()) {
  77. return true;
  78. }
  79. values--;
  80. (*this)[0].~T();
  81. readIndex = (readIndex + 1) % N;
  82. return false;
  83. }
  84. // returns true on error
  85. template<int L>
  86. check_return bool toString(ArrayString<L>& s) const {
  87. if(s.append("[")) {
  88. return true;
  89. }
  90. for(int i = 0; i < getLength() - 1; i++) {
  91. if(s.append((*this)[i]) || s.append(", ")) {
  92. return true;
  93. }
  94. }
  95. if(getLength() > 0 && s.append((*this)[getLength() - 1])) {
  96. return true;
  97. }
  98. return s.append("]");
  99. }
  100. private:
  101. void copy(const RingBuffer& other) {
  102. for(int i = 0; i < other.getLength(); i++) {
  103. (void)add(other[i]);
  104. }
  105. }
  106. void move(RingBuffer&& other) {
  107. for(int i = 0; i < other.getLength(); i++) {
  108. (void)add(Core::move(other[i]));
  109. }
  110. }
  111. };
  112. }
  113. #endif