Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle committed Nov 21, 2023
1 parent f8c132b commit 143e49f
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 59 deletions.
26 changes: 16 additions & 10 deletions include/ylt/struct_pack/calculate_size.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,17 @@ template <uint64_t parent_tag, typename Arg, typename... Args>
constexpr bool STRUCT_PACK_INLINE has_signed_varint() {
if constexpr (sizeof...(Args) == 0) {
if constexpr (varint_t<Arg, parent_tag>) {
return std::is_signed_v<remove_cvref_t<decltype(get_varint_value(declval<Arg>()))>>;
return std::is_signed_v<
remove_cvref_t<decltype(get_varint_value(declval<Arg>()))>>;
}
else {
return false;
}
}
else {
if constexpr (varint_t<Arg, parent_tag>) {
return std::is_signed_v<remove_cvref_t<decltype(get_varint_value(declval<Arg>()))>> ||
return std::is_signed_v<
remove_cvref_t<decltype(get_varint_value(declval<Arg>()))>> ||
has_signed_varint<parent_tag, Args...>();
}
else {
Expand All @@ -222,15 +224,17 @@ template <uint64_t parent_tag, typename Arg, typename... Args>
constexpr bool STRUCT_PACK_INLINE has_unsigned_varint() {
if constexpr (sizeof...(Args) == 0) {
if constexpr (varint_t<Arg, parent_tag>) {
return std::is_unsigned_v<remove_cvref_t<decltype(get_varint_value(declval<Arg>()))>>;
return std::is_unsigned_v<
remove_cvref_t<decltype(get_varint_value(declval<Arg>()))>>;
}
else {
return false;
}
}
else {
if constexpr (varint_t<Arg, parent_tag>) {
return std::is_unsigned_v<remove_cvref_t<decltype(get_varint_value(declval<Arg>()))>> ||
return std::is_unsigned_v<
remove_cvref_t<decltype(get_varint_value(declval<Arg>()))>> ||
has_unsigned_varint<parent_tag, Args...>();
}
else {
Expand All @@ -254,13 +258,15 @@ constexpr void STRUCT_PACK_INLINE get_fast_varint_width_impl(
static_assert(!sizeof(Arg), "illegal branch");
}
if constexpr (varint_t<Arg, parent_tag>) {
if constexpr (std::is_unsigned_v<
std::remove_reference_t<decltype(get_varint_value(item))>>) {
if constexpr (std::is_unsigned_v<std::remove_reference_t<
decltype(get_varint_value(item))>>) {
unsigned_max = std::max<uint64_t>(unsigned_max, get_varint_value(item));
}
else {
signed_max = std::max<int64_t>(
signed_max, get_varint_value(item) > 0 ? get_varint_value(item) : -(get_varint_value(item) + 1));
signed_max =
std::max<int64_t>(signed_max, get_varint_value(item) > 0
? get_varint_value(item)
: -(get_varint_value(item) + 1));
}
}
}
Expand All @@ -269,7 +275,7 @@ constexpr void STRUCT_PACK_INLINE get_fast_varint_width_impl(
template <uint64_t parent_tag, typename... Args>
constexpr int STRUCT_PACK_INLINE
get_fast_varint_width_from_max(uint64_t unsigned_max, int64_t signed_max) {
int width_unsigned, width_signed;
int width_unsigned = 0, width_signed = 0;
if constexpr (has_unsigned_varint<parent_tag, Args...>()) {
if SP_LIKELY (unsigned_max <= UINT8_MAX) {
width_unsigned = 0;
Expand Down Expand Up @@ -298,7 +304,7 @@ get_fast_varint_width_from_max(uint64_t unsigned_max, int64_t signed_max) {
width_signed = 3;
}
}
if constexpr (has_signed_varint<parent_tag, Args...>()&&
if constexpr (has_signed_varint<parent_tag, Args...>() &&
has_unsigned_varint<parent_tag, Args...>()) {
return std::max(width_unsigned, width_signed);
}
Expand Down
4 changes: 2 additions & 2 deletions include/ylt/struct_pack/endian_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,12 @@ template <std::size_t block_size, typename reader_t, typename T>
bool low_bytes_read_wrapper(reader_t& reader, T& elem) {
static_assert(sizeof(T) >= block_size);
if constexpr (is_system_little_endian || block_size == sizeof(T)) {
char* SP_RESTRICT data = (char* )&elem;
char* SP_RESTRICT data = (char*)&elem;
return static_cast<bool>(reader.read(data, block_size));
}
else {
char tmp[block_size];
char* SP_RESTRICT data = (char* )&elem + sizeof(T) - block_size;
char* SP_RESTRICT data = (char*)&elem + sizeof(T) - block_size;
bool res = static_cast<bool>(reader.read(tmp, block_size));
if SP_UNLIKELY (!res) {
return res;
Expand Down
2 changes: 1 addition & 1 deletion include/ylt/struct_pack/packer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class packer {
write_wrapper<sizeof(char)>(writer_, (char *)&has_value);
if (has_value) {
if constexpr (is_base_class<typename type::element_type>) {
bool is_ok;
bool is_ok{};
uint32_t id = item->get_struct_pack_id();
auto index = search_type_by_md5<typename type::element_type>(
item->get_struct_pack_id(), is_ok);
Expand Down
5 changes: 2 additions & 3 deletions include/ylt/struct_pack/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,15 +563,14 @@ template <typename T, typename = void>
#if __cpp_concepts >= 201907L
template <typename Type>
concept user_defined_config = requires {
std::is_same_v<decltype(Type::struct_pack_config), sp_config>;
Type::struct_pack_config;
};
#else
template <typename T, typename = void>
struct user_defined_config_impl : std::false_type {};

template <typename T>
struct user_defined_config_impl<T, std::void_t<
std::enable_if_t<std::is_same_v<decltype(T::struct_pack_config),struct_pack::sp_config>>>>
struct user_defined_config_impl<T, std::void_t<decltype(T::struct_pack_config)>>
: std::true_type {};

template <typename T>
Expand Down
14 changes: 7 additions & 7 deletions include/ylt/struct_pack/unpacker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ class unpacker {
if SP_UNLIKELY (code != struct_pack::errc{}) {
return code;
}
if constexpr (sizeof...(items)) {
if constexpr (sizeof...(items) > 0) {
return deserialize_many<size_type, version, NotSkip, parent_tag>(
items...);
}
Expand Down Expand Up @@ -628,7 +628,7 @@ class unpacker {
reader_.ignore(real_width) ? errc{} : errc::no_buffer_space;
}
else {
bool ec;
bool ec{};
if constexpr (std::is_unsigned_v<std::remove_reference_t<
decltype(get_varint_value(item))>>) {
get_varint_value(item) = 0;
Expand Down Expand Up @@ -689,7 +689,7 @@ class unpacker {
}
std::size_t width = vec[cnt] + vec[cnt + 1] * 2;
int i = 0;
struct_pack::errc ec;
struct_pack::errc ec{};
switch (width) {
case 0:
ec = deserialize_fast_varint_helper<parent_tag, no_skip, 1>(vec, i,
Expand Down Expand Up @@ -785,9 +785,9 @@ class unpacker {
return {};
}
if constexpr (is_base_class<typename type::element_type>) {
uint32_t id;
uint32_t id{};
read_wrapper<sizeof(id)>(reader_, (char *)&id);
bool ok;
bool ok{};
auto index = search_type_by_md5<typename type::element_type>(id, ok);
if SP_UNLIKELY (!ok) {
return errc::invalid_buffer;
Expand Down Expand Up @@ -831,7 +831,7 @@ class unpacker {
}
else if constexpr (container<type>) {
uint64_t size64 = 0;
bool result;
bool result{};
if constexpr (size_type == 1) {
if SP_UNLIKELY (!low_bytes_read_wrapper<size_type>(reader_, size64)) {
return struct_pack::errc::no_buffer_space;
Expand Down Expand Up @@ -1137,7 +1137,7 @@ class unpacker {
}
if constexpr (is_base_class<typename type::element_type>) {
uint32_t id = item->get_struct_pack_id();
bool ok;
bool ok{};
auto index = search_type_by_md5<typename type::element_type>(id, ok);
assert(ok);
return template_switch<deserialize_one_derived_class_helper<
Expand Down
2 changes: 0 additions & 2 deletions include/ylt/struct_pack/varint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,6 @@ auto& get_varint_value(T& v) {
}
}



} // namespace detail
using var_int32_t = detail::sint<int32_t>;
using var_int64_t = detail::sint<int64_t>;
Expand Down
8 changes: 4 additions & 4 deletions src/struct_pack/tests/test_compile_time_calculate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,8 @@ TEST_CASE("test fast varint tag") {
{(char)type_id::struct_t, (char)type_id::vint32_t,
(char)type_id::vint64_t, (char)type_id::vuint32_t,
(char)type_id::vuint64_t, (char)type_id::type_end_flag}};
CHECK(type_info1 == type_info2);
CHECK(type_info1 == type_info3);
CHECK(type_info3 != type_info4);
CHECK(type_info1 == fast_varint_info);
static_assert(type_info1 == type_info2);
static_assert(type_info1 == type_info3);
static_assert(type_info3 != type_info4);
static_assert(type_info1 == fast_varint_info);
}
30 changes: 0 additions & 30 deletions test.cpp

This file was deleted.

0 comments on commit 143e49f

Please sign in to comment.