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