| 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 28 matching lines...) Expand all Loading... |
| 39 #include <google/protobuf/message_lite.h> | 39 #include <google/protobuf/message_lite.h> |
| 40 #include <google/protobuf/io/coded_stream.h> | 40 #include <google/protobuf/io/coded_stream.h> |
| 41 #include <google/protobuf/wire_format_lite_inl.h> | 41 #include <google/protobuf/wire_format_lite_inl.h> |
| 42 #include <google/protobuf/repeated_field.h> | 42 #include <google/protobuf/repeated_field.h> |
| 43 #include <google/protobuf/stubs/map_util.h> | 43 #include <google/protobuf/stubs/map_util.h> |
| 44 | 44 |
| 45 namespace google { | 45 namespace google { |
| 46 namespace protobuf { | 46 namespace protobuf { |
| 47 namespace internal { | 47 namespace internal { |
| 48 | 48 |
| 49 // Registry stuff. | |
| 50 typedef hash_map<pair<const MessageLite*, int>, ExtensionInfo> | |
| 51 ExtensionRegistry; | |
| 52 extern ExtensionRegistry* cr_registry_; | |
| 53 extern ProtobufOnceType cr_registry_init_; | |
| 54 | |
| 55 namespace { | 49 namespace { |
| 56 | 50 |
| 57 inline WireFormatLite::FieldType real_type(FieldType type) { | 51 inline WireFormatLite::FieldType real_type(FieldType type) { |
| 58 GOOGLE_DCHECK(type > 0 && type <= WireFormatLite::MAX_FIELD_TYPE); | 52 GOOGLE_DCHECK(type > 0 && type <= WireFormatLite::MAX_FIELD_TYPE); |
| 59 return static_cast<WireFormatLite::FieldType>(type); | 53 return static_cast<WireFormatLite::FieldType>(type); |
| 60 } | 54 } |
| 61 | 55 |
| 62 inline WireFormatLite::CppType cpp_type(FieldType type) { | 56 inline WireFormatLite::CppType cpp_type(FieldType type) { |
| 63 return WireFormatLite::FieldTypeToCppType(real_type(type)); | 57 return WireFormatLite::FieldTypeToCppType(real_type(type)); |
| 64 } | 58 } |
| 65 | 59 |
| 66 inline bool is_packable(WireFormatLite::WireType type) { | 60 inline bool is_packable(WireFormatLite::WireType type) { |
| 67 switch (type) { | 61 switch (type) { |
| 68 case WireFormatLite::WIRETYPE_VARINT: | 62 case WireFormatLite::WIRETYPE_VARINT: |
| 69 case WireFormatLite::WIRETYPE_FIXED64: | 63 case WireFormatLite::WIRETYPE_FIXED64: |
| 70 case WireFormatLite::WIRETYPE_FIXED32: | 64 case WireFormatLite::WIRETYPE_FIXED32: |
| 71 return true; | 65 return true; |
| 72 case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: | 66 case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: |
| 73 case WireFormatLite::WIRETYPE_START_GROUP: | 67 case WireFormatLite::WIRETYPE_START_GROUP: |
| 74 case WireFormatLite::WIRETYPE_END_GROUP: | 68 case WireFormatLite::WIRETYPE_END_GROUP: |
| 75 return false; | 69 return false; |
| 76 | 70 |
| 77 // Do not add a default statement. Let the compiler complain when someone | 71 // Do not add a default statement. Let the compiler complain when someone |
| 78 // adds a new wire type. | 72 // adds a new wire type. |
| 79 } | 73 } |
| 80 GOOGLE_LOG(FATAL) << "can't reach here."; | 74 GOOGLE_LOG(FATAL) << "can't reach here."; |
| 81 return false; | 75 return false; |
| 82 } | 76 } |
| 83 | 77 |
| 78 // Registry stuff. |
| 79 typedef hash_map<pair<const MessageLite*, int>, |
| 80 ExtensionInfo> ExtensionRegistry; |
| 81 ExtensionRegistry* registry_ = NULL; |
| 82 GOOGLE_PROTOBUF_DECLARE_ONCE(registry_init_); |
| 83 |
| 84 void DeleteRegistry() { | 84 void DeleteRegistry() { |
| 85 delete cr_registry_; | 85 delete registry_; |
| 86 cr_registry_ = NULL; | 86 registry_ = NULL; |
| 87 } | 87 } |
| 88 | 88 |
| 89 void InitRegistry() { | 89 void InitRegistry() { |
| 90 cr_registry_ = new ExtensionRegistry; | 90 registry_ = new ExtensionRegistry; |
| 91 OnShutdown(&DeleteRegistry); | 91 OnShutdown(&DeleteRegistry); |
| 92 } | 92 } |
| 93 | 93 |
| 94 // This function is only called at startup, so there is no need for thread- | 94 // This function is only called at startup, so there is no need for thread- |
| 95 // safety. | 95 // safety. |
| 96 void Register(const MessageLite* containing_type, | 96 void Register(const MessageLite* containing_type, |
| 97 int number, ExtensionInfo info) { | 97 int number, ExtensionInfo info) { |
| 98 ::google::protobuf::GoogleOnceInit(&cr_registry_init_, &InitRegistry); | 98 ::google::protobuf::GoogleOnceInit(®istry_init_, &InitRegistry); |
| 99 | 99 |
| 100 if (!InsertIfNotPresent(cr_registry_, std::make_pair(containing_type, number), | 100 if (!InsertIfNotPresent(registry_, std::make_pair(containing_type, number), |
| 101 info)) { | 101 info)) { |
| 102 GOOGLE_LOG(FATAL) << "Multiple extension registrations for type \"" | 102 GOOGLE_LOG(FATAL) << "Multiple extension registrations for type \"" |
| 103 << containing_type->GetTypeName() | 103 << containing_type->GetTypeName() |
| 104 << "\", field number " << number << "."; | 104 << "\", field number " << number << "."; |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 const ExtensionInfo* FindRegisteredExtension( | 108 const ExtensionInfo* FindRegisteredExtension( |
| 109 const MessageLite* containing_type, int number) { | 109 const MessageLite* containing_type, int number) { |
| 110 return (cr_registry_ == NULL) | 110 return (registry_ == NULL) |
| 111 ? NULL | 111 ? NULL |
| 112 : FindOrNull(*cr_registry_, | 112 : FindOrNull(*registry_, std::make_pair(containing_type, number)); |
| 113 std::make_pair(containing_type, number)); | |
| 114 } | 113 } |
| 115 | 114 |
| 116 } // namespace | 115 } // namespace |
| 117 | 116 |
| 118 ExtensionFinder::~ExtensionFinder() {} | 117 ExtensionFinder::~ExtensionFinder() {} |
| 119 | 118 |
| 120 bool GeneratedExtensionFinder::Find(int number, ExtensionInfo* output) { | 119 bool GeneratedExtensionFinder::Find(int number, ExtensionInfo* output) { |
| 121 const ExtensionInfo* extension = | 120 const ExtensionInfo* extension = |
| 122 FindRegisteredExtension(containing_type_, number); | 121 FindRegisteredExtension(containing_type_, number); |
| 123 if (extension == NULL) { | 122 if (extension == NULL) { |
| (...skipping 1618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1742 } | 1741 } |
| 1743 } | 1742 } |
| 1744 } | 1743 } |
| 1745 | 1744 |
| 1746 // Defined in extension_set_heavy.cc. | 1745 // Defined in extension_set_heavy.cc. |
| 1747 // int ExtensionSet::Extension::SpaceUsedExcludingSelf() const | 1746 // int ExtensionSet::Extension::SpaceUsedExcludingSelf() const |
| 1748 | 1747 |
| 1749 // ================================================================== | 1748 // ================================================================== |
| 1750 // Default repeated field instances for iterator-compatible accessors | 1749 // Default repeated field instances for iterator-compatible accessors |
| 1751 | 1750 |
| 1751 GOOGLE_PROTOBUF_DECLARE_ONCE(repeated_primitive_generic_type_traits_once_init_); |
| 1752 GOOGLE_PROTOBUF_DECLARE_ONCE(repeated_string_type_traits_once_init_); |
| 1753 GOOGLE_PROTOBUF_DECLARE_ONCE(repeated_message_generic_type_traits_once_init_); |
| 1754 |
| 1752 void RepeatedPrimitiveGenericTypeTraits::InitializeDefaultRepeatedFields() { | 1755 void RepeatedPrimitiveGenericTypeTraits::InitializeDefaultRepeatedFields() { |
| 1753 cr_default_repeated_field_int32_ = new RepeatedField<int32>; | 1756 default_repeated_field_int32_ = new RepeatedField<int32>; |
| 1754 cr_default_repeated_field_int64_ = new RepeatedField<int64>; | 1757 default_repeated_field_int64_ = new RepeatedField<int64>; |
| 1755 cr_default_repeated_field_uint32_ = new RepeatedField<uint32>; | 1758 default_repeated_field_uint32_ = new RepeatedField<uint32>; |
| 1756 cr_default_repeated_field_uint64_ = new RepeatedField<uint64>; | 1759 default_repeated_field_uint64_ = new RepeatedField<uint64>; |
| 1757 cr_default_repeated_field_double_ = new RepeatedField<double>; | 1760 default_repeated_field_double_ = new RepeatedField<double>; |
| 1758 cr_default_repeated_field_float_ = new RepeatedField<float>; | 1761 default_repeated_field_float_ = new RepeatedField<float>; |
| 1759 cr_default_repeated_field_bool_ = new RepeatedField<bool>; | 1762 default_repeated_field_bool_ = new RepeatedField<bool>; |
| 1760 OnShutdown(&DestroyDefaultRepeatedFields); | 1763 OnShutdown(&DestroyDefaultRepeatedFields); |
| 1761 } | 1764 } |
| 1762 | 1765 |
| 1763 void RepeatedPrimitiveGenericTypeTraits::DestroyDefaultRepeatedFields() { | 1766 void RepeatedPrimitiveGenericTypeTraits::DestroyDefaultRepeatedFields() { |
| 1764 delete cr_default_repeated_field_int32_; | 1767 delete default_repeated_field_int32_; |
| 1765 delete cr_default_repeated_field_int64_; | 1768 delete default_repeated_field_int64_; |
| 1766 delete cr_default_repeated_field_uint32_; | 1769 delete default_repeated_field_uint32_; |
| 1767 delete cr_default_repeated_field_uint64_; | 1770 delete default_repeated_field_uint64_; |
| 1768 delete cr_default_repeated_field_double_; | 1771 delete default_repeated_field_double_; |
| 1769 delete cr_default_repeated_field_float_; | 1772 delete default_repeated_field_float_; |
| 1770 delete cr_default_repeated_field_bool_; | 1773 delete default_repeated_field_bool_; |
| 1771 } | 1774 } |
| 1772 | 1775 |
| 1773 void RepeatedStringTypeTraits::InitializeDefaultRepeatedFields() { | 1776 void RepeatedStringTypeTraits::InitializeDefaultRepeatedFields() { |
| 1774 cr_default_repeated_field_ = new RepeatedFieldType; | 1777 default_repeated_field_ = new RepeatedFieldType; |
| 1775 OnShutdown(&DestroyDefaultRepeatedFields); | 1778 OnShutdown(&DestroyDefaultRepeatedFields); |
| 1776 } | 1779 } |
| 1777 | 1780 |
| 1778 void RepeatedStringTypeTraits::DestroyDefaultRepeatedFields() { | 1781 void RepeatedStringTypeTraits::DestroyDefaultRepeatedFields() { |
| 1779 delete cr_default_repeated_field_; | 1782 delete default_repeated_field_; |
| 1780 } | 1783 } |
| 1781 | 1784 |
| 1782 void RepeatedMessageGenericTypeTraits::InitializeDefaultRepeatedFields() { | 1785 void RepeatedMessageGenericTypeTraits::InitializeDefaultRepeatedFields() { |
| 1783 cr_default_repeated_field_ = new RepeatedFieldType; | 1786 default_repeated_field_ = new RepeatedFieldType; |
| 1784 OnShutdown(&DestroyDefaultRepeatedFields); | 1787 OnShutdown(&DestroyDefaultRepeatedFields); |
| 1785 } | 1788 } |
| 1786 | 1789 |
| 1787 void RepeatedMessageGenericTypeTraits::DestroyDefaultRepeatedFields() { | 1790 void RepeatedMessageGenericTypeTraits::DestroyDefaultRepeatedFields() { |
| 1788 delete cr_default_repeated_field_; | 1791 delete default_repeated_field_; |
| 1789 } | 1792 } |
| 1790 | 1793 |
| 1794 const RepeatedField<int32>* |
| 1795 RepeatedPrimitiveGenericTypeTraits::default_repeated_field_int32_ = NULL; |
| 1796 const RepeatedField<int64>* |
| 1797 RepeatedPrimitiveGenericTypeTraits::default_repeated_field_int64_ = NULL; |
| 1798 const RepeatedField<uint32>* |
| 1799 RepeatedPrimitiveGenericTypeTraits::default_repeated_field_uint32_ = NULL; |
| 1800 const RepeatedField<uint64>* |
| 1801 RepeatedPrimitiveGenericTypeTraits::default_repeated_field_uint64_ = NULL; |
| 1802 const RepeatedField<double>* |
| 1803 RepeatedPrimitiveGenericTypeTraits::default_repeated_field_double_ = NULL; |
| 1804 const RepeatedField<float>* |
| 1805 RepeatedPrimitiveGenericTypeTraits::default_repeated_field_float_ = NULL; |
| 1806 const RepeatedField<bool>* |
| 1807 RepeatedPrimitiveGenericTypeTraits::default_repeated_field_bool_ = NULL; |
| 1808 const RepeatedStringTypeTraits::RepeatedFieldType* |
| 1809 RepeatedStringTypeTraits::default_repeated_field_ = NULL; |
| 1810 const RepeatedMessageGenericTypeTraits::RepeatedFieldType* |
| 1811 RepeatedMessageGenericTypeTraits::default_repeated_field_ = NULL; |
| 1812 |
| 1791 } // namespace internal | 1813 } // namespace internal |
| 1792 } // namespace protobuf | 1814 } // namespace protobuf |
| 1793 } // namespace google | 1815 } // namespace google |
| OLD | NEW |