Buffer.h 634 B

123456789101112131415161718192021222324252627282930313233
  1. #ifndef BUFFER_H
  2. #define BUFFER_H
  3. #include <cstring>
  4. #include "memory/StackAllocator.h"
  5. class Buffer {
  6. StackAllocator::Array<char> data;
  7. int length = 0;
  8. public:
  9. Buffer(int n);
  10. template<typename T>
  11. Buffer& add(const T& t) {
  12. int bytes = data.getLength() - length;
  13. if(bytes > static_cast<int>(sizeof(T))) {
  14. bytes = sizeof(T);
  15. } else if(data.grow(data.getLength()) > 0) {
  16. return add(t);
  17. }
  18. memcpy(data + length, &t, bytes);
  19. length += bytes;
  20. return *this;
  21. }
  22. int getLength() const;
  23. operator const char*() const;
  24. };
  25. #endif