mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-30 11:40:01 +00:00
Added min/max values for enums/unions.
Bug: 21642898 Change-Id: Ifaf0b3c4274fe30ef29507ba1c1216d700efe85b Tested: on Linux.
This commit is contained in:
@@ -15,7 +15,9 @@ struct Monster;
|
|||||||
enum Color {
|
enum Color {
|
||||||
Color_Red = 0,
|
Color_Red = 0,
|
||||||
Color_Green = 1,
|
Color_Green = 1,
|
||||||
Color_Blue = 2
|
Color_Blue = 2,
|
||||||
|
Color_MIN = Color_Red,
|
||||||
|
Color_MAX = Color_Blue
|
||||||
};
|
};
|
||||||
|
|
||||||
inline const char **EnumNamesColor() {
|
inline const char **EnumNamesColor() {
|
||||||
@@ -27,7 +29,9 @@ inline const char *EnumNameColor(Color e) { return EnumNamesColor()[static_cast<
|
|||||||
|
|
||||||
enum Any {
|
enum Any {
|
||||||
Any_NONE = 0,
|
Any_NONE = 0,
|
||||||
Any_Monster = 1
|
Any_Monster = 1,
|
||||||
|
Any_MIN = Any_NONE,
|
||||||
|
Any_MAX = Any_Monster
|
||||||
};
|
};
|
||||||
|
|
||||||
inline const char **EnumNamesAny() {
|
inline const char **EnumNamesAny() {
|
||||||
|
|||||||
@@ -131,10 +131,10 @@ static std::string GenEnumDecl(const EnumDef &enum_def,
|
|||||||
return (opts.scoped_enums ? "enum class " : "enum ") + enum_def.name;
|
return (opts.scoped_enums ? "enum class " : "enum ") + enum_def.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string GenEnumVal(const EnumDef &enum_def, const EnumVal &enum_val,
|
static std::string GenEnumVal(const EnumDef &enum_def,
|
||||||
|
const std::string &enum_val,
|
||||||
const IDLOptions &opts) {
|
const IDLOptions &opts) {
|
||||||
return opts.prefixed_enums ? enum_def.name + "_" + enum_val.name
|
return opts.prefixed_enums ? enum_def.name + "_" + enum_val : enum_val;
|
||||||
: enum_val.name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string GetEnumVal(const EnumDef &enum_def, const EnumVal &enum_val,
|
static std::string GetEnumVal(const EnumDef &enum_def, const EnumVal &enum_val,
|
||||||
@@ -159,15 +159,22 @@ static void GenEnum(const Parser &parser, EnumDef &enum_def,
|
|||||||
if (parser.opts.scoped_enums)
|
if (parser.opts.scoped_enums)
|
||||||
code += " : " + GenTypeBasic(parser, enum_def.underlying_type, false);
|
code += " : " + GenTypeBasic(parser, enum_def.underlying_type, false);
|
||||||
code += " {\n";
|
code += " {\n";
|
||||||
|
EnumVal *minv = nullptr, *maxv = nullptr;
|
||||||
for (auto it = enum_def.vals.vec.begin();
|
for (auto it = enum_def.vals.vec.begin();
|
||||||
it != enum_def.vals.vec.end();
|
it != enum_def.vals.vec.end();
|
||||||
++it) {
|
++it) {
|
||||||
auto &ev = **it;
|
auto &ev = **it;
|
||||||
GenComment(ev.doc_comment, code_ptr, nullptr, " ");
|
GenComment(ev.doc_comment, code_ptr, nullptr, " ");
|
||||||
code += " " + GenEnumVal(enum_def, ev, parser.opts) + " = ";
|
code += " " + GenEnumVal(enum_def, ev.name, parser.opts) + " = ";
|
||||||
code += NumToString(ev.value);
|
code += NumToString(ev.value) + ",\n";
|
||||||
code += (it + 1) != enum_def.vals.vec.end() ? ",\n" : "\n";
|
minv = !minv || minv->value > ev.value ? &ev : minv;
|
||||||
|
maxv = !maxv || maxv->value < ev.value ? &ev : maxv;
|
||||||
}
|
}
|
||||||
|
assert(minv && maxv);
|
||||||
|
code += " " + GenEnumVal(enum_def, "MIN", parser.opts) + " = ";
|
||||||
|
code += GenEnumVal(enum_def, minv->name, parser.opts) + ",\n";
|
||||||
|
code += " " + GenEnumVal(enum_def, "MAX", parser.opts) + " = ";
|
||||||
|
code += GenEnumVal(enum_def, maxv->name, parser.opts) + "\n";
|
||||||
code += "};\n\n";
|
code += "};\n\n";
|
||||||
|
|
||||||
// Generate a generate string table for enum values.
|
// Generate a generate string table for enum values.
|
||||||
|
|||||||
@@ -23,7 +23,9 @@ struct Monster;
|
|||||||
enum Color {
|
enum Color {
|
||||||
Color_Red = 1,
|
Color_Red = 1,
|
||||||
Color_Green = 2,
|
Color_Green = 2,
|
||||||
Color_Blue = 8
|
Color_Blue = 8,
|
||||||
|
Color_MIN = Color_Red,
|
||||||
|
Color_MAX = Color_Blue
|
||||||
};
|
};
|
||||||
|
|
||||||
inline const char **EnumNamesColor() {
|
inline const char **EnumNamesColor() {
|
||||||
@@ -36,7 +38,9 @@ inline const char *EnumNameColor(Color e) { return EnumNamesColor()[static_cast<
|
|||||||
enum Any {
|
enum Any {
|
||||||
Any_NONE = 0,
|
Any_NONE = 0,
|
||||||
Any_Monster = 1,
|
Any_Monster = 1,
|
||||||
Any_TestSimpleTableWithEnum = 2
|
Any_TestSimpleTableWithEnum = 2,
|
||||||
|
Any_MIN = Any_NONE,
|
||||||
|
Any_MAX = Any_TestSimpleTableWithEnum
|
||||||
};
|
};
|
||||||
|
|
||||||
inline const char **EnumNamesAny() {
|
inline const char **EnumNamesAny() {
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ class TableInFirstNS extends Table
|
|||||||
{
|
{
|
||||||
$obj = new TableInNestedNS();
|
$obj = new TableInNestedNS();
|
||||||
$o = $this->__offset(4);
|
$o = $this->__offset(4);
|
||||||
return $o != 0 ? $obj->init($o + $this->bb_pos, $this->bb) : 0;
|
return $o != 0 ? $obj->init($this->__indirect($o + $this->bb_pos), $this->bb) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -15,7 +15,9 @@ struct StructInNestedNS;
|
|||||||
enum EnumInNestedNS {
|
enum EnumInNestedNS {
|
||||||
EnumInNestedNS_A = 0,
|
EnumInNestedNS_A = 0,
|
||||||
EnumInNestedNS_B = 1,
|
EnumInNestedNS_B = 1,
|
||||||
EnumInNestedNS_C = 2
|
EnumInNestedNS_C = 2,
|
||||||
|
EnumInNestedNS_MIN_VAL = EnumInNestedNS_A,
|
||||||
|
EnumInNestedNS_MAX_VAL = EnumInNestedNS_C
|
||||||
};
|
};
|
||||||
|
|
||||||
inline const char **EnumNamesEnumInNestedNS() {
|
inline const char **EnumNamesEnumInNestedNS() {
|
||||||
|
|||||||
Reference in New Issue
Block a user