CircularBuffer.h 825 B

1234567891011121314151617181920212223242526272829303132333435
  1. #pragma once
  2. #include <cstdint> // for uint8_t
  3. #include <cstring> // for size_t
  4. #include <memory> // for unique_ptr
  5. #include <mutex> // for mutex
  6. #include <vector> // for vector
  7. #include "WrappedSemaphore.h" // for WrappedSemaphore
  8. namespace bell {
  9. class CircularBuffer {
  10. public:
  11. CircularBuffer(size_t dataCapacity);
  12. std::unique_ptr<bell::WrappedSemaphore> dataSemaphore;
  13. size_t size() const { return dataSize; }
  14. size_t capacity() const { return dataCapacity; }
  15. size_t write(const uint8_t* data, size_t bytes);
  16. size_t read(uint8_t* data, size_t bytes);
  17. void emptyBuffer();
  18. void emptyExcept(size_t size);
  19. private:
  20. std::mutex bufferMutex;
  21. size_t begIndex = 0;
  22. size_t endIndex = 0;
  23. size_t dataSize = 0;
  24. size_t dataCapacity = 0;
  25. std::vector<uint8_t> buffer;
  26. };
  27. } // namespace bell