aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/packedtypes/packed.h
blob: 88cff26ae2ab83a9f210ffb878e059cded5bdece (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
#pragma once

#include <library/cpp/streams/zc_memory_input/zc_memory_input.h>

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

#include "longs.h"

struct Stream_traits {
    template <typename T>
    static T get(IInputStream& in) {
        T x;
        ::Load(&in, x);
        return x;
    }
    static ui8 get_8(IInputStream& in) {
        return get<ui8>(in);
    }
    static ui16 get_16(IInputStream& in) {
        return get<ui16>(in);
    }
    static ui32 get_32(IInputStream& in) {
        return get<ui32>(in);
    }
    static void put_8(ui8 x, IOutputStream& out) {
        ::Save(&out, x);
    }
    static void put_16(ui16 x, IOutputStream& out) {
        ::Save(&out, x);
    }
    static void put_32(ui32 x, IOutputStream& out) {
        ::Save(&out, x);
    }
    static int is_good(IInputStream& /*in*/) {
        return 1;
    }
    static int is_good(IOutputStream& /*out*/) {
        return 1;
    }
};

struct TZCMemoryInput_traits {
    template <typename T>
    static T get(TZCMemoryInput& in) {
        T x;
        in.ReadPOD(x);
        return x;
    }

    static ui8 Y_FORCE_INLINE get_8(TZCMemoryInput& in) {
        return get<ui8>(in);
    }

    static ui16 Y_FORCE_INLINE get_16(TZCMemoryInput& in) {
        return get<ui16>(in);
    }

    static ui32 Y_FORCE_INLINE get_32(TZCMemoryInput& in) {
        return get<ui32>(in);
    }

    static int Y_FORCE_INLINE is_good(TZCMemoryInput&) {
        return 1;
    }
};

void Y_FORCE_INLINE PackUI32(IOutputStream& out, ui32 v) {
    char buf[sizeof(ui32)];
    char* bufPtr = buf;
    size_t size;
    PACK_28(v, bufPtr, mem_traits, size);
    out.Write(buf, size);
}

template <class TStream>
struct TInputStream2Traits {
    typedef Stream_traits TTraits;
};

template <>
struct TInputStream2Traits<TZCMemoryInput> {
    typedef TZCMemoryInput_traits TTraits;
};

template <class TStream>
void Y_FORCE_INLINE UnPackUI32(TStream& in, ui32& v) {
    size_t size;
    UNPACK_28(v, in, TInputStream2Traits<TStream>::TTraits, size);
    (void)size;
}

template <class TStream>
ui32 Y_FORCE_INLINE UnPackUI32(TStream& in) {
    ui32 res;
    UnPackUI32(in, res);
    return res;
}