ProtoHelper.h 919 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #ifndef PROTOBUF_H
  2. #define PROTOBUF_H
  3. #include <iostream>
  4. #include <memory>
  5. #include "protobuf.h"
  6. #include <PbReader.h>
  7. #include <PbCommon.h>
  8. std::optional<AnyRef> findFieldWithProtobufTag(AnyRef ref, uint32_t tag);
  9. void decodeField(std::shared_ptr<PbReader> reader, AnyRef any);
  10. void decodeProtobuf(std::shared_ptr<PbReader> reader, AnyRef any);
  11. void encodeProtobuf(std::shared_ptr<PbWriter> writer, AnyRef any, uint32_t protobufTag = 0);
  12. template <typename T>
  13. std::vector<uint8_t> encodePb(T & data)
  14. {
  15. auto ref = AnyRef::of(&data);
  16. std::vector<uint8_t> rawData;;
  17. auto writer = std::make_shared<PbWriter>(rawData);
  18. encodeProtobuf(writer, ref);
  19. return rawData;
  20. }
  21. template <typename T>
  22. T decodePb(std::vector<uint8_t> & bytes)
  23. {
  24. T data = {};
  25. auto ref = AnyRef::of(&data);
  26. auto writer = std::make_shared<PbReader>(bytes);
  27. decodeProtobuf(writer, ref);
  28. return data;
  29. }
  30. #endif