Skip to content

Commit

Permalink
Added sized and unsized aliases.
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalz800 committed Dec 30, 2021
1 parent 08638e2 commit d82e99f
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 4 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<T>; // vector with 1 byte size.
zpp::bits::vector2b<T>; // vector with 2 byte size.
zpp::bits::vector4b<T>; // vector with 4 byte size == default std::vector configuration
zpp::bits::vector8b<T>; // vector with 8 byte size.
zpp::bits::static_vector<T>; // unsized vector
zpp::bits::native_vector<T>; // vector with native (size_type) byte size.
zpp::bits::span1b<T>; // span with 1 byte size.
zpp::bits::span2b<T>; // span with 2 byte size.
zpp::bits::span4b<T>; // span with 4 byte size == default std::span configuration
zpp::bits::span8b<T>; // span with 8 byte size.
zpp::bits::static_span<T>; // unsized span
zpp::bits::native_span<T>; // 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
Expand Down
35 changes: 35 additions & 0 deletions test/src/test_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>{1,2,3,4}).or_throw();

EXPECT_EQ(hexlify(data),
"01000000"
"02000000"
"03000000"
"04000000");

zpp::bits::static_vector<int> 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<int>{1,2,3,4}).or_throw();

EXPECT_EQ(hexlify(data),
"04"
"01000000"
"02000000"
"03000000"
"04000000");

zpp::bits::vector1b<int> v(4);
in(v).or_throw();

EXPECT_EQ(v, (std::vector{1,2,3,4}));
}

} // namespace test_vector
96 changes: 96 additions & 0 deletions zpp_bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -3402,6 +3402,102 @@ constexpr auto operator""_sha256_int()
}
} // namespace string_literals
} // namespace literals

template <typename... Arguments>
using vector1b = sized_t<std::vector<Arguments...>, unsigned char>;
template <typename... Arguments>
using vector2b = sized_t<std::vector<Arguments...>, std::uint16_t>;
template <typename... Arguments>
using vector4b = sized_t<std::vector<Arguments...>, std::uint32_t>;
template <typename... Arguments>
using vector8b = sized_t<std::vector<Arguments...>, std::uint64_t>;
template <typename... Arguments>
using static_vector = unsized_t<std::vector<Arguments...>>;
template <typename... Arguments>
using native_vector = sized_t<std::vector<Arguments...>, typename std::vector<Arguments...>::size_type>;

template <typename... Arguments>
using span1b = sized_t<std::span<Arguments...>, unsigned char>;
template <typename... Arguments>
using span2b = sized_t<std::span<Arguments...>, std::uint16_t>;
template <typename... Arguments>
using span4b = sized_t<std::span<Arguments...>, std::uint32_t>;
template <typename... Arguments>
using span8b = sized_t<std::span<Arguments...>, std::uint64_t>;
template <typename... Arguments>
using static_span = unsized_t<std::span<Arguments...>>;
template <typename... Arguments>
using native_span = sized_t<std::span<Arguments...>, typename std::span<Arguments...>::size_type>;

using string1b = sized_t<std::string, unsigned char>;
using string2b = sized_t<std::string, std::uint16_t>;
using string4b = sized_t<std::string, std::uint32_t>;
using string8b = sized_t<std::string, std::uint64_t>;
using static_string = unsized_t<std::string>;
using native_string = sized_t<std::string, std::string::size_type>;

using string_view1b = sized_t<std::string_view, unsigned char>;
using string_view2b = sized_t<std::string_view, std::uint16_t>;
using string_view4b = sized_t<std::string_view, std::uint32_t>;
using string_view8b = sized_t<std::string_view, std::uint64_t>;
using static_string_view = unsized_t<std::string_view>;
using native_string_view = sized_t<std::string_view, std::string_view::size_type>;

using wstring1b = sized_t<std::wstring, unsigned char>;
using wstring2b = sized_t<std::wstring, std::uint16_t>;
using wstring4b = sized_t<std::wstring, std::uint32_t>;
using wstring8b = sized_t<std::wstring, std::uint64_t>;
using static_wstring = unsized_t<std::wstring>;
using native_wstring = sized_t<std::wstring, std::wstring::size_type>;

using wstring_view1b = sized_t<std::wstring_view, unsigned char>;
using wstring_view2b = sized_t<std::wstring_view, std::uint16_t>;
using wstring_view4b = sized_t<std::wstring_view, std::uint32_t>;
using wstring_view8b = sized_t<std::wstring_view, std::uint64_t>;
using static_wstring_view = unsized_t<std::wstring_view>;
using native_wstring_view = sized_t<std::wstring_view, std::wstring_view::size_type>;

using u8string1b = sized_t<std::u8string, unsigned char>;
using u8string2b = sized_t<std::u8string, std::uint16_t>;
using u8string4b = sized_t<std::u8string, std::uint32_t>;
using u8string8b = sized_t<std::u8string, std::uint64_t>;
using static_u8string = unsized_t<std::u8string>;
using native_u8string = sized_t<std::u8string, std::u8string::size_type>;

using u8string_view1b = sized_t<std::u8string_view, unsigned char>;
using u8string_view2b = sized_t<std::u8string_view, std::uint16_t>;
using u8string_view4b = sized_t<std::u8string_view, std::uint32_t>;
using u8string_view8b = sized_t<std::u8string_view, std::uint64_t>;
using static_u8string_view = unsized_t<std::u8string_view>;
using native_u8string_view = sized_t<std::u8string_view, std::u8string_view::size_type>;

using u16string1b = sized_t<std::u16string, unsigned char>;
using u16string2b = sized_t<std::u16string, std::uint16_t>;
using u16string4b = sized_t<std::u16string, std::uint32_t>;
using u16string8b = sized_t<std::u16string, std::uint64_t>;
using static_u16string = unsized_t<std::u16string>;
using native_u16string = sized_t<std::u16string, std::u16string::size_type>;

using u16string_view1b = sized_t<std::u16string_view, unsigned char>;
using u16string_view2b = sized_t<std::u16string_view, std::uint16_t>;
using u16string_view4b = sized_t<std::u16string_view, std::uint32_t>;
using u16string_view8b = sized_t<std::u16string_view, std::uint64_t>;
using static_u16string_view = unsized_t<std::u16string_view>;
using native_u16string_view = sized_t<std::u16string_view, std::u16string_view::size_type>;

using u32string1b = sized_t<std::u32string, unsigned char>;
using u32string2b = sized_t<std::u32string, std::uint16_t>;
using u32string4b = sized_t<std::u32string, std::uint32_t>;
using u32string8b = sized_t<std::u32string, std::uint64_t>;
using static_u32string = unsized_t<std::u32string>;
using native_u32string = sized_t<std::u32string, std::u32string::size_type>;

using u32string_view1b = sized_t<std::u32string_view, unsigned char>;
using u32string_view2b = sized_t<std::u32string_view, std::uint16_t>;
using u32string_view4b = sized_t<std::u32string_view, std::uint32_t>;
using u32string_view8b = sized_t<std::u32string_view, std::uint64_t>;
using static_u32string_view = unsized_t<std::u32string_view>;
using native_u32string_view = sized_t<std::u32string_view, std::u32string_view::size_type>;
} // namespace zpp::bits

#endif // ZPP_BITS_H
Expand Down

0 comments on commit d82e99f

Please sign in to comment.