From f72b1034b4f5f702bc6e7ef58c3e352a8b0623d7 Mon Sep 17 00:00:00 2001 From: Hong Shin Date: Mon, 18 Nov 2024 11:04:24 -0800 Subject: [PATCH] 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 --- .../Reflection/FeatureSetDescriptor.g.cs | 17 ----------------- hpb/extension.h | 13 +++++++++++++ hpb/internal/message_lock_test.cc | 7 ++++++- hpb_generator/tests/test_generated.cc | 15 +++++++-------- 4 files changed, 26 insertions(+), 26 deletions(-) delete mode 100644 csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs diff --git a/csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs b/csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs deleted file mode 100644 index 208ce1fcb631..000000000000 --- a/csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs +++ /dev/null @@ -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"; -} diff --git a/hpb/extension.h b/hpb/extension.h index 6dea740af9b3..2cacc765426c 100644 --- a/hpb/extension.h +++ b/hpb/extension.h @@ -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 (bytes.value(), extensions).value(); + ::hpb::Parse(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(data, extensions).value(); + TestModel parsed_model = + ::hpb::Parse(data, + hpb::ExtensionRegistry::generated_registry()) + .value(); EXPECT_TRUE(::hpb::HasExtension(&parsed_model, theme)); }