aboutsummaryrefslogtreecommitdiffstats
path: root/docs/concepts.rst
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/concepts.rst
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/concepts.rst')
-rw-r--r--docs/concepts.rst12
1 files changed, 7 insertions, 5 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)
{