11namespace boost::context {
struct stack_context; }
22 virtual void operator () (std::string_view
data) = 0;
23 virtual bool good() {
return true; }
31 void operator () (std::string_view
data)
override
38 virtual void finish() = 0;
46struct BufferedSink :
virtual Sink
48 size_t bufSize, bufPos;
49 std::unique_ptr<char[]> buffer;
51 BufferedSink(
size_t bufSize = 32 * 1024)
52 : bufSize(bufSize), bufPos(0), buffer(
nullptr) { }
54 void operator () (std::string_view
data)
override;
60 virtual void writeUnbuffered(std::string_view
data) = 0;
84 virtual size_t read(
char *
data,
size_t len) = 0;
86 virtual bool good() {
return true; }
88 void drainInto(
Sink & sink);
100 size_t bufSize, bufPosIn, bufPosOut;
101 std::unique_ptr<char[]> buffer;
103 BufferedSource(
size_t bufSize = 32 * 1024)
104 : bufSize(bufSize), bufPosIn(0), bufPosOut(0), buffer(
nullptr) { }
106 size_t read(
char *
data,
size_t len)
override;
124struct FdSink : BufferedSink
129 FdSink() : fd(INVALID_DESCRIPTOR) { }
131 FdSink(FdSink&&) =
default;
133 FdSink & operator=(FdSink &&
s)
137 s.fd = INVALID_DESCRIPTOR;
144 void writeUnbuffered(std::string_view
data)
override;
146 bool good()
override;
156struct FdSource : BufferedSource
162 FdSource() : fd(INVALID_DESCRIPTOR) { }
164 FdSource(FdSource &&) =
default;
166 FdSource & operator=(FdSource &&
s) =
default;
168 bool good()
override;
190 explicit StringSink(
const size_t reservedSize)
192 s.reserve(reservedSize);
194 StringSink(std::string && s) : s(std::move(s)) { };
195 void operator () (std::string_view
data)
override;
210 StringSource(std::string &&) =
delete;
211 StringSource(std::string_view s) : s(s), pos(0) { }
212 StringSource(
const std::string&
str): StringSource(std::string_view(
str)) {}
214 size_t read(
char *
data,
size_t len)
override;
223 Sink & sink1, & sink2;
224 TeeSink(
Sink & sink1,
Sink & sink2) : sink1(sink1), sink2(sink2) { }
225 virtual void operator () (std::string_view
data)
override
241 : orig(orig), sink(sink) { }
244 size_t n = orig.read(
data, len);
257 SizedSource(
Source & orig,
size_t size)
258 : orig(orig), remain(size) { }
261 if (this->remain <= 0) {
262 throw EndOfFile(
"sized: unexpected end-of-file");
264 len = std::min(len, this->remain);
265 size_t n = this->orig.
read(
data, len);
275 std::vector<char> buf(8192);
277 while (this->remain > 0) {
278 size_t n =
read(buf.data(), buf.size());
292 void operator () (std::string_view
data)
override
294 length +=
data.size();
305 LengthSource(
Source & next) : next(next)
312 auto n = next.read(
data, len);
323 typedef std::function<void(std::string_view
data)> lambda_t;
327 LambdaSink(
const lambda_t & lambda) : lambda(lambda) { }
329 void operator () (std::string_view
data)
override
341 typedef std::function<
size_t(
char *,
size_t)> lambda_t;
345 LambdaSource(
const lambda_t & lambda) : lambda(lambda) { }
349 return lambda(
data, len);
359 Source & source1, & source2;
360 bool useSecond =
false;
362 : source1(s1), source2(s2)
365 size_t read(
char *
data,
size_t len)
override;
368std::unique_ptr<FinishSink> sourceToSink(std::function<
void(
Source &)>
fun);
374std::unique_ptr<Source> sinkToSource(
375 std::function<
void(
Sink &)>
fun,
376 std::function<
void()> eof = []() {
377 throw EndOfFile(
"coroutine has finished");
381void writePadding(
size_t len, Sink & sink);
382void writeString(std::string_view
s, Sink & sink);
384inline Sink & operator << (Sink & sink, uint64_t n)
386 unsigned char buf[8];
388 buf[1] = (n >> 8) & 0xff;
389 buf[2] = (n >> 16) & 0xff;
390 buf[3] = (n >> 24) & 0xff;
391 buf[4] = (n >> 32) & 0xff;
392 buf[5] = (n >> 40) & 0xff;
393 buf[6] = (n >> 48) & 0xff;
394 buf[7] = (
unsigned char) (n >> 56) & 0xff;
395 sink({(
char *) buf,
sizeof(buf)});
399Sink & operator << (
Sink & in,
const Error & ex);
400Sink & operator << (
Sink & sink, std::string_view
s);
401Sink & operator << (
Sink & sink,
const Strings &
s);
402Sink & operator << (
Sink & sink,
const StringSet &
s);
405MakeError(SerialisationError, Error);
411 unsigned char buf[8];
412 source((
char *) buf,
sizeof(buf));
416 if (n > (uint64_t) std::numeric_limits<T>::max())
417 throw SerialisationError(
"serialised integer %d is too large for type '%s'", n,
typeid(T).
name());
423inline unsigned int readInt(
Source & source)
425 return readNum<unsigned int>(source);
429inline uint64_t readLongLong(
Source & source)
431 return readNum<uint64_t>(source);
435void readPadding(
size_t len,
Source & source);
436size_t readString(
char * buf,
size_t max,
Source & source);
437std::string readString(
Source & source,
size_t max = std::numeric_limits<size_t>::max());
438template<
class T> T readStrings(
Source & source);
452 b = readNum<uint64_t>(in);
456Error readError(
Source & source);
464 std::shared_ptr<std::basic_istream<char>> istream;
466 StreamToSourceAdapter(std::shared_ptr<std::basic_istream<char>> istream)
472 if (!istream->read(
data, len)) {
473 if (istream->eof()) {
474 if (istream->gcount() == 0)
475 throw EndOfFile(
"end of file");
477 throw Error(
"I/O error in StreamToSourceAdapter");
479 return istream->gcount();
496 std::vector<char> pending;
499 FramedSource(
Source & from) : from(from)
507 auto n = readInt(from);
509 std::vector<char>
data(n);
510 from(
data.data(), n);
514 ignoreExceptionInDestructor();
520 if (eof)
throw EndOfFile(
"reached end of FramedSource");
522 if (pos >= pending.size()) {
523 size_t len = readInt(from);
528 pending = std::vector<char>(len);
530 from(pending.data(), len);
533 auto n = std::min(len, pending.size() - pos);
534 memcpy(
data, pending.data() + pos, n);
549 std::function<void()> checkError;
551 FramedSink(BufferedSink & to, std::function<
void()> && checkError)
552 : to(to), checkError(checkError)
561 ignoreExceptionInDestructor();
565 void writeUnbuffered(std::string_view
data)
override
int Descriptor
Definition file-descriptor.hh:20
uint32_t size_t
Definition lexer.l:6336
char
Definition lexer.l:3739
return s
Definition lexer.l:459
std::function< void(Sink &)> fun
Definition lexer.l:3485
std::ostream & str
Definition lexer.l:1728
const std::string_view & name
Definition lexer.l:1709
std::variant< std::string, std::string_view > data
Definition lexer.l:177
Definition serialise.hh:47
virtual size_t readUnbuffered(char *data, size_t len)=0
size_t read(char *data, size_t len) override
Definition serialise.cc:114
bool hasData()
Definition serialise.cc:129
size_t read(char *data, size_t len) override
Definition serialise.cc:454
bool hasData()
Definition serialise.cc:164
size_t readUnbuffered(char *data, size_t len) override
Definition serialise.cc:135
Definition serialise.hh:37
size_t read(char *data, size_t len) override
Definition serialise.hh:518
size_t read(char *data, size_t len) override
Definition serialise.hh:347
Definition serialise.hh:289
size_t read(char *data, size_t len) override
Definition serialise.hh:310
Definition serialise.hh:30
Definition serialise.hh:20
size_t drainAll()
Definition serialise.hh:273
size_t read(char *data, size_t len) override
Definition serialise.hh:259
Definition serialise.hh:68
void operator()(char *data, size_t len)
Definition serialise.cc:78
virtual size_t read(char *data, size_t len)=0
size_t read(char *data, size_t len) override
Definition serialise.hh:470
size_t read(char *data, size_t len) override
Definition serialise.cc:188
size_t read(char *data, size_t len) override
Definition serialise.hh:242
T readLittleEndian(unsigned char *p)
Definition util.hh:125