File indexing completed on 2026-09-25 09:15:31
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef GOOGLE_PROTOBUF_EXTENSION_SET_H__
0016 #define GOOGLE_PROTOBUF_EXTENSION_SET_H__
0017
0018 #include <algorithm>
0019 #include <atomic>
0020 #include <cassert>
0021 #include <cstddef>
0022 #include <cstdint>
0023 #include <initializer_list>
0024 #include <string>
0025 #include <tuple>
0026 #include <type_traits>
0027 #include <utility>
0028 #include <variant>
0029 #include <vector>
0030
0031 #include "google/protobuf/stubs/common.h"
0032 #include "absl/base/casts.h"
0033 #include "absl/base/prefetch.h"
0034 #include "absl/container/btree_map.h"
0035 #include "absl/log/absl_check.h"
0036 #include "absl/strings/string_view.h"
0037 #include "google/protobuf/generated_enum_util.h"
0038 #include "google/protobuf/generated_message_tctable_decl.h"
0039 #include "google/protobuf/internal_visibility.h"
0040 #include "google/protobuf/port.h"
0041 #include "google/protobuf/io/coded_stream.h"
0042 #include "google/protobuf/message_lite.h"
0043 #include "google/protobuf/parse_context.h"
0044 #include "google/protobuf/repeated_field.h"
0045 #include "google/protobuf/repeated_ptr_field.h"
0046 #include "google/protobuf/wire_format_lite.h"
0047
0048
0049 #include "google/protobuf/port_def.inc" // Must be last
0050
0051
0052 #ifdef SWIG
0053 #error "You cannot SWIG proto headers"
0054 #endif
0055
0056
0057 namespace google {
0058 namespace protobuf {
0059 class Arena;
0060 class Descriptor;
0061 class FieldDescriptor;
0062 class DescriptorPool;
0063 class MessageLite;
0064 class Message;
0065 class MessageFactory;
0066 class Reflection;
0067 class UnknownFieldSet;
0068 class FeatureSet;
0069 namespace internal {
0070 struct DescriptorTable;
0071 class FieldSkipper;
0072 class ReflectionVisit;
0073 class WireFormat;
0074 struct DynamicExtensionInfoHelper;
0075 void InitializeLazyExtensionSet();
0076 }
0077 }
0078 }
0079 namespace pb {
0080 class CppFeatures;
0081 namespace internal {
0082
0083
0084
0085 template <class T>
0086 inline ::absl::string_view GetFeatureSetDefaultsData();
0087 }
0088 }
0089
0090 namespace google {
0091 namespace protobuf {
0092 namespace internal {
0093
0094 class InternalMetadata;
0095
0096 namespace v2 {
0097 class TableDrivenMessage;
0098 }
0099
0100
0101
0102
0103
0104 typedef uint8_t FieldType;
0105
0106
0107
0108 typedef bool EnumValidityFuncWithArg(const void* arg, int number);
0109
0110 enum class LazyAnnotation : int8_t {
0111 kUndefined = 0,
0112 kLazy = 1,
0113 kEager = 2,
0114 };
0115
0116
0117 struct ExtensionInfo {
0118 constexpr ExtensionInfo() : enum_validity_check() {}
0119 constexpr ExtensionInfo(const MessageLite* extendee, int param_number,
0120 FieldType type_param, bool isrepeated, bool ispacked)
0121 : message(extendee),
0122 number(param_number),
0123 type(type_param),
0124 is_repeated(isrepeated),
0125 is_packed(ispacked),
0126 enum_validity_check() {}
0127 constexpr ExtensionInfo(const MessageLite* extendee, int param_number,
0128 FieldType type_param, bool isrepeated, bool ispacked,
0129 LazyEagerVerifyFnType verify_func,
0130 LazyAnnotation islazy = LazyAnnotation::kUndefined)
0131 : message(extendee),
0132 number(param_number),
0133 type(type_param),
0134 is_repeated(isrepeated),
0135 is_packed(ispacked),
0136 is_lazy(islazy),
0137 enum_validity_check(),
0138 lazy_eager_verify_func(verify_func) {}
0139
0140 const MessageLite* message = nullptr;
0141 int number = 0;
0142
0143 FieldType type = 0;
0144 bool is_repeated = false;
0145 bool is_packed = false;
0146 LazyAnnotation is_lazy = LazyAnnotation::kUndefined;
0147
0148 struct EnumValidityCheck {
0149
0150 EnumValidityFuncWithArg* func;
0151 const void* arg;
0152
0153 bool IsValid(int value) const {
0154 return func != nullptr ? func(arg, value)
0155 : internal::ValidateEnum(
0156 value, static_cast<const uint32_t*>(arg));
0157 }
0158 };
0159
0160 struct MessageInfo {
0161 const MessageLite* prototype = nullptr;
0162
0163
0164
0165 const internal::TcParseTableBase* tc_table = nullptr;
0166
0167 const ClassData* GetClassData() const {
0168 #ifdef PROTOBUF_CONSTINIT_DEFAULT_INSTANCES
0169 return tc_table->class_data;
0170 #else
0171 return google::protobuf::internal::GetClassData(*prototype);
0172 #endif
0173 }
0174 };
0175
0176 union {
0177 EnumValidityCheck enum_validity_check;
0178 MessageInfo message_info;
0179 };
0180
0181
0182
0183
0184 const FieldDescriptor* descriptor = nullptr;
0185
0186
0187
0188
0189 LazyEagerVerifyFnType lazy_eager_verify_func = nullptr;
0190 };
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200 class PROTOBUF_EXPORT GeneratedExtensionFinder {
0201 public:
0202 explicit GeneratedExtensionFinder(const MessageLite* extendee)
0203 : extendee_(extendee) {}
0204
0205
0206 bool Find(int number, ExtensionInfo* output);
0207
0208 private:
0209 const MessageLite* extendee_;
0210 };
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226 class PROTOBUF_EXPORT ExtensionSet {
0227 public:
0228 constexpr ExtensionSet() : ExtensionSet(nullptr) {}
0229 ExtensionSet(const ExtensionSet& rhs) = delete;
0230
0231
0232 ExtensionSet(internal::InternalVisibility, Arena* arena)
0233 : ExtensionSet(arena) {}
0234
0235
0236
0237 explicit constexpr ExtensionSet(Arena* arena);
0238 ExtensionSet(ArenaInitialized, Arena* arena) : ExtensionSet(arena) {}
0239
0240 ExtensionSet& operator=(const ExtensionSet&) = delete;
0241 ~ExtensionSet();
0242
0243
0244
0245
0246
0247
0248 static void RegisterExtension(const MessageLite* extendee, int number,
0249 FieldType type, bool is_repeated,
0250 bool is_packed);
0251 static void RegisterEnumExtension(const MessageLite* extendee, int number,
0252 FieldType type, bool is_repeated,
0253 bool is_packed,
0254 const uint32_t* validation_data);
0255 static void RegisterMessageExtension(const MessageLite* extendee, int number,
0256 FieldType type, bool is_repeated,
0257 bool is_packed,
0258 const MessageLite* prototype,
0259 LazyEagerVerifyFnType verify_func,
0260 LazyAnnotation is_lazy);
0261
0262
0263
0264
0265
0266
0267 struct WeakPrototypeRef {
0268 const internal::DescriptorTable* table;
0269 int index;
0270 };
0271 static bool ShouldRegisterAtThisTime(
0272 std::initializer_list<WeakPrototypeRef> messages,
0273 bool is_preregistration);
0274
0275
0276
0277
0278
0279
0280 void AppendToList(const Descriptor* extendee, const DescriptorPool* pool,
0281 std::vector<const FieldDescriptor*>* output) const;
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313 bool Has(int number) const;
0314 int ExtensionSize(int number) const;
0315 int NumExtensions() const;
0316 FieldType ExtensionType(int number) const;
0317 void ClearExtension(int number);
0318
0319
0320
0321 template <typename T>
0322 const T& Get(int number,
0323 const internal::type_identity_t<T>& default_value) const {
0324 const Extension* extension = FindOrNull(number);
0325 if (extension == nullptr || extension->is_cleared) {
0326 return default_value;
0327 } else {
0328 return extension->Get<T>();
0329 }
0330 }
0331
0332 template <typename T, typename U>
0333 void Set(int number, FieldType type, U&& value,
0334 const FieldDescriptor* descriptor) {
0335 if constexpr (Extension::kUsesPointer<T>) {
0336 Extension& extension =
0337 FindOrCreate(number, type, false, false, descriptor, CreateImpl<T>);
0338 *extension.Mutable<T>() = std::forward<U>(value);
0339 } else {
0340 FindOrCreate(number, type, false, false, descriptor, nullptr)
0341 .Mutable<T>() = std::forward<U>(value);
0342 }
0343 }
0344
0345 const MessageLite& GetMessage(int number,
0346 const MessageLite& default_value) const;
0347 const MessageLite& GetMessage(int number, const Descriptor* message_type,
0348 MessageFactory* factory) const;
0349
0350
0351
0352
0353 #define desc const FieldDescriptor* descriptor
0354 std::string* MutableString(int number, FieldType type, desc);
0355 MessageLite* MutableMessage(int number, FieldType type,
0356 const MessageLite& prototype, desc);
0357 MessageLite* MutableMessage(const FieldDescriptor* descriptor,
0358 MessageFactory* factory);
0359
0360
0361
0362 void SetAllocatedMessage(int number, FieldType type,
0363 const FieldDescriptor* descriptor,
0364 MessageLite* message);
0365 void UnsafeArenaSetAllocatedMessage(int number, FieldType type,
0366 const FieldDescriptor* descriptor,
0367 MessageLite* message);
0368 [[nodiscard]] MessageLite* ReleaseMessage(int number,
0369 const MessageLite& prototype);
0370 MessageLite* UnsafeArenaReleaseMessage(int number,
0371 const MessageLite& prototype);
0372
0373 [[nodiscard]] MessageLite* ReleaseMessage(const FieldDescriptor* descriptor,
0374 MessageFactory* factory);
0375 MessageLite* UnsafeArenaReleaseMessage(const FieldDescriptor* descriptor,
0376 MessageFactory* factory);
0377 #undef desc
0378 Arena* GetArena() const { return arena_; }
0379
0380
0381
0382
0383
0384
0385 const void* GetRawRepeatedField(int number, const void* default_value) const;
0386
0387
0388
0389 void* MutableRawRepeatedField(int number, FieldType field_type, bool packed,
0390 const FieldDescriptor* desc);
0391
0392
0393
0394
0395
0396 void* MutableRawRepeatedField(int number);
0397
0398 template <typename T>
0399 const T& GetRepeated(int number, int index) const {
0400 const Extension* extension = FindOrNull(number);
0401 ABSL_CHECK(extension != nullptr) << "Index out-of-bounds (field is empty).";
0402 return extension->Get<RepFor<T>>().Get(index);
0403 }
0404
0405 template <typename T, typename U>
0406 void SetRepeated(int number, int index, U&& value) {
0407 Extension* extension = FindOrNull(number);
0408 ABSL_CHECK(extension != nullptr) << "Index out-of-bounds (field is empty).";
0409 (*extension->Mutable<RepFor<T>>())[index] = std::forward<U>(value);
0410 }
0411
0412 template <typename T>
0413 auto& Add(int number, FieldType type, const FieldDescriptor* descriptor) {
0414 static_assert(std::is_class_v<T>);
0415 Extension& ext = FindOrCreate(number, type, true, false, descriptor,
0416 &CreateImpl<RepFor<T>>);
0417 return *ext.Mutable<RepFor<T>>()->Add();
0418 }
0419
0420 template <typename T>
0421 void Add(int number, FieldType type, bool packed, T value,
0422 const FieldDescriptor* descriptor) {
0423 static_assert(std::is_arithmetic_v<T>,
0424 "Only arithmetic types take `packed`");
0425 Extension& ext = FindOrCreate(number, type, true, packed, descriptor,
0426 &CreateImpl<RepFor<T>>);
0427 ext.Mutable<RepFor<T>>()->Add(value);
0428 }
0429
0430 const MessageLite& GetRepeatedMessage(int number, int index) const;
0431 std::string* MutableRepeatedString(int number, int index);
0432 MessageLite* MutableRepeatedMessage(int number, int index);
0433
0434 #define desc const FieldDescriptor* descriptor
0435 std::string* AddString(int number, FieldType type, desc);
0436 MessageLite* AddMessage(int number, FieldType type,
0437 const ClassData* class_data, desc);
0438 MessageLite* AddMessage(const FieldDescriptor* descriptor,
0439 MessageFactory* factory);
0440 void AddAllocatedMessage(const FieldDescriptor* descriptor,
0441 MessageLite* new_entry);
0442 void UnsafeArenaAddAllocatedMessage(const FieldDescriptor* descriptor,
0443 MessageLite* new_entry);
0444 #undef desc
0445
0446 void RemoveLast(int number);
0447 [[nodiscard]] MessageLite* ReleaseLast(int number);
0448 MessageLite* UnsafeArenaReleaseLast(int number);
0449 void SwapElements(int number, int index1, int index2);
0450
0451
0452
0453
0454
0455
0456
0457 void Clear();
0458 void MergeFrom(const MessageLite* extendee, const ExtensionSet& other);
0459 void Swap(const MessageLite* extendee, ExtensionSet* other);
0460 void InternalSwap(ExtensionSet* other);
0461 void SwapExtension(const MessageLite* extendee, ExtensionSet* other,
0462 int number);
0463 void UnsafeShallowSwapExtension(ExtensionSet* other, int number);
0464 bool IsInitialized(const MessageLite* extendee) const;
0465
0466
0467 const char* ParseField(uint64_t tag, const char* ptr,
0468 const MessageLite* extendee,
0469 internal::InternalMetadata* metadata,
0470 internal::ParseContext* ctx);
0471
0472 const char* ParseField(uint64_t tag, const char* ptr, const Message* extendee,
0473 internal::InternalMetadata* metadata,
0474 internal::ParseContext* ctx);
0475 template <typename Msg>
0476 const char* ParseMessageSet(const char* ptr, const Msg* extendee,
0477 InternalMetadata* metadata,
0478 internal::ParseContext* ctx) {
0479 while (!ctx->Done(&ptr)) {
0480 uint32_t tag;
0481 ptr = ReadTag(ptr, &tag);
0482 GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
0483 if (tag == WireFormatLite::kMessageSetItemStartTag) {
0484 ptr = ctx->ParseGroupInlined(ptr, tag, [&](const char* ptr) {
0485 return ParseMessageSetItem(ptr, extendee, metadata, ctx);
0486 });
0487 GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
0488 } else {
0489 if (tag == 0 || (tag & 7) == 4) {
0490 ctx->SetLastTag(tag);
0491 return ptr;
0492 }
0493 ptr = ParseField(tag, ptr, extendee, metadata, ctx);
0494 GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
0495 }
0496 }
0497 return ptr;
0498 }
0499
0500
0501
0502
0503
0504 void SerializeWithCachedSizes(const MessageLite* extendee,
0505 int start_field_number, int end_field_number,
0506 io::CodedOutputStream* output) const {
0507 output->SetCur(_InternalSerialize(extendee, start_field_number,
0508 end_field_number, output->Cur(),
0509 output->EpsCopy()));
0510 }
0511
0512
0513
0514
0515
0516
0517 uint8_t* _InternalSerialize(const MessageLite* extendee,
0518 int start_field_number, int end_field_number,
0519 uint8_t* target,
0520 io::EpsCopyOutputStream* stream) const {
0521 if (flat_size_ == 0) {
0522 assert(!is_large());
0523 return target;
0524 }
0525 return _InternalSerializeImpl(extendee, start_field_number,
0526 end_field_number, target, stream);
0527 }
0528
0529
0530 uint8_t* _InternalSerializeAll(const MessageLite* extendee, uint8_t* target,
0531 io::EpsCopyOutputStream* stream) const {
0532 if (flat_size_ == 0) {
0533 assert(!is_large());
0534 return target;
0535 }
0536 return _InternalSerializeAllImpl(extendee, target, stream);
0537 }
0538
0539
0540 void SerializeMessageSetWithCachedSizes(const MessageLite* extendee,
0541 io::CodedOutputStream* output) const {
0542 output->SetCur(InternalSerializeMessageSetWithCachedSizesToArray(
0543 extendee, output->Cur(), output->EpsCopy()));
0544 }
0545 uint8_t* InternalSerializeMessageSetWithCachedSizesToArray(
0546 const MessageLite* extendee, uint8_t* target,
0547 io::EpsCopyOutputStream* stream) const;
0548
0549
0550
0551
0552 uint8_t* SerializeWithCachedSizesToArray(int start_field_number,
0553 int end_field_number,
0554 uint8_t* target) const;
0555 uint8_t* SerializeMessageSetWithCachedSizesToArray(
0556 const MessageLite* extendee, uint8_t* target) const;
0557
0558
0559 size_t ByteSize() const;
0560
0561
0562 size_t MessageSetByteSize() const;
0563
0564
0565
0566
0567
0568
0569
0570
0571 size_t SpaceUsedExcludingSelfLong() const;
0572
0573
0574
0575
0576
0577
0578 int SpaceUsedExcludingSelf() const;
0579
0580 static constexpr size_t InternalGetArenaOffset(internal::InternalVisibility) {
0581 return PROTOBUF_FIELD_OFFSET(ExtensionSet, arena_);
0582 }
0583
0584 private:
0585 template <typename Type>
0586 friend class PrimitiveTypeTraits;
0587
0588 template <typename Type>
0589 friend class RepeatedPrimitiveTypeTraits;
0590
0591 template <typename Type>
0592 friend class EnumTypeTraits;
0593
0594 template <typename Type>
0595 friend class RepeatedEnumTypeTraits;
0596
0597 friend class google::protobuf::Reflection;
0598 friend class google::protobuf::internal::ReflectionVisit;
0599 friend struct google::protobuf::internal::DynamicExtensionInfoHelper;
0600 friend class google::protobuf::internal::WireFormat;
0601 friend class google::protobuf::internal::v2::TableDrivenMessage;
0602
0603 friend void internal::InitializeLazyExtensionSet();
0604
0605
0606 template <typename T>
0607 using RepFor = std::conditional_t<std::is_arithmetic_v<T>,
0608 RepeatedField<std::decay_t<T>>,
0609 RepeatedPtrField<std::decay_t<T>>>;
0610
0611 static bool FieldTypeIsPointer(FieldType type);
0612
0613 size_t GetMessageByteSizeLong(int number) const;
0614 uint8_t* InternalSerializeMessage(int number, const MessageLite* prototype,
0615 uint8_t* target,
0616 io::EpsCopyOutputStream* stream) const;
0617
0618
0619 uint8_t* _InternalSerializeImpl(const MessageLite* extendee,
0620 int start_field_number, int end_field_number,
0621 uint8_t* target,
0622 io::EpsCopyOutputStream* stream) const;
0623
0624 uint8_t* _InternalSerializeAllImpl(const MessageLite* extendee,
0625 uint8_t* target,
0626 io::EpsCopyOutputStream* stream) const;
0627
0628
0629
0630 uint8_t* _InternalSerializeImplLarge(const MessageLite* extendee,
0631 int start_field_number,
0632 int end_field_number, uint8_t* target,
0633 io::EpsCopyOutputStream* stream) const;
0634
0635 class PROTOBUF_EXPORT LazyMessageExtension {
0636 public:
0637 LazyMessageExtension() = default;
0638 LazyMessageExtension(const LazyMessageExtension&) = delete;
0639 LazyMessageExtension& operator=(const LazyMessageExtension&) = delete;
0640 virtual ~LazyMessageExtension() = default;
0641
0642 virtual LazyMessageExtension* Clone(Arena* arena,
0643 const LazyMessageExtension& other,
0644 Arena* other_arena) const = 0;
0645 virtual const MessageLite& GetMessage(const MessageLite& prototype,
0646 Arena* arena) const = 0;
0647 virtual const MessageLite& GetMessageIgnoreUnparsed(
0648 const MessageLite& prototype, Arena* arena) const = 0;
0649 virtual MessageLite* MutableMessage(const MessageLite& prototype,
0650 Arena* arena) = 0;
0651 virtual void SetAllocatedMessage(MessageLite* message, Arena* arena) = 0;
0652 virtual void UnsafeArenaSetAllocatedMessage(MessageLite* message,
0653 Arena* arena) = 0;
0654 [[nodiscard]] virtual MessageLite* ReleaseMessage(
0655 const MessageLite& prototype, Arena* arena) = 0;
0656 virtual MessageLite* UnsafeArenaReleaseMessage(const MessageLite& prototype,
0657 Arena* arena) = 0;
0658
0659 virtual bool IsInitialized(const MessageLite* prototype,
0660 Arena* arena) const = 0;
0661 virtual bool IsEagerSerializeSafe(const MessageLite* prototype,
0662 Arena* arena) const = 0;
0663
0664 [[deprecated("Please use ByteSizeLong() instead")]] virtual int ByteSize()
0665 const {
0666 return internal::ToIntSize(ByteSizeLong());
0667 }
0668 virtual size_t ByteSizeLong() const = 0;
0669 virtual size_t SpaceUsedLong() const = 0;
0670
0671 virtual std::variant<size_t, const MessageLite*> UnparsedSizeOrMessage()
0672 const = 0;
0673
0674 virtual void MergeFrom(const MessageLite* prototype,
0675 const LazyMessageExtension& other, Arena* arena,
0676 Arena* other_arena) = 0;
0677 virtual void MergeFromMessage(const MessageLite& msg, Arena* arena) = 0;
0678 virtual void Clear() = 0;
0679
0680 virtual const char* _InternalParse(const MessageLite& prototype,
0681 Arena* arena, const char* ptr,
0682 ParseContext* ctx) = 0;
0683 virtual uint8_t* WriteMessageToArray(
0684 const MessageLite* prototype, int number, uint8_t* target,
0685 io::EpsCopyOutputStream* stream) const = 0;
0686
0687
0688 private:
0689 virtual void UnusedKeyMethod();
0690 };
0691
0692 static LazyMessageExtension* MaybeCreateLazyExtensionImpl(Arena* arena);
0693 static LazyMessageExtension* MaybeCreateLazyExtension(Arena* arena) {
0694 auto* f = maybe_create_lazy_extension_.load(std::memory_order_relaxed);
0695 return f != nullptr ? f(arena) : nullptr;
0696 }
0697 static std::atomic<LazyMessageExtension* (*)(Arena* arena)>
0698 maybe_create_lazy_extension_;
0699
0700
0701
0702 class TrivialAtomicInt {
0703 public:
0704 int operator()() const {
0705 return reinterpret_cast<const AtomicT*>(int_)->load(
0706 std::memory_order_relaxed);
0707 }
0708 void set(int v) {
0709 reinterpret_cast<AtomicT*>(int_)->store(v, std::memory_order_relaxed);
0710 }
0711
0712 private:
0713 using AtomicT = std::atomic<int>;
0714 alignas(AtomicT) char int_[sizeof(AtomicT)];
0715 };
0716
0717 struct Extension {
0718
0719 uint8_t* InternalSerializeFieldWithCachedSizesToArray(
0720 const MessageLite* extendee, const ExtensionSet* extension_set,
0721 int number, uint8_t* target, io::EpsCopyOutputStream* stream) const;
0722 uint8_t* InternalSerializeMessageSetItemWithCachedSizesToArray(
0723 const MessageLite* extendee, const ExtensionSet* extension_set,
0724 int number, uint8_t* target, io::EpsCopyOutputStream* stream) const;
0725 size_t ByteSize(int number) const;
0726 size_t MessageSetItemByteSize(int number) const;
0727 void Clear();
0728 int GetSize() const;
0729 void Free();
0730 size_t SpaceUsedExcludingSelfLong() const;
0731 bool IsInitialized(const ExtensionSet* ext_set, const MessageLite* extendee,
0732 int number, Arena* arena) const;
0733 const void* PrefetchPtr() const {
0734 ABSL_DCHECK_EQ(is_pointer, is_repeated || FieldTypeIsPointer(type));
0735
0736
0737 return is_pointer ? raw_ptr() : this;
0738 }
0739
0740
0741
0742
0743
0744
0745 union Pointer {
0746 std::string* string_value;
0747 MessageLite* message_value;
0748 LazyMessageExtension* lazymessage_value;
0749
0750 RepeatedField<int32_t>* repeated_int32_t_value;
0751 RepeatedField<int64_t>* repeated_int64_t_value;
0752 RepeatedField<uint32_t>* repeated_uint32_t_value;
0753 RepeatedField<uint64_t>* repeated_uint64_t_value;
0754 RepeatedField<float>* repeated_float_value;
0755 RepeatedField<double>* repeated_double_value;
0756 RepeatedField<bool>* repeated_bool_value;
0757 RepeatedPtrField<std::string>* repeated_string_value;
0758 RepeatedPtrField<MessageLite>* repeated_message_value;
0759 };
0760
0761 union {
0762 int32_t int32_t_value;
0763 int64_t int64_t_value;
0764 uint32_t uint32_t_value;
0765 uint64_t uint64_t_value;
0766 float float_value;
0767 double double_value;
0768 bool bool_value;
0769 Pointer ptr;
0770 };
0771
0772 template <typename T>
0773 static inline constexpr auto kUnionMember = std::get<T Extension::*>(
0774 std::tuple{&Extension::int32_t_value, &Extension::int64_t_value,
0775 &Extension::uint32_t_value, &Extension::uint64_t_value,
0776 &Extension::float_value, &Extension::double_value,
0777 &Extension::bool_value});
0778
0779 template <typename T>
0780 static inline constexpr auto kPtrUnionMember =
0781 std::get<T Pointer::*>(std::tuple{
0782
0783 &Pointer::string_value, &Pointer::repeated_int32_t_value,
0784 &Pointer::repeated_int64_t_value, &Pointer::repeated_uint32_t_value,
0785 &Pointer::repeated_uint64_t_value, &Pointer::repeated_float_value,
0786 &Pointer::repeated_double_value, &Pointer::repeated_bool_value,
0787 &Pointer::repeated_string_value, &Pointer::repeated_message_value});
0788
0789 void* raw_ptr() const { return absl::bit_cast<void*>(ptr); }
0790
0791 template <typename T>
0792 static inline constexpr bool kUsesPointer = !std::is_arithmetic_v<T>;
0793
0794 template <typename T>
0795 void VerifyType() const {
0796 ABSL_DCHECK_EQ(is_repeated || FieldTypeIsPointer(type), kUsesPointer<T>);
0797 constexpr auto expected_cpp_type = WireFormatLite::CppTypeFor<T>();
0798 ABSL_DCHECK_EQ(
0799 +expected_cpp_type,
0800 +(type == WireFormatLite::TYPE_ENUM
0801 ? WireFormatLite::CPPTYPE_INT32
0802 : WireFormatLite::FieldTypeToCppType(
0803 static_cast<WireFormatLite::FieldType>(type))));
0804 }
0805
0806
0807
0808 template <typename T>
0809 const T& Get() const {
0810 VerifyType<T>();
0811 if constexpr (kUsesPointer<T>) {
0812 return *(ptr.*kPtrUnionMember<T*>);
0813 } else {
0814 return this->*kUnionMember<T>;
0815 }
0816 }
0817
0818
0819
0820 template <typename T>
0821 auto& Mutable() {
0822 VerifyType<T>();
0823 if constexpr (kUsesPointer<T>) {
0824 return ptr.*kPtrUnionMember<T*>;
0825 } else {
0826 return this->*kUnionMember<T>;
0827 }
0828 }
0829
0830 FieldType type;
0831 bool is_repeated;
0832
0833
0834 bool is_pointer : 1;
0835
0836
0837
0838
0839
0840
0841
0842
0843 bool is_cleared : 1;
0844
0845
0846
0847
0848
0849
0850 bool is_lazy : 1;
0851
0852
0853 bool is_packed;
0854
0855
0856
0857 mutable TrivialAtomicInt cached_size;
0858
0859
0860
0861
0862 const FieldDescriptor* descriptor;
0863 };
0864
0865
0866
0867
0868
0869
0870
0871
0872
0873 struct KeyValue {
0874 int first;
0875 Extension second;
0876 };
0877
0878 using LargeMap = absl::btree_map<int, Extension>;
0879
0880
0881
0882
0883 const Extension* FindOrNull(int key) const;
0884 Extension* FindOrNull(int key);
0885
0886
0887 const Extension* FindOrNullInLargeMap(int key) const;
0888 Extension* FindOrNullInLargeMap(int key);
0889
0890
0891
0892
0893 std::pair<Extension*, bool> Insert(int key);
0894
0895 std::pair<Extension*, bool> InternalInsertIntoLargeMap(int key);
0896
0897
0898
0899 void GrowCapacity(size_t minimum_new_capacity);
0900
0901 static constexpr uint16_t kMaximumFlatCapacity = 256;
0902
0903
0904
0905
0906 void InternalReserveSmallCapacityFromEmpty(size_t minimum_new_capacity);
0907
0908 bool is_large() const { return static_cast<int16_t>(flat_size_) < 0; }
0909
0910
0911 void Erase(int key);
0912
0913
0914
0915 size_t Size() const {
0916 return ABSL_PREDICT_FALSE(is_large()) ? map_.large->size() : flat_size_;
0917 }
0918
0919
0920 struct Prefetch {
0921 void operator()(const void* ptr) const { absl::PrefetchToLocalCache(ptr); }
0922 };
0923 struct PrefetchNta {
0924 void operator()(const void* ptr) const {
0925 absl::PrefetchToLocalCacheNta(ptr);
0926 }
0927 };
0928
0929 template <typename Iterator, typename KeyValueFunctor,
0930 typename PrefetchFunctor>
0931 static void ForEachPrefetchImpl(Iterator it, Iterator end,
0932 KeyValueFunctor func,
0933 PrefetchFunctor prefetch_func) {
0934
0935
0936 constexpr int kPrefetchDistance = 16;
0937 Iterator prefetch = it;
0938
0939 for (int i = 0; prefetch != end && i < kPrefetchDistance; ++prefetch, ++i) {
0940 prefetch_func(prefetch->second.PrefetchPtr());
0941 }
0942
0943
0944 for (; prefetch != end; ++it, ++prefetch) {
0945 func(it->first, it->second);
0946 prefetch_func(prefetch->second.PrefetchPtr());
0947 }
0948
0949 for (; it != end; ++it) func(it->first, it->second);
0950 }
0951
0952
0953
0954
0955
0956
0957 template <typename KeyValueFunctor, typename PrefetchFunctor>
0958 void ForEach(KeyValueFunctor func, PrefetchFunctor prefetch_func) {
0959 if (ABSL_PREDICT_FALSE(is_large())) {
0960 ForEachPrefetchImpl(map_.large->begin(), map_.large->end(),
0961 std::move(func), std::move(prefetch_func));
0962 return;
0963 }
0964 ForEachPrefetchImpl(flat_begin(), flat_end(), std::move(func),
0965 std::move(prefetch_func));
0966 }
0967
0968 template <typename KeyValueFunctor, typename PrefetchFunctor>
0969 void ForEach(KeyValueFunctor func, PrefetchFunctor prefetch_func) const {
0970 if (ABSL_PREDICT_FALSE(is_large())) {
0971 ForEachPrefetchImpl(map_.large->begin(), map_.large->end(),
0972 std::move(func), std::move(prefetch_func));
0973 return;
0974 }
0975 ForEachPrefetchImpl(flat_begin(), flat_end(), std::move(func),
0976 std::move(prefetch_func));
0977 }
0978
0979
0980
0981 template <typename Iterator, typename KeyValueFunctor>
0982 static void ForEachNoPrefetch(Iterator begin, Iterator end,
0983 KeyValueFunctor func) {
0984 for (Iterator it = begin; it != end; ++it) func(it->first, it->second);
0985 }
0986
0987
0988 template <typename KeyValueFunctor>
0989 void ForEachNoPrefetch(KeyValueFunctor func) {
0990 if (ABSL_PREDICT_FALSE(is_large())) {
0991 ForEachNoPrefetch(map_.large->begin(), map_.large->end(),
0992 std::move(func));
0993 return;
0994 }
0995 ForEachNoPrefetch(flat_begin(), flat_end(), std::move(func));
0996 }
0997
0998
0999 template <typename KeyValueFunctor>
1000 void ForEachNoPrefetch(KeyValueFunctor func) const {
1001 if (ABSL_PREDICT_FALSE(is_large())) {
1002 ForEachNoPrefetch(map_.large->begin(), map_.large->end(),
1003 std::move(func));
1004 return;
1005 }
1006 ForEachNoPrefetch(flat_begin(), flat_end(), std::move(func));
1007 }
1008
1009
1010 bool IsCompletelyEmpty() const {
1011 return flat_size_ == 0 && flat_capacity_ == 0;
1012 }
1013
1014
1015
1016
1017
1018
1019
1020 void InternalMergeFromSmallToEmpty(const MessageLite* extendee,
1021 const ExtensionSet& other);
1022
1023 void InternalMergeFromSlow(const MessageLite* extendee,
1024 const ExtensionSet& other);
1025
1026 void InternalExtensionMergeFrom(const MessageLite* extendee, int number,
1027 const Extension& other_extension,
1028 Arena* other_arena);
1029
1030 void InternalExtensionMergeFromIntoUninitializedExtension(
1031 Extension& dst_extension, const MessageLite* extendee, int number,
1032 const Extension& other_extension, Arena* other_arena);
1033
1034 inline static bool is_packable(WireFormatLite::WireType type) {
1035 switch (type) {
1036 case WireFormatLite::WIRETYPE_VARINT:
1037 case WireFormatLite::WIRETYPE_FIXED64:
1038 case WireFormatLite::WIRETYPE_FIXED32:
1039 return true;
1040 case WireFormatLite::WIRETYPE_LENGTH_DELIMITED:
1041 case WireFormatLite::WIRETYPE_START_GROUP:
1042 case WireFormatLite::WIRETYPE_END_GROUP:
1043 return false;
1044
1045
1046
1047
1048 }
1049 Unreachable();
1050 return false;
1051 }
1052
1053
1054
1055
1056
1057 template <typename ExtensionFinder>
1058 bool FindExtensionInfoFromTag(uint32_t tag, ExtensionFinder* extension_finder,
1059 int* field_number, ExtensionInfo* extension,
1060 bool* was_packed_on_wire) {
1061 *field_number = WireFormatLite::GetTagFieldNumber(tag);
1062 WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag);
1063 return FindExtensionInfoFromFieldNumber(wire_type, *field_number,
1064 extension_finder, extension,
1065 was_packed_on_wire);
1066 }
1067
1068
1069
1070
1071
1072 template <typename ExtensionFinder>
1073 bool FindExtensionInfoFromFieldNumber(int wire_type, int field_number,
1074 ExtensionFinder* extension_finder,
1075 ExtensionInfo* extension,
1076 bool* was_packed_on_wire) const {
1077 if (!extension_finder->Find(field_number, extension)) {
1078 return false;
1079 }
1080
1081 ABSL_DCHECK(extension->type > 0 &&
1082 extension->type <= WireFormatLite::MAX_FIELD_TYPE);
1083 auto real_type = static_cast<WireFormatLite::FieldType>(extension->type);
1084
1085 WireFormatLite::WireType expected_wire_type =
1086 WireFormatLite::WireTypeForFieldType(real_type);
1087
1088
1089 *was_packed_on_wire = false;
1090 if (extension->is_repeated &&
1091 wire_type == WireFormatLite::WIRETYPE_LENGTH_DELIMITED &&
1092 is_packable(expected_wire_type)) {
1093 *was_packed_on_wire = true;
1094 return true;
1095 }
1096
1097 return expected_wire_type == wire_type;
1098 }
1099
1100
1101
1102 const MessageLite* GetPrototypeForLazyMessage(const MessageLite* extendee,
1103 int number) const;
1104
1105
1106 bool HasLazy(int number) const;
1107
1108
1109
1110 bool MaybeNewExtension(int number, const FieldDescriptor* descriptor,
1111 Extension** result);
1112
1113
1114
1115 Extension* MaybeNewRepeatedExtension(const FieldDescriptor* descriptor);
1116
1117
1118
1119 Extension& FindOrCreate(int number, FieldType type, bool repeated,
1120 bool packed, const FieldDescriptor* descriptor,
1121 Extension& (*pointer_creator)(Extension& ext,
1122 Arena* arena));
1123
1124 template <typename T>
1125 static Extension& CreateImpl(Extension& ext, Arena* arena) {
1126 ext.Mutable<T>() = Arena::Create<T>(arena);
1127 return ext;
1128 }
1129
1130 bool FindExtension(int wire_type, uint32_t field, const MessageLite* extendee,
1131 const internal::ParseContext* ,
1132 ExtensionInfo* extension, bool* was_packed_on_wire) {
1133 GeneratedExtensionFinder finder(extendee);
1134 return FindExtensionInfoFromFieldNumber(wire_type, field, &finder,
1135 extension, was_packed_on_wire);
1136 }
1137 inline bool FindExtension(int wire_type, uint32_t field,
1138 const Message* extendee,
1139 const internal::ParseContext* ctx,
1140 ExtensionInfo* extension, bool* was_packed_on_wire);
1141
1142 const char* ParseFieldMaybeLazily(uint64_t tag, const char* ptr,
1143 const MessageLite* extendee,
1144 internal::InternalMetadata* metadata,
1145 internal::ParseContext* ctx) {
1146
1147 return ParseField(tag, ptr, extendee, metadata, ctx);
1148 }
1149 const char* ParseFieldMaybeLazily(uint64_t tag, const char* ptr,
1150 const Message* extendee,
1151 internal::InternalMetadata* metadata,
1152 internal::ParseContext* ctx);
1153 const char* ParseMessageSetItem(const char* ptr, const MessageLite* extendee,
1154 internal::InternalMetadata* metadata,
1155 internal::ParseContext* ctx);
1156 const char* ParseMessageSetItem(const char* ptr, const Message* extendee,
1157 internal::InternalMetadata* metadata,
1158 internal::ParseContext* ctx);
1159
1160
1161 template <typename T>
1162 const char* ParseFieldWithExtensionInfo(int number, bool was_packed_on_wire,
1163 const ExtensionInfo& info,
1164 internal::InternalMetadata* metadata,
1165 const char* ptr,
1166 internal::ParseContext* ctx);
1167 template <typename Msg, typename T>
1168 const char* ParseMessageSetItemTmpl(const char* ptr, const Msg* extendee,
1169 internal::InternalMetadata* metadata,
1170 internal::ParseContext* ctx);
1171
1172
1173
1174
1175
1176
1177
1178
1179 static inline size_t RepeatedMessage_SpaceUsedExcludingSelfLong(
1180 RepeatedPtrFieldBase* field);
1181
1182 KeyValue* flat_begin() {
1183 assert(!is_large());
1184 return map_.flat;
1185 }
1186 const KeyValue* flat_begin() const {
1187 assert(!is_large());
1188 return map_.flat;
1189 }
1190 KeyValue* flat_end() {
1191 assert(!is_large());
1192 return map_.flat + flat_size_;
1193 }
1194 const KeyValue* flat_end() const {
1195 assert(!is_large());
1196 return map_.flat + flat_size_;
1197 }
1198
1199 static KeyValue* AllocateFlatMap(Arena* arena,
1200 uint16_t powerof2_flat_capacity);
1201 static void DeleteFlatMap(const KeyValue* flat, uint16_t flat_capacity);
1202
1203 Arena* arena_;
1204
1205
1206
1207
1208 uint16_t flat_capacity_;
1209 uint16_t flat_size_;
1210 union AllocatedData {
1211 KeyValue* flat;
1212
1213
1214
1215 LargeMap* large;
1216 } map_;
1217 };
1218
1219 constexpr ExtensionSet::ExtensionSet(Arena* arena)
1220 : arena_(arena), flat_capacity_(0), flat_size_(0), map_{nullptr} {}
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283 template <typename Type>
1284 class PrimitiveTypeTraits {
1285 public:
1286 typedef Type ConstType;
1287 typedef Type MutableType;
1288 using InitType = ConstType;
1289 static const ConstType& FromInitType(const InitType& v) { return v; }
1290 typedef PrimitiveTypeTraits<Type> Singular;
1291 static constexpr bool kLifetimeBound = false;
1292
1293 static inline ConstType Get(int number, const ExtensionSet& set,
1294 ConstType default_value) {
1295 return set.Get<Type>(number, default_value);
1296 }
1297
1298 static inline const ConstType* GetPtr(int number, const ExtensionSet& set,
1299 const ConstType& default_value) {
1300 return &set.Get<Type>(number, default_value);
1301 }
1302 static inline void Set(int number, FieldType field_type, ConstType value,
1303 ExtensionSet* set) {
1304 set->Set<Type>(number, field_type, value, nullptr);
1305 }
1306 };
1307
1308 template <typename Type>
1309 class RepeatedPrimitiveTypeTraits {
1310 public:
1311 typedef Type ConstType;
1312 typedef Type MutableType;
1313 using InitType = ConstType;
1314 static const ConstType& FromInitType(const InitType& v) { return v; }
1315 typedef RepeatedPrimitiveTypeTraits<Type> Repeated;
1316 static constexpr bool kLifetimeBound = false;
1317
1318 typedef RepeatedField<Type> RepeatedFieldType;
1319
1320 static inline Type Get(int number, const ExtensionSet& set, int index) {
1321 return set.GetRepeated<Type>(number, index);
1322 }
1323 static inline const Type* GetPtr(int number, const ExtensionSet& set,
1324 int index) {
1325 return &set.GetRepeated<Type>(number, index);
1326 }
1327 static inline const RepeatedField<ConstType>* GetRepeatedPtr(
1328 int number, const ExtensionSet& set);
1329 static inline void Set(int number, int index, Type value, ExtensionSet* set) {
1330 set->SetRepeated<Type>(number, index, value);
1331 }
1332 static inline void Add(int number, FieldType field_type, bool is_packed,
1333 Type value, ExtensionSet* set) {
1334 set->Add<Type>(number, field_type, is_packed, value, nullptr);
1335 }
1336
1337 static inline const RepeatedField<ConstType>& GetRepeated(
1338 int number, const ExtensionSet& set);
1339 static inline RepeatedField<Type>* MutableRepeated(int number,
1340 FieldType field_type,
1341 bool is_packed,
1342 ExtensionSet* set);
1343
1344 static const RepeatedFieldType* GetDefaultRepeatedField();
1345 };
1346
1347 class PROTOBUF_EXPORT RepeatedPrimitiveDefaults {
1348 private:
1349 template <typename Type>
1350 friend class RepeatedPrimitiveTypeTraits;
1351 static const RepeatedPrimitiveDefaults* default_instance();
1352 RepeatedField<int32_t> default_repeated_field_int32_t_;
1353 RepeatedField<int64_t> default_repeated_field_int64_t_;
1354 RepeatedField<uint32_t> default_repeated_field_uint32_t_;
1355 RepeatedField<uint64_t> default_repeated_field_uint64_t_;
1356 RepeatedField<double> default_repeated_field_double_;
1357 RepeatedField<float> default_repeated_field_float_;
1358 RepeatedField<bool> default_repeated_field_bool_;
1359 };
1360
1361 #define PROTOBUF_DEFINE_PRIMITIVE_TYPE(TYPE, METHOD) \
1362 template <> \
1363 inline const RepeatedField<TYPE>* \
1364 RepeatedPrimitiveTypeTraits<TYPE>::GetDefaultRepeatedField() { \
1365 return &RepeatedPrimitiveDefaults::default_instance() \
1366 ->default_repeated_field_##TYPE##_; \
1367 } \
1368 template <> \
1369 inline const RepeatedField<TYPE>& \
1370 RepeatedPrimitiveTypeTraits<TYPE>::GetRepeated(int number, \
1371 const ExtensionSet& set) { \
1372 return *reinterpret_cast<const RepeatedField<TYPE>*>( \
1373 set.GetRawRepeatedField(number, GetDefaultRepeatedField())); \
1374 } \
1375 template <> \
1376 inline const RepeatedField<TYPE>* \
1377 RepeatedPrimitiveTypeTraits<TYPE>::GetRepeatedPtr(int number, \
1378 const ExtensionSet& set) { \
1379 return &GetRepeated(number, set); \
1380 } \
1381 template <> \
1382 inline RepeatedField<TYPE>* \
1383 RepeatedPrimitiveTypeTraits<TYPE>::MutableRepeated( \
1384 int number, FieldType field_type, bool is_packed, ExtensionSet* set) { \
1385 return reinterpret_cast<RepeatedField<TYPE>*>( \
1386 set->MutableRawRepeatedField(number, field_type, is_packed, nullptr)); \
1387 }
1388
1389 PROTOBUF_DEFINE_PRIMITIVE_TYPE(int32_t, Int32)
1390 PROTOBUF_DEFINE_PRIMITIVE_TYPE(int64_t, Int64)
1391 PROTOBUF_DEFINE_PRIMITIVE_TYPE(uint32_t, UInt32)
1392 PROTOBUF_DEFINE_PRIMITIVE_TYPE(uint64_t, UInt64)
1393 PROTOBUF_DEFINE_PRIMITIVE_TYPE(float, Float)
1394 PROTOBUF_DEFINE_PRIMITIVE_TYPE(double, Double)
1395 PROTOBUF_DEFINE_PRIMITIVE_TYPE(bool, Bool)
1396
1397 #undef PROTOBUF_DEFINE_PRIMITIVE_TYPE
1398
1399
1400
1401
1402
1403 class PROTOBUF_EXPORT StringTypeTraits {
1404 public:
1405 typedef const std::string& ConstType;
1406 typedef std::string* MutableType;
1407 using InitType = ConstType;
1408 static ConstType FromInitType(InitType v) { return v; }
1409 typedef StringTypeTraits Singular;
1410 static constexpr bool kLifetimeBound = true;
1411
1412 static inline const std::string& Get(int number, const ExtensionSet& set,
1413 ConstType default_value) {
1414 return set.Get<std::string>(number, default_value);
1415 }
1416 static inline const std::string* GetPtr(int number, const ExtensionSet& set,
1417 ConstType default_value) {
1418 return &Get(number, set, default_value);
1419 }
1420 static inline void Set(int number, FieldType field_type,
1421 const std::string& value, ExtensionSet* set) {
1422 set->Set<std::string>(number, field_type, value, nullptr);
1423 }
1424 static inline std::string* Mutable(int number, FieldType field_type,
1425 ExtensionSet* set) {
1426 return set->MutableString(number, field_type, nullptr);
1427 }
1428 };
1429
1430 class PROTOBUF_EXPORT RepeatedStringTypeTraits {
1431 public:
1432 typedef const std::string& ConstType;
1433 typedef std::string* MutableType;
1434 using InitType = ConstType;
1435 static ConstType FromInitType(InitType v) { return v; }
1436 typedef RepeatedStringTypeTraits Repeated;
1437 static constexpr bool kLifetimeBound = true;
1438
1439 typedef RepeatedPtrField<std::string> RepeatedFieldType;
1440
1441 static inline const std::string& Get(int number, const ExtensionSet& set,
1442 int index) {
1443 return set.GetRepeated<std::string>(number, index);
1444 }
1445 static inline const std::string* GetPtr(int number, const ExtensionSet& set,
1446 int index) {
1447 return &Get(number, set, index);
1448 }
1449 static inline const RepeatedPtrField<std::string>* GetRepeatedPtr(
1450 int number, const ExtensionSet& set) {
1451 return &GetRepeated(number, set);
1452 }
1453 static inline void Set(int number, int index, const std::string& value,
1454 ExtensionSet* set) {
1455 set->SetRepeated<std::string>(number, index, value);
1456 }
1457 static inline std::string* Mutable(int number, int index, ExtensionSet* set) {
1458 return set->MutableRepeatedString(number, index);
1459 }
1460 static inline void Add(int number, FieldType field_type, bool ,
1461 const std::string& value, ExtensionSet* set) {
1462 set->Add<std::string>(number, field_type, nullptr) = value;
1463 }
1464 static inline std::string* Add(int number, FieldType field_type,
1465 ExtensionSet* set) {
1466 return &set->Add<std::string>(number, field_type, nullptr);
1467 }
1468
1469 static inline const RepeatedPtrField<std::string>& GetRepeated(
1470 int number, const ExtensionSet& set) {
1471 return *reinterpret_cast<const RepeatedPtrField<std::string>*>(
1472 set.GetRawRepeatedField(number, GetDefaultRepeatedField()));
1473 }
1474
1475 static inline RepeatedPtrField<std::string>* MutableRepeated(
1476 int number, FieldType field_type, bool is_packed, ExtensionSet* set) {
1477 return reinterpret_cast<RepeatedPtrField<std::string>*>(
1478 set->MutableRawRepeatedField(number, field_type, is_packed, nullptr));
1479 }
1480
1481 static const RepeatedFieldType* GetDefaultRepeatedField();
1482
1483 private:
1484 static void InitializeDefaultRepeatedFields();
1485 static void DestroyDefaultRepeatedFields();
1486 };
1487
1488
1489
1490
1491
1492
1493 template <typename Type>
1494 class EnumTypeTraits {
1495 public:
1496 typedef Type ConstType;
1497 typedef Type MutableType;
1498 using InitType = ConstType;
1499 static const ConstType& FromInitType(const InitType& v) { return v; }
1500 typedef EnumTypeTraits<Type> Singular;
1501 static constexpr bool kLifetimeBound = false;
1502
1503 static inline ConstType Get(int number, const ExtensionSet& set,
1504 ConstType default_value) {
1505 return static_cast<Type>(set.Get<int>(number, default_value));
1506 }
1507 static inline const ConstType* GetPtr(int number, const ExtensionSet& set,
1508 const ConstType& default_value) {
1509 return reinterpret_cast<const Type*>(&set.Get<int>(number, default_value));
1510 }
1511 static inline void Set(int number, FieldType field_type, ConstType value,
1512 ExtensionSet* set) {
1513 ABSL_DCHECK(
1514 internal::ValidateEnum(value, EnumTraits<Type>::validation_data()));
1515 set->Set<int>(number, field_type, value, nullptr);
1516 }
1517 };
1518
1519 template <typename Type>
1520 class RepeatedEnumTypeTraits {
1521 public:
1522 typedef Type ConstType;
1523 typedef Type MutableType;
1524 using InitType = ConstType;
1525 static const ConstType& FromInitType(const InitType& v) { return v; }
1526 typedef RepeatedEnumTypeTraits<Type> Repeated;
1527 static constexpr bool kLifetimeBound = false;
1528
1529 typedef RepeatedField<Type> RepeatedFieldType;
1530
1531 static inline ConstType Get(int number, const ExtensionSet& set, int index) {
1532 return static_cast<Type>(set.GetRepeated<int>(number, index));
1533 }
1534 static inline const ConstType* GetPtr(int number, const ExtensionSet& set,
1535 int index) {
1536 return reinterpret_cast<const Type*>(&set.GetRepeated<int>(number, index));
1537 }
1538 static inline void Set(int number, int index, ConstType value,
1539 ExtensionSet* set) {
1540 ABSL_DCHECK(
1541 internal::ValidateEnum(value, EnumTraits<Type>::validation_data()));
1542 set->SetRepeated<int>(number, index, value);
1543 }
1544 static inline void Add(int number, FieldType field_type, bool is_packed,
1545 ConstType value, ExtensionSet* set) {
1546 ABSL_DCHECK(
1547 internal::ValidateEnum(value, EnumTraits<Type>::validation_data()));
1548 set->Add<int>(number, field_type, is_packed, value, nullptr);
1549 }
1550 static inline const RepeatedField<Type>& GetRepeated(
1551 int number, const ExtensionSet& set) {
1552
1553
1554
1555
1556 return *reinterpret_cast<const RepeatedField<Type>*>(
1557 set.GetRawRepeatedField(number, GetDefaultRepeatedField()));
1558 }
1559 static inline const RepeatedField<Type>* GetRepeatedPtr(
1560 int number, const ExtensionSet& set) {
1561 return &GetRepeated(number, set);
1562 }
1563 static inline RepeatedField<Type>* MutableRepeated(int number,
1564 FieldType field_type,
1565 bool is_packed,
1566 ExtensionSet* set) {
1567 return reinterpret_cast<RepeatedField<Type>*>(
1568 set->MutableRawRepeatedField(number, field_type, is_packed, nullptr));
1569 }
1570
1571 static const RepeatedFieldType* GetDefaultRepeatedField() {
1572
1573
1574
1575
1576
1577
1578 return reinterpret_cast<const RepeatedField<Type>*>(
1579 RepeatedPrimitiveTypeTraits<int32_t>::GetDefaultRepeatedField());
1580 }
1581 };
1582
1583
1584
1585
1586
1587
1588
1589 template <typename Type>
1590 class MessageTypeTraits {
1591 public:
1592 typedef const Type& ConstType;
1593 typedef Type* MutableType;
1594 using InitType = const void*;
1595 static ConstType FromInitType(InitType v) {
1596 return *static_cast<const Type*>(v);
1597 }
1598 typedef MessageTypeTraits<Type> Singular;
1599 static constexpr bool kLifetimeBound = true;
1600
1601 static inline ConstType Get(int number, const ExtensionSet& set,
1602 ConstType default_value) {
1603 return static_cast<const Type&>(set.GetMessage(number, default_value));
1604 }
1605 static inline std::nullptr_t GetPtr(int ,
1606 const ExtensionSet& ,
1607 ConstType ) {
1608
1609 return nullptr;
1610 }
1611 static inline MutableType Mutable(int number, FieldType field_type,
1612 ExtensionSet* set) {
1613 return static_cast<Type*>(set->MutableMessage(
1614 number, field_type, Type::default_instance(), nullptr));
1615 }
1616 static inline void SetAllocated(int number, FieldType field_type,
1617 MutableType message, ExtensionSet* set) {
1618 set->SetAllocatedMessage(number, field_type, nullptr, message);
1619 }
1620 static inline void UnsafeArenaSetAllocated(int number, FieldType field_type,
1621 MutableType message,
1622 ExtensionSet* set) {
1623 set->UnsafeArenaSetAllocatedMessage(number, field_type, nullptr, message);
1624 }
1625 [[nodiscard]] static inline MutableType Release(int number,
1626 FieldType ,
1627 ExtensionSet* set) {
1628 return static_cast<Type*>(
1629 set->ReleaseMessage(number, Type::default_instance()));
1630 }
1631 static inline MutableType UnsafeArenaRelease(int number,
1632 FieldType ,
1633 ExtensionSet* set) {
1634 return static_cast<Type*>(
1635 set->UnsafeArenaReleaseMessage(number, Type::default_instance()));
1636 }
1637 };
1638
1639
1640 LazyEagerVerifyFnType FindExtensionLazyEagerVerifyFn(
1641 const MessageLite* extendee, int number);
1642
1643
1644 class RepeatedMessageGenericTypeTraits;
1645
1646 template <typename Type>
1647 class RepeatedMessageTypeTraits {
1648 public:
1649 typedef const Type& ConstType;
1650 typedef Type* MutableType;
1651 using InitType = const void*;
1652 static ConstType FromInitType(InitType v) {
1653 return *static_cast<const Type*>(v);
1654 }
1655 typedef RepeatedMessageTypeTraits<Type> Repeated;
1656 static constexpr bool kLifetimeBound = true;
1657
1658 typedef RepeatedPtrField<Type> RepeatedFieldType;
1659
1660 static inline ConstType Get(int number, const ExtensionSet& set, int index) {
1661 return static_cast<const Type&>(set.GetRepeatedMessage(number, index));
1662 }
1663 static inline std::nullptr_t GetPtr(int ,
1664 const ExtensionSet& ,
1665 int ) {
1666
1667 return nullptr;
1668 }
1669 static inline std::nullptr_t GetRepeatedPtr(int ,
1670 const ExtensionSet& ) {
1671
1672 return nullptr;
1673 }
1674 static inline MutableType Mutable(int number, int index, ExtensionSet* set) {
1675 return static_cast<Type*>(set->MutableRepeatedMessage(number, index));
1676 }
1677 static inline MutableType Add(int number, FieldType field_type,
1678 ExtensionSet* set) {
1679 static const ClassData* class_data = MessageTraits<Type>::class_data();
1680 return static_cast<Type*>(
1681 set->AddMessage(number, field_type, class_data, nullptr));
1682 }
1683 static inline const RepeatedPtrField<Type>& GetRepeated(
1684 int number, const ExtensionSet& set) {
1685
1686
1687
1688
1689
1690 return *reinterpret_cast<const RepeatedPtrField<Type>*>(
1691 set.GetRawRepeatedField(number, GetDefaultRepeatedField()));
1692 }
1693 static inline RepeatedPtrField<Type>* MutableRepeated(int number,
1694 FieldType field_type,
1695 bool is_packed,
1696 ExtensionSet* set) {
1697 return reinterpret_cast<RepeatedPtrField<Type>*>(
1698 set->MutableRawRepeatedField(number, field_type, is_packed, nullptr));
1699 }
1700
1701 static const RepeatedFieldType* GetDefaultRepeatedField();
1702 };
1703
1704 template <typename Type>
1705 inline const typename RepeatedMessageTypeTraits<Type>::RepeatedFieldType*
1706 RepeatedMessageTypeTraits<Type>::GetDefaultRepeatedField() {
1707 static auto instance = OnShutdownDelete(new RepeatedFieldType);
1708 return instance;
1709 }
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730 template <typename ExtendeeType, typename TypeTraitsType, FieldType field_type,
1731 bool is_packed>
1732 class ExtensionIdentifier {
1733 public:
1734 typedef TypeTraitsType TypeTraits;
1735 typedef ExtendeeType Extendee;
1736
1737 constexpr ExtensionIdentifier(int number,
1738 typename TypeTraits::InitType default_value)
1739 : number_(number), default_value_(default_value) {}
1740
1741 inline int number() const { return number_; }
1742 typename TypeTraits::ConstType default_value() const {
1743 return TypeTraits::FromInitType(default_value_);
1744 }
1745
1746 typename TypeTraits::ConstType const& default_value_ref() const {
1747 return TypeTraits::FromInitType(default_value_);
1748 }
1749
1750 private:
1751 const int number_;
1752 typename TypeTraits::InitType default_value_;
1753 };
1754
1755
1756
1757
1758
1759 }
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783 template <typename ExtendeeType, typename TypeTraitsType,
1784 internal::FieldType field_type, bool is_packed>
1785 void LinkExtensionReflection(
1786 const google::protobuf::internal::ExtensionIdentifier<
1787 ExtendeeType, TypeTraitsType, field_type, is_packed>& extension) {
1788 internal::StrongReference(extension);
1789 }
1790
1791
1792
1793 template <typename ExtendeeType, typename TypeTraitsType,
1794 internal::FieldType field_type, bool is_packed,
1795 typename PoolType = DescriptorPool>
1796 const FieldDescriptor* GetExtensionReflection(
1797 const google::protobuf::internal::ExtensionIdentifier<
1798 ExtendeeType, TypeTraitsType, field_type, is_packed>& extension) {
1799 return PoolType::generated_pool()->FindExtensionByNumber(
1800 google::protobuf::internal::ExtensionIdentifier<ExtendeeType, TypeTraitsType,
1801 field_type,
1802 is_packed>::Extendee::descriptor(),
1803 extension.number());
1804 }
1805
1806 }
1807 }
1808
1809 #include "google/protobuf/port_undef.inc"
1810
1811 #endif