class Reflection

Defined at line 457 of file ../../third_party/protobuf/src/google/protobuf/message.h

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):

- The FieldDescriptor is not a field of this message type.

- The method called is not appropriate for the field's type. For

each field type in FieldDescriptor::TYPE_*, there is only one

Get*() method, one Set*() method, and one Add*() method that is

valid for that type. It should be obvious which (except maybe

for TYPE_BYTES, which are represented using strings in C++).

- A Get*() or Set*() method for singular fields is called on a repeated

field.

- GetRepeated*(), SetRepeated*(), or Add*() is called on a non-repeated

field.

- The Message object passed to any method is not of the right type for

this Reflection object (i.e. message.GetReflection() != reflection).

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.

Public Methods

const UnknownFieldSet & GetUnknownFields (const Message & message)

Get 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.

UnknownFieldSet * MutableUnknownFields (Message * message)

Get 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.

bool HasField (const Message & message, const FieldDescriptor * field)

Check if the given non-repeated field is set.

int FieldSize (const Message & message, const FieldDescriptor * field)

Get the number of elements of a repeated field.

void ClearField (Message * message, const FieldDescriptor * field)

Clear the value of a field, so that HasField() returns false or

FieldSize() returns zero.

bool HasOneof (const Message & message, const OneofDescriptor * oneof_descriptor)

Check if the oneof is set. Returns true if any field in oneof

is set, false otherwise.

void ClearOneof (Message * message, const OneofDescriptor * oneof_descriptor)
const FieldDescriptor * GetOneofFieldDescriptor (const Message & message, const OneofDescriptor * oneof_descriptor)

Returns the field descriptor if the oneof is set. nullptr otherwise.

void ListFields (const Message & message, std::vector<const FieldDescriptor *> * output)

List all fields of the message which are currently set, except for unknown

fields, but including extension known to the parser (i.e. compiled in).

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.

Use Reflection::GetUnknownFields() or message.unknown_fields() to also get

access to fields/extensions unknown to the parser.

int32_t GetInt32 (const Message & message, const FieldDescriptor * field)

Singular field getters ------------------------------------------

These get the value of a non-repeated field. They return the default

value for fields that aren't set.

int64_t GetInt64 (const Message & message, const FieldDescriptor * field)
uint32_t GetUInt32 (const Message & message, const FieldDescriptor * field)
uint64_t GetUInt64 (const Message & message, const FieldDescriptor * field)
float GetFloat (const Message & message, const FieldDescriptor * field)
double GetDouble (const Message & message, const FieldDescriptor * field)
bool GetBool (const Message & message, const FieldDescriptor * field)
std::string GetString (const Message & message, const FieldDescriptor * field)
const EnumValueDescriptor * GetEnum (const Message & message, const FieldDescriptor * field)
int GetEnumValue (const Message & message, const FieldDescriptor * field)

GetEnumValue() returns an enum field's value as an integer rather than

an EnumValueDescriptor*. If the integer value does not correspond to a

known value descriptor, a new value descriptor is created. (Such a value

will only be present when the new unknown-enum-value semantics are enabled

for a message.)

const Message & GetMessage (const Message & message, const FieldDescriptor * field, MessageFactory * factory)

See MutableMessage() for the meaning of the "factory" parameter.

const std::string & GetStringReference (const Message & message, const FieldDescriptor * field, std::string * scratch)

Get a string value without copying, if possible.

GetString() necessarily returns a copy of the string. This can be

inefficient when the std::string is already stored in a std::string object

in the underlying message. GetStringReference() will return a reference to

the underlying std::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(message, 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:

std::string str = reflection->GetString(message, field);

absl::Cord GetCord (const Message & message, const FieldDescriptor * field)

Returns a Cord containing the value of the string field. If the

underlying field is stored as a cord (e.g. it has the [ctype=CORD]

option), this involves no copies (just reference counting). If the

underlying representation is not a Cord, a copy will have to be made.

absl::string_view GetStringView (const Message & message, const FieldDescriptor * field, ScratchSpace & scratch)

Returns a view into the contents of a string field. "scratch" is used to

flatten bytes if it is non-contiguous. The lifetime of absl::string_view is

either tied to "message" (contiguous) or "scratch" (otherwise).

Message * MutableMessage (Message * message, const FieldDescriptor * field, MessageFactory * factory)

Get a mutable pointer to a field with a message type. If a MessageFactory

is provided, it will be used to construct instances of the sub-message;

otherwise, the default factory is used. If the field is an extension that

does not live in the same pool as the containing message's descriptor (e.g.

it lives in an overlay pool), then a MessageFactory must be provided.

If you have no idea what that meant, then you probably don't need to worry

about it (don't provide a MessageFactory). WARNING: If the

FieldDescriptor is for a compiled-in extension, then

factory->GetPrototype(field->message_type()) MUST return an instance of

the compiled-in class for this type, NOT DynamicMessage.

int32_t GetRepeatedInt32 (const Message & message, const FieldDescriptor * field, int index)

Repeated field getters ------------------------------------------

These get the value of one element of a repeated field.

int64_t GetRepeatedInt64 (const Message & message, const FieldDescriptor * field, int index)
uint32_t GetRepeatedUInt32 (const Message & message, const FieldDescriptor * field, int index)
uint64_t GetRepeatedUInt64 (const Message & message, const FieldDescriptor * field, int index)
float GetRepeatedFloat (const Message & message, const FieldDescriptor * field, int index)
double GetRepeatedDouble (const Message & message, const FieldDescriptor * field, int index)
bool GetRepeatedBool (const Message & message, const FieldDescriptor * field, int index)
std::string GetRepeatedString (const Message & message, const FieldDescriptor * field, int index)
const EnumValueDescriptor * GetRepeatedEnum (const Message & message, const FieldDescriptor * field, int index)
int GetRepeatedEnumValue (const Message & message, const FieldDescriptor * field, int index)

GetRepeatedEnumValue() returns an enum field's value as an integer rather

than an EnumValueDescriptor*. If the integer value does not correspond to a

known value descriptor, a new value descriptor is created. (Such a value

will only be present when the new unknown-enum-value semantics are enabled

for a message.)

const Message & GetRepeatedMessage (const Message & message, const FieldDescriptor * field, int index)
const std::string & GetRepeatedStringReference (const Message & message, const FieldDescriptor * field, int index, std::string * scratch)

See GetStringReference(), above.

absl::string_view GetRepeatedStringView (const Message & message, const FieldDescriptor * field, int index, ScratchSpace & scratch)

See GetStringView(), above.

Message * MutableRepeatedMessage (Message * message, const FieldDescriptor * field, int index)

Get a mutable pointer to an element of a repeated field with a message

type.

void AddInt32 (Message * message, const FieldDescriptor * field, int32_t value)

Repeated field adders -------------------------------------------

These add an element to a repeated field.

void AddInt64 (Message * message, const FieldDescriptor * field, int64_t value)
void AddUInt32 (Message * message, const FieldDescriptor * field, uint32_t value)
void AddUInt64 (Message * message, const FieldDescriptor * field, uint64_t value)
void AddFloat (Message * message, const FieldDescriptor * field, float value)
void AddDouble (Message * message, const FieldDescriptor * field, double value)
void AddBool (Message * message, const FieldDescriptor * field, bool value)
void AddString (Message * message, const FieldDescriptor * field, std::string value)
void AddEnum (Message * message, const FieldDescriptor * field, const EnumValueDescriptor * value)
void AddEnumValue (Message * message, const FieldDescriptor * field, int value)

Add an integer value to a repeated enum field rather than

EnumValueDescriptor. For proto3 this is just setting the enum field to the

value specified, for proto2 it's more complicated. If value is a known enum

value the field is set as usual. If the value is unknown then it is added

to the unknown field set. Note this matches the behavior of parsing unknown

enum values. If multiple calls with unknown values happen than they are all

added to the unknown field set in order of the calls.

Message * AddMessage (Message * message, const FieldDescriptor * field, MessageFactory * factory)

See MutableMessage() for comments on the "factory" parameter.

void AddAllocatedMessage (Message * message, const FieldDescriptor * field, Message * new_entry)

Appends an already-allocated object 'new_entry' to the repeated field

specified by 'field' passing ownership to the message.

const FieldDescriptor * FindKnownExtensionByName (absl::string_view name)

Try to find an extension of this message type by fully-qualified field

name. Returns nullptr if no extension is known for this name or number.

const FieldDescriptor * FindKnownExtensionByNumber (int number)

Try to find an extension of this message type by field number.

Returns nullptr if no extension is known for this name or number.

MessageFactory * GetMessageFactory ()

Returns the MessageFactory associated with this message. This can be

useful for determining if a message is a generated message or not, for

example:

if (message->GetReflection()->GetMessageFactory() ==

google::protobuf::MessageFactory::generated_factory()) {

// This is a generated message.

}

It can also be used to create more messages of this type, though

Message::New() is an easier way to accomplish this.

void Reflection (const Reflection & )

Defined at line 459 of file ../../third_party/protobuf/src/google/protobuf/message.h

bool IsDefaultInstance (const Message & message)

Returns true if the given message is a default message instance.

Defined at line 481 of file ../../third_party/protobuf/src/google/protobuf/message.h

template <typename T>
const RepeatedField<T> & GetRepeatedField (const Message & msg, const FieldDescriptor * d)

DEPRECATED. Please use GetRepeatedFieldRef().

for T = Cord and all protobuf scalar types except enums.

Defined at line 912 of file ../../third_party/protobuf/src/google/protobuf/message.h

template <typename T>
RepeatedField<T> * MutableRepeatedField (Message * msg, const FieldDescriptor * d)

DEPRECATED. Please use GetMutableRepeatedFieldRef().

for T = Cord and all protobuf scalar types except enums.

Defined at line 922 of file ../../third_party/protobuf/src/google/protobuf/message.h

template <typename T>
const RepeatedPtrField<T> & GetRepeatedPtrField (const Message & msg, const FieldDescriptor * d)

DEPRECATED. Please use GetRepeatedFieldRef().

for T = std::string, google::protobuf::internal::StringPieceField

google::protobuf::Message

&

descendants.

Defined at line 933 of file ../../third_party/protobuf/src/google/protobuf/message.h

template <typename T>
RepeatedPtrField<T> * MutableRepeatedPtrField (Message * msg, const FieldDescriptor * d)

DEPRECATED. Please use GetMutableRepeatedFieldRef().

for T = std::string, google::protobuf::internal::StringPieceField

google::protobuf::Message

&

descendants.

Defined at line 944 of file ../../third_party/protobuf/src/google/protobuf/message.h

void ~Reflection ()
size_t SpaceUsedLong (const Message & message)

Estimate the amount of memory used by the message object.

void RemoveLast (Message * message, const FieldDescriptor * field)

Removes the last element of a repeated field.

We don't provide a way to remove any element other than the last

because it invites inefficient use, such as O(n^2) filtering loops

that should have been O(n). If you want to remove an element other

than the last, the best way to do it is to re-arrange the elements

(using Swap()) so that the one you want removed is at the end, then

call RemoveLast().

Message * ReleaseLast (Message * message, const FieldDescriptor * field)

Removes the last element of a repeated message field, and returns the

pointer to the caller. Caller takes ownership of the returned pointer.

Message * UnsafeArenaReleaseLast (Message * message, const FieldDescriptor * field)

Similar to ReleaseLast() without internal safety and ownershp checks. This

method should only be used when the objects are on the same arena or paired

with a call to `UnsafeArenaAddAllocatedMessage`.

void Swap (Message * message1, Message * message2)

Swap the complete contents of two messages.

void SwapFields (Message * message1, Message * message2, const std::vector<const FieldDescriptor *> & fields)

Swap fields listed in fields vector of two messages.

void SwapElements (Message * message, const FieldDescriptor * field, int index1, int index2)

Swap two elements of a repeated field.

void UnsafeArenaSwap (Message * lhs, Message * rhs)

Swap without internal safety and ownership checks. This method should only

be used when the objects are on the same arena.

void UnsafeArenaSwapFields (Message * lhs, Message * rhs, const std::vector<const FieldDescriptor *> & fields)

SwapFields without internal safety and ownership checks. This method should

only be used when the objects are on the same arena.

void SetInt32 (Message * message, const FieldDescriptor * field, int32_t value)

Singular field mutators -----------------------------------------

These mutate the value of a non-repeated field.

void SetInt64 (Message * message, const FieldDescriptor * field, int64_t value)
void SetUInt32 (Message * message, const FieldDescriptor * field, uint32_t value)
void SetUInt64 (Message * message, const FieldDescriptor * field, uint64_t value)
void SetFloat (Message * message, const FieldDescriptor * field, float value)
void SetDouble (Message * message, const FieldDescriptor * field, double value)
void SetBool (Message * message, const FieldDescriptor * field, bool value)
void SetString (Message * message, const FieldDescriptor * field, std::string value)
void SetString (Message * message, const FieldDescriptor * field, const absl::Cord & value)

Set a string field to a Cord value. If the underlying field is

represented using a Cord already, this involves no copies (just

reference counting). Otherwise, a copy must be made.

void SetEnum (Message * message, const FieldDescriptor * field, const EnumValueDescriptor * value)
void SetEnumValue (Message * message, const FieldDescriptor * field, int value)

Set an enum field's value with an integer rather than EnumValueDescriptor.

For proto3 this is just setting the enum field to the value specified, for

proto2 it's more complicated. If value is a known enum value the field is

set as usual. If the value is unknown then it is added to the unknown field

set. Note this matches the behavior of parsing unknown enum values.

If multiple calls with unknown values happen than they are all added to the

unknown field set in order of the calls.

void SetAllocatedMessage (Message * message, Message * sub_message, const FieldDescriptor * field)

Replaces the message specified by 'field' with the already-allocated object

sub_message, passing ownership to the message. If the field contained a

message, that message is deleted. If sub_message is nullptr, the field is

cleared.

void SetRepeatedInt32 (Message * message, const FieldDescriptor * field, int index, int32_t value)

Repeated field mutators -----------------------------------------

These mutate the value of one element of a repeated field.

void SetRepeatedInt64 (Message * message, const FieldDescriptor * field, int index, int64_t value)
void SetRepeatedUInt32 (Message * message, const FieldDescriptor * field, int index, uint32_t value)
void SetRepeatedUInt64 (Message * message, const FieldDescriptor * field, int index, uint64_t value)
void SetRepeatedFloat (Message * message, const FieldDescriptor * field, int index, float value)
void SetRepeatedDouble (Message * message, const FieldDescriptor * field, int index, double value)
void SetRepeatedBool (Message * message, const FieldDescriptor * field, int index, bool value)
void SetRepeatedString (Message * message, const FieldDescriptor * field, int index, std::string value)
void SetRepeatedEnum (Message * message, const FieldDescriptor * field, int index, const EnumValueDescriptor * value)
void SetRepeatedEnumValue (Message * message, const FieldDescriptor * field, int index, int value)

Set an enum field's value with an integer rather than EnumValueDescriptor.

For proto3 this is just setting the enum field to the value specified, for

proto2 it's more complicated. If value is a known enum value the field is

set as usual. If the value is unknown then it is added to the unknown field

set. Note this matches the behavior of parsing unknown enum values.

If multiple calls with unknown values happen than they are all added to the

unknown field set in order of the calls.

int SpaceUsed (const Message & message)

Defined at line 475 of file ../../third_party/protobuf/src/google/protobuf/message.h

void UnsafeArenaSetAllocatedMessage (Message * message, Message * sub_message, const FieldDescriptor * field)

Similar to `SetAllocatedMessage`, but omits all internal safety and

ownership checks. This method should only be used when the objects are on

the same arena or paired with a call to `UnsafeArenaReleaseMessage`.

Message * ReleaseMessage (Message * message, const FieldDescriptor * field, MessageFactory * factory)

Releases the message specified by 'field' and returns the pointer,

ReleaseMessage() will return the message the message object if it exists.

Otherwise, it may or may not return nullptr. In any case, if the return

value is non-null, the caller takes ownership of the pointer.

If the field existed (HasField() is true), then the returned pointer will

be the same as the pointer returned by MutableMessage().

This function has the same effect as ClearField().

Message * UnsafeArenaReleaseMessage (Message * message, const FieldDescriptor * field, MessageFactory * factory)

Similar to `ReleaseMessage`, but omits all internal safety and ownership

checks. This method should only be used when the objects are on the same

arena or paired with a call to `UnsafeArenaSetAllocatedMessage`.

void UnsafeArenaAddAllocatedMessage (Message * message, const FieldDescriptor * field, Message * new_entry)

Similar to AddAllocatedMessage() without internal safety and ownership

checks. This method should only be used when the objects are on the same

arena or paired with a call to `UnsafeArenaReleaseLast`.

Reflection & operator= (const Reflection & )

Defined at line 460 of file ../../third_party/protobuf/src/google/protobuf/message.h

template <typename T>
RepeatedFieldRef<T> GetRepeatedFieldRef (const Message & message, const FieldDescriptor * field)

Get a RepeatedFieldRef object that can be used to read the underlying

repeated field. The type parameter T must be set according to the

field's cpp type. The following table shows the mapping from cpp type

to acceptable T.

field->cpp_type() T

CPPTYPE_INT32 int32_t

CPPTYPE_UINT32 uint32_t

CPPTYPE_INT64 int64_t

CPPTYPE_UINT64 uint64_t

CPPTYPE_DOUBLE double

CPPTYPE_FLOAT float

CPPTYPE_BOOL bool

CPPTYPE_ENUM generated enum type or int32_t

CPPTYPE_STRING std::string

CPPTYPE_MESSAGE generated message type or google::protobuf::Message

A RepeatedFieldRef object can be copied and the resulted object will point

to the same repeated field in the same message. The object can be used as

long as the message is not destroyed.

Note that to use this method users need to include the header file

"reflection.h" (which defines the RepeatedFieldRef class templates).

Defined at line 1640 of file ../../third_party/protobuf/src/google/protobuf/message.h

template <typename T>
MutableRepeatedFieldRef<T> GetMutableRepeatedFieldRef (Message * message, const FieldDescriptor * field)

Like GetRepeatedFieldRef() but return an object that can also be used

manipulate the underlying repeated field.

Defined at line 1646 of file ../../third_party/protobuf/src/google/protobuf/message.h

Records

Friends

const char * Reflection (const FieldDescriptor * field, Message * msg, const Reflection * reflection, const char * ptr, internal::ParseContext * ctx)
const char * Reflection (int field_number, const FieldDescriptor * field, Message * msg, const Reflection * reflection, const char * ptr, internal::ParseContext * ctx)
class WireFormatForMapFieldTest
class MapIterator
class V2TableGenTester
class MapReflectionTester
class MapFieldPrinterHelper
class FuzzPeer
template <bool is_oneof>
class DynamicFieldInfoHelper
class SwapFieldHelper
class ReflectionOps
class WireFormat
class MessageUtil
class MapKeySorter
class MapFieldReflectionTest
class CelMapReflectionFriend
class MessageDifferencer
class MessageReflectionFriend
class MapReflectionFriend
class GeneratedMessageReflectionTestHelper
class DynamicMessageFactory
class AssignDescriptorsHelper
class MessageLayoutInspector
class Message
template <typename T, typename Enable>
class MutableRepeatedFieldRef
template <typename T, typename Enable>
class RepeatedFieldRef
void Reflection (Message * root)
bool Reflection (Message & root, const Message & message)
class ReflectionVisit
class FastReflectionMessageMutator
class FastReflectionBase