proto_weighing.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #pragma once
  2. #include <library/cpp/unified_agent_client/f_maybe.h>
  3. #include <util/generic/deque.h>
  4. #include <util/generic/string.h>
  5. namespace NUnifiedAgent::NPW {
  6. class TLengthDelimited;
  7. class TFieldLink {
  8. public:
  9. TFieldLink(TLengthDelimited* container, bool repeated = false, size_t keySize = 1);
  10. void SetValueSize(bool empty, size_t size);
  11. private:
  12. TLengthDelimited* Container;
  13. int OuterSize;
  14. bool Repeated;
  15. size_t KeySize;
  16. };
  17. class TLengthDelimited {
  18. public:
  19. explicit TLengthDelimited(const TFMaybe<TFieldLink>& link = Nothing());
  20. void IncSize(int sizeDelta);
  21. size_t ByteSizeLong() const {
  22. return static_cast<size_t>(ByteSize);
  23. }
  24. private:
  25. TFMaybe<TFieldLink> Link;
  26. int ByteSize;
  27. };
  28. using TMessage = TLengthDelimited;
  29. template <typename T>
  30. class TRepeatedField: public TLengthDelimited {
  31. public:
  32. static_assert(std::is_same_v<T, ui32> ||
  33. std::is_same_v<T, ui64> ||
  34. std::is_same_v<T, i64>,
  35. "type is not supported");
  36. using TLengthDelimited::TLengthDelimited;
  37. void Add(T value);
  38. };
  39. template <typename T>
  40. class TRepeatedPtrField {
  41. public:
  42. explicit TRepeatedPtrField(TMessage* message, size_t keySize = 1)
  43. : Message(message)
  44. , Children()
  45. , KeySize(keySize)
  46. {
  47. }
  48. size_t GetSize() const {
  49. return Children.size();
  50. }
  51. T& Get(size_t index) {
  52. return Children[index];
  53. }
  54. T& Add() {
  55. if constexpr (std::is_constructible<T, TFieldLink>::value) {
  56. Children.emplace_back(TFieldLink(Message, true, KeySize));
  57. } else {
  58. Children.emplace_back(Message);
  59. }
  60. return Children.back();
  61. }
  62. private:
  63. TMessage* Message;
  64. TDeque<T> Children;
  65. size_t KeySize;
  66. };
  67. template <typename T>
  68. class TNumberField {
  69. public:
  70. static_assert(std::is_same_v<T, ui32> ||
  71. std::is_same_v<T, ui64> ||
  72. std::is_same_v<T, i64>,
  73. "type is not supported");
  74. explicit TNumberField(const TFieldLink& link);
  75. void SetValue(T value);
  76. private:
  77. TFieldLink Link;
  78. };
  79. template <typename T>
  80. class TFixedNumberField {
  81. public:
  82. static_assert(std::is_same_v<T, ui32> ||
  83. std::is_same_v<T, ui64> ||
  84. std::is_same_v<T, i64>,
  85. "type is not supported");
  86. explicit TFixedNumberField(const TFieldLink& link);
  87. void SetValue();
  88. private:
  89. TFieldLink Link;
  90. };
  91. class TStringField {
  92. public:
  93. explicit TStringField(const TFieldLink& link);
  94. void SetValue(const TString& value);
  95. private:
  96. TFieldLink Link;
  97. };
  98. extern template class TNumberField<ui64>;
  99. extern template class TNumberField<ui32>;
  100. extern template class TNumberField<i64>;
  101. extern template class TFixedNumberField<ui64>;
  102. extern template class TFixedNumberField<ui32>;
  103. extern template class TFixedNumberField<i64>;
  104. extern template class TRepeatedField<ui64>;
  105. extern template class TRepeatedField<ui32>;
  106. extern template class TRepeatedField<i64>;
  107. }