diff --git a/README.md b/README.md index 730bac7..0dc4d8c 100644 --- a/README.md +++ b/README.md @@ -273,9 +273,9 @@ std::vector v = {1,2,3,4}; out(v); in(v); ``` -The reason why the default size type is of 4 bytes (i.e `std::uint32_t`) is that most programs -almost never reach a case of a container being more than ~4 billion items, and it may be unjust to -pay the price of 8 bytes size by default. +The reason why the default size type is of 4 bytes (i.e `std::uint32_t`) is for portability between +different architectures, as well as most programs almost never reach a case of a container being +more than 2^32 items, and it may be unjust to pay the price of 8 bytes size by default. * For specific size types that are not 4 bytes, use `zpp::bits::sized`/`zpp::bits::sized_t` like so: ```cpp @@ -306,7 +306,25 @@ out(v); in(v); ``` -* Serialization of fixed size types such as arrays, `std::array`s, `std::tuple`s don't include +For where it is common, there are alias declarations for sized / unsized versions of types, for example, +here are `vector` and `span`, others such as `string`, `string_view`, etc are using the same pattern. +```cpp +zpp::bits::vector1b; // vector with 1 byte size. +zpp::bits::vector2b; // vector with 2 byte size. +zpp::bits::vector4b; // vector with 4 byte size == default std::vector configuration +zpp::bits::vector8b; // vector with 8 byte size. +zpp::bits::static_vector; // unsized vector +zpp::bits::native_vector; // vector with native (size_type) byte size. + +zpp::bits::span1b; // span with 1 byte size. +zpp::bits::span2b; // span with 2 byte size. +zpp::bits::span4b; // span with 4 byte size == default std::span configuration +zpp::bits::span8b; // span with 8 byte size. +zpp::bits::static_span; // unsized span +zpp::bits::native_span; // span with native (size_type) byte size. +``` + +Serialization of fixed size types such as arrays, `std::array`s, `std::tuple`s don't include any overhead except the elements followed by each other. * Serialization using argument dependent lookup is also possible, using both diff --git a/test/src/test_vector.cpp b/test/src/test_vector.cpp index 8e7eb27..8646cc1 100644 --- a/test/src/test_vector.cpp +++ b/test/src/test_vector.cpp @@ -157,4 +157,39 @@ TEST(vector, unsized_t_integer) EXPECT_EQ(v, (std::vector{1,2,3,4})); } +TEST(vector, static_vector) +{ + auto [data, in, out] = zpp::bits::data_in_out(); + out(zpp::bits::static_vector{1,2,3,4}).or_throw(); + + EXPECT_EQ(hexlify(data), + "01000000" + "02000000" + "03000000" + "04000000"); + + zpp::bits::static_vector v(4); + in(v).or_throw(); + + EXPECT_EQ(v, (std::vector{1,2,3,4})); +} + +TEST(vector, vector1b) +{ + auto [data, in, out] = zpp::bits::data_in_out(); + out(zpp::bits::vector1b{1,2,3,4}).or_throw(); + + EXPECT_EQ(hexlify(data), + "04" + "01000000" + "02000000" + "03000000" + "04000000"); + + zpp::bits::vector1b v(4); + in(v).or_throw(); + + EXPECT_EQ(v, (std::vector{1,2,3,4})); +} + } // namespace test_vector diff --git a/zpp_bits.h b/zpp_bits.h index 6690cc4..4950b2e 100644 --- a/zpp_bits.h +++ b/zpp_bits.h @@ -3402,6 +3402,102 @@ constexpr auto operator""_sha256_int() } } // namespace string_literals } // namespace literals + +template +using vector1b = sized_t, unsigned char>; +template +using vector2b = sized_t, std::uint16_t>; +template +using vector4b = sized_t, std::uint32_t>; +template +using vector8b = sized_t, std::uint64_t>; +template +using static_vector = unsized_t>; +template +using native_vector = sized_t, typename std::vector::size_type>; + +template +using span1b = sized_t, unsigned char>; +template +using span2b = sized_t, std::uint16_t>; +template +using span4b = sized_t, std::uint32_t>; +template +using span8b = sized_t, std::uint64_t>; +template +using static_span = unsized_t>; +template +using native_span = sized_t, typename std::span::size_type>; + +using string1b = sized_t; +using string2b = sized_t; +using string4b = sized_t; +using string8b = sized_t; +using static_string = unsized_t; +using native_string = sized_t; + +using string_view1b = sized_t; +using string_view2b = sized_t; +using string_view4b = sized_t; +using string_view8b = sized_t; +using static_string_view = unsized_t; +using native_string_view = sized_t; + +using wstring1b = sized_t; +using wstring2b = sized_t; +using wstring4b = sized_t; +using wstring8b = sized_t; +using static_wstring = unsized_t; +using native_wstring = sized_t; + +using wstring_view1b = sized_t; +using wstring_view2b = sized_t; +using wstring_view4b = sized_t; +using wstring_view8b = sized_t; +using static_wstring_view = unsized_t; +using native_wstring_view = sized_t; + +using u8string1b = sized_t; +using u8string2b = sized_t; +using u8string4b = sized_t; +using u8string8b = sized_t; +using static_u8string = unsized_t; +using native_u8string = sized_t; + +using u8string_view1b = sized_t; +using u8string_view2b = sized_t; +using u8string_view4b = sized_t; +using u8string_view8b = sized_t; +using static_u8string_view = unsized_t; +using native_u8string_view = sized_t; + +using u16string1b = sized_t; +using u16string2b = sized_t; +using u16string4b = sized_t; +using u16string8b = sized_t; +using static_u16string = unsized_t; +using native_u16string = sized_t; + +using u16string_view1b = sized_t; +using u16string_view2b = sized_t; +using u16string_view4b = sized_t; +using u16string_view8b = sized_t; +using static_u16string_view = unsized_t; +using native_u16string_view = sized_t; + +using u32string1b = sized_t; +using u32string2b = sized_t; +using u32string4b = sized_t; +using u32string8b = sized_t; +using static_u32string = unsized_t; +using native_u32string = sized_t; + +using u32string_view1b = sized_t; +using u32string_view2b = sized_t; +using u32string_view4b = sized_t; +using u32string_view8b = sized_t; +using static_u32string_view = unsized_t; +using native_u32string_view = sized_t; } // namespace zpp::bits #endif // ZPP_BITS_H