-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stream.h
65 lines (55 loc) · 1.39 KB
/
Stream.h
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
/*
* File: Stream.h
* Author: michal
*
* Created on March 1, 2016, 2:25 AM
*/
#ifndef STREAM_H
#define STREAM_H
#include <exception>
namespace Serialize
{
struct EndOfStream : std::exception
{
const char *what() const noexcept
{
return "End of stream";
}
};
struct IOutputStream
{
virtual void write(const void *data, std::size_t size)=0;
};
struct IInputStream
{
virtual std::size_t read_some(void *data, std::size_t size)=0;
inline void read(void *data, std::size_t size)
{
if (read_some(data, size) != size)
throw EndOfStream();
}
};
template <class Format>
struct FormattedOStream
{
explicit FormattedOStream(IOutputStream &os) : stream(os) {}
inline void write(const void *data, std::size_t size) const
{
stream.write(data, size);
}
operator IOutputStream &() const { return stream; }
IOutputStream &stream;
};
template <class Format>
struct FormattedIStream
{
explicit FormattedIStream(IInputStream &is) : stream(is) {}
inline void read(void *data, std::size_t size) const
{
stream.read(data, size);
}
operator IInputStream &() const { return stream; }
IInputStream &stream;
};
}
#endif /* STREAM_H */