2424#include " absl/container/btree_set.h"
2525#include " absl/container/flat_hash_map.h"
2626#include " absl/strings/str_cat.h"
27+ #include " google/protobuf/compiler/cpp/generator.h"
2728#include " google/protobuf/compiler/cpp/helpers.h"
2829#include " google/protobuf/compiler/cpp/names.h"
2930#include " google/protobuf/descriptor.h"
@@ -49,6 +50,11 @@ absl::flat_hash_map<absl::string_view, std::string> EnumVars(
4950 enum_->containing_type () == nullptr ? " " : absl::StrCat (classname, " _" )},
5051 {" kMin" , absl::StrCat (min->number ())},
5152 {" kMax" , absl::StrCat (max->number ())},
53+ {" return_type" , CppGenerator::GetResolvedSourceFeatures (*enum_)
54+ .GetExtension (::pb::cpp)
55+ .enum_name_uses_string_view ()
56+ ? " ::absl::string_view"
57+ : " const std::string&" },
5258 };
5359}
5460
@@ -182,7 +188,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
182188 )cc" );
183189 } else {
184190 p->Emit (R"cc(
185- const std::string& $Msg_Enum$_Name($Msg_Enum$ value);
191+ $return_type$ $Msg_Enum$_Name($Msg_Enum$ value);
186192 )cc" );
187193 }
188194
@@ -204,7 +210,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
204210 if (should_cache_ || !has_reflection_) {
205211 p->Emit ({{" static_assert" , write_assert}}, R"cc(
206212 template <typename T>
207- const std::string& $Msg_Enum$_Name(T value) {
213+ $return_type$ $Msg_Enum$_Name(T value) {
208214 $static_assert$;
209215 return $Msg_Enum$_Name(static_cast<$Msg_Enum$>(value));
210216 }
@@ -216,7 +222,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
216222 // pointers, so if the enum values are sparse, it's not worth it.
217223 p->Emit (R"cc(
218224 template <>
219- inline const std::string& $Msg_Enum$_Name($Msg_Enum$ value) {
225+ inline $return_type$ $Msg_Enum$_Name($Msg_Enum$ value) {
220226 return ::$proto_ns$::internal::NameOfDenseEnum<$Msg_Enum$_descriptor,
221227 $kMin$, $kMax$>(
222228 static_cast<int>(value));
@@ -226,7 +232,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
226232 } else {
227233 p->Emit ({{" static_assert" , write_assert}}, R"cc(
228234 template <typename T>
229- const std::string& $Msg_Enum$_Name(T value) {
235+ $return_type$ $Msg_Enum$_Name(T value) {
230236 $static_assert$;
231237 return ::$proto_ns$::internal::NameOfEnum($Msg_Enum$_descriptor(), value);
232238 }
@@ -322,7 +328,7 @@ void EnumGenerator::GenerateSymbolImports(io::Printer* p) const {
322328
323329 p->Emit (R"cc(
324330 template <typename T>
325- static inline const std::string& $Enum$_Name(T value) {
331+ static inline $return_type$ $Enum$_Name(T value) {
326332 return $Msg_Enum$_Name(value);
327333 }
328334 static inline bool $Enum$_Parse(absl::string_view name, $Enum_$* value) {
@@ -514,7 +520,7 @@ void EnumGenerator::GenerateMethods(int idx, io::Printer* p) {
514520 $entries_by_number$,
515521 };
516522
517- const std::string& $Msg_Enum$_Name($Msg_Enum$ value) {
523+ $return_type$ $Msg_Enum$_Name($Msg_Enum$ value) {
518524 static const bool kDummy =
519525 ::$proto_ns$::internal::InitializeEnumStrings(
520526 $Msg_Enum$_entries, $Msg_Enum$_entries_by_number,
0 commit comments