24
24
#include " absl/container/btree_set.h"
25
25
#include " absl/container/flat_hash_map.h"
26
26
#include " absl/strings/str_cat.h"
27
+ #include " google/protobuf/compiler/cpp/generator.h"
27
28
#include " google/protobuf/compiler/cpp/helpers.h"
28
29
#include " google/protobuf/compiler/cpp/names.h"
29
30
#include " google/protobuf/descriptor.h"
@@ -49,6 +50,11 @@ absl::flat_hash_map<absl::string_view, std::string> EnumVars(
49
50
enum_->containing_type () == nullptr ? " " : absl::StrCat (classname, " _" )},
50
51
{" kMin" , absl::StrCat (min->number ())},
51
52
{" 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&" },
52
58
};
53
59
}
54
60
@@ -182,7 +188,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
182
188
)cc" );
183
189
} else {
184
190
p->Emit (R"cc(
185
- const std::string& $Msg_Enum$_Name($Msg_Enum$ value);
191
+ $return_type$ $Msg_Enum$_Name($Msg_Enum$ value);
186
192
)cc" );
187
193
}
188
194
@@ -204,7 +210,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
204
210
if (should_cache_ || !has_reflection_) {
205
211
p->Emit ({{" static_assert" , write_assert}}, R"cc(
206
212
template <typename T>
207
- const std::string& $Msg_Enum$_Name(T value) {
213
+ $return_type$ $Msg_Enum$_Name(T value) {
208
214
$static_assert$;
209
215
return $Msg_Enum$_Name(static_cast<$Msg_Enum$>(value));
210
216
}
@@ -216,7 +222,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
216
222
// pointers, so if the enum values are sparse, it's not worth it.
217
223
p->Emit (R"cc(
218
224
template <>
219
- inline const std::string& $Msg_Enum$_Name($Msg_Enum$ value) {
225
+ inline $return_type$ $Msg_Enum$_Name($Msg_Enum$ value) {
220
226
return ::$proto_ns$::internal::NameOfDenseEnum<$Msg_Enum$_descriptor,
221
227
$kMin$, $kMax$>(
222
228
static_cast<int>(value));
@@ -226,7 +232,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
226
232
} else {
227
233
p->Emit ({{" static_assert" , write_assert}}, R"cc(
228
234
template <typename T>
229
- const std::string& $Msg_Enum$_Name(T value) {
235
+ $return_type$ $Msg_Enum$_Name(T value) {
230
236
$static_assert$;
231
237
return ::$proto_ns$::internal::NameOfEnum($Msg_Enum$_descriptor(), value);
232
238
}
@@ -322,7 +328,7 @@ void EnumGenerator::GenerateSymbolImports(io::Printer* p) const {
322
328
323
329
p->Emit (R"cc(
324
330
template <typename T>
325
- static inline const std::string& $Enum$_Name(T value) {
331
+ static inline $return_type$ $Enum$_Name(T value) {
326
332
return $Msg_Enum$_Name(value);
327
333
}
328
334
static inline bool $Enum$_Parse(absl::string_view name, $Enum_$* value) {
@@ -514,7 +520,7 @@ void EnumGenerator::GenerateMethods(int idx, io::Printer* p) {
514
520
$entries_by_number$,
515
521
};
516
522
517
- const std::string& $Msg_Enum$_Name($Msg_Enum$ value) {
523
+ $return_type$ $Msg_Enum$_Name($Msg_Enum$ value) {
518
524
static const bool kDummy =
519
525
::$proto_ns$::internal::InitializeEnumStrings(
520
526
$Msg_Enum$_entries, $Msg_Enum$_entries_by_number,
0 commit comments