2022-03-08 23:53:26 +02:00
|
|
|
#pragma once
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* Template implementations (as opposed to mere declarations).
|
|
|
|
*
|
|
|
|
* This file is an exmample of the "impl.hh" pattern. See the
|
|
|
|
* contributing guide.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "worker-protocol.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
template<typename T>
|
2023-04-17 20:40:46 +03:00
|
|
|
std::vector<T> WorkerProto::Serialise<std::vector<T>>::read(const Store & store, WorkerProto::ReadConn conn)
|
2022-03-08 23:53:26 +02:00
|
|
|
{
|
|
|
|
std::vector<T> resSet;
|
2023-04-17 20:40:46 +03:00
|
|
|
auto size = readNum<size_t>(conn.from);
|
2022-03-08 23:53:26 +02:00
|
|
|
while (size--) {
|
2023-04-17 20:40:46 +03:00
|
|
|
resSet.push_back(WorkerProto::Serialise<T>::read(store, conn));
|
2022-03-08 23:53:26 +02:00
|
|
|
}
|
|
|
|
return resSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-04-17 20:40:46 +03:00
|
|
|
void WorkerProto::Serialise<std::vector<T>>::write(const Store & store, WorkerProto::WriteConn conn, const std::vector<T> & resSet)
|
2022-03-08 23:53:26 +02:00
|
|
|
{
|
2023-04-17 20:40:46 +03:00
|
|
|
conn.to << resSet.size();
|
2022-03-08 23:53:26 +02:00
|
|
|
for (auto & key : resSet) {
|
2023-04-17 20:40:46 +03:00
|
|
|
WorkerProto::Serialise<T>::write(store, conn, key);
|
2022-03-08 23:53:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-04-17 20:40:46 +03:00
|
|
|
std::set<T> WorkerProto::Serialise<std::set<T>>::read(const Store & store, WorkerProto::ReadConn conn)
|
2022-03-08 23:53:26 +02:00
|
|
|
{
|
|
|
|
std::set<T> resSet;
|
2023-04-17 20:40:46 +03:00
|
|
|
auto size = readNum<size_t>(conn.from);
|
2022-03-08 23:53:26 +02:00
|
|
|
while (size--) {
|
2023-04-17 20:40:46 +03:00
|
|
|
resSet.insert(WorkerProto::Serialise<T>::read(store, conn));
|
2022-03-08 23:53:26 +02:00
|
|
|
}
|
|
|
|
return resSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-04-17 20:40:46 +03:00
|
|
|
void WorkerProto::Serialise<std::set<T>>::write(const Store & store, WorkerProto::WriteConn conn, const std::set<T> & resSet)
|
2022-03-08 23:53:26 +02:00
|
|
|
{
|
2023-04-17 20:40:46 +03:00
|
|
|
conn.to << resSet.size();
|
2022-03-08 23:53:26 +02:00
|
|
|
for (auto & key : resSet) {
|
2023-04-17 20:40:46 +03:00
|
|
|
WorkerProto::Serialise<T>::write(store, conn, key);
|
2022-03-08 23:53:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
2023-04-17 20:40:46 +03:00
|
|
|
std::map<K, V> WorkerProto::Serialise<std::map<K, V>>::read(const Store & store, WorkerProto::ReadConn conn)
|
2022-03-08 23:53:26 +02:00
|
|
|
{
|
|
|
|
std::map<K, V> resMap;
|
2023-04-17 20:40:46 +03:00
|
|
|
auto size = readNum<size_t>(conn.from);
|
2022-03-08 23:53:26 +02:00
|
|
|
while (size--) {
|
2023-04-17 20:40:46 +03:00
|
|
|
auto k = WorkerProto::Serialise<K>::read(store, conn);
|
|
|
|
auto v = WorkerProto::Serialise<V>::read(store, conn);
|
2022-03-08 23:53:26 +02:00
|
|
|
resMap.insert_or_assign(std::move(k), std::move(v));
|
|
|
|
}
|
|
|
|
return resMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
2023-04-17 20:40:46 +03:00
|
|
|
void WorkerProto::Serialise<std::map<K, V>>::write(const Store & store, WorkerProto::WriteConn conn, const std::map<K, V> & resMap)
|
2022-03-08 23:53:26 +02:00
|
|
|
{
|
2023-04-17 20:40:46 +03:00
|
|
|
conn.to << resMap.size();
|
2022-03-08 23:53:26 +02:00
|
|
|
for (auto & i : resMap) {
|
2023-04-17 20:40:46 +03:00
|
|
|
WorkerProto::Serialise<K>::write(store, conn, i.first);
|
|
|
|
WorkerProto::Serialise<V>::write(store, conn, i.second);
|
2022-03-08 23:53:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 01:15:32 +03:00
|
|
|
template<typename... Ts>
|
|
|
|
std::tuple<Ts...> WorkerProto::Serialise<std::tuple<Ts...>>::read(const Store & store, WorkerProto::ReadConn conn)
|
|
|
|
{
|
|
|
|
return std::tuple<Ts...> {
|
|
|
|
WorkerProto::Serialise<Ts>::read(store, conn)...,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Ts>
|
|
|
|
void WorkerProto::Serialise<std::tuple<Ts...>>::write(const Store & store, WorkerProto::WriteConn conn, const std::tuple<Ts...> & res)
|
|
|
|
{
|
|
|
|
std::apply([&]<typename... Us>(const Us &... args) {
|
|
|
|
(WorkerProto::Serialise<Us>::write(store, conn, args), ...);
|
|
|
|
}, res);
|
|
|
|
}
|
|
|
|
|
2022-03-08 23:53:26 +02:00
|
|
|
}
|