Warning, file /include/google/protobuf/message.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092 #ifndef GOOGLE_PROTOBUF_MESSAGE_H__
0093 #define GOOGLE_PROTOBUF_MESSAGE_H__
0094
0095 #include <cstddef>
0096 #include <cstdint>
0097 #include <memory>
0098 #include <optional>
0099 #include <string>
0100 #include <type_traits>
0101 #include <vector>
0102
0103 #include "absl/base/attributes.h"
0104 #include "absl/base/call_once.h"
0105 #include "absl/base/macros.h"
0106 #include "absl/base/optimization.h"
0107 #include "absl/log/absl_check.h"
0108 #include "absl/memory/memory.h"
0109 #include "absl/strings/cord.h"
0110 #include "absl/strings/string_view.h"
0111 #include "google/protobuf/arena.h"
0112 #include "google/protobuf/descriptor.h"
0113 #include "google/protobuf/generated_message_reflection.h"
0114 #include "google/protobuf/generated_message_tctable_decl.h"
0115 #include "google/protobuf/generated_message_util.h"
0116 #include "google/protobuf/map.h" // TODO: cleanup
0117 #include "google/protobuf/message_lite.h"
0118 #include "google/protobuf/port.h"
0119 #include "google/protobuf/reflection.h"
0120
0121
0122 #include "google/protobuf/port_def.inc"
0123
0124 #ifdef SWIG
0125 #error "You cannot SWIG proto headers"
0126 #endif
0127
0128 namespace google {
0129 namespace protobuf {
0130
0131
0132 class Message;
0133 class Reflection;
0134 class MessageFactory;
0135
0136
0137 class AssignDescriptorsHelper;
0138 class ConstMapIterator;
0139 class DynamicMessageFactory;
0140 class GeneratedMessageReflectionTestHelper;
0141 class MapKey;
0142 class MapValueConstRef;
0143 class MapValueRef;
0144 class MapIterator;
0145 class MapReflectionTester;
0146 class TextFormat;
0147
0148 namespace internal {
0149 struct FuzzPeer;
0150 struct DescriptorTable;
0151 template <bool is_oneof>
0152 struct DynamicFieldInfoHelper;
0153 class MapFieldBase;
0154 class MessageUtil;
0155 class ReflectionVisit;
0156 class SwapFieldHelper;
0157 class CachedSize;
0158 struct TailCallTableInfo;
0159 template <typename MessageT, typename FieldT>
0160 struct RepeatedEntityDynamicFieldInfoBase;
0161 template <typename MessageT, typename FieldT>
0162 struct RepeatedPtrEntityDynamicFieldInfoBase;
0163
0164 namespace field_layout {
0165 enum TransformValidation : uint16_t;
0166 }
0167
0168 namespace v2 {
0169 class V2TableGenTester;
0170 }
0171 }
0172 class UnknownFieldSet;
0173 namespace io {
0174 class EpsCopyOutputStream;
0175 class ZeroCopyInputStream;
0176 class ZeroCopyOutputStream;
0177 class CodedInputStream;
0178 class CodedOutputStream;
0179 }
0180 namespace python {
0181 class MapReflectionFriend;
0182 class MessageReflectionFriend;
0183 }
0184 namespace expr {
0185 class CelMapReflectionFriend;
0186 class SudoMapReflectionFriend;
0187 }
0188
0189 namespace internal {
0190 class MapFieldPrinterHelper;
0191 PROTOBUF_EXPORT std::string StringifyMessage(
0192 const Message& message);
0193 }
0194 PROTOBUF_EXPORT std::string ShortFormat(
0195 const Message& message);
0196 PROTOBUF_EXPORT std::string Utf8Format(
0197 const Message& message);
0198 namespace util {
0199 class MessageDifferencer;
0200 }
0201
0202
0203 namespace internal {
0204 class ReflectionAccessor;
0205 class ReflectionOps;
0206 class MapKeySorter;
0207 class WireFormat;
0208 class MapFieldReflectionTest;
0209 }
0210
0211 template <typename T>
0212 class RepeatedField;
0213
0214 template <typename T>
0215 class RepeatedPtrField;
0216
0217
0218 struct Metadata {
0219 const Descriptor* descriptor;
0220 const Reflection* reflection;
0221 };
0222
0223 namespace internal {
0224 template <class To>
0225 inline To* GetPointerAtOffset(void* message, uint32_t offset) {
0226 return reinterpret_cast<To*>(reinterpret_cast<char*>(message) + offset);
0227 }
0228
0229 template <class To>
0230 const To* GetConstPointerAtOffset(const void* message, uint32_t offset) {
0231 return reinterpret_cast<const To*>(reinterpret_cast<const char*>(message) +
0232 offset);
0233 }
0234
0235 template <class To>
0236 const To& GetConstRefAtOffset(const Message& message, uint32_t offset) {
0237 return *GetConstPointerAtOffset<To>(&message, offset);
0238 }
0239
0240 bool CreateUnknownEnumValues(const FieldDescriptor* field);
0241
0242
0243 PROTOBUF_EXPORT bool IsDescendant(Message& root, const Message& message);
0244
0245 inline void MaybePoisonAfterClear(Message* root);
0246 }
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261 class PROTOBUF_EXPORT Message : public MessageLite {
0262 public:
0263 Message(const Message&) = delete;
0264 Message& operator=(const Message&) = delete;
0265
0266
0267
0268
0269
0270
0271 Message* New() const { return New(nullptr); }
0272
0273
0274
0275 Message* New(Arena* arena) const {
0276 return static_cast<Message*>(MessageLite::New(arena));
0277 }
0278
0279
0280
0281
0282 void CopyFrom(const Message& from);
0283
0284
0285
0286
0287
0288
0289 void MergeFrom(const Message& from);
0290
0291
0292
0293 void CheckInitialized() const;
0294
0295
0296
0297
0298
0299 void FindInitializationErrors(std::vector<std::string>* errors) const;
0300
0301
0302
0303 std::string InitializationErrorString() const;
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315 void DiscardUnknownFields();
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328 size_t SpaceUsedLong() const;
0329
0330 [[deprecated("Please use SpaceUsedLong() instead")]] int SpaceUsed() const {
0331 return internal::ToIntSize(SpaceUsedLong());
0332 }
0333
0334
0335
0336
0337
0338
0339
0340
0341 std::string DebugString() const;
0342
0343 std::string ShortDebugString() const;
0344
0345 std::string Utf8DebugString() const;
0346
0347 void PrintDebugString() const;
0348
0349
0350
0351
0352 template <typename Sink>
0353 friend void AbslStringify(Sink& sink, const google::protobuf::Message& message) {
0354 sink.Append(internal::StringifyMessage(message));
0355 }
0356
0357
0358
0359
0360 #if !defined(PROTOBUF_CUSTOM_VTABLE)
0361 void Clear() override;
0362
0363 size_t ByteSizeLong() const override;
0364 uint8_t* _InternalSerialize(uint8_t* target,
0365 io::EpsCopyOutputStream* stream) const override;
0366 #endif
0367
0368
0369
0370
0371
0372
0373
0374 const Descriptor* GetDescriptor() const { return GetMetadata().descriptor; }
0375
0376
0377
0378
0379
0380 const Reflection* GetReflection() const { return GetMetadata().reflection; }
0381
0382 protected:
0383 #if !defined(PROTOBUF_CUSTOM_VTABLE)
0384 constexpr Message() {}
0385 #endif
0386 using MessageLite::MessageLite;
0387
0388
0389
0390 Metadata GetMetadata() const;
0391 static Metadata GetMetadataImpl(const internal::ClassDataFull& data);
0392
0393
0394 static bool IsInitializedImpl(const MessageLite&);
0395
0396 size_t ComputeUnknownFieldsSize(
0397 size_t total_size, const internal::CachedSize* cached_size) const;
0398 size_t MaybeComputeUnknownFieldsSize(
0399 size_t total_size, const internal::CachedSize* cached_size) const;
0400
0401
0402
0403 static absl::string_view GetTypeNameImpl(const internal::ClassData* data);
0404 static void MergeImpl(MessageLite& to, const MessageLite& from);
0405 void ClearImpl();
0406 static size_t ByteSizeLongImpl(const MessageLite& msg);
0407 static uint8_t* _InternalSerializeImpl(const MessageLite& msg,
0408 uint8_t* target,
0409 io::EpsCopyOutputStream* stream);
0410
0411 static const internal::TcParseTableBase* GetTcParseTableImpl(
0412 const MessageLite& msg);
0413
0414 static size_t SpaceUsedLongImpl(const MessageLite& msg_lite);
0415
0416 static const internal::DescriptorMethods kDescriptorMethods;
0417
0418 };
0419
0420 namespace internal {
0421
0422 void* CreateSplitMessageGeneric(Arena* arena, const void* default_split,
0423 size_t size, const void* message,
0424 const void* default_message);
0425
0426
0427
0428 class RepeatedFieldAccessor;
0429 }
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469 class PROTOBUF_EXPORT Reflection final {
0470 public:
0471 Reflection(const Reflection&) = delete;
0472 Reflection& operator=(const Reflection&) = delete;
0473 ~Reflection();
0474
0475
0476
0477
0478 const UnknownFieldSet& GetUnknownFields(const Message& message) const;
0479
0480
0481
0482 UnknownFieldSet* MutableUnknownFields(Message* message) const;
0483
0484
0485 size_t SpaceUsedLong(const Message& message) const;
0486
0487 [[deprecated("Please use SpaceUsedLong() instead")]] int SpaceUsed(
0488 const Message& message) const {
0489 return internal::ToIntSize(SpaceUsedLong(message));
0490 }
0491
0492
0493 bool IsDefaultInstance(const Message& message) const {
0494 ABSL_DCHECK_EQ(message.GetReflection(), this);
0495 return schema_.IsDefaultInstance(message);
0496 }
0497
0498
0499 bool HasField(const Message& message, const FieldDescriptor* field) const;
0500
0501
0502 int FieldSize(const Message& message, const FieldDescriptor* field) const;
0503
0504
0505
0506 void ClearField(Message* message, const FieldDescriptor* field) const;
0507
0508
0509
0510 bool HasOneof(const Message& message,
0511 const OneofDescriptor* oneof_descriptor) const;
0512
0513 void ClearOneof(Message* message,
0514 const OneofDescriptor* oneof_descriptor) const;
0515
0516
0517 const FieldDescriptor* GetOneofFieldDescriptor(
0518 const Message& message, const OneofDescriptor* oneof_descriptor) const;
0519
0520
0521
0522
0523
0524
0525
0526
0527 void RemoveLast(Message* message, const FieldDescriptor* field) const;
0528
0529
0530 [[nodiscard]] Message* ReleaseLast(Message* message,
0531 const FieldDescriptor* field) const;
0532
0533
0534
0535
0536 Message* UnsafeArenaReleaseLast(Message* message,
0537 const FieldDescriptor* field) const;
0538
0539
0540 void Swap(Message* message1, Message* message2) const;
0541
0542
0543 void SwapFields(Message* message1, Message* message2,
0544 const std::vector<const FieldDescriptor*>& fields) const;
0545
0546
0547 void SwapElements(Message* message, const FieldDescriptor* field, int index1,
0548 int index2) const;
0549
0550
0551
0552 void UnsafeArenaSwap(Message* lhs, Message* rhs) const;
0553
0554
0555
0556 void UnsafeArenaSwapFields(
0557 Message* lhs, Message* rhs,
0558 const std::vector<const FieldDescriptor*>& fields) const;
0559
0560
0561
0562
0563
0564
0565
0566
0567
0568 void ListFields(const Message& message,
0569 std::vector<const FieldDescriptor*>* output) const;
0570
0571
0572
0573
0574
0575 int32_t GetInt32(const Message& message, const FieldDescriptor* field) const;
0576 int64_t GetInt64(const Message& message, const FieldDescriptor* field) const;
0577 uint32_t GetUInt32(const Message& message,
0578 const FieldDescriptor* field) const;
0579 uint64_t GetUInt64(const Message& message,
0580 const FieldDescriptor* field) const;
0581 float GetFloat(const Message& message, const FieldDescriptor* field) const;
0582 double GetDouble(const Message& message, const FieldDescriptor* field) const;
0583 bool GetBool(const Message& message, const FieldDescriptor* field) const;
0584 std::string GetString(const Message& message,
0585 const FieldDescriptor* field) const;
0586 const EnumValueDescriptor* GetEnum(const Message& message,
0587 const FieldDescriptor* field) const;
0588
0589
0590
0591
0592
0593
0594 int GetEnumValue(const Message& message, const FieldDescriptor* field) const;
0595
0596
0597 const Message& GetMessage(const Message& message,
0598 const FieldDescriptor* field,
0599 MessageFactory* factory = nullptr) const;
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614
0615
0616 const std::string& GetStringReference(const Message& message,
0617 const FieldDescriptor* field,
0618 std::string* scratch) const;
0619
0620
0621
0622
0623
0624 absl::Cord GetCord(const Message& message,
0625 const FieldDescriptor* field) const;
0626
0627
0628
0629
0630 class ScratchSpace {
0631 public:
0632 ScratchSpace() = default;
0633
0634 ScratchSpace(const ScratchSpace&) = delete;
0635 ScratchSpace& operator=(const ScratchSpace&) = delete;
0636
0637 private:
0638 friend class Reflection;
0639
0640 absl::string_view CopyFromCord(const absl::Cord& cord) {
0641 if (auto flat = cord.TryFlat()) {
0642 return *flat;
0643 }
0644 if (!buffer_) {
0645 buffer_ = absl::make_unique<std::string>();
0646 }
0647 absl::CopyCordToString(cord, buffer_.get());
0648 return *buffer_;
0649 }
0650
0651 std::unique_ptr<std::string> buffer_;
0652 };
0653
0654
0655
0656
0657 absl::string_view GetStringView(
0658 const Message& message, const FieldDescriptor* field,
0659 ScratchSpace& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) const;
0660
0661
0662
0663
0664
0665 void SetInt32(Message* message, const FieldDescriptor* field,
0666 int32_t value) const;
0667 void SetInt64(Message* message, const FieldDescriptor* field,
0668 int64_t value) const;
0669 void SetUInt32(Message* message, const FieldDescriptor* field,
0670 uint32_t value) const;
0671 void SetUInt64(Message* message, const FieldDescriptor* field,
0672 uint64_t value) const;
0673 void SetFloat(Message* message, const FieldDescriptor* field,
0674 float value) const;
0675 void SetDouble(Message* message, const FieldDescriptor* field,
0676 double value) const;
0677 void SetBool(Message* message, const FieldDescriptor* field,
0678 bool value) const;
0679 void SetString(Message* message, const FieldDescriptor* field,
0680 std::string value) const;
0681
0682
0683
0684 void SetString(Message* message, const FieldDescriptor* field,
0685 const absl::Cord& value) const;
0686 void SetEnum(Message* message, const FieldDescriptor* field,
0687 const EnumValueDescriptor* value) const;
0688
0689
0690
0691
0692
0693
0694
0695 void SetEnumValue(Message* message, const FieldDescriptor* field,
0696 int value) const;
0697
0698
0699
0700
0701
0702
0703
0704
0705
0706
0707
0708 Message* MutableMessage(Message* message, const FieldDescriptor* field,
0709 MessageFactory* factory = nullptr) const;
0710
0711
0712
0713
0714
0715 void SetAllocatedMessage(Message* message, Message* sub_message,
0716 const FieldDescriptor* field) const;
0717
0718
0719
0720
0721 void UnsafeArenaSetAllocatedMessage(Message* message, Message* sub_message,
0722 const FieldDescriptor* field) const;
0723
0724
0725
0726
0727
0728
0729
0730
0731 [[nodiscard]] Message* ReleaseMessage(
0732 Message* message, const FieldDescriptor* field,
0733 MessageFactory* factory = nullptr) const;
0734
0735
0736
0737
0738 Message* UnsafeArenaReleaseMessage(Message* message,
0739 const FieldDescriptor* field,
0740 MessageFactory* factory = nullptr) const;
0741
0742
0743
0744
0745
0746 int32_t GetRepeatedInt32(const Message& message, const FieldDescriptor* field,
0747 int index) const;
0748 int64_t GetRepeatedInt64(const Message& message, const FieldDescriptor* field,
0749 int index) const;
0750 uint32_t GetRepeatedUInt32(const Message& message,
0751 const FieldDescriptor* field, int index) const;
0752 uint64_t GetRepeatedUInt64(const Message& message,
0753 const FieldDescriptor* field, int index) const;
0754 float GetRepeatedFloat(const Message& message, const FieldDescriptor* field,
0755 int index) const;
0756 double GetRepeatedDouble(const Message& message, const FieldDescriptor* field,
0757 int index) const;
0758 bool GetRepeatedBool(const Message& message, const FieldDescriptor* field,
0759 int index) const;
0760 std::string GetRepeatedString(const Message& message,
0761 const FieldDescriptor* field, int index) const;
0762 const EnumValueDescriptor* GetRepeatedEnum(const Message& message,
0763 const FieldDescriptor* field,
0764 int index) const;
0765
0766
0767
0768
0769
0770 int GetRepeatedEnumValue(const Message& message, const FieldDescriptor* field,
0771 int index) const;
0772 const Message& GetRepeatedMessage(const Message& message,
0773 const FieldDescriptor* field,
0774 int index) const;
0775
0776
0777 const std::string& GetRepeatedStringReference(const Message& message,
0778 const FieldDescriptor* field,
0779 int index,
0780 std::string* scratch) const;
0781
0782
0783 absl::string_view GetRepeatedStringView(
0784 const Message& message, const FieldDescriptor* field, int index,
0785 ScratchSpace& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) const;
0786
0787
0788
0789
0790
0791 void SetRepeatedInt32(Message* message, const FieldDescriptor* field,
0792 int index, int32_t value) const;
0793 void SetRepeatedInt64(Message* message, const FieldDescriptor* field,
0794 int index, int64_t value) const;
0795 void SetRepeatedUInt32(Message* message, const FieldDescriptor* field,
0796 int index, uint32_t value) const;
0797 void SetRepeatedUInt64(Message* message, const FieldDescriptor* field,
0798 int index, uint64_t value) const;
0799 void SetRepeatedFloat(Message* message, const FieldDescriptor* field,
0800 int index, float value) const;
0801 void SetRepeatedDouble(Message* message, const FieldDescriptor* field,
0802 int index, double value) const;
0803 void SetRepeatedBool(Message* message, const FieldDescriptor* field,
0804 int index, bool value) const;
0805 void SetRepeatedString(Message* message, const FieldDescriptor* field,
0806 int index, std::string value) const;
0807 void SetRepeatedEnum(Message* message, const FieldDescriptor* field,
0808 int index, const EnumValueDescriptor* value) const;
0809
0810
0811
0812
0813
0814
0815
0816 void SetRepeatedEnumValue(Message* message, const FieldDescriptor* field,
0817 int index, int value) const;
0818
0819
0820 Message* MutableRepeatedMessage(Message* message,
0821 const FieldDescriptor* field,
0822 int index) const;
0823
0824
0825
0826
0827
0828 void AddInt32(Message* message, const FieldDescriptor* field,
0829 int32_t value) const;
0830 void AddInt64(Message* message, const FieldDescriptor* field,
0831 int64_t value) const;
0832 void AddUInt32(Message* message, const FieldDescriptor* field,
0833 uint32_t value) const;
0834 void AddUInt64(Message* message, const FieldDescriptor* field,
0835 uint64_t value) const;
0836 void AddFloat(Message* message, const FieldDescriptor* field,
0837 float value) const;
0838 void AddDouble(Message* message, const FieldDescriptor* field,
0839 double value) const;
0840 void AddBool(Message* message, const FieldDescriptor* field,
0841 bool value) const;
0842 void AddString(Message* message, const FieldDescriptor* field,
0843 std::string value) const;
0844 void AddEnum(Message* message, const FieldDescriptor* field,
0845 const EnumValueDescriptor* value) const;
0846
0847
0848
0849
0850
0851
0852
0853
0854 void AddEnumValue(Message* message, const FieldDescriptor* field,
0855 int value) const;
0856
0857 Message* AddMessage(Message* message, const FieldDescriptor* field,
0858 MessageFactory* factory = nullptr) const;
0859
0860
0861
0862 void AddAllocatedMessage(Message* message, const FieldDescriptor* field,
0863 Message* new_entry) const;
0864
0865
0866
0867
0868 void UnsafeArenaAddAllocatedMessage(Message* message,
0869 const FieldDescriptor* field,
0870 Message* new_entry) const;
0871
0872
0873
0874
0875
0876
0877
0878
0879
0880
0881
0882
0883
0884
0885
0886
0887
0888
0889
0890
0891
0892
0893
0894
0895
0896 template <typename T>
0897 RepeatedFieldRef<T> GetRepeatedFieldRef(const Message& message,
0898 const FieldDescriptor* field) const;
0899
0900
0901
0902 template <typename T>
0903 MutableRepeatedFieldRef<T> GetMutableRepeatedFieldRef(
0904 Message* message, const FieldDescriptor* field) const;
0905
0906
0907
0908
0909
0910
0911
0912
0913
0914
0915
0916
0917
0918
0919
0920
0921
0922
0923 template <typename T>
0924 [[deprecated(
0925 "Please use GetRepeatedFieldRef() instead")]] const RepeatedField<T>&
0926 GetRepeatedField(const Message& msg, const FieldDescriptor* d) const {
0927 return GetRepeatedFieldInternal<T>(msg, d);
0928 }
0929
0930
0931
0932
0933 template <typename T>
0934 [[deprecated(
0935 "Please use GetMutableRepeatedFieldRef() instead")]] RepeatedField<T>*
0936 MutableRepeatedField(Message* msg, const FieldDescriptor* d) const {
0937 return MutableRepeatedFieldInternal<T>(msg, d);
0938 }
0939
0940
0941
0942
0943
0944 template <typename T>
0945 [[deprecated(
0946 "Please use GetRepeatedFieldRef() instead")]] const RepeatedPtrField<T>&
0947 GetRepeatedPtrField(const Message& msg, const FieldDescriptor* d) const {
0948 return GetRepeatedPtrFieldInternal<T>(msg, d);
0949 }
0950
0951
0952
0953
0954
0955 template <typename T>
0956 [[deprecated(
0957 "Please use GetMutableRepeatedFieldRef() instead")]] RepeatedPtrField<T>*
0958 MutableRepeatedPtrField(Message* msg, const FieldDescriptor* d) const {
0959 return MutableRepeatedPtrFieldInternal<T>(msg, d);
0960 }
0961
0962
0963
0964
0965
0966 const FieldDescriptor* FindKnownExtensionByName(absl::string_view name) const;
0967
0968
0969
0970 const FieldDescriptor* FindKnownExtensionByNumber(int number) const;
0971
0972
0973
0974
0975
0976
0977
0978
0979
0980
0981 MessageFactory* GetMessageFactory() const;
0982
0983 private:
0984 template <typename T>
0985 const RepeatedField<T>& GetRepeatedFieldInternal(
0986 const Message& message, const FieldDescriptor* field) const;
0987 template <typename T>
0988 RepeatedField<T>* MutableRepeatedFieldInternal(
0989 Message* message, const FieldDescriptor* field) const;
0990 template <typename T>
0991 const RepeatedPtrField<T>& GetRepeatedPtrFieldInternal(
0992 const Message& message, const FieldDescriptor* field) const;
0993 template <typename T>
0994 RepeatedPtrField<T>* MutableRepeatedPtrFieldInternal(
0995 Message* message, const FieldDescriptor* field) const;
0996
0997
0998 absl::string_view GetStringViewImpl(const Message& message,
0999 const FieldDescriptor* field,
1000 ScratchSpace* scratch) const;
1001 absl::string_view GetRepeatedStringViewImpl(const Message& message,
1002 const FieldDescriptor* field,
1003 int index,
1004 ScratchSpace* scratch) const;
1005
1006
1007
1008
1009
1010
1011 void* MutableRawRepeatedField(Message* message, const FieldDescriptor* field,
1012 FieldDescriptor::CppType cpptype, int ctype,
1013 const Descriptor* desc) const;
1014
1015 const void* GetRawRepeatedField(const Message& message,
1016 const FieldDescriptor* field,
1017 FieldDescriptor::CppType cpptype, int ctype,
1018 const Descriptor* desc) const;
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033 const void* RepeatedFieldData(const Message& message,
1034 const FieldDescriptor* field,
1035 FieldDescriptor::CppType cpp_type,
1036 const Descriptor* message_type) const;
1037 void* RepeatedFieldData(Message* message, const FieldDescriptor* field,
1038 FieldDescriptor::CppType cpp_type,
1039 const Descriptor* message_type) const;
1040
1041
1042
1043 const internal::RepeatedFieldAccessor* RepeatedFieldAccessor(
1044 const FieldDescriptor* field) const;
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054 bool IsLazyField(const FieldDescriptor* field) const {
1055 return IsLazilyVerifiedLazyField(field) ||
1056 IsEagerlyVerifiedLazyField(field);
1057 }
1058
1059
1060
1061 bool IsLazyExtension(const Message& message,
1062 const FieldDescriptor* field) const;
1063
1064 bool IsLazilyVerifiedLazyField(const FieldDescriptor* field) const;
1065 bool IsEagerlyVerifiedLazyField(const FieldDescriptor* field) const;
1066 internal::field_layout::TransformValidation GetLazyStyle(
1067 const FieldDescriptor* field) const;
1068
1069 bool IsSplit(const FieldDescriptor* field) const {
1070 return schema_.IsSplit(field);
1071 }
1072
1073
1074
1075
1076 void MaybePoisonAfterClear(Message& root) const;
1077
1078 friend class FastReflectionBase;
1079 friend class FastReflectionMessageMutator;
1080 friend class internal::ReflectionVisit;
1081 friend bool internal::IsDescendant(Message& root, const Message& message);
1082 friend void internal::MaybePoisonAfterClear(Message* root);
1083
1084 const Descriptor* const descriptor_;
1085 const internal::ReflectionSchema schema_;
1086 const DescriptorPool* const descriptor_pool_;
1087 MessageFactory* const message_factory_;
1088
1089
1090
1091
1092 int last_non_weak_field_index_;
1093
1094
1095
1096
1097 mutable absl::once_flag tcparse_table_once_;
1098 using TcParseTableBase = internal::TcParseTableBase;
1099 mutable const TcParseTableBase* tcparse_table_ = nullptr;
1100
1101 const TcParseTableBase* GetTcParseTable() const {
1102 absl::call_once(tcparse_table_once_,
1103 [&] { tcparse_table_ = CreateTcParseTable(); });
1104 return tcparse_table_;
1105 }
1106
1107 const TcParseTableBase* CreateTcParseTable() const;
1108 void PopulateTcParseFastEntries(
1109 const internal::TailCallTableInfo& table_info,
1110 TcParseTableBase::FastFieldEntry* fast_entries) const;
1111 void PopulateTcParseEntries(internal::TailCallTableInfo& table_info,
1112 TcParseTableBase::FieldEntry* entries) const;
1113 void PopulateTcParseFieldAux(const internal::TailCallTableInfo& table_info,
1114 TcParseTableBase::FieldAux* field_aux) const;
1115
1116 template <typename T, typename Enable>
1117 friend class RepeatedFieldRef;
1118 template <typename T, typename Enable>
1119 friend class MutableRepeatedFieldRef;
1120 template <typename MessageT, typename FieldT>
1121 friend struct internal::RepeatedEntityDynamicFieldInfoBase;
1122 template <typename MessageT, typename FieldT>
1123 friend struct internal::RepeatedPtrEntityDynamicFieldInfoBase;
1124 friend class Message;
1125 friend class MessageLayoutInspector;
1126 friend class AssignDescriptorsHelper;
1127 friend class DynamicMessageFactory;
1128 friend class GeneratedMessageReflectionTestHelper;
1129 friend class python::MapReflectionFriend;
1130 friend class python::MessageReflectionFriend;
1131 friend class util::MessageDifferencer;
1132 #define GOOGLE_PROTOBUF_HAS_CEL_MAP_REFLECTION_FRIEND
1133 friend class expr::CelMapReflectionFriend;
1134 friend class internal::MapFieldReflectionTest;
1135 friend class internal::MapKeySorter;
1136 friend class internal::MessageUtil;
1137 friend class internal::WireFormat;
1138 friend class internal::ReflectionOps;
1139 friend class internal::SwapFieldHelper;
1140 template <bool is_oneof>
1141 friend struct internal::DynamicFieldInfoHelper;
1142 friend struct internal::FuzzPeer;
1143
1144 friend class internal::MapFieldPrinterHelper;
1145
1146 Reflection(const Descriptor* descriptor,
1147 const internal::ReflectionSchema& schema,
1148 const DescriptorPool* pool, MessageFactory* factory);
1149
1150
1151
1152
1153
1154 const void* GetRawRepeatedString(const Message& message,
1155 const FieldDescriptor* field,
1156 bool is_string) const;
1157 void* MutableRawRepeatedString(Message* message, const FieldDescriptor* field,
1158 bool is_string) const;
1159
1160 friend class MapReflectionTester;
1161 friend class internal::v2::V2TableGenTester;
1162
1163
1164 bool ContainsMapKey(const Message& message, const FieldDescriptor* field,
1165 const MapKey& key) const;
1166
1167
1168
1169
1170
1171 bool InsertOrLookupMapValue(Message* message, const FieldDescriptor* field,
1172 const MapKey& key, MapValueRef* val) const;
1173
1174
1175
1176
1177 bool LookupMapValue(const Message& message, const FieldDescriptor* field,
1178 const MapKey& key, MapValueConstRef* val) const;
1179 bool LookupMapValue(const Message&, const FieldDescriptor*, const MapKey&,
1180 MapValueRef*) const = delete;
1181
1182
1183
1184 bool DeleteMapValue(Message* message, const FieldDescriptor* field,
1185 const MapKey& key) const;
1186
1187
1188
1189
1190 MapIterator MapBegin(Message* message, const FieldDescriptor* field) const;
1191
1192
1193
1194
1195 MapIterator MapEnd(Message* message, const FieldDescriptor* field) const;
1196
1197
1198
1199
1200 ConstMapIterator ConstMapBegin(const Message* message,
1201 const FieldDescriptor* field) const;
1202
1203
1204
1205
1206 ConstMapIterator ConstMapEnd(const Message* message,
1207 const FieldDescriptor* field) const;
1208
1209
1210
1211 int MapSize(const Message& message, const FieldDescriptor* field) const;
1212
1213
1214 template <bool>
1215 friend class MapIteratorBase;
1216 friend class WireFormatForMapFieldTest;
1217 internal::MapFieldBase* MutableMapData(Message* message,
1218 const FieldDescriptor* field) const;
1219
1220 const internal::MapFieldBase* GetMapData(const Message& message,
1221 const FieldDescriptor* field) const;
1222
1223
1224
1225
1226 template <typename T>
1227 void VerifyFieldType(const FieldDescriptor* field) const;
1228
1229 template <typename Type>
1230 const Type& GetRaw(const Message& message,
1231 const FieldDescriptor* field) const;
1232
1233 void* MutableRawSplitImpl(Message* message,
1234 const FieldDescriptor* field) const;
1235
1236 template <typename Type>
1237 Type* MutableRaw(Message* message, const FieldDescriptor* field) const;
1238
1239 template <typename Type>
1240 const Type& DefaultRaw(const FieldDescriptor* field) const;
1241
1242 const Message* GetDefaultMessageInstance(const FieldDescriptor* field) const;
1243
1244 const uint32_t* GetHasBits(const Message& message) const;
1245 inline uint32_t* MutableHasBits(Message* message) const;
1246 uint32_t GetOneofCase(const Message& message,
1247 const OneofDescriptor* oneof_descriptor) const;
1248 inline uint32_t* MutableOneofCase(
1249 Message* message, const OneofDescriptor* oneof_descriptor) const;
1250 inline bool HasExtensionSet(const Message& ) const {
1251 return schema_.HasExtensionSet();
1252 }
1253 const internal::ExtensionSet& GetExtensionSet(const Message& message) const;
1254 internal::ExtensionSet* MutableExtensionSet(Message* message) const;
1255
1256 const internal::InternalMetadata& GetInternalMetadata(
1257 const Message& message) const {
1258 return message._internal_metadata_;
1259 }
1260
1261 internal::InternalMetadata* MutableInternalMetadata(Message* message) const {
1262 return &message->_internal_metadata_;
1263 }
1264
1265 inline bool IsInlined(const FieldDescriptor* field) const {
1266 return schema_.IsFieldInlined(field);
1267 }
1268
1269 inline bool IsMicroString(const FieldDescriptor* field) const {
1270 return schema_.IsFieldMicroString(field);
1271 }
1272
1273
1274
1275 bool IsSingularFieldNonEmpty(const Message& message,
1276 const FieldDescriptor* field) const;
1277
1278
1279
1280 bool IsFieldPresentGivenHasbits(const Message& message,
1281 const FieldDescriptor* field,
1282 const uint32_t* hasbits,
1283 uint32_t hasbit_index) const;
1284
1285
1286
1287
1288
1289 bool HasFieldSingular(const Message& message,
1290 const FieldDescriptor* field) const;
1291 void SetHasBit(Message* message, const FieldDescriptor* field) const;
1292 inline void ClearHasBit(Message* message, const FieldDescriptor* field) const;
1293
1294
1295
1296
1297
1298
1299 inline void NaiveSwapHasBit(Message* message1, Message* message2,
1300 const FieldDescriptor* field) const;
1301
1302 inline const uint32_t* GetInlinedStringDonatedArray(
1303 const Message& message) const;
1304 inline uint32_t* MutableInlinedStringDonatedArray(Message* message) const;
1305 inline bool IsInlinedStringDonated(const Message& message,
1306 const FieldDescriptor* field) const;
1307 inline void SwapInlinedStringDonated(Message* lhs, Message* rhs,
1308 const FieldDescriptor* field) const;
1309
1310
1311 inline const void* GetSplitField(const Message* message) const;
1312
1313 inline void** MutableSplitField(Message* message) const;
1314
1315
1316 void PrepareSplitMessageForWrite(Message* message) const;
1317
1318
1319
1320 void UnsafeShallowSwapFields(
1321 Message* message1, Message* message2,
1322 const std::vector<const FieldDescriptor*>& fields) const;
1323
1324
1325
1326 void SwapField(Message* message1, Message* message2,
1327 const FieldDescriptor* field) const;
1328
1329
1330 void UnsafeShallowSwapField(Message* message1, Message* message2,
1331 const FieldDescriptor* field) const;
1332
1333 template <bool unsafe_shallow_swap>
1334 void SwapFieldsImpl(Message* message1, Message* message2,
1335 const std::vector<const FieldDescriptor*>& fields) const;
1336
1337 template <bool unsafe_shallow_swap, typename FromType, typename ToType>
1338 void InternalMoveOneofField(const FieldDescriptor* field, FromType* from,
1339 ToType* to) const;
1340
1341 template <bool unsafe_shallow_swap>
1342 void SwapOneofField(Message* lhs, Message* rhs,
1343 const OneofDescriptor* oneof_descriptor) const;
1344
1345 void InternalSwap(Message* lhs, Message* rhs) const;
1346
1347 inline bool HasOneofField(const Message& message,
1348 const FieldDescriptor* field) const;
1349 inline void SetOneofCase(Message* message,
1350 const FieldDescriptor* field) const;
1351 void ClearOneofField(Message* message, const FieldDescriptor* field) const;
1352
1353 template <typename Type>
1354 inline const Type& GetField(const Message& message,
1355 const FieldDescriptor* field) const;
1356 template <typename Type>
1357 inline void SetField(Message* message, const FieldDescriptor* field,
1358 const Type& value) const;
1359 template <typename Type>
1360 inline Type* MutableField(Message* message,
1361 const FieldDescriptor* field) const;
1362 template <typename Type>
1363 inline const Type& GetRepeatedField(const Message& message,
1364 const FieldDescriptor* field,
1365 int index) const;
1366 template <typename Type>
1367 inline const Type& GetRepeatedPtrField(const Message& message,
1368 const FieldDescriptor* field,
1369 int index) const;
1370 template <typename Type>
1371 inline void SetRepeatedField(Message* message, const FieldDescriptor* field,
1372 int index, Type value) const;
1373 template <typename Type>
1374 inline Type* MutableRepeatedField(Message* message,
1375 const FieldDescriptor* field,
1376 int index) const;
1377 template <typename Type>
1378 inline void AddField(Message* message, const FieldDescriptor* field,
1379 const Type& value) const;
1380 template <typename Type>
1381 inline Type* AddField(Message* message, const FieldDescriptor* field) const;
1382
1383 int GetExtensionNumberOrDie(const Descriptor* type) const;
1384
1385
1386
1387 void SetEnumValueInternal(Message* message, const FieldDescriptor* field,
1388 int value) const;
1389 void SetRepeatedEnumValueInternal(Message* message,
1390 const FieldDescriptor* field, int index,
1391 int value) const;
1392 void AddEnumValueInternal(Message* message, const FieldDescriptor* field,
1393 int value) const;
1394
1395 friend inline const char* ParseLenDelim(int field_number,
1396 const FieldDescriptor* field,
1397 Message* msg,
1398 const Reflection* reflection,
1399 const char* ptr,
1400 internal::ParseContext* ctx);
1401 friend inline const char* ParsePackedField(const FieldDescriptor* field,
1402 Message* msg,
1403 const Reflection* reflection,
1404 const char* ptr,
1405 internal::ParseContext* ctx);
1406 };
1407
1408 extern template void Reflection::SwapFieldsImpl<true>(
1409 Message* message1, Message* message2,
1410 const std::vector<const FieldDescriptor*>& fields) const;
1411
1412 extern template void Reflection::SwapFieldsImpl<false>(
1413 Message* message1, Message* message2,
1414 const std::vector<const FieldDescriptor*>& fields) const;
1415
1416
1417
1418
1419
1420 class PROTOBUF_EXPORT MessageFactory {
1421 public:
1422 inline MessageFactory() = default;
1423 MessageFactory(const MessageFactory&) = delete;
1424 MessageFactory& operator=(const MessageFactory&) = delete;
1425 virtual ~MessageFactory();
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445 virtual const Message* GetPrototype(const Descriptor* type) = 0;
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461 static MessageFactory* generated_factory();
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471 static void InternalRegisterGeneratedFile(
1472 const google::protobuf::internal::DescriptorTable* table);
1473
1474
1475
1476
1477 static void InternalRegisterGeneratedMessage(const Descriptor* descriptor,
1478 const Message* prototype);
1479
1480
1481 private:
1482 friend class DynamicMessageFactory;
1483 static const Message* TryGetGeneratedPrototype(const Descriptor* type);
1484 };
1485
1486 #define DECLARE_GET_REPEATED_FIELD(TYPE) \
1487 template <> \
1488 PROTOBUF_EXPORT const RepeatedField<TYPE>& \
1489 Reflection::GetRepeatedFieldInternal<TYPE>( \
1490 const Message& message, const FieldDescriptor* field) const; \
1491 \
1492 template <> \
1493 PROTOBUF_EXPORT RepeatedField<TYPE>* \
1494 Reflection::MutableRepeatedFieldInternal<TYPE>( \
1495 Message * message, const FieldDescriptor* field) const;
1496
1497 DECLARE_GET_REPEATED_FIELD(int32_t)
1498 DECLARE_GET_REPEATED_FIELD(int64_t)
1499 DECLARE_GET_REPEATED_FIELD(uint32_t)
1500 DECLARE_GET_REPEATED_FIELD(uint64_t)
1501 DECLARE_GET_REPEATED_FIELD(float)
1502 DECLARE_GET_REPEATED_FIELD(double)
1503 DECLARE_GET_REPEATED_FIELD(bool)
1504
1505 #undef DECLARE_GET_REPEATED_FIELD
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527 template <typename T>
1528 void LinkMessageReflection() {
1529 internal::StrongReferenceToType<T>();
1530 }
1531
1532
1533
1534 template <>
1535 inline const Message* DynamicCastMessage(const MessageLite* from) {
1536 return from == nullptr || internal::GetClassData(*from)->is_lite
1537 ? nullptr
1538 : static_cast<const Message*>(from);
1539 }
1540 template <>
1541 inline const Message* DownCastMessage(const MessageLite* from) {
1542 ABSL_DCHECK_EQ(DynamicCastMessage<Message>(from), from)
1543 << "Cannot downcast " << from->GetTypeName() << " to Message";
1544 return static_cast<const Message*>(from);
1545 }
1546
1547
1548
1549
1550
1551
1552
1553
1554 template <>
1555 inline const RepeatedPtrField<std::string>&
1556 Reflection::GetRepeatedPtrFieldInternal<std::string>(
1557 const Message& message, const FieldDescriptor* field) const {
1558 return *static_cast<const RepeatedPtrField<std::string>*>(
1559 GetRawRepeatedString(message, field, true));
1560 }
1561
1562 template <>
1563 inline RepeatedPtrField<std::string>*
1564 Reflection::MutableRepeatedPtrFieldInternal<std::string>(
1565 Message* message, const FieldDescriptor* field) const {
1566 return static_cast<RepeatedPtrField<std::string>*>(
1567 MutableRawRepeatedString(message, field, true));
1568 }
1569
1570
1571
1572
1573 template <>
1574 inline const RepeatedPtrField<Message>& Reflection::GetRepeatedPtrFieldInternal(
1575 const Message& message, const FieldDescriptor* field) const {
1576 return *static_cast<const RepeatedPtrField<Message>*>(GetRawRepeatedField(
1577 message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1, nullptr));
1578 }
1579
1580 template <>
1581 inline RepeatedPtrField<Message>* Reflection::MutableRepeatedPtrFieldInternal(
1582 Message* message, const FieldDescriptor* field) const {
1583 return static_cast<RepeatedPtrField<Message>*>(MutableRawRepeatedField(
1584 message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1, nullptr));
1585 }
1586
1587 template <typename PB>
1588 inline const RepeatedPtrField<PB>& Reflection::GetRepeatedPtrFieldInternal(
1589 const Message& message, const FieldDescriptor* field) const {
1590 return *static_cast<const RepeatedPtrField<PB>*>(
1591 GetRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1,
1592 PB::default_instance().GetDescriptor()));
1593 }
1594
1595 template <typename PB>
1596 inline RepeatedPtrField<PB>* Reflection::MutableRepeatedPtrFieldInternal(
1597 Message* message, const FieldDescriptor* field) const {
1598 return static_cast<RepeatedPtrField<PB>*>(
1599 MutableRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE,
1600 -1, PB::default_instance().GetDescriptor()));
1601 }
1602
1603 template <typename Type>
1604 const Type& Reflection::DefaultRaw(const FieldDescriptor* field) const {
1605 return *reinterpret_cast<const Type*>(schema_.GetFieldDefault(field));
1606 }
1607
1608 bool Reflection::HasOneofField(const Message& message,
1609 const FieldDescriptor* field) const {
1610 return (GetOneofCase(message, field->containing_oneof()) ==
1611 static_cast<uint32_t>(field->number()));
1612 }
1613
1614 const void* Reflection::GetSplitField(const Message* message) const {
1615 ABSL_DCHECK(schema_.IsSplit());
1616 return *internal::GetConstPointerAtOffset<void*>(message,
1617 schema_.SplitOffset());
1618 }
1619
1620 void** Reflection::MutableSplitField(Message* message) const {
1621 ABSL_DCHECK(schema_.IsSplit());
1622 return internal::GetPointerAtOffset<void*>(message, schema_.SplitOffset());
1623 }
1624
1625 namespace internal {
1626
1627
1628
1629
1630
1631 template <typename T>
1632 bool SplitFieldHasExtraIndirectionStatic(const FieldDescriptor* field) {
1633 if (std::is_base_of<RepeatedFieldBase, T>() ||
1634 std::is_base_of<RepeatedPtrFieldBase, T>()) {
1635 ABSL_DCHECK(SplitFieldHasExtraIndirection(field));
1636 return true;
1637 } else if (std::is_base_of<MessageLite, T>()) {
1638 ABSL_DCHECK(!SplitFieldHasExtraIndirection(field));
1639 return false;
1640 }
1641 return SplitFieldHasExtraIndirection(field);
1642 }
1643
1644 inline void MaybePoisonAfterClear(Message* root) {
1645 if (root == nullptr) return;
1646 if constexpr (HasMemoryPoisoning()) {
1647 const Reflection* reflection = root->GetReflection();
1648 reflection->MaybePoisonAfterClear(*root);
1649 } else {
1650 root->Clear();
1651 }
1652 }
1653
1654 template <typename T>
1655 inline constexpr std::false_type IsRepeatedT{};
1656 template <typename T>
1657 inline constexpr std::true_type IsRepeatedT<RepeatedField<T>>{};
1658 template <typename T>
1659 inline constexpr std::true_type IsRepeatedT<RepeatedPtrField<T>>{};
1660 template <>
1661 inline constexpr std::true_type IsRepeatedT<internal::RepeatedPtrFieldBase>{};
1662 template <>
1663 inline constexpr std::true_type IsRepeatedT<internal::MapFieldBase>{};
1664
1665 template <typename T>
1666 constexpr FieldDescriptor::CppType GetCppType() {
1667 if constexpr (IsRepeatedT<T>) {
1668 return GetCppType<typename T::value_type>();
1669 } else {
1670 if (std::is_same_v<T, int32_t>) return FieldDescriptor::CPPTYPE_INT32;
1671 if (std::is_same_v<T, int64_t>) return FieldDescriptor::CPPTYPE_INT64;
1672 if (std::is_same_v<T, uint32_t>) return FieldDescriptor::CPPTYPE_UINT32;
1673 if (std::is_same_v<T, uint64_t>) return FieldDescriptor::CPPTYPE_UINT64;
1674 if (std::is_same_v<T, float>) return FieldDescriptor::CPPTYPE_FLOAT;
1675 if (std::is_same_v<T, double>) return FieldDescriptor::CPPTYPE_DOUBLE;
1676 if (std::is_same_v<T, bool>) return FieldDescriptor::CPPTYPE_BOOL;
1677
1678 using PCV = std::remove_cv_t<std::remove_pointer_t<T>>;
1679
1680
1681 if (std::is_same_v<PCV, internal::ArenaStringPtr> ||
1682 std::is_same_v<PCV, std::string> ||
1683 std::is_same_v<PCV, internal::MicroString> ||
1684 std::is_same_v<PCV, absl::Cord>) {
1685 return FieldDescriptor::CPPTYPE_STRING;
1686 }
1687
1688
1689 if (std::is_same_v<PCV, Message> ||
1690 std::is_same_v<PCV, MessageLite> ||
1691 std::is_same_v<PCV, internal::LazyField>) {
1692 return FieldDescriptor::CPPTYPE_MESSAGE;
1693 }
1694 }
1695
1696
1697
1698 return FieldDescriptor::CppType{};
1699 }
1700
1701 }
1702
1703 template <typename T>
1704 void Reflection::VerifyFieldType(const FieldDescriptor* field) const {
1705 if constexpr (!internal::PerformDebugChecks()) {
1706 return;
1707 }
1708 if constexpr (std::is_const_v<T>) {
1709 return VerifyFieldType<std::remove_const_t<T>>(field);
1710 }
1711
1712
1713 if constexpr (std::is_same_v<T, char> || std::is_same_v<T, void>) {
1714 return;
1715 }
1716
1717 const auto error = [&] {
1718 return absl::StrFormat("Invalid cast of %s to type %s.", field->full_name(),
1719 internal::RttiTypeName<T>().value_or("unknown"));
1720 };
1721
1722 ABSL_DCHECK_EQ(field->is_repeated(), internal::IsRepeatedT<T>) << error();
1723 if constexpr (std::is_same_v<T, internal::MapFieldBase>) {
1724 ABSL_DCHECK(field->is_map()) << error();
1725 } else if constexpr (std::is_same_v<T, internal::RepeatedPtrFieldBase>) {
1726
1727 ABSL_DCHECK(field->cpp_type() == field->CPPTYPE_STRING ||
1728 field->cpp_type() == field->CPPTYPE_MESSAGE)
1729 << error();
1730 } else {
1731 auto cpp_type = field->cpp_type();
1732
1733 if (cpp_type == field->CPPTYPE_ENUM) cpp_type = field->CPPTYPE_INT32;
1734 ABSL_DCHECK_EQ(+cpp_type, +internal::GetCppType<T>()) << error();
1735
1736
1737 if constexpr (internal::GetCppType<T>() ==
1738 FieldDescriptor::CPPTYPE_MESSAGE) {
1739
1740 if (!field->is_repeated() &&
1741 (!IsLazyField(field) || field->real_containing_oneof() != nullptr)) {
1742 ABSL_DCHECK(std::is_pointer_v<T>) << error();
1743 }
1744 }
1745
1746
1747 if constexpr (internal::GetCppType<T>() ==
1748 FieldDescriptor::CPPTYPE_STRING) {
1749 switch (field->cpp_string_type()) {
1750 case FieldDescriptor::CppStringType::kView:
1751 case FieldDescriptor::CppStringType::kString:
1752 if (IsMicroString(field)) {
1753 ABSL_DCHECK((std::is_same_v<T, internal::MicroString>)) << error();
1754 } else {
1755 ABSL_DCHECK((std::is_same_v<T, internal::ArenaStringPtr> ||
1756 std::is_same_v<T, RepeatedPtrField<std::string>>))
1757 << error();
1758 }
1759 break;
1760 case FieldDescriptor::CppStringType::kCord:
1761 if (field->real_containing_oneof() != nullptr) {
1762 ABSL_DCHECK((std::is_same_v<T, absl::Cord*>)) << error();
1763 } else {
1764 ABSL_DCHECK((std::is_same_v<T, absl::Cord> ||
1765 std::is_same_v<T, RepeatedField<absl::Cord>>))
1766 << error();
1767 }
1768 break;
1769 }
1770 }
1771 }
1772 }
1773
1774 template <typename Type>
1775 const Type& Reflection::GetRaw(const Message& message,
1776 const FieldDescriptor* field) const {
1777 VerifyFieldType<Type>(field);
1778
1779 const uint32_t field_offset = schema_.GetFieldOffset<Type>(field);
1780
1781 if (ABSL_PREDICT_FALSE(schema_.IsSplit(field))) {
1782 ABSL_DCHECK(!schema_.InRealOneof(field))
1783 << "Field = " << field->full_name();
1784
1785 const void* split = GetSplitField(&message);
1786 if (internal::SplitFieldHasExtraIndirectionStatic<Type>(field)) {
1787 return **internal::GetConstPointerAtOffset<Type*>(split, field_offset);
1788 }
1789 return *internal::GetConstPointerAtOffset<Type>(split, field_offset);
1790 }
1791 return internal::GetConstRefAtOffset<Type>(message, field_offset);
1792 }
1793
1794 template <typename T>
1795 RepeatedFieldRef<T> Reflection::GetRepeatedFieldRef(
1796 const Message& message, const FieldDescriptor* field) const {
1797 ABSL_DCHECK_EQ(message.GetReflection(), this);
1798 return RepeatedFieldRef<T>(message, field);
1799 }
1800
1801 template <typename T>
1802 MutableRepeatedFieldRef<T> Reflection::GetMutableRepeatedFieldRef(
1803 Message* message, const FieldDescriptor* field) const {
1804 ABSL_DCHECK_EQ(message->GetReflection(), this);
1805 return MutableRepeatedFieldRef<T>(message, field);
1806 }
1807
1808 template <typename Type>
1809 Type* Reflection::MutableRaw(Message* message,
1810 const FieldDescriptor* field) const {
1811 VerifyFieldType<Type>(field);
1812
1813 if (ABSL_PREDICT_FALSE(schema_.IsSplit(field))) {
1814 return reinterpret_cast<Type*>(MutableRawSplitImpl(message, field));
1815 }
1816
1817 const uint32_t field_offset = schema_.GetFieldOffset<Type>(field);
1818 return internal::GetPointerAtOffset<Type>(message, field_offset);
1819 }
1820
1821
1822 }
1823 }
1824
1825 #include "google/protobuf/port_undef.inc"
1826
1827 #endif