aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/message.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/message.h')
-rw-r--r--src/google/protobuf/message.h130
1 files changed, 78 insertions, 52 deletions
diff --git a/src/google/protobuf/message.h b/src/google/protobuf/message.h
index 9705e97e..f3d1a58a 100644
--- a/src/google/protobuf/message.h
+++ b/src/google/protobuf/message.h
@@ -112,7 +112,7 @@
#include <iosfwd>
#include <string>
-#include <google/protobuf/stubs/type_traits.h>
+#include <type_traits>
#include <vector>
#include <google/protobuf/arena.h>
@@ -152,7 +152,17 @@ class CodedOutputStream; // coded_stream.h
namespace python {
class MapReflectionFriend; // scalar_map_container.h
}
+namespace expr {
+class CelMapReflectionFriend; // field_backed_map_impl.cc
+}
+
+namespace internal {
+class ReflectionOps; // reflection_ops.h
+class MapKeySorter; // wire_format.cc
+class WireFormat; // wire_format.h
+class MapFieldReflectionTest; // map_test.cc
+}
template<typename T>
class RepeatedField; // repeated_field.h
@@ -245,7 +255,10 @@ class LIBPROTOBUF_EXPORT Message : public MessageLite {
// using reflection (rather than the generated code implementation for
// ByteSize()). Like ByteSize(), its CPU time is linear in the number of
// fields defined for the proto.
- virtual int SpaceUsed() const;
+ virtual size_t SpaceUsedLong() const;
+
+ PROTOBUF_RUNTIME_DEPRECATED("Please use SpaceUsedLong() instead")
+ int SpaceUsed() const { return internal::ToIntSize(SpaceUsedLong()); }
// Debugging & Testing----------------------------------------------
@@ -271,10 +284,10 @@ class LIBPROTOBUF_EXPORT Message : public MessageLite {
bool ParsePartialFromFileDescriptor(int file_descriptor);
// Parse a protocol buffer from a C++ istream. If successful, the entire
// input will be consumed.
- bool ParseFromIstream(istream* input);
+ bool ParseFromIstream(std::istream* input);
// Like ParseFromIstream(), but accepts messages that are missing
// required fields.
- bool ParsePartialFromIstream(istream* input);
+ bool ParsePartialFromIstream(std::istream* input);
// Serialize the message and write it to the given file descriptor. All
// required fields must be set.
@@ -283,9 +296,9 @@ class LIBPROTOBUF_EXPORT Message : public MessageLite {
bool SerializePartialToFileDescriptor(int file_descriptor) const;
// Serialize the message and write it to the given C++ ostream. All
// required fields must be set.
- bool SerializeToOstream(ostream* output) const;
+ bool SerializeToOstream(std::ostream* output) const;
// Like SerializeToOstream(), but allows missing required fields.
- bool SerializePartialToOstream(ostream* output) const;
+ bool SerializePartialToOstream(std::ostream* output) const;
// Reflection-based methods ----------------------------------------
@@ -297,7 +310,7 @@ class LIBPROTOBUF_EXPORT Message : public MessageLite {
virtual bool IsInitialized() const;
virtual void CheckTypeAndMergeFrom(const MessageLite& other);
virtual bool MergePartialFromCodedStream(io::CodedInputStream* input);
- virtual int ByteSize() const;
+ virtual size_t ByteSizeLong() const;
virtual void SerializeWithCachedSizes(io::CodedOutputStream* output) const;
private:
@@ -317,18 +330,19 @@ class LIBPROTOBUF_EXPORT Message : public MessageLite {
// Typedef for backwards-compatibility.
typedef google::protobuf::Reflection Reflection;
- // Get a Descriptor for this message's type. This describes what
- // fields the message contains, the types of those fields, etc.
+ // Get a non-owning pointer to a Descriptor for this message's type. This
+ // describes what fields the message contains, the types of those fields, etc.
+ // This object remains property of the Message.
const Descriptor* GetDescriptor() const { return GetMetadata().descriptor; }
- // Get 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.
+ // Get a non-owning pointer to 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.
//
// This method remains virtual in case a subclass does not implement
// reflection and wants to override the default behavior.
- virtual const Reflection* GetReflection() const {
+ virtual const Reflection* GetReflection() const final {
return GetMetadata().reflection;
}
@@ -395,9 +409,6 @@ class MutableRepeatedFieldRef;
// 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.
class LIBPROTOBUF_EXPORT Reflection {
public:
inline Reflection() {}
@@ -417,7 +428,12 @@ class LIBPROTOBUF_EXPORT Reflection {
virtual UnknownFieldSet* MutableUnknownFields(Message* message) const = 0;
// Estimate the amount of memory used by the message object.
- virtual int SpaceUsed(const Message& message) const = 0;
+ virtual size_t SpaceUsedLong(const Message& message) const = 0;
+
+ PROTOBUF_RUNTIME_DEPRECATED("Please use SpaceUsedLong() instead")
+ int SpaceUsed(const Message& message) const {
+ return internal::ToIntSize(SpaceUsedLong(message));
+ }
// Check if the given non-repeated field is set.
virtual bool HasField(const Message& message,
@@ -481,11 +497,14 @@ class LIBPROTOBUF_EXPORT Reflection {
int index1,
int index2) const = 0;
- // List 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.
+ // 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.
virtual void ListFields(
const Message& message,
std::vector<const FieldDescriptor*>* output) const = 0;
@@ -519,7 +538,7 @@ class LIBPROTOBUF_EXPORT Reflection {
// will only be present when the new unknown-enum-value semantics are enabled
// for a message.)
virtual int GetEnumValue(
- const Message& message, const FieldDescriptor* field) const;
+ const Message& message, const FieldDescriptor* field) const = 0;
// See MutableMessage() for the meaning of the "factory" parameter.
virtual const Message& GetMessage(const Message& message,
@@ -575,7 +594,7 @@ class LIBPROTOBUF_EXPORT Reflection {
// messages with new unknown-enum-value semantics.
virtual void SetEnumValue(Message* message,
const FieldDescriptor* field,
- int value) const;
+ int value) const = 0;
// 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;
@@ -646,7 +665,7 @@ class LIBPROTOBUF_EXPORT Reflection {
// for a message.)
virtual int GetRepeatedEnumValue(
const Message& message,
- const FieldDescriptor* field, int index) const;
+ const FieldDescriptor* field, int index) const = 0;
virtual const Message& GetRepeatedMessage(
const Message& message,
const FieldDescriptor* field, int index) const = 0;
@@ -693,7 +712,7 @@ class LIBPROTOBUF_EXPORT Reflection {
// messages with new unknown-enum-value semantics.
virtual void SetRepeatedEnumValue(Message* message,
const FieldDescriptor* field, int index,
- int value) const;
+ int value) const = 0;
// Get a mutable pointer to an element of a repeated field with a message
// type.
virtual Message* MutableRepeatedMessage(
@@ -729,7 +748,7 @@ class LIBPROTOBUF_EXPORT Reflection {
// messages with new unknown-enum-value semantics.
virtual void AddEnumValue(Message* message,
const FieldDescriptor* field,
- int value) const;
+ int value) const = 0;
// See MutableMessage() for comments on the "factory" parameter.
virtual Message* AddMessage(Message* message,
const FieldDescriptor* field,
@@ -739,9 +758,9 @@ class LIBPROTOBUF_EXPORT Reflection {
// specifyed by 'field' passing ownership to the message.
// TODO(tmarek): Make virtual after all subclasses have been
// updated.
- virtual void AddAllocatedMessage(Message* /* message */,
- const FieldDescriptor* /*field */,
- Message* /* new_entry */) const {}
+ virtual void AddAllocatedMessage(Message* message,
+ const FieldDescriptor* field,
+ Message* new_entry) const;
// Get a RepeatedFieldRef object that can be used to read the underlying
@@ -796,6 +815,7 @@ class LIBPROTOBUF_EXPORT Reflection {
//
// for T = Cord and all protobuf scalar types except enums.
template<typename T>
+ PROTOBUF_RUNTIME_DEPRECATED("Please use GetRepeatedFieldRef() instead")
const RepeatedField<T>& GetRepeatedField(
const Message&, const FieldDescriptor*) const;
@@ -803,6 +823,7 @@ class LIBPROTOBUF_EXPORT Reflection {
//
// for T = Cord and all protobuf scalar types except enums.
template<typename T>
+ PROTOBUF_RUNTIME_DEPRECATED("Please use GetMutableRepeatedFieldRef() instead")
RepeatedField<T>* MutableRepeatedField(
Message*, const FieldDescriptor*) const;
@@ -811,6 +832,7 @@ class LIBPROTOBUF_EXPORT Reflection {
// for T = string, google::protobuf::internal::StringPieceField
// google::protobuf::Message & descendants.
template<typename T>
+ PROTOBUF_RUNTIME_DEPRECATED("Please use GetRepeatedFieldRef() instead")
const RepeatedPtrField<T>& GetRepeatedPtrField(
const Message&, const FieldDescriptor*) const;
@@ -819,6 +841,7 @@ class LIBPROTOBUF_EXPORT Reflection {
// for T = string, google::protobuf::internal::StringPieceField
// google::protobuf::Message & descendants.
template<typename T>
+ PROTOBUF_RUNTIME_DEPRECATED("Please use GetMutableRepeatedFieldRef() instead")
RepeatedPtrField<T>* MutableRepeatedPtrField(
Message*, const FieldDescriptor*) const;
@@ -848,34 +871,31 @@ class LIBPROTOBUF_EXPORT Reflection {
// downgrade to a compatible value or use the UnknownFieldSet if not. For
// example:
//
- // int new_value = GetValueFromApplicationLogic();
- // if (reflection->SupportsUnknownEnumValues()) {
+ // int new_value = GetValueFromApplicationLogic();
+ // if (reflection->SupportsUnknownEnumValues()) {
// reflection->SetEnumValue(message, field, new_value);
- // } else {
+ // } else {
// if (field_descriptor->enum_type()->
- // FindValueByNumver(new_value) != NULL) {
- // reflection->SetEnumValue(message, field, new_value);
+ // FindValueByNumber(new_value) != NULL) {
+ // reflection->SetEnumValue(message, field, new_value);
// } else if (emit_unknown_enum_values) {
- // reflection->MutableUnknownFields(message)->AddVarint(
- // field->number(),
- // new_value);
+ // reflection->MutableUnknownFields(message)->AddVarint(
+ // field->number(), new_value);
// } else {
- // // convert value to a compatible/default value.
- // new_value = CompatibleDowngrade(new_value);
- // reflection->SetEnumValue(message, field, new_value);
+ // // convert value to a compatible/default value.
+ // new_value = CompatibleDowngrade(new_value);
+ // reflection->SetEnumValue(message, field, new_value);
// }
- // }
+ // }
virtual bool SupportsUnknownEnumValues() const { return false; }
// 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.
- // }
- //
+ // 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.
virtual MessageFactory* GetMessageFactory() const;
@@ -910,7 +930,7 @@ class LIBPROTOBUF_EXPORT Reflection {
// Returns a raw pointer to the repeated field
//
- // "cpp_type" and "message_type" are decuded from the type parameter T passed
+ // "cpp_type" and "message_type" are deduced from the type parameter T passed
// to Get(Mutable)RepeatedFieldRef. If T is a generated message type,
// "message_type" should be set to its descriptor. Otherwise "message_type"
// should be set to NULL. Implementations of this method should check whether
@@ -933,6 +953,12 @@ class LIBPROTOBUF_EXPORT Reflection {
template<typename T, typename Enable>
friend class MutableRepeatedFieldRef;
friend class ::google::protobuf::python::MapReflectionFriend;
+#define GOOGLE_PROTOBUF_HAS_CEL_MAP_REFLECTION_FRIEND
+ friend class ::google::protobuf::expr::CelMapReflectionFriend;
+ friend class internal::MapFieldReflectionTest;
+ friend class internal::MapKeySorter;
+ friend class internal::WireFormat;
+ friend class internal::ReflectionOps;
// Special version for specialized implementations of string. We can't call
// MutableRawRepeatedField directly here because we don't have access to
@@ -947,7 +973,7 @@ class LIBPROTOBUF_EXPORT Reflection {
// TODO(jieluo) - make the map APIs pure virtual after updating
// all the subclasses.
// Returns true if key is in map. Returns false if key is not in map field.
- virtual bool ContainsMapKey(const Message& /* message*/,
+ virtual bool ContainsMapKey(const Message& /* message */,
const FieldDescriptor* /* field */,
const MapKey& /* key */) const {
return false;
@@ -965,7 +991,7 @@ class LIBPROTOBUF_EXPORT Reflection {
// Delete and returns true if key is in the map field. Returns false
// otherwise.
- virtual bool DeleteMapValue(Message* /* mesage */,
+ virtual bool DeleteMapValue(Message* /* message */,
const FieldDescriptor* /* field */,
const MapKey& /* key */) const {
return false;