| 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> | |
| 49 #include <google/protobuf/message.h> | 48 #include <google/protobuf/message.h> |
| 50 #include <google/protobuf/metadata.h> | 49 #include <google/protobuf/metadata.h> |
| 51 #include <google/protobuf/unknown_field_set.h> | 50 #include <google/protobuf/unknown_field_set.h> |
| 52 | 51 |
| 53 | 52 |
| 54 namespace google { | 53 namespace google { |
| 55 namespace upb { | 54 namespace upb { |
| 56 namespace google_opensource { | 55 namespace google_opensource { |
| 57 class GMR_Handlers; | 56 class GMR_Handlers; |
| 58 } // namespace google_opensource | 57 } // namespace google_opensource |
| 59 } // namespace upb | 58 } // namespace upb |
| 60 | 59 |
| 61 namespace protobuf { | 60 namespace protobuf { |
| 62 class DescriptorPool; | 61 class DescriptorPool; |
| 63 class MapKey; | 62 class MapKey; |
| 64 class MapValueRef; | 63 class MapValueRef; |
| 65 } | 64 } |
| 66 | 65 |
| 67 namespace protobuf { | 66 namespace protobuf { |
| 68 namespace internal { | 67 namespace internal { |
| 69 class DefaultEmptyOneof; | 68 class DefaultEmptyOneof; |
| 70 | 69 |
| 71 // Defined in this file. | 70 // Defined in this file. |
| 72 class GeneratedMessageReflection; | 71 class GeneratedMessageReflection; |
| 73 | 72 |
| 74 // Defined in other files. | 73 // Defined in other files. |
| 75 class ExtensionSet; // extension_set.h | 74 class ExtensionSet; // extension_set.h |
| 76 | 75 |
| 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 | |
| 228 // THIS CLASS IS NOT INTENDED FOR DIRECT USE. It is intended for use | 76 // THIS CLASS IS NOT INTENDED FOR DIRECT USE. It is intended for use |
| 229 // by generated code. This class is just a big hack that reduces code | 77 // by generated code. This class is just a big hack that reduces code |
| 230 // size. | 78 // size. |
| 231 // | 79 // |
| 232 // A GeneratedMessageReflection is an implementation of Reflection | 80 // A GeneratedMessageReflection is an implementation of Reflection |
| 233 // which expects all fields to be backed by simple variables located in | 81 // which expects all fields to be backed by simple variables located in |
| 234 // memory. The locations are given using a base pointer and a set of | 82 // memory. The locations are given using a base pointer and a set of |
| 235 // offsets. | 83 // offsets. |
| 236 // | 84 // |
| 237 // It is required that the user represents fields of each type in a standard | 85 // It is required that the user represents fields of each type in a standard |
| 238 // way, so that GeneratedMessageReflection can cast the void* pointer to | 86 // way, so that GeneratedMessageReflection can cast the void* pointer to |
| 239 // the appropriate type. For primitive fields and string fields, each field | 87 // the appropriate type. For primitive fields and string fields, each field |
| 240 // should be represented using the obvious C++ primitive type. Enums and | 88 // should be represented using the obvious C++ primitive type. Enums and |
| 241 // Messages are different: | 89 // Messages are different: |
| 242 // - Singular Message fields are stored as a pointer to a Message. These | 90 // - Singular Message fields are stored as a pointer to a Message. These |
| 243 // should start out NULL, except for in the default instance where they | 91 // should start out NULL, except for in the default instance where they |
| 244 // should start out pointing to other default instances. | 92 // should start out pointing to other default instances. |
| 245 // - Enum fields are stored as an int. This int must always contain | 93 // - Enum fields are stored as an int. This int must always contain |
| 246 // a valid value, such that EnumDescriptor::FindValueByNumber() would | 94 // a valid value, such that EnumDescriptor::FindValueByNumber() would |
| 247 // not return NULL. | 95 // not return NULL. |
| 248 // - Repeated fields are stored as RepeatedFields or RepeatedPtrFields | 96 // - Repeated fields are stored as RepeatedFields or RepeatedPtrFields |
| 249 // of whatever type the individual field would be. Strings and | 97 // of whatever type the individual field would be. Strings and |
| 250 // Messages use RepeatedPtrFields while everything else uses | 98 // Messages use RepeatedPtrFields while everything else uses |
| 251 // RepeatedFields. | 99 // RepeatedFields. |
| 252 class LIBPROTOBUF_EXPORT GeneratedMessageReflection PROTOBUF_FINAL : public Refl
ection { | 100 class LIBPROTOBUF_EXPORT GeneratedMessageReflection : public Reflection { |
| 253 public: | 101 public: |
| 254 // Constructs a GeneratedMessageReflection. | 102 // Constructs a GeneratedMessageReflection. |
| 255 // Parameters: | 103 // Parameters: |
| 256 // descriptor: The descriptor for the message type being implemented. | 104 // descriptor: The descriptor for the message type being implemented. |
| 257 // schema: The description of the internal guts of the message. | 105 // default_instance: The default instance of the message. This is only |
| 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. |
| 258 // pool: DescriptorPool to search for extension definitions. Only | 127 // pool: DescriptorPool to search for extension definitions. Only |
| 259 // used by FindKnownExtensionByName() and | 128 // used by FindKnownExtensionByName() and |
| 260 // FindKnownExtensionByNumber(). | 129 // FindKnownExtensionByNumber(). |
| 261 // factory: MessageFactory to use to construct extension messages. | 130 // 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(). |
| 262 GeneratedMessageReflection(const Descriptor* descriptor, | 133 GeneratedMessageReflection(const Descriptor* descriptor, |
| 263 const ReflectionSchema& schema, | 134 const Message* default_instance, |
| 135 const int offsets[], |
| 136 int has_bits_offset, |
| 137 int unknown_fields_offset, |
| 138 int extensions_offset, |
| 264 const DescriptorPool* pool, | 139 const DescriptorPool* pool, |
| 265 MessageFactory* factory); | 140 MessageFactory* factory, |
| 141 int object_size, |
| 142 int arena_offset, |
| 143 int is_default_instance_offset = -1); |
| 266 | 144 |
| 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); |
| 267 ~GeneratedMessageReflection(); | 182 ~GeneratedMessageReflection(); |
| 268 | 183 |
| 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 |
| 269 // implements Reflection ------------------------------------------- | 212 // implements Reflection ------------------------------------------- |
| 270 | 213 |
| 271 const UnknownFieldSet& GetUnknownFields(const Message& message) const; | 214 const UnknownFieldSet& GetUnknownFields(const Message& message) const; |
| 272 UnknownFieldSet* MutableUnknownFields(Message* message) const; | 215 UnknownFieldSet* MutableUnknownFields(Message* message) const; |
| 273 | 216 |
| 274 int SpaceUsed(const Message& message) const; | 217 int SpaceUsed(const Message& message) const; |
| 275 | 218 |
| 276 bool HasField(const Message& message, const FieldDescriptor* field) const; | 219 bool HasField(const Message& message, const FieldDescriptor* field) const; |
| 277 int FieldSize(const Message& message, const FieldDescriptor* field) const; | 220 int FieldSize(const Message& message, const FieldDescriptor* field) const; |
| 278 void ClearField(Message* message, const FieldDescriptor* field) const; | 221 void ClearField(Message* message, const FieldDescriptor* field) const; |
| 279 bool HasOneof(const Message& message, | 222 bool HasOneof(const Message& message, |
| 280 const OneofDescriptor* oneof_descriptor) const; | 223 const OneofDescriptor* oneof_descriptor) const; |
| 281 void ClearOneof(Message* message, const OneofDescriptor* field) const; | 224 void ClearOneof(Message* message, const OneofDescriptor* field) const; |
| 282 void RemoveLast(Message* message, const FieldDescriptor* field) const; | 225 void RemoveLast(Message* message, const FieldDescriptor* field) const; |
| 283 Message* ReleaseLast(Message* message, const FieldDescriptor* field) const; | 226 Message* ReleaseLast(Message* message, const FieldDescriptor* field) const; |
| 284 void Swap(Message* message1, Message* message2) const; | 227 void Swap(Message* message1, Message* message2) const; |
| 285 void SwapFields(Message* message1, Message* message2, | 228 void SwapFields(Message* message1, Message* message2, |
| 286 const std::vector<const FieldDescriptor*>& fields) const; | 229 const vector<const FieldDescriptor*>& fields) const; |
| 287 void SwapElements(Message* message, const FieldDescriptor* field, | 230 void SwapElements(Message* message, const FieldDescriptor* field, |
| 288 int index1, int index2) const; | 231 int index1, int index2) const; |
| 289 void ListFields(const Message& message, | 232 void ListFields(const Message& message, |
| 290 std::vector<const FieldDescriptor*>* output) const; | 233 vector<const FieldDescriptor*>* output) const; |
| 291 | 234 |
| 292 int32 GetInt32 (const Message& message, | 235 int32 GetInt32 (const Message& message, |
| 293 const FieldDescriptor* field) const; | 236 const FieldDescriptor* field) const; |
| 294 int64 GetInt64 (const Message& message, | 237 int64 GetInt64 (const Message& message, |
| 295 const FieldDescriptor* field) const; | 238 const FieldDescriptor* field) const; |
| 296 uint32 GetUInt32(const Message& message, | 239 uint32 GetUInt32(const Message& message, |
| 297 const FieldDescriptor* field) const; | 240 const FieldDescriptor* field) const; |
| 298 uint64 GetUInt64(const Message& message, | 241 uint64 GetUInt64(const Message& message, |
| 299 const FieldDescriptor* field) const; | 242 const FieldDescriptor* field) const; |
| 300 float GetFloat (const Message& message, | 243 float GetFloat (const Message& message, |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 | 426 |
| 484 virtual MessageFactory* GetMessageFactory() const; | 427 virtual MessageFactory* GetMessageFactory() const; |
| 485 | 428 |
| 486 virtual void* RepeatedFieldData( | 429 virtual void* RepeatedFieldData( |
| 487 Message* message, const FieldDescriptor* field, | 430 Message* message, const FieldDescriptor* field, |
| 488 FieldDescriptor::CppType cpp_type, | 431 FieldDescriptor::CppType cpp_type, |
| 489 const Descriptor* message_type) const; | 432 const Descriptor* message_type) const; |
| 490 | 433 |
| 491 private: | 434 private: |
| 492 friend class GeneratedMessage; | 435 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_; | |
| 499 | 436 |
| 500 // To parse directly into a proto2 generated class, the class GMR_Handlers | 437 // To parse directly into a proto2 generated class, the class GMR_Handlers |
| 501 // needs access to member offsets and hasbits. | 438 // needs access to member offsets and hasbits. |
| 502 // upb still needs these. | 439 friend class upb::google_opensource::GMR_Handlers; |
| 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_; | |
| 514 | 440 |
| 515 template <class T> | 441 const Descriptor* descriptor_; |
| 516 const T& GetRawNonOneof(const Message& message, | 442 const Message* default_instance_; |
| 517 const FieldDescriptor* field) const; | 443 const void* default_oneof_instance_; |
| 518 template <class T> | 444 const int* offsets_; |
| 519 T* MutableRawNonOneof(Message* message, const FieldDescriptor* field) const; | 445 |
| 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_; |
| 520 | 458 |
| 521 template <typename Type> | 459 template <typename Type> |
| 522 const Type& GetRaw(const Message& message, | 460 inline const Type& GetRaw(const Message& message, |
| 523 const FieldDescriptor* field) const; | 461 const FieldDescriptor* field) const; |
| 524 template <typename Type> | 462 template <typename Type> |
| 525 inline Type* MutableRaw(Message* message, | 463 inline Type* MutableRaw(Message* message, |
| 526 const FieldDescriptor* field) const; | 464 const FieldDescriptor* field) const; |
| 527 template <typename Type> | 465 template <typename Type> |
| 528 inline const Type& DefaultRaw(const FieldDescriptor* field) const; | 466 inline const Type& DefaultRaw(const FieldDescriptor* field) const; |
| 467 template <typename Type> |
| 468 inline const Type& DefaultOneofRaw(const FieldDescriptor* field) const; |
| 529 | 469 |
| 530 inline const uint32* GetHasBits(const Message& message) const; | 470 inline const uint32* GetHasBits(const Message& message) const; |
| 531 inline uint32* MutableHasBits(Message* message) const; | 471 inline uint32* MutableHasBits(Message* message) const; |
| 532 inline uint32 GetOneofCase( | 472 inline uint32 GetOneofCase( |
| 533 const Message& message, | 473 const Message& message, |
| 534 const OneofDescriptor* oneof_descriptor) const; | 474 const OneofDescriptor* oneof_descriptor) const; |
| 535 inline uint32* MutableOneofCase( | 475 inline uint32* MutableOneofCase( |
| 536 Message* message, | 476 Message* message, |
| 537 const OneofDescriptor* oneof_descriptor) const; | 477 const OneofDescriptor* oneof_descriptor) const; |
| 538 inline const ExtensionSet& GetExtensionSet(const Message& message) const; | 478 inline const ExtensionSet& GetExtensionSet(const Message& message) const; |
| 539 inline ExtensionSet* MutableExtensionSet(Message* message) const; | 479 inline ExtensionSet* MutableExtensionSet(Message* message) const; |
| 540 inline Arena* GetArena(Message* message) const; | 480 inline Arena* GetArena(Message* message) const; |
| 481 inline const internal::InternalMetadataWithArena& |
| 482 GetInternalMetadataWithArena(const Message& message) const; |
| 483 inline internal::InternalMetadataWithArena* |
| 484 MutableInternalMetadataWithArena(Message* message) const; |
| 541 | 485 |
| 542 inline const InternalMetadataWithArena& GetInternalMetadataWithArena( | 486 inline bool GetIsDefaultInstance(const Message& message) const; |
| 543 const Message& message) const; | |
| 544 | |
| 545 inline InternalMetadataWithArena* | |
| 546 MutableInternalMetadataWithArena(Message* message) const; | |
| 547 | 487 |
| 548 inline bool HasBit(const Message& message, | 488 inline bool HasBit(const Message& message, |
| 549 const FieldDescriptor* field) const; | 489 const FieldDescriptor* field) const; |
| 550 inline void SetBit(Message* message, | 490 inline void SetBit(Message* message, |
| 551 const FieldDescriptor* field) const; | 491 const FieldDescriptor* field) const; |
| 552 inline void ClearBit(Message* message, | 492 inline void ClearBit(Message* message, |
| 553 const FieldDescriptor* field) const; | 493 const FieldDescriptor* field) const; |
| 554 inline void SwapBit(Message* message1, | 494 inline void SwapBit(Message* message1, |
| 555 Message* message2, | 495 Message* message2, |
| 556 const FieldDescriptor* field) const; | 496 const FieldDescriptor* field) const; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 const FieldDescriptor* field, | 564 const FieldDescriptor* field, |
| 625 MessageFactory* factory = NULL) const; | 565 MessageFactory* factory = NULL) const; |
| 626 | 566 |
| 627 void UnsafeArenaSetAllocatedMessage(Message* message, | 567 void UnsafeArenaSetAllocatedMessage(Message* message, |
| 628 Message* sub_message, | 568 Message* sub_message, |
| 629 const FieldDescriptor* field) const; | 569 const FieldDescriptor* field) const; |
| 630 | 570 |
| 631 internal::MapFieldBase* MapData( | 571 internal::MapFieldBase* MapData( |
| 632 Message* message, const FieldDescriptor* field) const; | 572 Message* message, const FieldDescriptor* field) const; |
| 633 | 573 |
| 634 friend inline // inline so nobody can call this function. | |
| 635 void | |
| 636 RegisterAllTypesInternal(const Metadata* file_level_metadata, int size); | |
| 637 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratedMessageReflection); | 574 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratedMessageReflection); |
| 638 }; | 575 }; |
| 639 | 576 |
| 640 // Returns the offset of the given field within the given aggregate type. | 577 // Returns the offset of the given field within the given aggregate type. |
| 641 // This is equivalent to the ANSI C offsetof() macro. However, according | 578 // This is equivalent to the ANSI C offsetof() macro. However, according |
| 642 // to the C++ standard, offsetof() only works on POD types, and GCC | 579 // to the C++ standard, offsetof() only works on POD types, and GCC |
| 643 // enforces this requirement with a warning. In practice, this rule is | 580 // enforces this requirement with a warning. In practice, this rule is |
| 644 // unnecessarily strict; there is probably no compiler or platform on | 581 // unnecessarily strict; there is probably no compiler or platform on |
| 645 // which the offsets of the direct fields of a class are non-constant. | 582 // which the offsets of the direct fields of a class are non-constant. |
| 646 // Fields inherited from superclasses *can* have non-constant offsets, | 583 // Fields inherited from superclasses *can* have non-constant offsets, |
| 647 // but that's not what this macro will be used for. | 584 // but that's not what this macro will be used for. |
| 648 #if defined(__clang__) | 585 #if defined(__clang__) |
| 649 // For Clang we use __builtin_offsetof() and suppress the warning, | 586 // For Clang we use __builtin_offsetof() and suppress the warning, |
| 650 // to avoid Control Flow Integrity and UBSan vptr sanitizers from | 587 // to avoid Control Flow Integrity and UBSan vptr sanitizers from |
| 651 // crashing while trying to validate the invalid reinterpet_casts. | 588 // crashing while trying to validate the invalid reinterpet_casts. |
| 652 #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(TYPE, FIELD) \ | 589 #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(TYPE, FIELD) \ |
| 653 _Pragma("clang diagnostic push") \ | 590 _Pragma("clang diagnostic push") \ |
| 654 _Pragma("clang diagnostic ignored \"-Winvalid-offsetof\"") \ | 591 _Pragma("clang diagnostic ignored \"-Winvalid-offsetof\"") \ |
| 655 __builtin_offsetof(TYPE, FIELD) \ | 592 __builtin_offsetof(TYPE, FIELD) \ |
| 656 _Pragma("clang diagnostic pop") | 593 _Pragma("clang diagnostic pop") |
| 657 #else | 594 #else |
| 658 // Note that we calculate relative to the pointer value 16 here since if we | 595 // Note that we calculate relative to the pointer value 16 here since if we |
| 659 // just use zero, GCC complains about dereferencing a NULL pointer. We | 596 // just use zero, GCC complains about dereferencing a NULL pointer. We |
| 660 // choose 16 rather than some other number just in case the compiler would | 597 // choose 16 rather than some other number just in case the compiler would |
| 661 // be confused by an unaligned pointer. | 598 // be confused by an unaligned pointer. |
| 662 #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(TYPE, FIELD) \ | 599 #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(TYPE, FIELD) \ |
| 663 static_cast< ::google::protobuf::uint32>( \ | 600 static_cast<int>( \ |
| 664 reinterpret_cast<const char*>( \ | 601 reinterpret_cast<const char*>( \ |
| 665 &reinterpret_cast<const TYPE*>(16)->FIELD) - \ | 602 &reinterpret_cast<const TYPE*>(16)->FIELD) - \ |
| 666 reinterpret_cast<const char*>(16)) | 603 reinterpret_cast<const char*>(16)) |
| 667 #endif | 604 #endif |
| 668 | 605 |
| 669 #define PROTO2_GENERATED_DEFAULT_ONEOF_FIELD_OFFSET(ONEOF, FIELD) \ | 606 #define PROTO2_GENERATED_DEFAULT_ONEOF_FIELD_OFFSET(ONEOF, FIELD) \ |
| 670 static_cast< ::google::protobuf::uint32>( \ | 607 static_cast<int>( \ |
| 671 reinterpret_cast<const char*>(&(ONEOF->FIELD)) \ | 608 reinterpret_cast<const char*>(&(ONEOF->FIELD)) \ |
| 672 - reinterpret_cast<const char*>(ONEOF)) | 609 - reinterpret_cast<const char*>(ONEOF)) |
| 673 | 610 |
| 674 // There are some places in proto2 where dynamic_cast would be useful as an | 611 // There are some places in proto2 where dynamic_cast would be useful as an |
| 675 // optimization. For example, take Message::MergeFrom(const Message& other). | 612 // optimization. For example, take Message::MergeFrom(const Message& other). |
| 676 // For a given generated message FooMessage, we generate these two methods: | 613 // For a given generated message FooMessage, we generate these two methods: |
| 677 // void MergeFrom(const FooMessage& other); | 614 // void MergeFrom(const FooMessage& other); |
| 678 // void MergeFrom(const Message& other); | 615 // void MergeFrom(const Message& other); |
| 679 // The former method can be implemented directly in terms of FooMessage's | 616 // The former method can be implemented directly in terms of FooMessage's |
| 680 // inline accessors, but the latter method must work with the reflection | 617 // inline accessors, but the latter method must work with the reflection |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 732 return dynamic_cast<T*>(from); | 669 return dynamic_cast<T*>(from); |
| 733 #endif | 670 #endif |
| 734 } | 671 } |
| 735 | 672 |
| 736 template <typename T> | 673 template <typename T> |
| 737 T* DynamicCastToGenerated(Message* from) { | 674 T* DynamicCastToGenerated(Message* from) { |
| 738 const Message* message_const = from; | 675 const Message* message_const = from; |
| 739 return const_cast<T*>(DynamicCastToGenerated<const T>(message_const)); | 676 return const_cast<T*>(DynamicCastToGenerated<const T>(message_const)); |
| 740 } | 677 } |
| 741 | 678 |
| 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 | |
| 761 } // namespace internal | 679 } // namespace internal |
| 762 } // namespace protobuf | 680 } // namespace protobuf |
| 763 | 681 |
| 764 } // namespace google | 682 } // namespace google |
| 765 #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__ | 683 #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__ |
| OLD | NEW |