generated_message_reflection.h
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 //
35 // This header is logically internal, but is made public because it is used
36 // from protocol-compiler-generated code, which may reside in other components.
37 
38 #ifndef GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__
39 #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__
40 
41 #include <string>
42 #include <vector>
45 // TODO(jasonh): Remove this once the compiler change to directly include this
46 // is released to components.
51 #include <google/protobuf/port.h>
53 
54 
55 #include <google/protobuf/port_def.inc>
56 
57 #ifdef SWIG
58 #error "You cannot SWIG proto headers"
59 #endif
60 
61 namespace google {
62 namespace protobuf {
63 class DescriptorPool;
64 class MapKey;
65 class MapValueRef;
66 class MessageLayoutInspector;
67 class Message;
68 struct Metadata;
69 } // namespace protobuf
70 } // namespace google
71 
72 
73 namespace google {
74 namespace protobuf {
75 namespace internal {
76 class DefaultEmptyOneof;
77 class ReflectionAccessor;
78 
79 // Defined in other files.
80 class ExtensionSet; // extension_set.h
81 class WeakFieldMap; // weak_field_map.h
82 
83 // This struct describes the internal layout of the message, hence this is
84 // used to act on the message reflectively.
85 // default_instance: The default instance of the message. This is only
86 // used to obtain pointers to default instances of embedded
87 // messages, which GetMessage() will return if the particular
88 // sub-message has not been initialized yet. (Thus, all
89 // embedded message fields *must* have non-null pointers
90 // in the default instance.)
91 // offsets: An array of ints giving the byte offsets.
92 // For each oneof or weak field, the offset is relative to the
93 // default_instance. These can be computed at compile time
94 // using the
95 // PROTO2_GENERATED_DEFAULT_ONEOF_FIELD_OFFSET()
96 // macro. For each none oneof field, the offset is related to
97 // the start of the message object. These can be computed at
98 // compile time using the
99 // PROTO2_GENERATED_MESSAGE_FIELD_OFFSET() macro.
100 // Besides offsets for all fields, this array also contains
101 // offsets for oneof unions. The offset of the i-th oneof union
102 // is offsets[descriptor->field_count() + i].
103 // has_bit_indices: Mapping from field indexes to their index in the has
104 // bit array.
105 // has_bits_offset: Offset in the message of an array of uint32s of size
106 // descriptor->field_count()/32, rounded up. This is a
107 // bitfield where each bit indicates whether or not the
108 // corresponding field of the message has been initialized.
109 // The bit for field index i is obtained by the expression:
110 // has_bits[i / 32] & (1 << (i % 32))
111 // unknown_fields_offset: Offset in the message of the UnknownFieldSet for
112 // the message.
113 // extensions_offset: Offset in the message of the ExtensionSet for the
114 // message, or -1 if the message type has no extension
115 // ranges.
116 // oneof_case_offset: Offset in the message of an array of uint32s of
117 // size descriptor->oneof_decl_count(). Each uint32
118 // indicates what field is set for each oneof.
119 // object_size: The size of a message object of this type, as measured
120 // by sizeof().
121 // arena_offset: If a message doesn't have a unknown_field_set that stores
122 // the arena, it must have a direct pointer to the arena.
123 // weak_field_map_offset: If the message proto has weak fields, this is the
124 // offset of _weak_field_map_ in the generated proto. Otherwise
125 // -1.
127  public:
128  // Size of a google::protobuf::Message object of this type.
129  uint32 GetObjectSize() const { return static_cast<uint32>(object_size_); }
130 
131  // Offset of a non-oneof field. Getting a field offset is slightly more
132  // efficient when we know statically that it is not a oneof field.
134  GOOGLE_DCHECK(!field->containing_oneof());
135  return OffsetValue(offsets_[field->index()], field->type());
136  }
137 
138  // Offset of any field.
140  if (field->containing_oneof()) {
141  size_t offset =
142  static_cast<size_t>(field->containing_type()->field_count() +
143  field->containing_oneof()->index());
144  return OffsetValue(offsets_[offset], field->type());
145  } else {
147  }
148  }
149 
150  bool IsFieldInlined(const FieldDescriptor* field) const {
151  if (field->containing_oneof()) {
152  size_t offset =
153  static_cast<size_t>(field->containing_type()->field_count() +
154  field->containing_oneof()->index());
155  return Inlined(offsets_[offset], field->type());
156  } else {
157  return Inlined(offsets_[field->index()], field->type());
158  }
159  }
160 
161  uint32 GetOneofCaseOffset(const OneofDescriptor* oneof_descriptor) const {
162  return static_cast<uint32>(oneof_case_offset_) +
163  static_cast<uint32>(static_cast<size_t>(oneof_descriptor->index()) *
164  sizeof(uint32));
165  }
166 
167  bool HasHasbits() const { return has_bits_offset_ != -1; }
168 
169  // Bit index within the bit array of hasbits. Bit order is low-to-high.
172  return has_bit_indices_[field->index()];
173  }
174 
175  // Byte offset of the hasbits array.
178  return static_cast<uint32>(has_bits_offset_);
179  }
180 
181  // The offset of the InternalMetadataWithArena member.
182  // For Lite this will actually be an InternalMetadataWithArenaLite.
183  // The schema doesn't contain enough information to distinguish between
184  // these two cases.
186  return static_cast<uint32>(metadata_offset_);
187  }
188 
189  // Whether this message has an ExtensionSet.
190  bool HasExtensionSet() const { return extensions_offset_ != -1; }
191 
192  // The offset of the ExtensionSet in this message.
195  return static_cast<uint32>(extensions_offset_);
196  }
197 
198  // The off set of WeakFieldMap when the message contains weak fields.
199  // The default is 0 for now.
201 
202  bool IsDefaultInstance(const Message& message) const {
203  return &message == default_instance_;
204  }
205 
206  // Returns a pointer to the default value for this field. The size and type
207  // of the underlying data depends on the field's type.
208  const void* GetFieldDefault(const FieldDescriptor* field) const {
209  return reinterpret_cast<const uint8*>(default_instance_) +
210  OffsetValue(offsets_[field->index()], field->type());
211  }
212 
213 
214  bool HasWeakFields() const { return weak_field_map_offset_ > 0; }
215 
216  // These members are intended to be private, but we cannot actually make them
217  // private because this prevents us from using aggregate initialization of
218  // them, ie.
219  //
220  // ReflectionSchema schema = {a, b, c, d, e, ...};
221  // private:
223  const uint32* offsets_;
231 
232  // We tag offset values to provide additional data about fields (such as
233  // inlined).
237  return v & ~1u;
238  } else {
239  return v;
240  }
241  }
242 
246  return v & 1u;
247  } else {
248  // Non string/byte fields are not inlined.
249  return false;
250  }
251  }
252 };
253 
254 // Structs that the code generator emits directly to describe a message.
255 // These should never used directly except to build a ReflectionSchema
256 // object.
257 //
258 // EXPERIMENTAL: these are changing rapidly, and may completely disappear
259 // or merge with ReflectionSchema.
264 };
265 
266 struct PROTOBUF_EXPORT DescriptorTable {
268  const char* descriptor;
269  const char* filename;
270  int size; // of serialized descriptor
273  const DescriptorTable* const* deps;
274  int num_sccs;
275  int num_deps;
277  const Message* const* default_instances;
278  const uint32* offsets;
279  // update the following descriptor arrays.
284 };
285 
286 // AssignDescriptors() pulls the compiled FileDescriptor from the DescriptorPool
287 // and uses it to populate all of the global variables which store pointers to
288 // the descriptor objects. It also constructs the reflection objects. It is
289 // called the first time anyone calls descriptor() or GetReflection() on one of
290 // the types defined in the file. AssignDescriptors() is thread-safe.
291 void PROTOBUF_EXPORT AssignDescriptors(const DescriptorTable* table);
292 
293 // AddDescriptors() is a file-level procedure which adds the encoded
294 // FileDescriptorProto for this .proto file to the global DescriptorPool for
295 // generated files (DescriptorPool::generated_pool()). It ordinarily runs at
296 // static initialization time, but is not used at all in LITE_RUNTIME mode.
297 // AddDescriptors() is *not* thread-safe.
298 void PROTOBUF_EXPORT AddDescriptors(const DescriptorTable* table);
299 
300 // These cannot be in lite so we put them in the reflection.
301 PROTOBUF_EXPORT void UnknownFieldSetSerializer(const uint8* base, uint32 offset,
302  uint32 tag, uint32 has_offset,
304 
305 } // namespace internal
306 } // namespace protobuf
307 } // namespace google
308 
309 #include <google/protobuf/port_undef.inc>
310 
311 #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__
google::protobuf::FieldDescriptor::Type
Type
Definition: src/google/protobuf/descriptor.h:521
google::protobuf.internal::ReflectionSchema::Inlined
static bool Inlined(uint32 v, FieldDescriptor::Type type)
Definition: generated_message_reflection.h:243
table
upb_strtable table
Definition: php/ext/google/protobuf/protobuf.h:1065
google::protobuf.internal::ReflectionSchema::GetOneofCaseOffset
uint32 GetOneofCaseOffset(const OneofDescriptor *oneof_descriptor) const
Definition: generated_message_reflection.h:161
google::protobuf.internal::ReflectionSchema::object_size_
int object_size_
Definition: generated_message_reflection.h:229
google::protobuf.internal::ReflectionSchema::metadata_offset_
int metadata_offset_
Definition: generated_message_reflection.h:226
google::protobuf.internal::DescriptorTable::init_default_instances
SCCInfoBase *const * init_default_instances
Definition: generated_message_reflection.h:272
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
google::protobuf.internal::ReflectionSchema::GetObjectSize
uint32 GetObjectSize() const
Definition: generated_message_reflection.h:129
google::protobuf.internal::DescriptorTable::num_deps
int num_deps
Definition: generated_message_reflection.h:275
google::protobuf.internal::ReflectionSchema::IsFieldInlined
bool IsFieldInlined(const FieldDescriptor *field) const
Definition: generated_message_reflection.h:150
base
Definition: logging.cc:2162
google::protobuf::uint8
uint8_t uint8
Definition: protobuf/src/google/protobuf/stubs/port.h:153
google::protobuf.internal::ReflectionSchema::GetFieldDefault
const void * GetFieldDefault(const FieldDescriptor *field) const
Definition: generated_message_reflection.h:208
google::protobuf::FieldDescriptor::TYPE_BYTES
@ TYPE_BYTES
Definition: src/google/protobuf/descriptor.h:538
GOOGLE_DCHECK
#define GOOGLE_DCHECK
Definition: logging.h:194
google::protobuf.internal::DescriptorTable::default_instances
const Message *const * default_instances
Definition: generated_message_reflection.h:277
google::protobuf::uint32
uint32_t uint32
Definition: protobuf/src/google/protobuf/stubs/port.h:155
google::protobuf.internal::SCCInfoBase
Definition: generated_message_util.h:181
google::protobuf::OneofDescriptor
Definition: src/google/protobuf/descriptor.h:843
google::protobuf.internal::ReflectionSchema::OffsetValue
static uint32 OffsetValue(uint32 v, FieldDescriptor::Type type)
Definition: generated_message_reflection.h:234
google::protobuf.internal::DescriptorTable::num_messages
int num_messages
Definition: generated_message_reflection.h:281
google::protobuf.internal::ReflectionSchema::default_instance_
const Message * default_instance_
Definition: generated_message_reflection.h:222
google::protobuf.internal::DescriptorTable::file_level_metadata
Metadata * file_level_metadata
Definition: generated_message_reflection.h:280
google::protobuf.internal::ReflectionSchema::GetWeakFieldMapOffset
int GetWeakFieldMapOffset() const
Definition: generated_message_reflection.h:200
google::protobuf.internal::DescriptorTable::file_level_enum_descriptors
const EnumDescriptor ** file_level_enum_descriptors
Definition: generated_message_reflection.h:282
google::protobuf.internal::ReflectionSchema::GetFieldOffset
uint32 GetFieldOffset(const FieldDescriptor *field) const
Definition: generated_message_reflection.h:139
port.h
google::protobuf.internal::DescriptorTable::schemas
const MigrationSchema * schemas
Definition: generated_message_reflection.h:276
google::protobuf.internal::ReflectionSchema::has_bits_offset_
int has_bits_offset_
Definition: generated_message_reflection.h:225
google::protobuf.internal::DescriptorTable::once
once_flag * once
Definition: generated_message_reflection.h:271
google::protobuf.internal::MigrationSchema::object_size
int object_size
Definition: generated_message_reflection.h:263
google::protobuf::ServiceDescriptor
Definition: src/google/protobuf/descriptor.h:1152
google::protobuf::int32
int32_t int32
Definition: protobuf/src/google/protobuf/stubs/port.h:150
google::protobuf.internal::DescriptorTable::is_initialized
bool * is_initialized
Definition: generated_message_reflection.h:267
google::protobuf.internal::ReflectionSchema::offsets_
const uint32 * offsets_
Definition: generated_message_reflection.h:223
unknown_field_set.h
google::protobuf.internal::ReflectionSchema::HasBitIndex
uint32 HasBitIndex(const FieldDescriptor *field) const
Definition: generated_message_reflection.h:170
google::protobuf.internal::once_flag
std::once_flag once_flag
Definition: once.h:43
offset
GLintptr offset
Definition: glcorearb.h:2944
google::protobuf.internal::DescriptorTable::size
int size
Definition: generated_message_reflection.h:270
google::protobuf.internal::DescriptorTable::deps
const DescriptorTable *const * deps
Definition: generated_message_reflection.h:273
google::protobuf.internal::UnknownFieldSetSerializer
void UnknownFieldSetSerializer(const uint8 *base, uint32 offset, uint32 tag, uint32 has_offset, io::CodedOutputStream *output)
Definition: generated_message_reflection.cc:2422
google::protobuf.internal::ReflectionSchema::HasWeakFields
bool HasWeakFields() const
Definition: generated_message_reflection.h:214
google::protobuf::FieldDescriptor::TYPE_STRING
@ TYPE_STRING
Definition: src/google/protobuf/descriptor.h:534
google::protobuf.internal::DescriptorTable
Definition: generated_message_reflection.h:266
google::protobuf.internal::DescriptorTable::file_level_service_descriptors
const ServiceDescriptor ** file_level_service_descriptors
Definition: generated_message_reflection.h:283
google::protobuf.internal::MigrationSchema::has_bit_indices_index
int32 has_bit_indices_index
Definition: generated_message_reflection.h:262
casts.h
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
google::protobuf.internal::DescriptorTable::num_sccs
int num_sccs
Definition: generated_message_reflection.h:274
google::protobuf.internal::ReflectionSchema::weak_field_map_offset_
int weak_field_map_offset_
Definition: generated_message_reflection.h:230
google::protobuf::io::CodedOutputStream
Definition: coded_stream.h:693
google::protobuf.internal::ReflectionSchema::oneof_case_offset_
int oneof_case_offset_
Definition: generated_message_reflection.h:228
google::protobuf::Metadata
Definition: src/google/protobuf/message.h:190
google::protobuf.internal::ReflectionSchema::GetMetadataOffset
uint32 GetMetadataOffset() const
Definition: generated_message_reflection.h:185
google::protobuf.internal::ReflectionSchema::GetFieldOffsetNonOneof
uint32 GetFieldOffsetNonOneof(const FieldDescriptor *field) const
Definition: generated_message_reflection.h:133
type
GLenum type
Definition: glcorearb.h:2695
google::protobuf.internal::ReflectionSchema::HasHasbits
bool HasHasbits() const
Definition: generated_message_reflection.h:167
google::protobuf::Message
Definition: src/google/protobuf/message.h:205
google::protobuf.internal::DescriptorTable::descriptor
const char * descriptor
Definition: generated_message_reflection.h:268
google::protobuf.internal::DescriptorTable::offsets
const uint32 * offsets
Definition: generated_message_reflection.h:278
v
const GLdouble * v
Definition: glcorearb.h:3106
common.h
google::protobuf.internal::MigrationSchema
Definition: generated_message_reflection.h:260
generated_enum_reflection.h
google::protobuf::OneofDescriptor::index
int index() const
Definition: src/google/protobuf/descriptor.h:2103
once.h
metadata.h
descriptor.h
google::protobuf.internal::ReflectionSchema
Definition: generated_message_reflection.h:126
internal
Definition: any.pb.h:40
google::protobuf.internal::AssignDescriptors
void AssignDescriptors(const DescriptorTable *table)
Definition: generated_message_reflection.cc:2403
google::protobuf.internal::ReflectionSchema::HasExtensionSet
bool HasExtensionSet() const
Definition: generated_message_reflection.h:190
google::protobuf.internal::ReflectionSchema::IsDefaultInstance
bool IsDefaultInstance(const Message &message) const
Definition: generated_message_reflection.h:202
google::protobuf.internal::ReflectionSchema::has_bit_indices_
const uint32 * has_bit_indices_
Definition: generated_message_reflection.h:224
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
google::protobuf.internal::ReflectionSchema::extensions_offset_
int extensions_offset_
Definition: generated_message_reflection.h:227
google::protobuf.internal::MigrationSchema::offsets_index
int32 offsets_index
Definition: generated_message_reflection.h:261
google::protobuf::EnumDescriptor
Definition: src/google/protobuf/descriptor.h:918
DescriptorPool
Definition: ruby/ext/google/protobuf_c/protobuf.h:109
google::protobuf.internal::ReflectionSchema::GetExtensionSetOffset
uint32 GetExtensionSetOffset() const
Definition: generated_message_reflection.h:193
google::protobuf.internal::ReflectionSchema::HasBitsOffset
uint32 HasBitsOffset() const
Definition: generated_message_reflection.h:176
google::protobuf.internal::DescriptorTable::filename
const char * filename
Definition: generated_message_reflection.h:269
google
Definition: data_proto2_to_proto3_util.h:11
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695
google::protobuf.internal::AddDescriptors
void AddDescriptors(const DescriptorTable *table)
Definition: generated_message_reflection.cc:2407


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:51