Skip to content

Commit f3921e3

Browse files
committed
Replace function pointers for base encode/decode with virtual methods
1 parent 4ef747a commit f3921e3

File tree

3 files changed

+67
-20
lines changed

3 files changed

+67
-20
lines changed

src/libutil/base-n.cc

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,28 +177,62 @@ std::string base64::decode(std::string_view s)
177177
return res;
178178
}
179179

180-
decltype(base16::encode) * encodeForBase(Base base)
180+
namespace {
181+
182+
struct Base16Encoding final : BaseEncoding
181183
{
182-
switch (base) {
183-
case Base::Base16:
184-
return base16::encode;
185-
case Base::Nix32:
186-
return BaseNix32::encode;
187-
case Base::Base64:
188-
return base64::encode;
184+
std::string encode(std::span<const std::byte> data) const override
185+
{
186+
return base16::encode(data);
189187
}
190-
unreachable();
191-
}
192188

193-
decltype(base16::decode) * decodeForBase(Base base)
189+
std::string decode(std::string_view s) const override
190+
{
191+
return base16::decode(s);
192+
}
193+
};
194+
195+
struct Nix32Encoding final : BaseEncoding
196+
{
197+
std::string encode(std::span<const std::byte> data) const override
198+
{
199+
return BaseNix32::encode(data);
200+
}
201+
202+
std::string decode(std::string_view s) const override
203+
{
204+
return BaseNix32::decode(s);
205+
}
206+
};
207+
208+
struct Base64Encoding final : BaseEncoding
209+
{
210+
std::string encode(std::span<const std::byte> data) const override
211+
{
212+
return base64::encode(data);
213+
}
214+
215+
std::string decode(std::string_view s) const override
216+
{
217+
return base64::decode(s);
218+
}
219+
};
220+
221+
const Base16Encoding base16Encoding;
222+
const Nix32Encoding nix32Encoding;
223+
const Base64Encoding base64Encoding;
224+
225+
} // anonymous namespace
226+
227+
const BaseEncoding & getBaseEncoding(Base base)
194228
{
195229
switch (base) {
196230
case Base::Base16:
197-
return base16::decode;
231+
return base16Encoding;
198232
case Base::Nix32:
199-
return BaseNix32::decode;
233+
return nix32Encoding;
200234
case Base::Base64:
201-
return base64::decode;
235+
return base64Encoding;
202236
}
203237
unreachable();
204238
}

src/libutil/hash.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ std::string Hash::to_string(HashFormat hashFormat, bool includeAlgo) const
9191
hashFormat.raw);
9292
const auto bytes = std::as_bytes(std::span<const uint8_t>{&hash[0], hashSize});
9393
assert(hashSize);
94-
s += encodeForBase(hashFormat.toBase())(bytes);
94+
s += getBaseEncoding(hashFormat.toBase()).encode(bytes);
9595
return s;
9696
}
9797

@@ -125,7 +125,7 @@ static Hash parseLowLevel(
125125
Hash res{algo, xpSettings};
126126
std::string d;
127127
try {
128-
d = decodeForBase(format.toBase())(rest);
128+
d = getBaseEncoding(format.toBase()).decode(rest);
129129
} catch (Error & e) {
130130
e.addTrace({}, "While decoding hash '%s'", rest);
131131
}

src/libutil/include/nix/util/base-n.hh

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,26 @@ std::string decode(std::string_view s);
101101
} // namespace base64
102102

103103
/**
104-
* Get the encoding function for the given base encoding.
104+
* Abstract interface for base-N encoding/decoding operations.
105105
*/
106-
decltype(base16::encode) * encodeForBase(Base base);
106+
struct BaseEncoding
107+
{
108+
virtual ~BaseEncoding() = default;
109+
110+
/**
111+
* Encode arbitrary bytes to a string.
112+
*/
113+
virtual std::string encode(std::span<const std::byte> data) const = 0;
114+
115+
/**
116+
* Decode a string to bytes.
117+
*/
118+
virtual std::string decode(std::string_view s) const = 0;
119+
};
107120

108121
/**
109-
* Get the decoding function for the given base encoding.
122+
* Get the encoding/decoding functions for the given base encoding.
110123
*/
111-
decltype(base16::decode) * decodeForBase(Base base);
124+
const BaseEncoding & getBaseEncoding(Base base);
112125

113126
} // namespace nix

0 commit comments

Comments
 (0)