RingBuffer.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef CORE_RINGBUFFER_HPP
  2. #define CORE_RINGBUFFER_HPP
  3. #include "core/utils/AlignedData.hpp"
  4. #include "core/utils/ArrayString.hpp"
  5. namespace Core {
  6. template<typename T, i64 N>
  7. class RingBuffer final {
  8. static_assert(N > 0, "RingBuffer size must be positive");
  9. AlignedType<T> data[static_cast<u64>(N)];
  10. i64 writeIndex;
  11. i64 readIndex;
  12. i64 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[](i64 index) {
  57. return reinterpret_cast<T*>(data)[(index + readIndex) % N];
  58. }
  59. const T& operator[](i64 index) const {
  60. return reinterpret_cast<const T*>(data)[(index + readIndex) % N];
  61. }
  62. i64 getLength() const {
  63. return values;
  64. }
  65. bool canRemove() const {
  66. return getLength() > 0;
  67. }
  68. void clear() {
  69. for(i64 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. i64 end = getLength();
  89. if(end > 0) {
  90. end--;
  91. for(i64 i = 0; i < end; i++) {
  92. CORE_RETURN_ERROR(s.append((*this)[i]));
  93. CORE_RETURN_ERROR(s.append(", "));
  94. }
  95. CORE_RETURN_ERROR(s.append((*this)[end]));
  96. }
  97. return s.append("]");
  98. }
  99. private:
  100. void copy(const RingBuffer& other) {
  101. for(i64 i = 0; i < other.getLength(); i++) {
  102. (void)add(other[i]);
  103. }
  104. }
  105. void move(RingBuffer&& other) {
  106. for(i64 i = 0; i < other.getLength(); i++) {
  107. (void)add(Core::move(other[i]));
  108. }
  109. }
  110. };
  111. }
  112. #endif