diff options
author | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2023-10-19 11:39:47 +0300 |
---|---|---|
committer | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2023-10-19 11:42:33 +0300 |
commit | 15a0ec97eccc63db6334c3c900ad1095d572a81c (patch) | |
tree | 6e954648e03ebd7e4ca958b7fea254f330c1472b | |
parent | 7d790f9ebd419d9cece7752177dba6c4f6c85646 (diff) |
Fix initializer macros for custom callback datatype (#806)
Uses NULL for pointer types and {} for other types.
User can override the type with generator option "initializer".
-rwxr-xr-x | generator/nanopb_generator.py | 18 | ||||
-rw-r--r-- | generator/proto/nanopb.proto | 4 |
2 files changed, 20 insertions, 2 deletions
diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py index 93aebbd..6ea8e84 100755 --- a/generator/nanopb_generator.py +++ b/generator/nanopb_generator.py @@ -596,6 +596,11 @@ class Field(ProtoElement): if field_options.HasField("max_size"): self.max_size = field_options.max_size + if field_options.HasField("initializer"): + self.initializer = field_options.initializer + else: + self.initializer = None + self.default_has = field_options.default_has if desc.type == FieldD.TYPE_STRING and field_options.HasField("max_length"): @@ -801,7 +806,9 @@ class Field(ProtoElement): ''' inner_init = None - if self.pbtype in ['MESSAGE', 'MSG_W_CB']: + if self.initializer is not None: + inner_init = self.initializer + elif self.pbtype in ['MESSAGE', 'MSG_W_CB']: if null_init: inner_init = Globals.naming_style.define_name('%s_init_zero' % self.ctype) else: @@ -878,8 +885,14 @@ class Field(ProtoElement): elif self.allocation == 'CALLBACK': if self.pbtype == 'EXTENSION': outer_init = 'NULL' - else: + elif self.callback_datatype == 'pb_callback_t': outer_init = '{{NULL}, NULL}' + elif self.initializer is not None: + outer_init = inner_init + elif self.callback_datatype.strip().endswith('*'): + outer_init = 'NULL' + else: + outer_init = '{}' if self.pbtype == 'MSG_W_CB' and self.rules in ['REPEATED', 'OPTIONAL']: outer_init = '{{NULL}, NULL}, ' + outer_init @@ -1070,6 +1083,7 @@ class ExtensionRange(Field): self.data_item_size = 0 self.fixed_count = False self.callback_datatype = 'pb_extension_t*' + self.initializer = None def requires_custom_field_callback(self): return False diff --git a/generator/proto/nanopb.proto b/generator/proto/nanopb.proto index 5e36eaa..660685a 100644 --- a/generator/proto/nanopb.proto +++ b/generator/proto/nanopb.proto @@ -153,6 +153,10 @@ message NanoPBOptions { // will be a a static field. // Fields with dynamic length are converted to either a pointer or a callback. optional FieldType fallback_type = 29 [default = FT_CALLBACK]; + + // Override initializer used in generated MyMessage_init_zero and MyMessage_init_default macros + // By default decided automatically based on field default value and datatype. + optional string initializer = 30; } // Extensions to protoc 'Descriptor' type in order to define options |