Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. class CanBuffer
  2. {
  3. public:
  4. enum { MAX_SIZE = 8 };
  5.  
  6. CanBuffer(): _size(0) {}
  7. CanBuffer(uint8_t initial_size) { resize(initial_size); }
  8. CanBuffer(const CanBuffer& rhs) = default;
  9. CanBuffer& operator=(const CanBuffer& rhs) = default;
  10.  
  11. void resize(uint8_t new_size)
  12. {
  13. if( new_size >= MAX_SIZE ){
  14. throw std::runtime_error("CanBuffer size exceeded");
  15. }
  16. _size = new_size;
  17. }
  18. void push_back(uint8_t value)
  19. {
  20. if( _size >= 8){
  21. throw std::runtime_error("CanBuffer size exceeded");
  22. }
  23. _buffer[_size++] = value;
  24. }
  25.  
  26. uint8_t size() const { return _size; }
  27.  
  28. uint8_t& operator[](size_t index) { return _buffer[index]; }
  29. uint8_t operator[](size_t index) const { return _buffer[index]; }
  30.  
  31. uint8_t& at(size_t index) { return _buffer.at(index); }
  32. uint8_t at(size_t index) const { return _buffer.at(index); }
  33.  
  34. uint8_t* data() { return _buffer.data(); }
  35. const uint8_t* data() const { return _buffer.data(); }
  36.  
  37. protected:
  38. std::array<uint8_t, 8> _buffer;
  39. uint8_t _size;
  40. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement