Introduce hpb::interop::upb::GetMessage; née protos::internal::GetInternalMsg

PiperOrigin-RevId: 672659339
pull/18134/head
Hong Shin 3 months ago committed by Copybara-Service
parent a0f79ebc57
commit 877b02506c
  1. 1
      hpb/backend/upb/BUILD
  2. 5
      hpb/backend/upb/interop.h
  3. 2
      hpb/backend/upb/upb.h
  4. 40
      hpb/hpb.h
  5. 6
      hpb/internal/internal.h
  6. 6
      hpb_generator/gen_messages.cc
  7. 14
      hpb_generator/tests/test_generated.cc
  8. 1
      protos/protos.h

@ -25,6 +25,7 @@ cc_library(
hdrs = ["interop.h"],
visibility = [
"//hpb:__subpackages__",
"//src/google/protobuf/compiler:__subpackages__",
],
deps = [
"//hpb:ptr",

@ -28,6 +28,11 @@ const upb_MiniTable* GetMiniTable(Ptr<T>) {
return T::minitable();
}
template <typename T>
auto* GetMessage(T&& message) {
return hpb::internal::PrivateAccess::GetInternalMsg(std::forward<T>(message));
}
template <typename T>
upb_Arena* GetArena(Ptr<T> message) {
return static_cast<upb_Arena*>(message->GetInternalArena());

@ -19,7 +19,7 @@ template <typename T>
void ClearMessage(hpb::internal::PtrOrRaw<T> message) {
auto ptr = Ptr(message);
auto minitable = hpb::interop::upb::GetMiniTable(ptr);
upb_Message_Clear(hpb::internal::GetInternalMsg(ptr), minitable);
upb_Message_Clear(hpb::interop::upb::GetMessage(ptr), minitable);
}
} // namespace hpb::internal::backend::upb

@ -109,7 +109,7 @@ ABSL_MUST_USE_RESULT bool HasExtension(
Ptr<T> message,
const ::hpb::internal::ExtensionIdentifier<Extendee, Extension>& id) {
return ::hpb::internal::HasExtensionOrUnknown(
::hpb::internal::GetInternalMsg(message), id.mini_table_ext());
hpb::interop::upb::GetMessage(message), id.mini_table_ext());
}
template <typename T, typename Extendee, typename Extension,
@ -127,7 +127,7 @@ void ClearExtension(
Ptr<T> message,
const ::hpb::internal::ExtensionIdentifier<T, Extension>& id) {
static_assert(!std::is_const_v<T>, "");
upb_Message_ClearExtension(hpb::internal::GetInternalMsg(message),
upb_Message_ClearExtension(hpb::interop::upb::GetMessage(message),
id.mini_table_ext());
}
@ -147,9 +147,9 @@ absl::Status SetExtension(
const Extension& value) {
static_assert(!std::is_const_v<T>);
auto* message_arena = static_cast<upb_Arena*>(message->GetInternalArena());
return ::hpb::internal::SetExtension(hpb::internal::GetInternalMsg(message),
return ::hpb::internal::SetExtension(hpb::interop::upb::GetMessage(message),
message_arena, id.mini_table_ext(),
hpb::internal::GetInternalMsg(&value));
hpb::interop::upb::GetMessage(&value));
}
template <typename T, typename Extension,
@ -161,9 +161,9 @@ absl::Status SetExtension(
Ptr<Extension> value) {
static_assert(!std::is_const_v<T>);
auto* message_arena = static_cast<upb_Arena*>(message->GetInternalArena());
return ::hpb::internal::SetExtension(hpb::internal::GetInternalMsg(message),
return ::hpb::internal::SetExtension(hpb::interop::upb::GetMessage(message),
message_arena, id.mini_table_ext(),
hpb::internal::GetInternalMsg(value));
hpb::interop::upb::GetMessage(value));
}
template <typename T, typename Extension,
@ -177,9 +177,9 @@ absl::Status SetExtension(
static_assert(!std::is_const_v<T>);
auto* message_arena = static_cast<upb_Arena*>(message->GetInternalArena());
auto* extension_arena = static_cast<upb_Arena*>(ext.GetInternalArena());
return ::hpb::internal::MoveExtension(hpb::internal::GetInternalMsg(message),
return ::hpb::internal::MoveExtension(hpb::interop::upb::GetMessage(message),
message_arena, id.mini_table_ext(),
hpb::internal::GetInternalMsg(&ext),
hpb::interop::upb::GetMessage(&ext),
extension_arena);
}
@ -215,7 +215,7 @@ absl::StatusOr<Ptr<const Extension>> GetExtension(
// TODO: Fix const correctness issues.
upb_MessageValue value;
const bool ok = ::hpb::internal::GetOrPromoteExtension(
const_cast<upb_Message*>(::hpb::internal::GetInternalMsg(message)),
const_cast<upb_Message*>(hpb::interop::upb::GetMessage(message)),
id.mini_table_ext(), hpb::interop::upb::GetArena(message), &value);
if (!ok) {
return ExtensionNotFoundError(
@ -248,7 +248,7 @@ typename T::Proxy CreateMessage(::hpb::Arena& arena) {
template <typename T>
typename T::Proxy CloneMessage(Ptr<T> message, upb_Arena* arena) {
return ::hpb::internal::PrivateAccess::Proxy<T>(
::hpb::internal::DeepClone(::hpb::internal::GetInternalMsg(message),
::hpb::internal::DeepClone(hpb::interop::upb::GetMessage(message),
T::minitable(), arena),
arena);
}
@ -257,8 +257,8 @@ template <typename T>
void DeepCopy(Ptr<const T> source_message, Ptr<T> target_message) {
static_assert(!std::is_const_v<T>);
::hpb::internal::DeepCopy(
hpb::internal::GetInternalMsg(target_message),
hpb::internal::GetInternalMsg(source_message), T::minitable(),
hpb::interop::upb::GetMessage(target_message),
hpb::interop::upb::GetMessage(source_message), T::minitable(),
static_cast<upb_Arena*>(target_message->GetInternalArena()));
}
@ -288,11 +288,11 @@ void ClearMessage(hpb::internal::PtrOrRaw<T> message) {
template <typename T>
ABSL_MUST_USE_RESULT bool Parse(Ptr<T> message, absl::string_view bytes) {
static_assert(!std::is_const_v<T>);
upb_Message_Clear(::hpb::internal::GetInternalMsg(message),
upb_Message_Clear(hpb::interop::upb::GetMessage(message),
::hpb::interop::upb::GetMiniTable(message));
auto* arena = static_cast<upb_Arena*>(message->GetInternalArena());
return upb_Decode(bytes.data(), bytes.size(),
::hpb::internal::GetInternalMsg(message),
hpb::interop::upb::GetMessage(message),
::hpb::interop::upb::GetMiniTable(message),
/* extreg= */ nullptr, /* options= */ 0,
arena) == kUpb_DecodeStatus_Ok;
@ -303,11 +303,11 @@ ABSL_MUST_USE_RESULT bool Parse(
Ptr<T> message, absl::string_view bytes,
const ::hpb::ExtensionRegistry& extension_registry) {
static_assert(!std::is_const_v<T>);
upb_Message_Clear(::hpb::internal::GetInternalMsg(message),
upb_Message_Clear(hpb::interop::upb::GetMessage(message),
::hpb::interop::upb::GetMiniTable(message));
auto* arena = static_cast<upb_Arena*>(message->GetInternalArena());
return upb_Decode(bytes.data(), bytes.size(),
::hpb::internal::GetInternalMsg(message),
hpb::interop::upb::GetMessage(message),
::hpb::interop::upb::GetMiniTable(message),
/* extreg= */
::hpb::internal::GetUpbExtensions(extension_registry),
@ -325,11 +325,11 @@ ABSL_MUST_USE_RESULT bool Parse(
template <typename T>
ABSL_MUST_USE_RESULT bool Parse(T* message, absl::string_view bytes) {
static_assert(!std::is_const_v<T>);
upb_Message_Clear(::hpb::internal::GetInternalMsg(message),
upb_Message_Clear(hpb::interop::upb::GetMessage(message),
::hpb::interop::upb::GetMiniTable(message));
auto* arena = static_cast<upb_Arena*>(message->GetInternalArena());
return upb_Decode(bytes.data(), bytes.size(),
::hpb::internal::GetInternalMsg(message),
hpb::interop::upb::GetMessage(message),
::hpb::interop::upb::GetMiniTable(message),
/* extreg= */ nullptr, /* options= */ 0,
arena) == kUpb_DecodeStatus_Ok;
@ -369,7 +369,7 @@ absl::StatusOr<T> Parse(absl::string_view bytes,
template <typename T>
absl::StatusOr<absl::string_view> Serialize(const T* message, upb::Arena& arena,
int options = 0) {
return ::hpb::internal::Serialize(::hpb::internal::GetInternalMsg(message),
return ::hpb::internal::Serialize(hpb::interop::upb::GetMessage(message),
::hpb::interop::upb::GetMiniTable(message),
arena.ptr(), options);
}
@ -377,7 +377,7 @@ absl::StatusOr<absl::string_view> Serialize(const T* message, upb::Arena& arena,
template <typename T>
absl::StatusOr<absl::string_view> Serialize(Ptr<T> message, upb::Arena& arena,
int options = 0) {
return ::hpb::internal::Serialize(::hpb::internal::GetInternalMsg(message),
return ::hpb::internal::Serialize(hpb::interop::upb::GetMessage(message),
::hpb::interop::upb::GetMiniTable(message),
arena.ptr(), options);
}

@ -2,7 +2,6 @@
#define GOOGLE_PROTOBUF_HPB_INTERNAL_INTERNAL_H__
#include <cstdint>
#include <utility>
#include "upb/mem/arena.h"
#include "upb/message/message.h"
@ -33,11 +32,6 @@ struct PrivateAccess {
}
};
template <typename T>
auto* GetInternalMsg(T&& message) {
return PrivateAccess::GetInternalMsg(std::forward<T>(message));
}
} // namespace hpb::internal
#endif // GOOGLE_PROTOBUF_HPB_INTERNAL_INTERNAL_H__

@ -395,12 +395,12 @@ void WriteMessageImplementation(
$0::$0(const CProxy& from) : $0Access() {
arena_ = owned_arena_.ptr();
msg_ = ($1*)::hpb::internal::DeepClone(
::hpb::internal::GetInternalMsg(&from), &$2, arena_);
::hpb::interop::upb::GetMessage(&from), &$2, arena_);
}
$0::$0(const Proxy& from) : $0(static_cast<const CProxy&>(from)) {}
internal::$0CProxy::$0CProxy($0Proxy m) : $0Access() {
arena_ = m.arena_;
msg_ = ($1*)::hpb::internal::GetInternalMsg(&m);
msg_ = ($1*)::hpb::interop::upb::GetMessage(&m);
}
$0& $0::operator=(const $3& from) {
arena_ = owned_arena_.ptr();
@ -410,7 +410,7 @@ void WriteMessageImplementation(
$0& $0::operator=(const CProxy& from) {
arena_ = owned_arena_.ptr();
msg_ = ($1*)::hpb::internal::DeepClone(
::hpb::internal::GetInternalMsg(&from), &$2, arena_);
::hpb::interop::upb::GetMessage(&from), &$2, arena_);
return *this;
}
)cc",

@ -22,7 +22,9 @@
#include "google/protobuf/compiler/hpb/tests/no_package.upb.proto.h"
#include "google/protobuf/compiler/hpb/tests/test_extension.upb.proto.h"
#include "google/protobuf/compiler/hpb/tests/test_model.upb.proto.h"
#include "google/protobuf/hpb/backend/upb/interop.h"
#include "google/protobuf/hpb/hpb.h"
#include "google/protobuf/hpb/ptr.h"
#include "google/protobuf/hpb/repeated_field.h"
#include "google/protobuf/hpb/requires.h"
#include "upb/mem/arena.h"
@ -711,7 +713,7 @@ TEST(CppGeneratedCode, SetExtension) {
// Use a nested scope to make sure the arenas are fused correctly.
ThemeExtension extension1;
extension1.set_ext_name("Hello World");
prior_message = ::hpb::internal::GetInternalMsg(&extension1);
prior_message = hpb::interop::upb::GetMessage(&extension1);
EXPECT_EQ(false, ::hpb::HasExtension(&model, theme));
EXPECT_EQ(true,
::hpb::SetExtension(&model, theme, std::move(extension1)).ok());
@ -719,7 +721,7 @@ TEST(CppGeneratedCode, SetExtension) {
EXPECT_EQ(true, ::hpb::HasExtension(&model, theme));
auto ext = hpb::GetExtension(&model, theme);
EXPECT_TRUE(ext.ok());
EXPECT_EQ(::hpb::internal::GetInternalMsg(*ext), prior_message);
EXPECT_EQ(hpb::interop::upb::GetMessage(*ext), prior_message);
}
TEST(CppGeneratedCode, SetExtensionWithPtr) {
@ -732,7 +734,7 @@ TEST(CppGeneratedCode, SetExtensionWithPtr) {
::hpb::Ptr<ThemeExtension> extension1 =
::hpb::CreateMessage<ThemeExtension>(arena);
extension1->set_ext_name("Hello World");
prior_message = ::hpb::internal::GetInternalMsg(extension1);
prior_message = hpb::interop::upb::GetMessage(extension1);
EXPECT_EQ(false, ::hpb::HasExtension(model, theme));
auto res = ::hpb::SetExtension(model, theme, extension1);
EXPECT_EQ(true, res.ok());
@ -740,7 +742,7 @@ TEST(CppGeneratedCode, SetExtensionWithPtr) {
EXPECT_EQ(true, ::hpb::HasExtension(model, theme));
auto ext = hpb::GetExtension(model, theme);
EXPECT_TRUE(ext.ok());
EXPECT_NE(::hpb::internal::GetInternalMsg(*ext), prior_message);
EXPECT_NE(hpb::interop::upb::GetMessage(*ext), prior_message);
}
#ifndef _MSC_VER
@ -774,7 +776,7 @@ TEST(CppGeneratedCode, SetExtensionWithPtrSameArena) {
::hpb::Ptr<ThemeExtension> extension1 =
::hpb::CreateMessage<ThemeExtension>(arena);
extension1->set_ext_name("Hello World");
prior_message = ::hpb::internal::GetInternalMsg(extension1);
prior_message = hpb::interop::upb::GetMessage(extension1);
EXPECT_EQ(false, ::hpb::HasExtension(model, theme));
auto res = ::hpb::SetExtension(model, theme, extension1);
EXPECT_EQ(true, res.ok());
@ -782,7 +784,7 @@ TEST(CppGeneratedCode, SetExtensionWithPtrSameArena) {
EXPECT_EQ(true, ::hpb::HasExtension(model, theme));
auto ext = hpb::GetExtension(model, theme);
EXPECT_TRUE(ext.ok());
EXPECT_NE(::hpb::internal::GetInternalMsg(*ext), prior_message);
EXPECT_NE(hpb::interop::upb::GetMessage(*ext), prior_message);
}
TEST(CppGeneratedCode, SetExtensionFusingFailureShouldCopy) {

@ -9,7 +9,6 @@
#include "google/protobuf/hpb/hpb.h"
namespace protos {
namespace internal {
using hpb::internal::GetInternalMsg;
} // namespace internal
using hpb::Arena;

Loading…
Cancel
Save