summaryrefslogtreecommitdiffstats
path: root/docs
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2013-04-02 19:55:21 +0300
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2013-04-02 19:55:21 +0300
commit214b0eae8aa011fa8b3e8a3dcc784f8d423aeffb (patch)
tree8cdebc1ab067bca01d47eefcc9dcf43ce50048db /docs
parent6f3740f74ed48daf51908676b203f1889455c17d (diff)
Change the callback function to use void**.
NOTE: This change breaks backwards-compatibility by default. If you have old callback functions, you can define PB_OLD_CALLBACK_STYLE to retain the old behaviour. If you want to convert your old callbacks to new signature, you need to do the following: 1) Change decode callback argument to void **arg and encode callback argument to void * const *arg. 2) Change any reference to arg into *arg. The rationale for making the new behaviour the default is that it simplifies the common case of "allocate some memory in decode callback". Update issue 69 Status: FixedInGit
Diffstat (limited to 'docs')
-rw-r--r--docs/concepts.rst12
-rw-r--r--docs/reference.rst10
2 files changed, 14 insertions, 8 deletions
diff --git a/docs/concepts.rst b/docs/concepts.rst
index 052edcc1..4bc0dee8 100644
--- a/docs/concepts.rst
+++ b/docs/concepts.rst
@@ -181,7 +181,9 @@ Field callbacks
===============
When a field has dynamic length, nanopb cannot statically allocate storage for it. Instead, it allows you to handle the field in whatever way you want, using a callback function.
-The `pb_callback_t`_ structure contains a function pointer and a *void* pointer you can use for passing data to the callback. If the function pointer is NULL, the field will be skipped. The actual behavior of the callback function is different in encoding and decoding modes.
+The `pb_callback_t`_ structure contains a function pointer and a *void* pointer called *arg* you can use for passing data to the callback. If the function pointer is NULL, the field will be skipped. A pointer to the *arg* is passed to the function, so that it can modify it and retrieve the value.
+
+The actual behavior of the callback function is different in encoding and decoding modes. In encoding mode, the callback is called once and should write out everything, including field tags. In decoding mode, the callback is called repeatedly for every data item.
.. _`pb_callback_t`: reference.html#pb-callback-t
@@ -189,7 +191,7 @@ Encoding callbacks
------------------
::
- bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg);
+ bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg);
When encoding, the callback should write out complete fields, including the wire type and field number tag. It can write as many or as few fields as it likes. For example, if you want to write out an array as *repeated* field, you should do it all in a single call.
@@ -203,7 +205,7 @@ If the callback is used in a submessage, it will be called multiple times during
This callback writes out a dynamically sized string::
- bool write_string(pb_ostream_t *stream, const pb_field_t *field, const void *arg)
+ bool write_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
{
char *str = get_string_from_somewhere();
if (!pb_encode_tag_for_field(stream, field))
@@ -216,7 +218,7 @@ Decoding callbacks
------------------
::
- bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
+ bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg);
When decoding, the callback receives a length-limited substring that reads the contents of a single field. The field tag has already been read. For *string* and *bytes*, the length value has already been parsed, and is available at *stream->bytes_left*.
@@ -226,7 +228,7 @@ The callback will be called multiple times for repeated fields. For packed field
This callback reads multiple integers and prints them::
- bool read_ints(pb_istream_t *stream, const pb_field_t *field, void *arg)
+ bool read_ints(pb_istream_t *stream, const pb_field_t *field, void **arg)
{
while (stream->bytes_left)
{
diff --git a/docs/reference.rst b/docs/reference.rst
index 93aae8b0..0db8e43e 100644
--- a/docs/reference.rst
+++ b/docs/reference.rst
@@ -24,6 +24,8 @@ PB_NO_ERRMSG Disables the support for error messages; only err
Decreases the code size by a few hundred bytes.
PB_BUFFER_ONLY Disables the support for custom streams. Only supports encoding to memory buffers.
Speeds up execution and decreases code size slightly.
+PB_OLD_CALLBACK_STYLE Use the old function signature (void\* instead of void\*\*) for callback fields. This was the
+ default until nanopb-0.2.1.
============================ ================================================================================================
The PB_MAX_REQUIRED_FIELDS, PB_FIELD_16BIT and PB_FIELD_32BIT settings allow raising some datatype limits to suit larger messages.
@@ -122,14 +124,16 @@ Part of a message structure, for fields with type PB_HTYPE_CALLBACK::
typedef struct _pb_callback_t pb_callback_t;
struct _pb_callback_t {
union {
- bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
- bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg);
+ bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg);
+ bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg);
} funcs;
void *arg;
};
-The *arg* is passed to the callback when calling. It can be used to store any information that the callback might need.
+A pointer to the *arg* is passed to the callback when calling. It can be used to store any information that the callback might need.
+
+Previously the function received just the value of *arg* instead of a pointer to it. This old behaviour can be enabled by defining *PB_OLD_CALLBACK_STYLE*.
When calling `pb_encode`_, *funcs.encode* is used, and similarly when calling `pb_decode`_, *funcs.decode* is used. The function pointers are stored in the same memory location but are of incompatible types. You can set the function pointer to NULL to skip the field.