Skip to content

Commit 64af701

Browse files
authored
Turn all runtime classes into non-templated classes (#830)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 857e36a commit 64af701

File tree

8 files changed

+147
-116
lines changed

8 files changed

+147
-116
lines changed

src/runtime/include/sourcemeta/jsonbinpack/runtime_decoder.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
namespace sourcemeta::jsonbinpack {
2121

2222
/// @ingroup runtime
23-
template <typename CharT, typename Traits>
24-
class Decoder : private BasicDecoder<CharT, Traits> {
23+
class Decoder : private BasicDecoder {
2524
public:
26-
Decoder(std::basic_istream<CharT, Traits> &input)
27-
: BasicDecoder<CharT, Traits>{input} {}
25+
Decoder(std::basic_istream<sourcemeta::jsontoolkit::JSON::Char,
26+
sourcemeta::jsontoolkit::JSON::CharTraits> &input)
27+
: BasicDecoder{input} {}
2828

2929
auto decode(const Plan &encoding) -> sourcemeta::jsontoolkit::JSON {
3030
switch (encoding.index()) {
@@ -272,7 +272,9 @@ class Decoder : private BasicDecoder<CharT, Traits> {
272272
assert(month >= 1 && month <= 12);
273273
assert(day >= 1 && day <= 31);
274274

275-
std::basic_ostringstream<CharT, Traits> output;
275+
std::basic_ostringstream<sourcemeta::jsontoolkit::JSON::Char,
276+
sourcemeta::jsontoolkit::JSON::CharTraits>
277+
output;
276278
output << std::setfill('0');
277279
output << std::setw(4) << year;
278280
output << "-";

src/runtime/include/sourcemeta/jsonbinpack/runtime_decoder_basic.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#ifndef DOXYGEN
44

55
#include <sourcemeta/jsonbinpack/numeric.h>
6+
#include <sourcemeta/jsontoolkit/json.h>
67

78
#include <sourcemeta/jsonbinpack/runtime_varint.h>
89

@@ -14,9 +15,13 @@
1415

1516
namespace sourcemeta::jsonbinpack {
1617

17-
template <typename CharT, typename Traits> class BasicDecoder {
18+
/// @ingroup runtime
19+
class BasicDecoder {
1820
public:
19-
BasicDecoder(std::basic_istream<CharT, Traits> &input) : stream{input} {
21+
BasicDecoder(
22+
std::basic_istream<sourcemeta::jsontoolkit::JSON::Char,
23+
sourcemeta::jsontoolkit::JSON::CharTraits> &input)
24+
: stream{input} {
2025
this->stream.exceptions(std::ios_base::badbit | std::ios_base::failbit |
2126
std::ios_base::eofbit);
2227
}
@@ -76,12 +81,13 @@ template <typename CharT, typename Traits> class BasicDecoder {
7681
}
7782

7883
inline auto get_string_utf8(const std::uint64_t length)
79-
-> std::basic_string<CharT> {
80-
std::basic_string<CharT> result;
84+
-> std::basic_string<sourcemeta::jsontoolkit::JSON::Char> {
85+
std::basic_string<sourcemeta::jsontoolkit::JSON::Char> result;
8186
result.reserve(length);
8287
std::uint64_t counter = 0;
8388
while (counter < length) {
84-
result += static_cast<CharT>(this->get_byte());
89+
result +=
90+
static_cast<sourcemeta::jsontoolkit::JSON::Char>(this->get_byte());
8591
counter += 1;
8692
}
8793

@@ -91,7 +97,8 @@ template <typename CharT, typename Traits> class BasicDecoder {
9197
}
9298

9399
private:
94-
std::basic_istream<CharT, Traits> &stream;
100+
std::basic_istream<sourcemeta::jsontoolkit::JSON::Char,
101+
sourcemeta::jsontoolkit::JSON::CharTraits> &stream;
95102
};
96103

97104
} // namespace sourcemeta::jsonbinpack

src/runtime/include/sourcemeta/jsonbinpack/runtime_encoder.h

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
namespace sourcemeta::jsonbinpack {
1919

2020
/// @ingroup runtime
21-
template <typename CharT, typename Traits>
22-
class Encoder : private BasicEncoder<CharT, Traits> {
21+
class Encoder : private BasicEncoder {
2322
public:
24-
Encoder(std::basic_ostream<CharT, Traits> &output)
25-
: BasicEncoder<CharT, Traits>{output} {}
23+
Encoder(std::basic_ostream<sourcemeta::jsontoolkit::JSON::Char,
24+
sourcemeta::jsontoolkit::JSON::CharTraits> &output)
25+
: BasicEncoder{output} {}
2626

2727
auto encode(const sourcemeta::jsontoolkit::JSON &document,
2828
const Plan &encoding) -> void {
@@ -203,18 +203,20 @@ class Encoder : private BasicEncoder<CharT, Traits> {
203203
auto UTF8_STRING_NO_LENGTH(const sourcemeta::jsontoolkit::JSON &document,
204204
const UTF8_STRING_NO_LENGTH &options) -> void {
205205
assert(document.is_string());
206-
const std::basic_string<CharT> value{document.to_string()};
206+
const std::basic_string<sourcemeta::jsontoolkit::JSON::Char> value{
207+
document.to_string()};
207208
this->put_string_utf8(value, options.size);
208209
}
209210

210211
auto FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED(
211212
const sourcemeta::jsontoolkit::JSON &document,
212213
const FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED &options) -> void {
213214
assert(document.is_string());
214-
const std::basic_string<CharT> value{document.to_string()};
215+
const std::basic_string<sourcemeta::jsontoolkit::JSON::Char> value{
216+
document.to_string()};
215217
const auto size{value.size()};
216218
assert(document.size() == size);
217-
const bool is_shared{this->context().has(value, ContextType::Standalone)};
219+
const bool is_shared{this->context().has(value, Context::Type::Standalone)};
218220

219221
// (1) Write 0x00 if shared, else do nothing
220222
if (is_shared) {
@@ -226,10 +228,12 @@ class Encoder : private BasicEncoder<CharT, Traits> {
226228

227229
// (3) Write relative offset if shared, else write plain string
228230
if (is_shared) {
229-
this->put_varint(this->position() -
230-
this->context().offset(value, ContextType::Standalone));
231+
this->put_varint(
232+
this->position() -
233+
this->context().offset(value, Context::Type::Standalone));
231234
} else {
232-
this->context().record(value, this->position(), ContextType::Standalone);
235+
this->context().record(value, this->position(),
236+
Context::Type::Standalone);
233237
this->put_string_utf8(value, size);
234238
}
235239
}
@@ -238,11 +242,12 @@ class Encoder : private BasicEncoder<CharT, Traits> {
238242
const sourcemeta::jsontoolkit::JSON &document,
239243
const ROOF_VARINT_PREFIX_UTF8_STRING_SHARED &options) -> void {
240244
assert(document.is_string());
241-
const std::basic_string<CharT> value{document.to_string()};
245+
const std::basic_string<sourcemeta::jsontoolkit::JSON::Char> value{
246+
document.to_string()};
242247
const auto size{value.size()};
243248
assert(document.size() == size);
244249
assert(size <= options.maximum);
245-
const bool is_shared{this->context().has(value, ContextType::Standalone)};
250+
const bool is_shared{this->context().has(value, Context::Type::Standalone)};
246251

247252
// (1) Write 0x00 if shared, else do nothing
248253
if (is_shared) {
@@ -254,10 +259,12 @@ class Encoder : private BasicEncoder<CharT, Traits> {
254259

255260
// (3) Write relative offset if shared, else write plain string
256261
if (is_shared) {
257-
this->put_varint(this->position() -
258-
this->context().offset(value, ContextType::Standalone));
262+
this->put_varint(
263+
this->position() -
264+
this->context().offset(value, Context::Type::Standalone));
259265
} else {
260-
this->context().record(value, this->position(), ContextType::Standalone);
266+
this->context().record(value, this->position(),
267+
Context::Type::Standalone);
261268
this->put_string_utf8(value, size);
262269
}
263270
}
@@ -266,13 +273,14 @@ class Encoder : private BasicEncoder<CharT, Traits> {
266273
const sourcemeta::jsontoolkit::JSON &document,
267274
const BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED &options) -> void {
268275
assert(document.is_string());
269-
const std::basic_string<CharT> value{document.to_string()};
276+
const std::basic_string<sourcemeta::jsontoolkit::JSON::Char> value{
277+
document.to_string()};
270278
const auto size{value.size()};
271279
assert(document.size() == size);
272280
assert(options.minimum <= options.maximum);
273281
assert(is_byte(options.maximum - options.minimum + 1));
274282
assert(is_within(size, options.minimum, options.maximum));
275-
const bool is_shared{this->context().has(value, ContextType::Standalone)};
283+
const bool is_shared{this->context().has(value, Context::Type::Standalone)};
276284

277285
// (1) Write 0x00 if shared, else do nothing
278286
if (is_shared) {
@@ -284,10 +292,12 @@ class Encoder : private BasicEncoder<CharT, Traits> {
284292

285293
// (3) Write relative offset if shared, else write plain string
286294
if (is_shared) {
287-
this->put_varint(this->position() -
288-
this->context().offset(value, ContextType::Standalone));
295+
this->put_varint(
296+
this->position() -
297+
this->context().offset(value, Context::Type::Standalone));
289298
} else {
290-
this->context().record(value, this->position(), ContextType::Standalone);
299+
this->context().record(value, this->position(),
300+
Context::Type::Standalone);
291301
this->put_string_utf8(value, size);
292302
}
293303
}
@@ -320,25 +330,27 @@ class Encoder : private BasicEncoder<CharT, Traits> {
320330
const sourcemeta::jsontoolkit::JSON &document,
321331
const PREFIX_VARINT_LENGTH_STRING_SHARED &) -> void {
322332
assert(document.is_string());
323-
const std::basic_string<CharT> value{document.to_string()};
333+
const std::basic_string<sourcemeta::jsontoolkit::JSON::Char> value{
334+
document.to_string()};
324335

325-
if (this->context().has(value, ContextType::PrefixLengthVarintPlusOne)) {
336+
if (this->context().has(value, Context::Type::PrefixLengthVarintPlusOne)) {
326337
const auto new_offset{this->position()};
327338
this->put_byte(0);
328339
this->put_varint(this->position() -
329340
this->context().offset(
330-
value, ContextType::PrefixLengthVarintPlusOne));
341+
value, Context::Type::PrefixLengthVarintPlusOne));
331342
// Bump the context cache for locality purposes
332343
this->context().record(value, new_offset,
333-
ContextType::PrefixLengthVarintPlusOne);
344+
Context::Type::PrefixLengthVarintPlusOne);
334345
} else {
335346
const auto size{value.size()};
336347
assert(document.size() == size);
337348
this->context().record(value, this->position(),
338-
ContextType::PrefixLengthVarintPlusOne);
349+
Context::Type::PrefixLengthVarintPlusOne);
339350
this->put_varint(size + 1);
340351
// Also record a standalone variant of it
341-
this->context().record(value, this->position(), ContextType::Standalone);
352+
this->context().record(value, this->position(),
353+
Context::Type::Standalone);
342354
this->put_string_utf8(value, size);
343355
}
344356
}
@@ -460,20 +472,22 @@ class Encoder : private BasicEncoder<CharT, Traits> {
460472
this->put_varint(absolute);
461473
}
462474
} else if (document.is_string()) {
463-
const std::basic_string<CharT> value{document.to_string()};
475+
const std::basic_string<sourcemeta::jsontoolkit::JSON::Char> value{
476+
document.to_string()};
464477
const auto size{document.byte_size()};
465-
const bool is_shared{this->context().has(value, ContextType::Standalone)};
478+
const bool is_shared{
479+
this->context().has(value, Context::Type::Standalone)};
466480
if (size < uint_max<5>) {
467481
const std::uint8_t type{is_shared ? TYPE_SHARED_STRING : TYPE_STRING};
468482
this->put_byte(
469483
static_cast<std::uint8_t>(type | ((size + 1) << type_size)));
470484
if (is_shared) {
471485
this->put_varint(
472486
this->position() -
473-
this->context().offset(value, ContextType::Standalone));
487+
this->context().offset(value, Context::Type::Standalone));
474488
} else {
475489
this->context().record(value, this->position(),
476-
ContextType::Standalone);
490+
Context::Type::Standalone);
477491
this->put_string_utf8(value, size);
478492
}
479493
} else if (size >= uint_max<5> && size < uint_max<5> * 2 && !is_shared) {
@@ -539,9 +553,6 @@ class Encoder : private BasicEncoder<CharT, Traits> {
539553
}
540554

541555
#endif
542-
543-
private:
544-
using ContextType = typename Context<CharT>::Type;
545556
};
546557

547558
} // namespace sourcemeta::jsonbinpack

src/runtime/include/sourcemeta/jsonbinpack/runtime_encoder_basic.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#ifndef DOXYGEN
44

55
#include <sourcemeta/jsonbinpack/numeric.h>
6+
#include <sourcemeta/jsontoolkit/json.h>
67

78
#include <sourcemeta/jsonbinpack/runtime_encoder_context.h>
89
#include <sourcemeta/jsonbinpack/runtime_varint.h>
@@ -17,9 +18,13 @@
1718

1819
namespace sourcemeta::jsonbinpack {
1920

20-
template <typename CharT, typename Traits> class BasicEncoder {
21+
/// @ingroup runtime
22+
class BasicEncoder {
2123
public:
22-
BasicEncoder(std::basic_ostream<CharT, Traits> &output) : stream{output} {
24+
BasicEncoder(
25+
std::basic_ostream<sourcemeta::jsontoolkit::JSON::Char,
26+
sourcemeta::jsontoolkit::JSON::CharTraits> &output)
27+
: stream{output} {
2328
this->stream.exceptions(std::ios_base::badbit | std::ios_base::failbit |
2429
std::ios_base::eofbit);
2530
}
@@ -33,11 +38,13 @@ template <typename CharT, typename Traits> class BasicEncoder {
3338
}
3439

3540
inline auto put_byte(const std::uint8_t byte) -> void {
36-
this->stream.put(static_cast<CharT>(byte));
41+
this->stream.put(static_cast<sourcemeta::jsontoolkit::JSON::Char>(byte));
3742
}
3843

3944
inline auto put_bytes(const std::uint16_t bytes) -> void {
40-
this->stream.write(reinterpret_cast<const CharT *>(&bytes), sizeof bytes);
45+
this->stream.write(
46+
reinterpret_cast<const sourcemeta::jsontoolkit::JSON::Char *>(&bytes),
47+
sizeof bytes);
4148
}
4249

4350
inline auto put_varint(const std::uint64_t value) -> void {
@@ -48,8 +55,9 @@ template <typename CharT, typename Traits> class BasicEncoder {
4855
varint_encode(this->stream, zigzag_encode(value));
4956
}
5057

51-
inline auto put_string_utf8(const std::basic_string<CharT> &string,
52-
const std::uint64_t length) -> void {
58+
inline auto put_string_utf8(
59+
const std::basic_string<sourcemeta::jsontoolkit::JSON::Char> &string,
60+
const std::uint64_t length) -> void {
5361
assert(string.size() == length);
5462
// Do a manual for-loop based on the provided length instead of a range
5563
// loop based on the string value to avoid accidental overflows
@@ -58,11 +66,12 @@ template <typename CharT, typename Traits> class BasicEncoder {
5866
}
5967
}
6068

61-
inline auto context() -> Context<CharT> & { return this->context_; }
69+
inline auto context() -> Context & { return this->context_; }
6270

6371
private:
64-
std::basic_ostream<CharT, Traits> &stream;
65-
Context<CharT> context_;
72+
std::basic_ostream<sourcemeta::jsontoolkit::JSON::Char,
73+
sourcemeta::jsontoolkit::JSON::CharTraits> &stream;
74+
Context context_;
6675
};
6776

6877
} // namespace sourcemeta::jsonbinpack

0 commit comments

Comments
 (0)