Nix 2.36.0
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 "nix/util/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> \
50 using S = typename Inner::template Serialise<U>; \
51 }
53template<class Inner, typename T>
54LENGTH_PREFIXED_PROTO_HELPER(Inner, std::vector<T>);
55
56#define COMMA_ ,
57template<class Inner, typename T, typename Compare>
58LENGTH_PREFIXED_PROTO_HELPER(Inner, std::set<T COMMA_ Compare>);
59
60template<class Inner, typename... Ts>
61LENGTH_PREFIXED_PROTO_HELPER(Inner, std::tuple<Ts...>);
62
63template<class Inner, typename K, typename V, typename Compare>
64#define LENGTH_PREFIXED_PROTO_HELPER_X std::map<K, V, Compare>
65LENGTH_PREFIXED_PROTO_HELPER(Inner, LENGTH_PREFIXED_PROTO_HELPER_X);
66#undef COMMA_
67
68template<class Inner, typename T>
69std::vector<T>
70LengthPrefixedProtoHelper<Inner, std::vector<T>>::read(const StoreDirConfig & store, typename Inner::ReadConn conn)
71{
72 std::vector<T> resSet;
73 auto size = readNum<size_t>(conn.from);
74 while (size--) {
75 resSet.push_back(S<T>::read(store, conn));
76 }
77 return resSet;
78}
79
80template<class Inner, typename T>
81void LengthPrefixedProtoHelper<Inner, std::vector<T>>::write(
82 const StoreDirConfig & store, typename Inner::WriteConn conn, const std::vector<T> & resSet)
83{
84 conn.to << resSet.size();
85 for (auto & key : resSet) {
86 S<T>::write(store, conn, key);
87 }
88}
89
90template<class Inner, typename T, typename Compare>
92 const StoreDirConfig & store, typename Inner::ReadConn conn)
93{
94 std::set<T, Compare> 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, typename Compare>
104 const StoreDirConfig & store, typename Inner::WriteConn conn, const std::set<T, Compare> & resSet)
105{
106 conn.to << resSet.size();
107 for (auto & key : resSet) {
108 S<T>::write(store, conn, key);
109 }
110}
111
112template<class Inner, typename K, typename V, typename Compare>
114 const StoreDirConfig & store, typename Inner::ReadConn conn)
115{
116 std::map<K, V, Compare> resMap;
117 auto size = readNum<size_t>(conn.from);
118 while (size--) {
119 auto k = S<K>::read(store, conn);
120 auto v = S<V>::read(store, conn);
121 resMap.insert_or_assign(std::move(k), std::move(v));
122 }
123 return resMap;
124}
125
126template<class Inner, typename K, typename V, typename Compare>
128 const StoreDirConfig & store, typename Inner::WriteConn conn, const std::map<K, V, Compare> & resMap)
129{
130 conn.to << resMap.size();
131 for (auto & i : resMap) {
132 S<K>::write(store, conn, i.first);
133 S<V>::write(store, conn, i.second);
134 }
135}
136
137template<class Inner, typename... Ts>
138std::tuple<Ts...>
139LengthPrefixedProtoHelper<Inner, std::tuple<Ts...>>::read(const StoreDirConfig & store, typename Inner::ReadConn conn)
140{
141 return std::tuple<Ts...>{
142 S<Ts>::read(store, conn)...,
143 };
144}
145
146template<class Inner, typename... Ts>
147void LengthPrefixedProtoHelper<Inner, std::tuple<Ts...>>::write(
148 const StoreDirConfig & store, typename Inner::WriteConn conn, const std::tuple<Ts...> & res)
149{
150 std::apply([&]<typename... Us>(const Us &... args) { (S<Us>::write(store, conn, args), ...); }, res);
151}
152
153} // namespace nix
Definition length-prefixed-protocol-helper.hh:31
Definition store-dir-config.hh:29