Skip to content

Commit

Permalink
Update struct_pack_impl.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
982945902 authored Jun 29, 2023
1 parent 8adf9dc commit c6836d6
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions include/struct_pack/struct_pack/struct_pack_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ namespace detail {
#endif
}

#if __GNUC__ || __clang__
template <typename T>
constexpr inline bool is_integral128_v = std::is_same_v<__int128, T> | std::is_same_v<unsigned __int128, T>;
#endif

template <typename... T>
constexpr inline bool is_trivial_tuple<tuplet::tuple<T...>> = true;

Expand All @@ -247,10 +252,17 @@ template <typename T>
template <typename U>
constexpr auto get_types() {
using T = std::remove_cvref_t<U>;
#if __GNUC__ || __clang__
if constexpr (std::is_fundamental_v<T> || std::is_enum_v<T> || varint_t<T> ||
std::is_same_v<std::string, T> || container<T> || optional<T> ||
unique_ptr<T> || variant<T> || expected<T> || array<T> ||
c_array<T> || std::is_same_v<std::monostate, T> || is_integral128_v<T>) {
#else
if constexpr (std::is_fundamental_v<T> || std::is_enum_v<T> || varint_t<T> ||
std::is_same_v<std::string, T> || container<T> || optional<T> ||
unique_ptr<T> || variant<T> || expected<T> || array<T> ||
c_array<T> || std::is_same_v<std::monostate, T>) {
#endif
return declval<std::tuple<T>>();
}
else if constexpr (tuple<T>) {
Expand Down Expand Up @@ -307,7 +319,7 @@ enum class type_id {
uint8_t,
int16_t,
uint16_t,
int128_t, // TODO: support int128/uint128
int128_t, // TODO: support int128/uint128 on msvc, now support on gcc clang
uint128_t,
bool_t,
char_8_t,
Expand Down Expand Up @@ -509,7 +521,7 @@ consteval type_id get_type_id() {
else if constexpr (std::is_integral_v<T>) {
return get_integral_type<T>();
}
#if __GNUC__ && __clang__
#if __GNUC__ || __clang__
else if constexpr (std::is_same_v<__int128, T>) {
return type_id::int128_t;
}
Expand Down Expand Up @@ -1155,9 +1167,15 @@ constexpr size_info inline calculate_one_size(const T &item) {
size_info ret{.total = 0, .size_cnt = 0, .max_size = 0};
if constexpr (id == type_id::monostate_t) {
}
#if __GNUC__ || __clang__
else if constexpr (std::is_fundamental_v<type> || std::is_enum_v<type> || is_integral128_v<type>) {
ret.total = sizeof(type);
}
#else
else if constexpr (std::is_fundamental_v<type> || std::is_enum_v<type>) {
ret.total = sizeof(type);
}
#endif
else if constexpr (detail::varint_t<type>) {
ret.total = detail::calculate_varint_size(item);
}
Expand Down Expand Up @@ -1693,9 +1711,15 @@ class packer {
else if constexpr (std::is_same_v<type, std::monostate>) {
// do nothing
}
#if __GNUC__ || __clang__
else if constexpr (std::is_fundamental_v<type> || std::is_enum_v<type> || is_integral128_v<type>) {
writer_.write((char *)&item, sizeof(type));
}
#else
else if constexpr (std::is_fundamental_v<type> || std::is_enum_v<type>) {
writer_.write((char *)&item, sizeof(type));
}
#endif
else if constexpr (detail::varint_t<type>) {
detail::serialize_varint(writer_, item);
}
Expand Down Expand Up @@ -2431,7 +2455,11 @@ class unpacker {
else if constexpr (std::is_same_v<type, std::monostate>) {
// do nothing
}
#if __GNUC__ || __clang__
else if constexpr (std::is_fundamental_v<type> || std::is_enum_v<type> || is_integral128_v<type>) {
#else
else if constexpr (std::is_fundamental_v<type> || std::is_enum_v<type>) {
#endif
if constexpr (NotSkip) {
if (!reader_.read((char *)&item, sizeof(type))) [[unlikely]] {
return struct_pack::errc::no_buffer_space;
Expand Down

0 comments on commit c6836d6

Please sign in to comment.