diff options
author | Daniil Cherednik <dan.cherednik@gmail.com> | 2023-03-31 10:54:08 +0300 |
---|---|---|
committer | Daniil Cherednik <dan.cherednik@gmail.com> | 2023-03-31 12:28:07 +0300 |
commit | fc1cffcfa7f0497a1f97b384a24bcbf23362f3be (patch) | |
tree | c15f7ab5b9e9b20fd0ef8fc07d598d28e8b32004 /library/cpp/unified_agent_client/proto_weighing.h | |
parent | 8a749596d40e91c896a1907afcd108d9221fbde1 (diff) | |
download | ydb-e9cbe5c5cf67db853d223fd365c9f05b695f7b96.tar.gz |
Ydb stable 23-1-1923.1.19
x-stable-origin-commit: c5d5a396e89d0a72e0267a55e93d8404d4fb54fe
Diffstat (limited to 'library/cpp/unified_agent_client/proto_weighing.h')
-rw-r--r-- | library/cpp/unified_agent_client/proto_weighing.h | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/library/cpp/unified_agent_client/proto_weighing.h b/library/cpp/unified_agent_client/proto_weighing.h new file mode 100644 index 0000000000..47cf577e14 --- /dev/null +++ b/library/cpp/unified_agent_client/proto_weighing.h @@ -0,0 +1,138 @@ +#pragma once + +#include <library/cpp/unified_agent_client/f_maybe.h> + +#include <util/generic/deque.h> +#include <util/generic/string.h> + +namespace NUnifiedAgent::NPW { + class TLengthDelimited; + + class TFieldLink { + public: + TFieldLink(TLengthDelimited* container, bool repeated = false, size_t keySize = 1); + + void SetValueSize(bool empty, size_t size); + + private: + TLengthDelimited* Container; + int OuterSize; + bool Repeated; + size_t KeySize; + }; + + class TLengthDelimited { + public: + explicit TLengthDelimited(const TFMaybe<TFieldLink>& link = Nothing()); + + void IncSize(int sizeDelta); + + size_t ByteSizeLong() const { + return static_cast<size_t>(ByteSize); + } + + private: + TFMaybe<TFieldLink> Link; + int ByteSize; + }; + + using TMessage = TLengthDelimited; + + template <typename T> + class TRepeatedField: public TLengthDelimited { + public: + static_assert(std::is_same_v<T, ui32> || + std::is_same_v<T, ui64> || + std::is_same_v<T, i64>, + "type is not supported"); + + using TLengthDelimited::TLengthDelimited; + + void Add(T value); + }; + + template <typename T> + class TRepeatedPtrField { + public: + explicit TRepeatedPtrField(TMessage* message, size_t keySize = 1) + : Message(message) + , Children() + , KeySize(keySize) + { + } + + size_t GetSize() const { + return Children.size(); + } + + T& Get(size_t index) { + return Children[index]; + } + + T& Add() { + if constexpr (std::is_constructible<T, TFieldLink>::value) { + Children.emplace_back(TFieldLink(Message, true, KeySize)); + } else { + Children.emplace_back(Message); + } + return Children.back(); + } + + private: + TMessage* Message; + TDeque<T> Children; + size_t KeySize; + }; + + template <typename T> + class TNumberField { + public: + static_assert(std::is_same_v<T, ui32> || + std::is_same_v<T, ui64> || + std::is_same_v<T, i64>, + "type is not supported"); + + explicit TNumberField(const TFieldLink& link); + + void SetValue(T value); + + private: + TFieldLink Link; + }; + + template <typename T> + class TFixedNumberField { + public: + static_assert(std::is_same_v<T, ui32> || + std::is_same_v<T, ui64> || + std::is_same_v<T, i64>, + "type is not supported"); + + explicit TFixedNumberField(const TFieldLink& link); + + void SetValue(); + + private: + TFieldLink Link; + }; + + class TStringField { + public: + explicit TStringField(const TFieldLink& link); + + void SetValue(const TString& value); + + private: + TFieldLink Link; + }; + + extern template class TNumberField<ui64>; + extern template class TNumberField<ui32>; + extern template class TNumberField<i64>; + extern template class TFixedNumberField<ui64>; + extern template class TFixedNumberField<ui32>; + extern template class TFixedNumberField<i64>; + extern template class TRepeatedField<ui64>; + extern template class TRepeatedField<ui32>; + extern template class TRepeatedField<i64>; +} |