Add int_size option for generator.
This allows overriding the integer field types to e.g. uint8_t for saving RAM. Update issue 139 Status: FixedInGit
This commit is contained in:
@@ -44,22 +44,30 @@ except:
|
||||
import time
|
||||
import os.path
|
||||
|
||||
# Values are tuple (c type, pb type, encoded size)
|
||||
# Values are tuple (c type, pb type, encoded size, int_size_allowed)
|
||||
FieldD = descriptor.FieldDescriptorProto
|
||||
datatypes = {
|
||||
FieldD.TYPE_BOOL: ('bool', 'BOOL', 1),
|
||||
FieldD.TYPE_DOUBLE: ('double', 'DOUBLE', 8),
|
||||
FieldD.TYPE_FIXED32: ('uint32_t', 'FIXED32', 4),
|
||||
FieldD.TYPE_FIXED64: ('uint64_t', 'FIXED64', 8),
|
||||
FieldD.TYPE_FLOAT: ('float', 'FLOAT', 4),
|
||||
FieldD.TYPE_INT32: ('int32_t', 'INT32', 10),
|
||||
FieldD.TYPE_INT64: ('int64_t', 'INT64', 10),
|
||||
FieldD.TYPE_SFIXED32: ('int32_t', 'SFIXED32', 4),
|
||||
FieldD.TYPE_SFIXED64: ('int64_t', 'SFIXED64', 8),
|
||||
FieldD.TYPE_SINT32: ('int32_t', 'SINT32', 5),
|
||||
FieldD.TYPE_SINT64: ('int64_t', 'SINT64', 10),
|
||||
FieldD.TYPE_UINT32: ('uint32_t', 'UINT32', 5),
|
||||
FieldD.TYPE_UINT64: ('uint64_t', 'UINT64', 10)
|
||||
FieldD.TYPE_BOOL: ('bool', 'BOOL', 1, False),
|
||||
FieldD.TYPE_DOUBLE: ('double', 'DOUBLE', 8, False),
|
||||
FieldD.TYPE_FIXED32: ('uint32_t', 'FIXED32', 4, False),
|
||||
FieldD.TYPE_FIXED64: ('uint64_t', 'FIXED64', 8, False),
|
||||
FieldD.TYPE_FLOAT: ('float', 'FLOAT', 4, False),
|
||||
FieldD.TYPE_INT32: ('int32_t', 'INT32', 10, True),
|
||||
FieldD.TYPE_INT64: ('int64_t', 'INT64', 10, True),
|
||||
FieldD.TYPE_SFIXED32: ('int32_t', 'SFIXED32', 4, False),
|
||||
FieldD.TYPE_SFIXED64: ('int64_t', 'SFIXED64', 8, False),
|
||||
FieldD.TYPE_SINT32: ('int32_t', 'SINT32', 5, True),
|
||||
FieldD.TYPE_SINT64: ('int64_t', 'SINT64', 10, True),
|
||||
FieldD.TYPE_UINT32: ('uint32_t', 'UINT32', 5, True),
|
||||
FieldD.TYPE_UINT64: ('uint64_t', 'UINT64', 10, True)
|
||||
}
|
||||
|
||||
# Integer size overrides (from .proto settings)
|
||||
intsizes = {
|
||||
nanopb_pb2.IS_8: 'int8_t',
|
||||
nanopb_pb2.IS_16: 'int16_t',
|
||||
nanopb_pb2.IS_32: 'int32_t',
|
||||
nanopb_pb2.IS_64: 'int64_t',
|
||||
}
|
||||
|
||||
class Names:
|
||||
@@ -226,7 +234,13 @@ class Field:
|
||||
|
||||
# Decide the C data type to use in the struct.
|
||||
if datatypes.has_key(desc.type):
|
||||
self.ctype, self.pbtype, self.enc_size = datatypes[desc.type]
|
||||
self.ctype, self.pbtype, self.enc_size, isa = datatypes[desc.type]
|
||||
|
||||
# Override the field size if user wants to use smaller integers
|
||||
if isa and field_options.int_size != nanopb_pb2.IS_DEFAULT:
|
||||
self.ctype = intsizes[field_options.int_size]
|
||||
if desc.type == FieldD.TYPE_UINT32 or desc.type == FieldD.TYPE_UINT64:
|
||||
self.ctype = 'u' + self.ctype;
|
||||
elif desc.type == FieldD.TYPE_ENUM:
|
||||
self.pbtype = 'ENUM'
|
||||
self.ctype = names_from_type_name(desc.type_name)
|
||||
|
||||
@@ -18,6 +18,14 @@ enum FieldType {
|
||||
FT_IGNORE = 3; // Ignore the field completely.
|
||||
}
|
||||
|
||||
enum IntSize {
|
||||
IS_DEFAULT = 0; // Default, 32/64bit based on type in .proto
|
||||
IS_8 = 1;
|
||||
IS_16 = 2;
|
||||
IS_32 = 3;
|
||||
IS_64 = 4;
|
||||
}
|
||||
|
||||
// This is the inner options message, which basically defines options for
|
||||
// a field. When it is used in message or file scope, it applies to all
|
||||
// fields.
|
||||
@@ -28,6 +36,10 @@ message NanoPBOptions {
|
||||
// Allocated number of entries in arrays ('repeated' fields)
|
||||
optional int32 max_count = 2;
|
||||
|
||||
// Size of integer fields. Can save some memory if you don't need
|
||||
// full 32 bits for the value.
|
||||
optional IntSize int_size = 7 [default = IS_DEFAULT];
|
||||
|
||||
// Force type of field (callback or static allocation)
|
||||
optional FieldType type = 3 [default = FT_DEFAULT];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user