From 1f3ef3bedbaa2828574a6f75814a127bab61d368 Mon Sep 17 00:00:00 2001 From: Ethan Slattery Date: Mon, 27 Sep 2021 15:14:11 -0700 Subject: [PATCH] Fixes #9 - unable to pack into spans with span-lite this currently breaks on MSVC (but not gcc). Waiting to see response to bug report in span-lite --- conanfile.py | 2 +- include/bitpacker/bitstruct.hpp | 14 +++++++------- tests/python_common.hpp | 1 + 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/conanfile.py b/conanfile.py index 8e1092d..30bed2d 100644 --- a/conanfile.py +++ b/conanfile.py @@ -32,7 +32,7 @@ def validate(self): def requirements(self): if not tools.valid_min_cppstd(self, "20"): - self.requires("span-lite/0.7.0") + self.requires("span-lite/0.10.1") def build_requirements(self): if tools.get_env("CONAN_RUN_TESTS", True) or self.develop: diff --git a/include/bitpacker/bitstruct.hpp b/include/bitpacker/bitstruct.hpp index 7bcc133..7704d56 100644 --- a/include/bitpacker/bitstruct.hpp +++ b/include/bitpacker/bitstruct.hpp @@ -410,7 +410,8 @@ namespace bitpacker { /// helper function to pack types into the given buffer template - constexpr void pack(std::array& output, const size_type start_bit, std::index_sequence /*unused*/, Args&&... args) { + constexpr void pack(span output, const size_type start_bit, std::index_sequence /*unused*/, Args&&... args) { + static_assert(calcbytes(Fmt{}) <= N, "bitpacker::pack : format larger than given array, not even counting the offset!"); static_assert(sizeof...(args) == sizeof...(Items), "pack expected items for packing != sizeof...(args) passed"); constexpr auto byte_order = impl::get_byte_order(Fmt{}); static_assert(byte_order == impl::Endian::big, "Unpacking little endian byte order not supported yet..."); @@ -501,21 +502,20 @@ namespace bitpacker { template constexpr auto pack(Fmt /*unused*/, Args&&... args) { std::array output{}; - impl::pack(output, 0, std::make_index_sequence(), std::forward(args)...); + impl::pack(span(output), 0, std::make_index_sequence(), std::forward(args)...); return output; } /** * Pack Args... into data, starting at given bit offset offset, according to given format string fmt. * @param fmt [IN] format string created with macro `BP_STRING()` - * @param data [IN/OUT] reference to existing std::array of bytes to pack into + * @param data [IN/OUT] reference to a fixed size span of bytes to pack into * @param offset [IN] bit index to start unpacking from * @param args... [IN] list of arguments to pack into the format string */ - template - constexpr void pack_into(Fmt /*unused*/, std::array& data, const size_type offset, Args&&... args) { - static_assert(calcbytes(Fmt{}) <= N, "bitpacker::pack_into : format larger than given array, not even counting the offset!"); - impl::pack(data, offset, std::make_index_sequence(), std::forward(args)...); + template + constexpr void pack_into(Fmt /*unused*/, Output& data, const size_type offset, Args&&... args) { + impl::pack(span(data), offset, std::make_index_sequence(), std::forward(args)...); } } // namespace bitpacker diff --git a/tests/python_common.hpp b/tests/python_common.hpp index c354c3f..b186cb4 100644 --- a/tests/python_common.hpp +++ b/tests/python_common.hpp @@ -13,6 +13,7 @@ #define CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER #include "test_common.hpp" +#include "bitpacker/bitstruct.hpp" template < typename T > std::string escapeString(const T &val)