diff options
author | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2015-07-19T23·16+0200 |
---|---|---|
committer | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2015-07-19T23·16+0200 |
commit | b3491c781cb4be55f981b7456fcdbe5c4f869f01 (patch) | |
tree | bbc6467a5552c26eb8af8e71b8a05c0e3cc429af /src/libutil/serialise.hh | |
parent | 6bd2c7bb386de16310fa5534275e6e638be60862 (diff) |
More cleanup
Diffstat (limited to 'src/libutil/serialise.hh')
-rw-r--r-- | src/libutil/serialise.hh | 43 |
1 files changed, 27 insertions, 16 deletions
diff --git a/src/libutil/serialise.hh b/src/libutil/serialise.hh index 3a20ad032254..97ac3e912f85 100644 --- a/src/libutil/serialise.hh +++ b/src/libutil/serialise.hh @@ -1,13 +1,14 @@ #pragma once #include "types.hh" +#include "util.hh" namespace nix { /* Abstract destination of binary data. */ -struct Sink +struct Sink { virtual ~Sink() { } virtual void operator () (const unsigned char * data, size_t len) = 0; @@ -25,9 +26,9 @@ struct BufferedSink : Sink ~BufferedSink(); void operator () (const unsigned char * data, size_t len); - + void flush(); - + virtual void write(const unsigned char * data, size_t len) = 0; }; @@ -36,7 +37,7 @@ struct BufferedSink : Sink struct Source { virtual ~Source() { } - + /* Store exactly ‘len’ bytes in the buffer pointed to by ‘data’. It blocks until all the requested data is available, or throws an error if it is not going to be available. */ @@ -58,9 +59,9 @@ struct BufferedSource : Source BufferedSource(size_t bufSize = 32 * 1024) : bufSize(bufSize), bufPosIn(0), bufPosOut(0), buffer(0) { } ~BufferedSource(); - + size_t read(unsigned char * data, size_t len); - + /* Underlying read call, to be overridden. */ virtual size_t readUnbuffered(unsigned char * data, size_t len) = 0; @@ -78,7 +79,7 @@ struct FdSink : BufferedSink FdSink() : fd(-1), warn(false), written(0) { } FdSink(int fd) : fd(fd), warn(false), written(0) { } ~FdSink(); - + void write(const unsigned char * data, size_t len); }; @@ -107,21 +108,31 @@ struct StringSource : Source const string & s; size_t pos; StringSource(const string & _s) : s(_s), pos(0) { } - size_t read(unsigned char * data, size_t len); + size_t read(unsigned char * data, size_t len); }; void writePadding(size_t len, Sink & sink); -void writeInt(unsigned int n, Sink & sink); -void writeLongLong(unsigned long long n, Sink & sink); void writeString(const unsigned char * buf, size_t len, Sink & sink); -void writeString(const string & s, Sink & sink); -template<class T> void writeStrings(const T & ss, Sink & sink); -Sink & operator << (Sink & out, unsigned int n); -Sink & operator << (Sink & out, const string & s); -Sink & operator << (Sink & out, const Strings & s); -Sink & operator << (Sink & out, const StringSet & s); +inline Sink & operator << (Sink & sink, uint64_t n) +{ + unsigned char buf[8]; + buf[0] = n & 0xff; + buf[1] = (n >> 8) & 0xff; + buf[2] = (n >> 16) & 0xff; + buf[3] = (n >> 24) & 0xff; + buf[4] = (n >> 32) & 0xff; + buf[5] = (n >> 40) & 0xff; + buf[6] = (n >> 48) & 0xff; + buf[7] = (n >> 56) & 0xff; + sink(buf, sizeof(buf)); + return sink; +} + +Sink & operator << (Sink & sink, const string & s); +Sink & operator << (Sink & sink, const Strings & s); +Sink & operator << (Sink & sink, const StringSet & s); void readPadding(size_t len, Source & source); |