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