aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/binsaver/util_stream_io.h
blob: 31d8df535184325917326b424a0201eada44ece5 (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
#pragma once

#include "bin_saver.h"

#include <util/stream/input.h>
#include <util/stream/output.h>
#include <util/stream/file.h> 

class TYaStreamInput: public IBinaryStream {
    IInputStream& Stream;
 
    int WriteImpl(const void*, int) override {
        Y_ASSERT(0);
        return 0;
    }
    int ReadImpl(void* userBuffer, int size) override {
        return (int)Stream.Read(userBuffer, (size_t)size);
    }
    bool IsValid() const override {
        return true;
    }
    bool IsFailed() const override {
        return false;
    }

public:
    TYaStreamInput(IInputStream& stream)
        : Stream(stream)
    {
    }
};

template <class T>
inline void SerializeFromStream(IInputStream& stream, T& c) {
    TYaStreamInput f(stream);
    {
        IBinSaver bs(f, true);
        bs.Add(1, &c);
    }
}

template <class T>
inline void SerializeFromFile(const TString& fileName, T& c) {
    TIFStream in(fileName); 
    SerializeFromStream(in, c); 
} 
 
class TYaStreamOutput: public IBinaryStream {
    IOutputStream& Stream;
 
    int WriteImpl(const void* what, int size) override {
        Stream.Write(what, (size_t)size);
        return size;
    }
    int ReadImpl(void*, int) override {
        Y_ASSERT(0);
        return 0;
    }
    bool IsValid() const override {
        return true;
    }
    bool IsFailed() const override {
        return false;
    }

public:
    TYaStreamOutput(IOutputStream& stream)
        : Stream(stream)
    {
    }
};

template <class T>
inline void SerializeToArcadiaStream(IOutputStream& stream, T& c) {
    TYaStreamOutput f(stream);
    {
        IBinSaver bs(f, false);
        bs.Add(1, &c);
    }
}
 
template <class T>
inline void SerializeToFile(const TString& fileName, T& c) {
    TOFStream out(fileName); 
    SerializeToArcadiaStream(out, c);
}