| 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 |
| 49 namespace { | 55 namespace { |
| 50 | 56 |
| 51 inline WireFormatLite::FieldType real_type(FieldType type) { | 57 inline WireFormatLite::FieldType real_type(FieldType type) { |
| 52 GOOGLE_DCHECK(type > 0 && type <= WireFormatLite::MAX_FIELD_TYPE); | 58 GOOGLE_DCHECK(type > 0 && type <= WireFormatLite::MAX_FIELD_TYPE); |
| 53 return static_cast<WireFormatLite::FieldType>(type); | 59 return static_cast<WireFormatLite::FieldType>(type); |
| 54 } | 60 } |
| 55 | 61 |
| 56 inline WireFormatLite::CppType cpp_type(FieldType type) { | 62 inline WireFormatLite::CppType cpp_type(FieldType type) { |
| 57 return WireFormatLite::FieldTypeToCppType(real_type(type)); | 63 return WireFormatLite::FieldTypeToCppType(real_type(type)); |
| 58 } | 64 } |
| 59 | 65 |
| 60 inline bool is_packable(WireFormatLite::WireType type) { | 66 inline bool is_packable(WireFormatLite::WireType type) { |
| 61 switch (type) { | 67 switch (type) { |
| 62 case WireFormatLite::WIRETYPE_VARINT: | 68 case WireFormatLite::WIRETYPE_VARINT: |
| 63 case WireFormatLite::WIRETYPE_FIXED64: | 69 case WireFormatLite::WIRETYPE_FIXED64: |
| 64 case WireFormatLite::WIRETYPE_FIXED32: | 70 case WireFormatLite::WIRETYPE_FIXED32: |
| 65 return true; | 71 return true; |
| 66 case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: | 72 case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: |
| 67 case WireFormatLite::WIRETYPE_START_GROUP: | 73 case WireFormatLite::WIRETYPE_START_GROUP: |
| 68 case WireFormatLite::WIRETYPE_END_GROUP: | 74 case WireFormatLite::WIRETYPE_END_GROUP: |
| 69 return false; | 75 return false; |
| 70 | 76 |
| 71 // Do not add a default statement. Let the compiler complain when someone | 77 // Do not add a default statement. Let the compiler complain when someone |
| 72 // adds a new wire type. | 78 // adds a new wire type. |
| 73 } | 79 } |
| 74 GOOGLE_LOG(FATAL) << "can't reach here."; | 80 GOOGLE_LOG(FATAL) << "can't reach here."; |
| 75 return false; | 81 return false; |
| 76 } | 82 } |
| 77 | 83 |
| 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 registry_; | 85 delete cr_registry_; |
| 86 registry_ = NULL; | 86 cr_registry_ = NULL; |
| 87 } | 87 } |
| 88 | 88 |
| 89 void InitRegistry() { | 89 void InitRegistry() { |
| 90 registry_ = new ExtensionRegistry; | 90 cr_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(®istry_init_, &InitRegistry); | 98 ::google::protobuf::GoogleOnceInit(&cr_registry_init_, &InitRegistry); |
| 99 | 99 |
| 100 if (!InsertIfNotPresent(registry_, std::make_pair(containing_type, number), | 100 if (!InsertIfNotPresent(cr_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 (registry_ == NULL) | 110 return (cr_registry_ == NULL) |
| 111 ? NULL | 111 ? NULL |
| 112 : FindOrNull(*registry_, std::make_pair(containing_type, number)); | 112 : FindOrNull(*cr_registry_, |
| 113 std::make_pair(containing_type, number)); |
| 113 } | 114 } |
| 114 | 115 |
| 115 } // namespace | 116 } // namespace |
| 116 | 117 |
| 117 ExtensionFinder::~ExtensionFinder() {} | 118 ExtensionFinder::~ExtensionFinder() {} |
| 118 | 119 |
| 119 bool GeneratedExtensionFinder::Find(int number, ExtensionInfo* output) { | 120 bool GeneratedExtensionFinder::Find(int number, ExtensionInfo* output) { |
| 120 const ExtensionInfo* extension = | 121 const ExtensionInfo* extension = |
| 121 FindRegisteredExtension(containing_type_, number); | 122 FindRegisteredExtension(containing_type_, number); |
| 122 if (extension == NULL) { | 123 if (extension == NULL) { |
| (...skipping 1618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1741 } | 1742 } |
| 1742 } | 1743 } |
| 1743 } | 1744 } |
| 1744 | 1745 |
| 1745 // Defined in extension_set_heavy.cc. | 1746 // Defined in extension_set_heavy.cc. |
| 1746 // int ExtensionSet::Extension::SpaceUsedExcludingSelf() const | 1747 // int ExtensionSet::Extension::SpaceUsedExcludingSelf() const |
| 1747 | 1748 |
| 1748 // ================================================================== | 1749 // ================================================================== |
| 1749 // Default repeated field instances for iterator-compatible accessors | 1750 // Default repeated field instances for iterator-compatible accessors |
| 1750 | 1751 |
| 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 | |
| 1755 void RepeatedPrimitiveGenericTypeTraits::InitializeDefaultRepeatedFields() { | 1752 void RepeatedPrimitiveGenericTypeTraits::InitializeDefaultRepeatedFields() { |
| 1756 default_repeated_field_int32_ = new RepeatedField<int32>; | 1753 cr_default_repeated_field_int32_ = new RepeatedField<int32>; |
| 1757 default_repeated_field_int64_ = new RepeatedField<int64>; | 1754 cr_default_repeated_field_int64_ = new RepeatedField<int64>; |
| 1758 default_repeated_field_uint32_ = new RepeatedField<uint32>; | 1755 cr_default_repeated_field_uint32_ = new RepeatedField<uint32>; |
| 1759 default_repeated_field_uint64_ = new RepeatedField<uint64>; | 1756 cr_default_repeated_field_uint64_ = new RepeatedField<uint64>; |
| 1760 default_repeated_field_double_ = new RepeatedField<double>; | 1757 cr_default_repeated_field_double_ = new RepeatedField<double>; |
| 1761 default_repeated_field_float_ = new RepeatedField<float>; | 1758 cr_default_repeated_field_float_ = new RepeatedField<float>; |
| 1762 default_repeated_field_bool_ = new RepeatedField<bool>; | 1759 cr_default_repeated_field_bool_ = new RepeatedField<bool>; |
| 1763 OnShutdown(&DestroyDefaultRepeatedFields); | 1760 OnShutdown(&DestroyDefaultRepeatedFields); |
| 1764 } | 1761 } |
| 1765 | 1762 |
| 1766 void RepeatedPrimitiveGenericTypeTraits::DestroyDefaultRepeatedFields() { | 1763 void RepeatedPrimitiveGenericTypeTraits::DestroyDefaultRepeatedFields() { |
| 1767 delete default_repeated_field_int32_; | 1764 delete cr_default_repeated_field_int32_; |
| 1768 delete default_repeated_field_int64_; | 1765 delete cr_default_repeated_field_int64_; |
| 1769 delete default_repeated_field_uint32_; | 1766 delete cr_default_repeated_field_uint32_; |
| 1770 delete default_repeated_field_uint64_; | 1767 delete cr_default_repeated_field_uint64_; |
| 1771 delete default_repeated_field_double_; | 1768 delete cr_default_repeated_field_double_; |
| 1772 delete default_repeated_field_float_; | 1769 delete cr_default_repeated_field_float_; |
| 1773 delete default_repeated_field_bool_; | 1770 delete cr_default_repeated_field_bool_; |
| 1774 } | 1771 } |
| 1775 | 1772 |
| 1776 void RepeatedStringTypeTraits::InitializeDefaultRepeatedFields() { | 1773 void RepeatedStringTypeTraits::InitializeDefaultRepeatedFields() { |
| 1777 default_repeated_field_ = new RepeatedFieldType; | 1774 cr_default_repeated_field_ = new RepeatedFieldType; |
| 1778 OnShutdown(&DestroyDefaultRepeatedFields); | 1775 OnShutdown(&DestroyDefaultRepeatedFields); |
| 1779 } | 1776 } |
| 1780 | 1777 |
| 1781 void RepeatedStringTypeTraits::DestroyDefaultRepeatedFields() { | 1778 void RepeatedStringTypeTraits::DestroyDefaultRepeatedFields() { |
| 1782 delete default_repeated_field_; | 1779 delete cr_default_repeated_field_; |
| 1783 } | 1780 } |
| 1784 | 1781 |
| 1785 void RepeatedMessageGenericTypeTraits::InitializeDefaultRepeatedFields() { | 1782 void RepeatedMessageGenericTypeTraits::InitializeDefaultRepeatedFields() { |
| 1786 default_repeated_field_ = new RepeatedFieldType; | 1783 cr_default_repeated_field_ = new RepeatedFieldType; |
| 1787 OnShutdown(&DestroyDefaultRepeatedFields); | 1784 OnShutdown(&DestroyDefaultRepeatedFields); |
| 1788 } | 1785 } |
| 1789 | 1786 |
| 1790 void RepeatedMessageGenericTypeTraits::DestroyDefaultRepeatedFields() { | 1787 void RepeatedMessageGenericTypeTraits::DestroyDefaultRepeatedFields() { |
| 1791 delete default_repeated_field_; | 1788 delete cr_default_repeated_field_; |
| 1792 } | 1789 } |
| 1793 | 1790 |
| 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 | |
| 1813 } // namespace internal | 1791 } // namespace internal |
| 1814 } // namespace protobuf | 1792 } // namespace protobuf |
| 1815 } // namespace google | 1793 } // namespace google |
| OLD | NEW |