Skip to content

Commit

Permalink
add NetworkSerializableArray concept to support std::array on paramet…
Browse files Browse the repository at this point in the history
…er pack. Fix typo
  • Loading branch information
farukeryilmaz committed Jan 5, 2024
1 parent 2657263 commit 7f695ed
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions include/bytepack/bytepack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ concept NetworkSerializableBasic =
template<typename T>
concept NetworkSerializableBasicArray = std::is_array_v<T> && NetworkSerializableBasic<std::remove_extent_t<T>>;

template<typename T>
concept NetworkSerializableArray =
requires {
typename T::value_type;
{
std::tuple_size<T>::value
};
} && std::is_same_v<T, std::array<typename T::value_type, std::tuple_size<T>::value>>
&& NetworkSerializableBasic<typename T::value_type>;

template<typename T>
concept NetworkSerializableVector = std::is_same_v<T, std::vector<typename T::value_type, typename T::allocator_type>>
&& NetworkSerializableBasic<typename T::value_type>;
Expand All @@ -121,7 +131,8 @@ concept NetworkSerializableString = std::same_as<T, std::string> || std::same_as

template<typename T>
concept NetworkSerializableType = NetworkSerializableBasic<T> || NetworkSerializableBasicArray<T>
|| NetworkSerializableString<T> || NetworkSerializableVector<T>;
|| NetworkSerializableArray<T> || NetworkSerializableString<T>
|| NetworkSerializableVector<T>;

template<typename T>
concept IntegralType = std::is_integral_v<T>;
Expand Down Expand Up @@ -215,7 +226,7 @@ class binary_stream final
std::memcpy(buffer_.as<std::uint8_t>() + write_index_, value, sizeof(T));
write_index_ += sizeof(T);
} else {
// For multi-byte types with differing endianness, handle each element individually
// For multibyte types with differing endianness, handle each element individually
for (std::size_t i = 0; i < numElements; ++i) {
std::memcpy(buffer_.as<std::uint8_t>() + write_index_, &value[i], elementSize);
std::ranges::reverse(buffer_.as<std::uint8_t>() + write_index_,
Expand All @@ -242,7 +253,7 @@ class binary_stream final
std::memcpy(buffer_.as<std::uint8_t>() + write_index_, array.data(), N * sizeof(T));
write_index_ += N * sizeof(T);
} else {
// For multi-byte types with differing endianness, handle each element individually
// For multibyte types with differing endianness, handle each element individually
for (std::size_t i = 0; i < N; ++i) {
std::memcpy(buffer_.as<std::uint8_t>() + write_index_, &array[i], sizeof(T));
std::ranges::reverse(buffer_.as<std::uint8_t>() + write_index_,
Expand Down Expand Up @@ -280,7 +291,7 @@ class binary_stream final
std::memcpy(buffer_.as<std::uint8_t>() + write_index_, vector.data(), vector.size() * sizeof(T));
write_index_ += vector.size() * sizeof(T);
} else {
// For multi-byte types with differing endianness, handle each element individually
// For multibyte types with differing endianness, handle each element individually
for (std::size_t i = 0; i < vector.size(); ++i) {
std::memcpy(buffer_.as<std::uint8_t>() + write_index_, &vector[i], sizeof(T));
std::ranges::reverse(buffer_.as<std::uint8_t>() + write_index_,
Expand All @@ -305,7 +316,7 @@ class binary_stream final
std::memcpy(buffer_.as<std::uint8_t>() + write_index_, vector.data(), N * sizeof(T));
write_index_ += N * sizeof(T);
} else {
// For multi-byte types with differing endianness, handle each element individually
// For multibyte types with differing endianness, handle each element individually
for (std::size_t i = 0; i < N; ++i) {
std::memcpy(buffer_.as<std::uint8_t>() + write_index_, &vector[i], sizeof(T));
std::ranges::reverse(buffer_.as<std::uint8_t>() + write_index_,
Expand Down Expand Up @@ -436,7 +447,7 @@ class binary_stream final
std::memcpy(value, buffer_.as<std::uint8_t>() + read_index_, sizeof(T));
read_index_ += sizeof(T);
} else {
// For multi-byte types with differing endianness, handle each element individually
// For multibyte types with differing endianness, handle each element individually
for (size_t i = 0; i < numElements; ++i) {
std::memcpy(&value[i], buffer_.as<std::uint8_t>() + read_index_, elementSize);
std::ranges::reverse(reinterpret_cast<std::uint8_t*>(&value[i]),
Expand All @@ -463,7 +474,7 @@ class binary_stream final
std::memcpy(array.data(), buffer_.as<std::uint8_t>() + read_index_, N * sizeof(T));
read_index_ += N * sizeof(T);
} else {
// For multi-byte types with differing endianness, handle each element individually
// For multibyte types with differing endianness, handle each element individually
for (size_t i = 0; i < N; ++i) {
std::memcpy(&array[i], buffer_.as<std::uint8_t>() + read_index_, sizeof(T));
std::ranges::reverse(reinterpret_cast<std::uint8_t*>(&array[i]),
Expand All @@ -480,7 +491,7 @@ class binary_stream final
bool read(std::vector<T>& vector) noexcept
{
SizeType size_custom{};
// vector size cannot be negative, and zero-size is also unusual so it's treated as an error.
// vector size cannot be negative, and zero-size is also unusual, so it's treated as an error.
if (read(size_custom) == false || size_custom < 1) {
return false;
}
Expand All @@ -498,7 +509,7 @@ class binary_stream final
std::memcpy(vector.data(), buffer_.as<std::uint8_t>() + read_index_, size * sizeof(T));
read_index_ += size * sizeof(T);
} else {
// For multi-byte types with differing endianness, handle each element individually
// For multibyte types with differing endianness, handle each element individually
for (std::size_t i = 0; i < size; ++i) {
std::memcpy(&vector[i], buffer_.as<std::uint8_t>() + read_index_, sizeof(T));
std::ranges::reverse(reinterpret_cast<std::uint8_t*>(&vector[i]),
Expand All @@ -525,7 +536,7 @@ class binary_stream final
std::memcpy(vector.data(), buffer_.as<std::uint8_t>() + read_index_, N * sizeof(T));
read_index_ += N * sizeof(T);
} else {
// For multi-byte types with differing endianness, handle each element individually
// For multibyte types with differing endianness, handle each element individually
for (std::size_t i = 0; i < N; ++i) {
std::memcpy(&vector[i], buffer_.as<std::uint8_t>() + read_index_, sizeof(T));
std::ranges::reverse(reinterpret_cast<std::uint8_t*>(&vector[i]),
Expand Down Expand Up @@ -553,7 +564,7 @@ class binary_stream final
reinterpret_cast<std::uint8_t*>(&str_length) + sizeof(SizeType));
}

// String length cannot be negative, and zero-length is also unusual so it's treated as an error.
// String length cannot be negative, and zero-length is also unusual, so it's treated as an error.
if (str_length < 1) {
return false;
}
Expand Down

0 comments on commit 7f695ed

Please sign in to comment.