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>
43 #include <google/protobuf/stubs/casts.h>
44 #include <google/protobuf/stubs/common.h>
45 // TODO(jasonh): Remove this once the compiler change to directly include this
46 // is released to components.
47 #include <google/protobuf/generated_enum_reflection.h>
48 #include <google/protobuf/generated_message_util.h>
49 #include <google/protobuf/message.h>
50 #include <google/protobuf/metadata.h>
51 #include <google/protobuf/unknown_field_set.h>
52
53
54 namespace google {
55 namespace upb {
56 namespace google_opensource {
57 class GMR_Handlers;
58 } // namespace google_opensource
59 } // namespace upb
60
61 namespace protobuf {
62 class DescriptorPool;
63 class MapKey;
64 class MapValueRef;
65 } // namespace protobuf
66
67
68 namespace protobuf {
69 namespace flat {
70 class MetadataBuilder;
71 } // namespace flat
72 } // namespace protobuf
73
74
75 namespace protobuf {
76 namespace internal {
77 class DefaultEmptyOneof;
78
79 // Defined in this file.
80 class GeneratedMessageReflection;
81
82 // Defined in other files.
83 class ExtensionSet; // extension_set.h
84 class WeakFieldMap; // weak_field_map.h
85
86 // This struct describes the internal layout of the message, hence this is
87 // used to act on the message reflectively.
88 // default_instance: The default instance of the message. This is only
89 // used to obtain pointers to default instances of embedded
90 // messages, which GetMessage() will return if the particular
91 // sub-message has not been initialized yet. (Thus, all
92 // embedded message fields *must* have non-NULL pointers
93 // in the default instance.)
94 // offsets: An array of ints giving the byte offsets.
95 // For each oneof or weak field, the offset is relative to the
96 // default_instance. These can be computed at compile time
97 // using the
98 // GOOGLE_PROTOBUF_GENERATED_DEFAULT_ONEOF_FIELD_OFFSET()
99 // macro. For each none oneof field, the offset is related to
100 // the start of the message object. These can be computed at
101 // compile time using the
102 // GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET() macro.
103 // Besides offsets for all fields, this array also contains
104 // offsets for oneof unions. The offset of the i-th oneof union
105 // is offsets[descriptor->field_count() + i].
106 // has_bit_indices: Mapping from field indexes to their index in the has
107 // bit array.
108 // has_bits_offset: Offset in the message of an array of uint32s of size
109 // descriptor->field_count()/32, rounded up. This is a
110 // bitfield where each bit indicates whether or not the
111 // corresponding field of the message has been initialized.
112 // The bit for field index i is obtained by the expression:
113 // has_bits[i / 32] & (1 << (i % 32))
114 // unknown_fields_offset: Offset in the message of the UnknownFieldSet for
115 // the message.
116 // extensions_offset: Offset in the message of the ExtensionSet for the
117 // message, or -1 if the message type has no extension
118 // ranges.
119 // oneof_case_offset: Offset in the message of an array of uint32s of
120 // size descriptor->oneof_decl_count(). Each uint32
121 // indicates what field is set for each oneof.
122 // object_size: The size of a message object of this type, as measured
123 // by sizeof().
124 // arena_offset: If a message doesn't have a unknown_field_set that stores
125 // the arena, it must have a direct pointer to the arena.
126 // weak_field_map_offset: If the message proto has weak fields, this is the
127 // offset of _weak_field_map_ in the generated proto. Otherwise
128 // -1.
129 struct ReflectionSchema {
130 public:
131 // Size of a google::protobuf::Message object of this type.
GetObjectSizeReflectionSchema132 uint32 GetObjectSize() const { return static_cast<uint32>(object_size_); }
133
134 // Offset of a non-oneof field. Getting a field offset is slightly more
135 // efficient when we know statically that it is not a oneof field.
GetFieldOffsetNonOneofReflectionSchema136 uint32 GetFieldOffsetNonOneof(const FieldDescriptor* field) const {
137 GOOGLE_DCHECK(!field->containing_oneof());
138 return offsets_[field->index()];
139 }
140
141 // Offset of any field.
GetFieldOffsetReflectionSchema142 uint32 GetFieldOffset(const FieldDescriptor* field) const {
143 if (field->containing_oneof()) {
144 size_t offset =
145 static_cast<size_t>(field->containing_type()->field_count() +
146 field->containing_oneof()->index());
147 return offsets_[offset];
148 } else {
149 return GetFieldOffsetNonOneof(field);
150 }
151 }
152
GetOneofCaseOffsetReflectionSchema153 uint32 GetOneofCaseOffset(const OneofDescriptor* oneof_descriptor) const {
154 return static_cast<uint32>(oneof_case_offset_) +
155 static_cast<uint32>(
156 static_cast<size_t>(oneof_descriptor->index()) * sizeof(uint32));
157 }
158
HasHasbitsReflectionSchema159 bool HasHasbits() const { return has_bits_offset_ != -1; }
160
161 // Bit index within the bit array of hasbits. Bit order is low-to-high.
HasBitIndexReflectionSchema162 uint32 HasBitIndex(const FieldDescriptor* field) const {
163 GOOGLE_DCHECK(HasHasbits());
164 return has_bit_indices_[field->index()];
165 }
166
167 // Byte offset of the hasbits array.
HasBitsOffsetReflectionSchema168 uint32 HasBitsOffset() const {
169 GOOGLE_DCHECK(HasHasbits());
170 return static_cast<uint32>(has_bits_offset_);
171 }
172
173 // The offset of the InternalMetadataWithArena member.
174 // For Lite this will actually be an InternalMetadataWithArenaLite.
175 // The schema doesn't contain enough information to distinguish between
176 // these two cases.
GetMetadataOffsetReflectionSchema177 uint32 GetMetadataOffset() const {
178 return static_cast<uint32>(metadata_offset_);
179 }
180
181 // Whether this message has an ExtensionSet.
HasExtensionSetReflectionSchema182 bool HasExtensionSet() const { return extensions_offset_ != -1; }
183
184 // The offset of the ExtensionSet in this message.
GetExtensionSetOffsetReflectionSchema185 uint32 GetExtensionSetOffset() const {
186 GOOGLE_DCHECK(HasExtensionSet());
187 return static_cast<uint32>(extensions_offset_);
188 }
189
190 // The off set of WeakFieldMap when the message contains weak fields.
191 // The default is 0 for now.
GetWeakFieldMapOffsetReflectionSchema192 int GetWeakFieldMapOffset() const { return weak_field_map_offset_; }
193
IsDefaultInstanceReflectionSchema194 bool IsDefaultInstance(const Message& message) const {
195 return &message == default_instance_;
196 }
197
198 // Returns a pointer to the default value for this field. The size and type
199 // of the underlying data depends on the field's type.
GetFieldDefaultReflectionSchema200 const void *GetFieldDefault(const FieldDescriptor* field) const {
201 return reinterpret_cast<const uint8*>(default_instance_) +
202 offsets_[field->index()];
203 }
204
205
HasWeakFieldsReflectionSchema206 bool HasWeakFields() const { return weak_field_map_offset_ > 0; }
207
208 // These members are intended to be private, but we cannot actually make them
209 // private because this prevents us from using aggregate initialization of
210 // them, ie.
211 //
212 // ReflectionSchema schema = {a, b, c, d, e, ...};
213 // private:
214 const Message* default_instance_;
215 const uint32* offsets_;
216 const uint32* has_bit_indices_;
217 int has_bits_offset_;
218 int metadata_offset_;
219 int extensions_offset_;
220 int oneof_case_offset_;
221 int object_size_;
222 int weak_field_map_offset_;
223 };
224
225 // Structs that the code generator emits directly to describe a message.
226 // These should never used directly except to build a ReflectionSchema
227 // object.
228 //
229 // EXPERIMENTAL: these are changing rapidly, and may completely disappear
230 // or merge with ReflectionSchema.
231 struct MigrationSchema {
232 int32 offsets_index;
233 int32 has_bit_indices_index;
234 int object_size;
235 };
236
237 // THIS CLASS IS NOT INTENDED FOR DIRECT USE. It is intended for use
238 // by generated code. This class is just a big hack that reduces code
239 // size.
240 //
241 // A GeneratedMessageReflection is an implementation of Reflection
242 // which expects all fields to be backed by simple variables located in
243 // memory. The locations are given using a base pointer and a set of
244 // offsets.
245 //
246 // It is required that the user represents fields of each type in a standard
247 // way, so that GeneratedMessageReflection can cast the void* pointer to
248 // the appropriate type. For primitive fields and string fields, each field
249 // should be represented using the obvious C++ primitive type. Enums and
250 // Messages are different:
251 // - Singular Message fields are stored as a pointer to a Message. These
252 // should start out NULL, except for in the default instance where they
253 // should start out pointing to other default instances.
254 // - Enum fields are stored as an int. This int must always contain
255 // a valid value, such that EnumDescriptor::FindValueByNumber() would
256 // not return NULL.
257 // - Repeated fields are stored as RepeatedFields or RepeatedPtrFields
258 // of whatever type the individual field would be. Strings and
259 // Messages use RepeatedPtrFields while everything else uses
260 // RepeatedFields.
261 class LIBPROTOBUF_EXPORT GeneratedMessageReflection PROTOBUF_FINAL : public Reflection {
262 public:
263 // Constructs a GeneratedMessageReflection.
264 // Parameters:
265 // descriptor: The descriptor for the message type being implemented.
266 // schema: The description of the internal guts of the message.
267 // pool: DescriptorPool to search for extension definitions. Only
268 // used by FindKnownExtensionByName() and
269 // FindKnownExtensionByNumber().
270 // factory: MessageFactory to use to construct extension messages.
271 GeneratedMessageReflection(const Descriptor* descriptor,
272 const ReflectionSchema& schema,
273 const DescriptorPool* pool,
274 MessageFactory* factory);
275
276 ~GeneratedMessageReflection();
277
278 // implements Reflection -------------------------------------------
279
280 const UnknownFieldSet& GetUnknownFields(const Message& message) const;
281 UnknownFieldSet* MutableUnknownFields(Message* message) const;
282
283 size_t SpaceUsedLong(const Message& message) const;
284
285 bool HasField(const Message& message, const FieldDescriptor* field) const;
286 int FieldSize(const Message& message, const FieldDescriptor* field) const;
287 void ClearField(Message* message, const FieldDescriptor* field) const;
288 bool HasOneof(const Message& message,
289 const OneofDescriptor* oneof_descriptor) const;
290 void ClearOneof(Message* message, const OneofDescriptor* field) const;
291 void RemoveLast(Message* message, const FieldDescriptor* field) const;
292 Message* ReleaseLast(Message* message, const FieldDescriptor* field) const;
293 void Swap(Message* message1, Message* message2) const;
294 void SwapFields(Message* message1, Message* message2,
295 const std::vector<const FieldDescriptor*>& fields) const;
296 void SwapElements(Message* message, const FieldDescriptor* field,
297 int index1, int index2) const;
298 void ListFields(const Message& message,
299 std::vector<const FieldDescriptor*>* output) const;
300
301 int32 GetInt32 (const Message& message,
302 const FieldDescriptor* field) const;
303 int64 GetInt64 (const Message& message,
304 const FieldDescriptor* field) const;
305 uint32 GetUInt32(const Message& message,
306 const FieldDescriptor* field) const;
307 uint64 GetUInt64(const Message& message,
308 const FieldDescriptor* field) const;
309 float GetFloat (const Message& message,
310 const FieldDescriptor* field) const;
311 double GetDouble(const Message& message,
312 const FieldDescriptor* field) const;
313 bool GetBool (const Message& message,
314 const FieldDescriptor* field) const;
315 string GetString(const Message& message,
316 const FieldDescriptor* field) const;
317 const string& GetStringReference(const Message& message,
318 const FieldDescriptor* field,
319 string* scratch) const;
320 const EnumValueDescriptor* GetEnum(const Message& message,
321 const FieldDescriptor* field) const;
322 int GetEnumValue(const Message& message,
323 const FieldDescriptor* field) const;
324 const Message& GetMessage(const Message& message,
325 const FieldDescriptor* field,
326 MessageFactory* factory = NULL) const;
327
328 const FieldDescriptor* GetOneofFieldDescriptor(
329 const Message& message,
330 const OneofDescriptor* oneof_descriptor) const;
331
332 private:
333 bool ContainsMapKey(const Message& message,
334 const FieldDescriptor* field,
335 const MapKey& key) const;
336 bool InsertOrLookupMapValue(Message* message,
337 const FieldDescriptor* field,
338 const MapKey& key,
339 MapValueRef* val) const;
340 bool DeleteMapValue(Message* message,
341 const FieldDescriptor* field,
342 const MapKey& key) const;
343 MapIterator MapBegin(
344 Message* message,
345 const FieldDescriptor* field) const;
346 MapIterator MapEnd(
347 Message* message,
348 const FieldDescriptor* field) const;
349 int MapSize(const Message& message, const FieldDescriptor* field) const;
350
351 public:
352 void SetInt32 (Message* message,
353 const FieldDescriptor* field, int32 value) const;
354 void SetInt64 (Message* message,
355 const FieldDescriptor* field, int64 value) const;
356 void SetUInt32(Message* message,
357 const FieldDescriptor* field, uint32 value) const;
358 void SetUInt64(Message* message,
359 const FieldDescriptor* field, uint64 value) const;
360 void SetFloat (Message* message,
361 const FieldDescriptor* field, float value) const;
362 void SetDouble(Message* message,
363 const FieldDescriptor* field, double value) const;
364 void SetBool (Message* message,
365 const FieldDescriptor* field, bool value) const;
366 void SetString(Message* message,
367 const FieldDescriptor* field,
368 const string& value) const;
369 void SetEnum (Message* message, const FieldDescriptor* field,
370 const EnumValueDescriptor* value) const;
371 void SetEnumValue(Message* message, const FieldDescriptor* field,
372 int value) const;
373 Message* MutableMessage(Message* message, const FieldDescriptor* field,
374 MessageFactory* factory = NULL) const;
375 void SetAllocatedMessage(Message* message,
376 Message* sub_message,
377 const FieldDescriptor* field) const;
378 Message* ReleaseMessage(Message* message, const FieldDescriptor* field,
379 MessageFactory* factory = NULL) const;
380
381 int32 GetRepeatedInt32 (const Message& message,
382 const FieldDescriptor* field, int index) const;
383 int64 GetRepeatedInt64 (const Message& message,
384 const FieldDescriptor* field, int index) const;
385 uint32 GetRepeatedUInt32(const Message& message,
386 const FieldDescriptor* field, int index) const;
387 uint64 GetRepeatedUInt64(const Message& message,
388 const FieldDescriptor* field, int index) const;
389 float GetRepeatedFloat (const Message& message,
390 const FieldDescriptor* field, int index) const;
391 double GetRepeatedDouble(const Message& message,
392 const FieldDescriptor* field, int index) const;
393 bool GetRepeatedBool (const Message& message,
394 const FieldDescriptor* field, int index) const;
395 string GetRepeatedString(const Message& message,
396 const FieldDescriptor* field, int index) const;
397 const string& GetRepeatedStringReference(const Message& message,
398 const FieldDescriptor* field,
399 int index, string* scratch) const;
400 const EnumValueDescriptor* GetRepeatedEnum(const Message& message,
401 const FieldDescriptor* field,
402 int index) const;
403 int GetRepeatedEnumValue(const Message& message,
404 const FieldDescriptor* field,
405 int index) const;
406 const Message& GetRepeatedMessage(const Message& message,
407 const FieldDescriptor* field,
408 int index) const;
409
410 // Set the value of a field.
411 void SetRepeatedInt32 (Message* message,
412 const FieldDescriptor* field, int index, int32 value) const;
413 void SetRepeatedInt64 (Message* message,
414 const FieldDescriptor* field, int index, int64 value) const;
415 void SetRepeatedUInt32(Message* message,
416 const FieldDescriptor* field, int index, uint32 value) const;
417 void SetRepeatedUInt64(Message* message,
418 const FieldDescriptor* field, int index, uint64 value) const;
419 void SetRepeatedFloat (Message* message,
420 const FieldDescriptor* field, int index, float value) const;
421 void SetRepeatedDouble(Message* message,
422 const FieldDescriptor* field, int index, double value) const;
423 void SetRepeatedBool (Message* message,
424 const FieldDescriptor* field, int index, bool value) const;
425 void SetRepeatedString(Message* message,
426 const FieldDescriptor* field, int index,
427 const string& value) const;
428 void SetRepeatedEnum(Message* message, const FieldDescriptor* field,
429 int index, const EnumValueDescriptor* value) const;
430 void SetRepeatedEnumValue(Message* message, const FieldDescriptor* field,
431 int index, int value) const;
432 // Get a mutable pointer to a field with a message type.
433 Message* MutableRepeatedMessage(Message* message,
434 const FieldDescriptor* field,
435 int index) const;
436
437 void AddInt32 (Message* message,
438 const FieldDescriptor* field, int32 value) const;
439 void AddInt64 (Message* message,
440 const FieldDescriptor* field, int64 value) const;
441 void AddUInt32(Message* message,
442 const FieldDescriptor* field, uint32 value) const;
443 void AddUInt64(Message* message,
444 const FieldDescriptor* field, uint64 value) const;
445 void AddFloat (Message* message,
446 const FieldDescriptor* field, float value) const;
447 void AddDouble(Message* message,
448 const FieldDescriptor* field, double value) const;
449 void AddBool (Message* message,
450 const FieldDescriptor* field, bool value) const;
451 void AddString(Message* message,
452 const FieldDescriptor* field, const string& value) const;
453 void AddEnum(Message* message,
454 const FieldDescriptor* field,
455 const EnumValueDescriptor* value) const;
456 void AddEnumValue(Message* message,
457 const FieldDescriptor* field,
458 int value) const;
459 Message* AddMessage(Message* message, const FieldDescriptor* field,
460 MessageFactory* factory = NULL) const;
461 void AddAllocatedMessage(
462 Message* message, const FieldDescriptor* field,
463 Message* new_entry) const;
464
465 const FieldDescriptor* FindKnownExtensionByName(const string& name) const;
466 const FieldDescriptor* FindKnownExtensionByNumber(int number) const;
467
468 bool SupportsUnknownEnumValues() const;
469
470 // This value for arena_offset_ indicates that there is no arena pointer in
471 // this message (e.g., old generated code).
472 static const int kNoArenaPointer = -1;
473
474 // This value for unknown_field_offset_ indicates that there is no
475 // UnknownFieldSet in this message, and that instead, we are using the
476 // Zero-Overhead Arena Pointer trick. When this is the case, arena_offset_
477 // actually indexes to an InternalMetadataWithArena instance, which can return
478 // either an arena pointer or an UnknownFieldSet or both. It is never the case
479 // that unknown_field_offset_ == kUnknownFieldSetInMetadata && arena_offset_
480 // == kNoArenaPointer.
481 static const int kUnknownFieldSetInMetadata = -1;
482
483 protected:
484 void* MutableRawRepeatedField(
485 Message* message, const FieldDescriptor* field, FieldDescriptor::CppType,
486 int ctype, const Descriptor* desc) const;
487
488 const void* GetRawRepeatedField(
489 const Message& message, const FieldDescriptor* field,
490 FieldDescriptor::CppType, int ctype,
491 const Descriptor* desc) const;
492
493 virtual MessageFactory* GetMessageFactory() const;
494
495 virtual void* RepeatedFieldData(
496 Message* message, const FieldDescriptor* field,
497 FieldDescriptor::CppType cpp_type,
498 const Descriptor* message_type) const;
499
500 private:
501 friend class google::protobuf::flat::MetadataBuilder;
502 friend class upb::google_opensource::GMR_Handlers;
503
504 const Descriptor* const descriptor_;
505 const ReflectionSchema schema_;
506 const DescriptorPool* const descriptor_pool_;
507 MessageFactory* const message_factory_;
508
509 // Last non weak field index. This is an optimization when most weak fields
510 // are at the end of the containing message. If a message proto doesn't
511 // contain weak fields, then this field equals descriptor_->field_count().
512 int last_non_weak_field_index_;
513
514 template <class T>
515 const T& GetRawNonOneof(const Message& message,
516 const FieldDescriptor* field) const;
517 template <class T>
518 T* MutableRawNonOneof(Message* message, const FieldDescriptor* field) const;
519
520 template <typename Type>
521 const Type& GetRaw(const Message& message,
522 const FieldDescriptor* field) const;
523 template <typename Type>
524 inline Type* MutableRaw(Message* message,
525 const FieldDescriptor* field) const;
526 template <typename Type>
527 inline const Type& DefaultRaw(const FieldDescriptor* field) const;
528
529 inline const uint32* GetHasBits(const Message& message) const;
530 inline uint32* MutableHasBits(Message* message) const;
531 inline uint32 GetOneofCase(
532 const Message& message,
533 const OneofDescriptor* oneof_descriptor) const;
534 inline uint32* MutableOneofCase(
535 Message* message,
536 const OneofDescriptor* oneof_descriptor) const;
537 inline const ExtensionSet& GetExtensionSet(const Message& message) const;
538 inline ExtensionSet* MutableExtensionSet(Message* message) const;
539 inline Arena* GetArena(Message* message) const;
540
541 inline const InternalMetadataWithArena& GetInternalMetadataWithArena(
542 const Message& message) const;
543
544 inline InternalMetadataWithArena*
545 MutableInternalMetadataWithArena(Message* message) const;
546
547 inline bool HasBit(const Message& message,
548 const FieldDescriptor* field) const;
549 inline void SetBit(Message* message,
550 const FieldDescriptor* field) const;
551 inline void ClearBit(Message* message,
552 const FieldDescriptor* field) const;
553 inline void SwapBit(Message* message1,
554 Message* message2,
555 const FieldDescriptor* field) const;
556
557 // This function only swaps the field. Should swap corresponding has_bit
558 // before or after using this function.
559 void SwapField(Message* message1,
560 Message* message2,
561 const FieldDescriptor* field) const;
562
563 void SwapOneofField(Message* message1,
564 Message* message2,
565 const OneofDescriptor* oneof_descriptor) const;
566
567 inline bool HasOneofField(const Message& message,
568 const FieldDescriptor* field) const;
569 inline void SetOneofCase(Message* message,
570 const FieldDescriptor* field) const;
571 inline void ClearOneofField(Message* message,
572 const FieldDescriptor* field) const;
573
574 template <typename Type>
575 inline const Type& GetField(const Message& message,
576 const FieldDescriptor* field) const;
577 template <typename Type>
578 inline void SetField(Message* message,
579 const FieldDescriptor* field, const Type& value) const;
580 template <typename Type>
581 inline Type* MutableField(Message* message,
582 const FieldDescriptor* field) const;
583 template <typename Type>
584 inline const Type& GetRepeatedField(const Message& message,
585 const FieldDescriptor* field,
586 int index) const;
587 template <typename Type>
588 inline const Type& GetRepeatedPtrField(const Message& message,
589 const FieldDescriptor* field,
590 int index) const;
591 template <typename Type>
592 inline void SetRepeatedField(Message* message,
593 const FieldDescriptor* field, int index,
594 Type value) const;
595 template <typename Type>
596 inline Type* MutableRepeatedField(Message* message,
597 const FieldDescriptor* field,
598 int index) const;
599 template <typename Type>
600 inline void AddField(Message* message,
601 const FieldDescriptor* field, const Type& value) const;
602 template <typename Type>
603 inline Type* AddField(Message* message,
604 const FieldDescriptor* field) const;
605
606 int GetExtensionNumberOrDie(const Descriptor* type) const;
607
608 // Internal versions of EnumValue API perform no checking. Called after checks
609 // by public methods.
610 void SetEnumValueInternal(Message* message,
611 const FieldDescriptor* field,
612 int value) const;
613 void SetRepeatedEnumValueInternal(Message* message,
614 const FieldDescriptor* field,
615 int index,
616 int value) const;
617 void AddEnumValueInternal(Message* message,
618 const FieldDescriptor* field,
619 int value) const;
620
621
622 Message* UnsafeArenaReleaseMessage(Message* message,
623 const FieldDescriptor* field,
624 MessageFactory* factory = NULL) const;
625
626 void UnsafeArenaSetAllocatedMessage(Message* message,
627 Message* sub_message,
628 const FieldDescriptor* field) const;
629
630 internal::MapFieldBase* MapData(
631 Message* message, const FieldDescriptor* field) const;
632
633 friend inline // inline so nobody can call this function.
634 void
635 RegisterAllTypesInternal(const Metadata* file_level_metadata, int size);
636 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratedMessageReflection);
637 };
638
639 // There are some places in proto2 where dynamic_cast would be useful as an
640 // optimization. For example, take Message::MergeFrom(const Message& other).
641 // For a given generated message FooMessage, we generate these two methods:
642 // void MergeFrom(const FooMessage& other);
643 // void MergeFrom(const Message& other);
644 // The former method can be implemented directly in terms of FooMessage's
645 // inline accessors, but the latter method must work with the reflection
646 // interface. However, if the parameter to the latter method is actually of
647 // type FooMessage, then we'd like to be able to just call the other method
648 // as an optimization. So, we use dynamic_cast to check this.
649 //
650 // That said, dynamic_cast requires RTTI, which many people like to disable
651 // for performance and code size reasons. When RTTI is not available, we
652 // still need to produce correct results. So, in this case we have to fall
653 // back to using reflection, which is what we would have done anyway if the
654 // objects were not of the exact same class.
655 //
656 // dynamic_cast_if_available() implements this logic. If RTTI is
657 // enabled, it does a dynamic_cast. If RTTI is disabled, it just returns
658 // NULL.
659 //
660 // If you need to compile without RTTI, simply #define GOOGLE_PROTOBUF_NO_RTTI.
661 // On MSVC, this should be detected automatically.
662 template<typename To, typename From>
dynamic_cast_if_available(From from)663 inline To dynamic_cast_if_available(From from) {
664 #if defined(GOOGLE_PROTOBUF_NO_RTTI) || (defined(_MSC_VER)&&!defined(_CPPRTTI))
665 // Avoid the compiler warning about unused variables.
666 (void)from;
667 return NULL;
668 #else
669 return dynamic_cast<To>(from);
670 #endif
671 }
672
673 // Tries to downcast this message to a generated message type.
674 // Returns NULL if this class is not an instance of T.
675 //
676 // This is like dynamic_cast_if_available, except it works even when
677 // dynamic_cast is not available by using Reflection. However it only works
678 // with Message objects.
679 //
680 // TODO(haberman): can we remove dynamic_cast_if_available in favor of this?
681 template <typename T>
DynamicCastToGenerated(const Message * from)682 T* DynamicCastToGenerated(const Message* from) {
683 // Compile-time assert that T is a generated type that has a
684 // default_instance() accessor, but avoid actually calling it.
685 const T&(*get_default_instance)() = &T::default_instance;
686 (void)get_default_instance;
687
688 // Compile-time assert that T is a subclass of google::protobuf::Message.
689 const Message* unused = static_cast<T*>(NULL);
690 (void)unused;
691
692 #if defined(GOOGLE_PROTOBUF_NO_RTTI) || \
693 (defined(_MSC_VER) && !defined(_CPPRTTI))
694 bool ok = &T::default_instance() ==
695 from->GetReflection()->GetMessageFactory()->GetPrototype(
696 from->GetDescriptor());
697 return ok ? down_cast<T*>(from) : NULL;
698 #else
699 return dynamic_cast<T*>(from);
700 #endif
701 }
702
703 template <typename T>
DynamicCastToGenerated(Message * from)704 T* DynamicCastToGenerated(Message* from) {
705 const Message* message_const = from;
706 return const_cast<T*>(DynamicCastToGenerated<const T>(message_const));
707 }
708
709 LIBPROTOBUF_EXPORT void AssignDescriptors(
710 const string& filename, const MigrationSchema* schemas,
711 const Message* const* default_instances_, const uint32* offsets,
712 MessageFactory* factory,
713 // update the following descriptor arrays.
714 Metadata* file_level_metadata,
715 const EnumDescriptor** file_level_enum_descriptors,
716 const ServiceDescriptor** file_level_service_descriptors);
717
718 LIBPROTOBUF_EXPORT void RegisterAllTypes(const Metadata* file_level_metadata, int size);
719
720 // These cannot be in lite so we put them in the reflection.
721 LIBPROTOBUF_EXPORT void UnknownFieldSetSerializer(const uint8* base, uint32 offset, uint32 tag,
722 uint32 has_offset,
723 ::google::protobuf::io::CodedOutputStream* output);
724
725 } // namespace internal
726 } // namespace protobuf
727
728 } // namespace google
729 #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__
730