@@ -194,6 +194,13 @@ namespace internal {
194
194
#define PROTOBUF_INTERNAL_CHECK_CLASS_SIZE (t, expected )
195
195
#endif
196
196
197
+ // `typedef` instead of `using` for SWIG
198
+ #if defined(PROTOBUF_FUTURE_STRING_VIEW_RETURN_TYPE)
199
+ typedef absl::string_view DescriptorStringView;
200
+ #else
201
+ typedef const std::string& DescriptorStringView;
202
+ #endif
203
+
197
204
class FlatAllocator ;
198
205
199
206
class PROTOBUF_EXPORT LazyDescriptor {
@@ -286,6 +293,9 @@ PROTOBUF_EXPORT absl::string_view ShortEditionName(Edition edition);
286
293
287
294
bool IsEnumFullySequential (const EnumDescriptor* enum_desc);
288
295
296
+ const std::string& DefaultValueStringAsString (const FieldDescriptor* field);
297
+ const std::string& NameOfEnumAsString (const EnumValueDescriptor* descriptor);
298
+
289
299
} // namespace internal
290
300
291
301
// Provide an Abseil formatter for edition names.
@@ -308,14 +318,14 @@ class PROTOBUF_EXPORT Descriptor : private internal::SymbolBase {
308
318
#endif
309
319
310
320
// The name of the message type, not including its scope.
311
- const std::string& name () const ;
321
+ internal::DescriptorStringView name () const ;
312
322
313
323
// The fully-qualified name of the message type, scope delimited by
314
324
// periods. For example, message type "Foo" which is declared in package
315
325
// "bar" has full name "bar.Foo". If a type "Baz" is nested within
316
326
// Foo, Baz's full_name is "bar.Foo.Baz". To get only the part that
317
327
// comes after the last '.', use name().
318
- const std::string& full_name () const ;
328
+ internal::DescriptorStringView full_name () const ;
319
329
320
330
// Index of this descriptor within the file or containing type's message
321
331
// type array.
@@ -493,10 +503,12 @@ class PROTOBUF_EXPORT Descriptor : private internal::SymbolBase {
493
503
const ExtensionRangeOptions& options () const { return *options_; }
494
504
495
505
// Returns the name of the containing type.
496
- const std::string& name () const { return containing_type_->name (); }
506
+ internal::DescriptorStringView name () const {
507
+ return containing_type_->name ();
508
+ }
497
509
498
510
// Returns the full name of the containing type.
499
- const std::string& full_name () const {
511
+ internal::DescriptorStringView full_name () const {
500
512
return containing_type_->full_name ();
501
513
}
502
514
@@ -615,7 +627,7 @@ class PROTOBUF_EXPORT Descriptor : private internal::SymbolBase {
615
627
int reserved_name_count () const ;
616
628
617
629
// Gets a reserved name by index, where 0 <= index < reserved_name_count().
618
- const std::string& reserved_name (int index) const ;
630
+ internal::DescriptorStringView reserved_name (int index) const ;
619
631
620
632
// Returns true if the field name is reserved.
621
633
bool IsReservedName (absl::string_view name) const ;
@@ -827,9 +839,13 @@ class PROTOBUF_EXPORT FieldDescriptor : private internal::SymbolBase,
827
839
// Users may not declare fields that use reserved numbers.
828
840
static const int kLastReservedNumber = 19999 ;
829
841
830
- const std::string& name () const ; // Name of this field within the message.
831
- const std::string& full_name () const ; // Fully-qualified name of the field.
832
- const std::string& json_name () const ; // JSON name of this field.
842
+ // Name of this field within the message.
843
+ internal::DescriptorStringView name () const ;
844
+ // Fully-qualified name of the field.
845
+ internal::DescriptorStringView full_name () const ;
846
+ // JSON name of this field.
847
+ internal::DescriptorStringView json_name () const ;
848
+
833
849
const FileDescriptor* file () const ; // File in which this field was defined.
834
850
bool is_extension () const ; // Is this an extension field?
835
851
int number () const ; // Declared tag number.
@@ -840,7 +856,7 @@ class PROTOBUF_EXPORT FieldDescriptor : private internal::SymbolBase,
840
856
// field names should be lowercased anyway according to the protobuf style
841
857
// guide, so this only makes a difference when dealing with old .proto files
842
858
// which do not follow the guide.)
843
- const std::string& lowercase_name () const ;
859
+ internal::DescriptorStringView lowercase_name () const ;
844
860
845
861
// Same as name() except converted to camel-case. In this conversion, any
846
862
// time an underscore appears in the name, it is removed and the next
@@ -851,7 +867,7 @@ class PROTOBUF_EXPORT FieldDescriptor : private internal::SymbolBase,
851
867
// fooBar -> fooBar
852
868
// This (and especially the FindFieldByCamelcaseName() method) can be useful
853
869
// when parsing formats which prefer to use camel-case naming style.
854
- const std::string& camelcase_name () const ;
870
+ internal::DescriptorStringView camelcase_name () const ;
855
871
856
872
Type type () const ; // Declared type of this field.
857
873
const char * type_name () const ; // Name of the declared type.
@@ -947,7 +963,7 @@ class PROTOBUF_EXPORT FieldDescriptor : private internal::SymbolBase,
947
963
const EnumValueDescriptor* default_value_enum () const ;
948
964
// Get the field default value if cpp_type() == CPPTYPE_STRING. If no
949
965
// explicit default was defined, the default is the empty string.
950
- const std::string& default_value_string () const ;
966
+ internal::DescriptorStringView default_value_string () const ;
951
967
952
968
// The Descriptor for the message of which this is a field. For extensions,
953
969
// this is the extended type. Never nullptr.
@@ -1025,7 +1041,7 @@ class PROTOBUF_EXPORT FieldDescriptor : private internal::SymbolBase,
1025
1041
// its printable name) can be accomplished with
1026
1042
// message->file()->pool()->FindExtensionByPrintableName(message, name)
1027
1043
// where the extension extends "message".
1028
- const std::string& PrintableNameForExtension () const ;
1044
+ internal::DescriptorStringView PrintableNameForExtension () const ;
1029
1045
1030
1046
// Source Location ---------------------------------------------------
1031
1047
@@ -1043,6 +1059,8 @@ class PROTOBUF_EXPORT FieldDescriptor : private internal::SymbolBase,
1043
1059
friend class compiler ::cpp::Formatter;
1044
1060
friend class Reflection ;
1045
1061
friend class FieldDescriptorLegacy ;
1062
+ friend const std::string& internal::DefaultValueStringAsString (
1063
+ const FieldDescriptor* field);
1046
1064
1047
1065
// Returns true if this field was syntactically written with "optional" in the
1048
1066
// .proto file. Excludes singular proto3 fields that do not have a label.
@@ -1176,8 +1194,10 @@ class PROTOBUF_EXPORT OneofDescriptor : private internal::SymbolBase {
1176
1194
OneofDescriptor& operator =(const OneofDescriptor&) = delete ;
1177
1195
#endif
1178
1196
1179
- const std::string& name () const ; // Name of this oneof.
1180
- const std::string& full_name () const ; // Fully-qualified name of the oneof.
1197
+ // Name of this oneof.
1198
+ internal::DescriptorStringView name () const ;
1199
+ // Fully-qualified name of the oneof.
1200
+ internal::DescriptorStringView full_name () const ;
1181
1201
1182
1202
// Index of this oneof within the message's oneof array.
1183
1203
int index () const ;
@@ -1282,10 +1302,10 @@ class PROTOBUF_EXPORT EnumDescriptor : private internal::SymbolBase {
1282
1302
#endif
1283
1303
1284
1304
// The name of this enum type in the containing scope.
1285
- const std::string& name () const ;
1305
+ internal::DescriptorStringView name () const ;
1286
1306
1287
1307
// The fully-qualified name of the enum type, scope delimited by periods.
1288
- const std::string& full_name () const ;
1308
+ internal::DescriptorStringView full_name () const ;
1289
1309
1290
1310
// Index of this enum within the file or containing message's enum array.
1291
1311
int index () const ;
@@ -1381,7 +1401,7 @@ class PROTOBUF_EXPORT EnumDescriptor : private internal::SymbolBase {
1381
1401
int reserved_name_count () const ;
1382
1402
1383
1403
// Gets a reserved name by index, where 0 <= index < reserved_name_count().
1384
- const std::string& reserved_name (int index) const ;
1404
+ internal::DescriptorStringView reserved_name (int index) const ;
1385
1405
1386
1406
// Returns true if the field name is reserved.
1387
1407
bool IsReservedName (absl::string_view name) const ;
@@ -1494,7 +1514,7 @@ class PROTOBUF_EXPORT EnumValueDescriptor : private internal::SymbolBaseN<0>,
1494
1514
EnumValueDescriptor& operator =(const EnumValueDescriptor&) = delete ;
1495
1515
#endif
1496
1516
1497
- const std::string& name () const ; // Name of this enum constant.
1517
+ internal::DescriptorStringView name () const ; // Name of this enum constant.
1498
1518
int index () const ; // Index within the enums's Descriptor.
1499
1519
int number () const ; // Numeric value of this enum constant.
1500
1520
@@ -1503,7 +1523,7 @@ class PROTOBUF_EXPORT EnumValueDescriptor : private internal::SymbolBaseN<0>,
1503
1523
// "google.protobuf.FieldDescriptorProto.TYPE_INT32", NOT
1504
1524
// "google.protobuf.FieldDescriptorProto.Type.TYPE_INT32". This is to conform
1505
1525
// with C++ scoping rules for enums.
1506
- const std::string& full_name () const ;
1526
+ internal::DescriptorStringView full_name () const ;
1507
1527
1508
1528
// The .proto file in which this value was defined. Never nullptr.
1509
1529
const FileDescriptor* file () const ;
@@ -1545,6 +1565,8 @@ class PROTOBUF_EXPORT EnumValueDescriptor : private internal::SymbolBaseN<0>,
1545
1565
// Allows access to GetLocationPath for annotations.
1546
1566
friend class io ::Printer;
1547
1567
friend class compiler ::cpp::Formatter;
1568
+ friend const std::string& internal::NameOfEnumAsString (
1569
+ const EnumValueDescriptor* descriptor);
1548
1570
1549
1571
// Get the merged features that apply to this enum value. These are specified
1550
1572
// in the .proto file through the feature options in the message definition.
@@ -1595,9 +1617,9 @@ class PROTOBUF_EXPORT ServiceDescriptor : private internal::SymbolBase {
1595
1617
#endif
1596
1618
1597
1619
// The name of the service, not including its containing scope.
1598
- const std::string& name () const ;
1620
+ internal::DescriptorStringView name () const ;
1599
1621
// The fully-qualified name of the service, scope delimited by periods.
1600
- const std::string& full_name () const ;
1622
+ internal::DescriptorStringView full_name () const ;
1601
1623
// Index of this service within the file's services array.
1602
1624
int index () const ;
1603
1625
@@ -1699,9 +1721,9 @@ class PROTOBUF_EXPORT MethodDescriptor : private internal::SymbolBase {
1699
1721
#endif
1700
1722
1701
1723
// Name of this method, not including containing scope.
1702
- const std::string& name () const ;
1724
+ internal::DescriptorStringView name () const ;
1703
1725
// The fully-qualified name of the method, scope delimited by periods.
1704
- const std::string& full_name () const ;
1726
+ internal::DescriptorStringView full_name () const ;
1705
1727
// Index within the service's Descriptor.
1706
1728
int index () const ;
1707
1729
@@ -1807,10 +1829,10 @@ class PROTOBUF_EXPORT FileDescriptor : private internal::SymbolBase {
1807
1829
1808
1830
// The filename, relative to the source tree.
1809
1831
// e.g. "foo/bar/baz.proto"
1810
- const std::string& name () const ;
1832
+ internal::DescriptorStringView name () const ;
1811
1833
1812
1834
// The package, e.g. "google.protobuf.compiler".
1813
- const std::string& package () const ;
1835
+ internal::DescriptorStringView package () const ;
1814
1836
1815
1837
// The DescriptorPool in which this FileDescriptor and all its contents were
1816
1838
// allocated. Never nullptr.
@@ -2472,13 +2494,19 @@ class PROTOBUF_EXPORT DescriptorPool {
2472
2494
inline TYPE CLASS::FIELD () const { return FIELD##_; }
2473
2495
2474
2496
// Strings fields are stored as pointers but returned as const references.
2475
- #define PROTOBUF_DEFINE_STRING_ACCESSOR (CLASS, FIELD ) \
2476
- inline const std::string& CLASS::FIELD () const { return *FIELD##_; }
2497
+ #define PROTOBUF_DEFINE_STRING_ACCESSOR (CLASS, FIELD ) \
2498
+ inline internal::DescriptorStringView CLASS::FIELD () const { \
2499
+ return *FIELD##_; \
2500
+ }
2477
2501
2478
2502
// Name and full name are stored in a single array to save space.
2479
- #define PROTOBUF_DEFINE_NAME_ACCESSOR (CLASS ) \
2480
- inline const std::string& CLASS::name () const { return all_names_[0 ]; } \
2481
- inline const std::string& CLASS::full_name () const { return all_names_[1 ]; }
2503
+ #define PROTOBUF_DEFINE_NAME_ACCESSOR (CLASS ) \
2504
+ inline internal::DescriptorStringView CLASS::name () const { \
2505
+ return all_names_[0 ]; \
2506
+ } \
2507
+ inline internal::DescriptorStringView CLASS::full_name () const { \
2508
+ return all_names_[1 ]; \
2509
+ }
2482
2510
2483
2511
// Arrays take an index parameter, obviously.
2484
2512
#define PROTOBUF_DEFINE_ARRAY_ACCESSOR (CLASS, FIELD, TYPE ) \
@@ -2627,7 +2655,8 @@ inline bool Descriptor::IsReservedName(absl::string_view name) const {
2627
2655
2628
2656
// Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because reserved_names_ is actually
2629
2657
// an array of pointers rather than the usual array of objects.
2630
- inline const std::string& Descriptor::reserved_name (int index) const {
2658
+ inline internal::DescriptorStringView Descriptor::reserved_name (
2659
+ int index) const {
2631
2660
return *reserved_names_[index];
2632
2661
}
2633
2662
@@ -2646,19 +2675,20 @@ inline bool EnumDescriptor::IsReservedName(absl::string_view name) const {
2646
2675
2647
2676
// Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because reserved_names_ is actually
2648
2677
// an array of pointers rather than the usual array of objects.
2649
- inline const std::string& EnumDescriptor::reserved_name (int index) const {
2678
+ inline internal::DescriptorStringView EnumDescriptor::reserved_name (
2679
+ int index) const {
2650
2680
return *reserved_names_[index];
2651
2681
}
2652
2682
2653
- inline const std::string& FieldDescriptor::lowercase_name () const {
2683
+ inline internal::DescriptorStringView FieldDescriptor::lowercase_name () const {
2654
2684
return all_names_[lowercase_name_index_];
2655
2685
}
2656
2686
2657
- inline const std::string& FieldDescriptor::camelcase_name () const {
2687
+ inline internal::DescriptorStringView FieldDescriptor::camelcase_name () const {
2658
2688
return all_names_[camelcase_name_index_];
2659
2689
}
2660
2690
2661
- inline const std::string& FieldDescriptor::json_name () const {
2691
+ inline internal::DescriptorStringView FieldDescriptor::json_name () const {
2662
2692
return all_names_[json_name_index_];
2663
2693
}
2664
2694
@@ -2822,6 +2852,16 @@ inline const FileDescriptor* FileDescriptor::weak_dependency(int index) const {
2822
2852
2823
2853
namespace internal {
2824
2854
2855
+ inline const std::string& DefaultValueStringAsString (
2856
+ const FieldDescriptor* field) {
2857
+ return *field->default_value_string_ ;
2858
+ }
2859
+
2860
+ inline const std::string& NameOfEnumAsString (
2861
+ const EnumValueDescriptor* descriptor) {
2862
+ return descriptor->all_names_ [0 ];
2863
+ }
2864
+
2825
2865
inline bool IsEnumFullySequential (const EnumDescriptor* enum_desc) {
2826
2866
return enum_desc->sequential_value_limit_ == enum_desc->value_count () - 1 ;
2827
2867
}
0 commit comments