Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
length-prefixed-protocol-helper.hh
Go to the documentation of this file.
1#pragma once
10
11#include "types.hh"
12
13namespace nix {
14
15struct StoreDirConfig;
16
30template<class Inner, typename T>
32
33#define LENGTH_PREFIXED_PROTO_HELPER(Inner, T) \
34 struct LengthPrefixedProtoHelper< Inner, T > \
35 { \
36 static T read(const StoreDirConfig & store, typename Inner::ReadConn conn); \
37 static void write(const StoreDirConfig & store, typename Inner::WriteConn conn, const T & str); \
38 private: \
39 \
49 template<typename U> using S = typename Inner::template Serialise<U>; \
50 }
51
52template<class Inner, typename T>
53LENGTH_PREFIXED_PROTO_HELPER(Inner, std::vector<T>);
55template<class Inner, typename T>
56LENGTH_PREFIXED_PROTO_HELPER(Inner, std::set<T>);
57
58template<class Inner, typename... Ts>
59LENGTH_PREFIXED_PROTO_HELPER(Inner, std::tuple<Ts...>);
60
61template<class Inner, typename K, typename V>
62#define LENGTH_PREFIXED_PROTO_HELPER_X std::map<K, V>
63LENGTH_PREFIXED_PROTO_HELPER(Inner, LENGTH_PREFIXED_PROTO_HELPER_X);
64
65template<class Inner, typename T>
66std::vector<T>
68 const StoreDirConfig & store, typename Inner::ReadConn conn)
69{
70 std::vector<T> resSet;
71 auto size = readNum<size_t>(conn.from);
72 while (size--) {
73 resSet.push_back(S<T>::read(store, conn));
74 }
75 return resSet;
76}
77
78template<class Inner, typename T>
79void
80LengthPrefixedProtoHelper<Inner, std::vector<T>>::write(
81 const StoreDirConfig & store, typename Inner::WriteConn conn, const std::vector<T> & resSet)
82{
83 conn.to << resSet.size();
84 for (auto & key : resSet) {
85 S<T>::write(store, conn, key);
86 }
87}
88
89template<class Inner, typename T>
90std::set<T>
92 const StoreDirConfig & store, typename Inner::ReadConn conn)
93{
94 std::set<T> resSet;
95 auto size = readNum<size_t>(conn.from);
96 while (size--) {
97 resSet.insert(S<T>::read(store, conn));
98 }
99 return resSet;
100}
101
102template<class Inner, typename T>
103void
105 const StoreDirConfig & store, typename Inner::WriteConn conn, const std::set<T> & resSet)
106{
107 conn.to << resSet.size();
108 for (auto & key : resSet) {
109 S<T>::write(store, conn, key);
110 }
111}
112
113template<class Inner, typename K, typename V>
114std::map<K, V>
116 const StoreDirConfig & store, typename Inner::ReadConn conn)
117{
118 std::map<K, V> resMap;
119 auto size = readNum<size_t>(conn.from);
120 while (size--) {
121 auto k = S<K>::read(store, conn);
122 auto v = S<V>::read(store, conn);
123 resMap.insert_or_assign(std::move(k), std::move(v));
124 }
125 return resMap;
126}
127
128template<class Inner, typename K, typename V>
129void
131 const StoreDirConfig & store, typename Inner::WriteConn conn, const std::map<K, V> & resMap)
132{
133 conn.to << resMap.size();
134 for (auto & i : resMap) {
135 S<K>::write(store, conn, i.first);
136 S<V>::write(store, conn, i.second);
137 }
138}
139
140template<class Inner, typename... Ts>
141std::tuple<Ts...>
142LengthPrefixedProtoHelper<Inner, std::tuple<Ts...>>::read(
143 const StoreDirConfig & store, typename Inner::ReadConn conn)
144{
145 return std::tuple<Ts...> {
146 S<Ts>::read(store, conn)...,
147 };
148}
149
150template<class Inner, typename... Ts>
151void
152LengthPrefixedProtoHelper<Inner, std::tuple<Ts...>>::write(
153 const StoreDirConfig & store, typename Inner::WriteConn conn, const std::tuple<Ts...> & res)
154{
155 std::apply([&]<typename... Us>(const Us &... args) {
156 (S<Us>::write(store, conn, args), ...);
157 }, res);
158}
159
160}
ChunkedVector< std::string, 8192 > store
Definition lexer.l:1011
const T::key_type & key
Definition lexer.l:2763
auto i
Definition lexer.l:2745
Strings res
Definition lexer.l:2566
T Inner
Definition lexer.l:4920
std::vector< Expr * > args
Definition lexer.l:6126
Definition length-prefixed-protocol-helper.hh:31
Definition store-dir-config.hh:22