Commit b3d802d2 authored by Clement Courbet's avatar Clement Courbet

Make cpp generated enum constants constexpr when Options::proto_h is

specified.
parent 9ab11c65
...@@ -178,12 +178,13 @@ void EnumGenerator::GenerateSymbolImports(io::Printer* printer) { ...@@ -178,12 +178,13 @@ void EnumGenerator::GenerateSymbolImports(io::Printer* printer) {
map<string, string> vars; map<string, string> vars;
vars["nested_name"] = descriptor_->name(); vars["nested_name"] = descriptor_->name();
vars["classname"] = classname_; vars["classname"] = classname_;
vars["constexpr"] = options_.proto_h ? "constexpr " : "";
printer->Print(vars, "typedef $classname$ $nested_name$;\n"); printer->Print(vars, "typedef $classname$ $nested_name$;\n");
for (int j = 0; j < descriptor_->value_count(); j++) { for (int j = 0; j < descriptor_->value_count(); j++) {
vars["tag"] = EnumValueName(descriptor_->value(j)); vars["tag"] = EnumValueName(descriptor_->value(j));
printer->Print(vars, printer->Print(vars,
"static const $nested_name$ $tag$ = $classname$_$tag$;\n"); "static $constexpr$const $nested_name$ $tag$ = $classname$_$tag$;\n");
} }
printer->Print(vars, printer->Print(vars,
...@@ -237,6 +238,7 @@ void EnumGenerator::GenerateDescriptorInitializer( ...@@ -237,6 +238,7 @@ void EnumGenerator::GenerateDescriptorInitializer(
void EnumGenerator::GenerateMethods(io::Printer* printer) { void EnumGenerator::GenerateMethods(io::Printer* printer) {
map<string, string> vars; map<string, string> vars;
vars["classname"] = classname_; vars["classname"] = classname_;
vars["constexpr"] = options_.proto_h ? "constexpr " : "";
if (HasDescriptorMethods(descriptor_->file())) { if (HasDescriptorMethods(descriptor_->file())) {
printer->Print(vars, printer->Print(vars,
...@@ -287,7 +289,7 @@ void EnumGenerator::GenerateMethods(io::Printer* printer) { ...@@ -287,7 +289,7 @@ void EnumGenerator::GenerateMethods(io::Printer* printer) {
for (int i = 0; i < descriptor_->value_count(); i++) { for (int i = 0; i < descriptor_->value_count(); i++) {
vars["value"] = EnumValueName(descriptor_->value(i)); vars["value"] = EnumValueName(descriptor_->value(i));
printer->Print(vars, printer->Print(vars,
"const $classname$ $parent$::$value$;\n"); "$constexpr$const $classname$ $parent$::$value$;\n");
} }
printer->Print(vars, printer->Print(vars,
"const $classname$ $parent$::$nested_name$_MIN;\n" "const $classname$ $parent$::$nested_name$_MIN;\n"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment