class DescriptorPool
Defined at line 2069 of file ../../third_party/protobuf/src/google/protobuf/descriptor.h
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.
Public Methods
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). 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.
Defined at line 2249 of file ../../third_party/protobuf/src/google/protobuf/descriptor.h
void AddUnusedImportTrackFile (absl::string_view file_name, bool is_error)
Defined at line 2381 of file ../../third_party/protobuf/src/google/protobuf/descriptor.h
void ClearUnusedImportTrackFiles ()
Defined at line 2386 of file ../../third_party/protobuf/src/google/protobuf/descriptor.h
void DescriptorPool ()
Create a normal, empty DescriptorPool.
void DescriptorPool (DescriptorDatabase * fallback_database, ErrorCollector * error_collector)
void DescriptorPool (const DescriptorPool & )
Defined at line 2102 of file ../../third_party/protobuf/src/google/protobuf/descriptor.h
DescriptorPool & operator= (const DescriptorPool & )
Defined at line 2103 of file ../../third_party/protobuf/src/google/protobuf/descriptor.h
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. If you call EnforceWeakDependencies(true), however, the
DescriptorPool will report a import not found error.
Defined at line 2255 of file ../../third_party/protobuf/src/google/protobuf/descriptor.h
void EnforceExtensionDeclarations (bool enforce)
Toggles enforcement of extension declarations.
This enforcement is disabled by default because it requires full
descriptors with source-retention options, which are generally not
available at runtime.
Defined at line 2268 of file ../../third_party/protobuf/src/google/protobuf/descriptor.h
void SetRecursiveBuildDispatcher (absl::AnyInvocable<void (absl::FunctionRef<void ()>) const> dispatcher)
Dispatch recursive builds to a callback that may stick them onto a separate
thread. This is primarily to avoid stack overflows on untrusted inputs.
The dispatcher must always synchronously execute the provided callback.
Asynchronous execution is undefined behavior.
Defined at line 2277 of file ../../third_party/protobuf/src/google/protobuf/descriptor.h
void DisallowEnforceUtf8 ()
Disallow [enforce_utf8 = false] in .proto files.
Defined at line 2323 of file ../../third_party/protobuf/src/google/protobuf/descriptor.h
void UseDeprecatedLegacyJsonFieldConflicts ()
Use the deprecated legacy behavior for handling JSON field name conflicts.
Defined at line 2326 of file ../../third_party/protobuf/src/google/protobuf/descriptor.h
void 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.
Defined at line 2356 of file ../../third_party/protobuf/src/google/protobuf/descriptor.h
void internal_set_underlay (const DescriptorPool * underlay)
For internal use only.
Defined at line 2364 of file ../../third_party/protobuf/src/google/protobuf/descriptor.h
void ~DescriptorPool ()
const 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 FileDescriptor * FindFileByName (absl::string_view name)
Find a FileDescriptor in the pool by file name. Returns nullptr if not
found.
const FileDescriptor * FindFileContainingSymbol (absl::string_view symbol_name)
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.
const Descriptor * FindMessageTypeByName (absl::string_view name)
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 FieldDescriptor * FindFieldByName (absl::string_view name)
const FieldDescriptor * FindExtensionByName (absl::string_view name)
const OneofDescriptor * FindOneofByName (absl::string_view name)
const EnumDescriptor * FindEnumTypeByName (absl::string_view name)
const EnumValueDescriptor * FindEnumValueByName (absl::string_view name)
const ServiceDescriptor * FindServiceByName (absl::string_view name)
const MethodDescriptor * FindMethodByName (absl::string_view name)
const FieldDescriptor * FindExtensionByNumber (const Descriptor * extendee, int number)
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 * FindExtensionByPrintableName (const Descriptor * extendee, absl::string_view printable_name)
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 FindAllExtensions (const Descriptor * extendee, std::vector<const FieldDescriptor *> * out)
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 * 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 ABSL_LOG(ERROR).
const FileDescriptor * BuildFileCollectingErrors (const FileDescriptorProto & proto, ErrorCollector * error_collector)
Same as BuildFile() except errors are sent to the given ErrorCollector.
absl::Status SetFeatureSetDefaults (FeatureSetDefaults spec)
Sets the default feature mappings used during the build. If this function
isn't called, the C++ feature set defaults are used. If this function is
called, these defaults will be used instead.
FeatureSetDefaults includes a minimum/maximum supported edition, which will
be enforced while building proto files.
void 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 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).
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.
DescriptorDatabase * internal_generated_database ()
For internal use only: Gets a non-const pointer to the generated
descriptor database.
Only used for testing.
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.
bool InternalIsFileLoaded (absl::string_view filename)
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 AddDirectInputFile (absl::string_view file_name, bool unused_import_is_error)
Add a file to to apply more strict checks to.
- unused imports will log either warnings or errors.
- deprecated features will log warnings.
void ClearDirectInputFiles ()
Records
Friends
class CommandLineInterface
class ValidationErrorTest
class FileDescriptorTables
class DescriptorBuilder
class FileDescriptor
class MethodDescriptor
class ServiceDescriptor
class EnumDescriptor
class FieldDescriptor
class LazyDescriptor
class Descriptor