RingBuffer.h 3.4 KB

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