Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
pb.h
00001 /* Common parts of the nanopb library. Most of these are quite low-level 00002 * stuff. For the high-level interface, see pb_encode.h and pb_decode.h. 00003 */ 00004 00005 #ifndef PB_H_INCLUDED 00006 #define PB_H_INCLUDED 00007 00008 /***************************************************************** 00009 * Nanopb compilation time options. You can change these here by * 00010 * uncommenting the lines, or on the compiler command line. * 00011 *****************************************************************/ 00012 00013 /* Enable support for dynamically allocated fields */ 00014 /* #define PB_ENABLE_MALLOC 1 */ 00015 00016 /* Define this if your CPU architecture is big endian, i.e. it 00017 * stores the most-significant byte first. */ 00018 /* #define __BIG_ENDIAN__ 1 */ 00019 00020 /* Define this if your CPU / compiler combination does not support 00021 * unaligned memory access to packed structures. */ 00022 /* #define PB_NO_PACKED_STRUCTS 1 */ 00023 00024 /* Increase the number of required fields that are tracked. 00025 * A compiler warning will tell if you need this. */ 00026 /* #define PB_MAX_REQUIRED_FIELDS 256 */ 00027 00028 /* Add support for tag numbers > 255 and fields larger than 255 bytes. */ 00029 /* #define PB_FIELD_16BIT 1 */ 00030 00031 /* Add support for tag numbers > 65536 and fields larger than 65536 bytes. */ 00032 /* #define PB_FIELD_32BIT 1 */ 00033 00034 /* Disable support for error messages in order to save some code space. */ 00035 /* #define PB_NO_ERRMSG 1 */ 00036 00037 /* Disable support for custom streams (support only memory buffers). */ 00038 /* #define PB_BUFFER_ONLY 1 */ 00039 00040 /* Switch back to the old-style callback function signature. 00041 * This was the default until nanopb-0.2.1. */ 00042 /* #define PB_OLD_CALLBACK_STYLE */ 00043 00044 00045 /****************************************************************** 00046 * You usually don't need to change anything below this line. * 00047 * Feel free to look around and use the defined macros, though. * 00048 ******************************************************************/ 00049 00050 00051 /* Version of the nanopb library. Just in case you want to check it in 00052 * your own program. */ 00053 #define NANOPB_VERSION nanopb-0.3.4 00054 00055 /* Include all the system headers needed by nanopb. You will need the 00056 * definitions of the following: 00057 * - strlen, memcpy, memset functions 00058 * - [u]int8_t, [u]int16_t, [u]int32_t, [u]int64_t 00059 * - size_t 00060 * - bool 00061 * 00062 * If you don't have the standard header files, you can instead provide 00063 * a custom header that defines or includes all this. In that case, 00064 * define PB_SYSTEM_HEADER to the path of this file. 00065 */ 00066 #ifdef PB_SYSTEM_HEADER 00067 #include PB_SYSTEM_HEADER 00068 #else 00069 #include <stdint.h> 00070 #include <stddef.h> 00071 #include <stdbool.h> 00072 #include <string.h> 00073 00074 #ifdef PB_ENABLE_MALLOC 00075 #include <stdlib.h> 00076 #endif 00077 #endif 00078 00079 /* Macro for defining packed structures (compiler dependent). 00080 * This just reduces memory requirements, but is not required. 00081 */ 00082 #if defined(PB_NO_PACKED_STRUCTS) 00083 /* Disable struct packing */ 00084 # define PB_PACKED_STRUCT_START 00085 # define PB_PACKED_STRUCT_END 00086 # define pb_packed 00087 #elif defined(__GNUC__) || defined(__clang__) 00088 /* For GCC and clang */ 00089 # define PB_PACKED_STRUCT_START 00090 # define PB_PACKED_STRUCT_END 00091 # define pb_packed __attribute__((packed)) 00092 #elif defined(__ICCARM__) || defined(__CC_ARM) 00093 /* For IAR ARM and Keil MDK-ARM compilers */ 00094 # define PB_PACKED_STRUCT_START _Pragma("pack(push, 1)") 00095 # define PB_PACKED_STRUCT_END _Pragma("pack(pop)") 00096 # define pb_packed 00097 #elif defined(_MSC_VER) && (_MSC_VER >= 1500) 00098 /* For Microsoft Visual C++ */ 00099 # define PB_PACKED_STRUCT_START __pragma(pack(push, 1)) 00100 # define PB_PACKED_STRUCT_END __pragma(pack(pop)) 00101 # define pb_packed 00102 #else 00103 /* Unknown compiler */ 00104 # define PB_PACKED_STRUCT_START 00105 # define PB_PACKED_STRUCT_END 00106 # define pb_packed 00107 #endif 00108 00109 /* Handly macro for suppressing unreferenced-parameter compiler warnings. */ 00110 #ifndef PB_UNUSED 00111 #define PB_UNUSED(x) (void)(x) 00112 #endif 00113 00114 /* Compile-time assertion, used for checking compatible compilation options. 00115 * If this does not work properly on your compiler, use 00116 * #define PB_NO_STATIC_ASSERT to disable it. 00117 * 00118 * But before doing that, check carefully the error message / place where it 00119 * comes from to see if the error has a real cause. Unfortunately the error 00120 * message is not always very clear to read, but you can see the reason better 00121 * in the place where the PB_STATIC_ASSERT macro was called. 00122 */ 00123 #ifndef PB_NO_STATIC_ASSERT 00124 #ifndef PB_STATIC_ASSERT 00125 #define PB_STATIC_ASSERT(COND,MSG) typedef char PB_STATIC_ASSERT_MSG(MSG, __LINE__, __COUNTER__)[(COND)?1:-1]; 00126 #define PB_STATIC_ASSERT_MSG(MSG, LINE, COUNTER) PB_STATIC_ASSERT_MSG_(MSG, LINE, COUNTER) 00127 #define PB_STATIC_ASSERT_MSG_(MSG, LINE, COUNTER) pb_static_assertion_##MSG##LINE##COUNTER 00128 #endif 00129 #else 00130 #define PB_STATIC_ASSERT(COND,MSG) 00131 #endif 00132 00133 /* Number of required fields to keep track of. */ 00134 #ifndef PB_MAX_REQUIRED_FIELDS 00135 #define PB_MAX_REQUIRED_FIELDS 64 00136 #endif 00137 00138 #if PB_MAX_REQUIRED_FIELDS < 64 00139 #error You should not lower PB_MAX_REQUIRED_FIELDS from the default value (64). 00140 #endif 00141 00142 /* List of possible field types. These are used in the autogenerated code. 00143 * Least-significant 4 bits tell the scalar type 00144 * Most-significant 4 bits specify repeated/required/packed etc. 00145 */ 00146 00147 typedef uint8_t pb_type_t; 00148 00149 /**** Field data types ****/ 00150 00151 /* Numeric types */ 00152 #define PB_LTYPE_VARINT 0x00 /* int32, int64, enum, bool */ 00153 #define PB_LTYPE_UVARINT 0x01 /* uint32, uint64 */ 00154 #define PB_LTYPE_SVARINT 0x02 /* sint32, sint64 */ 00155 #define PB_LTYPE_FIXED32 0x03 /* fixed32, sfixed32, float */ 00156 #define PB_LTYPE_FIXED64 0x04 /* fixed64, sfixed64, double */ 00157 00158 /* Marker for last packable field type. */ 00159 #define PB_LTYPE_LAST_PACKABLE 0x04 00160 00161 /* Byte array with pre-allocated buffer. 00162 * data_size is the length of the allocated PB_BYTES_ARRAY structure. */ 00163 #define PB_LTYPE_BYTES 0x05 00164 00165 /* String with pre-allocated buffer. 00166 * data_size is the maximum length. */ 00167 #define PB_LTYPE_STRING 0x06 00168 00169 /* Submessage 00170 * submsg_fields is pointer to field descriptions */ 00171 #define PB_LTYPE_SUBMESSAGE 0x07 00172 00173 /* Extension pseudo-field 00174 * The field contains a pointer to pb_extension_t */ 00175 #define PB_LTYPE_EXTENSION 0x08 00176 00177 /* Number of declared LTYPES */ 00178 #define PB_LTYPES_COUNT 9 00179 #define PB_LTYPE_MASK 0x0F 00180 00181 /**** Field repetition rules ****/ 00182 00183 #define PB_HTYPE_REQUIRED 0x00 00184 #define PB_HTYPE_OPTIONAL 0x10 00185 #define PB_HTYPE_REPEATED 0x20 00186 #define PB_HTYPE_ONEOF 0x30 00187 #define PB_HTYPE_MASK 0x30 00188 00189 /**** Field allocation types ****/ 00190 00191 #define PB_ATYPE_STATIC 0x00 00192 #define PB_ATYPE_POINTER 0x80 00193 #define PB_ATYPE_CALLBACK 0x40 00194 #define PB_ATYPE_MASK 0xC0 00195 00196 #define PB_ATYPE(x) ((x) & PB_ATYPE_MASK) 00197 #define PB_HTYPE(x) ((x) & PB_HTYPE_MASK) 00198 #define PB_LTYPE(x) ((x) & PB_LTYPE_MASK) 00199 00200 /* Data type used for storing sizes of struct fields 00201 * and array counts. 00202 */ 00203 #if defined(PB_FIELD_32BIT) 00204 #define PB_SIZE_MAX ((uint32_t)-1) 00205 typedef uint32_t pb_size_t; 00206 typedef int32_t pb_ssize_t; 00207 #elif defined(PB_FIELD_16BIT) 00208 #define PB_SIZE_MAX ((uint16_t)-1) 00209 typedef uint16_t pb_size_t; 00210 typedef int16_t pb_ssize_t; 00211 #else 00212 #define PB_SIZE_MAX ((uint8_t)-1) 00213 typedef uint8_t pb_size_t; 00214 typedef int8_t pb_ssize_t; 00215 #endif 00216 00217 /* This structure is used in auto-generated constants 00218 * to specify struct fields. 00219 * You can change field sizes if you need structures 00220 * larger than 256 bytes or field tags larger than 256. 00221 * The compiler should complain if your .proto has such 00222 * structures. Fix that by defining PB_FIELD_16BIT or 00223 * PB_FIELD_32BIT. 00224 */ 00225 PB_PACKED_STRUCT_START 00226 typedef struct pb_field_s pb_field_t; 00227 struct pb_field_s { 00228 pb_size_t tag; 00229 pb_type_t type; 00230 pb_size_t data_offset; /* Offset of field data, relative to previous field. */ 00231 pb_ssize_t size_offset; /* Offset of array size or has-boolean, relative to data */ 00232 pb_size_t data_size; /* Data size in bytes for a single item */ 00233 pb_size_t array_size; /* Maximum number of entries in array */ 00234 00235 /* Field definitions for submessage 00236 * OR default value for all other non-array, non-callback types 00237 * If null, then field will zeroed. */ 00238 const void *ptr; 00239 } pb_packed; 00240 PB_PACKED_STRUCT_END 00241 00242 /* Make sure that the standard integer types are of the expected sizes. 00243 * All kinds of things may break otherwise.. atleast all fixed* types. 00244 * 00245 * If you get errors here, it probably means that your stdint.h is not 00246 * correct for your platform. 00247 */ 00248 PB_STATIC_ASSERT(sizeof(int8_t) == 1, INT8_T_WRONG_SIZE) 00249 PB_STATIC_ASSERT(sizeof(uint8_t) == 1, UINT8_T_WRONG_SIZE) 00250 PB_STATIC_ASSERT(sizeof(int16_t) == 2, INT16_T_WRONG_SIZE) 00251 PB_STATIC_ASSERT(sizeof(uint16_t) == 2, UINT16_T_WRONG_SIZE) 00252 PB_STATIC_ASSERT(sizeof(int32_t) == 4, INT32_T_WRONG_SIZE) 00253 PB_STATIC_ASSERT(sizeof(uint32_t) == 4, UINT32_T_WRONG_SIZE) 00254 PB_STATIC_ASSERT(sizeof(int64_t) == 8, INT64_T_WRONG_SIZE) 00255 PB_STATIC_ASSERT(sizeof(uint64_t) == 8, UINT64_T_WRONG_SIZE) 00256 00257 /* This structure is used for 'bytes' arrays. 00258 * It has the number of bytes in the beginning, and after that an array. 00259 * Note that actual structs used will have a different length of bytes array. 00260 */ 00261 #define PB_BYTES_ARRAY_T(n) struct { pb_size_t size; uint8_t bytes[n]; } 00262 #define PB_BYTES_ARRAY_T_ALLOCSIZE(n) ((size_t)n + offsetof(pb_bytes_array_t, bytes)) 00263 00264 struct pb_bytes_array_s { 00265 pb_size_t size; 00266 uint8_t bytes[1]; 00267 }; 00268 typedef struct pb_bytes_array_s pb_bytes_array_t; 00269 00270 /* This structure is used for giving the callback function. 00271 * It is stored in the message structure and filled in by the method that 00272 * calls pb_decode. 00273 * 00274 * The decoding callback will be given a limited-length stream 00275 * If the wire type was string, the length is the length of the string. 00276 * If the wire type was a varint/fixed32/fixed64, the length is the length 00277 * of the actual value. 00278 * The function may be called multiple times (especially for repeated types, 00279 * but also otherwise if the message happens to contain the field multiple 00280 * times.) 00281 * 00282 * The encoding callback will receive the actual output stream. 00283 * It should write all the data in one call, including the field tag and 00284 * wire type. It can write multiple fields. 00285 * 00286 * The callback can be null if you want to skip a field. 00287 */ 00288 typedef struct pb_istream_s pb_istream_t; 00289 typedef struct pb_ostream_s pb_ostream_t; 00290 typedef struct pb_callback_s pb_callback_t; 00291 struct pb_callback_s { 00292 #ifdef PB_OLD_CALLBACK_STYLE 00293 /* Deprecated since nanopb-0.2.1 */ 00294 union { 00295 bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg); 00296 bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg); 00297 } funcs; 00298 #else 00299 /* New function signature, which allows modifying arg contents in callback. */ 00300 union { 00301 bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg); 00302 bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg); 00303 } funcs; 00304 #endif 00305 00306 /* Free arg for use by callback */ 00307 void *arg; 00308 }; 00309 00310 /* Wire types. Library user needs these only in encoder callbacks. */ 00311 typedef enum { 00312 PB_WT_VARINT = 0, 00313 PB_WT_64BIT = 1, 00314 PB_WT_STRING = 2, 00315 PB_WT_32BIT = 5 00316 } pb_wire_type_t; 00317 00318 /* Structure for defining the handling of unknown/extension fields. 00319 * Usually the pb_extension_type_t structure is automatically generated, 00320 * while the pb_extension_t structure is created by the user. However, 00321 * if you want to catch all unknown fields, you can also create a custom 00322 * pb_extension_type_t with your own callback. 00323 */ 00324 typedef struct pb_extension_type_s pb_extension_type_t; 00325 typedef struct pb_extension_s pb_extension_t; 00326 struct pb_extension_type_s { 00327 /* Called for each unknown field in the message. 00328 * If you handle the field, read off all of its data and return true. 00329 * If you do not handle the field, do not read anything and return true. 00330 * If you run into an error, return false. 00331 * Set to NULL for default handler. 00332 */ 00333 bool (*decode)(pb_istream_t *stream, pb_extension_t *extension, 00334 uint32_t tag, pb_wire_type_t wire_type); 00335 00336 /* Called once after all regular fields have been encoded. 00337 * If you have something to write, do so and return true. 00338 * If you do not have anything to write, just return true. 00339 * If you run into an error, return false. 00340 * Set to NULL for default handler. 00341 */ 00342 bool (*encode)(pb_ostream_t *stream, const pb_extension_t *extension); 00343 00344 /* Free field for use by the callback. */ 00345 const void *arg; 00346 }; 00347 00348 struct pb_extension_s { 00349 /* Type describing the extension field. Usually you'll initialize 00350 * this to a pointer to the automatically generated structure. */ 00351 const pb_extension_type_t *type; 00352 00353 /* Destination for the decoded data. This must match the datatype 00354 * of the extension field. */ 00355 void *dest; 00356 00357 /* Pointer to the next extension handler, or NULL. 00358 * If this extension does not match a field, the next handler is 00359 * automatically called. */ 00360 pb_extension_t *next; 00361 00362 /* The decoder sets this to true if the extension was found. 00363 * Ignored for encoding. */ 00364 bool found; 00365 }; 00366 00367 /* Memory allocation functions to use. You can define pb_realloc and 00368 * pb_free to custom functions if you want. */ 00369 #ifdef PB_ENABLE_MALLOC 00370 # ifndef pb_realloc 00371 # define pb_realloc(ptr, size) realloc(ptr, size) 00372 # endif 00373 # ifndef pb_free 00374 # define pb_free(ptr) free(ptr) 00375 # endif 00376 #endif 00377 00378 /* This is used to inform about need to regenerate .pb.h/.pb.c files. */ 00379 #define PB_PROTO_HEADER_VERSION 30 00380 00381 /* These macros are used to declare pb_field_t's in the constant array. */ 00382 /* Size of a structure member, in bytes. */ 00383 #define pb_membersize(st, m) (sizeof ((st*)0)->m) 00384 /* Number of entries in an array. */ 00385 #define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0])) 00386 /* Delta from start of one member to the start of another member. */ 00387 #define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2)) 00388 /* Marks the end of the field list */ 00389 #define PB_LAST_FIELD {0,(pb_type_t) 0,0,0,0,0,0} 00390 00391 /* Macros for filling in the data_offset field */ 00392 /* data_offset for first field in a message */ 00393 #define PB_DATAOFFSET_FIRST(st, m1, m2) (offsetof(st, m1)) 00394 /* data_offset for subsequent fields */ 00395 #define PB_DATAOFFSET_OTHER(st, m1, m2) (offsetof(st, m1) - offsetof(st, m2) - pb_membersize(st, m2)) 00396 /* Choose first/other based on m1 == m2 (deprecated, remains for backwards compatibility) */ 00397 #define PB_DATAOFFSET_CHOOSE(st, m1, m2) (int)(offsetof(st, m1) == offsetof(st, m2) \ 00398 ? PB_DATAOFFSET_FIRST(st, m1, m2) \ 00399 : PB_DATAOFFSET_OTHER(st, m1, m2)) 00400 00401 /* Required fields are the simplest. They just have delta (padding) from 00402 * previous field end, and the size of the field. Pointer is used for 00403 * submessages and default values. 00404 */ 00405 #define PB_REQUIRED_STATIC(tag, st, m, fd, ltype, ptr) \ 00406 {tag, PB_ATYPE_STATIC | PB_HTYPE_REQUIRED | ltype, \ 00407 fd, 0, pb_membersize(st, m), 0, ptr} 00408 00409 /* Optional fields add the delta to the has_ variable. */ 00410 #define PB_OPTIONAL_STATIC(tag, st, m, fd, ltype, ptr) \ 00411 {tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype, \ 00412 fd, \ 00413 pb_delta(st, has_ ## m, m), \ 00414 pb_membersize(st, m), 0, ptr} 00415 00416 /* Repeated fields have a _count field and also the maximum number of entries. */ 00417 #define PB_REPEATED_STATIC(tag, st, m, fd, ltype, ptr) \ 00418 {tag, PB_ATYPE_STATIC | PB_HTYPE_REPEATED | ltype, \ 00419 fd, \ 00420 pb_delta(st, m ## _count, m), \ 00421 pb_membersize(st, m[0]), \ 00422 pb_arraysize(st, m), ptr} 00423 00424 /* Allocated fields carry the size of the actual data, not the pointer */ 00425 #define PB_REQUIRED_POINTER(tag, st, m, fd, ltype, ptr) \ 00426 {tag, PB_ATYPE_POINTER | PB_HTYPE_REQUIRED | ltype, \ 00427 fd, 0, pb_membersize(st, m[0]), 0, ptr} 00428 00429 /* Optional fields don't need a has_ variable, as information would be redundant */ 00430 #define PB_OPTIONAL_POINTER(tag, st, m, fd, ltype, ptr) \ 00431 {tag, PB_ATYPE_POINTER | PB_HTYPE_OPTIONAL | ltype, \ 00432 fd, 0, pb_membersize(st, m[0]), 0, ptr} 00433 00434 /* Repeated fields have a _count field and a pointer to array of pointers */ 00435 #define PB_REPEATED_POINTER(tag, st, m, fd, ltype, ptr) \ 00436 {tag, PB_ATYPE_POINTER | PB_HTYPE_REPEATED | ltype, \ 00437 fd, pb_delta(st, m ## _count, m), \ 00438 pb_membersize(st, m[0]), 0, ptr} 00439 00440 /* Callbacks are much like required fields except with special datatype. */ 00441 #define PB_REQUIRED_CALLBACK(tag, st, m, fd, ltype, ptr) \ 00442 {tag, PB_ATYPE_CALLBACK | PB_HTYPE_REQUIRED | ltype, \ 00443 fd, 0, pb_membersize(st, m), 0, ptr} 00444 00445 #define PB_OPTIONAL_CALLBACK(tag, st, m, fd, ltype, ptr) \ 00446 {tag, PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype, \ 00447 fd, 0, pb_membersize(st, m), 0, ptr} 00448 00449 #define PB_REPEATED_CALLBACK(tag, st, m, fd, ltype, ptr) \ 00450 {tag, PB_ATYPE_CALLBACK | PB_HTYPE_REPEATED | ltype, \ 00451 fd, 0, pb_membersize(st, m), 0, ptr} 00452 00453 /* Optional extensions don't have the has_ field, as that would be redundant. */ 00454 #define PB_OPTEXT_STATIC(tag, st, m, fd, ltype, ptr) \ 00455 {tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype, \ 00456 0, \ 00457 0, \ 00458 pb_membersize(st, m), 0, ptr} 00459 00460 #define PB_OPTEXT_POINTER(tag, st, m, fd, ltype, ptr) \ 00461 PB_OPTIONAL_POINTER(tag, st, m, fd, ltype, ptr) 00462 00463 #define PB_OPTEXT_CALLBACK(tag, st, m, fd, ltype, ptr) \ 00464 PB_OPTIONAL_CALLBACK(tag, st, m, fd, ltype, ptr) 00465 00466 /* The mapping from protobuf types to LTYPEs is done using these macros. */ 00467 #define PB_LTYPE_MAP_BOOL PB_LTYPE_VARINT 00468 #define PB_LTYPE_MAP_BYTES PB_LTYPE_BYTES 00469 #define PB_LTYPE_MAP_DOUBLE PB_LTYPE_FIXED64 00470 #define PB_LTYPE_MAP_ENUM PB_LTYPE_VARINT 00471 #define PB_LTYPE_MAP_UENUM PB_LTYPE_UVARINT 00472 #define PB_LTYPE_MAP_FIXED32 PB_LTYPE_FIXED32 00473 #define PB_LTYPE_MAP_FIXED64 PB_LTYPE_FIXED64 00474 #define PB_LTYPE_MAP_FLOAT PB_LTYPE_FIXED32 00475 #define PB_LTYPE_MAP_INT32 PB_LTYPE_VARINT 00476 #define PB_LTYPE_MAP_INT64 PB_LTYPE_VARINT 00477 #define PB_LTYPE_MAP_MESSAGE PB_LTYPE_SUBMESSAGE 00478 #define PB_LTYPE_MAP_SFIXED32 PB_LTYPE_FIXED32 00479 #define PB_LTYPE_MAP_SFIXED64 PB_LTYPE_FIXED64 00480 #define PB_LTYPE_MAP_SINT32 PB_LTYPE_SVARINT 00481 #define PB_LTYPE_MAP_SINT64 PB_LTYPE_SVARINT 00482 #define PB_LTYPE_MAP_STRING PB_LTYPE_STRING 00483 #define PB_LTYPE_MAP_UINT32 PB_LTYPE_UVARINT 00484 #define PB_LTYPE_MAP_UINT64 PB_LTYPE_UVARINT 00485 #define PB_LTYPE_MAP_EXTENSION PB_LTYPE_EXTENSION 00486 00487 /* This is the actual macro used in field descriptions. 00488 * It takes these arguments: 00489 * - Field tag number 00490 * - Field type: BOOL, BYTES, DOUBLE, ENUM, UENUM, FIXED32, FIXED64, 00491 * FLOAT, INT32, INT64, MESSAGE, SFIXED32, SFIXED64 00492 * SINT32, SINT64, STRING, UINT32, UINT64 or EXTENSION 00493 * - Field rules: REQUIRED, OPTIONAL or REPEATED 00494 * - Allocation: STATIC or CALLBACK 00495 * - Placement: FIRST or OTHER, depending on if this is the first field in structure. 00496 * - Message name 00497 * - Field name 00498 * - Previous field name (or field name again for first field) 00499 * - Pointer to default value or submsg fields. 00500 */ 00501 00502 #define PB_FIELD(tag, type, rules, allocation, placement, message, field, prevfield, ptr) \ 00503 PB_ ## rules ## _ ## allocation(tag, message, field, \ 00504 PB_DATAOFFSET_ ## placement(message, field, prevfield), \ 00505 PB_LTYPE_MAP_ ## type, ptr) 00506 00507 /* Field description for oneof fields. This requires taking into account the 00508 * union name also, that's why a separate set of macros is needed. 00509 */ 00510 #define PB_ONEOF_STATIC(u, tag, st, m, fd, ltype, ptr) \ 00511 {tag, PB_ATYPE_STATIC | PB_HTYPE_ONEOF | ltype, \ 00512 fd, pb_delta(st, which_ ## u, u.m), \ 00513 pb_membersize(st, u.m), 0, ptr} 00514 00515 #define PB_ONEOF_POINTER(u, tag, st, m, fd, ltype, ptr) \ 00516 {tag, PB_ATYPE_POINTER | PB_HTYPE_ONEOF | ltype, \ 00517 fd, pb_delta(st, which_ ## u, u.m), \ 00518 pb_membersize(st, u.m[0]), 0, ptr} 00519 00520 #define PB_ONEOF_FIELD(union_name, tag, type, rules, allocation, placement, message, field, prevfield, ptr) \ 00521 PB_ ## rules ## _ ## allocation(union_name, tag, message, field, \ 00522 PB_DATAOFFSET_ ## placement(message, union_name.field, prevfield), \ 00523 PB_LTYPE_MAP_ ## type, ptr) 00524 00525 /* These macros are used for giving out error messages. 00526 * They are mostly a debugging aid; the main error information 00527 * is the true/false return value from functions. 00528 * Some code space can be saved by disabling the error 00529 * messages if not used. 00530 * 00531 * PB_SET_ERROR() sets the error message if none has been set yet. 00532 * msg must be a constant string literal. 00533 * PB_GET_ERROR() always returns a pointer to a string. 00534 * PB_RETURN_ERROR() sets the error and returns false from current 00535 * function. 00536 */ 00537 #ifdef PB_NO_ERRMSG 00538 #define PB_SET_ERROR(stream, msg) PB_UNUSED(stream) 00539 #define PB_GET_ERROR(stream) "(errmsg disabled)" 00540 #else 00541 #define PB_SET_ERROR(stream, msg) (stream->errmsg = (stream)->errmsg ? (stream)->errmsg : (msg)) 00542 #define PB_GET_ERROR(stream) ((stream)->errmsg ? (stream)->errmsg : "(none)") 00543 #endif 00544 00545 #define PB_RETURN_ERROR(stream, msg) return PB_SET_ERROR(stream, msg), false 00546 00547 #endif 00548
Generated on Fri Sep 23 2022 19:29:17 by
1.7.2