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