File indexing completed on 2025-01-31 10:12:23
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef GOOGLE_PROTOBUF_METADATA_LITE_H__
0009 #define GOOGLE_PROTOBUF_METADATA_LITE_H__
0010
0011 #include <string>
0012
0013 #include "google/protobuf/arena.h"
0014 #include "google/protobuf/port.h"
0015
0016
0017 #include "google/protobuf/port_def.inc"
0018
0019 #ifdef SWIG
0020 #error "You cannot SWIG proto headers"
0021 #endif
0022
0023 namespace google {
0024 namespace protobuf {
0025
0026 class UnknownFieldSet;
0027
0028 namespace internal {
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043 class PROTOBUF_EXPORT InternalMetadata {
0044 public:
0045 constexpr InternalMetadata() : ptr_(0) {}
0046 explicit InternalMetadata(Arena* arena) {
0047 ptr_ = reinterpret_cast<intptr_t>(arena);
0048 }
0049
0050
0051
0052
0053
0054
0055
0056
0057 template <typename T>
0058 void Delete() {
0059
0060 if (have_unknown_fields()) {
0061 DeleteOutOfLineHelper<T>();
0062 }
0063 }
0064
0065 PROTOBUF_NDEBUG_INLINE Arena* arena() const {
0066 if (PROTOBUF_PREDICT_FALSE(have_unknown_fields())) {
0067 return PtrValue<ContainerBase>()->arena;
0068 } else {
0069 return PtrValue<Arena>();
0070 }
0071 }
0072
0073 PROTOBUF_NDEBUG_INLINE bool have_unknown_fields() const {
0074 return HasUnknownFieldsTag();
0075 }
0076
0077 PROTOBUF_NDEBUG_INLINE void* raw_arena_ptr() const {
0078 return reinterpret_cast<void*>(ptr_);
0079 }
0080
0081 template <typename T>
0082 PROTOBUF_NDEBUG_INLINE const T& unknown_fields(
0083 const T& (*default_instance)()) const {
0084 if (PROTOBUF_PREDICT_FALSE(have_unknown_fields())) {
0085 return PtrValue<Container<T>>()->unknown_fields;
0086 } else {
0087 return default_instance();
0088 }
0089 }
0090
0091 template <typename T>
0092 PROTOBUF_NDEBUG_INLINE T* mutable_unknown_fields() {
0093 if (PROTOBUF_PREDICT_TRUE(have_unknown_fields())) {
0094 return &PtrValue<Container<T>>()->unknown_fields;
0095 } else {
0096 return mutable_unknown_fields_slow<T>();
0097 }
0098 }
0099
0100 template <typename T>
0101 PROTOBUF_NDEBUG_INLINE void Swap(InternalMetadata* other) {
0102
0103
0104
0105
0106
0107
0108 if (have_unknown_fields() || other->have_unknown_fields()) {
0109 DoSwap<T>(other->mutable_unknown_fields<T>());
0110 }
0111 }
0112
0113 PROTOBUF_NDEBUG_INLINE void InternalSwap(
0114 InternalMetadata* PROTOBUF_RESTRICT other) {
0115 std::swap(ptr_, other->ptr_);
0116 }
0117
0118 template <typename T>
0119 PROTOBUF_NDEBUG_INLINE void MergeFrom(const InternalMetadata& other) {
0120 if (other.have_unknown_fields()) {
0121 DoMergeFrom<T>(other.unknown_fields<T>(nullptr));
0122 }
0123 }
0124
0125 template <typename T>
0126 PROTOBUF_NDEBUG_INLINE void Clear() {
0127 if (have_unknown_fields()) {
0128 DoClear<T>();
0129 }
0130 }
0131
0132 private:
0133 intptr_t ptr_;
0134
0135
0136 static constexpr intptr_t kUnknownFieldsTagMask = 1;
0137 static constexpr intptr_t kPtrTagMask = kUnknownFieldsTagMask;
0138 static constexpr intptr_t kPtrValueMask = ~kPtrTagMask;
0139
0140
0141 PROTOBUF_ALWAYS_INLINE bool HasUnknownFieldsTag() const {
0142 return ptr_ & kUnknownFieldsTagMask;
0143 }
0144
0145 template <typename U>
0146 U* PtrValue() const {
0147 return reinterpret_cast<U*>(ptr_ & kPtrValueMask);
0148 }
0149
0150
0151 struct ContainerBase {
0152 Arena* arena;
0153 };
0154
0155 template <typename T>
0156 struct Container : public ContainerBase {
0157 T unknown_fields;
0158 };
0159
0160 template <typename T>
0161 PROTOBUF_NOINLINE void DeleteOutOfLineHelper() {
0162 delete PtrValue<Container<T>>();
0163
0164
0165 ptr_ = 0;
0166 }
0167
0168 template <typename T>
0169 PROTOBUF_NOINLINE T* mutable_unknown_fields_slow() {
0170 Arena* my_arena = arena();
0171 Container<T>* container = Arena::Create<Container<T>>(my_arena);
0172
0173
0174 ptr_ = reinterpret_cast<intptr_t>(container);
0175 ptr_ |= kUnknownFieldsTagMask;
0176 container->arena = my_arena;
0177 return &(container->unknown_fields);
0178 }
0179
0180
0181
0182 template <typename T>
0183 PROTOBUF_NOINLINE void DoClear() {
0184 mutable_unknown_fields<T>()->Clear();
0185 }
0186
0187 template <typename T>
0188 PROTOBUF_NOINLINE void DoMergeFrom(const T& other) {
0189 mutable_unknown_fields<T>()->MergeFrom(other);
0190 }
0191
0192 template <typename T>
0193 PROTOBUF_NOINLINE void DoSwap(T* other) {
0194 mutable_unknown_fields<T>()->Swap(other);
0195 }
0196
0197
0198 void CheckedDestruct();
0199 };
0200
0201
0202
0203 template <>
0204 PROTOBUF_EXPORT void InternalMetadata::DoClear<std::string>();
0205 template <>
0206 PROTOBUF_EXPORT void InternalMetadata::DoMergeFrom<std::string>(
0207 const std::string& other);
0208 template <>
0209 PROTOBUF_EXPORT void InternalMetadata::DoSwap<std::string>(std::string* other);
0210
0211
0212
0213 extern template PROTOBUF_EXPORT void
0214 InternalMetadata::DoClear<UnknownFieldSet>();
0215 extern template PROTOBUF_EXPORT void
0216 InternalMetadata::DoMergeFrom<UnknownFieldSet>(const UnknownFieldSet& other);
0217 extern template PROTOBUF_EXPORT void
0218 InternalMetadata::DoSwap<UnknownFieldSet>(UnknownFieldSet* other);
0219 extern template PROTOBUF_EXPORT void
0220 InternalMetadata::DeleteOutOfLineHelper<UnknownFieldSet>();
0221 extern template PROTOBUF_EXPORT UnknownFieldSet*
0222 InternalMetadata::mutable_unknown_fields_slow<UnknownFieldSet>();
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232 class PROTOBUF_EXPORT LiteUnknownFieldSetter {
0233 public:
0234 explicit LiteUnknownFieldSetter(InternalMetadata* metadata)
0235 : metadata_(metadata) {
0236 if (metadata->have_unknown_fields()) {
0237 buffer_.swap(*metadata->mutable_unknown_fields<std::string>());
0238 }
0239 }
0240 ~LiteUnknownFieldSetter() {
0241 if (!buffer_.empty())
0242 metadata_->mutable_unknown_fields<std::string>()->swap(buffer_);
0243 }
0244 std::string* buffer() { return &buffer_; }
0245
0246 private:
0247 InternalMetadata* metadata_;
0248 std::string buffer_;
0249 };
0250
0251 }
0252 }
0253 }
0254
0255 #include "google/protobuf/port_undef.inc"
0256
0257 #endif