Buffer.h 541 B

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