Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode size_t types with fixed-length + extended generated tests #44

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions include/alpaca/detail/from_bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ namespace alpaca {

namespace detail {

using size_t_serialized_type = uint64_t;

template<typename T>
struct map_size_t_to_type {
using type = T;
};

template<>
struct map_size_t_to_type<size_t> {
using type = size_t_serialized_type;
};

template<typename T>
using map_size_t_to_type_t = typename map_size_t_to_type<T>::type;


template <options O, typename Container>
typename std::enable_if<!std::is_array_v<Container>, bool>::type
from_bytes_crc32(uint32_t &value, Container &bytes, std::size_t &current_index,
Expand Down Expand Up @@ -211,11 +227,13 @@ typename std::enable_if<
from_bytes(T &value, Container &bytes, std::size_t &current_index,
std::size_t &end_index, std::error_code &) {

using ActualType = map_size_t_to_type_t<T>;

if (current_index >= end_index) {
// end of input

// default initialize the value
value = T();
value = ActualType();

// return true for forward compatibility
return true;
Expand All @@ -232,18 +250,18 @@ from_bytes(T &value, Container &bytes, std::size_t &current_index,
(detail::fixed_length_encoding<O>()));

if constexpr (use_fixed_length_encoding) {
constexpr auto num_bytes_to_read = sizeof(T);
constexpr auto num_bytes_to_read = sizeof(ActualType);
if (end_index < num_bytes_to_read) {
/// TODO: report error
return false;
}
value = *(reinterpret_cast<const T *>(bytes.data() + current_index));
value = *(reinterpret_cast<const ActualType *>(bytes.data() + current_index));
current_index += num_bytes_to_read;
} else {
value = decode_varint<T>(bytes, current_index);
value = decode_varint<ActualType>(bytes, current_index);
}

update_value_based_on_alpaca_endian_rules<O, T>(value);
update_value_based_on_alpaca_endian_rules<O, ActualType>((ActualType &) value);
return true;
}

Expand All @@ -255,16 +273,18 @@ typename std::enable_if<
std::is_array_v<Container> &&
(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
std::is_same_v<T, uint32_t> || std::is_same_v<T, uint64_t> ||
std::is_same_v<T, std::size_t>),
std::is_same_v<T, size_t>),
bool>::type
from_bytes(T &value, Container &bytes, std::size_t &current_index,
std::size_t &end_index, std::error_code &) {

using ActualType = map_size_t_to_type_t<T>;

if (current_index >= end_index) {
// end of input

// default initialize the value
value = T();
value = ActualType();

// return true for forward compatibility
return true;
Expand All @@ -281,18 +301,19 @@ from_bytes(T &value, Container &bytes, std::size_t &current_index,
(detail::fixed_length_encoding<O>()));

if constexpr (use_fixed_length_encoding) {
constexpr auto num_bytes_to_read = sizeof(T);
constexpr auto num_bytes_to_read = sizeof(ActualType);
if (end_index < num_bytes_to_read) {
/// TODO: report error
return false;
}
value = *(reinterpret_cast<const T *>(bytes + current_index));

value = *(reinterpret_cast<const ActualType *>(bytes + current_index));
current_index += num_bytes_to_read;
} else {
value = decode_varint<T>(bytes, current_index);
value = decode_varint<ActualType>(bytes, current_index);
}

update_value_based_on_alpaca_endian_rules<O, T>(value);
update_value_based_on_alpaca_endian_rules<O, ActualType>((ActualType&) value);
return true;
}

Expand All @@ -304,16 +325,19 @@ typename std::enable_if<
std::is_same_v<Container, std::ifstream> &&
(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
std::is_same_v<T, uint32_t> || std::is_same_v<T, uint64_t> ||
std::is_same_v<T, std::size_t>),
std::is_same_v<T, size_t>),
bool>::type

from_bytes(T &value, Container &bytes, std::size_t &current_index,
std::size_t &end_index, std::error_code &) {

using ActualType = map_size_t_to_type_t<T>;

if (current_index >= end_index) {
// end of input

// default initialize the value
value = T();
value = ActualType();

// return true for forward compatibility
return true;
Expand All @@ -330,20 +354,20 @@ from_bytes(T &value, Container &bytes, std::size_t &current_index,
(detail::fixed_length_encoding<O>()));

if constexpr (use_fixed_length_encoding) {
constexpr auto num_bytes_to_read = sizeof(T);
constexpr auto num_bytes_to_read = sizeof(ActualType);
if (end_index < num_bytes_to_read) {
/// TODO: report error
return false;
}
char value_bytes[num_bytes_to_read];
bytes.read(&value_bytes[0], num_bytes_to_read);
current_index += num_bytes_to_read;
value = *(reinterpret_cast<const T *>(value_bytes));
value = *(reinterpret_cast<const ActualType *>(value_bytes));
} else {
value = decode_varint<T>(bytes, current_index);
value = decode_varint<ActualType>(bytes, current_index);
}

update_value_based_on_alpaca_endian_rules<O, T>(value);
update_value_based_on_alpaca_endian_rules<O, ActualType>((ActualType &)value);
return true;
}

Expand Down
36 changes: 33 additions & 3 deletions include/alpaca/detail/to_bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ to_bytes(T &bytes, std::size_t &byte_index, const U &original_value) {
// encode as variable-length
template <options O, typename T, typename U>
typename std::enable_if<
std::is_same_v<U, uint32_t> || std::is_same_v<U, uint64_t> ||
std::is_same_v<U, int32_t> || std::is_same_v<U, long> || std::is_same_v<U, int64_t> ||
std::is_same_v<U, std::size_t>,
(std::is_same_v<U, uint32_t> || std::is_same_v<U, uint64_t> ||
std::is_same_v<U, int32_t> || std::is_same_v<U, int64_t> || std::is_same_v<U, long>) &&
!std::is_same_v<U, size_t>,
void>::type
to_bytes(T &bytes, std::size_t &byte_index, const U &original_value) {

Expand All @@ -73,6 +73,36 @@ to_bytes(T &bytes, std::size_t &byte_index, const U &original_value) {
}
}



// encode size_t as fixed 4-bytes integer
template <options O, typename T, typename U>
typename std::enable_if<
std::is_same_v<U, std::size_t>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be just size_t instead of std::size_t? To be consistent with the rest of your code in from_bytes.h

void>::type
to_bytes(T &bytes, std::size_t &byte_index, const U &original_value) {

alpaca::detail::size_t_serialized_type value = original_value;
update_value_based_on_alpaca_endian_rules<O,
alpaca::detail::size_t_serialized_type>(value);

constexpr auto use_fixed_length_encoding = (
// Do not perform VLQ if:
// 1. if the system is little-endian and the requested byte order is
// big-endian
// 2. if the system is big-endian and the requested byte order is
// little-endian
// 3. If fixed length encoding is requested
(is_system_little_endian() && detail::big_endian<O>()) ||
(detail::fixed_length_encoding<O>()));

if constexpr (use_fixed_length_encoding) {
copy_bytes_in_range(value, bytes, byte_index);
} else {
encode_varint<U, T>(value, bytes, byte_index);
}
}

// enum class
template <options O, typename T, typename U>
typename std::enable_if<std::is_enum<U>::value, void>::type
Expand Down
Loading