aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/unified_agent_client/proto_weighing.cpp
blob: 7a532213ea55ee53f0fb82e853eb33b764d4d520 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "proto_weighing.h"

#include <google/protobuf/io/coded_stream.h>

namespace NUnifiedAgent::NPW {
    template <typename T>
    inline size_t SizeOf(T value);

    using CodedOutputStream = google::protobuf::io::CodedOutputStream;

    template <>
    inline size_t SizeOf(ui64 value) {
        return CodedOutputStream::VarintSize64(value);
    }

    template <>
    inline size_t SizeOf(ui32 value) {
        return CodedOutputStream::VarintSize32(value);
    }

    template <>
    inline size_t SizeOf(i64 value) {
        return CodedOutputStream::VarintSize64(static_cast<google::protobuf::uint64>(value));
    }

    TFieldLink::TFieldLink(TLengthDelimited* container, bool repeated, size_t keySize)
        : Container(container)
        , OuterSize(0)
        , Repeated(repeated)
        , KeySize(keySize)
    {
    }

    void TFieldLink::SetValueSize(bool empty, size_t size) {
        const auto newOuterSize = empty && !Repeated ? 0 : KeySize + static_cast<int>(size);
        Container->IncSize(newOuterSize - OuterSize);
        OuterSize = newOuterSize;
    }

    TLengthDelimited::TLengthDelimited(const TFMaybe<TFieldLink>& link)
        : Link(link)
        , ByteSize(0)
    {
    }

    void TLengthDelimited::IncSize(int sizeDelta) {
        ByteSize += sizeDelta;
        if (Link) {
            const auto byteSize = static_cast<ui32>(ByteSize);
            Link->SetValueSize(false, byteSize + SizeOf(byteSize));
        }
    }

    template <typename T>
    void TRepeatedField<T>::Add(T value) {
        IncSize(static_cast<int>(SizeOf(value)));
    }

    template <typename T>
    TNumberField<T>::TNumberField(const TFieldLink& link)
        : Link(link)
    {
    }

    template <typename T>
    void TNumberField<T>::SetValue(T value) {
        Link.SetValueSize(value == 0, SizeOf(value));
    }

    template <typename T>
    TFixedNumberField<T>::TFixedNumberField(const TFieldLink& link)
        : Link(link)
    {
    }

    template <typename T>
    void TFixedNumberField<T>::SetValue() {
        Link.SetValueSize(false, sizeof(T));
    }

    TStringField::TStringField(const TFieldLink& link)
        : Link(link)
    {
    }

    void TStringField::SetValue(const TString& value) {
        Link.SetValueSize(value.Empty(), value.Size() + SizeOf(static_cast<ui32>(value.Size())));
    }

    template class TNumberField<ui64>;
    template class TNumberField<ui32>;
    template class TNumberField<i64>;
    template class TFixedNumberField<ui64>;
    template class TFixedNumberField<ui32>;
    template class TFixedNumberField<i64>;
    template class TRepeatedField<ui64>;
    template class TRepeatedField<ui32>;
    template class TRepeatedField<i64>;
}