protopacker.h 805 B

1234567891011121314151617181920212223242526272829
  1. #pragma once
  2. #include <util/stream/mem.h>
  3. #include <util/ysaveload.h>
  4. template <class Proto>
  5. class TProtoPacker {
  6. public:
  7. TProtoPacker() = default;
  8. void UnpackLeaf(const char* p, Proto& entry) const {
  9. TMemoryInput in(p + sizeof(ui32), SkipLeaf(p) - sizeof(ui32));
  10. entry.ParseFromArcadiaStream(&in);
  11. }
  12. void PackLeaf(char* p, const Proto& entry, size_t size) const {
  13. TMemoryOutput out(p, size + sizeof(ui32));
  14. Save<ui32>(&out, size);
  15. entry.SerializeToArcadiaStream(&out);
  16. }
  17. size_t MeasureLeaf(const Proto& entry) const {
  18. return entry.ByteSize() + sizeof(ui32);
  19. }
  20. size_t SkipLeaf(const char* p) const {
  21. TMemoryInput in(p, sizeof(ui32));
  22. ui32 size;
  23. Load<ui32>(&in, size);
  24. return size;
  25. }
  26. };