File indexing completed on 2025-01-31 10:12:23
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef GOOGLE_PROTOBUF_MESSAGE_LITE_H__
0017 #define GOOGLE_PROTOBUF_MESSAGE_LITE_H__
0018
0019 #include <climits>
0020 #include <cstddef>
0021 #include <cstdint>
0022 #include <iosfwd>
0023 #include <string>
0024 #include <type_traits>
0025
0026 #include "absl/base/attributes.h"
0027 #include "absl/log/absl_check.h"
0028 #include "absl/strings/cord.h"
0029 #include "absl/strings/string_view.h"
0030 #include "google/protobuf/arena.h"
0031 #include "google/protobuf/explicitly_constructed.h"
0032 #include "google/protobuf/internal_visibility.h"
0033 #include "google/protobuf/io/coded_stream.h"
0034 #include "google/protobuf/metadata_lite.h"
0035 #include "google/protobuf/port.h"
0036
0037
0038
0039 #include "google/protobuf/port_def.inc"
0040
0041
0042 #ifdef SWIG
0043 #error "You cannot SWIG proto headers"
0044 #endif
0045
0046 namespace google {
0047 namespace protobuf {
0048
0049 template <typename T>
0050 class RepeatedPtrField;
0051
0052 class FastReflectionMessageMutator;
0053 class FastReflectionStringSetter;
0054 class Reflection;
0055 class Descriptor;
0056 class AssignDescriptorsHelper;
0057 class MessageLite;
0058
0059 namespace io {
0060
0061 class CodedInputStream;
0062 class CodedOutputStream;
0063 class ZeroCopyInputStream;
0064 class ZeroCopyOutputStream;
0065
0066 }
0067 namespace internal {
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083 class PROTOBUF_EXPORT CachedSize {
0084 private:
0085 using Scalar = int;
0086
0087 public:
0088 constexpr CachedSize() noexcept : atom_(Scalar{}) {}
0089
0090 constexpr CachedSize(Scalar desired) noexcept : atom_(desired) {}
0091 #if PROTOBUF_BUILTIN_ATOMIC
0092 constexpr CachedSize(const CachedSize& other) = default;
0093
0094 Scalar Get() const noexcept {
0095 return __atomic_load_n(&atom_, __ATOMIC_RELAXED);
0096 }
0097
0098 void Set(Scalar desired) noexcept {
0099 __atomic_store_n(&atom_, desired, __ATOMIC_RELAXED);
0100 }
0101 #else
0102 CachedSize(const CachedSize& other) noexcept : atom_(other.Get()) {}
0103 CachedSize& operator=(const CachedSize& other) noexcept {
0104 Set(other.Get());
0105 return *this;
0106 }
0107
0108 Scalar Get() const noexcept {
0109 return atom_.load(std::memory_order_relaxed);
0110 }
0111
0112 void Set(Scalar desired) noexcept {
0113 atom_.store(desired, std::memory_order_relaxed);
0114 }
0115 #endif
0116
0117 private:
0118 #if PROTOBUF_BUILTIN_ATOMIC
0119 Scalar atom_;
0120 #else
0121 std::atomic<Scalar> atom_;
0122 #endif
0123 };
0124
0125
0126 auto GetClassData(const MessageLite& msg);
0127
0128 class SwapFieldHelper;
0129
0130
0131 class ParseContext;
0132
0133 struct DescriptorTable;
0134 class DescriptorPoolExtensionFinder;
0135 class ExtensionSet;
0136 class LazyField;
0137 class RepeatedPtrFieldBase;
0138 class TcParser;
0139 struct TcParseTableBase;
0140 class WireFormatLite;
0141 class WeakFieldMap;
0142
0143 template <typename Type>
0144 class GenericTypeHandler;
0145
0146
0147
0148
0149
0150
0151
0152
0153 inline int ToCachedSize(size_t size) { return static_cast<int>(size); }
0154
0155
0156
0157
0158
0159
0160 inline size_t FromIntSize(int size) {
0161
0162 return static_cast<unsigned int>(size);
0163 }
0164
0165
0166
0167
0168 inline int ToIntSize(size_t size) {
0169 ABSL_DCHECK_LE(size, static_cast<size_t>(INT_MAX));
0170 return static_cast<int>(size);
0171 }
0172
0173
0174
0175
0176 PROTOBUF_EXPORT extern ExplicitlyConstructedArenaString
0177 fixed_address_empty_string;
0178
0179
0180 PROTOBUF_EXPORT constexpr const std::string& GetEmptyStringAlreadyInited() {
0181 return fixed_address_empty_string.get();
0182 }
0183
0184 PROTOBUF_EXPORT size_t StringSpaceUsedExcludingSelfLong(const std::string& str);
0185
0186 }
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214 class PROTOBUF_EXPORT MessageLite {
0215 public:
0216 MessageLite(const MessageLite&) = delete;
0217 MessageLite& operator=(const MessageLite&) = delete;
0218 PROTOBUF_VIRTUAL ~MessageLite() = default;
0219
0220
0221
0222
0223 std::string GetTypeName() const;
0224
0225
0226
0227 MessageLite* New() const { return New(nullptr); }
0228
0229
0230
0231 #if defined(PROTOBUF_CUSTOM_VTABLE)
0232 MessageLite* New(Arena* arena) const;
0233 #else
0234 virtual MessageLite* New(Arena* arena) const = 0;
0235 #endif
0236
0237
0238
0239
0240
0241
0242 Arena* GetArena() const { return _internal_metadata_.arena(); }
0243
0244
0245
0246
0247
0248 #if defined(PROTOBUF_CUSTOM_VTABLE)
0249 void Clear();
0250 #else
0251 virtual void Clear() = 0;
0252 #endif
0253
0254
0255 bool IsInitialized() const;
0256
0257
0258
0259
0260 std::string InitializationErrorString() const;
0261
0262
0263
0264 void CheckTypeAndMergeFrom(const MessageLite& other);
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275 std::string DebugString() const;
0276 std::string ShortDebugString() const { return DebugString(); }
0277
0278
0279 std::string Utf8DebugString() const { return DebugString(); }
0280
0281
0282
0283 template <typename Sink>
0284 friend void AbslStringify(Sink& sink, const google::protobuf::MessageLite& msg) {
0285 sink.Append(msg.DebugString());
0286 }
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298 ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromCodedStream(
0299 io::CodedInputStream* input);
0300
0301
0302 ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromCodedStream(
0303 io::CodedInputStream* input);
0304
0305
0306 ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromZeroCopyStream(
0307 io::ZeroCopyInputStream* input);
0308
0309
0310 ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromZeroCopyStream(
0311 io::ZeroCopyInputStream* input);
0312
0313
0314 ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromFileDescriptor(
0315 int file_descriptor);
0316
0317
0318 ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromFileDescriptor(
0319 int file_descriptor);
0320
0321
0322 ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromIstream(std::istream* input);
0323
0324
0325 ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromIstream(
0326 std::istream* input);
0327
0328
0329
0330 bool MergePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input,
0331 int size);
0332
0333
0334 bool MergeFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, int size);
0335 ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromBoundedZeroCopyStream(
0336 io::ZeroCopyInputStream* input, int size);
0337
0338
0339 ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromBoundedZeroCopyStream(
0340 io::ZeroCopyInputStream* input, int size);
0341
0342
0343
0344
0345
0346 ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromString(absl::string_view data);
0347
0348
0349 ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromString(
0350 absl::string_view data);
0351
0352 ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromArray(const void* data, int size);
0353
0354
0355 ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromArray(const void* data,
0356 int size);
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370 bool MergeFromCodedStream(io::CodedInputStream* input);
0371
0372
0373
0374
0375
0376
0377 bool MergePartialFromCodedStream(io::CodedInputStream* input);
0378
0379
0380 bool MergeFromString(absl::string_view data);
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390 bool SerializeToCodedStream(io::CodedOutputStream* output) const;
0391
0392 bool SerializePartialToCodedStream(io::CodedOutputStream* output) const;
0393
0394
0395 bool SerializeToZeroCopyStream(io::ZeroCopyOutputStream* output) const;
0396
0397 bool SerializePartialToZeroCopyStream(io::ZeroCopyOutputStream* output) const;
0398
0399
0400 bool SerializeToString(std::string* output) const;
0401
0402 bool SerializePartialToString(std::string* output) const;
0403
0404
0405 bool SerializeToArray(void* data, int size) const;
0406
0407 bool SerializePartialToArray(void* data, int size) const;
0408
0409
0410
0411
0412
0413
0414
0415 std::string SerializeAsString() const;
0416
0417 std::string SerializePartialAsString() const;
0418
0419
0420
0421 bool SerializeToFileDescriptor(int file_descriptor) const;
0422
0423 bool SerializePartialToFileDescriptor(int file_descriptor) const;
0424
0425
0426 bool SerializeToOstream(std::ostream* output) const;
0427
0428 bool SerializePartialToOstream(std::ostream* output) const;
0429
0430
0431
0432 bool AppendToString(std::string* output) const;
0433
0434 bool AppendPartialToString(std::string* output) const;
0435
0436
0437 bool MergeFromCord(const absl::Cord& cord);
0438
0439
0440 bool MergePartialFromCord(const absl::Cord& cord);
0441
0442 ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromCord(const absl::Cord& cord);
0443
0444
0445 ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromCord(
0446 const absl::Cord& cord);
0447
0448
0449
0450 bool SerializeToCord(absl::Cord* output) const;
0451
0452 bool SerializePartialToCord(absl::Cord* output) const;
0453
0454
0455
0456
0457 absl::Cord SerializeAsCord() const;
0458
0459 absl::Cord SerializePartialAsCord() const;
0460
0461
0462
0463 bool AppendToCord(absl::Cord* output) const;
0464
0465 bool AppendPartialToCord(absl::Cord* output) const;
0466
0467
0468
0469
0470
0471
0472 #if defined(PROTOBUF_CUSTOM_VTABLE)
0473 size_t ByteSizeLong() const;
0474 #else
0475 virtual size_t ByteSizeLong() const = 0;
0476 #endif
0477
0478
0479 [[deprecated("Please use ByteSizeLong() instead")]] int ByteSize() const {
0480 return internal::ToIntSize(ByteSizeLong());
0481 }
0482
0483
0484
0485
0486 void SerializeWithCachedSizes(io::CodedOutputStream* output) const {
0487 output->SetCur(_InternalSerialize(output->Cur(), output->EpsCopy()));
0488 }
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500 uint8_t* SerializeWithCachedSizesToArray(uint8_t* target) const;
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515 int GetCachedSize() const;
0516
0517 const char* _InternalParse(const char* ptr, internal::ParseContext* ctx);
0518
0519 void OnDemandRegisterArenaDtor(Arena* arena);
0520
0521 protected:
0522
0523 static constexpr internal::InternalVisibility internal_visibility() {
0524 return internal::InternalVisibility{};
0525 }
0526
0527 template <typename T>
0528 PROTOBUF_ALWAYS_INLINE static T* DefaultConstruct(Arena* arena) {
0529 return static_cast<T*>(Arena::DefaultConstruct<T>(arena));
0530 }
0531
0532 #if defined(PROTOBUF_CUSTOM_VTABLE)
0533 template <typename T>
0534 static void* NewImpl(const void* prototype, Arena* arena) {
0535 return static_cast<const T*>(prototype)->New(arena);
0536 }
0537 template <typename T>
0538 static constexpr auto GetNewImpl() {
0539 return NewImpl<T>;
0540 }
0541
0542 template <typename T>
0543 static void DeleteImpl(void* msg, bool free_memory) {
0544 static_cast<T*>(msg)->~T();
0545 if (free_memory) internal::SizedDelete(msg, sizeof(T));
0546 }
0547 template <typename T>
0548 static constexpr auto GetDeleteImpl() {
0549 return DeleteImpl<T>;
0550 }
0551
0552 template <typename T>
0553 static void ClearImpl(MessageLite& msg) {
0554 return static_cast<T&>(msg).Clear();
0555 }
0556 template <typename T>
0557 static constexpr auto GetClearImpl() {
0558 return ClearImpl<T>;
0559 }
0560 #else
0561
0562
0563 template <typename T>
0564 using GetNewImpl = std::nullptr_t;
0565 template <typename T>
0566 using GetDeleteImpl = std::nullptr_t;
0567 template <typename T>
0568 using GetClearImpl = std::nullptr_t;
0569 #endif
0570
0571 template <typename T>
0572 PROTOBUF_ALWAYS_INLINE static T* CopyConstruct(Arena* arena, const T& from) {
0573 return static_cast<T*>(Arena::CopyConstruct<T>(arena, &from));
0574 }
0575
0576 const internal::TcParseTableBase* GetTcParseTable() const {
0577 auto* data = GetClassData();
0578 ABSL_DCHECK(data != nullptr);
0579
0580 auto* tc_table = data->tc_table;
0581 if (ABSL_PREDICT_FALSE(tc_table == nullptr)) {
0582 ABSL_DCHECK(!data->is_lite);
0583 return data->full().descriptor_methods->get_tc_table(*this);
0584 }
0585 return tc_table;
0586 }
0587
0588
0589
0590
0591 struct ClassData;
0592 struct ClassDataFull;
0593 struct DescriptorMethods {
0594 absl::string_view (*get_type_name)(const ClassData* data);
0595 std::string (*initialization_error_string)(const MessageLite&);
0596 const internal::TcParseTableBase* (*get_tc_table)(const MessageLite&);
0597 size_t (*space_used_long)(const MessageLite&);
0598 std::string (*debug_string)(const MessageLite&);
0599 };
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609 using NewMessageF = void* (*)(const void* prototype, Arena* arena);
0610 using DeleteMessageF = void (*)(void* msg, bool free_memory);
0611 struct ClassData {
0612 const MessageLite* prototype;
0613 const internal::TcParseTableBase* tc_table;
0614 void (*on_demand_register_arena_dtor)(MessageLite& msg, Arena& arena);
0615 bool (*is_initialized)(const MessageLite&);
0616 void (*merge_to_from)(MessageLite& to, const MessageLite& from_msg);
0617 #if defined(PROTOBUF_CUSTOM_VTABLE)
0618 DeleteMessageF delete_message;
0619 NewMessageF new_message;
0620 void (*clear)(MessageLite&);
0621 size_t (*byte_size_long)(const MessageLite&);
0622 uint8_t* (*serialize)(const MessageLite& msg, uint8_t* ptr,
0623 io::EpsCopyOutputStream* stream);
0624 #endif
0625
0626
0627 uint32_t cached_size_offset;
0628
0629
0630 bool is_lite;
0631 bool is_dynamic = false;
0632
0633
0634
0635 #if !defined(PROTOBUF_CUSTOM_VTABLE)
0636 constexpr ClassData(const MessageLite* prototype,
0637 const internal::TcParseTableBase* tc_table,
0638 void (*on_demand_register_arena_dtor)(MessageLite&,
0639 Arena&),
0640 bool (*is_initialized)(const MessageLite&),
0641 void (*merge_to_from)(MessageLite& to,
0642 const MessageLite& from_msg),
0643 uint32_t cached_size_offset, bool is_lite)
0644 : prototype(prototype),
0645 tc_table(tc_table),
0646 on_demand_register_arena_dtor(on_demand_register_arena_dtor),
0647 is_initialized(is_initialized),
0648 merge_to_from(merge_to_from),
0649 cached_size_offset(cached_size_offset),
0650 is_lite(is_lite) {}
0651 #endif
0652
0653
0654
0655 constexpr ClassData(
0656 const MessageLite* prototype,
0657 const internal::TcParseTableBase* tc_table,
0658 void (*on_demand_register_arena_dtor)(MessageLite&, Arena&),
0659 bool (*is_initialized)(const MessageLite&),
0660 void (*merge_to_from)(MessageLite& to, const MessageLite& from_msg),
0661 DeleteMessageF delete_message,
0662 NewMessageF new_message,
0663 void (*clear)(MessageLite&),
0664 size_t (*byte_size_long)(const MessageLite&),
0665 uint8_t* (*serialize)(const MessageLite& msg, uint8_t* ptr,
0666 io::EpsCopyOutputStream* stream),
0667 uint32_t cached_size_offset, bool is_lite)
0668 : prototype(prototype),
0669 tc_table(tc_table),
0670 on_demand_register_arena_dtor(on_demand_register_arena_dtor),
0671 is_initialized(is_initialized),
0672 merge_to_from(merge_to_from),
0673 #if defined(PROTOBUF_CUSTOM_VTABLE)
0674 delete_message(delete_message),
0675 new_message(new_message),
0676 clear(clear),
0677 byte_size_long(byte_size_long),
0678 serialize(serialize),
0679 #endif
0680 cached_size_offset(cached_size_offset),
0681 is_lite(is_lite) {
0682 }
0683
0684 const ClassDataFull& full() const {
0685 ABSL_DCHECK(!is_lite);
0686 return *static_cast<const ClassDataFull*>(this);
0687 }
0688 };
0689 template <size_t N>
0690 struct ClassDataLite {
0691 ClassData header;
0692 const char type_name[N];
0693
0694 constexpr const ClassData* base() const { return &header; }
0695 };
0696 struct ClassDataFull : ClassData {
0697 constexpr ClassDataFull(ClassData base,
0698 const DescriptorMethods* descriptor_methods,
0699 const internal::DescriptorTable* descriptor_table,
0700 void (*get_metadata_tracker)())
0701 : ClassData(base),
0702 descriptor_methods(descriptor_methods),
0703 descriptor_table(descriptor_table),
0704 reflection(),
0705 descriptor(),
0706 get_metadata_tracker(get_metadata_tracker) {}
0707
0708 constexpr const ClassData* base() const { return this; }
0709
0710 const DescriptorMethods* descriptor_methods;
0711
0712
0713
0714
0715
0716 const internal::DescriptorTable* descriptor_table;
0717
0718
0719
0720 mutable const Reflection* reflection;
0721 mutable const Descriptor* descriptor;
0722
0723
0724
0725 void (*get_metadata_tracker)();
0726 };
0727
0728 #if defined(PROTOBUF_CUSTOM_VTABLE)
0729 explicit constexpr MessageLite(const ClassData* data) : _class_data_(data) {}
0730 explicit MessageLite(Arena* arena, const ClassData* data)
0731 : _internal_metadata_(arena), _class_data_(data) {}
0732 #else
0733 constexpr MessageLite() {}
0734 explicit MessageLite(Arena* arena) : _internal_metadata_(arena) {}
0735 explicit constexpr MessageLite(const ClassData*) {}
0736 explicit MessageLite(Arena* arena, const ClassData*)
0737 : _internal_metadata_(arena) {}
0738 #endif
0739
0740
0741
0742
0743
0744
0745
0746
0747 #if defined(PROTOBUF_CUSTOM_VTABLE)
0748 const ClassData* GetClassData() const {
0749 ::absl::PrefetchToLocalCache(_class_data_);
0750 return _class_data_;
0751 }
0752 #else
0753 virtual const ClassData* GetClassData() const = 0;
0754 #endif
0755
0756 template <typename T>
0757 static auto GetClassDataGenerated() {
0758 static_assert(std::is_base_of<MessageLite, T>::value, "");
0759
0760
0761 static_assert(
0762 std::is_same<const T&, decltype(T::default_instance())>::value, "");
0763 return T::default_instance().T::GetClassData();
0764 }
0765
0766 internal::InternalMetadata _internal_metadata_;
0767 #if defined(PROTOBUF_CUSTOM_VTABLE)
0768 const ClassData* _class_data_;
0769 #endif
0770
0771
0772
0773 internal::CachedSize& AccessCachedSize() const;
0774
0775 public:
0776 enum ParseFlags {
0777 kMerge = 0,
0778 kParse = 1,
0779 kMergePartial = 2,
0780 kParsePartial = 3,
0781 kMergeWithAliasing = 4,
0782 kParseWithAliasing = 5,
0783 kMergePartialWithAliasing = 6,
0784 kParsePartialWithAliasing = 7
0785 };
0786
0787 template <ParseFlags flags, typename T>
0788 bool ParseFrom(const T& input);
0789
0790
0791
0792 #if defined(PROTOBUF_CUSTOM_VTABLE)
0793 uint8_t* _InternalSerialize(uint8_t* ptr,
0794 io::EpsCopyOutputStream* stream) const;
0795 #else
0796 virtual uint8_t* _InternalSerialize(
0797 uint8_t* ptr, io::EpsCopyOutputStream* stream) const = 0;
0798 #endif
0799
0800
0801 bool IsInitializedWithErrors() const {
0802 if (IsInitialized()) return true;
0803 LogInitializationErrorMessage();
0804 return false;
0805 }
0806
0807 #if defined(PROTOBUF_CUSTOM_VTABLE)
0808 void operator delete(MessageLite* msg, std::destroying_delete_t) {
0809 msg->DestroyInstance(true);
0810 }
0811 #endif
0812
0813 private:
0814 friend class FastReflectionMessageMutator;
0815 friend class AssignDescriptorsHelper;
0816 friend class FastReflectionStringSetter;
0817 friend class Message;
0818 friend class Reflection;
0819 friend class TypeId;
0820 friend class internal::DescriptorPoolExtensionFinder;
0821 friend class internal::ExtensionSet;
0822 friend class internal::LazyField;
0823 friend class internal::SwapFieldHelper;
0824 friend class internal::TcParser;
0825 friend struct internal::TcParseTableBase;
0826 friend class internal::UntypedMapBase;
0827 friend class internal::WeakFieldMap;
0828 friend class internal::WireFormatLite;
0829
0830 template <typename Type>
0831 friend class Arena::InternalHelper;
0832 template <typename Type>
0833 friend class internal::GenericTypeHandler;
0834
0835 friend auto internal::GetClassData(const MessageLite& msg);
0836
0837 void LogInitializationErrorMessage() const;
0838
0839 bool MergeFromImpl(io::CodedInputStream* input, ParseFlags parse_flags);
0840
0841
0842
0843 void DestroyInstance(bool free_memory);
0844
0845 template <typename T, const void* ptr = T::_raw_default_instance_>
0846 static constexpr auto GetStrongPointerForTypeImpl(int) {
0847 return ptr;
0848 }
0849 template <typename T>
0850 static constexpr auto GetStrongPointerForTypeImpl(char) {
0851 return &T::default_instance;
0852 }
0853
0854
0855
0856
0857
0858 template <typename T>
0859 static constexpr auto GetStrongPointerForType() {
0860 return GetStrongPointerForTypeImpl<T>(0);
0861 }
0862 template <typename T>
0863 friend void internal::StrongReferenceToType();
0864 };
0865
0866
0867
0868
0869
0870
0871
0872
0873
0874
0875
0876
0877
0878 class TypeId {
0879 public:
0880 static TypeId Get(const MessageLite& msg) {
0881 return TypeId(msg.GetClassData());
0882 }
0883
0884 template <typename T>
0885 static TypeId Get() {
0886 return TypeId(MessageLite::GetClassDataGenerated<T>());
0887 }
0888
0889
0890
0891 absl::string_view name() const;
0892
0893 friend constexpr bool operator==(TypeId a, TypeId b) {
0894 return a.data_ == b.data_;
0895 }
0896 friend constexpr bool operator!=(TypeId a, TypeId b) { return !(a == b); }
0897 friend constexpr bool operator<(TypeId a, TypeId b) {
0898 return a.data_ < b.data_;
0899 }
0900 friend constexpr bool operator>(TypeId a, TypeId b) {
0901 return a.data_ > b.data_;
0902 }
0903 friend constexpr bool operator<=(TypeId a, TypeId b) {
0904 return a.data_ <= b.data_;
0905 }
0906 friend constexpr bool operator>=(TypeId a, TypeId b) {
0907 return a.data_ >= b.data_;
0908 }
0909
0910 #if defined(__cpp_impl_three_way_comparison) && \
0911 __cpp_impl_three_way_comparison >= 201907L
0912 friend constexpr auto operator<=>(TypeId a, TypeId b) {
0913 return a.data_ <=> b.data_;
0914 }
0915 #endif
0916
0917 template <typename H>
0918 friend H AbslHashValue(H state, TypeId id) {
0919 return H::combine(std::move(state), id.data_);
0920 }
0921
0922 private:
0923 constexpr explicit TypeId(const MessageLite::ClassData* data) : data_(data) {}
0924
0925 const MessageLite::ClassData* data_;
0926 };
0927
0928 namespace internal {
0929
0930 inline auto GetClassData(const MessageLite& msg) { return msg.GetClassData(); }
0931
0932 template <bool alias>
0933 bool MergeFromImpl(absl::string_view input, MessageLite* msg,
0934 const internal::TcParseTableBase* tc_table,
0935 MessageLite::ParseFlags parse_flags);
0936 extern template PROTOBUF_EXPORT_TEMPLATE_DECLARE bool MergeFromImpl<false>(
0937 absl::string_view input, MessageLite* msg,
0938 const internal::TcParseTableBase* tc_table,
0939 MessageLite::ParseFlags parse_flags);
0940 extern template PROTOBUF_EXPORT_TEMPLATE_DECLARE bool MergeFromImpl<true>(
0941 absl::string_view input, MessageLite* msg,
0942 const internal::TcParseTableBase* tc_table,
0943 MessageLite::ParseFlags parse_flags);
0944
0945 template <bool alias>
0946 bool MergeFromImpl(io::ZeroCopyInputStream* input, MessageLite* msg,
0947 const internal::TcParseTableBase* tc_table,
0948 MessageLite::ParseFlags parse_flags);
0949 extern template PROTOBUF_EXPORT_TEMPLATE_DECLARE bool MergeFromImpl<false>(
0950 io::ZeroCopyInputStream* input, MessageLite* msg,
0951 const internal::TcParseTableBase* tc_table,
0952 MessageLite::ParseFlags parse_flags);
0953 extern template PROTOBUF_EXPORT_TEMPLATE_DECLARE bool MergeFromImpl<true>(
0954 io::ZeroCopyInputStream* input, MessageLite* msg,
0955 const internal::TcParseTableBase* tc_table,
0956 MessageLite::ParseFlags parse_flags);
0957
0958 struct BoundedZCIS {
0959 io::ZeroCopyInputStream* zcis;
0960 int limit;
0961 };
0962
0963 template <bool alias>
0964 bool MergeFromImpl(BoundedZCIS input, MessageLite* msg,
0965 const internal::TcParseTableBase* tc_table,
0966 MessageLite::ParseFlags parse_flags);
0967 extern template PROTOBUF_EXPORT_TEMPLATE_DECLARE bool MergeFromImpl<false>(
0968 BoundedZCIS input, MessageLite* msg,
0969 const internal::TcParseTableBase* tc_table,
0970 MessageLite::ParseFlags parse_flags);
0971 extern template PROTOBUF_EXPORT_TEMPLATE_DECLARE bool MergeFromImpl<true>(
0972 BoundedZCIS input, MessageLite* msg,
0973 const internal::TcParseTableBase* tc_table,
0974 MessageLite::ParseFlags parse_flags);
0975
0976 template <typename T>
0977 struct SourceWrapper;
0978
0979 template <bool alias, typename T>
0980 bool MergeFromImpl(const SourceWrapper<T>& input, MessageLite* msg,
0981 const internal::TcParseTableBase* tc_table,
0982 MessageLite::ParseFlags parse_flags) {
0983 return input.template MergeInto<alias>(msg, tc_table, parse_flags);
0984 }
0985
0986 }
0987
0988 template <MessageLite::ParseFlags flags, typename T>
0989 bool MessageLite::ParseFrom(const T& input) {
0990 if (flags & kParse) Clear();
0991 constexpr bool alias = (flags & kMergeWithAliasing) != 0;
0992 const internal::TcParseTableBase* tc_table;
0993 PROTOBUF_ALWAYS_INLINE_CALL tc_table = GetTcParseTable();
0994 return internal::MergeFromImpl<alias>(input, this, tc_table, flags);
0995 }
0996
0997
0998
0999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015 PROTOBUF_EXPORT void ShutdownProtobufLibrary();
1016
1017 namespace internal {
1018
1019
1020 PROTOBUF_EXPORT void OnShutdown(void (*func)());
1021
1022 PROTOBUF_EXPORT void OnShutdownRun(void (*f)(const void*), const void* arg);
1023
1024 template <typename T>
1025 T* OnShutdownDelete(T* p) {
1026 OnShutdownRun([](const void* pp) { delete static_cast<const T*>(pp); }, p);
1027 return p;
1028 }
1029
1030 inline void AssertDownCast(const MessageLite& from, const MessageLite& to) {
1031 ABSL_DCHECK(TypeId::Get(from) == TypeId::Get(to))
1032 << "Cannot downcast " << from.GetTypeName() << " to " << to.GetTypeName();
1033 }
1034
1035 }
1036
1037 std::string ShortFormat(const MessageLite& message_lite);
1038 std::string Utf8Format(const MessageLite& message_lite);
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056 template <typename T>
1057 const T* DynamicCastMessage(const MessageLite* from) {
1058 static_assert(std::is_base_of<MessageLite, T>::value, "");
1059
1060
1061
1062 if (from == nullptr || TypeId::Get<T>() != TypeId::Get(*from)) {
1063 return nullptr;
1064 }
1065
1066 return static_cast<const T*>(from);
1067 }
1068
1069 template <typename T>
1070 T* DynamicCastMessage(MessageLite* from) {
1071 return const_cast<T*>(
1072 DynamicCastMessage<T>(static_cast<const MessageLite*>(from)));
1073 }
1074
1075 namespace internal {
1076 [[noreturn]] PROTOBUF_EXPORT void FailDynamicCast(const MessageLite& from,
1077 const MessageLite& to);
1078 }
1079
1080 template <typename T>
1081 const T& DynamicCastMessage(const MessageLite& from) {
1082 const T* destination_message = DynamicCastMessage<T>(&from);
1083 if (ABSL_PREDICT_FALSE(destination_message == nullptr)) {
1084
1085
1086 internal::FailDynamicCast(from, T::default_instance());
1087 }
1088 return *destination_message;
1089 }
1090
1091 template <typename T>
1092 T& DynamicCastMessage(MessageLite& from) {
1093 return const_cast<T&>(
1094 DynamicCastMessage<T>(static_cast<const MessageLite&>(from)));
1095 }
1096
1097 template <typename T>
1098 const T* DownCastMessage(const MessageLite* from) {
1099 internal::StrongReferenceToType<T>();
1100 ABSL_DCHECK(DynamicCastMessage<T>(from) == from)
1101 << "Cannot downcast " << from->GetTypeName() << " to "
1102 << T::default_instance().GetTypeName();
1103 return static_cast<const T*>(from);
1104 }
1105
1106 template <typename T>
1107 T* DownCastMessage(MessageLite* from) {
1108 return const_cast<T*>(
1109 DownCastMessage<T>(static_cast<const MessageLite*>(from)));
1110 }
1111
1112 template <typename T>
1113 const T& DownCastMessage(const MessageLite& from) {
1114 return *DownCastMessage<T>(&from);
1115 }
1116
1117 template <typename T>
1118 T& DownCastMessage(MessageLite& from) {
1119 return *DownCastMessage<T>(&from);
1120 }
1121
1122 template <>
1123 inline const MessageLite* DynamicCastMessage(const MessageLite* from) {
1124 return from;
1125 }
1126 template <>
1127 inline const MessageLite* DownCastMessage(const MessageLite* from) {
1128 return from;
1129 }
1130
1131
1132
1133 template <typename T>
1134 PROTOBUF_DEPRECATE_AND_INLINE()
1135 const T* DynamicCastToGenerated(const MessageLite* from) {
1136 return DynamicCastMessage<T>(from);
1137 }
1138
1139 template <typename T>
1140 PROTOBUF_DEPRECATE_AND_INLINE()
1141 T* DynamicCastToGenerated(MessageLite* from) {
1142 return DynamicCastMessage<T>(from);
1143 }
1144
1145 template <typename T>
1146 PROTOBUF_DEPRECATE_AND_INLINE()
1147 const T& DynamicCastToGenerated(const MessageLite& from) {
1148 return DynamicCastMessage<T>(from);
1149 }
1150
1151 template <typename T>
1152 PROTOBUF_DEPRECATE_AND_INLINE()
1153 T& DynamicCastToGenerated(MessageLite& from) {
1154 return DynamicCastMessage<T>(from);
1155 }
1156
1157 template <typename T>
1158 PROTOBUF_DEPRECATE_AND_INLINE()
1159 const T* DownCastToGenerated(const MessageLite* from) {
1160 return DownCastMessage<T>(from);
1161 }
1162
1163 template <typename T>
1164 PROTOBUF_DEPRECATE_AND_INLINE()
1165 T* DownCastToGenerated(MessageLite* from) {
1166 return DownCastMessage<T>(from);
1167 }
1168
1169 template <typename T>
1170 PROTOBUF_DEPRECATE_AND_INLINE()
1171 const T& DownCastToGenerated(const MessageLite& from) {
1172 return DownCastMessage<T>(from);
1173 }
1174
1175 template <typename T>
1176 PROTOBUF_DEPRECATE_AND_INLINE()
1177 T& DownCastToGenerated(MessageLite& from) {
1178 return DownCastMessage<T>(from);
1179 }
1180
1181 }
1182 }
1183
1184 #include "google/protobuf/port_undef.inc"
1185
1186 #endif