-
Notifications
You must be signed in to change notification settings - Fork 225
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
improve [struct_pb] #257
Merged
Merged
improve [struct_pb] #257
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
8f541f2
fix; support [double, float, bool]
bbbgan ddbd4a4
support precaculate the pb size
bbbgan 1850ff1
support compile time caculate key size
bbbgan a009b17
struct_pb split to pb_reader and pb_writer
bbbgan d8256bf
improve pb_writer
bbbgan eadde96
Simplify code
bbbgan ee62aaa
support custom reflection struct
bbbgan 11c3e29
add test with proto[struct_pb]
bbbgan 9af6367
add test; fix packed value miss default value
bbbgan d316358
fix to_pb break condition; add test
bbbgan 680c277
update workflow
bbbgan fbf1ca6
add map test[struct_pb]
bbbgan 9bcac28
add some unusual tests; fix some[struct_pb]
bbbgan 4e046a6
simplify pb_reader
bbbgan 81aafed
support oneof by std::variant[struct_pb]
bbbgan 46bbc22
fix warning[struct_pb]
bbbgan bda33a4
fix get_map_tuple_impl; add oneof test[struct_pb]
bbbgan 0482628
remove one_of_t[struct_pb]
bbbgan eb3e2d1
add benchmark
bbbgan a0221e9
improve pb_writer[struct_pb]
bbbgan 14eacc5
add pb_base to improve pb_writer; improve pb_reder [struct_pb]
bbbgan 1e369a7
simplify some
qicosmos cc9f2a7
add assertions to benchmark[struct_pb]
bbbgan 9dd7b1a
fix[struct_pb]
bbbgan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,15 +112,17 @@ constexpr bool is_one_of_v = is_one_of_t<T>::value; | |
template <typename T> | ||
constexpr inline WireType get_wire_type() { | ||
if constexpr (std::is_integral_v<T> || is_signed_varint_v<T> || | ||
std::is_enum_v<T>) { | ||
std::is_enum_v<T> || std::is_same_v<T, bool>) { | ||
return WireType::Varint; | ||
} | ||
else if constexpr (std::is_same_v<T, fixed32_t> || | ||
std::is_same_v<T, sfixed32_t>) { | ||
std::is_same_v<T, sfixed32_t> || | ||
std::is_same_v<T, float>) { | ||
return WireType::Fixed32; | ||
} | ||
else if constexpr (std::is_same_v<T, fixed64_t> || | ||
std::is_same_v<T, sfixed64_t>) { | ||
std::is_same_v<T, sfixed64_t> || | ||
std::is_same_v<T, double>) { | ||
return WireType::Fixed64; | ||
} | ||
else if constexpr (std::is_same_v<T, std::string> || | ||
|
@@ -221,10 +223,10 @@ inline void from_pb_impl(T& val, std::string_view& pb_str, uint32_t field_no) { | |
if constexpr (is_reflection_v<value_type>) { | ||
size_t pos; | ||
uint32_t size = detail::decode_varint(pb_str, pos); | ||
pb_str = pb_str.substr(pos); | ||
if (pb_str.size() < size) { | ||
throw std::invalid_argument("Invalid fixed int value: too few bytes."); | ||
} | ||
pb_str = pb_str.substr(pos); | ||
if (size == 0) { | ||
return; | ||
} | ||
|
@@ -257,11 +259,10 @@ inline void from_pb_impl(T& val, std::string_view& pb_str, uint32_t field_no) { | |
// non-message has size | ||
size_t pos; | ||
uint32_t size = detail::decode_varint(pb_str, pos); | ||
pb_str = pb_str.substr(pos); | ||
if (pb_str.size() < size) { | ||
throw std::invalid_argument("Invalid fixed int value: too few bytes."); | ||
} | ||
pb_str = pb_str.substr(pos); | ||
|
||
using item_type = typename value_type::value_type; | ||
size_t start = pb_str.size(); | ||
|
||
|
@@ -281,10 +282,10 @@ inline void from_pb_impl(T& val, std::string_view& pb_str, uint32_t field_no) { | |
while (!pb_str.empty()) { | ||
size_t pos; | ||
uint32_t size = detail::decode_varint(pb_str, pos); | ||
pb_str = pb_str.substr(pos); | ||
if (pb_str.size() < size) { | ||
throw std::invalid_argument("Invalid fixed int value: too few bytes."); | ||
} | ||
pb_str = pb_str.substr(pos); | ||
|
||
item_type item = {}; | ||
decode_pair_value(item.first, pb_str); | ||
|
@@ -305,7 +306,7 @@ inline void from_pb_impl(T& val, std::string_view& pb_str, uint32_t field_no) { | |
} | ||
} | ||
else if constexpr (std::is_integral_v<value_type>) { | ||
val = detail::decode_varint(pb_str, pos); | ||
val = static_cast<value_type>(detail::decode_varint(pb_str, pos)); | ||
pb_str = pb_str.substr(pos); | ||
} | ||
else if constexpr (detail::is_signed_varint_v<value_type>) { | ||
|
@@ -320,8 +321,11 @@ inline void from_pb_impl(T& val, std::string_view& pb_str, uint32_t field_no) { | |
} | ||
pb_str = pb_str.substr(pos); | ||
} | ||
else if constexpr (detail::is_fixed_v<value_type>) { | ||
constexpr size_t size = sizeof(typename value_type::value_type); | ||
else if constexpr (detail::is_fixed_v<value_type> || | ||
std::is_same_v<value_type, double> || | ||
std::is_same_v<value_type, float>) { | ||
constexpr size_t size = | ||
sizeof(typename detail::get_inner_type<value_type>::v_type); | ||
if (pb_str.size() < size) { | ||
throw std::invalid_argument("Invalid fixed int value: too few bytes."); | ||
} | ||
|
@@ -405,7 +409,6 @@ inline void to_pb_impl(T& val, size_t field_no, std::string& out) { | |
std::string temp = encode_pair_value<first_type>(k, 1); | ||
std::string second_temp = encode_pair_value<second_type>(v, 2); | ||
encode_key(field_no, WireType::LengthDelimeted, out); | ||
|
||
serialize_varint(temp.size() + second_temp.size(), out); | ||
out.append(temp).append(second_temp); | ||
} | ||
|
@@ -416,14 +419,14 @@ inline void to_pb_impl(T& val, size_t field_no, std::string& out) { | |
else if constexpr (detail::is_signed_varint_v<value_type>) { | ||
detail::encode_signed_varint_field(field_no, WireType::Varint, val, out); | ||
} | ||
else if constexpr (detail::is_fixed_v<value_type>) { | ||
else if constexpr (detail::is_fixed_v<value_type> || | ||
std::is_same_v<value_type, double> || | ||
std::is_same_v<value_type, float>) { | ||
if constexpr (sizeof(value_type) == 8) { | ||
detail::encode_fixed_field(field_no, WireType::Fixed64, (uint64_t)(val), | ||
out); | ||
detail::encode_fixed_field(field_no, WireType::Fixed64, val, out); | ||
} | ||
else { | ||
detail::encode_fixed_field(field_no, WireType::Fixed32, (uint32_t)(val), | ||
out); | ||
detail::encode_fixed_field(field_no, WireType::Fixed32, val, out); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里应该不需要强制转换吧,在处理double类型会出现问题,我认为没有强制转换的必要,所以我删除了。 |
||
} | ||
} | ||
else if constexpr (std::is_same_v<value_type, std::string> || | ||
|
@@ -439,7 +442,6 @@ inline void to_pb_impl(T& val, size_t field_no, std::string& out) { | |
if (!val.has_value()) { | ||
return; | ||
} | ||
|
||
to_pb_impl<typename value_type::value_type>(*val, field_no, out); | ||
} | ||
else if constexpr (is_one_of_v<value_type>) { | ||
|
@@ -486,7 +488,6 @@ inline void from_pb(T& t, std::string_view pb_str) { | |
if (wire_type != detail::get_wire_type<value_type>()) { | ||
return; | ||
} | ||
|
||
if constexpr (detail::is_signed_varint_v<value_type> || | ||
detail::is_fixed_v<value_type>) { | ||
detail::from_pb_impl<value_type>(val.value(t).val, pb_str); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bug,应该先移动再判断,判断剩余的字符串的大小和size之间的关系。剩下有几处也类似