#include <google/protobuf/message.h>
namespace google::protobuf
This file contains the abstract interface for all protocol messages.
Although it's possible to implement this interface manually, most users will use the protocol compiler to generate implementations.
Example usage:
Say you have a message defined as:
message Foo {
optional string text = 1;
repeated int32 numbers = 2;
}
Then, if you used the protocol compiler to generate a class from the above definition, you could use it like so:
string data; // Will store a serialized version of the message.
{
// Create a message and serialize it.
Foo foo;
foo.set_text("Hello World!");
foo.add_numbers(1);
foo.add_numbers(5);
foo.add_numbers(42);
foo.SerializeToString(&data);
}
{
// Parse the serialized message and check that it contains the
// correct data.
Foo foo;
foo.ParseFromString(data);
assert(foo.text() == "Hello World!");
assert(foo.numbers_size() == 3);
assert(foo.numbers(0) == 1);
assert(foo.numbers(1) == 5);
assert(foo.numbers(2) == 42);
}
{
// Same as the last block, but do it dynamically via the Message
// reflection interface.
Message* foo = new Foo;
Descriptor* descriptor = foo->GetDescriptor();
// Get the descriptors for the fields we're interested in and verify
// their types.
FieldDescriptor* text_field = descriptor->FindFieldByName("text");
assert(text_field != NULL);
assert(text_field->type() == FieldDescriptor::TYPE_STRING);
assert(text_field->label() == FieldDescriptor::TYPE_OPTIONAL);
FieldDescriptor* numbers_field = descriptor->FindFieldByName("numbers");
assert(numbers_field != NULL);
assert(numbers_field->type() == FieldDescriptor::TYPE_INT32);
assert(numbers_field->label() == FieldDescriptor::TYPE_REPEATED);
// Parse the message.
foo->ParseFromString(data);
// Use the reflection interface to examine the contents.
const Reflection* reflection = foo->GetReflection();
assert(reflection->GetString(foo, text_field) == "Hello World!");
assert(reflection->CountField(foo, numbers_field) == 3);
assert(reflection->GetInt32(foo, numbers_field, 0) == 1);
assert(reflection->GetInt32(foo, numbers_field, 1) == 5);
assert(reflection->GetInt32(foo, numbers_field, 2) == 42);
delete foo;
}
Classes in this file | |
|---|---|
Abstract interface for protocol messages. | |
This interface contains methods that can be used to dynamically access and modify the fields of a protocol message. | |
Abstract interface for a factory for message objects. | |
#include <google/protobuf/message.h>
namespace google::protobuf
Abstract interface for protocol messages.
The methods of this class that are virtual but not pure-virtual have default implementations based on reflection. Message classes which are optimized for speed will want to override these with faster implementations, but classes optimized for code size may be happy with keeping them. See the optimize_for option in descriptor.proto.
Known subclasses:
DescriptorProtoDescriptorProto_ExtensionRangeEnumDescriptorProtoEnumOptionsEnumValueDescriptorProtoEnumValueOptionsFieldDescriptorProtoFieldOptionsFileDescriptorProtoFileDescriptorSetFileOptionsMessageOptionsMethodDescriptorProtoMethodOptionsServiceDescriptorProtoServiceOptionsUninterpretedOptionUninterpretedOption_NamePartMembers | |
|---|---|
| Message() |
virtual | ~Message() |
Introspection | |
typedef | google::protobuf::Reflection ReflectionTypedef for backwards-compatibility. |
virtual const Descriptor * | GetDescriptor() const = 0Get a Descriptor for this message's type. more... |
virtual const Reflection * | GetReflection() const = 0Get the Reflection interface for this Message, which can be used to read and modify the fields of the Message dynamically (in other words, without knowing the message type at compile time). more... |
Basic Operations | |
virtual Message * | New() const = 0Construct a new instance of the same type. more... |
virtual void | CopyFrom(const Message & from)Make this message into a copy of the given message. more... |
virtual void | MergeFrom(const Message & from)Merge the fields from the given message into this message. more... |
virtual void | Clear()Clear all fields of the message and set them to their default values. more... |
virtual bool | IsInitialized() constQuickly check if all required fields have values set. |
void | CheckInitialized() constVerifies that IsInitialized() returns true. more... |
void | FindInitializationErrors(vector< string > * errors) constSlowly build a list of all required fields that are not set. more... |
string | InitializationErrorString() constLike FindInitializationErrors, but joins all the strings, delimited by commas, and returns them. |
virtual void | DiscardUnknownFields()Clears all unknown fields from this message and all embedded messages. more... |
virtual int | SpaceUsed() constComputes (an estimate of) the total number of bytes currently used for storing the message in memory. more... |
Debugging | |
string | DebugString() constGenerates a human readable form of this message, useful for debugging and other purposes. |
string | ShortDebugString() constLike DebugString(), but with less whitespace. |
void | PrintDebugString() constConvenience function useful in GDB. Prints DebugString() to stdout. |
ParsingMethods for parsing in protocol buffer format. Most of these are just simple wrappers around MergeFromCodedStream(). | |
bool | ParseFromCodedStream(io::CodedInputStream * input)Fill the message with a protocol buffer parsed from the given input stream. more... |
bool | ParsePartialFromCodedStream(io::CodedInputStream * input)Like ParseFromCodedStream(), but accepts messages that are missing required fields. |
bool | ParseFromZeroCopyStream(io::ZeroCopyInputStream * input)Read a protocol buffer from the given zero-copy input stream. more... |
bool | ParsePartialFromZeroCopyStream(io::ZeroCopyInputStream * input)Like ParseFromZeroCopyStream(), but accepts messages that are missing required fields. |
bool | ParseFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input, int size)Read a protocol buffer from the given zero-copy input stream, expecting the message to be exactly "size" bytes long. more... |
bool | ParsePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input, int size)Like ParseFromBoundedZeroCopyStream(), but accepts messages that are missing required fields. |
bool | ParseFromString(const string & data)Parse a protocol buffer contained in a string. |
bool | ParsePartialFromString(const string & data)Like ParseFromString(), but accepts messages that are missing required fields. |
bool | ParseFromArray(const void * data, int size)Parse a protocol buffer contained in an array of bytes. |
bool | ParsePartialFromArray(const void * data, int size)Like ParseFromArray(), but accepts messages that are missing required fields. |
bool | ParseFromFileDescriptor(int file_descriptor)Parse a protocol buffer from a file descriptor. more... |
bool | ParsePartialFromFileDescriptor(int file_descriptor)Like ParseFromFileDescriptor(), but accepts messages that are missing required fields. |
bool | ParseFromIstream(istream * input)Parse a protocol buffer from a C++ istream. more... |
bool | ParsePartialFromIstream(istream * input)Like ParseFromIstream(), but accepts messages that are missing required fields. |
bool | MergeFromCodedStream(io::CodedInputStream * input) |
virtual bool | MergePartialFromCodedStream(io::CodedInputStream * input)Like MergeFromCodedStream(), but succeeds even if required fields are missing in the input. more... |
SerializationMethods for serializing in protocol buffer format. Most of these are just simple wrappers around ByteSize() and SerializeWithCachedSizes(). | |
bool | SerializeToCodedStream(io::CodedOutputStream * output) constWrite a protocol buffer of this message to the given output. more... |
bool | SerializePartialToCodedStream(io::CodedOutputStream * output) constLike SerializeToCodedStream(), but allows missing required fields. |
bool | SerializeToZeroCopyStream(io::ZeroCopyOutputStream * output) constWrite the message to the given zero-copy output stream. more... |
bool | SerializePartialToZeroCopyStream(io::ZeroCopyOutputStream * output) constLike SerializeToZeroCopyStream(), but allows missing required fields. |
bool | SerializeToString(string * output) constSerialize the message and store it in the given string. more... |
bool | SerializePartialToString(string * output) constLike SerializeToString(), but allows missing required fields. |
bool | SerializeToArray(void * data, int size) constSerialize the message and store it in the given byte array. more... |
bool | SerializePartialToArray(void * data, int size) constLike SerializeToArray(), but allows missing required fields. |
bool | SerializeToFileDescriptor(int file_descriptor) constSerialize the message and write it to the given file descriptor. more... |
bool | SerializePartialToFileDescriptor(int file_descriptor) constLike SerializeToFileDescriptor(), but allows missing required fields. |
bool | SerializeToOstream(ostream * output) constSerialize the message and write it to the given C++ ostream. more... |
bool | SerializePartialToOstream(ostream * output) constLike SerializeToOstream(), but allows missing required fields. |
string | SerializeAsString() constMake a string encoding the message. more... |
string | SerializePartialAsString() constLike SerializeAsString(), but allows missing required fields. |
bool | AppendToString(string * output) constLike SerializeToString(), but appends to the data to the string's existing contents. more... |
bool | AppendPartialToString(string * output) constLike AppendToString(), but allows missing required fields. |
virtual int | ByteSize() constComputes the serialized size of the message. more... |
virtual void | SerializeWithCachedSizes(io::CodedOutputStream * output) constSerializes the message without recomputing the size. more... |
virtual uint8 * | SerializeWithCachedSizesToArray(uint8 * target) constLike SerializeWithCachedSizes, but writes directly to *target, returning a pointer to the byte immediately after the last byte written. more... |
virtual int | GetCachedSize() const = 0Returns the result of the last call to ByteSize(). more... |
virtual const Descriptor *
Message::GetDescriptor() const = 0Get a Descriptor for this message's type.
This describes what fields the message contains, the types of those fields, etc.
virtual const Reflection *
Message::GetReflection() const = 0Get the Reflection interface for this Message, which can be used to read and modify the fields of the Message dynamically (in other words, without knowing the message type at compile time).
This object remains property of the Message.
virtual Message * Message::New() const = 0Construct a new instance of the same type.
Ownership is passed to the caller.
virtual void Message::CopyFrom(
const Message & from)Make this message into a copy of the given message.
The given message must have the same descriptor, but need not necessarily be the same class. By default this is just implemented as "Clear(); MergeFrom(from);".
virtual void Message::MergeFrom(
const Message & from)Merge the fields from the given message into this message.
Singular fields will be overwritten, except for embedded messages which will be merged. Repeated fields will be concatenated. The given message must be of the same type as this message (i.e. the exact same class).
virtual void Message::Clear()Clear all fields of the message and set them to their default values.
Clear() avoids freeing memory, assuming that any memory allocated to hold parts of the message will be needed again to hold the next message. If you actually want to free the memory used by a Message, you must delete it.
void Message::CheckInitialized() constVerifies that IsInitialized() returns true.
GOOGLE_CHECK-fails otherwise, with a nice error message.
void Message::FindInitializationErrors(
vector< string > * errors) constSlowly build a list of all required fields that are not set.
This is much, much slower than IsInitialized() as it is implemented purely via reflection. Generally, you should not call this unless you have already determined that an error exists by calling IsInitialized().
virtual void Message::DiscardUnknownFields()Clears all unknown fields from this message and all embedded messages.
Normally, if unknown tag numbers are encountered when parsing a message, the tag and value are stored in the message's UnknownFieldSet and then written back out when the message is serialized. This allows servers which simply route messages to other servers to pass through messages that have new field definitions which they don't yet know about. However, this behavior can have security implications. To avoid it, call this method after parsing.
See Reflection::GetUnknownFields() for more on unknown fields.
virtual int Message::SpaceUsed() constComputes (an estimate of) the total number of bytes currently used for storing the message in memory.
The default implementation calls the Reflection object's SpaceUsed() method.
bool Message::ParseFromCodedStream(
io::CodedInputStream * input)Fill the message with a protocol buffer parsed from the given input stream.
Returns false on a read error or if the input is in the wrong format.
bool Message::ParseFromZeroCopyStream(
io::ZeroCopyInputStream * input)Read a protocol buffer from the given zero-copy input stream.
If successful, the entire input will be consumed.
bool Message::ParseFromBoundedZeroCopyStream(
io::ZeroCopyInputStream * input,
int size)Read a protocol buffer from the given zero-copy input stream, expecting the message to be exactly "size" bytes long.
If successful, exactly this many bytes will have been consumed from the input.
bool Message::ParseFromFileDescriptor(
int file_descriptor)Parse a protocol buffer from a file descriptor.
If successful, the entire input will be consumed.
bool Message::ParseFromIstream(
istream * input)Parse a protocol buffer from a C++ istream.
If successful, the entire input will be consumed.
bool Message::MergeFromCodedStream(
io::CodedInputStream * input)Reads a protocol buffer from the stream and merges it into this Message.
Singular fields read from the input overwrite what is already in the Message and repeated fields are appended to those already present.
It is the responsibility of the caller to call input->LastTagWas() (for groups) or input->ConsumedEntireMessage() (for non-groups) after this returns to verify that the message's end was delimited correctly.
ParsefromCodedStream() is implemented as Clear() followed by MergeFromCodedStream().
virtual bool Message::MergePartialFromCodedStream(
io::CodedInputStream * input)Like MergeFromCodedStream(), but succeeds even if required fields are missing in the input.
MergeFromCodedStream() is just implemented as MergePartialFromCodedStream() followed by IsInitialized().
bool Message::SerializeToCodedStream(
io::CodedOutputStream * output) constWrite a protocol buffer of this message to the given output.
Returns false on a write error. If the message is missing required fields, this may GOOGLE_CHECK-fail.
bool Message::SerializeToZeroCopyStream(
io::ZeroCopyOutputStream * output) constWrite the message to the given zero-copy output stream.
All required fields must be set.
bool Message::SerializeToString(
string * output) constSerialize the message and store it in the given string.
All required fields must be set.
bool Message::SerializeToArray(
void * data,
int size) constSerialize the message and store it in the given byte array.
All required fields must be set.
bool Message::SerializeToFileDescriptor(
int file_descriptor) constSerialize the message and write it to the given file descriptor.
All required fields must be set.
bool Message::SerializeToOstream(
ostream * output) constSerialize the message and write it to the given C++ ostream.
All required fields must be set.
string Message::SerializeAsString() constMake a string encoding the message.
Is equivalent to calling SerializeToString() on a string and using that. Returns the empty string if SerializeToString() would have returned an error. Note: If you intend to generate many such strings, you may reduce heap fragmentation by instead re-using the same string object with calls to SerializeToString().
bool Message::AppendToString(
string * output) constLike SerializeToString(), but appends to the data to the string's existing contents.
All required fields must be set.
virtual int Message::ByteSize() constComputes the serialized size of the message.
This recursively calls ByteSize() on all embedded messages. If a subclass does not override this, it MUST override SetCachedSize().
virtual void Message::SerializeWithCachedSizes(
io::CodedOutputStream * output) constSerializes the message without recomputing the size.
The message must not have changed since the last call to ByteSize(); if it has, the results are undefined.
virtual uint8 * Message::SerializeWithCachedSizesToArray(
uint8 * target) constLike SerializeWithCachedSizes, but writes directly to *target, returning a pointer to the byte immediately after the last byte written.
"target" must point at a byte array of at least ByteSize() bytes.
virtual int Message::GetCachedSize() const = 0Returns the result of the last call to ByteSize().
An embedded message's size is needed both to serialize it (because embedded messages are length-delimited) and to compute the outer message's size. Caching the size avoids computing it multiple times.
ByteSize() does not automatically use the cached size when available because this would require invalidating it every time the message was modified, which would be too hard and expensive. (E.g. if a deeply-nested sub-message is changed, all of its parents' cached sizes would need to be invalidated, which is too much work for an otherwise inlined setter method.)
#include <google/protobuf/message.h>
namespace google::protobuf
This interface contains methods that can be used to dynamically access and modify the fields of a protocol message.
Their semantics are similar to the accessors the protocol compiler generates.
To get the Reflection for a given Message, call Message::GetReflection().
This interface is separate from Message only for efficiency reasons; the vast majority of implementations of Message will share the same implementation of Reflection (GeneratedMessageReflection, defined in generated_message.h), and all Messages of a particular class should share the same Reflection object (though you should not rely on the latter fact).
There are several ways that these methods can be used incorrectly. For example, any of the following conditions will lead to undefined results (probably assertion failures):
You might wonder why there is not any abstract representation for a field of arbitrary type. E.g., why isn't there just a "GetField()" method that returns "const Field&", where "Field" is some class with accessors like "GetInt32Value()". The problem is that someone would have to deal with allocating these Field objects. For generated message classes, having to allocate space for an additional object to wrap every field would at least double the message's memory footprint, probably worse. Allocating the objects on-demand, on the other hand, would be expensive and prone to memory leaks. So, instead we ended up with this flat interface.
TODO(kenton): Create a utility class which callers can use to read and write fields from a Reflection without paying attention to the type.
Members | |
|---|---|
| Reflection()TODO(kenton): Remove parameter. |
virtual | ~Reflection() |
virtual const UnknownFieldSet & | GetUnknownFields(const Message & message) const = 0Get the UnknownFieldSet for the message. more... |
virtual UnknownFieldSet * | MutableUnknownFields(Message * message) const = 0Get a mutable pointer to the UnknownFieldSet for the message. more... |
virtual int | SpaceUsed(const Message & message) const = 0Estimate the amount of memory used by the message object. |
virtual bool | HasField(const Message & message, const FieldDescriptor * field) const = 0Check if the given non-repeated field is set. |
virtual int | FieldSize(const Message & message, const FieldDescriptor * field) const = 0Get the number of elements of a repeated field. |
virtual void | ClearField(Message * message, const FieldDescriptor * field) const = 0Clear the value of a field, so that HasField() returns false or FieldSize() returns zero. |
virtual void | ListFields(const Message & message, vector< const FieldDescriptor * > * output) const = 0List all fields of the message which are currently set. more... |
Singular field gettersThese get the value of a non-repeated field. They return the default value for fields that aren't set. | |
virtual int32 | GetInt32(const Message & message, const FieldDescriptor * field) const = 0 |
virtual int64 | GetInt64(const Message & message, const FieldDescriptor * field) const = 0 |
virtual uint32 | GetUInt32(const Message & message, const FieldDescriptor * field) const = 0 |
virtual uint64 | GetUInt64(const Message & message, const FieldDescriptor * field) const = 0 |
virtual float | GetFloat(const Message & message, const FieldDescriptor * field) const = 0 |
virtual double | GetDouble(const Message & message, const FieldDescriptor * field) const = 0 |
virtual bool | GetBool(const Message & message, const FieldDescriptor * field) const = 0 |
virtual string | GetString(const Message & message, const FieldDescriptor * field) const = 0 |
virtual const EnumValueDescriptor * | GetEnum(const Message & message, const FieldDescriptor * field) const = 0 |
virtual const Message & | GetMessage(const Message & message, const FieldDescriptor * field) const = 0 |
virtual const string & | GetStringReference(const Message & message, const FieldDescriptor * field, string * scratch) const = 0Get a string value without copying, if possible. more... |
Singular field mutatorsThese mutate the value of a non-repeated field. | |
virtual void | SetInt32(Message * message, const FieldDescriptor * field, int32 value) const = 0 |
virtual void | SetInt64(Message * message, const FieldDescriptor * field, int64 value) const = 0 |
virtual void | SetUInt32(Message * message, const FieldDescriptor * field, uint32 value) const = 0 |
virtual void | SetUInt64(Message * message, const FieldDescriptor * field, uint64 value) const = 0 |
virtual void | SetFloat(Message * message, const FieldDescriptor * field, float value) const = 0 |
virtual void | SetDouble(Message * message, const FieldDescriptor * field, double value) const = 0 |
virtual void | SetBool(Message * message, const FieldDescriptor * field, bool value) const = 0 |
virtual void | SetString(Message * message, const FieldDescriptor * field, const string & value) const = 0 |
virtual void | SetEnum(Message * message, const FieldDescriptor * field, const EnumValueDescriptor * value) const = 0 |
virtual Message * | MutableMessage(Message * message, const FieldDescriptor * field) const = 0Get a mutable pointer to a field with a message type. |
Repeated field gettersThese get the value of one element of a repeated field. | |
virtual int32 | GetRepeatedInt32(const Message & message, const FieldDescriptor * field, int index) const = 0 |
virtual int64 | GetRepeatedInt64(const Message & message, const FieldDescriptor * field, int index) const = 0 |
virtual uint32 | GetRepeatedUInt32(const Message & message, const FieldDescriptor * field, int index) const = 0 |
virtual uint64 | GetRepeatedUInt64(const Message & message, const FieldDescriptor * field, int index) const = 0 |
virtual float | GetRepeatedFloat(const Message & message, const FieldDescriptor * field, int index) const = 0 |
virtual double | GetRepeatedDouble(const Message & message, const FieldDescriptor * field, int index) const = 0 |
virtual bool | GetRepeatedBool(const Message & message, const FieldDescriptor * field, int index) const = 0 |
virtual string | GetRepeatedString(const Message & message, const FieldDescriptor * field, int index) const = 0 |
virtual const EnumValueDescriptor * | GetRepeatedEnum(const Message & message, const FieldDescriptor * field, int index) const = 0 |
virtual const Message & | GetRepeatedMessage(const Message & message, const FieldDescriptor * field, int index) const = 0 |
virtual const string & | GetRepeatedStringReference(const Message & message, const FieldDescriptor * field, int index, string * scratch) const = 0See GetStringReference(), above. |
Repeated field mutatorsThese mutate the value of one element of a repeated field. | |
virtual void | SetRepeatedInt32(Message * message, const FieldDescriptor * field, int index, int32 value) const = 0 |
virtual void | SetRepeatedInt64(Message * message, const FieldDescriptor * field, int index, int64 value) const = 0 |
virtual void | SetRepeatedUInt32(Message * message, const FieldDescriptor * field, int index, uint32 value) const = 0 |
virtual void | SetRepeatedUInt64(Message * message, const FieldDescriptor * field, int index, uint64 value) const = 0 |
virtual void | SetRepeatedFloat(Message * message, const FieldDescriptor * field, int index, float value) const = 0 |
virtual void | SetRepeatedDouble(Message * message, const FieldDescriptor * field, int index, double value) const = 0 |
virtual void | SetRepeatedBool(Message * message, const FieldDescriptor * field, int index, bool value) const = 0 |
virtual void | SetRepeatedString(Message * message, const FieldDescriptor * field, int index, const string & value) const = 0 |
virtual void | SetRepeatedEnum(Message * message, const FieldDescriptor * field, int index, const EnumValueDescriptor * value) const = 0 |
virtual Message * | MutableRepeatedMessage(Message * message, const FieldDescriptor * field, int index) const = 0Get a mutable pointer to an element of a repeated field with a message type. |
Repeated field addersThese add an element to a repeated field. | |
virtual void | AddInt32(Message * message, const FieldDescriptor * field, int32 value) const = 0 |
virtual void | AddInt64(Message * message, const FieldDescriptor * field, int64 value) const = 0 |
virtual void | AddUInt32(Message * message, const FieldDescriptor * field, uint32 value) const = 0 |
virtual void | AddUInt64(Message * message, const FieldDescriptor * field, uint64 value) const = 0 |
virtual void | AddFloat(Message * message, const FieldDescriptor * field, float value) const = 0 |
virtual void | AddDouble(Message * message, const FieldDescriptor * field, double value) const = 0 |
virtual void | AddBool(Message * message, const FieldDescriptor * field, bool value) const = 0 |
virtual void | AddString(Message * message, const FieldDescriptor * field, const string & value) const = 0 |
virtual void | AddEnum(Message * message, const FieldDescriptor * field, const EnumValueDescriptor * value) const = 0 |
virtual Message * | AddMessage(Message * message, const FieldDescriptor * field) const = 0 |
Extensions | |
virtual const FieldDescriptor * | FindKnownExtensionByName(const string & name) const = 0Try to find an extension of this message type by fully-qualified field name. more... |
virtual const FieldDescriptor * | FindKnownExtensionByNumber(int number) const = 0Try to find an extension of this message type by field number. more... |
virtual const UnknownFieldSet &
Reflection::GetUnknownFields(
const Message & message) const = 0Get the UnknownFieldSet for the message.
This contains fields which were seen when the Message was parsed but were not recognized according to the Message's definition.
virtual UnknownFieldSet *
Reflection::MutableUnknownFields(
Message * message) const = 0Get a mutable pointer to the UnknownFieldSet for the message.
This contains fields which were seen when the Message was parsed but were not recognized according to the Message's definition.
virtual void Reflection::ListFields(
const Message & message,
vector< const FieldDescriptor * > * output) const = 0List all fields of the message which are currently set.
This includes extensions. Singular fields will only be listed if HasField(field) would return true and repeated fields will only be listed if FieldSize(field) would return non-zero. Fields (both normal fields and extension fields) will be listed ordered by field number.
virtual const string & Reflection::GetStringReference(
const Message & message,
const FieldDescriptor * field,
string * scratch) const = 0Get a string value without copying, if possible.
GetString() necessarily returns a copy of the string. This can be inefficient when the string is already stored in a string object in the underlying message. GetStringReference() will return a reference to the underlying string in this case. Otherwise, it will copy the string into scratch and return that.
Note: It is perfectly reasonable and useful to write code like:
str = reflection->GetStringReference(field, &str);
This line would ensure that only one copy of the string is made regardless of the field's underlying representation. When initializing a newly-constructed string, though, it's just as fast and more readable to use code like:
string str = reflection->GetString(field);
virtual const FieldDescriptor *
Reflection::FindKnownExtensionByName(
const string & name) const = 0Try to find an extension of this message type by fully-qualified field name.
Returns NULL if no extension is known for this name or number.
virtual const FieldDescriptor *
Reflection::FindKnownExtensionByNumber(
int number) const = 0Try to find an extension of this message type by field number.
Returns NULL if no extension is known for this name or number.
#include <google/protobuf/message.h>
namespace google::protobuf
Abstract interface for a factory for message objects.
Known subclasses:
Members | |
|---|---|
| MessageFactory() |
virtual | ~MessageFactory() |
virtual const Message * | GetPrototype(const Descriptor * type) = 0 |
static MessageFactory * | generated_factory()Gets a MessageFactory which supports all generated, compiled-in messages. more... |
static void | InternalRegisterGeneratedFile(const char * filename, void(*)() register_messages)For internal use only: Registers a .proto file at static initialization time, to be placed in generated_factory. more... |
static void | InternalRegisterGeneratedMessage(const Descriptor * descriptor, const Message * prototype)For internal use only: Registers a message type. more... |
virtual const Message * MessageFactory::GetPrototype(
const Descriptor * type) = 0Given a Descriptor, gets or 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. Also, any objects created by calling the prototype's New() method share some data with the prototype, so these must be destoyed before the MessageFactory is destroyed.
The given descriptor must outlive the returned message, and hence must outlive the MessageFactory.
Some implementations do not support all types. GetPrototype() will return NULL if the descriptor passed in is not supported.
This method may or may not be thread-safe depending on the implementation. Each implementation should document its own degree thread-safety.
static MessageFactory * MessageFactory::generated_factory()Gets a MessageFactory which supports all generated, compiled-in messages.
In other words, for any compiled-in type FooMessage, the following is true:
MessageFactory::generated_factory()->GetPrototype( FooMessage::descriptor()) == FooMessage::default_instance()
This factory supports all types which are found in DescriptorPool::generated_pool(). If given a descriptor from any other pool, GetPrototype() will return NULL. (You can also check if a descriptor is for a generated message by checking if descriptor->file()->pool() == DescriptorPool::generated_pool().)
This factory is 100% thread-safe; calling GetPrototype() does not modify any shared data.
This factory is a singleton. The caller must not delete the object.
static void MessageFactory::InternalRegisterGeneratedFile(
const char * filename,
void(*)() register_messages)For internal use only: Registers a .proto file at static initialization time, to be placed in generated_factory.
The first time GetPrototype() is called with a descriptor from this file, |register_messages| will be called. It must call InternalRegisterGeneratedMessage() (below) to register each message type in the file. This strange mechanism is necessary because descriptors are built lazily, so we can't register types by their descriptor until we know that the descriptor exists.
static void MessageFactory::InternalRegisterGeneratedMessage(
const Descriptor * descriptor,
const Message * prototype)For internal use only: Registers a message type.
Called only by the functions which are registered with InternalRegisterGeneratedFile(), above.