Make ExtensionIdentifier's constructor private

PiperOrigin-RevId: 687065564
pull/18881/head
Hong Shin 1 month ago committed by Copybara-Service
parent e26cb4864d
commit 9a3c096b0c
  1. 4
      hpb/extension.h
  2. 6
      hpb/internal/internal.h
  3. 9
      hpb_generator/gen_extensions.cc

@ -61,14 +61,14 @@ class ExtensionIdentifier : public ExtensionMiniTableProvider {
using Extension = ExtensionType;
using Extendee = ExtendeeType;
private:
constexpr explicit ExtensionIdentifier(
const upb_MiniTableExtension* mini_table_ext)
: ExtensionMiniTableProvider(mini_table_ext) {}
private:
constexpr uint32_t number() const {
return upb_MiniTableExtension_Number(mini_table_ext());
}
friend struct PrivateAccess;
};

@ -9,6 +9,7 @@
#define GOOGLE_PROTOBUF_HPB_INTERNAL_INTERNAL_H__
#include <cstdint>
#include <utility>
#include "upb/mem/arena.h"
#include "upb/message/message.h"
@ -37,6 +38,11 @@ struct PrivateAccess {
return typename T::Proxy(upb_Message_New(T::minitable(), arena), arena);
}
template <typename T, typename... Args>
static auto InvokeConstructor(Args&&... args) {
return T(std::forward<Args>(args)...);
}
template <typename ExtensionId>
static constexpr uint32_t GetExtensionNumber(const ExtensionId& id) {
return id.number();

@ -9,7 +9,6 @@
#include "absl/strings/str_cat.h"
#include "google/protobuf/compiler/hpb/context.h"
#include "google/protobuf/compiler/hpb/gen_utils.h"
#include "google/protobuf/compiler/hpb/names.h"
#include "upb_generator/c/names.h"
@ -69,14 +68,18 @@ void WriteExtensionIdentifier(const protobuf::FieldDescriptor* ext,
if (ext->extension_scope()) {
ctx.EmitLegacy(
R"cc(
const hpb::internal::ExtensionIdentifier<$0, $3> $4::$2(&$1);
const hpb::internal::ExtensionIdentifier<$0, $3> $4::$2 =
hpb::internal::PrivateAccess::InvokeConstructor<
hpb::internal::ExtensionIdentifier<$0, $3>>(&$1);
)cc",
ContainingTypeName(ext), mini_table_name, ext->name(),
CppTypeParameterName(ext), ClassName(ext->extension_scope()));
} else {
ctx.EmitLegacy(
R"cc(
const hpb::internal::ExtensionIdentifier<$0, $3> $2(&$1);
const hpb::internal::ExtensionIdentifier<$0, $3> $2 =
hpb::internal::PrivateAccess::InvokeConstructor<
hpb::internal::ExtensionIdentifier<$0, $3>>(&$1);
)cc",
ContainingTypeName(ext), mini_table_name, ext->name(),
CppTypeParameterName(ext));

Loading…
Cancel
Save