Add support for POINTER type in extensions

This commit is contained in:
Petteri Aimonen
2014-12-26 18:23:36 +02:00
parent 980f899dd5
commit 418f7d88b3
3 changed files with 24 additions and 3 deletions

6
pb.h
View File

@@ -456,9 +456,11 @@ struct pb_extension_s {
0, \ 0, \
pb_membersize(st, m), 0, ptr} pb_membersize(st, m), 0, ptr}
#define PB_OPTEXT_POINTER(tag, st, m, fd, ltype, ptr) \
PB_OPTIONAL_POINTER(tag, st, m, fd, ltype, ptr)
#define PB_OPTEXT_CALLBACK(tag, st, m, fd, ltype, ptr) \ #define PB_OPTEXT_CALLBACK(tag, st, m, fd, ltype, ptr) \
{tag, PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype, \ PB_OPTIONAL_CALLBACK(tag, st, m, fd, ltype, ptr)
0, 0, pb_membersize(st, m), 0, ptr}
/* The mapping from protobuf types to LTYPEs is done using these macros. */ /* The mapping from protobuf types to LTYPEs is done using these macros. */
#define PB_LTYPE_MAP_BOOL PB_LTYPE_VARINT #define PB_LTYPE_MAP_BOOL PB_LTYPE_VARINT

View File

@@ -650,6 +650,14 @@ static bool checkreturn default_extension_decoder(pb_istream_t *stream,
iter.pData = extension->dest; iter.pData = extension->dest;
iter.pSize = &extension->found; iter.pSize = &extension->found;
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
{
/* For pointer extensions, the pointer is stored directly
* in the extension structure. This avoids having an extra
* indirection. */
iter.pData = &extension->dest;
}
return decode_field(stream, wire_type, &iter); return decode_field(stream, wire_type, &iter);
} }

View File

@@ -302,8 +302,19 @@ static bool checkreturn default_extension_encoder(pb_ostream_t *stream,
const pb_extension_t *extension) const pb_extension_t *extension)
{ {
const pb_field_t *field = (const pb_field_t*)extension->type->arg; const pb_field_t *field = (const pb_field_t*)extension->type->arg;
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
{
/* For pointer extensions, the pointer is stored directly
* in the extension structure. This avoids having an extra
* indirection. */
return encode_field(stream, field, &extension->dest);
}
else
{
return encode_field(stream, field, extension->dest); return encode_field(stream, field, extension->dest);
} }
}
/* Walk through all the registered extensions and give them a chance /* Walk through all the registered extensions and give them a chance
* to encode themselves. */ * to encode themselves. */