Generate MIN/MAX/ARRAYSIZE for enums
authorisotes <isotes@gmail.com>
Fri, 19 Feb 2016 22:51:00 +0000 (23:51 +0100)
committerisotes <isotes@gmail.com>
Fri, 19 Feb 2016 22:51:00 +0000 (23:51 +0100)
This generates #defines mirroring the following values from the generated C++ code of GPB
* const Foo Foo_MIN: the smallest valid value of the enum (VALUE_A in the example).
* const Foo Foo_MAX: the largest valid value of the enum (VALUE_C in the example).
* const Foo Foo_ARRAYSIZE: always defined as Foo_MAX + 1.

generator/nanopb_generator.py

index bc9e9da..0e9b018 100755 (executable)
@@ -197,6 +197,10 @@ class Enum:
 
         result += ' %s;' % self.names
 
+        result += '\n#define _%s_MIN %s' % (self.names, self.values[0][0])
+        result += '\n#define _%s_MAX %s' % (self.names, self.values[-1][0])
+        result += '\n#define _%s_ARRAYSIZE ((%s)(%s+1))' % (self.names, self.names, self.values[-1][0])
+
         if not self.options.long_names:
             # Define the long names always so that enum value references
             # from other files work properly.