dynamic_message.h

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

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

Defines an implementation of Message which can emulate types which are not known at compile-time.

Classes in this file

Constructs implementations of Message which can emulate types which are not known at compile-time.
Helper for computing a sorted list of map entries via reflection.

class DynamicMessageFactory: public MessageFactory

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

Constructs implementations of Message which can emulate types which are not known at compile-time.

Sometimes you want to be able to manipulate protocol types that you don't know about at compile time. It would be nice to be able to construct a Message object which implements the message type given by any arbitrary Descriptor. DynamicMessage provides this.

As it turns out, a DynamicMessage needs to construct extra information about its type in order to operate. Most of this information can be shared between all DynamicMessages of the same type. But, caching this information in some sort of global map would be a bad idea, since the cached information for a particular descriptor could outlive the descriptor itself. To avoid this problem, DynamicMessageFactory encapsulates this "cache". All DynamicMessages of the same type created from the same factory will share the same support data. Any Descriptors used with a particular factory must outlive the factory.

Members

DynamicMessageFactory()
Construct a DynamicMessageFactory that will search for extensions in the DescriptorPool in which the extendee is defined.
DynamicMessageFactory(const DescriptorPool * pool)
Construct a DynamicMessageFactory that will search for extensions in the given DescriptorPool. more...
~DynamicMessageFactory()
void
SetDelegateToGeneratedFactory(bool enable)
Call this to tell the DynamicMessageFactory that if it is given a Descriptor d for which: more...

implements MessageFactory

virtual const Message *
GetPrototype(const Descriptor * type)
Given a Descriptor, constructs the default (prototype) Message of that type. more...

DynamicMessageFactory::DynamicMessageFactory(
        const DescriptorPool * pool)

Construct a DynamicMessageFactory that will search for extensions in the given DescriptorPool.

DEPRECATED: Use CodedInputStream::SetExtensionRegistry() to tell the parser to look for extensions in an alternate pool. However, note that this is almost never what you want to do. Almost all users should use the zero-arg constructor.


void DynamicMessageFactory::SetDelegateToGeneratedFactory(
        bool enable)

Call this to tell the DynamicMessageFactory that if it is given a Descriptor d for which:

d->file()->pool() == DescriptorPool::generated_pool(),

then it should delegate to MessageFactory::generated_factory() instead of constructing a dynamic implementation of the message. In theory there is no down side to doing this, so it may become the default in the future.


virtual const Message * DynamicMessageFactory::GetPrototype(
        const Descriptor * type)

Given a Descriptor, constructs the default (prototype) Message of that type.

You can then call that message's New() method to construct a mutable message of that type.

Calling this method twice with the same Descriptor returns the same object. The returned object remains property of the factory and will be destroyed when the factory is destroyed. Also, any objects created by calling the prototype's New() method share some data with the prototype, so these must be destroyed before the DynamicMessageFactory is destroyed.

The given descriptor must outlive the returned message, and hence must outlive the DynamicMessageFactory.

The method is thread-safe.

class DynamicMapSorter

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

Helper for computing a sorted list of map entries via reflection.

Members

static std::vector< const Message * >
Sort(const Message & message, int map_size, const Reflection * reflection, const FieldDescriptor * field)