diff options
Diffstat (limited to 'src/libstore/worker-protocol.hh')
-rw-r--r-- | src/libstore/worker-protocol.hh | 78 |
1 files changed, 76 insertions, 2 deletions
diff --git a/src/libstore/worker-protocol.hh b/src/libstore/worker-protocol.hh index 5eddaff56..13cf8d4ab 100644 --- a/src/libstore/worker-protocol.hh +++ b/src/libstore/worker-protocol.hh @@ -70,10 +70,84 @@ template<class T> T readStorePaths(const Store & store, Source & from); void writeStorePaths(const Store & store, Sink & out, const StorePathSet & paths); +/* To guide overloading */ +template<typename T> +struct Phantom {}; + + +namespace worker_proto { +/* FIXME maybe move more stuff inside here */ + +StorePath read(const Store & store, Source & from, Phantom<StorePath> _); +void write(const Store & store, Sink & out, const StorePath & storePath); + +template<typename T> +std::map<std::string, T> read(const Store & store, Source & from, Phantom<std::map<std::string, T>> _); +template<typename T> +void write(const Store & store, Sink & out, const std::map<string, T> & resMap); +template<typename T> +std::optional<T> read(const Store & store, Source & from, Phantom<std::optional<T>> _); +template<typename T> +void write(const Store & store, Sink & out, const std::optional<T> & optVal); + +/* Specialization which uses and empty string for the empty case, taking + advantage of the fact StorePaths always serialize to a non-empty string. + This is done primarily for backwards compatability, so that StorePath <= + std::optional<StorePath>, where <= is the compatability partial order. + */ +template<> +void write(const Store & store, Sink & out, const std::optional<StorePath> & optVal); + +template<typename T> +std::map<std::string, T> read(const Store & store, Source & from, Phantom<std::map<std::string, T>> _) +{ + std::map<string, T> resMap; + auto size = (size_t)readInt(from); + while (size--) { + auto thisKey = readString(from); + resMap.insert_or_assign(std::move(thisKey), nix::worker_proto::read(store, from, Phantom<T> {})); + } + return resMap; +} + +template<typename T> +void write(const Store & store, Sink & out, const std::map<string, T> & resMap) +{ + out << resMap.size(); + for (auto & i : resMap) { + out << i.first; + nix::worker_proto::write(store, out, i.second); + } +} + +template<typename T> +std::optional<T> read(const Store & store, Source & from, Phantom<std::optional<T>> _) +{ + auto tag = readNum<uint8_t>(from); + switch (tag) { + case 0: + return std::nullopt; + case 1: + return nix::worker_proto::read(store, from, Phantom<T> {}); + default: + throw Error("got an invalid tag bit for std::optional: %#04x", tag); + } +} + +template<typename T> +void write(const Store & store, Sink & out, const std::optional<T> & optVal) +{ + out << (optVal ? 1 : 0); + if (optVal) + nix::worker_proto::write(store, out, *optVal); +} + + +} + + StorePathCAMap readStorePathCAMap(const Store & store, Source & from); void writeStorePathCAMap(const Store & store, Sink & out, const StorePathCAMap & paths); -void writeOutputPathMap(const Store & store, Sink & out, const OutputPathMap & paths); - } |