Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nanopb/nanopb.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjheaff1 <jheaff1@outlook.com>2023-04-21 17:01:17 +0300
committerPetteri Aimonen <jpa@github.mail.kapsi.fi>2023-06-12 09:44:42 +0300
commit19bea327c5706ef2b48c030a3937fed998d580b4 (patch)
tree89a0f0f6cb4f83d1485c74c6f21b34df2623a6c2
parent37bea38a137dda4d90433fe91ac88b671035a5e1 (diff)
Add ability to specify size of generated enums
-rw-r--r--docs/reference.md2
-rwxr-xr-xgenerator/nanopb_generator.py19
2 files changed, 18 insertions, 3 deletions
diff --git a/docs/reference.md b/docs/reference.md
index c86330e..720e679 100644
--- a/docs/reference.md
+++ b/docs/reference.md
@@ -50,7 +50,7 @@ The full set of available options is defined in [nanopb.proto](https://github.co
* `fixed_length`: Generate `bytes` fields with a constant length defined by `max_size`. A separate `.size` field will then not be generated.
* `fixed_count`: Generate arrays with constant length defined by `max_count`.
* `package`: Package name that applies only for nanopb generator. Defaults to name defined by `package` keyword in .proto file, which applies for all languages.
-* `int_size`: Override the integer type of a field. For example, specify `int_size = IS_8` to convert `int32` from protocol definition into `int8_t` in the structure.
+* `int_size`: Override the integer type of a field. For example, specify `int_size = IS_8` to convert `int32` from protocol definition into `int8_t` in the structure. When used with enum types, the size of the generated enum can be specified (C++ only)
These options can be defined for the .proto files before they are
converted using the nanopb-generator.py. There are three ways to define
diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py
index 3b79948..8dc0e80 100755
--- a/generator/nanopb_generator.py
+++ b/generator/nanopb_generator.py
@@ -99,7 +99,11 @@ datatypes = {
FieldD.TYPE_UINT32: ('uint32_t', 'UINT32', 5, 4),
FieldD.TYPE_UINT64: ('uint64_t', 'UINT64', 10, 8),
- # Integer size override options
+ # Integer size override option
+ (FieldD.TYPE_ENUM, nanopb_pb2.IS_8): ('uint8_t', 'ENUM', 4, 1),
+ (FieldD.TYPE_ENUM, nanopb_pb2.IS_16): ('uint16_t', 'ENUM', 4, 2),
+ (FieldD.TYPE_ENUM, nanopb_pb2.IS_32): ('uint32_t', 'ENUM', 4, 4),
+ (FieldD.TYPE_ENUM, nanopb_pb2.IS_64): ('uint64_t', 'ENUM', 4, 8),
(FieldD.TYPE_INT32, nanopb_pb2.IS_8): ('int8_t', 'INT32', 10, 1),
(FieldD.TYPE_INT32, nanopb_pb2.IS_16): ('int16_t', 'INT32', 10, 2),
(FieldD.TYPE_INT32, nanopb_pb2.IS_32): ('int32_t', 'INT32', 10, 4),
@@ -431,7 +435,18 @@ class Enum(ProtoElement):
if leading_comment:
result = '%s\n' % leading_comment
- result += 'typedef enum %s {' % Globals.naming_style.enum_name(self.names)
+ result += 'typedef enum %s' % Globals.naming_style.enum_name(self.names)
+
+ # Override the enum size if user wants to use smaller integers
+ if (FieldD.TYPE_ENUM, self.options.int_size) in datatypes:
+ self.ctype, self.pbtype, self.enc_size, self.data_item_size = datatypes[(FieldD.TYPE_ENUM, self.options.int_size)]
+ result += '\n#ifdef __cplusplus\n'
+ result += ' : ' + self.ctype + '\n'
+ result += '#endif\n'
+ result += '{'
+ else:
+ result += ' {'
+
if trailing_comment:
result += " " + trailing_comment