Skip to content

Commit

Permalink
Merge pull request #15 from theodelrieu/feat/lazy
Browse files Browse the repository at this point in the history
feat: add lazy_encode/lazy_decode functions to basic_codec
  • Loading branch information
theodelrieu authored Sep 16, 2021
2 parents 68235d2 + ce6ea52 commit 90a742f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
28 changes: 28 additions & 0 deletions codecs/include/mgs/codecs/basic_codec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,34 @@ class basic_codec
{
return output_traits<T>::create(make_decoder(std::forward<Args>(args)...));
}

template <typename T = default_encoded_output>
static auto lazy_encode()
{
return
[](auto&&... args)
-> codecs::codec_output<
decltype(output_traits<T>::create(
make_encoder(std::forward<decltype(args)>(args)...))),
decltype(make_encoder(std::forward<decltype(args)>(args)...))> {
return output_traits<T>::create(
make_encoder(std::forward<decltype(args)>(args)...));
};
}

template <typename T = default_decoded_output>
static auto lazy_decode()
{
return
[](auto&&... args)
-> codecs::codec_output<
decltype(output_traits<T>::create(
make_decoder(std::forward<decltype(args)>(args)...))),
decltype(make_decoder(std::forward<decltype(args)>(args)...))> {
return output_traits<T>::create(
make_decoder(std::forward<decltype(args)>(args)...));
};
}
};
}
}
20 changes: 16 additions & 4 deletions codecs/test/codec_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ template <typename Codec,
typename Range2>
void test_encode(Range1&& it, Range2&& expected)
{
auto const encoded = Codec::template encode<CodecOutput>(it);
auto encoded = Codec::template encode<CodecOutput>(it);
check_equal(encoded, expected);

encoded = Codec::template lazy_encode<CodecOutput>()(it);
check_equal(encoded, expected);
}

Expand All @@ -68,7 +71,10 @@ template <typename Codec,
typename Range>
void test_encode(I i, S s, Range&& expected)
{
auto const encoded = Codec::template encode<CodecOutput>(i, s);
auto encoded = Codec::template encode<CodecOutput>(i, s);
check_equal(encoded, expected);

encoded = Codec::template lazy_encode<CodecOutput>()(i, s);
check_equal(encoded, expected);
}

Expand All @@ -95,7 +101,10 @@ template <typename Codec,
typename Range2>
void test_decode(Range1&& it, Range2&& expected)
{
auto const decoded = Codec::template decode<CodecOutput>(it);
auto decoded = Codec::template decode<CodecOutput>(it);
check_equal(decoded, expected);

decoded = Codec::template lazy_decode<CodecOutput>()(it);
check_equal(decoded, expected);
}

Expand All @@ -106,7 +115,10 @@ template <typename Codec,
typename Range>
void test_decode(I i, S s, Range&& expected)
{
auto const decoded = Codec::template decode<CodecOutput>(i, s);
auto decoded = Codec::template decode<CodecOutput>(i, s);
check_equal(decoded, expected);

decoded = Codec::template lazy_decode<CodecOutput>()(i, s);
check_equal(decoded, expected);
}

Expand Down

0 comments on commit 90a742f

Please sign in to comment.