summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xgenerator/nanopb_generator.py19
-rw-r--r--generator/proto/nanopb.proto3
-rw-r--r--pb.h17
3 files changed, 35 insertions, 4 deletions
diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py
index 2dad4ec1..a21257b5 100755
--- a/generator/nanopb_generator.py
+++ b/generator/nanopb_generator.py
@@ -463,7 +463,10 @@ class Field:
'''
if self.rules == 'ONEOF':
- result = ' PB_ONEOF_FIELD(%s, ' % self.union_name
+ if self.anonymous:
+ result = ' PB_ANONYMOUS_ONEOF_FIELD(%s, ' % self.union_name
+ else:
+ result = ' PB_ONEOF_FIELD(%s, ' % self.union_name
else:
result = ' PB_FIELD('
@@ -497,7 +500,10 @@ class Field:
if self.rules == 'REPEATED' and self.allocation == 'STATIC':
check.append('pb_membersize(%s, %s[0])' % (self.struct_name, self.name))
elif self.rules == 'ONEOF':
- check.append('pb_membersize(%s, %s.%s)' % (self.struct_name, self.union_name, self.name))
+ if self.anonymous:
+ check.append('pb_membersize(%s, %s)' % (self.struct_name, self.name))
+ else:
+ check.append('pb_membersize(%s, %s.%s)' % (self.struct_name, self.union_name, self.name))
else:
check.append('pb_membersize(%s, %s)' % (self.struct_name, self.name))
@@ -653,6 +659,7 @@ class OneOf(Field):
self.allocation = 'ONEOF'
self.default = None
self.rules = 'ONEOF'
+ self.anonymous = False
def add_field(self, field):
if field.allocation == 'CALLBACK':
@@ -661,6 +668,7 @@ class OneOf(Field):
field.union_name = self.name
field.rules = 'ONEOF'
+ field.anonymous = self.anonymous
self.fields.append(field)
self.fields.sort(key = lambda f: f.tag)
@@ -674,7 +682,10 @@ class OneOf(Field):
result += ' union {\n'
for f in self.fields:
result += ' ' + str(f).replace('\n', '\n ') + '\n'
- result += ' } ' + self.name + ';'
+ if self.anonymous:
+ result += ' };'
+ else:
+ result += ' } ' + self.name + ';'
return result
def types(self):
@@ -742,6 +753,8 @@ class Message:
pass # No union and skip fields also
else:
oneof = OneOf(self.name, f)
+ if oneof_options.anonymous_oneof:
+ oneof.anonymous = True
self.oneofs[i] = oneof
self.fields.append(oneof)
diff --git a/generator/proto/nanopb.proto b/generator/proto/nanopb.proto
index b8671bbd..9b2f0fbc 100644
--- a/generator/proto/nanopb.proto
+++ b/generator/proto/nanopb.proto
@@ -62,6 +62,9 @@ message NanoPBOptions {
// integer type tag for a message
optional uint32 msgid = 9;
+
+ // decode oneof as anonymous union
+ optional bool anonymous_oneof = 11 [default = false];
}
// Extensions to protoc 'Descriptor' type in order to define options
diff --git a/pb.h b/pb.h
index 98613a06..0286b855 100644
--- a/pb.h
+++ b/pb.h
@@ -518,10 +518,25 @@ struct pb_extension_s {
pb_membersize(st, u.m[0]), 0, ptr}
#define PB_ONEOF_FIELD(union_name, tag, type, rules, allocation, placement, message, field, prevfield, ptr) \
- PB_ ## rules ## _ ## allocation(union_name, tag, message, field, \
+ PB_ ## ONEOF_ ## allocation(union_name, tag, message, field, \
PB_DATAOFFSET_ ## placement(message, union_name.field, prevfield), \
PB_LTYPE_MAP_ ## type, ptr)
+#define PB_ANONYMOUS_ONEOF_STATIC(u, tag, st, m, fd, ltype, ptr) \
+ {tag, PB_ATYPE_STATIC | PB_HTYPE_ONEOF | ltype, \
+ fd, pb_delta(st, which_ ## u, m), \
+ pb_membersize(st, m), 0, ptr}
+
+#define PB_ANONYMOUS_ONEOF_POINTER(u, tag, st, m, fd, ltype, ptr) \
+ {tag, PB_ATYPE_POINTER | PB_HTYPE_ONEOF | ltype, \
+ fd, pb_delta(st, which_ ## u, m), \
+ pb_membersize(st, m[0]), 0, ptr}
+
+#define PB_ANONYMOUS_ONEOF_FIELD(union_name, tag, type, rules, allocation, placement, message, field, prevfield, ptr) \
+ PB_ ## ANONYMOUS_ONEOF_ ## allocation(union_name, tag, message, field, \
+ PB_DATAOFFSET_ ## placement(message, field, prevfield), \
+ PB_LTYPE_MAP_ ## type, ptr)
+
/* These macros are used for giving out error messages.
* They are mostly a debugging aid; the main error information
* is the true/false return value from functions.