blob_io.h 945 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #pragma once
  2. #include "bin_saver.h"
  3. #include "buffered_io.h"
  4. #include <util/memory/blob.h>
  5. class TYaBlobStream: public IBinaryStream {
  6. TBlob Blob;
  7. i64 Pos;
  8. int WriteImpl(const void*, int) override {
  9. Y_ASSERT(0);
  10. return 0;
  11. }
  12. int ReadImpl(void* userBuffer, int size) override {
  13. if (size == 0)
  14. return 0;
  15. i64 res = Min<i64>(Blob.Length() - Pos, size);
  16. if (res)
  17. memcpy(userBuffer, ((const char*)Blob.Data()) + Pos, res);
  18. Pos += res;
  19. return res;
  20. }
  21. bool IsValid() const override {
  22. return true;
  23. }
  24. bool IsFailed() const override {
  25. return false;
  26. }
  27. public:
  28. TYaBlobStream(const TBlob& blob)
  29. : Blob(blob)
  30. , Pos(0)
  31. {
  32. }
  33. };
  34. template <class T>
  35. inline void SerializeBlob(const TBlob& data, T& c) {
  36. TYaBlobStream f(data);
  37. {
  38. IBinSaver bs(f, true);
  39. bs.Add(1, &c);
  40. }
  41. }