Implement extension support for the encoder

This commit is contained in:
Petteri Aimonen
2013-07-17 19:23:19 +03:00
parent 7c5e184c26
commit ebddda9b5c
2 changed files with 74 additions and 22 deletions

2
pb.h
View File

@@ -298,7 +298,7 @@ struct _pb_extension_type_t {
* If you run into an error, return false. * If you run into an error, return false.
* Set to NULL for default handler. * Set to NULL for default handler.
*/ */
bool (*encode)(pb_ostream_t *stream, pb_extension_t *extension); bool (*encode)(pb_ostream_t *stream, const pb_extension_t *extension);
/* Free field for use by the callback. */ /* Free field for use by the callback. */
const void *arg; const void *arg;

View File

@@ -83,10 +83,7 @@ bool checkreturn pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count
/* Main encoding stuff */ /* Main encoding stuff */
/* Callbacks don't need this function because they usually know the data type /* Encode a static array. Handles the size calculations and possible packing. */
* without examining the field structure.
* Therefore it is static for now.
*/
static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field, static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field,
const void *pData, size_t count, pb_encoder_t func) const void *pData, size_t count, pb_encoder_t func)
{ {
@@ -97,6 +94,7 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie
if (count == 0) if (count == 0)
return true; return true;
/* We always pack arrays if the datatype allows it. */
if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE) if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
{ {
if (!pb_encode_tag(stream, PB_WT_STRING, field->tag)) if (!pb_encode_tag(stream, PB_WT_STRING, field->tag))
@@ -155,7 +153,10 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie
return true; return true;
} }
bool checkreturn encode_static_field(pb_ostream_t *stream, const pb_field_t *field, const void *pData) /* Encode a field with static allocation, i.e. one whose data is stored
* in the structure itself. */
static bool checkreturn encode_static_field(pb_ostream_t *stream,
const pb_field_t *field, const void *pData)
{ {
pb_encoder_t func; pb_encoder_t func;
const void *pSize; const void *pSize;
@@ -195,7 +196,10 @@ bool checkreturn encode_static_field(pb_ostream_t *stream, const pb_field_t *fie
return true; return true;
} }
bool checkreturn encode_callback_field(pb_ostream_t *stream, const pb_field_t *field, const void *pData) /* Encode a field with callback semantics. This means that a user function is
* called to provide and encode the actual data. */
static bool checkreturn encode_callback_field(pb_ostream_t *stream,
const pb_field_t *field, const void *pData)
{ {
const pb_callback_t *callback = (const pb_callback_t*)pData; const pb_callback_t *callback = (const pb_callback_t*)pData;
@@ -213,6 +217,57 @@ bool checkreturn encode_callback_field(pb_ostream_t *stream, const pb_field_t *f
return true; return true;
} }
/* Encode a single field of any callback or static type. */
static bool checkreturn encode_field(pb_ostream_t *stream,
const pb_field_t *field, const void *pData)
{
switch (PB_ATYPE(field->type))
{
case PB_ATYPE_STATIC:
return encode_static_field(stream, field, pData);
case PB_ATYPE_CALLBACK:
return encode_callback_field(stream, field, pData);
default:
PB_RETURN_ERROR(stream, "invalid field type");
}
}
/* Default handler for extension fields. Expects to have a pb_field_t
* pointer in the extension->type->arg field. */
static bool checkreturn default_extension_handler(pb_ostream_t *stream,
const pb_extension_t *extension)
{
const pb_field_t *field = (const pb_field_t*)extension->type->arg;
return encode_field(stream, field, extension->dest);
}
/* Walk through all the registered extensions and give them a chance
* to encode themselves. */
static bool checkreturn encode_extension_field(pb_ostream_t *stream,
const pb_field_t *field, const void *pData)
{
const pb_extension_t *extension = *(const pb_extension_t* const *)pData;
UNUSED(field);
while (extension)
{
bool status;
if (extension->type->encode)
status = extension->type->encode(stream, extension);
else
status = default_extension_handler(stream, extension);
if (!status)
return false;
extension = extension->next;
}
return true;
}
bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct) bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
{ {
const pb_field_t *field = fields; const pb_field_t *field = fields;
@@ -231,20 +286,17 @@ bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], cons
prev_size *= field->array_size; prev_size *= field->array_size;
} }
switch (PB_ATYPE(field->type)) if (PB_LTYPE(field->type) == PB_LTYPE_EXTENSION)
{ {
case PB_ATYPE_STATIC: /* Special case for the extension field placeholder */
if (!encode_static_field(stream, field, pData)) if (!encode_extension_field(stream, field, pData))
return false; return false;
break; }
else
case PB_ATYPE_CALLBACK: {
if (!encode_callback_field(stream, field, pData)) /* Regular field */
return false; if (!encode_field(stream, field, pData))
break; return false;
default:
PB_RETURN_ERROR(stream, "invalid field type");
} }
field++; field++;