File indexing completed on 2025-01-31 10:12:22
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef GOOGLE_PROTOBUF_MAP_TYPE_HANDLER_H__
0009 #define GOOGLE_PROTOBUF_MAP_TYPE_HANDLER_H__
0010
0011 #include <cstdint>
0012 #include <type_traits>
0013
0014 #include "google/protobuf/arena.h"
0015 #include "google/protobuf/arenastring.h"
0016 #include "google/protobuf/io/coded_stream.h"
0017 #include "google/protobuf/parse_context.h"
0018 #include "google/protobuf/wire_format_lite.h"
0019
0020 #ifdef SWIG
0021 #error "You cannot SWIG proto headers"
0022 #endif
0023
0024 namespace google {
0025 namespace protobuf {
0026 namespace internal {
0027
0028
0029 template <WireFormatLite::FieldType field_type, typename Type>
0030 class MapWireFieldTypeTraits {};
0031
0032 #define TYPE_TRAITS(FieldType, CType, WireFormatType) \
0033 template <typename Type> \
0034 class MapWireFieldTypeTraits<WireFormatLite::TYPE_##FieldType, Type> { \
0035 public: \
0036 using TypeOnMemory = \
0037 std::conditional_t<WireFormatLite::TYPE_##FieldType == \
0038 WireFormatLite::TYPE_MESSAGE, \
0039 Type*, CType>; \
0040 using MapEntryAccessorType = \
0041 std::conditional_t<std::is_enum<Type>::value, int, Type>; \
0042 static const WireFormatLite::WireType kWireType = \
0043 WireFormatLite::WIRETYPE_##WireFormatType; \
0044 };
0045
0046 TYPE_TRAITS(MESSAGE, Type, LENGTH_DELIMITED)
0047 TYPE_TRAITS(STRING, ArenaStringPtr, LENGTH_DELIMITED)
0048 TYPE_TRAITS(BYTES, ArenaStringPtr, LENGTH_DELIMITED)
0049 TYPE_TRAITS(INT64, int64_t, VARINT)
0050 TYPE_TRAITS(UINT64, uint64_t, VARINT)
0051 TYPE_TRAITS(INT32, int32_t, VARINT)
0052 TYPE_TRAITS(UINT32, uint32_t, VARINT)
0053 TYPE_TRAITS(SINT64, int64_t, VARINT)
0054 TYPE_TRAITS(SINT32, int32_t, VARINT)
0055 TYPE_TRAITS(ENUM, int, VARINT)
0056 TYPE_TRAITS(DOUBLE, double, FIXED64)
0057 TYPE_TRAITS(FLOAT, float, FIXED32)
0058 TYPE_TRAITS(FIXED64, uint64_t, FIXED64)
0059 TYPE_TRAITS(FIXED32, uint32_t, FIXED32)
0060 TYPE_TRAITS(SFIXED64, int64_t, FIXED64)
0061 TYPE_TRAITS(SFIXED32, int32_t, FIXED32)
0062 TYPE_TRAITS(BOOL, bool, VARINT)
0063
0064 #undef TYPE_TRAITS
0065
0066 template <WireFormatLite::FieldType field_type, typename Type>
0067 class MapTypeHandler;
0068
0069 template <typename Type>
0070 class MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type> {
0071 public:
0072
0073
0074 typedef typename MapWireFieldTypeTraits<WireFormatLite::TYPE_MESSAGE,
0075 Type>::MapEntryAccessorType
0076 MapEntryAccessorType;
0077
0078 typedef typename MapWireFieldTypeTraits<WireFormatLite::TYPE_MESSAGE,
0079 Type>::TypeOnMemory TypeOnMemory;
0080
0081 static constexpr WireFormatLite::WireType kWireType =
0082 MapWireFieldTypeTraits<WireFormatLite::TYPE_MESSAGE, Type>::kWireType;
0083
0084
0085 static inline size_t ByteSize(const MapEntryAccessorType& value);
0086 static inline int GetCachedSize(const MapEntryAccessorType& value);
0087
0088 static inline uint8_t* Write(int field, const MapEntryAccessorType& value,
0089 uint8_t* ptr, io::EpsCopyOutputStream* stream);
0090
0091
0092 static inline void DeleteNoArena(const Type* x);
0093 static constexpr TypeOnMemory Constinit();
0094 };
0095
0096 #define MAP_HANDLER(FieldType) \
0097 template <typename Type> \
0098 class MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type> { \
0099 public: \
0100 typedef typename MapWireFieldTypeTraits<WireFormatLite::TYPE_##FieldType, \
0101 Type>::MapEntryAccessorType \
0102 MapEntryAccessorType; \
0103 typedef typename MapWireFieldTypeTraits<WireFormatLite::TYPE_##FieldType, \
0104 Type>::TypeOnMemory TypeOnMemory; \
0105 static const WireFormatLite::WireType kWireType = \
0106 MapWireFieldTypeTraits<WireFormatLite::TYPE_##FieldType, \
0107 Type>::kWireType; \
0108 static inline int ByteSize(const MapEntryAccessorType& value); \
0109 static inline int GetCachedSize(const MapEntryAccessorType& value); \
0110 static inline uint8_t* Write(int field, const MapEntryAccessorType& value, \
0111 uint8_t* ptr, \
0112 io::EpsCopyOutputStream* stream); \
0113 static inline void DeleteNoArena(const TypeOnMemory& x); \
0114 static void DeleteNoArena(TypeOnMemory& value); \
0115 static constexpr TypeOnMemory Constinit(); \
0116 };
0117 MAP_HANDLER(STRING)
0118 MAP_HANDLER(BYTES)
0119 MAP_HANDLER(INT64)
0120 MAP_HANDLER(UINT64)
0121 MAP_HANDLER(INT32)
0122 MAP_HANDLER(UINT32)
0123 MAP_HANDLER(SINT64)
0124 MAP_HANDLER(SINT32)
0125 MAP_HANDLER(ENUM)
0126 MAP_HANDLER(DOUBLE)
0127 MAP_HANDLER(FLOAT)
0128 MAP_HANDLER(FIXED64)
0129 MAP_HANDLER(FIXED32)
0130 MAP_HANDLER(SFIXED64)
0131 MAP_HANDLER(SFIXED32)
0132 MAP_HANDLER(BOOL)
0133 #undef MAP_HANDLER
0134
0135 template <typename Type>
0136 inline size_t MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::ByteSize(
0137 const MapEntryAccessorType& value) {
0138 return WireFormatLite::MessageSizeNoVirtual(value);
0139 }
0140
0141 #define GOOGLE_PROTOBUF_BYTE_SIZE(FieldType, DeclaredType) \
0142 template <typename Type> \
0143 inline int MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::ByteSize( \
0144 const MapEntryAccessorType& value) { \
0145 return static_cast<int>(WireFormatLite::DeclaredType##Size(value)); \
0146 }
0147
0148 GOOGLE_PROTOBUF_BYTE_SIZE(STRING, String)
0149 GOOGLE_PROTOBUF_BYTE_SIZE(BYTES, Bytes)
0150 GOOGLE_PROTOBUF_BYTE_SIZE(INT64, Int64)
0151 GOOGLE_PROTOBUF_BYTE_SIZE(UINT64, UInt64)
0152 GOOGLE_PROTOBUF_BYTE_SIZE(INT32, Int32)
0153 GOOGLE_PROTOBUF_BYTE_SIZE(UINT32, UInt32)
0154 GOOGLE_PROTOBUF_BYTE_SIZE(SINT64, SInt64)
0155 GOOGLE_PROTOBUF_BYTE_SIZE(SINT32, SInt32)
0156 GOOGLE_PROTOBUF_BYTE_SIZE(ENUM, Enum)
0157
0158 #undef GOOGLE_PROTOBUF_BYTE_SIZE
0159
0160 #define FIXED_BYTE_SIZE(FieldType, DeclaredType) \
0161 template <typename Type> \
0162 inline int MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::ByteSize( \
0163 const MapEntryAccessorType& ) { \
0164 return WireFormatLite::k##DeclaredType##Size; \
0165 }
0166
0167 FIXED_BYTE_SIZE(DOUBLE, Double)
0168 FIXED_BYTE_SIZE(FLOAT, Float)
0169 FIXED_BYTE_SIZE(FIXED64, Fixed64)
0170 FIXED_BYTE_SIZE(FIXED32, Fixed32)
0171 FIXED_BYTE_SIZE(SFIXED64, SFixed64)
0172 FIXED_BYTE_SIZE(SFIXED32, SFixed32)
0173 FIXED_BYTE_SIZE(BOOL, Bool)
0174
0175 #undef FIXED_BYTE_SIZE
0176
0177 template <typename Type>
0178 inline int MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::GetCachedSize(
0179 const MapEntryAccessorType& value) {
0180 return static_cast<int>(WireFormatLite::LengthDelimitedSize(
0181 static_cast<size_t>(value.GetCachedSize())));
0182 }
0183
0184 #define GET_CACHED_SIZE(FieldType, DeclaredType) \
0185 template <typename Type> \
0186 inline int \
0187 MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::GetCachedSize( \
0188 const MapEntryAccessorType& value) { \
0189 return static_cast<int>(WireFormatLite::DeclaredType##Size(value)); \
0190 }
0191
0192 GET_CACHED_SIZE(STRING, String)
0193 GET_CACHED_SIZE(BYTES, Bytes)
0194 GET_CACHED_SIZE(INT64, Int64)
0195 GET_CACHED_SIZE(UINT64, UInt64)
0196 GET_CACHED_SIZE(INT32, Int32)
0197 GET_CACHED_SIZE(UINT32, UInt32)
0198 GET_CACHED_SIZE(SINT64, SInt64)
0199 GET_CACHED_SIZE(SINT32, SInt32)
0200 GET_CACHED_SIZE(ENUM, Enum)
0201
0202 #undef GET_CACHED_SIZE
0203
0204 #define GET_FIXED_CACHED_SIZE(FieldType, DeclaredType) \
0205 template <typename Type> \
0206 inline int \
0207 MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::GetCachedSize( \
0208 const MapEntryAccessorType& ) { \
0209 return WireFormatLite::k##DeclaredType##Size; \
0210 }
0211
0212 GET_FIXED_CACHED_SIZE(DOUBLE, Double)
0213 GET_FIXED_CACHED_SIZE(FLOAT, Float)
0214 GET_FIXED_CACHED_SIZE(FIXED64, Fixed64)
0215 GET_FIXED_CACHED_SIZE(FIXED32, Fixed32)
0216 GET_FIXED_CACHED_SIZE(SFIXED64, SFixed64)
0217 GET_FIXED_CACHED_SIZE(SFIXED32, SFixed32)
0218 GET_FIXED_CACHED_SIZE(BOOL, Bool)
0219
0220 #undef GET_FIXED_CACHED_SIZE
0221
0222 template <typename Type>
0223 inline uint8_t* MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::Write(
0224 int field, const MapEntryAccessorType& value, uint8_t* ptr,
0225 io::EpsCopyOutputStream* stream) {
0226 ptr = stream->EnsureSpace(ptr);
0227 return WireFormatLite::InternalWriteMessage(
0228 field, value, value.GetCachedSize(), ptr, stream);
0229 }
0230
0231 #define WRITE_METHOD(FieldType, DeclaredType) \
0232 template <typename Type> \
0233 inline uint8_t* \
0234 MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::Write( \
0235 int field, const MapEntryAccessorType& value, uint8_t* ptr, \
0236 io::EpsCopyOutputStream* stream) { \
0237 ptr = stream->EnsureSpace(ptr); \
0238 return stream->Write##DeclaredType(field, value, ptr); \
0239 }
0240
0241 WRITE_METHOD(STRING, String)
0242 WRITE_METHOD(BYTES, Bytes)
0243
0244 #undef WRITE_METHOD
0245 #define WRITE_METHOD(FieldType, DeclaredType) \
0246 template <typename Type> \
0247 inline uint8_t* \
0248 MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::Write( \
0249 int field, const MapEntryAccessorType& value, uint8_t* ptr, \
0250 io::EpsCopyOutputStream* stream) { \
0251 ptr = stream->EnsureSpace(ptr); \
0252 return WireFormatLite::Write##DeclaredType##ToArray(field, value, ptr); \
0253 }
0254
0255 WRITE_METHOD(INT64, Int64)
0256 WRITE_METHOD(UINT64, UInt64)
0257 WRITE_METHOD(INT32, Int32)
0258 WRITE_METHOD(UINT32, UInt32)
0259 WRITE_METHOD(SINT64, SInt64)
0260 WRITE_METHOD(SINT32, SInt32)
0261 WRITE_METHOD(ENUM, Enum)
0262 WRITE_METHOD(DOUBLE, Double)
0263 WRITE_METHOD(FLOAT, Float)
0264 WRITE_METHOD(FIXED64, Fixed64)
0265 WRITE_METHOD(FIXED32, Fixed32)
0266 WRITE_METHOD(SFIXED64, SFixed64)
0267 WRITE_METHOD(SFIXED32, SFixed32)
0268 WRITE_METHOD(BOOL, Bool)
0269
0270 #undef WRITE_METHOD
0271
0272
0273
0274 template <typename Type>
0275 void MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::DeleteNoArena(
0276 const Type* ptr) {
0277 delete ptr;
0278 }
0279
0280 template <typename Type>
0281 constexpr auto MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::Constinit()
0282 -> TypeOnMemory {
0283 return nullptr;
0284 }
0285
0286
0287
0288 #define STRING_OR_BYTES_HANDLER_FUNCTIONS(FieldType) \
0289 template <typename Type> \
0290 void MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::DeleteNoArena( \
0291 TypeOnMemory& value) { \
0292 value.Destroy(); \
0293 } \
0294 template <typename Type> \
0295 constexpr auto \
0296 MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::Constinit() \
0297 -> TypeOnMemory { \
0298 return TypeOnMemory(&internal::fixed_address_empty_string, \
0299 ConstantInitialized{}); \
0300 }
0301 STRING_OR_BYTES_HANDLER_FUNCTIONS(STRING)
0302 STRING_OR_BYTES_HANDLER_FUNCTIONS(BYTES)
0303 #undef STRING_OR_BYTES_HANDLER_FUNCTIONS
0304
0305 #define PRIMITIVE_HANDLER_FUNCTIONS(FieldType) \
0306 template <typename Type> \
0307 inline void MapTypeHandler<WireFormatLite::TYPE_##FieldType, \
0308 Type>::DeleteNoArena(TypeOnMemory& ) {} \
0309 template <typename Type> \
0310 constexpr auto \
0311 MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::Constinit() \
0312 ->TypeOnMemory { \
0313 return 0; \
0314 }
0315 PRIMITIVE_HANDLER_FUNCTIONS(INT64)
0316 PRIMITIVE_HANDLER_FUNCTIONS(UINT64)
0317 PRIMITIVE_HANDLER_FUNCTIONS(INT32)
0318 PRIMITIVE_HANDLER_FUNCTIONS(UINT32)
0319 PRIMITIVE_HANDLER_FUNCTIONS(SINT64)
0320 PRIMITIVE_HANDLER_FUNCTIONS(SINT32)
0321 PRIMITIVE_HANDLER_FUNCTIONS(ENUM)
0322 PRIMITIVE_HANDLER_FUNCTIONS(DOUBLE)
0323 PRIMITIVE_HANDLER_FUNCTIONS(FLOAT)
0324 PRIMITIVE_HANDLER_FUNCTIONS(FIXED64)
0325 PRIMITIVE_HANDLER_FUNCTIONS(FIXED32)
0326 PRIMITIVE_HANDLER_FUNCTIONS(SFIXED64)
0327 PRIMITIVE_HANDLER_FUNCTIONS(SFIXED32)
0328 PRIMITIVE_HANDLER_FUNCTIONS(BOOL)
0329 #undef PRIMITIVE_HANDLER_FUNCTIONS
0330
0331
0332
0333
0334
0335 template <typename Key, typename Value, WireFormatLite::FieldType kKeyFieldType,
0336 WireFormatLite::FieldType kValueFieldType>
0337 struct MapEntryFuncs {
0338 typedef MapTypeHandler<kKeyFieldType, Key> KeyTypeHandler;
0339 typedef MapTypeHandler<kValueFieldType, Value> ValueTypeHandler;
0340 enum : int {
0341 kKeyFieldNumber = 1,
0342 kValueFieldNumber = 2
0343 };
0344
0345 static uint8_t* InternalSerialize(int field_number, const Key& key,
0346 const Value& value, uint8_t* ptr,
0347 io::EpsCopyOutputStream* stream) {
0348 ptr = stream->EnsureSpace(ptr);
0349 ptr = WireFormatLite::WriteTagToArray(
0350 field_number, WireFormatLite::WIRETYPE_LENGTH_DELIMITED, ptr);
0351 ptr = io::CodedOutputStream::WriteVarint32ToArray(GetCachedSize(key, value),
0352 ptr);
0353
0354 ptr = KeyTypeHandler::Write(kKeyFieldNumber, key, ptr, stream);
0355 return ValueTypeHandler::Write(kValueFieldNumber, value, ptr, stream);
0356 }
0357
0358 static size_t ByteSizeLong(const Key& key, const Value& value) {
0359
0360 size_t inner_length =
0361 2 + KeyTypeHandler::ByteSize(key) + ValueTypeHandler::ByteSize(value);
0362 return inner_length + io::CodedOutputStream::VarintSize32(
0363 static_cast<uint32_t>(inner_length));
0364 }
0365
0366 static int GetCachedSize(const Key& key, const Value& value) {
0367
0368 return 2 + KeyTypeHandler::GetCachedSize(key) +
0369 ValueTypeHandler::GetCachedSize(value);
0370 }
0371 };
0372
0373 }
0374 }
0375 }
0376
0377 #endif