aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/packers/proto_packer.h
blob: 5a3d008e2946279ba138567d5c061beaa8941cb9 (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
#pragma once

#include "packers.h"

#include <util/generic/yexception.h>

namespace NPackers {
    template <typename TProtoMessage>
    class TProtoMessagePacker {
    public:
        void UnpackLeaf(const char* bufferPtr, TProtoMessage& protoMessage) const {
            const size_t protoMessageByteSize = GetProtoMessageByteSize(bufferPtr);
            const size_t skipBytesCount = ProtoMessageByteSizePacker.SkipLeaf(bufferPtr);

            if (!protoMessage.ParseFromArray(static_cast<const void*>(bufferPtr + skipBytesCount), protoMessageByteSize)) {
                ythrow yexception() << "Cannot unpack leaf with proto message";
            }
        }

        void PackLeaf(char* bufferPtr, const TProtoMessage& protoMessage, const size_t totalByteSize) const {
            const size_t protoMessageByteSize = protoMessage.ByteSize();
            const size_t skipBytesCount = totalByteSize - protoMessageByteSize;

            ProtoMessageByteSizePacker.PackLeaf(bufferPtr, protoMessageByteSize, skipBytesCount);

            if (!protoMessage.SerializeToArray(static_cast<void*>(bufferPtr + skipBytesCount), protoMessageByteSize)) {
                ythrow yexception() << "Cannot pack leaf with proto message";
            }
        }

        size_t MeasureLeaf(const TProtoMessage& protoMessage) const {
            const size_t protoMessageByteSize = protoMessage.ByteSize();
            return ProtoMessageByteSizePacker.MeasureLeaf(protoMessageByteSize) + protoMessageByteSize;
        }

        size_t SkipLeaf(const char* bufferPtr) const {
            const size_t protoMessageByteSize = GetProtoMessageByteSize(bufferPtr);
            return ProtoMessageByteSizePacker.SkipLeaf(bufferPtr) + protoMessageByteSize;
        }

    private:
        TIntegralPacker<size_t> ProtoMessageByteSizePacker;

        size_t GetProtoMessageByteSize(const char* bufferPtr) const {
            size_t result;
            ProtoMessageByteSizePacker.UnpackLeaf(bufferPtr, result);
            return result;
        }
    };
}