hpb: impl GlobalExtensionRegistry

We introduce hpb::ExtensionRegistry::generated_registry. In the future, Parse calls will default to this being the default registry, while still being able to supply their own (if needed).

PiperOrigin-RevId: 697690765
pull/19250/head
Hong Shin 3 months ago committed by Copybara-Service
parent c2e5ad3b80
commit f72b1034b4
  1. 17
      csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs
  2. 13
      hpb/extension.h
  3. 7
      hpb/internal/message_lock_test.cc
  4. 15
      hpb_generator/tests/test_generated.cc

@ -1,17 +0,0 @@
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#endregion
namespace Google.Protobuf.Reflection;
internal sealed partial class FeatureSetDescriptor
{
// Canonical serialized form of the edition defaults, generated by embed_edition_defaults.
private const string DefaultsBase64 =
"ChMYhAciACoMCAEQAhgCIAMoATACChMY5wciACoMCAIQARgBIAIoATABChMY6AciDAgBEAEYASACKAEwASoAIOYHKOgH";
}

@ -141,10 +141,23 @@ class ExtensionRegistry {
}
}
static const ExtensionRegistry& generated_registry() {
static const ExtensionRegistry* r = NewGeneratedRegistry();
return *r;
}
private:
friend upb_ExtensionRegistry* ::hpb::internal::GetUpbExtensions(
const ExtensionRegistry& extension_registry);
upb_ExtensionRegistry* registry_;
// TODO: b/379100963 - Introduce ShutdownHpbLibrary
static const ExtensionRegistry* NewGeneratedRegistry() {
static upb::Arena* global_arena = new upb::Arena();
ExtensionRegistry* registry = new ExtensionRegistry(*global_arena);
upb_ExtensionRegistry_AddAllLinkedExtensions(registry->registry_);
return registry;
}
};
template <typename T, typename Extendee, typename Extension,

@ -122,12 +122,17 @@ TEST(CppGeneratedCode, ConcurrentAccessDoesNotRaceOneLazyOneEager) {
}
TEST(CppGeneratedCode, ConcurrentAccessDoesNotRaceBothEager) {
::upb::Arena arena;
upb::Arena arena;
hpb::ExtensionRegistry registry(arena);
registry.AddExtension(theme);
registry.AddExtension(ThemeExtension::theme_extension);
TestConcurrentExtensionAccess(registry);
}
TEST(CppGeneratedCode, ConcurrentAccessDoesNotRaceGlobalInstance) {
upb::Arena arena;
TestConcurrentExtensionAccess(hpb::ExtensionRegistry::generated_registry());
}
} // namespace
} // namespace hpb_unittest::protos

@ -1020,12 +1020,10 @@ TEST(CppGeneratedCode, ParseWithExtensionRegistry) {
auto bytes = ::hpb::Serialize(&model, arena);
EXPECT_EQ(true, bytes.ok());
hpb::ExtensionRegistry extensions(arena);
extensions.AddExtension(theme);
extensions.AddExtension(other_ext);
extensions.AddExtension(ThemeExtension::theme_extension);
TestModel parsed_model =
::hpb::Parse<TestModel>(bytes.value(), extensions).value();
::hpb::Parse<TestModel>(bytes.value(),
hpb::ExtensionRegistry::generated_registry())
.value();
EXPECT_EQ("Test123", parsed_model.str1());
EXPECT_EQ(true, hpb::GetExtension(&parsed_model, theme).ok());
EXPECT_EQ(
@ -1287,9 +1285,10 @@ TEST(CppGeneratedCode, HasExtensionAndRegistry) {
std::string data = std::string(::hpb::Serialize(&source, arena).value());
// Test with ExtensionRegistry
hpb::ExtensionRegistry extensions(arena);
extensions.AddExtension(theme);
TestModel parsed_model = ::hpb::Parse<TestModel>(data, extensions).value();
TestModel parsed_model =
::hpb::Parse<TestModel>(data,
hpb::ExtensionRegistry::generated_registry())
.value();
EXPECT_TRUE(::hpb::HasExtension(&parsed_model, theme));
}

Loading…
Cancel
Save