proto_weighing.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "proto_weighing.h"
  2. #include <google/protobuf/io/coded_stream.h>
  3. namespace NUnifiedAgent::NPW {
  4. template <typename T>
  5. inline size_t SizeOf(T value);
  6. using CodedOutputStream = google::protobuf::io::CodedOutputStream;
  7. template <>
  8. inline size_t SizeOf(ui64 value) {
  9. return CodedOutputStream::VarintSize64(value);
  10. }
  11. template <>
  12. inline size_t SizeOf(ui32 value) {
  13. return CodedOutputStream::VarintSize32(value);
  14. }
  15. template <>
  16. inline size_t SizeOf(i64 value) {
  17. return CodedOutputStream::VarintSize64(static_cast<google::protobuf::uint64>(value));
  18. }
  19. TFieldLink::TFieldLink(TLengthDelimited* container, bool repeated, size_t keySize)
  20. : Container(container)
  21. , OuterSize(0)
  22. , Repeated(repeated)
  23. , KeySize(keySize)
  24. {
  25. }
  26. void TFieldLink::SetValueSize(bool empty, size_t size) {
  27. const auto newOuterSize = empty && !Repeated ? 0 : KeySize + static_cast<int>(size);
  28. Container->IncSize(newOuterSize - OuterSize);
  29. OuterSize = newOuterSize;
  30. }
  31. TLengthDelimited::TLengthDelimited(const TFMaybe<TFieldLink>& link)
  32. : Link(link)
  33. , ByteSize(0)
  34. {
  35. }
  36. void TLengthDelimited::IncSize(int sizeDelta) {
  37. ByteSize += sizeDelta;
  38. if (Link) {
  39. const auto byteSize = static_cast<ui32>(ByteSize);
  40. Link->SetValueSize(false, byteSize + SizeOf(byteSize));
  41. }
  42. }
  43. template <typename T>
  44. void TRepeatedField<T>::Add(T value) {
  45. IncSize(static_cast<int>(SizeOf(value)));
  46. }
  47. template <typename T>
  48. TNumberField<T>::TNumberField(const TFieldLink& link)
  49. : Link(link)
  50. {
  51. }
  52. template <typename T>
  53. void TNumberField<T>::SetValue(T value) {
  54. Link.SetValueSize(value == 0, SizeOf(value));
  55. }
  56. template <typename T>
  57. TFixedNumberField<T>::TFixedNumberField(const TFieldLink& link)
  58. : Link(link)
  59. {
  60. }
  61. template <typename T>
  62. void TFixedNumberField<T>::SetValue() {
  63. Link.SetValueSize(false, sizeof(T));
  64. }
  65. TStringField::TStringField(const TFieldLink& link)
  66. : Link(link)
  67. {
  68. }
  69. void TStringField::SetValue(const TString& value) {
  70. Link.SetValueSize(value.Empty(), value.Size() + SizeOf(static_cast<ui32>(value.Size())));
  71. }
  72. template class TNumberField<ui64>;
  73. template class TNumberField<ui32>;
  74. template class TNumberField<i64>;
  75. template class TFixedNumberField<ui64>;
  76. template class TFixedNumberField<ui32>;
  77. template class TFixedNumberField<i64>;
  78. template class TRepeatedField<ui64>;
  79. template class TRepeatedField<ui32>;
  80. template class TRepeatedField<i64>;
  81. }