OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 MessageLite* message); | 266 MessageLite* message); |
267 void UnsafeArenaSetAllocatedMessage(int number, FieldType type, | 267 void UnsafeArenaSetAllocatedMessage(int number, FieldType type, |
268 const FieldDescriptor* descriptor, | 268 const FieldDescriptor* descriptor, |
269 MessageLite* message); | 269 MessageLite* message); |
270 MessageLite* ReleaseMessage(int number, const MessageLite& prototype); | 270 MessageLite* ReleaseMessage(int number, const MessageLite& prototype); |
271 MessageLite* UnsafeArenaReleaseMessage( | 271 MessageLite* UnsafeArenaReleaseMessage( |
272 int number, const MessageLite& prototype); | 272 int number, const MessageLite& prototype); |
273 | 273 |
274 MessageLite* ReleaseMessage(const FieldDescriptor* descriptor, | 274 MessageLite* ReleaseMessage(const FieldDescriptor* descriptor, |
275 MessageFactory* factory); | 275 MessageFactory* factory); |
| 276 MessageLite* UnsafeArenaReleaseMessage(const FieldDescriptor* descriptor, |
| 277 MessageFactory* factory); |
276 #undef desc | 278 #undef desc |
277 ::google::protobuf::Arena* GetArenaNoVirtual() const { return arena_; } | 279 ::google::protobuf::Arena* GetArenaNoVirtual() const { return arena_; } |
278 | 280 |
279 // repeated fields ------------------------------------------------- | 281 // repeated fields ------------------------------------------------- |
280 | 282 |
281 // Fetches a RepeatedField extension by number; returns |default_value| | 283 // Fetches a RepeatedField extension by number; returns |default_value| |
282 // if no such extension exists. User should not touch this directly; it is | 284 // if no such extension exists. User should not touch this directly; it is |
283 // used by the GetRepeatedExtension() method. | 285 // used by the GetRepeatedExtension() method. |
284 const void* GetRawRepeatedField(int number, const void* default_value) const; | 286 const void* GetRawRepeatedField(int number, const void* default_value) const; |
285 // Fetches a mutable version of a RepeatedField extension by number, | 287 // Fetches a mutable version of a RepeatedField extension by number, |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 // last called. Note that the range bounds are inclusive-exclusive. | 398 // last called. Note that the range bounds are inclusive-exclusive. |
397 void SerializeWithCachedSizes(int start_field_number, | 399 void SerializeWithCachedSizes(int start_field_number, |
398 int end_field_number, | 400 int end_field_number, |
399 io::CodedOutputStream* output) const; | 401 io::CodedOutputStream* output) const; |
400 | 402 |
401 // Same as SerializeWithCachedSizes, but without any bounds checking. | 403 // Same as SerializeWithCachedSizes, but without any bounds checking. |
402 // The caller must ensure that target has sufficient capacity for the | 404 // The caller must ensure that target has sufficient capacity for the |
403 // serialized extensions. | 405 // serialized extensions. |
404 // | 406 // |
405 // Returns a pointer past the last written byte. | 407 // Returns a pointer past the last written byte. |
| 408 uint8* InternalSerializeWithCachedSizesToArray(int start_field_number, |
| 409 int end_field_number, |
| 410 bool deterministic, |
| 411 uint8* target) const; |
| 412 |
| 413 // Like above but serializes in MessageSet format. |
| 414 void SerializeMessageSetWithCachedSizes(io::CodedOutputStream* output) const; |
| 415 uint8* InternalSerializeMessageSetWithCachedSizesToArray(bool deterministic, |
| 416 uint8* target) const; |
| 417 |
| 418 // For backward-compatibility, versions of two of the above methods that |
| 419 // are never forced to serialize deterministically. |
406 uint8* SerializeWithCachedSizesToArray(int start_field_number, | 420 uint8* SerializeWithCachedSizesToArray(int start_field_number, |
407 int end_field_number, | 421 int end_field_number, |
408 uint8* target) const; | 422 uint8* target) const; |
409 | |
410 // Like above but serializes in MessageSet format. | |
411 void SerializeMessageSetWithCachedSizes(io::CodedOutputStream* output) const; | |
412 uint8* SerializeMessageSetWithCachedSizesToArray(uint8* target) const; | 423 uint8* SerializeMessageSetWithCachedSizesToArray(uint8* target) const; |
413 | 424 |
414 // Returns the total serialized size of all the extensions. | 425 // Returns the total serialized size of all the extensions. |
415 int ByteSize() const; | 426 size_t ByteSize() const; |
416 | 427 |
417 // Like ByteSize() but uses MessageSet format. | 428 // Like ByteSize() but uses MessageSet format. |
418 int MessageSetByteSize() const; | 429 size_t MessageSetByteSize() const; |
419 | 430 |
420 // Returns (an estimate of) the total number of bytes used for storing the | 431 // Returns (an estimate of) the total number of bytes used for storing the |
421 // extensions in memory, excluding sizeof(*this). If the ExtensionSet is | 432 // extensions in memory, excluding sizeof(*this). If the ExtensionSet is |
422 // for a lite message (and thus possibly contains lite messages), the results | 433 // for a lite message (and thus possibly contains lite messages), the results |
423 // are undefined (might work, might crash, might corrupt data, might not even | 434 // are undefined (might work, might crash, might corrupt data, might not even |
424 // be linked in). It's up to the protocol compiler to avoid calling this on | 435 // be linked in). It's up to the protocol compiler to avoid calling this on |
425 // such ExtensionSets (easy enough since lite messages don't implement | 436 // such ExtensionSets (easy enough since lite messages don't implement |
426 // SpaceUsed()). | 437 // SpaceUsed()). |
427 int SpaceUsedExcludingSelf() const; | 438 int SpaceUsedExcludingSelf() const; |
428 | 439 |
(...skipping 20 matching lines...) Expand all Loading... |
449 virtual int SpaceUsed() const = 0; | 460 virtual int SpaceUsed() const = 0; |
450 | 461 |
451 virtual void MergeFrom(const LazyMessageExtension& other) = 0; | 462 virtual void MergeFrom(const LazyMessageExtension& other) = 0; |
452 virtual void Clear() = 0; | 463 virtual void Clear() = 0; |
453 | 464 |
454 virtual bool ReadMessage(const MessageLite& prototype, | 465 virtual bool ReadMessage(const MessageLite& prototype, |
455 io::CodedInputStream* input) = 0; | 466 io::CodedInputStream* input) = 0; |
456 virtual void WriteMessage(int number, | 467 virtual void WriteMessage(int number, |
457 io::CodedOutputStream* output) const = 0; | 468 io::CodedOutputStream* output) const = 0; |
458 virtual uint8* WriteMessageToArray(int number, uint8* target) const = 0; | 469 virtual uint8* WriteMessageToArray(int number, uint8* target) const = 0; |
| 470 virtual uint8* InternalWriteMessageToArray(int number, bool, |
| 471 uint8* target) const { |
| 472 // TODO(gpike): make this pure virtual. This is a placeholder because we |
| 473 // need to update third_party/upb, for example. |
| 474 return WriteMessageToArray(number, target); |
| 475 } |
| 476 |
459 private: | 477 private: |
460 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(LazyMessageExtension); | 478 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(LazyMessageExtension); |
461 }; | 479 }; |
462 struct Extension { | 480 struct Extension { |
463 // The order of these fields packs Extension into 24 bytes when using 8 | 481 // The order of these fields packs Extension into 24 bytes when using 8 |
464 // byte alignment. Consider this when adding or removing fields here. | 482 // byte alignment. Consider this when adding or removing fields here. |
465 union { | 483 union { |
466 int32 int32_value; | 484 int32 int32_value; |
467 int64 int64_value; | 485 int64 int64_value; |
468 uint32 uint32_value; | 486 uint32 uint32_value; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 | 533 |
516 // The descriptor for this extension, if one exists and is known. May be | 534 // The descriptor for this extension, if one exists and is known. May be |
517 // NULL. Must not be NULL if the descriptor for the extension does not | 535 // NULL. Must not be NULL if the descriptor for the extension does not |
518 // live in the same pool as the descriptor for the containing type. | 536 // live in the same pool as the descriptor for the containing type. |
519 const FieldDescriptor* descriptor; | 537 const FieldDescriptor* descriptor; |
520 | 538 |
521 // Some helper methods for operations on a single Extension. | 539 // Some helper methods for operations on a single Extension. |
522 void SerializeFieldWithCachedSizes( | 540 void SerializeFieldWithCachedSizes( |
523 int number, | 541 int number, |
524 io::CodedOutputStream* output) const; | 542 io::CodedOutputStream* output) const; |
525 uint8* SerializeFieldWithCachedSizesToArray( | 543 uint8* InternalSerializeFieldWithCachedSizesToArray( |
526 int number, | 544 int number, |
| 545 bool deterministic, |
527 uint8* target) const; | 546 uint8* target) const; |
528 void SerializeMessageSetItemWithCachedSizes( | 547 void SerializeMessageSetItemWithCachedSizes( |
529 int number, | 548 int number, |
530 io::CodedOutputStream* output) const; | 549 io::CodedOutputStream* output) const; |
531 uint8* SerializeMessageSetItemWithCachedSizesToArray( | 550 uint8* InternalSerializeMessageSetItemWithCachedSizesToArray( |
532 int number, | 551 int number, |
| 552 bool deterministic, |
533 uint8* target) const; | 553 uint8* target) const; |
534 int ByteSize(int number) const; | 554 size_t ByteSize(int number) const; |
535 int MessageSetItemByteSize(int number) const; | 555 size_t MessageSetItemByteSize(int number) const; |
536 void Clear(); | 556 void Clear(); |
537 int GetSize() const; | 557 int GetSize() const; |
538 void Free(); | 558 void Free(); |
539 int SpaceUsedExcludingSelf() const; | 559 int SpaceUsedExcludingSelf() const; |
540 }; | 560 }; |
| 561 typedef std::map<int, Extension> ExtensionMap; |
541 | 562 |
542 | 563 |
543 // Merges existing Extension from other_extension | 564 // Merges existing Extension from other_extension |
544 void InternalExtensionMergeFrom(int number, const Extension& other_extension); | 565 void InternalExtensionMergeFrom(int number, const Extension& other_extension); |
545 | 566 |
546 // Returns true and fills field_number and extension if extension is found. | 567 // Returns true and fills field_number and extension if extension is found. |
547 // Note to support packed repeated field compatibility, it also fills whether | 568 // Note to support packed repeated field compatibility, it also fills whether |
548 // the tag on wire is packed, which can be different from | 569 // the tag on wire is packed, which can be different from |
549 // extension->is_packed (whether packed=true is specified). | 570 // extension->is_packed (whether packed=true is specified). |
550 bool FindExtensionInfoFromTag(uint32 tag, ExtensionFinder* extension_finder, | 571 bool FindExtensionInfoFromTag(uint32 tag, ExtensionFinder* extension_finder, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
601 // Defined in extension_set_heavy.cc. | 622 // Defined in extension_set_heavy.cc. |
602 static inline int RepeatedMessage_SpaceUsedExcludingSelf( | 623 static inline int RepeatedMessage_SpaceUsedExcludingSelf( |
603 RepeatedPtrFieldBase* field); | 624 RepeatedPtrFieldBase* field); |
604 | 625 |
605 // The Extension struct is small enough to be passed by value, so we use it | 626 // The Extension struct is small enough to be passed by value, so we use it |
606 // directly as the value type in the map rather than use pointers. We use | 627 // directly as the value type in the map rather than use pointers. We use |
607 // a map rather than hash_map here because we expect most ExtensionSets will | 628 // a map rather than hash_map here because we expect most ExtensionSets will |
608 // only contain a small number of extensions whereas hash_map is optimized | 629 // only contain a small number of extensions whereas hash_map is optimized |
609 // for 100 elements or more. Also, we want AppendToList() to order fields | 630 // for 100 elements or more. Also, we want AppendToList() to order fields |
610 // by field number. | 631 // by field number. |
611 std::map<int, Extension> extensions_; | 632 ExtensionMap extensions_; |
612 ::google::protobuf::Arena* arena_; | 633 ::google::protobuf::Arena* arena_; |
613 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ExtensionSet); | 634 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ExtensionSet); |
614 }; | 635 }; |
615 | 636 |
616 // These are just for convenience... | 637 // These are just for convenience... |
617 inline void ExtensionSet::SetString(int number, FieldType type, | 638 inline void ExtensionSet::SetString(int number, FieldType type, |
618 const string& value, | 639 const string& value, |
619 const FieldDescriptor* descriptor) { | 640 const FieldDescriptor* descriptor) { |
620 MutableString(number, type, descriptor)->assign(value); | 641 MutableString(number, type, descriptor)->assign(value); |
621 } | 642 } |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
973 } | 994 } |
974 static inline MutableType Mutable(int number, FieldType field_type, | 995 static inline MutableType Mutable(int number, FieldType field_type, |
975 ExtensionSet* set) { | 996 ExtensionSet* set) { |
976 return static_cast<Type*>( | 997 return static_cast<Type*>( |
977 set->MutableMessage(number, field_type, Type::default_instance(), NULL)); | 998 set->MutableMessage(number, field_type, Type::default_instance(), NULL)); |
978 } | 999 } |
979 static inline void SetAllocated(int number, FieldType field_type, | 1000 static inline void SetAllocated(int number, FieldType field_type, |
980 MutableType message, ExtensionSet* set) { | 1001 MutableType message, ExtensionSet* set) { |
981 set->SetAllocatedMessage(number, field_type, NULL, message); | 1002 set->SetAllocatedMessage(number, field_type, NULL, message); |
982 } | 1003 } |
| 1004 static inline void UnsafeArenaSetAllocated(int number, FieldType field_type, |
| 1005 MutableType message, |
| 1006 ExtensionSet* set) { |
| 1007 set->UnsafeArenaSetAllocatedMessage(number, field_type, NULL, message); |
| 1008 } |
983 static inline MutableType Release(int number, FieldType /* field_type */, | 1009 static inline MutableType Release(int number, FieldType /* field_type */, |
984 ExtensionSet* set) { | 1010 ExtensionSet* set) { |
985 return static_cast<Type*>(set->ReleaseMessage( | 1011 return static_cast<Type*>(set->ReleaseMessage( |
986 number, Type::default_instance())); | 1012 number, Type::default_instance())); |
987 } | 1013 } |
| 1014 static inline MutableType UnsafeArenaRelease(int number, |
| 1015 FieldType /* field_type */, |
| 1016 ExtensionSet* set) { |
| 1017 return static_cast<Type*>(set->UnsafeArenaReleaseMessage( |
| 1018 number, Type::default_instance())); |
| 1019 } |
988 }; | 1020 }; |
989 | 1021 |
990 // forward declaration | 1022 // forward declaration |
991 class RepeatedMessageGenericTypeTraits; | 1023 class RepeatedMessageGenericTypeTraits; |
992 | 1024 |
993 template <typename Type> | 1025 template <typename Type> |
994 class RepeatedMessageTypeTraits { | 1026 class RepeatedMessageTypeTraits { |
995 public: | 1027 public: |
996 typedef const Type& ConstType; | 1028 typedef const Type& ConstType; |
997 typedef Type* MutableType; | 1029 typedef Type* MutableType; |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1171 inline void SetAllocatedExtension( \ | 1203 inline void SetAllocatedExtension( \ |
1172 const ::google::protobuf::internal::ExtensionIdentifier<
\ | 1204 const ::google::protobuf::internal::ExtensionIdentifier<
\ |
1173 CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ | 1205 CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ |
1174 typename _proto_TypeTraits::Singular::MutableType value) { \ | 1206 typename _proto_TypeTraits::Singular::MutableType value) { \ |
1175 _proto_TypeTraits::SetAllocated(id.number(), _field_type, \ | 1207 _proto_TypeTraits::SetAllocated(id.number(), _field_type, \ |
1176 value, &_extensions_); \ | 1208 value, &_extensions_); \ |
1177 } \ | 1209 } \ |
1178 template <typename _proto_TypeTraits, \ | 1210 template <typename _proto_TypeTraits, \ |
1179 ::google::protobuf::internal::FieldType _field_type,
\ | 1211 ::google::protobuf::internal::FieldType _field_type,
\ |
1180 bool _is_packed> \ | 1212 bool _is_packed> \ |
| 1213 inline void UnsafeArenaSetAllocatedExtension( \ |
| 1214 const ::google::protobuf::internal::ExtensionIdentifier<
\ |
| 1215 CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ |
| 1216 typename _proto_TypeTraits::Singular::MutableType value) { \ |
| 1217 _proto_TypeTraits::UnsafeArenaSetAllocated(id.number(), _field_type, \ |
| 1218 value, &_extensions_); \ |
| 1219 } \ |
| 1220 template <typename _proto_TypeTraits, \ |
| 1221 ::google::protobuf::internal::FieldType _field_type,
\ |
| 1222 bool _is_packed> \ |
1181 inline typename _proto_TypeTraits::Singular::MutableType ReleaseExtension( \ | 1223 inline typename _proto_TypeTraits::Singular::MutableType ReleaseExtension( \ |
1182 const ::google::protobuf::internal::ExtensionIdentifier<
\ | 1224 const ::google::protobuf::internal::ExtensionIdentifier<
\ |
1183 CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ | 1225 CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ |
1184 return _proto_TypeTraits::Release(id.number(), _field_type, \ | 1226 return _proto_TypeTraits::Release(id.number(), _field_type, \ |
1185 &_extensions_); \ | 1227 &_extensions_); \ |
1186 } \ | 1228 } \ |
| 1229 template <typename _proto_TypeTraits, \ |
| 1230 ::google::protobuf::internal::FieldType _field_type,
\ |
| 1231 bool _is_packed> \ |
| 1232 inline typename _proto_TypeTraits::Singular::MutableType \ |
| 1233 UnsafeArenaReleaseExtension( \ |
| 1234 const ::google::protobuf::internal::ExtensionIdentifier<
\ |
| 1235 CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ |
| 1236 return _proto_TypeTraits::UnsafeArenaRelease(id.number(), _field_type, \ |
| 1237 &_extensions_); \ |
| 1238 } \ |
1187 \ | 1239 \ |
1188 /* Repeated accessors */ \ | 1240 /* Repeated accessors */ \ |
1189 template <typename _proto_TypeTraits, \ | 1241 template <typename _proto_TypeTraits, \ |
1190 ::google::protobuf::internal::FieldType _field_type,
\ | 1242 ::google::protobuf::internal::FieldType _field_type,
\ |
1191 bool _is_packed> \ | 1243 bool _is_packed> \ |
1192 inline typename _proto_TypeTraits::Repeated::ConstType GetExtension( \ | 1244 inline typename _proto_TypeTraits::Repeated::ConstType GetExtension( \ |
1193 const ::google::protobuf::internal::ExtensionIdentifier<
\ | 1245 const ::google::protobuf::internal::ExtensionIdentifier<
\ |
1194 CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ | 1246 CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ |
1195 int index) const { \ | 1247 int index) const { \ |
1196 return _proto_TypeTraits::Get(id.number(), _extensions_, index); \ | 1248 return _proto_TypeTraits::Get(id.number(), _extensions_, index); \ |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1257 _is_packed>& id) { \ | 1309 _is_packed>& id) { \ |
1258 return _proto_TypeTraits::MutableRepeated(id.number(), _field_type, \ | 1310 return _proto_TypeTraits::MutableRepeated(id.number(), _field_type, \ |
1259 _is_packed, &_extensions_); \ | 1311 _is_packed, &_extensions_); \ |
1260 } | 1312 } |
1261 | 1313 |
1262 } // namespace internal | 1314 } // namespace internal |
1263 } // namespace protobuf | 1315 } // namespace protobuf |
1264 | 1316 |
1265 } // namespace google | 1317 } // namespace google |
1266 #endif // GOOGLE_PROTOBUF_EXTENSION_SET_H__ | 1318 #endif // GOOGLE_PROTOBUF_EXTENSION_SET_H__ |
OLD | NEW |