descriptor.h

This section contains reference documentation for working with protocol buffer classes in C++.

#include <google/protobuf/descriptor.h>
namespace google::protobuf

This file contains classes which describe a type of protocol message.

You can use a message's descriptor to learn at runtime what fields it contains and what the types of those fields are. The Message interface also allows you to dynamically access and modify individual fields by passing the FieldDescriptor of the field you are interested in.

Most users will not care about descriptors, because they will write code specific to certain protocol types and will simply use the classes generated by the protocol compiler directly. Advanced users who want to operate on arbitrary types (not known at compile time) may want to read descriptors in order to learn about the contents of a message. A very small number of users will want to construct their own Descriptors, either because they are implementing Message manually or because they are writing something like the protocol compiler.

For an example of how you might use descriptors, see the code example at the top of message.h.

Classes in this file

NB, all indices are zero-based.
Options when generating machine-parsable output from a descriptor with DebugString().
Describes a type of protocol message, or a particular group within a message.
A range of field numbers which are designated for third-party extensions.
A range of reserved field numbers.
Describes a single field of a message.
Describes a oneof defined in a message type.
Describes an enum type defined in a .proto file.
A range of reserved field numbers.
Describes an individual enum constant of a particular type.
Describes an RPC service.
Describes an individual service method.
Describes a whole .proto file.
Used to construct descriptors.
When converting a FileDescriptorProto to a FileDescriptor, various errors might be detected in the input.

struct SourceLocation

#include <google/protobuf/descriptor.h>
namespace google::protobuf

NB, all indices are zero-based.

Members

int
start_line
int
end_line
int
start_column
int
end_column
std::string
leading_comments
Doc comments found at the source location. more...
std::string
trailing_comments
std::vector< std::string >
leading_detached_comments

std::string SourceLocation::leading_comments

Doc comments found at the source location.

See the comments in SourceCodeInfo.Location (descriptor.proto) for details.

struct DebugStringOptions

#include <google/protobuf/descriptor.h>
namespace google::protobuf

Options when generating machine-parsable output from a descriptor with DebugString().

Members

bool
include_comments
include original user comments as recorded in SourceLocation entries. more...
bool
elide_group_body
If true, elide the braced body in the debug string.
bool
elide_oneof_body
DebugStringOptions()

bool DebugStringOptions::include_comments

include original user comments as recorded in SourceLocation entries.

N.B. that this must be |false| by default: several other pieces of code (for example, the C++ code generation for fields in the proto compiler) rely on DebugString() output being unobstructed by user comments.

class Descriptor

#include <google/protobuf/descriptor.h>
namespace google::protobuf

Describes a type of protocol message, or a particular group within a message.

To obtain the Descriptor for a given message object, call Message::GetDescriptor(). Generated message classes also have a static method called descriptor() which returns the type's descriptor. Use DescriptorPool to construct your own descriptors.

Members

enum
WellKnownType
typedef
DescriptorProto Proto
const std::string &
name() const
The name of the message type, not including its scope.
const std::string &
full_name() const
The fully-qualified name of the message type, scope delimited by periods. more...
int
index() const
Index of this descriptor within the file or containing type's message type array.
const FileDescriptor *
file() const
The .proto file in which this message type was defined. Never nullptr.
const Descriptor *
containing_type() const
If this Descriptor describes a nested type, this returns the type in which it is nested. more...
const MessageOptions &
options() const
Get options for this message type. more...
void
CopyTo(DescriptorProto * proto) const
Write the contents of this Descriptor into the given DescriptorProto. more...
std::string
DebugString() const
Write the contents of this descriptor in a human-readable form. more...
std::string
DebugStringWithOptions(const DebugStringOptions & options) const
Similar to DebugString(), but additionally takes options (e.g., include original user comments in output).
bool
is_placeholder() const
Returns true if this is a placeholder for an unknown type. more...
WellKnownType
well_known_type() const

Field stuff

int
field_count() const
The number of fields in this message type.
const FieldDescriptor *
field(int index) const
Gets a field by index, where 0 <= index < field_count(). more...
const FieldDescriptor *
FindFieldByNumber(int number) const
Looks up a field by declared tag number. more...
const FieldDescriptor *
FindFieldByName(ConstStringParam name) const
Looks up a field by name. Returns nullptr if no such field exists.
const FieldDescriptor *
FindFieldByLowercaseName(ConstStringParam lowercase_name) const
Looks up a field by lowercased name (as returned by lowercase_name()). more...
const FieldDescriptor *
FindFieldByCamelcaseName(ConstStringParam camelcase_name) const
Looks up a field by camel-case name (as returned by camelcase_name()). more...
int
oneof_decl_count() const
The number of oneofs in this message type.
int
real_oneof_decl_count() const
The number of oneofs in this message type, excluding synthetic oneofs. more...
const OneofDescriptor *
oneof_decl(int index) const
Get a oneof by index, where 0 <= index < oneof_decl_count(). more...
const OneofDescriptor *
FindOneofByName(ConstStringParam name) const
Looks up a oneof by name. Returns nullptr if no such oneof exists.

Nested type stuff

int
nested_type_count() const
The number of nested types in this message type.
const Descriptor *
nested_type(int index) const
Gets a nested type by index, where 0 <= index < nested_type_count(). more...
const Descriptor *
FindNestedTypeByName(ConstStringParam name) const
Looks up a nested type by name. more...

Enum stuff

int
enum_type_count() const
The number of enum types in this message type.
const EnumDescriptor *
enum_type(int index) const
Gets an enum type by index, where 0 <= index < enum_type_count(). more...
const EnumDescriptor *
FindEnumTypeByName(ConstStringParam name) const
Looks up an enum type by name. more...
const EnumValueDescriptor *
FindEnumValueByName(ConstStringParam name) const
Looks up an enum value by name, among all enum types in this message. more...

Extensions

int
extension_range_count() const
The number of extension ranges in this message type.
const ExtensionRange *
extension_range(int index) const
Gets an extension range by index, where 0 <= index < extension_range_count(). more...
bool
IsExtensionNumber(int number) const
Returns true if the number is in one of the extension ranges.
const ExtensionRange *
FindExtensionRangeContainingNumber(int number) const
Returns nullptr if no extension range contains the given number.
int
extension_count() const
The number of extensions defined nested within this message type's scope. more...
const FieldDescriptor *
extension(int index) const
Get an extension by index, where 0 <= index < extension_count(). more...
const FieldDescriptor *
FindExtensionByName(ConstStringParam name) const
Looks up a named extension (which extends some other message type) defined within this message type's scope.
const FieldDescriptor *
FindExtensionByLowercaseName(ConstStringParam name) const
Similar to FindFieldByLowercaseName(), but finds extensions defined within this message type's scope.
const FieldDescriptor *
FindExtensionByCamelcaseName(ConstStringParam name) const
Similar to FindFieldByCamelcaseName(), but finds extensions defined within this message type's scope.

Reserved fields

int
reserved_range_count() const
The number of reserved ranges in this message type.
const ReservedRange *
reserved_range(int index) const
Gets an reserved range by index, where 0 <= index < reserved_range_count(). more...
bool
IsReservedNumber(int number) const
Returns true if the number is in one of the reserved ranges.
const ReservedRange *
FindReservedRangeContainingNumber(int number) const
Returns nullptr if no reserved range contains the given number.
int
reserved_name_count() const
The number of reserved field names in this message type.
const std::string &
reserved_name(int index) const
Gets a reserved name by index, where 0 <= index < reserved_name_count(). more...
bool
IsReservedName(ConstStringParam name) const
Returns true if the field name is reserved.

Source Location

bool
GetSourceLocation(SourceLocation * out_location) const
Updates |*out_location| to the source location of the complete extent of this message declaration. more...

Maps

const FieldDescriptor *
map_key() const
Returns the FieldDescriptor for the "key" field. more...
const FieldDescriptor *
map_value() const
Returns the FieldDescriptor for the "value" field. more...

enum Descriptor::WellKnownType {
  WELLKNOWNTYPE_UNSPECIFIED,
  WELLKNOWNTYPE_DOUBLEVALUE,
  WELLKNOWNTYPE_FLOATVALUE,
  WELLKNOWNTYPE_INT64VALUE,
  WELLKNOWNTYPE_UINT64VALUE,
  WELLKNOWNTYPE_INT32VALUE,
  WELLKNOWNTYPE_UINT32VALUE,
  WELLKNOWNTYPE_STRINGVALUE,
  WELLKNOWNTYPE_BYTESVALUE,
  WELLKNOWNTYPE_BOOLVALUE,
  WELLKNOWNTYPE_ANY,
  WELLKNOWNTYPE_FIELDMASK,
  WELLKNOWNTYPE_DURATION,
  WELLKNOWNTYPE_TIMESTAMP,
  WELLKNOWNTYPE_VALUE,
  WELLKNOWNTYPE_LISTVALUE,
  WELLKNOWNTYPE_STRUCT,
  **WELLKNOWNTYPE**DO_NOT_USE__ADD_DEFAULT_INSTEAD__
}

WELLKNOWNTYPE_UNSPECIFIEDNot a well-known type.
WELLKNOWNTYPE_DOUBLEVALUE

Wrapper types.

google.protobuf.DoubleValue

WELLKNOWNTYPE_FLOATVALUEgoogle.protobuf.FloatValue
WELLKNOWNTYPE_INT64VALUEgoogle.protobuf.Int64Value
WELLKNOWNTYPE_UINT64VALUEgoogle.protobuf.UInt64Value
WELLKNOWNTYPE_INT32VALUEgoogle.protobuf.Int32Value
WELLKNOWNTYPE_UINT32VALUEgoogle.protobuf.UInt32Value
WELLKNOWNTYPE_STRINGVALUEgoogle.protobuf.StringValue
WELLKNOWNTYPE_BYTESVALUEgoogle.protobuf.BytesValue
WELLKNOWNTYPE_BOOLVALUEgoogle.protobuf.BoolValue
WELLKNOWNTYPE_ANY

Other well known types.

google.protobuf.Any

WELLKNOWNTYPE_FIELDMASKgoogle.protobuf.FieldMask
WELLKNOWNTYPE_DURATIONgoogle.protobuf.Duration
WELLKNOWNTYPE_TIMESTAMPgoogle.protobuf.Timestamp
WELLKNOWNTYPE_VALUEgoogle.protobuf.Value
WELLKNOWNTYPE_LISTVALUEgoogle.protobuf.ListValue
WELLKNOWNTYPE_STRUCTgoogle.protobuf.Struct
**WELLKNOWNTYPE**DO_NOT_USE__ADD_DEFAULT_INSTEAD__

New well-known types may be added in the future.

Please make sure any switch() statements have a 'default' case.


const std::string &
    Descriptor::full_name() const

The fully-qualified name of the message type, scope delimited by periods.

For example, message type "Foo" which is declared in package "bar" has full name "bar.Foo". If a type "Baz" is nested within Foo, Baz's full_name is "bar.Foo.Baz". To get only the part that comes after the last '.', use name().


const Descriptor *
    Descriptor::containing_type() const

If this Descriptor describes a nested type, this returns the type in which it is nested.

Otherwise, returns nullptr.


const MessageOptions &
    Descriptor::options() const

Get options for this message type.

These are specified in the .proto file by placing lines like "option foo = 1234;" in the message definition. Allowed options are defined by MessageOptions in descriptor.proto, and any available extensions of that message.


void Descriptor::CopyTo(
        DescriptorProto * proto) const

Write the contents of this Descriptor into the given DescriptorProto.

The target DescriptorProto must be clear before calling this; if it isn't, the result may be garbage.


std::string Descriptor::DebugString() const

Write the contents of this descriptor in a human-readable form.

Output will be suitable for re-parsing.


bool Descriptor::is_placeholder() const

Returns true if this is a placeholder for an unknown type.

This will only be the case if this descriptor comes from a DescriptorPool with AllowUnknownDependencies() set.


const FieldDescriptor *
    Descriptor::field(
        int index) const

Gets a field by index, where 0 <= index < field_count().

These are returned in the order they were defined in the .proto file.


const FieldDescriptor *
    Descriptor::FindFieldByNumber(
        int number) const

Looks up a field by declared tag number.

Returns nullptr if no such field exists.


const FieldDescriptor *
    Descriptor::FindFieldByLowercaseName(
        ConstStringParam lowercase_name) const

Looks up a field by lowercased name (as returned by lowercase_name()).

This lookup may be ambiguous if multiple field names differ only by case, in which case the field returned is chosen arbitrarily from the matches.


const FieldDescriptor *
    Descriptor::FindFieldByCamelcaseName(
        ConstStringParam camelcase_name) const

Looks up a field by camel-case name (as returned by camelcase_name()).

This lookup may be ambiguous if multiple field names differ in a way that leads them to have identical camel-case names, in which case the field returned is chosen arbitrarily from the matches.


int Descriptor::real_oneof_decl_count() const

The number of oneofs in this message type, excluding synthetic oneofs.

Real oneofs always come first, so iterating up to real_oneof_decl_cout() will yield all real oneofs.


const OneofDescriptor *
    Descriptor::oneof_decl(
        int index) const

Get a oneof by index, where 0 <= index < oneof_decl_count().

These are returned in the order they were defined in the .proto file.


const Descriptor *
    Descriptor::nested_type(
        int index) const

Gets a nested type by index, where 0 <= index < nested_type_count().

These are returned in the order they were defined in the .proto file.


const Descriptor *
    Descriptor::FindNestedTypeByName(
        ConstStringParam name) const

Looks up a nested type by name.

Returns nullptr if no such nested type exists.


const EnumDescriptor *
    Descriptor::enum_type(
        int index) const

Gets an enum type by index, where 0 <= index < enum_type_count().

These are returned in the order they were defined in the .proto file.


const EnumDescriptor *
    Descriptor::FindEnumTypeByName(
        ConstStringParam name) const

Looks up an enum type by name.

Returns nullptr if no such enum type exists.


const EnumValueDescriptor *
    Descriptor::FindEnumValueByName(
        ConstStringParam name) const

Looks up an enum value by name, among all enum types in this message.

Returns nullptr if no such value exists.


const ExtensionRange *
    Descriptor::extension_range(
        int index) const

Gets an extension range by index, where 0 <= index < extension_range_count().

These are returned in the order they were defined in the .proto file.


int Descriptor::extension_count() const


const FieldDescriptor *
    Descriptor::extension(
        int index) const

Get an extension by index, where 0 <= index < extension_count().

These are returned in the order they were defined in the .proto file.


const ReservedRange *
    Descriptor::reserved_range(
        int index) const

Gets an reserved range by index, where 0 <= index < reserved_range_count().

These are returned in the order they were defined in the .proto file.


const std::string &
    Descriptor::reserved_name(
        int index) const

Gets a reserved name by index, where 0 <= index < reserved_name_count().

Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because reserved_names_ is actually an array of pointers rather than the usual array of objects.


bool Descriptor::GetSourceLocation(
        SourceLocation * out_location) const

Updates |*out_location| to the source location of the complete extent of this message declaration.

Returns false and leaves |*out_location| unchanged iff location information was not available.


const FieldDescriptor *
    Descriptor::map_key() const

Returns the FieldDescriptor for the "key" field.

If this isn't a map entry field, returns nullptr.


const FieldDescriptor *
    Descriptor::map_value() const

Returns the FieldDescriptor for the "value" field.

If this isn't a map entry field, returns nullptr.

struct Descriptor::ExtensionRange

#include <google/protobuf/descriptor.h>
namespace google::protobuf

A range of field numbers which are designated for third-party extensions.

Members

typedef
DescriptorProto_ExtensionRange Proto
typedef
ExtensionRangeOptions OptionsType
int
start
inclusive
int
end
exclusive
const ExtensionRangeOptions *
options_
void
CopyTo(DescriptorProto_ExtensionRange * proto) const

struct Descriptor::ReservedRange

#include <google/protobuf/descriptor.h>
namespace google::protobuf

A range of reserved field numbers.

Members

int
start
inclusive
int
end
exclusive

class FieldDescriptor

#include <google/protobuf/descriptor.h>
namespace google::protobuf

Describes a single field of a message.

To get the descriptor for a given field, first get the Descriptor for the message in which it is defined, then call Descriptor::FindFieldByName(). To get a FieldDescriptor for an extension, do one of the following:

Members

enum
Type
Identifies a field type. more...
enum
CppType
Specifies the C++ data type used to represent the field. more...
enum
Label
Identifies whether the field is optional, required, or repeated. more...
typedef
FieldDescriptorProto Proto
const int
kMaxNumber = = (1 << 29) - 1
Valid field numbers are positive integers up to kMaxNumber.
const int
kFirstReservedNumber = = 19000
First field number reserved for the protocol buffer library implementation. more...
const int
kLastReservedNumber = = 19999
Last field number reserved for the protocol buffer library implementation. more...
int32
default_value_int32_
int64
default_value_int64_
uint32
default_value_uint32_
uint64
default_value_uint64_
float
default_value_float_
double
default_value_double_
bool
default_value_bool_
const EnumValueDescriptor *
default_value_enum_
const std::string *
default_value_string_
std::atomic< const Message * >
default_generated_instance_
const std::string &
name() const
Name of this field within the message.
const std::string &
full_name() const
Fully-qualified name of the field.
const std::string &
json_name() const
JSON name of this field.
const FileDescriptor *
file() const
File in which this field was defined.
bool
is_extension() const
Is this an extension field?
int
number() const
Declared tag number.
const std::string &
lowercase_name() const
Same as name() except converted to lower-case. more...
const std::string &
camelcase_name() const
Same as name() except converted to camel-case. more...
Type
type() const
Declared type of this field.
const char *
type_name() const
Name of the declared type.
CppType
cpp_type() const
C++ type of this field.
const char *
cpp_type_name() const
Name of the C++ type.
Label
label() const
optional/required/repeated
bool
is_required() const
shorthand for label() == LABEL_REQUIRED
bool
is_optional() const
shorthand for label() == LABEL_OPTIONAL
bool
is_repeated() const
shorthand for label() == LABEL_REPEATED
bool
is_packable() const
shorthand for is_repeated() && IsTypePackable(type())
bool
is_packed() const
shorthand for is_packable() && options().packed()
bool
is_map() const
shorthand for type() == TYPE_MESSAGE && message_type()->options().map_entry()
bool
has_optional_keyword() const
Returns true if this field was syntactically written with "optional" in the .proto file. more...
bool
has_presence() const
Returns true if this field tracks presence, ie. more...
int
index() const
Index of this field within the message's field array, or the file or extension scope's extensions array.
bool
has_default_value() const
Does this field have an explicitly-declared default value?
bool
has_json_name() const
Whether the user has specified the json_name field option in the .proto file.
int32
default_value_int32() const
Get the field default value if cpp_type() == CPPTYPE_INT32. more...
int64
default_value_int64() const
Get the field default value if cpp_type() == CPPTYPE_INT64. more...
uint32
default_value_uint32() const
Get the field default value if cpp_type() == CPPTYPE_UINT32. more...
uint64
default_value_uint64() const
Get the field default value if cpp_type() == CPPTYPE_UINT64. more...
float
default_value_float() const
Get the field default value if cpp_type() == CPPTYPE_FLOAT. more...
double
default_value_double() const
Get the field default value if cpp_type() == CPPTYPE_DOUBLE. more...
bool
default_value_bool() const
Get the field default value if cpp_type() == CPPTYPE_BOOL. more...
const EnumValueDescriptor *
default_value_enum() const
Get the field default value if cpp_type() == CPPTYPE_ENUM. more...
const std::string &
default_value_string() const
Get the field default value if cpp_type() == CPPTYPE_STRING. more...
const Descriptor *
containing_type() const
The Descriptor for the message of which this is a field. more...
const OneofDescriptor *
containing_oneof() const
If the field is a member of a oneof, this is the one, otherwise this is nullptr.
const OneofDescriptor *
real_containing_oneof() const
If the field is a member of a non-synthetic oneof, returns the descriptor for the oneof, otherwise returns nullptr.
int
index_in_oneof() const
If the field is a member of a oneof, returns the index in that oneof.
const Descriptor *
extension_scope() const
An extension may be declared within the scope of another message. more...
const Descriptor *
message_type() const
If type is TYPE_MESSAGE or TYPE_GROUP, returns a descriptor for the message or the group type. more...
const EnumDescriptor *
enum_type() const
If type is TYPE_ENUM, returns a descriptor for the enum. more...
const FieldOptions &
options() const
Get the FieldOptions for this field. more...
void
CopyTo(FieldDescriptorProto * proto) const
std::string
DebugString() const
std::string
DebugStringWithOptions(const DebugStringOptions & options) const
const std::string &
PrintableNameForExtension() const
Returns full_name() except if the field is a MessageSet extension, in which case it returns the full_name() of the containing message type for backwards compatibility with proto1. more...
static CppType
TypeToCppType(Type type)
Helper method to get the CppType for a particular Type.
static const char *
TypeName(Type type)
Helper method to get the name of a Type.
static const char *
CppTypeName(CppType cpp_type)
Helper method to get the name of a CppType.
static bool
IsTypePackable(Type field_type)
Return true iff [[]packed = true] is valid for fields of this type.

Source Location

bool
GetSourceLocation(SourceLocation * out_location) const
Updates |*out_location| to the source location of the complete extent of this field declaration. more...

enum FieldDescriptor::Type {
  TYPE_DOUBLE = = 1,
  TYPE_FLOAT = = 2,
  TYPE_INT64 = = 3,
  TYPE_UINT64 = = 4,
  TYPE_INT32 = = 5,
  TYPE_FIXED64 = = 6,
  TYPE_FIXED32 = = 7,
  TYPE_BOOL = = 8,
  TYPE_STRING = = 9,
  TYPE_GROUP = = 10,
  TYPE_MESSAGE = = 11,
  TYPE_BYTES = = 12,
  TYPE_UINT32 = = 13,
  TYPE_ENUM = = 14,
  TYPE_SFIXED32 = = 15,
  TYPE_SFIXED64 = = 16,
  TYPE_SINT32 = = 17,
  TYPE_SINT64 = = 18,
  MAX_TYPE = = 18
}

Identifies a field type.

0 is reserved for errors. The order is weird for historical reasons. Types 12 and up are new in proto2.

TYPE_DOUBLEdouble, exactly eight bytes on the wire.
TYPE_FLOATfloat, exactly four bytes on the wire.
TYPE_INT64

int64, varint on the wire.

Negative numbers take 10 bytes. Use TYPE_SINT64 if negative values are likely.

TYPE_UINT64uint64, varint on the wire.
TYPE_INT32

int32, varint on the wire.

Negative numbers take 10 bytes. Use TYPE_SINT32 if negative values are likely.

TYPE_FIXED64uint64, exactly eight bytes on the wire.
TYPE_FIXED32uint32, exactly four bytes on the wire.
TYPE_BOOLbool, varint on the wire.
TYPE_STRINGUTF-8 text.
TYPE_GROUPTag-delimited message. Deprecated.
TYPE_MESSAGELength-delimited message.
TYPE_BYTESArbitrary byte array.
TYPE_UINT32uint32, varint on the wire
TYPE_ENUMEnum, varint on the wire.
TYPE_SFIXED32int32, exactly four bytes on the wire
TYPE_SFIXED64int64, exactly eight bytes on the wire
TYPE_SINT32int32, ZigZag-encoded varint on the wire
TYPE_SINT64int64, ZigZag-encoded varint on the wire
MAX_TYPEConstant useful for defining lookup tables indexed by Type.

enum FieldDescriptor::CppType {
  CPPTYPE_INT32 = = 1,
  CPPTYPE_INT64 = = 2,
  CPPTYPE_UINT32 = = 3,
  CPPTYPE_UINT64 = = 4,
  CPPTYPE_DOUBLE = = 5,
  CPPTYPE_FLOAT = = 6,
  CPPTYPE_BOOL = = 7,
  CPPTYPE_ENUM = = 8,
  CPPTYPE_STRING = = 9,
  CPPTYPE_MESSAGE = = 10,
  MAX_CPPTYPE = = 10
}

Specifies the C++ data type used to represent the field.

There is a fixed mapping from Type to CppType where each Type maps to exactly one CppType. 0 is reserved for errors.

CPPTYPE_INT32TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32.
CPPTYPE_INT64TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64.
CPPTYPE_UINT32TYPE_UINT32, TYPE_FIXED32.
CPPTYPE_UINT64TYPE_UINT64, TYPE_FIXED64.
CPPTYPE_DOUBLETYPE_DOUBLE.
CPPTYPE_FLOATTYPE_FLOAT.
CPPTYPE_BOOLTYPE_BOOL.
CPPTYPE_ENUMTYPE_ENUM.
CPPTYPE_STRINGTYPE_STRING, TYPE_BYTES.
CPPTYPE_MESSAGETYPE_MESSAGE, TYPE_GROUP.
MAX_CPPTYPEConstant useful for defining lookup tables indexed by CppType.

enum FieldDescriptor::Label {
  LABEL_OPTIONAL = = 1,
  LABEL_REQUIRED = = 2,
  LABEL_REPEATED = = 3,
  MAX_LABEL = = 3
}

Identifies whether the field is optional, required, or repeated.

0 is reserved for errors.

LABEL_OPTIONALoptional
LABEL_REQUIREDrequired
LABEL_REPEATEDrepeated
MAX_LABELConstant useful for defining lookup tables indexed by Label.

const int FieldDescriptor::kFirstReservedNumber = = 19000

First field number reserved for the protocol buffer library implementation.

Users may not declare fields that use reserved numbers.


const int FieldDescriptor::kLastReservedNumber = = 19999

Last field number reserved for the protocol buffer library implementation.

Users may not declare fields that use reserved numbers.


const std::string &
    FieldDescriptor::lowercase_name() const

Same as name() except converted to lower-case.

This (and especially the FindFieldByLowercaseName() method) can be useful when parsing formats which prefer to use lowercase naming style. (Although, technically field names should be lowercased anyway according to the protobuf style guide, so this only makes a difference when dealing with old .proto files which do not follow the guide.)


const std::string &
    FieldDescriptor::camelcase_name() const

Same as name() except converted to camel-case.

In this conversion, any time an underscore appears in the name, it is removed and the next letter is capitalized. Furthermore, the first letter of the name is lower-cased. Examples:

FooBar -> fooBar
foo_bar -> fooBar
fooBar -> fooBar

This (and especially the FindFieldByCamelcaseName() method) can be useful when parsing formats which prefer to use camel-case naming style.


bool FieldDescriptor::has_optional_keyword() const

Returns true if this field was syntactically written with "optional" in the .proto file.

Excludes singular proto3 fields that do not have a label.


bool FieldDescriptor::has_presence() const

Returns true if this field tracks presence, ie.

does the field distinguish between "unset" and "present with default value." This includes required, optional, and oneof fields. It excludes maps, repeated fields, and singular proto3 fields without "optional".

For fields where has_presence() == true, the return value of Reflection::HasField() is semantically meaningful.


int32 FieldDescriptor::default_value_int32() const

Get the field default value if cpp_type() == CPPTYPE_INT32.

If no explicit default was defined, the default is 0.


int64 FieldDescriptor::default_value_int64() const

Get the field default value if cpp_type() == CPPTYPE_INT64.

If no explicit default was defined, the default is 0.


uint32 FieldDescriptor::default_value_uint32() const

Get the field default value if cpp_type() == CPPTYPE_UINT32.

If no explicit default was defined, the default is 0.


uint64 FieldDescriptor::default_value_uint64() const

Get the field default value if cpp_type() == CPPTYPE_UINT64.

If no explicit default was defined, the default is 0.


float FieldDescriptor::default_value_float() const

Get the field default value if cpp_type() == CPPTYPE_FLOAT.

If no explicit default was defined, the default is 0.0.


double FieldDescriptor::default_value_double() const

Get the field default value if cpp_type() == CPPTYPE_DOUBLE.

If no explicit default was defined, the default is 0.0.


bool FieldDescriptor::default_value_bool() const

Get the field default value if cpp_type() == CPPTYPE_BOOL.

If no explicit default was defined, the default is false.


const EnumValueDescriptor *
    FieldDescriptor::default_value_enum() const

Get the field default value if cpp_type() == CPPTYPE_ENUM.

If no explicit default was defined, the default is the first value defined in the enum type (all enum types are required to have at least one value). This never returns nullptr.


const std::string &
    FieldDescriptor::default_value_string() const

Get the field default value if cpp_type() == CPPTYPE_STRING.

If no explicit default was defined, the default is the empty string.


const Descriptor *
    FieldDescriptor::containing_type() const

The Descriptor for the message of which this is a field.

For extensions, this is the extended type. Never nullptr.


const Descriptor *
    FieldDescriptor::extension_scope() const

An extension may be declared within the scope of another message.

If this field is an extension (is_extension() is true), then extension_scope() returns that message, or nullptr if the extension was declared at global scope. If this is not an extension, extension_scope() is undefined (may assert-fail).


const Descriptor *
    FieldDescriptor::message_type() const

If type is TYPE_MESSAGE or TYPE_GROUP, returns a descriptor for the message or the group type.

Otherwise, returns null.


const EnumDescriptor *
    FieldDescriptor::enum_type() const

If type is TYPE_ENUM, returns a descriptor for the enum.

Otherwise, returns null.


const FieldOptions &
    FieldDescriptor::options() const

Get the FieldOptions for this field.

This includes things listed in square brackets after the field definition. E.g., the field:

optional string text = 1 [[]ctype=CORD];

has the "ctype" option set. Allowed options are defined by FieldOptions in descriptor.proto, and any available extensions of that message.


const std::string &
    FieldDescriptor::PrintableNameForExtension() const

Returns full_name() except if the field is a MessageSet extension, in which case it returns the full_name() of the containing message type for backwards compatibility with proto1.

A MessageSet extension is defined as an optional message extension whose containing type has the message_set_wire_format option set. This should be true of extensions of google.protobuf.bridge.MessageSet; by convention, such extensions are named "message_set_extension".

The opposite operation (looking up an extension's FieldDescriptor given its printable name) can be accomplished with

message->file()->pool()->FindExtensionByPrintableName(message, name)

where the extension extends "message".


bool FieldDescriptor::GetSourceLocation(
        SourceLocation * out_location) const

Updates |*out_location| to the source location of the complete extent of this field declaration.

Returns false and leaves |*out_location| unchanged iff location information was not available.

class OneofDescriptor

#include <google/protobuf/descriptor.h>
namespace google::protobuf

Describes a oneof defined in a message type.

Members

typedef
OneofDescriptorProto Proto
const std::string &
name() const
Name of this oneof.
const std::string &
full_name() const
Fully-qualified name of the oneof.
int
index() const
Index of this oneof within the message's oneof array.
bool
is_synthetic() const
Returns whether this oneof was inserted by the compiler to wrap a proto3 optional field. more...
const FileDescriptor *
file() const
The .proto file in which this oneof was defined. Never nullptr.
const Descriptor *
containing_type() const
The Descriptor for the message containing this oneof.
int
field_count() const
The number of (non-extension) fields which are members of this oneof.
const FieldDescriptor *
field(int index) const
Get a member of this oneof, in the order in which they were declared in the .proto file. more...
const OneofOptions &
options() const
void
CopyTo(OneofDescriptorProto * proto) const
std::string
DebugString() const
std::string
DebugStringWithOptions(const DebugStringOptions & options) const

Source Location

bool
GetSourceLocation(SourceLocation * out_location) const
Updates |*out_location| to the source location of the complete extent of this oneof declaration. more...

bool OneofDescriptor::is_synthetic() const

Returns whether this oneof was inserted by the compiler to wrap a proto3 optional field.

If this returns true, code generators should not emit it.


const FieldDescriptor *
    OneofDescriptor::field(
        int index) const

Get a member of this oneof, in the order in which they were declared in the .proto file.

Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because fields_ is actually an array of pointers rather than the usual array of objects.

Does not include extensions.


bool OneofDescriptor::GetSourceLocation(
        SourceLocation * out_location) const

Updates |*out_location| to the source location of the complete extent of this oneof declaration.

Returns false and leaves |*out_location| unchanged iff location information was not available.

class EnumDescriptor

#include <google/protobuf/descriptor.h>
namespace google::protobuf

Describes an enum type defined in a .proto file.

To get the EnumDescriptor for a generated enum type, call TypeName_descriptor(). Use DescriptorPool to construct your own descriptors.

Members

typedef
EnumDescriptorProto Proto
const std::string &
name() const
The name of this enum type in the containing scope.
const std::string &
full_name() const
The fully-qualified name of the enum type, scope delimited by periods.
int
index() const
Index of this enum within the file or containing message's enum array.
const FileDescriptor *
file() const
The .proto file in which this enum type was defined. Never nullptr.
int
value_count() const
The number of values for this EnumDescriptor. more...
const EnumValueDescriptor *
value(int index) const
Gets a value by index, where 0 <= index < value_count(). more...
const EnumValueDescriptor *
FindValueByName(ConstStringParam name) const
Looks up a value by name. Returns nullptr if no such value exists.
const EnumValueDescriptor *
FindValueByNumber(int number) const
Looks up a value by number. more...
const Descriptor *
containing_type() const
If this enum type is nested in a message type, this is that message type. more...
const EnumOptions &
options() const
Get options for this enum type. more...
void
CopyTo(EnumDescriptorProto * proto) const
std::string
DebugString() const
std::string
DebugStringWithOptions(const DebugStringOptions & options) const
bool
is_placeholder() const
Returns true if this is a placeholder for an unknown enum. more...

Reserved fields

int
reserved_range_count() const
The number of reserved ranges in this message type.
const EnumDescriptor::ReservedRange *
reserved_range(int index) const
Gets an reserved range by index, where 0 <= index < reserved_range_count(). more...
bool
IsReservedNumber(int number) const
Returns true if the number is in one of the reserved ranges.
const EnumDescriptor::ReservedRange *
FindReservedRangeContainingNumber(int number) const
Returns nullptr if no reserved range contains the given number.
int
reserved_name_count() const
The number of reserved field names in this message type.
const std::string &
reserved_name(int index) const
Gets a reserved name by index, where 0 <= index < reserved_name_count(). more...
bool
IsReservedName(ConstStringParam name) const
Returns true if the field name is reserved.

Source Location

bool
GetSourceLocation(SourceLocation * out_location) const
Updates |*out_location| to the source location of the complete extent of this enum declaration. more...

int EnumDescriptor::value_count() const

The number of values for this EnumDescriptor.

Guaranteed to be greater than zero.


const EnumValueDescriptor *
    EnumDescriptor::value(
        int index) const

Gets a value by index, where 0 <= index < value_count().

These are returned in the order they were defined in the .proto file.


const EnumValueDescriptor *
    EnumDescriptor::FindValueByNumber(
        int number) const

Looks up a value by number.

Returns nullptr if no such value exists. If multiple values have this number, the first one defined is returned.


const Descriptor *
    EnumDescriptor::containing_type() const

If this enum type is nested in a message type, this is that message type.

Otherwise, nullptr.


const EnumOptions &
    EnumDescriptor::options() const

Get options for this enum type.

These are specified in the .proto file by placing lines like "option foo = 1234;" in the enum definition. Allowed options are defined by EnumOptions in descriptor.proto, and any available extensions of that message.


bool EnumDescriptor::is_placeholder() const

Returns true if this is a placeholder for an unknown enum.

This will only be the case if this descriptor comes from a DescriptorPool with AllowUnknownDependencies() set.


const EnumDescriptor::ReservedRange *
    EnumDescriptor::reserved_range(
        int index) const

Gets an reserved range by index, where 0 <= index < reserved_range_count().

These are returned in the order they were defined in the .proto file.


const std::string &
    EnumDescriptor::reserved_name(
        int index) const

Gets a reserved name by index, where 0 <= index < reserved_name_count().

Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because reserved_names_ is actually an array of pointers rather than the usual array of objects.


bool EnumDescriptor::GetSourceLocation(
        SourceLocation * out_location) const

Updates |*out_location| to the source location of the complete extent of this enum declaration.

Returns false and leaves |*out_location| unchanged iff location information was not available.

struct EnumDescriptor::ReservedRange

#include <google/protobuf/descriptor.h>
namespace google::protobuf

A range of reserved field numbers.

Members

int
start
inclusive
int
end
inclusive

class EnumValueDescriptor

#include <google/protobuf/descriptor.h>
namespace google::protobuf

Describes an individual enum constant of a particular type.

To get the EnumValueDescriptor for a given enum value, first get the EnumDescriptor for its type, then use EnumDescriptor::FindValueByName() or EnumDescriptor::FindValueByNumber(). Use DescriptorPool to construct your own descriptors.

Members

typedef
EnumValueDescriptorProto Proto
const std::string &
name() const
Name of this enum constant.
int
index() const
Index within the enums's Descriptor.
int
number() const
Numeric value of this enum constant.
const std::string &
full_name() const
The full_name of an enum value is a sibling symbol of the enum type. more...
const FileDescriptor *
file() const
The .proto file in which this value was defined. Never nullptr.
const EnumDescriptor *
type() const
The type of this value. Never nullptr.
const EnumValueOptions &
options() const
Get options for this enum value. more...
void
CopyTo(EnumValueDescriptorProto * proto) const
std::string
DebugString() const
std::string
DebugStringWithOptions(const DebugStringOptions & options) const

Source Location

bool
GetSourceLocation(SourceLocation * out_location) const
Updates |*out_location| to the source location of the complete extent of this enum value declaration. more...

const std::string &
    EnumValueDescriptor::full_name() const

The full_name of an enum value is a sibling symbol of the enum type.

e.g. the full name of FieldDescriptorProto::TYPE_INT32 is actually "google.protobuf.FieldDescriptorProto.TYPE_INT32", NOT "google.protobuf.FieldDescriptorProto.Type.TYPE_INT32". This is to conform with C++ scoping rules for enums.


const EnumValueOptions &
    EnumValueDescriptor::options() const

Get options for this enum value.

These are specified in the .proto file by adding text like "[[]foo = 1234]" after an enum value definition. Allowed options are defined by EnumValueOptions in descriptor.proto, and any available extensions of that message.


bool EnumValueDescriptor::GetSourceLocation(
        SourceLocation * out_location) const

Updates |*out_location| to the source location of the complete extent of this enum value declaration.

Returns false and leaves |*out_location| unchanged iff location information was not available.

class ServiceDescriptor

#include <google/protobuf/descriptor.h>
namespace google::protobuf

Describes an RPC service.

Use DescriptorPool to construct your own descriptors.

Members

typedef
ServiceDescriptorProto Proto
const std::string &
name() const
The name of the service, not including its containing scope.
const std::string &
full_name() const
The fully-qualified name of the service, scope delimited by periods.
int
index() const
Index of this service within the file's services array.
const FileDescriptor *
file() const
The .proto file in which this service was defined. Never nullptr.
const ServiceOptions &
options() const
Get options for this service type. more...
int
method_count() const
The number of methods this service defines.
const MethodDescriptor *
method(int index) const
Gets a MethodDescriptor by index, where 0 <= index < method_count(). more...
const MethodDescriptor *
FindMethodByName(ConstStringParam name) const
Look up a MethodDescriptor by name.
void
CopyTo(ServiceDescriptorProto * proto) const
std::string
DebugString() const
std::string
DebugStringWithOptions(const DebugStringOptions & options) const

Source Location

bool
GetSourceLocation(SourceLocation * out_location) const
Updates |*out_location| to the source location of the complete extent of this service declaration. more...

const ServiceOptions &
    ServiceDescriptor::options() const

Get options for this service type.

These are specified in the .proto file by placing lines like "option foo = 1234;" in the service definition. Allowed options are defined by ServiceOptions in descriptor.proto, and any available extensions of that message.


const MethodDescriptor *
    ServiceDescriptor::method(
        int index) const

Gets a MethodDescriptor by index, where 0 <= index < method_count().

These are returned in the order they were defined in the .proto file.


bool ServiceDescriptor::GetSourceLocation(
        SourceLocation * out_location) const

Updates |*out_location| to the source location of the complete extent of this service declaration.

Returns false and leaves |*out_location| unchanged iff location information was not available.

class MethodDescriptor

#include <google/protobuf/descriptor.h>
namespace google::protobuf

Describes an individual service method.

To obtain a MethodDescriptor given a service, first get its ServiceDescriptor, then call ServiceDescriptor::FindMethodByName(). Use DescriptorPool to construct your own descriptors.

Members

typedef
MethodDescriptorProto Proto
const std::string &
name() const
Name of this method, not including containing scope.
const std::string &
full_name() const
The fully-qualified name of the method, scope delimited by periods.
int
index() const
Index within the service's Descriptor.
const FileDescriptor *
file() const
The .proto file in which this method was defined. Never nullptr.
const ServiceDescriptor *
service() const
Gets the service to which this method belongs. Never nullptr.
const Descriptor *
input_type() const
Gets the type of protocol message which this method accepts as input.
const Descriptor *
output_type() const
Gets the type of protocol message which this message produces as output.
bool
client_streaming() const
Gets whether the client streams multiple requests.
bool
server_streaming() const
Gets whether the server streams multiple responses.
const MethodOptions &
options() const
Get options for this method. more...
void
CopyTo(MethodDescriptorProto * proto) const
std::string
DebugString() const
std::string
DebugStringWithOptions(const DebugStringOptions & options) const

Source Location

bool
GetSourceLocation(SourceLocation * out_location) const
Updates |*out_location| to the source location of the complete extent of this method declaration. more...

const MethodOptions &
    MethodDescriptor::options() const

Get options for this method.

These are specified in the .proto file by placing lines like "option foo = 1234;" in curly-braces after a method declaration. Allowed options are defined by MethodOptions in descriptor.proto, and any available extensions of that message.


bool MethodDescriptor::GetSourceLocation(
        SourceLocation * out_location) const

Updates |*out_location| to the source location of the complete extent of this method declaration.

Returns false and leaves |*out_location| unchanged iff location information was not available.

class FileDescriptor

#include <google/protobuf/descriptor.h>
namespace google::protobuf

Describes a whole .proto file.

To get the FileDescriptor for a compiled-in file, get the descriptor for something defined in that file and call descriptor->file(). Use DescriptorPool to construct your own descriptors.

Members

enum
Syntax
Syntax of this file. more...
typedef
FileDescriptorProto Proto
const std::string &
name() const
The filename, relative to the source tree. more...
const std::string &
package() const
The package, e.g. "google.protobuf.compiler".
const DescriptorPool *
pool() const
The DescriptorPool in which this FileDescriptor and all its contents were allocated. more...
int
dependency_count() const
The number of files imported by this one.
const FileDescriptor *
dependency(int index) const
Gets an imported file by index, where 0 <= index < dependency_count(). more...
int
public_dependency_count() const
The number of files public imported by this one. more...
const FileDescriptor *
public_dependency(int index) const
Gets a public imported file by index, where 0 <= index < public_dependency_count(). more...
int
weak_dependency_count() const
The number of files that are imported for weak fields. more...
const FileDescriptor *
weak_dependency(int index) const
Gets a weak imported file by index, where 0 <= index < weak_dependency_count(). more...
int
message_type_count() const
Number of top-level message types defined in this file. more...
const Descriptor *
message_type(int index) const
Gets a top-level message type, where 0 <= index < message_type_count(). more...
int
enum_type_count() const
Number of top-level enum types defined in this file. more...
const EnumDescriptor *
enum_type(int index) const
Gets a top-level enum type, where 0 <= index < enum_type_count(). more...
int
service_count() const
Number of services defined in this file.
const ServiceDescriptor *
service(int index) const
Gets a service, where 0 <= index < service_count(). more...
int
extension_count() const
Number of extensions defined at file scope. more...
const FieldDescriptor *
extension(int index) const
Gets an extension's descriptor, where 0 <= index < extension_count(). more...
const FileOptions &
options() const
Get options for this file. more...
Syntax
syntax() const
const Descriptor *
FindMessageTypeByName(ConstStringParam name) const
Find a top-level message type by name (not full_name). more...
const EnumDescriptor *
FindEnumTypeByName(ConstStringParam name) const
Find a top-level enum type by name. Returns nullptr if not found.
const EnumValueDescriptor *
FindEnumValueByName(ConstStringParam name) const
Find an enum value defined in any top-level enum by name. more...
const ServiceDescriptor *
FindServiceByName(ConstStringParam name) const
Find a service definition by name. Returns nullptr if not found.
const FieldDescriptor *
FindExtensionByName(ConstStringParam name) const
Find a top-level extension definition by name. more...
const FieldDescriptor *
FindExtensionByLowercaseName(ConstStringParam name) const
Similar to FindExtensionByName(), but searches by lowercased-name. more...
const FieldDescriptor *
FindExtensionByCamelcaseName(ConstStringParam name) const
Similar to FindExtensionByName(), but searches by camelcased-name. more...
void
CopyTo(FileDescriptorProto * proto) const
void
CopySourceCodeInfoTo(FileDescriptorProto * proto) const
Write the source code information of this FileDescriptor into the given FileDescriptorProto. more...
void
CopyJsonNameTo(FileDescriptorProto * proto) const
Fill the json_name field of FieldDescriptorProto for all fields. more...
std::string
DebugString() const
std::string
DebugStringWithOptions(const DebugStringOptions & options) const
bool
is_placeholder() const
Returns true if this is a placeholder for an unknown file. more...
bool
GetSourceLocation(SourceLocation * out_location) const
Updates |*out_location| to the source location of the complete extent of this file declaration (namely, the empty path).
bool
GetSourceLocation(const std::vector< int > & path, SourceLocation * out_location) const
Updates |*out_location| to the source location of the complete extent of the declaration or declaration-part denoted by |path|. more...
static const char *
SyntaxName(Syntax syntax)

enum FileDescriptor::Syntax {
  SYNTAX_UNKNOWN = = 0,
  SYNTAX_PROTO2 = = 2,
  SYNTAX_PROTO3 = = 3
}

Syntax of this file.

SYNTAX_UNKNOWN
SYNTAX_PROTO2
SYNTAX_PROTO3

const std::string &
    FileDescriptor::name() const

The filename, relative to the source tree.

e.g. "foo/bar/baz.proto"


const DescriptorPool *
    FileDescriptor::pool() const

The DescriptorPool in which this FileDescriptor and all its contents were allocated.

Never nullptr.


const FileDescriptor *
    FileDescriptor::dependency(
        int index) const

Gets an imported file by index, where 0 <= index < dependency_count().

These are returned in the order they were defined in the .proto file.


int FileDescriptor::public_dependency_count() const

The number of files public imported by this one.

The public dependency list is a subset of the dependency list.


const FileDescriptor *
    FileDescriptor::public_dependency(
        int index) const

Gets a public imported file by index, where 0 <= index < public_dependency_count().

These are returned in the order they were defined in the .proto file.


int FileDescriptor::weak_dependency_count() const

The number of files that are imported for weak fields.

The weak dependency list is a subset of the dependency list.


const FileDescriptor *
    FileDescriptor::weak_dependency(
        int index) const

Gets a weak imported file by index, where 0 <= index < weak_dependency_count().

These are returned in the order they were defined in the .proto file.


int FileDescriptor::message_type_count() const

Number of top-level message types defined in this file.

(This does not include nested types.)


const Descriptor *
    FileDescriptor::message_type(
        int index) const

Gets a top-level message type, where 0 <= index < message_type_count().

These are returned in the order they were defined in the .proto file.


int FileDescriptor::enum_type_count() const

Number of top-level enum types defined in this file.

(This does not include nested types.)


const EnumDescriptor *
    FileDescriptor::enum_type(
        int index) const

Gets a top-level enum type, where 0 <= index < enum_type_count().

These are returned in the order they were defined in the .proto file.


const ServiceDescriptor *
    FileDescriptor::service(
        int index) const

Gets a service, where 0 <= index < service_count().

These are returned in the order they were defined in the .proto file.


int FileDescriptor::extension_count() const

Number of extensions defined at file scope.

(This does not include extensions nested within message types.)


const FieldDescriptor *
    FileDescriptor::extension(
        int index) const

Gets an extension's descriptor, where 0 <= index < extension_count().

These are returned in the order they were defined in the .proto file.


const FileOptions &
    FileDescriptor::options() const

Get options for this file.

These are specified in the .proto file by placing lines like "option foo = 1234;" at the top level, outside of any other definitions. Allowed options are defined by FileOptions in descriptor.proto, and any available extensions of that message.


const Descriptor *
    FileDescriptor::FindMessageTypeByName(
        ConstStringParam name) const

Find a top-level message type by name (not full_name).

Returns nullptr if not found.


const EnumValueDescriptor *
    FileDescriptor::FindEnumValueByName(
        ConstStringParam name) const

Find an enum value defined in any top-level enum by name.

Returns nullptr if not found.


const FieldDescriptor *
    FileDescriptor::FindExtensionByName(
        ConstStringParam name) const

Find a top-level extension definition by name.

Returns nullptr if not found.


const FieldDescriptor *
    FileDescriptor::FindExtensionByLowercaseName(
        ConstStringParam name) const

Similar to FindExtensionByName(), but searches by lowercased-name.

See Descriptor::FindFieldByLowercaseName().


const FieldDescriptor *
    FileDescriptor::FindExtensionByCamelcaseName(
        ConstStringParam name) const

Similar to FindExtensionByName(), but searches by camelcased-name.

See Descriptor::FindFieldByCamelcaseName().


void FileDescriptor::CopyTo(
        FileDescriptorProto * proto) const

See Descriptor::CopyTo().

Notes:

  • This method does NOT copy source code information since it is relatively large and rarely needed. See CopySourceCodeInfoTo() below.

void FileDescriptor::CopySourceCodeInfoTo(
        FileDescriptorProto * proto) const

Write the source code information of this FileDescriptor into the given FileDescriptorProto.

See CopyTo() above.


void FileDescriptor::CopyJsonNameTo(
        FileDescriptorProto * proto) const

Fill the json_name field of FieldDescriptorProto for all fields.

Can only be called after CopyTo().


bool FileDescriptor::is_placeholder() const

Returns true if this is a placeholder for an unknown file.

This will only be the case if this descriptor comes from a DescriptorPool with AllowUnknownDependencies() set.


bool FileDescriptor::GetSourceLocation(
        const std::vector< int > & path,
        SourceLocation * out_location) const

Updates |*out_location| to the source location of the complete extent of the declaration or declaration-part denoted by |path|.

Returns false and leaves |*out_location| unchanged iff location information was not available. (See SourceCodeInfo for description of path encoding.)

class DescriptorPool

#include <google/protobuf/descriptor.h>
namespace google::protobuf

Used to construct descriptors.

Normally you won't want to build your own descriptors. Message classes constructed by the protocol compiler will provide them for you. However, if you are implementing Message on your own, or if you are writing a program which can operate on totally arbitrary types and needs to load them from some sort of database, you might need to.

Since Descriptors are composed of a whole lot of cross-linked bits of data that would be a pain to put together manually, the DescriptorPool class is provided to make the process easier. It can take a FileDescriptorProto (defined in descriptor.proto), validate it, and convert it to a set of nicely cross-linked Descriptors.

DescriptorPool also helps with memory management. Descriptors are composed of many objects containing static data and pointers to each other. In all likelihood, when it comes time to delete this data, you'll want to delete it all at once. In fact, it is not uncommon to have a whole pool of descriptors all cross-linked with each other which you wish to delete all at once. This class represents such a pool, and handles the memory management for you.

You can also search for descriptors within a DescriptorPool by name, and extensions by number.

Members

DescriptorPool()
Create a normal, empty DescriptorPool.
explicit
DescriptorPool(DescriptorDatabase * fallback_database, ErrorCollector * error_collector = nullptr)
~DescriptorPool()
const FileDescriptor *
FindFileByName(ConstStringParam name) const
Find a FileDescriptor in the pool by file name. more...
const FileDescriptor *
FindFileContainingSymbol(ConstStringParam symbol_name) const
Find the FileDescriptor in the pool which defines the given symbol. more...
static const DescriptorPool *
generated_pool()
Get a pointer to the generated pool. more...

Looking up descriptors

These find descriptors by fully-qualified name. These will find both top-level descriptors and nested descriptors. They return nullptr if not found.
const Descriptor *
FindMessageTypeByName(ConstStringParam name) const
const FieldDescriptor *
FindFieldByName(ConstStringParam name) const
const FieldDescriptor *
FindExtensionByName(ConstStringParam name) const
const OneofDescriptor *
FindOneofByName(ConstStringParam name) const
const EnumDescriptor *
FindEnumTypeByName(ConstStringParam name) const
const EnumValueDescriptor *
FindEnumValueByName(ConstStringParam name) const
const ServiceDescriptor *
FindServiceByName(ConstStringParam name) const
const MethodDescriptor *
FindMethodByName(ConstStringParam name) const
const FieldDescriptor *
FindExtensionByNumber(const Descriptor * extendee, int number) const
Finds an extension of the given type by number. more...
const FieldDescriptor *
FindExtensionByPrintableName(const Descriptor * extendee, ConstStringParam printable_name) const
Finds an extension of the given type by its printable name. more...
void
FindAllExtensions(const Descriptor * extendee, std::vector< const FieldDescriptor * > * out) const
Finds extensions of extendee. more...

Building descriptors

const FileDescriptor *
BuildFile(const FileDescriptorProto & proto)
Convert the FileDescriptorProto to real descriptors and place them in this DescriptorPool. more...
const FileDescriptor *
BuildFileCollectingErrors(const FileDescriptorProto & proto, ErrorCollector * error_collector)
Same as BuildFile() except errors are sent to the given ErrorCollector.
void
AllowUnknownDependencies()
By default, it is an error if a FileDescriptorProto contains references to types or other files that are not found in the DescriptorPool (or its backing DescriptorDatabase, if any). more...
void
EnforceWeakDependencies(bool enforce)
By default, weak imports are allowed to be missing, in which case we will use a placeholder for the dependency and convert the field to be an Empty message field. more...

Internal stuff

These methods MUST NOT be called from outside the proto2 library.

These methods may contain hidden pitfalls and may be removed in a future library version.

explicit
DescriptorPool(const DescriptorPool * underlay)
Create a DescriptorPool which is overlaid on top of some other pool. more...
void
DisallowEnforceUtf8()
Disallow [[]enforce_utf8 = false] in .proto files.
void
InternalDontEnforceDependencies()
For internal use only: Changes the behavior of BuildFile() such that it allows the file to make reference to message types declared in other files which it did not officially declare as dependencies.
void
InternalSetLazilyBuildDependencies()
For internal use only: Enables lazy building of dependencies of a file. more...
void
internal_set_underlay(const DescriptorPool * underlay)
For internal use only.
bool
InternalIsFileLoaded(ConstStringParam filename) const
For internal (unit test) use only: Returns true if a FileDescriptor has been constructed for the given file, false otherwise. more...
void
AddUnusedImportTrackFile(ConstStringParam file_name, bool is_error = false)
Add a file to unused_import_track_files_. more...
void
ClearUnusedImportTrackFiles()
static void
InternalAddGeneratedFile(const void * encoded_file_descriptor, int size)
Called by generated classes at init time to add their descriptors to generated_pool. more...
static DescriptorPool *
internal_generated_pool()
For internal use only: Gets a non-const pointer to the generated pool. more...
static DescriptorDatabase *
internal_generated_database()
For internal use only: Gets a non-const pointer to the generated descriptor database. more...

const FileDescriptor *
    DescriptorPool::FindFileByName(
        ConstStringParam name) const

Find a FileDescriptor in the pool by file name.

Returns nullptr if not found.


const FileDescriptor *
    DescriptorPool::FindFileContainingSymbol(
        ConstStringParam symbol_name) const

Find the FileDescriptor in the pool which defines the given symbol.

If any of the Find*ByName() methods below would succeed, then this is equivalent to calling that method and calling the result's file() method. Otherwise this returns nullptr.


static const DescriptorPool *
    DescriptorPool::generated_pool()

Get a pointer to the generated pool.

Generated protocol message classes which are compiled into the binary will allocate their descriptors in this pool. Do not add your own descriptors to this pool.


const FieldDescriptor *
    DescriptorPool::FindExtensionByNumber(
        const Descriptor * extendee,
        int number) const

Finds an extension of the given type by number.

The extendee must be a member of this DescriptorPool or one of its underlays.


const FieldDescriptor *
    DescriptorPool::FindExtensionByPrintableName(
        const Descriptor * extendee,
        ConstStringParam printable_name) const

Finds an extension of the given type by its printable name.

See comments above PrintableNameForExtension() for the definition of "printable name". The extendee must be a member of this DescriptorPool or one of its underlays. Returns nullptr if there is no known message extension with the given printable name.


void DescriptorPool::FindAllExtensions(
        const Descriptor * extendee,
        std::vector< const FieldDescriptor * > * out) const

Finds extensions of extendee.

The extensions will be appended to out in an undefined order. Only extensions defined directly in this DescriptorPool or one of its underlays are guaranteed to be found: extensions defined in the fallback database might not be found depending on the database implementation.


const FileDescriptor *
    DescriptorPool::BuildFile(
        const FileDescriptorProto & proto)

Convert the FileDescriptorProto to real descriptors and place them in this DescriptorPool.

All dependencies of the file must already be in the pool. Returns the resulting FileDescriptor, or nullptr if there were problems with the input (e.g. the message was invalid, or dependencies were missing). Details about the errors are written to GOOGLE_LOG(ERROR).


void DescriptorPool::AllowUnknownDependencies()

By default, it is an error if a FileDescriptorProto contains references to types or other files that are not found in the DescriptorPool (or its backing DescriptorDatabase, if any).

If you call AllowUnknownDependencies(), however, then unknown types and files will be replaced by placeholder descriptors (which can be identified by the is_placeholder() method). This can allow you to perform some useful operations with a .proto file even if you do not have access to other .proto files on which it depends. However, some heuristics must be used to fill in the gaps in information, and these can lead to descriptors which are inaccurate. For example, the DescriptorPool may be forced to guess whether an unknown type is a message or an enum, as well as what package it resides in. Furthermore, placeholder types will not be discoverable via FindMessageTypeByName() and similar methods, which could confuse some descriptor-based algorithms. Generally, the results of this option should be handled with extreme care.


void DescriptorPool::EnforceWeakDependencies(
        bool enforce)

By default, weak imports are allowed to be missing, in which case we will use a placeholder for the dependency and convert the field to be an Empty message field.

If you call EnforceWeakDependencies(true), however, the DescriptorPool will report a import not found error.


explicit DescriptorPool::DescriptorPool(
        const DescriptorPool * underlay)

Create a DescriptorPool which is overlaid on top of some other pool.

If you search for a descriptor in the overlay and it is not found, the underlay will be searched as a backup. If the underlay has its own underlay, that will be searched next, and so on. This also means that files built in the overlay will be cross-linked with the underlay's descriptors if necessary. The underlay remains property of the caller; it must remain valid for the lifetime of the newly-constructed pool.

Example: Say you want to parse a .proto file at runtime in order to use its type with a DynamicMessage. Say this .proto file has dependencies, but you know that all the dependencies will be things that are already compiled into the binary. For ease of use, you'd like to load the types right out of generated_pool() rather than have to parse redundant copies of all these .protos and runtime. But, you don't want to add the parsed types directly into generated_pool(): this is not allowed, and would be bad design anyway. So, instead, you could use generated_pool() as an underlay for a new DescriptorPool in which you add only the new file.

WARNING: Use of underlays can lead to many subtle gotchas. Instead, try to formulate what you want to do in terms of DescriptorDatabases.


void DescriptorPool::InternalSetLazilyBuildDependencies()

For internal use only: Enables lazy building of dependencies of a file.

Delay the building of dependencies of a file descriptor until absolutely necessary, like when message_type() is called on a field that is defined in that dependency's file. This will cause functional issues if a proto or one of its dependencies has errors. Should only be enabled for the generated_pool_ (because no descriptor build errors are guaranteed by the compilation generation process), testing, or if a lack of descriptor build errors can be guaranteed for a pool.


bool DescriptorPool::InternalIsFileLoaded(
        ConstStringParam filename) const

For internal (unit test) use only: Returns true if a FileDescriptor has been constructed for the given file, false otherwise.

Useful for testing lazy descriptor initialization behavior.


void DescriptorPool::AddUnusedImportTrackFile(
        ConstStringParam file_name,
        bool is_error = false)

Add a file to unused_import_track_files_.

DescriptorBuilder will log warnings or errors for those files if there is any unused import.


static void DescriptorPool::InternalAddGeneratedFile(
        const void * encoded_file_descriptor,
        int size)

Called by generated classes at init time to add their descriptors to generated_pool.

Do NOT call this in your own code! filename must be a permanent string (e.g. a string literal).


static DescriptorPool * DescriptorPool::internal_generated_pool()

For internal use only: Gets a non-const pointer to the generated pool.

This is called at static-initialization time only, so thread-safety is not a concern. If both an underlay and a fallback database are present, the underlay takes precedence.


static DescriptorDatabase *
    DescriptorPool::internal_generated_database()

For internal use only: Gets a non-const pointer to the generated descriptor database.

Only used for testing.

class DescriptorPool::ErrorCollector

#include <google/protobuf/descriptor.h>
namespace google::protobuf

When converting a FileDescriptorProto to a FileDescriptor, various errors might be detected in the input.

The caller may handle these programmatically by implementing an ErrorCollector.

Members

enum
ErrorLocation
These constants specify what exact part of the construct is broken. more...
ErrorCollector()
virtual
~ErrorCollector()
virtual void
AddError(const std::string & filename, const std::string & element_name, const Message * descriptor, ErrorLocation location, const std::string & message) = 0
Reports an error in the FileDescriptorProto. more...
virtual void
AddWarning(const std::string & , const std::string & , const Message * , ErrorLocation , const std::string & )
Reports a warning in the FileDescriptorProto. more...

enum ErrorCollector::ErrorLocation {
  NAME,
  NUMBER,
  TYPE,
  EXTENDEE,
  DEFAULT_VALUE,
  INPUT_TYPE,
  OUTPUT_TYPE,
  OPTION_NAME,
  OPTION_VALUE,
  IMPORT,
  OTHER
}

These constants specify what exact part of the construct is broken.

This is useful e.g. for mapping the error back to an exact location in a .proto file.

NAMEthe symbol name, or the package name for files
NUMBERfield or extension range number
TYPEfield type
EXTENDEEfield extendee
DEFAULT_VALUEfield default value
INPUT_TYPEmethod input type
OUTPUT_TYPEmethod output type
OPTION_NAMEname in assignment
OPTION_VALUEvalue in option assignment
IMPORTimport error
OTHERsome other problem

virtual void ErrorCollector::AddError(
        const std::string & filename,
        const std::string & element_name,
        const Message * descriptor,
        ErrorLocation location,
        const std::string & message) = 0

Reports an error in the FileDescriptorProto.

Use this function if the problem occurred should interrupt building the FileDescriptorProto.


virtual void ErrorCollector::AddWarning(
        const std::string & ,
        const std::string & ,
        const Message * ,
        ErrorLocation ,
        const std::string & )

Reports a warning in the FileDescriptorProto.

Use this function if the problem occurred should NOT interrupt building the FileDescriptorProto.