mirror of
https://github.com/protobuf-c/protobuf-c.git
synced 2024-12-27 13:31:02 +08:00
Merge pull request #265 from pbor/wip/cleanup
Small cleanup in oneof handling
This commit is contained in:
commit
0014b7c046
@ -466,7 +466,7 @@ required_field_get_packed_size(const ProtobufCFieldDescriptor *field,
|
|||||||
* \param field
|
* \param field
|
||||||
* Field descriptor for member.
|
* Field descriptor for member.
|
||||||
* \param oneof_case
|
* \param oneof_case
|
||||||
* A pointer to the case enum that selects the field in the oneof.
|
* Enum value that selects the field in the oneof.
|
||||||
* \param member
|
* \param member
|
||||||
* Field to encode.
|
* Field to encode.
|
||||||
* \return
|
* \return
|
||||||
@ -474,20 +474,19 @@ required_field_get_packed_size(const ProtobufCFieldDescriptor *field,
|
|||||||
*/
|
*/
|
||||||
static size_t
|
static size_t
|
||||||
oneof_field_get_packed_size(const ProtobufCFieldDescriptor *field,
|
oneof_field_get_packed_size(const ProtobufCFieldDescriptor *field,
|
||||||
const uint32_t *oneof_case,
|
uint32_t oneof_case,
|
||||||
const void *member)
|
const void *member)
|
||||||
{
|
{
|
||||||
if (*oneof_case == field->id) {
|
if (oneof_case != field->id) {
|
||||||
if (field->type == PROTOBUF_C_TYPE_MESSAGE ||
|
|
||||||
field->type == PROTOBUF_C_TYPE_STRING)
|
|
||||||
{
|
|
||||||
const void *ptr = *(const void * const *) member;
|
|
||||||
if (ptr == NULL || ptr == field->default_value)
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
if (field->type == PROTOBUF_C_TYPE_MESSAGE ||
|
||||||
|
field->type == PROTOBUF_C_TYPE_STRING)
|
||||||
|
{
|
||||||
|
const void *ptr = *(const void * const *) member;
|
||||||
|
if (ptr == NULL || ptr == field->default_value)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
return required_field_get_packed_size(field, member);
|
return required_field_get_packed_size(field, member);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -690,7 +689,11 @@ size_t protobuf_c_message_get_packed_size(const ProtobufCMessage *message)
|
|||||||
} else if ((field->label == PROTOBUF_C_LABEL_OPTIONAL ||
|
} else if ((field->label == PROTOBUF_C_LABEL_OPTIONAL ||
|
||||||
field->label == PROTOBUF_C_LABEL_NONE) &&
|
field->label == PROTOBUF_C_LABEL_NONE) &&
|
||||||
(0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF))) {
|
(0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF))) {
|
||||||
rv += oneof_field_get_packed_size(field, qmember, member);
|
rv += oneof_field_get_packed_size(
|
||||||
|
field,
|
||||||
|
*(const uint32_t *) qmember,
|
||||||
|
member
|
||||||
|
);
|
||||||
} else if (field->label == PROTOBUF_C_LABEL_OPTIONAL) {
|
} else if (field->label == PROTOBUF_C_LABEL_OPTIONAL) {
|
||||||
rv += optional_field_get_packed_size(
|
rv += optional_field_get_packed_size(
|
||||||
field,
|
field,
|
||||||
@ -1105,7 +1108,7 @@ required_field_pack(const ProtobufCFieldDescriptor *field,
|
|||||||
* \param field
|
* \param field
|
||||||
* Field descriptor.
|
* Field descriptor.
|
||||||
* \param oneof_case
|
* \param oneof_case
|
||||||
* A pointer to the case enum that selects the field in the oneof.
|
* Enum value that selects the field in the oneof.
|
||||||
* \param member
|
* \param member
|
||||||
* The field member.
|
* The field member.
|
||||||
* \param[out] out
|
* \param[out] out
|
||||||
@ -1115,20 +1118,19 @@ required_field_pack(const ProtobufCFieldDescriptor *field,
|
|||||||
*/
|
*/
|
||||||
static size_t
|
static size_t
|
||||||
oneof_field_pack(const ProtobufCFieldDescriptor *field,
|
oneof_field_pack(const ProtobufCFieldDescriptor *field,
|
||||||
const uint32_t *oneof_case,
|
uint32_t oneof_case,
|
||||||
const void *member, uint8_t *out)
|
const void *member, uint8_t *out)
|
||||||
{
|
{
|
||||||
if (*oneof_case == field->id) {
|
if (oneof_case != field->id) {
|
||||||
if (field->type == PROTOBUF_C_TYPE_MESSAGE ||
|
|
||||||
field->type == PROTOBUF_C_TYPE_STRING)
|
|
||||||
{
|
|
||||||
const void *ptr = *(const void * const *) member;
|
|
||||||
if (ptr == NULL || ptr == field->default_value)
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
if (field->type == PROTOBUF_C_TYPE_MESSAGE ||
|
||||||
|
field->type == PROTOBUF_C_TYPE_STRING)
|
||||||
|
{
|
||||||
|
const void *ptr = *(const void * const *) member;
|
||||||
|
if (ptr == NULL || ptr == field->default_value)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
return required_field_pack(field, member, out);
|
return required_field_pack(field, member, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1459,7 +1461,12 @@ protobuf_c_message_pack(const ProtobufCMessage *message, uint8_t *out)
|
|||||||
} else if ((field->label == PROTOBUF_C_LABEL_OPTIONAL ||
|
} else if ((field->label == PROTOBUF_C_LABEL_OPTIONAL ||
|
||||||
field->label == PROTOBUF_C_LABEL_NONE) &&
|
field->label == PROTOBUF_C_LABEL_NONE) &&
|
||||||
(0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF))) {
|
(0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF))) {
|
||||||
rv += oneof_field_pack (field, qmember, member, out + rv);
|
rv += oneof_field_pack(
|
||||||
|
field,
|
||||||
|
*(const uint32_t *) qmember,
|
||||||
|
member,
|
||||||
|
out + rv
|
||||||
|
);
|
||||||
} else if (field->label == PROTOBUF_C_LABEL_OPTIONAL) {
|
} else if (field->label == PROTOBUF_C_LABEL_OPTIONAL) {
|
||||||
rv += optional_field_pack(
|
rv += optional_field_pack(
|
||||||
field,
|
field,
|
||||||
@ -1608,7 +1615,7 @@ required_field_pack_to_buffer(const ProtobufCFieldDescriptor *field,
|
|||||||
* \param field
|
* \param field
|
||||||
* Field descriptor.
|
* Field descriptor.
|
||||||
* \param oneof_case
|
* \param oneof_case
|
||||||
* A pointer to the case enum that selects the field in the oneof.
|
* Enum value that selects the field in the oneof.
|
||||||
* \param member
|
* \param member
|
||||||
* The element to be packed.
|
* The element to be packed.
|
||||||
* \param[out] buffer
|
* \param[out] buffer
|
||||||
@ -1618,20 +1625,19 @@ required_field_pack_to_buffer(const ProtobufCFieldDescriptor *field,
|
|||||||
*/
|
*/
|
||||||
static size_t
|
static size_t
|
||||||
oneof_field_pack_to_buffer(const ProtobufCFieldDescriptor *field,
|
oneof_field_pack_to_buffer(const ProtobufCFieldDescriptor *field,
|
||||||
const uint32_t *oneof_case,
|
uint32_t oneof_case,
|
||||||
const void *member, ProtobufCBuffer *buffer)
|
const void *member, ProtobufCBuffer *buffer)
|
||||||
{
|
{
|
||||||
if (*oneof_case == field->id) {
|
if (oneof_case != field->id) {
|
||||||
if (field->type == PROTOBUF_C_TYPE_MESSAGE ||
|
|
||||||
field->type == PROTOBUF_C_TYPE_STRING)
|
|
||||||
{
|
|
||||||
const void *ptr = *(const void *const *) member;
|
|
||||||
if (ptr == NULL || ptr == field->default_value)
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
if (field->type == PROTOBUF_C_TYPE_MESSAGE ||
|
||||||
|
field->type == PROTOBUF_C_TYPE_STRING)
|
||||||
|
{
|
||||||
|
const void *ptr = *(const void *const *) member;
|
||||||
|
if (ptr == NULL || ptr == field->default_value)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
return required_field_pack_to_buffer(field, member, buffer);
|
return required_field_pack_to_buffer(field, member, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1938,7 +1944,7 @@ protobuf_c_message_pack_to_buffer(const ProtobufCMessage *message,
|
|||||||
(0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF))) {
|
(0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF))) {
|
||||||
rv += oneof_field_pack_to_buffer(
|
rv += oneof_field_pack_to_buffer(
|
||||||
field,
|
field,
|
||||||
qmember,
|
*(const uint32_t *) qmember,
|
||||||
member,
|
member,
|
||||||
buffer
|
buffer
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user