aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/clickhouse/client/base/wire_format.h
blob: 805a2d3212e51ebba281cb2ea31ccb072a32960a (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
100
101
102
103
#pragma once

#include "coded.h"

#include <util/generic/string.h>
#include <util/memory/tempbuf.h>

namespace NClickHouse {
    class TWireFormat {
    public:
        template <typename T>
        static bool ReadFixed(TCodedInputStream* input, T* value);

        static bool ReadString(TCodedInputStream* input, TString* value);

        static bool ReadBytes(TCodedInputStream* input, void* buf, size_t len);

        static bool ReadUInt64(TCodedInputStream* input, ui64* value);

        template <typename T>
        static void WriteFixed(TCodedOutputStream* output, const T& value);

        static void WriteBytes(TCodedOutputStream* output, const void* buf, size_t len);

        static void WriteString(TCodedOutputStream* output, const TString& value);

        static void WriteStringBuf(TCodedOutputStream* output, const TStringBuf value);

        static void WriteUInt64(TCodedOutputStream* output, const ui64 value);
    };

    template <typename T>
    inline bool TWireFormat::ReadFixed(
        TCodedInputStream* input,
        T* value) {
        return input->ReadRaw(value, sizeof(T));
    }

    inline bool TWireFormat::ReadString(
        TCodedInputStream* input,
        TString* value) {
        ui64 len;

        if (input->ReadVarint64(&len)) {
            if (len > 0x00FFFFFFULL) {
                return false;
            }
            TTempBuf buf(len);
            if (input->ReadRaw(buf.Data(), (size_t)len)) {
                value->assign(buf.Data(), len);
                return true;
            }
        }

        return false;
    }

    inline bool TWireFormat::ReadBytes(
        TCodedInputStream* input, void* buf, size_t len) {
        return input->ReadRaw(buf, len);
    }

    inline bool TWireFormat::ReadUInt64(
        TCodedInputStream* input,
        ui64* value) {
        return input->ReadVarint64(value);
    }

    template <typename T>
    inline void TWireFormat::WriteFixed(
        TCodedOutputStream* output,
        const T& value) {
        output->WriteRaw(&value, sizeof(T));
    }

    inline void TWireFormat::WriteBytes(
        TCodedOutputStream* output,
        const void* buf,
        size_t len) {
        output->WriteRaw(buf, len);
    }

    inline void TWireFormat::WriteString(
        TCodedOutputStream* output,
        const TString& value) {
        output->WriteVarint64(value.size());
        output->WriteRaw(value.data(), value.size());
    }

    inline void TWireFormat::WriteStringBuf(
        TCodedOutputStream* output,
        const TStringBuf value) {
        output->WriteVarint64(value.size());
        output->WriteRaw(value.data(), value.size());
    }

    inline void TWireFormat::WriteUInt64(
        TCodedOutputStream* output,
        const ui64 value) {
        output->WriteVarint64(value);
    }

}