aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/comptrie/protopacker.h
blob: 3e15866dc5484679eafcdbbe1819c069614db70f (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
#pragma once

#include <util/stream/mem.h>
#include <util/ysaveload.h>

template <class Proto>
class TProtoPacker {
public:
    TProtoPacker() = default;

    void UnpackLeaf(const char* p, Proto& entry) const {
        TMemoryInput in(p + sizeof(ui32), SkipLeaf(p) - sizeof(ui32));
        entry.ParseFromArcadiaStream(&in);
    }
    void PackLeaf(char* p, const Proto& entry, size_t size) const {
        TMemoryOutput out(p, size + sizeof(ui32));
        Save<ui32>(&out, size);
        entry.SerializeToArcadiaStream(&out);
    }
    size_t MeasureLeaf(const Proto& entry) const {
        return entry.ByteSize() + sizeof(ui32);
    }
    size_t SkipLeaf(const char* p) const {
        TMemoryInput in(p, sizeof(ui32));
        ui32 size;
        Load<ui32>(&in, size);
        return size;
    }
};