gnu: linux-libre: Update to 5.1.4.
[guix.git] / nix / libutil / serialise.hh
blob6a6f028aa652f984262258ed37ac8b6d680f1a54
1 #pragma once
3 #include "types.hh"
6 namespace nix {
9 /* Abstract destination of binary data. */
10 struct Sink
12 virtual ~Sink() { }
13 virtual void operator () (const unsigned char * data, size_t len) = 0;
17 /* A buffered abstract sink. */
18 struct BufferedSink : Sink
20 size_t bufSize, bufPos;
21 unsigned char * buffer;
23 BufferedSink(size_t bufSize = 32 * 1024)
24 : bufSize(bufSize), bufPos(0), buffer(0) { }
25 ~BufferedSink();
27 void operator () (const unsigned char * data, size_t len);
29 void flush();
31 virtual void write(const unsigned char * data, size_t len) = 0;
35 /* Abstract source of binary data. */
36 struct Source
38 virtual ~Source() { }
40 /* Store exactly ‘len’ bytes in the buffer pointed to by ‘data’.
41 It blocks until all the requested data is available, or throws
42 an error if it is not going to be available. */
43 void operator () (unsigned char * data, size_t len);
45 /* Store up to ‘len’ in the buffer pointed to by ‘data’, and
46 return the number of bytes stored. If blocks until at least
47 one byte is available. */
48 virtual size_t read(unsigned char * data, size_t len) = 0;
52 /* A buffered abstract source. */
53 struct BufferedSource : Source
55 size_t bufSize, bufPosIn, bufPosOut;
56 unsigned char * buffer;
58 BufferedSource(size_t bufSize = 32 * 1024)
59 : bufSize(bufSize), bufPosIn(0), bufPosOut(0), buffer(0) { }
60 ~BufferedSource();
62 size_t read(unsigned char * data, size_t len);
64 /* Underlying read call, to be overridden. */
65 virtual size_t readUnbuffered(unsigned char * data, size_t len) = 0;
67 bool hasData();
71 /* A sink that writes data to a file descriptor. */
72 struct FdSink : BufferedSink
74 int fd;
75 bool warn;
76 size_t written;
78 FdSink() : fd(-1), warn(false), written(0) { }
79 FdSink(int fd) : fd(fd), warn(false), written(0) { }
80 ~FdSink();
82 void write(const unsigned char * data, size_t len);
86 /* A source that reads data from a file descriptor. */
87 struct FdSource : BufferedSource
89 int fd;
90 FdSource() : fd(-1) { }
91 FdSource(int fd) : fd(fd) { }
92 size_t readUnbuffered(unsigned char * data, size_t len);
96 /* A sink that writes data to a string. */
97 struct StringSink : Sink
99 string s;
100 void operator () (const unsigned char * data, size_t len);
104 /* A source that reads data from a string. */
105 struct StringSource : Source
107 const string & s;
108 size_t pos;
109 StringSource(const string & _s) : s(_s), pos(0) { }
110 size_t read(unsigned char * data, size_t len);
114 void writePadding(size_t len, Sink & sink);
115 void writeInt(unsigned int n, Sink & sink);
116 void writeLongLong(unsigned long long n, Sink & sink);
117 void writeString(const unsigned char * buf, size_t len, Sink & sink);
118 void writeString(const string & s, Sink & sink);
119 template<class T> void writeStrings(const T & ss, Sink & sink);
121 void readPadding(size_t len, Source & source);
122 unsigned int readInt(Source & source);
123 unsigned long long readLongLong(Source & source);
124 size_t readString(unsigned char * buf, size_t max, Source & source);
125 string readString(Source & source);
126 template<class T> T readStrings(Source & source);
129 MakeError(SerialisationError, Error)