Skip to content

Commit

Permalink
hpb: impl GlobalExtensionRegistry
Browse files Browse the repository at this point in the history
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
  • Loading branch information
honglooker authored and copybara-github committed Nov 18, 2024
1 parent c2e5ad3 commit f72b103
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
17 changes: 0 additions & 17 deletions csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs

This file was deleted.

13 changes: 13 additions & 0 deletions hpb/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion hpb/internal/message_lock_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 7 additions & 8 deletions hpb_generator/tests/test_generated.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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));
}

Expand Down

0 comments on commit f72b103

Please sign in to comment.