| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 #ifndef GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__ | 38 #ifndef GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__ |
| 39 #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__ | 39 #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__ |
| 40 | 40 |
| 41 #include <string> | 41 #include <string> |
| 42 #include <vector> | 42 #include <vector> |
| 43 #include <google/protobuf/stubs/casts.h> | 43 #include <google/protobuf/stubs/casts.h> |
| 44 #include <google/protobuf/stubs/common.h> | 44 #include <google/protobuf/stubs/common.h> |
| 45 // TODO(jasonh): Remove this once the compiler change to directly include this | 45 // TODO(jasonh): Remove this once the compiler change to directly include this |
| 46 // is released to components. | 46 // is released to components. |
| 47 #include <google/protobuf/generated_enum_reflection.h> | 47 #include <google/protobuf/generated_enum_reflection.h> |
| 48 #include <google/protobuf/generated_message_util.h> |
| 48 #include <google/protobuf/message.h> | 49 #include <google/protobuf/message.h> |
| 49 #include <google/protobuf/metadata.h> | 50 #include <google/protobuf/metadata.h> |
| 50 #include <google/protobuf/unknown_field_set.h> | 51 #include <google/protobuf/unknown_field_set.h> |
| 51 | 52 |
| 52 | 53 |
| 53 namespace google { | 54 namespace google { |
| 54 namespace upb { | 55 namespace upb { |
| 55 namespace google_opensource { | 56 namespace google_opensource { |
| 56 class GMR_Handlers; | 57 class GMR_Handlers; |
| 57 } // namespace google_opensource | 58 } // namespace google_opensource |
| 58 } // namespace upb | 59 } // namespace upb |
| 59 | 60 |
| 60 namespace protobuf { | 61 namespace protobuf { |
| 61 class DescriptorPool; | 62 class DescriptorPool; |
| 62 class MapKey; | 63 class MapKey; |
| 63 class MapValueRef; | 64 class MapValueRef; |
| 64 } | 65 } |
| 65 | 66 |
| 66 namespace protobuf { | 67 namespace protobuf { |
| 67 namespace internal { | 68 namespace internal { |
| 68 class DefaultEmptyOneof; | 69 class DefaultEmptyOneof; |
| 69 | 70 |
| 70 // Defined in this file. | 71 // Defined in this file. |
| 71 class GeneratedMessageReflection; | 72 class GeneratedMessageReflection; |
| 72 | 73 |
| 73 // Defined in other files. | 74 // Defined in other files. |
| 74 class ExtensionSet; // extension_set.h | 75 class ExtensionSet; // extension_set.h |
| 75 | 76 |
| 77 // This struct describes the internal layout of the message, hence this is |
| 78 // used to act on the message reflectively. |
| 79 // default_instance: The default instance of the message. This is only |
| 80 // used to obtain pointers to default instances of embedded |
| 81 // messages, which GetMessage() will return if the particular |
| 82 // sub-message has not been initialized yet. (Thus, all |
| 83 // embedded message fields *must* have non-NULL pointers |
| 84 // in the default instance.) |
| 85 // offsets: An array of ints giving the byte offsets. |
| 86 // For each oneof field, the offset is relative to the |
| 87 // default_oneof_instance. These can be computed at compile |
| 88 // time using the |
| 89 // PROTO2_GENERATED_DEFAULT_ONEOF_FIELD_OFFSET() macro. |
| 90 // For each none oneof field, the offset is related to |
| 91 // the start of the message object. These can be computed |
| 92 // at compile time using the |
| 93 // GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET() macro. |
| 94 // Besides offsets for all fields, this array also contains |
| 95 // offsets for oneof unions. The offset of the i-th oneof |
| 96 // union is offsets[descriptor->field_count() + i]. |
| 97 // has_bit_indices: Mapping from field indexes to their index in the has |
| 98 // bit array. |
| 99 // has_bits_offset: Offset in the message of an array of uint32s of size |
| 100 // descriptor->field_count()/32, rounded up. This is a |
| 101 // bitfield where each bit indicates whether or not the |
| 102 // corresponding field of the message has been initialized. |
| 103 // The bit for field index i is obtained by the expression: |
| 104 // has_bits[i / 32] & (1 << (i % 32)) |
| 105 // unknown_fields_offset: Offset in the message of the UnknownFieldSet for |
| 106 // the message. |
| 107 // extensions_offset: Offset in the message of the ExtensionSet for the |
| 108 // message, or -1 if the message type has no extension |
| 109 // ranges. |
| 110 // default_oneof_instance: The default instance of the oneofs. It is a |
| 111 // struct holding the default value of all oneof fields |
| 112 // for this message. It is only used to obtain pointers |
| 113 // to default instances of oneof fields, which Get |
| 114 // methods will return if the field is not set. |
| 115 // oneof_case_offset: Offset in the message of an array of uint32s of |
| 116 // size descriptor->oneof_decl_count(). Each uint32 |
| 117 // indicates what field is set for each oneof. |
| 118 // object_size: The size of a message object of this type, as measured |
| 119 // by sizeof(). |
| 120 // arena_offset: If a message doesn't have a unknown_field_set that stores |
| 121 // the arena, it must have a direct pointer to the arena. |
| 122 struct ReflectionSchema { |
| 123 public: |
| 124 // Size of a google::protobuf::Message object of this type. |
| 125 uint32 GetObjectSize() const { return object_size_; } |
| 126 |
| 127 // Offset of a non-oneof field. Getting a field offset is slightly more |
| 128 // efficient when we know statically that it is not a oneof field. |
| 129 uint32 GetFieldOffsetNonOneof(const FieldDescriptor* field) const { |
| 130 GOOGLE_DCHECK(!field->containing_oneof()); |
| 131 return offsets_[field->index()]; |
| 132 } |
| 133 |
| 134 // Offset of any field. |
| 135 uint32 GetFieldOffset(const FieldDescriptor* field) const { |
| 136 if (field->containing_oneof()) { |
| 137 size_t offset = field->containing_type()->field_count() + |
| 138 field->containing_oneof()->index(); |
| 139 return offsets_[offset]; |
| 140 } else { |
| 141 return GetFieldOffsetNonOneof(field); |
| 142 } |
| 143 } |
| 144 |
| 145 uint32 GetOneofCaseOffset(const OneofDescriptor* oneof_descriptor) const { |
| 146 return oneof_case_offset_ + (oneof_descriptor->index() * sizeof(uint32)); |
| 147 } |
| 148 |
| 149 bool HasHasbits() const { return has_bits_offset_ != -1; } |
| 150 |
| 151 // Bit index within the bit array of hasbits. Bit order is low-to-high. |
| 152 uint32 HasBitIndex(const FieldDescriptor* field) const { |
| 153 GOOGLE_DCHECK(HasHasbits()); |
| 154 return has_bit_indices_[field->index()]; |
| 155 } |
| 156 |
| 157 // Byte offset of the hasbits array. |
| 158 uint32 HasBitsOffset() const { |
| 159 GOOGLE_DCHECK(HasHasbits()); |
| 160 return has_bits_offset_; |
| 161 } |
| 162 |
| 163 // The offset of the InternalMetadataWithArenaOffset member. |
| 164 // For Lite this will actually be an InternalMetadataWithArenaOffsetLite. |
| 165 // The schema doesn't contain enough information to distinguish between |
| 166 // these two cases. |
| 167 uint32 GetMetadataOffset() const { |
| 168 return metadata_offset_; |
| 169 } |
| 170 |
| 171 // Whether this message has an ExtensionSet. |
| 172 bool HasExtensionSet() const { return extensions_offset_ != -1; } |
| 173 |
| 174 // The offset of the ExtensionSet in this message. |
| 175 uint32 GetExtensionSetOffset() const { |
| 176 GOOGLE_DCHECK(HasExtensionSet()); |
| 177 return extensions_offset_; |
| 178 } |
| 179 |
| 180 bool IsDefaultInstance(const Message& message) const { |
| 181 return &message == default_instance_; |
| 182 } |
| 183 |
| 184 // Returns a pointer to the default value for this field. The size and type |
| 185 // of the underlying data depends on the field's type. |
| 186 const void *GetFieldDefault(const FieldDescriptor* field) const { |
| 187 return field->containing_oneof() |
| 188 ? reinterpret_cast<const uint8*>(default_oneof_instance_) + |
| 189 offsets_[field->index()] |
| 190 : reinterpret_cast<const uint8*>(default_instance_) + |
| 191 offsets_[field->index()]; |
| 192 } |
| 193 |
| 194 // These members are intended to be private, but we cannot actually make them |
| 195 // private because this prevents us from using aggregate initialization of |
| 196 // them, ie. |
| 197 // |
| 198 // ReflectionSchema schema = {a, b, c, d, e, ...}; |
| 199 // private: |
| 200 const Message* default_instance_; |
| 201 const uint32* offsets_; |
| 202 const uint32* has_bit_indices_; |
| 203 int has_bits_offset_; |
| 204 int metadata_offset_; |
| 205 int extensions_offset_; |
| 206 const void* default_oneof_instance_; |
| 207 int oneof_case_offset_; |
| 208 int object_size_; |
| 209 }; |
| 210 |
| 211 // Structs that the code generator emits directly to describe a message. |
| 212 // These should never used directly except to build a ReflectionSchema |
| 213 // object. |
| 214 // |
| 215 // EXPERIMENTAL: these are changing rapidly, and may completely disappear |
| 216 // or merge with ReflectionSchema. |
| 217 struct DefaultInstanceData { |
| 218 const Message* default_instance; |
| 219 const void* default_oneof_instance; |
| 220 }; |
| 221 |
| 222 struct MigrationSchema { |
| 223 int32 offsets_index; |
| 224 int32 has_bit_indices_index; |
| 225 int object_size; |
| 226 }; |
| 227 |
| 76 // THIS CLASS IS NOT INTENDED FOR DIRECT USE. It is intended for use | 228 // THIS CLASS IS NOT INTENDED FOR DIRECT USE. It is intended for use |
| 77 // by generated code. This class is just a big hack that reduces code | 229 // by generated code. This class is just a big hack that reduces code |
| 78 // size. | 230 // size. |
| 79 // | 231 // |
| 80 // A GeneratedMessageReflection is an implementation of Reflection | 232 // A GeneratedMessageReflection is an implementation of Reflection |
| 81 // which expects all fields to be backed by simple variables located in | 233 // which expects all fields to be backed by simple variables located in |
| 82 // memory. The locations are given using a base pointer and a set of | 234 // memory. The locations are given using a base pointer and a set of |
| 83 // offsets. | 235 // offsets. |
| 84 // | 236 // |
| 85 // It is required that the user represents fields of each type in a standard | 237 // It is required that the user represents fields of each type in a standard |
| 86 // way, so that GeneratedMessageReflection can cast the void* pointer to | 238 // way, so that GeneratedMessageReflection can cast the void* pointer to |
| 87 // the appropriate type. For primitive fields and string fields, each field | 239 // the appropriate type. For primitive fields and string fields, each field |
| 88 // should be represented using the obvious C++ primitive type. Enums and | 240 // should be represented using the obvious C++ primitive type. Enums and |
| 89 // Messages are different: | 241 // Messages are different: |
| 90 // - Singular Message fields are stored as a pointer to a Message. These | 242 // - Singular Message fields are stored as a pointer to a Message. These |
| 91 // should start out NULL, except for in the default instance where they | 243 // should start out NULL, except for in the default instance where they |
| 92 // should start out pointing to other default instances. | 244 // should start out pointing to other default instances. |
| 93 // - Enum fields are stored as an int. This int must always contain | 245 // - Enum fields are stored as an int. This int must always contain |
| 94 // a valid value, such that EnumDescriptor::FindValueByNumber() would | 246 // a valid value, such that EnumDescriptor::FindValueByNumber() would |
| 95 // not return NULL. | 247 // not return NULL. |
| 96 // - Repeated fields are stored as RepeatedFields or RepeatedPtrFields | 248 // - Repeated fields are stored as RepeatedFields or RepeatedPtrFields |
| 97 // of whatever type the individual field would be. Strings and | 249 // of whatever type the individual field would be. Strings and |
| 98 // Messages use RepeatedPtrFields while everything else uses | 250 // Messages use RepeatedPtrFields while everything else uses |
| 99 // RepeatedFields. | 251 // RepeatedFields. |
| 100 class LIBPROTOBUF_EXPORT GeneratedMessageReflection : public Reflection { | 252 class LIBPROTOBUF_EXPORT GeneratedMessageReflection PROTOBUF_FINAL : public Refl
ection { |
| 101 public: | 253 public: |
| 102 // Constructs a GeneratedMessageReflection. | 254 // Constructs a GeneratedMessageReflection. |
| 103 // Parameters: | 255 // Parameters: |
| 104 // descriptor: The descriptor for the message type being implemented. | 256 // descriptor: The descriptor for the message type being implemented. |
| 105 // default_instance: The default instance of the message. This is only | 257 // schema: The description of the internal guts of the message. |
| 106 // used to obtain pointers to default instances of embedded | |
| 107 // messages, which GetMessage() will return if the particular | |
| 108 // sub-message has not been initialized yet. (Thus, all | |
| 109 // embedded message fields *must* have non-NULL pointers | |
| 110 // in the default instance.) | |
| 111 // offsets: An array of ints giving the byte offsets, relative to | |
| 112 // the start of the message object, of each field. These can | |
| 113 // be computed at compile time using the | |
| 114 // GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET() macro, de
fined | |
| 115 // below. | |
| 116 // has_bits_offset: Offset in the message of an array of uint32s of size | |
| 117 // descriptor->field_count()/32, rounded up. This is a | |
| 118 // bitfield where each bit indicates whether or not the | |
| 119 // corresponding field of the message has been initialized. | |
| 120 // The bit for field index i is obtained by the expression: | |
| 121 // has_bits[i / 32] & (1 << (i % 32)) | |
| 122 // unknown_fields_offset: Offset in the message of the UnknownFieldSet for | |
| 123 // the message. | |
| 124 // extensions_offset: Offset in the message of the ExtensionSet for the | |
| 125 // message, or -1 if the message type has no extension | |
| 126 // ranges. | |
| 127 // pool: DescriptorPool to search for extension definitions. Only | 258 // pool: DescriptorPool to search for extension definitions. Only |
| 128 // used by FindKnownExtensionByName() and | 259 // used by FindKnownExtensionByName() and |
| 129 // FindKnownExtensionByNumber(). | 260 // FindKnownExtensionByNumber(). |
| 130 // factory: MessageFactory to use to construct extension messages. | 261 // factory: MessageFactory to use to construct extension messages. |
| 131 // object_size: The size of a message object of this type, as measured | |
| 132 // by sizeof(). | |
| 133 GeneratedMessageReflection(const Descriptor* descriptor, | 262 GeneratedMessageReflection(const Descriptor* descriptor, |
| 134 const Message* default_instance, | 263 const ReflectionSchema& schema, |
| 135 const int offsets[], | |
| 136 int has_bits_offset, | |
| 137 int unknown_fields_offset, | |
| 138 int extensions_offset, | |
| 139 const DescriptorPool* pool, | 264 const DescriptorPool* pool, |
| 140 MessageFactory* factory, | 265 MessageFactory* factory); |
| 141 int object_size, | |
| 142 int arena_offset, | |
| 143 int is_default_instance_offset = -1); | |
| 144 | 266 |
| 145 // Similar with the construction above. Call this construction if the | |
| 146 // message has oneof definition. | |
| 147 // Parameters: | |
| 148 // offsets: An array of ints giving the byte offsets. | |
| 149 // For each oneof field, the offset is relative to the | |
| 150 // default_oneof_instance. These can be computed at compile | |
| 151 // time using the | |
| 152 // PROTO2_GENERATED_DEFAULT_ONEOF_FIELD_OFFSET() macro. | |
| 153 // For each none oneof field, the offset is related to | |
| 154 // the start of the message object. These can be computed | |
| 155 // at compile time using the | |
| 156 // GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET() macro. | |
| 157 // Besides offsets for all fields, this array also contains | |
| 158 // offsets for oneof unions. The offset of the i-th oneof | |
| 159 // union is offsets[descriptor->field_count() + i]. | |
| 160 // default_oneof_instance: The default instance of the oneofs. It is a | |
| 161 // struct holding the default value of all oneof fields | |
| 162 // for this message. It is only used to obtain pointers | |
| 163 // to default instances of oneof fields, which Get | |
| 164 // methods will return if the field is not set. | |
| 165 // oneof_case_offset: Offset in the message of an array of uint32s of | |
| 166 // size descriptor->oneof_decl_count(). Each uint32 | |
| 167 // indicates what field is set for each oneof. | |
| 168 // other parameters are the same with the construction above. | |
| 169 GeneratedMessageReflection(const Descriptor* descriptor, | |
| 170 const Message* default_instance, | |
| 171 const int offsets[], | |
| 172 int has_bits_offset, | |
| 173 int unknown_fields_offset, | |
| 174 int extensions_offset, | |
| 175 const void* default_oneof_instance, | |
| 176 int oneof_case_offset, | |
| 177 const DescriptorPool* pool, | |
| 178 MessageFactory* factory, | |
| 179 int object_size, | |
| 180 int arena_offset, | |
| 181 int is_default_instance_offset = -1); | |
| 182 ~GeneratedMessageReflection(); | 267 ~GeneratedMessageReflection(); |
| 183 | 268 |
| 184 // Shorter-to-call helpers for the above two constructions that work if the | |
| 185 // pool and factory are the usual, namely, DescriptorPool::generated_pool() | |
| 186 // and MessageFactory::generated_factory(). | |
| 187 | |
| 188 static GeneratedMessageReflection* NewGeneratedMessageReflection( | |
| 189 const Descriptor* descriptor, | |
| 190 const Message* default_instance, | |
| 191 const int offsets[], | |
| 192 int has_bits_offset, | |
| 193 int unknown_fields_offset, | |
| 194 int extensions_offset, | |
| 195 const void* default_oneof_instance, | |
| 196 int oneof_case_offset, | |
| 197 int object_size, | |
| 198 int arena_offset, | |
| 199 int is_default_instance_offset = -1); | |
| 200 | |
| 201 static GeneratedMessageReflection* NewGeneratedMessageReflection( | |
| 202 const Descriptor* descriptor, | |
| 203 const Message* default_instance, | |
| 204 const int offsets[], | |
| 205 int has_bits_offset, | |
| 206 int unknown_fields_offset, | |
| 207 int extensions_offset, | |
| 208 int object_size, | |
| 209 int arena_offset, | |
| 210 int is_default_instance_offset = -1); | |
| 211 | |
| 212 // implements Reflection ------------------------------------------- | 269 // implements Reflection ------------------------------------------- |
| 213 | 270 |
| 214 const UnknownFieldSet& GetUnknownFields(const Message& message) const; | 271 const UnknownFieldSet& GetUnknownFields(const Message& message) const; |
| 215 UnknownFieldSet* MutableUnknownFields(Message* message) const; | 272 UnknownFieldSet* MutableUnknownFields(Message* message) const; |
| 216 | 273 |
| 217 int SpaceUsed(const Message& message) const; | 274 int SpaceUsed(const Message& message) const; |
| 218 | 275 |
| 219 bool HasField(const Message& message, const FieldDescriptor* field) const; | 276 bool HasField(const Message& message, const FieldDescriptor* field) const; |
| 220 int FieldSize(const Message& message, const FieldDescriptor* field) const; | 277 int FieldSize(const Message& message, const FieldDescriptor* field) const; |
| 221 void ClearField(Message* message, const FieldDescriptor* field) const; | 278 void ClearField(Message* message, const FieldDescriptor* field) const; |
| 222 bool HasOneof(const Message& message, | 279 bool HasOneof(const Message& message, |
| 223 const OneofDescriptor* oneof_descriptor) const; | 280 const OneofDescriptor* oneof_descriptor) const; |
| 224 void ClearOneof(Message* message, const OneofDescriptor* field) const; | 281 void ClearOneof(Message* message, const OneofDescriptor* field) const; |
| 225 void RemoveLast(Message* message, const FieldDescriptor* field) const; | 282 void RemoveLast(Message* message, const FieldDescriptor* field) const; |
| 226 Message* ReleaseLast(Message* message, const FieldDescriptor* field) const; | 283 Message* ReleaseLast(Message* message, const FieldDescriptor* field) const; |
| 227 void Swap(Message* message1, Message* message2) const; | 284 void Swap(Message* message1, Message* message2) const; |
| 228 void SwapFields(Message* message1, Message* message2, | 285 void SwapFields(Message* message1, Message* message2, |
| 229 const vector<const FieldDescriptor*>& fields) const; | 286 const std::vector<const FieldDescriptor*>& fields) const; |
| 230 void SwapElements(Message* message, const FieldDescriptor* field, | 287 void SwapElements(Message* message, const FieldDescriptor* field, |
| 231 int index1, int index2) const; | 288 int index1, int index2) const; |
| 232 void ListFields(const Message& message, | 289 void ListFields(const Message& message, |
| 233 vector<const FieldDescriptor*>* output) const; | 290 std::vector<const FieldDescriptor*>* output) const; |
| 234 | 291 |
| 235 int32 GetInt32 (const Message& message, | 292 int32 GetInt32 (const Message& message, |
| 236 const FieldDescriptor* field) const; | 293 const FieldDescriptor* field) const; |
| 237 int64 GetInt64 (const Message& message, | 294 int64 GetInt64 (const Message& message, |
| 238 const FieldDescriptor* field) const; | 295 const FieldDescriptor* field) const; |
| 239 uint32 GetUInt32(const Message& message, | 296 uint32 GetUInt32(const Message& message, |
| 240 const FieldDescriptor* field) const; | 297 const FieldDescriptor* field) const; |
| 241 uint64 GetUInt64(const Message& message, | 298 uint64 GetUInt64(const Message& message, |
| 242 const FieldDescriptor* field) const; | 299 const FieldDescriptor* field) const; |
| 243 float GetFloat (const Message& message, | 300 float GetFloat (const Message& message, |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 | 483 |
| 427 virtual MessageFactory* GetMessageFactory() const; | 484 virtual MessageFactory* GetMessageFactory() const; |
| 428 | 485 |
| 429 virtual void* RepeatedFieldData( | 486 virtual void* RepeatedFieldData( |
| 430 Message* message, const FieldDescriptor* field, | 487 Message* message, const FieldDescriptor* field, |
| 431 FieldDescriptor::CppType cpp_type, | 488 FieldDescriptor::CppType cpp_type, |
| 432 const Descriptor* message_type) const; | 489 const Descriptor* message_type) const; |
| 433 | 490 |
| 434 private: | 491 private: |
| 435 friend class GeneratedMessage; | 492 friend class GeneratedMessage; |
| 493 friend class upb::google_opensource::GMR_Handlers; |
| 494 |
| 495 const Descriptor* const descriptor_; |
| 496 const ReflectionSchema schema_; |
| 497 const DescriptorPool* const descriptor_pool_; |
| 498 MessageFactory* const message_factory_; |
| 436 | 499 |
| 437 // To parse directly into a proto2 generated class, the class GMR_Handlers | 500 // To parse directly into a proto2 generated class, the class GMR_Handlers |
| 438 // needs access to member offsets and hasbits. | 501 // needs access to member offsets and hasbits. |
| 439 friend class upb::google_opensource::GMR_Handlers; | 502 // upb still needs these. |
| 503 // TODO(haberman) clean this up. |
| 504 const Message* const default_instance_; |
| 505 const void* const default_oneof_instance_; |
| 506 const uint32* const offsets_; |
| 507 const uint32* const has_bits_indices_; |
| 508 const int has_bits_offset_; |
| 509 const int oneof_case_offset_; |
| 510 const int unknown_fields_offset_; |
| 511 const int extensions_offset_; |
| 512 const int arena_offset_; |
| 513 const int object_size_; |
| 440 | 514 |
| 441 const Descriptor* descriptor_; | 515 template <class T> |
| 442 const Message* default_instance_; | 516 const T& GetRawNonOneof(const Message& message, |
| 443 const void* default_oneof_instance_; | 517 const FieldDescriptor* field) const; |
| 444 const int* offsets_; | 518 template <class T> |
| 445 | 519 T* MutableRawNonOneof(Message* message, const FieldDescriptor* field) const; |
| 446 int has_bits_offset_; | |
| 447 int oneof_case_offset_; | |
| 448 int unknown_fields_offset_; | |
| 449 int extensions_offset_; | |
| 450 int arena_offset_; | |
| 451 int is_default_instance_offset_; | |
| 452 int object_size_; | |
| 453 | |
| 454 static const int kHasNoDefaultInstanceField = -1; | |
| 455 | |
| 456 const DescriptorPool* descriptor_pool_; | |
| 457 MessageFactory* message_factory_; | |
| 458 | 520 |
| 459 template <typename Type> | 521 template <typename Type> |
| 460 inline const Type& GetRaw(const Message& message, | 522 const Type& GetRaw(const Message& message, |
| 461 const FieldDescriptor* field) const; | 523 const FieldDescriptor* field) const; |
| 462 template <typename Type> | 524 template <typename Type> |
| 463 inline Type* MutableRaw(Message* message, | 525 inline Type* MutableRaw(Message* message, |
| 464 const FieldDescriptor* field) const; | 526 const FieldDescriptor* field) const; |
| 465 template <typename Type> | 527 template <typename Type> |
| 466 inline const Type& DefaultRaw(const FieldDescriptor* field) const; | 528 inline const Type& DefaultRaw(const FieldDescriptor* field) const; |
| 467 template <typename Type> | |
| 468 inline const Type& DefaultOneofRaw(const FieldDescriptor* field) const; | |
| 469 | 529 |
| 470 inline const uint32* GetHasBits(const Message& message) const; | 530 inline const uint32* GetHasBits(const Message& message) const; |
| 471 inline uint32* MutableHasBits(Message* message) const; | 531 inline uint32* MutableHasBits(Message* message) const; |
| 472 inline uint32 GetOneofCase( | 532 inline uint32 GetOneofCase( |
| 473 const Message& message, | 533 const Message& message, |
| 474 const OneofDescriptor* oneof_descriptor) const; | 534 const OneofDescriptor* oneof_descriptor) const; |
| 475 inline uint32* MutableOneofCase( | 535 inline uint32* MutableOneofCase( |
| 476 Message* message, | 536 Message* message, |
| 477 const OneofDescriptor* oneof_descriptor) const; | 537 const OneofDescriptor* oneof_descriptor) const; |
| 478 inline const ExtensionSet& GetExtensionSet(const Message& message) const; | 538 inline const ExtensionSet& GetExtensionSet(const Message& message) const; |
| 479 inline ExtensionSet* MutableExtensionSet(Message* message) const; | 539 inline ExtensionSet* MutableExtensionSet(Message* message) const; |
| 480 inline Arena* GetArena(Message* message) const; | 540 inline Arena* GetArena(Message* message) const; |
| 481 inline const internal::InternalMetadataWithArena& | 541 |
| 482 GetInternalMetadataWithArena(const Message& message) const; | 542 inline const InternalMetadataWithArena& GetInternalMetadataWithArena( |
| 483 inline internal::InternalMetadataWithArena* | 543 const Message& message) const; |
| 544 |
| 545 inline InternalMetadataWithArena* |
| 484 MutableInternalMetadataWithArena(Message* message) const; | 546 MutableInternalMetadataWithArena(Message* message) const; |
| 485 | 547 |
| 486 inline bool GetIsDefaultInstance(const Message& message) const; | |
| 487 | |
| 488 inline bool HasBit(const Message& message, | 548 inline bool HasBit(const Message& message, |
| 489 const FieldDescriptor* field) const; | 549 const FieldDescriptor* field) const; |
| 490 inline void SetBit(Message* message, | 550 inline void SetBit(Message* message, |
| 491 const FieldDescriptor* field) const; | 551 const FieldDescriptor* field) const; |
| 492 inline void ClearBit(Message* message, | 552 inline void ClearBit(Message* message, |
| 493 const FieldDescriptor* field) const; | 553 const FieldDescriptor* field) const; |
| 494 inline void SwapBit(Message* message1, | 554 inline void SwapBit(Message* message1, |
| 495 Message* message2, | 555 Message* message2, |
| 496 const FieldDescriptor* field) const; | 556 const FieldDescriptor* field) const; |
| 497 | 557 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 const FieldDescriptor* field, | 624 const FieldDescriptor* field, |
| 565 MessageFactory* factory = NULL) const; | 625 MessageFactory* factory = NULL) const; |
| 566 | 626 |
| 567 void UnsafeArenaSetAllocatedMessage(Message* message, | 627 void UnsafeArenaSetAllocatedMessage(Message* message, |
| 568 Message* sub_message, | 628 Message* sub_message, |
| 569 const FieldDescriptor* field) const; | 629 const FieldDescriptor* field) const; |
| 570 | 630 |
| 571 internal::MapFieldBase* MapData( | 631 internal::MapFieldBase* MapData( |
| 572 Message* message, const FieldDescriptor* field) const; | 632 Message* message, const FieldDescriptor* field) const; |
| 573 | 633 |
| 634 friend inline // inline so nobody can call this function. |
| 635 void |
| 636 RegisterAllTypesInternal(const Metadata* file_level_metadata, int size); |
| 574 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratedMessageReflection); | 637 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratedMessageReflection); |
| 575 }; | 638 }; |
| 576 | 639 |
| 577 // Returns the offset of the given field within the given aggregate type. | 640 // Returns the offset of the given field within the given aggregate type. |
| 578 // This is equivalent to the ANSI C offsetof() macro. However, according | 641 // This is equivalent to the ANSI C offsetof() macro. However, according |
| 579 // to the C++ standard, offsetof() only works on POD types, and GCC | 642 // to the C++ standard, offsetof() only works on POD types, and GCC |
| 580 // enforces this requirement with a warning. In practice, this rule is | 643 // enforces this requirement with a warning. In practice, this rule is |
| 581 // unnecessarily strict; there is probably no compiler or platform on | 644 // unnecessarily strict; there is probably no compiler or platform on |
| 582 // which the offsets of the direct fields of a class are non-constant. | 645 // which the offsets of the direct fields of a class are non-constant. |
| 583 // Fields inherited from superclasses *can* have non-constant offsets, | 646 // Fields inherited from superclasses *can* have non-constant offsets, |
| 584 // but that's not what this macro will be used for. | 647 // but that's not what this macro will be used for. |
| 585 #if defined(__clang__) | 648 #if defined(__clang__) |
| 586 // For Clang we use __builtin_offsetof() and suppress the warning, | 649 // For Clang we use __builtin_offsetof() and suppress the warning, |
| 587 // to avoid Control Flow Integrity and UBSan vptr sanitizers from | 650 // to avoid Control Flow Integrity and UBSan vptr sanitizers from |
| 588 // crashing while trying to validate the invalid reinterpet_casts. | 651 // crashing while trying to validate the invalid reinterpet_casts. |
| 589 #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(TYPE, FIELD) \ | 652 #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(TYPE, FIELD) \ |
| 590 _Pragma("clang diagnostic push") \ | 653 _Pragma("clang diagnostic push") \ |
| 591 _Pragma("clang diagnostic ignored \"-Winvalid-offsetof\"") \ | 654 _Pragma("clang diagnostic ignored \"-Winvalid-offsetof\"") \ |
| 592 __builtin_offsetof(TYPE, FIELD) \ | 655 __builtin_offsetof(TYPE, FIELD) \ |
| 593 _Pragma("clang diagnostic pop") | 656 _Pragma("clang diagnostic pop") |
| 594 #else | 657 #else |
| 595 // Note that we calculate relative to the pointer value 16 here since if we | 658 // Note that we calculate relative to the pointer value 16 here since if we |
| 596 // just use zero, GCC complains about dereferencing a NULL pointer. We | 659 // just use zero, GCC complains about dereferencing a NULL pointer. We |
| 597 // choose 16 rather than some other number just in case the compiler would | 660 // choose 16 rather than some other number just in case the compiler would |
| 598 // be confused by an unaligned pointer. | 661 // be confused by an unaligned pointer. |
| 599 #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(TYPE, FIELD) \ | 662 #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(TYPE, FIELD) \ |
| 600 static_cast<int>( \ | 663 static_cast< ::google::protobuf::uint32>( \ |
| 601 reinterpret_cast<const char*>( \ | 664 reinterpret_cast<const char*>( \ |
| 602 &reinterpret_cast<const TYPE*>(16)->FIELD) - \ | 665 &reinterpret_cast<const TYPE*>(16)->FIELD) - \ |
| 603 reinterpret_cast<const char*>(16)) | 666 reinterpret_cast<const char*>(16)) |
| 604 #endif | 667 #endif |
| 605 | 668 |
| 606 #define PROTO2_GENERATED_DEFAULT_ONEOF_FIELD_OFFSET(ONEOF, FIELD) \ | 669 #define PROTO2_GENERATED_DEFAULT_ONEOF_FIELD_OFFSET(ONEOF, FIELD) \ |
| 607 static_cast<int>( \ | 670 static_cast< ::google::protobuf::uint32>( \ |
| 608 reinterpret_cast<const char*>(&(ONEOF->FIELD)) \ | 671 reinterpret_cast<const char*>(&(ONEOF->FIELD)) \ |
| 609 - reinterpret_cast<const char*>(ONEOF)) | 672 - reinterpret_cast<const char*>(ONEOF)) |
| 610 | 673 |
| 611 // There are some places in proto2 where dynamic_cast would be useful as an | 674 // There are some places in proto2 where dynamic_cast would be useful as an |
| 612 // optimization. For example, take Message::MergeFrom(const Message& other). | 675 // optimization. For example, take Message::MergeFrom(const Message& other). |
| 613 // For a given generated message FooMessage, we generate these two methods: | 676 // For a given generated message FooMessage, we generate these two methods: |
| 614 // void MergeFrom(const FooMessage& other); | 677 // void MergeFrom(const FooMessage& other); |
| 615 // void MergeFrom(const Message& other); | 678 // void MergeFrom(const Message& other); |
| 616 // The former method can be implemented directly in terms of FooMessage's | 679 // The former method can be implemented directly in terms of FooMessage's |
| 617 // inline accessors, but the latter method must work with the reflection | 680 // inline accessors, but the latter method must work with the reflection |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 669 return dynamic_cast<T*>(from); | 732 return dynamic_cast<T*>(from); |
| 670 #endif | 733 #endif |
| 671 } | 734 } |
| 672 | 735 |
| 673 template <typename T> | 736 template <typename T> |
| 674 T* DynamicCastToGenerated(Message* from) { | 737 T* DynamicCastToGenerated(Message* from) { |
| 675 const Message* message_const = from; | 738 const Message* message_const = from; |
| 676 return const_cast<T*>(DynamicCastToGenerated<const T>(message_const)); | 739 return const_cast<T*>(DynamicCastToGenerated<const T>(message_const)); |
| 677 } | 740 } |
| 678 | 741 |
| 742 LIBPROTOBUF_EXPORT void AssignDescriptors( |
| 743 const string& filename, const MigrationSchema* schemas, |
| 744 const DefaultInstanceData* default_instance_data, const uint32* offsets, |
| 745 MessageFactory* factory, |
| 746 // update the following descriptor arrays. |
| 747 Metadata* file_level_metadata, |
| 748 const EnumDescriptor** file_level_enum_descriptors, |
| 749 const ServiceDescriptor** file_level_service_descriptors); |
| 750 |
| 751 LIBPROTOBUF_EXPORT void AssignDescriptors( |
| 752 const string& filename, const ReflectionSchema* schemas, |
| 753 MessageFactory* factory, |
| 754 // update the following descriptor arrays. |
| 755 Metadata* file_level_metadata, |
| 756 const EnumDescriptor** file_level_enum_descriptors, |
| 757 const ServiceDescriptor** file_level_service_descriptors); |
| 758 |
| 759 LIBPROTOBUF_EXPORT void RegisterAllTypes(const Metadata* file_level_metadata, in
t size); |
| 760 |
| 679 } // namespace internal | 761 } // namespace internal |
| 680 } // namespace protobuf | 762 } // namespace protobuf |
| 681 | 763 |
| 682 } // namespace google | 764 } // namespace google |
| 683 #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__ | 765 #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__ |
| OLD | NEW |