Skip to content

Commit

Permalink
Introduce hpb::interop::upb and hpb::internal::backend::upb
Browse files Browse the repository at this point in the history
Migrate ::hpb::internal::GetMiniTable to ::hpb::interop::upb::GetMiniTable

hpb.h now shows signs of backend-awareness, #including upb.h if the backend is known to be upb. If no backend is picked, we error out at this time since we solely support upb.

ClearMessage has been migrated to backend-aware hpb.

PiperOrigin-RevId: 665527832
  • Loading branch information
honglooker authored and copybara-github committed Aug 20, 2024
1 parent b817373 commit 8bb789e
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 29 deletions.
4 changes: 4 additions & 0 deletions hpb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ cc_library(
deps = [
":hpb",
":traits",
"//hpb/backend/upb:interop",
"//upb:base",
"//upb:mem",
"//upb:message",
Expand All @@ -51,9 +52,12 @@ cc_library(
],
compatible_with = ["//buildenv/target:non_prod"],
copts = UPB_DEFAULT_CPPOPTS,
defines = ["HPB_BACKEND_UPB"],
visibility = ["//visibility:public"],
deps = [
"//hpb:ptr",
"//hpb/backend/upb",
"//hpb/backend/upb:interop",
"//hpb/internal",
"//hpb/internal:message_lock",
"//hpb/internal:template_help",
Expand Down
38 changes: 38 additions & 0 deletions hpb/backend/upb/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (c) 2024, Google LLC
# 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

# begin:google_only
# package(default_applicable_licenses = ["//src/google/protobuf:license"])
# end:google_only

cc_library(
name = "upb",
hdrs = ["upb.h"],
compatible_with = ["//buildenv/target:non_prod"],
visibility = ["//upb:friends"],
deps = [
":interop",
"//hpb:ptr",
"//hpb/internal",
"//hpb/internal:template_help",
"//upb:mini_table",
],
)

cc_library(
name = "interop",
hdrs = ["interop.h"],
compatible_with = ["//buildenv/target:non_prod"],
visibility = [
"//hpb:__subpackages__",
"//protos:__pkg__",
],
deps = [
"//hpb:ptr",
"//upb:mini_table",
],
)
30 changes: 30 additions & 0 deletions hpb/backend/upb/interop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google LLC. 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

#ifndef GOOGLE_PROTOBUF_HPB_BACKEND_UPB_INTEROP_H__
#define GOOGLE_PROTOBUF_HPB_BACKEND_UPB_INTEROP_H__

// The sole public header in hpb/backend/upb

#include "google/protobuf/hpb/ptr.h"
#include "upb/mini_table/message.h"

namespace hpb::interop::upb {

template <typename T>
const upb_MiniTable* GetMiniTable(const T*) {
return T::minitable();
}

template <typename T>
const upb_MiniTable* GetMiniTable(Ptr<T>) {
return T::minitable();
}

} // namespace hpb::interop::upb

#endif // GOOGLE_PROTOBUF_HPB_BACKEND_UPB_INTEROP_H__
27 changes: 27 additions & 0 deletions hpb/backend/upb/upb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google LLC. 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

#ifndef GOOGLE_PROTOBUF_HPB_BACKEND_UPB_UPB_H__
#define GOOGLE_PROTOBUF_HPB_BACKEND_UPB_UPB_H__

#include "google/protobuf/hpb/backend/upb/interop.h"
#include "google/protobuf/hpb/internal/internal.h"
#include "google/protobuf/hpb/internal/template_help.h"
#include "google/protobuf/hpb/ptr.h"

namespace hpb::internal::backend::upb {

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);
}

} // namespace hpb::internal::backend::upb

#endif // GOOGLE_PROTOBUF_HPB_BACKEND_UPB_UPB_H__
45 changes: 22 additions & 23 deletions hpb/hpb.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/hpb/backend/upb/interop.h"
#include "google/protobuf/hpb/internal/internal.h"
#include "google/protobuf/hpb/internal/template_help.h"
#include "google/protobuf/hpb/ptr.h"
Expand All @@ -25,6 +26,12 @@
#include "upb/wire/decode.h"
#include "upb/wire/encode.h"

#ifdef HPB_BACKEND_UPB
#include "google/protobuf/hpb/backend/upb/upb.h"
#else
#error hpb backend must be specified
#endif

namespace hpb {
class ExtensionRegistry;
using Arena = ::upb::Arena;
Expand Down Expand Up @@ -118,16 +125,6 @@ upb_Arena* GetArena(T* message) {
return static_cast<upb_Arena*>(message->GetInternalArena());
}

template <typename T>
const upb_MiniTable* GetMiniTable(const T*) {
return T::minitable();
}

template <typename T>
const upb_MiniTable* GetMiniTable(Ptr<T>) {
return T::minitable();
}

upb_ExtensionRegistry* GetUpbExtensions(
const ExtensionRegistry& extension_registry);

Expand Down Expand Up @@ -157,6 +154,10 @@ absl::Status SetExtension(upb_Message* message, upb_Arena* message_arena,

} // namespace internal

#ifdef HPB_BACKEND_UPB
namespace backend = ::hpb::internal::backend::upb;
#endif

class ExtensionRegistry {
public:
ExtensionRegistry(
Expand Down Expand Up @@ -361,20 +362,18 @@ void DeepCopy(const T* source_message, T* target_message) {

template <typename T>
void ClearMessage(hpb::internal::PtrOrRaw<T> message) {
auto ptr = Ptr(message);
auto minitable = hpb::internal::GetMiniTable(ptr);
upb_Message_Clear(hpb::internal::GetInternalMsg(ptr), minitable);
backend::ClearMessage(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),
::hpb::internal::GetMiniTable(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::internal::GetMiniTable(message),
::hpb::interop::upb::GetMiniTable(message),
/* extreg= */ nullptr, /* options= */ 0,
arena) == kUpb_DecodeStatus_Ok;
}
Expand All @@ -385,11 +384,11 @@ ABSL_MUST_USE_RESULT bool Parse(
const ::hpb::ExtensionRegistry& extension_registry) {
static_assert(!std::is_const_v<T>);
upb_Message_Clear(::hpb::internal::GetInternalMsg(message),
::hpb::internal::GetMiniTable(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::internal::GetMiniTable(message),
::hpb::interop::upb::GetMiniTable(message),
/* extreg= */
::hpb::internal::GetUpbExtensions(extension_registry),
/* options= */ 0, arena) == kUpb_DecodeStatus_Ok;
Expand All @@ -407,11 +406,11 @@ 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),
::hpb::internal::GetMiniTable(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::internal::GetMiniTable(message),
::hpb::interop::upb::GetMiniTable(message),
/* extreg= */ nullptr, /* options= */ 0,
arena) == kUpb_DecodeStatus_Ok;
}
Expand All @@ -422,7 +421,7 @@ absl::StatusOr<T> Parse(absl::string_view bytes, int options = 0) {
auto* arena = static_cast<upb_Arena*>(message.GetInternalArena());
upb_DecodeStatus status =
upb_Decode(bytes.data(), bytes.size(), message.msg(),
::hpb::internal::GetMiniTable(&message),
::hpb::interop::upb::GetMiniTable(&message),
/* extreg= */ nullptr, /* options= */ 0, arena);
if (status == kUpb_DecodeStatus_Ok) {
return message;
Expand All @@ -438,7 +437,7 @@ absl::StatusOr<T> Parse(absl::string_view bytes,
auto* arena = static_cast<upb_Arena*>(message.GetInternalArena());
upb_DecodeStatus status =
upb_Decode(bytes.data(), bytes.size(), message.msg(),
::hpb::internal::GetMiniTable(&message),
::hpb::interop::upb::GetMiniTable(&message),
::hpb::internal::GetUpbExtensions(extension_registry),
/* options= */ 0, arena);
if (status == kUpb_DecodeStatus_Ok) {
Expand All @@ -451,15 +450,15 @@ 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),
::hpb::internal::GetMiniTable(message),
::hpb::interop::upb::GetMiniTable(message),
arena.ptr(), options);
}

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),
::hpb::internal::GetMiniTable(message),
::hpb::interop::upb::GetMiniTable(message),
arena.ptr(), options);
}

Expand Down
3 changes: 2 additions & 1 deletion hpb/repeated_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <type_traits>

#include "absl/strings/string_view.h"
#include "google/protobuf/hpb/backend/upb/interop.h"
#include "google/protobuf/hpb/hpb.h"
#include "google/protobuf/hpb/repeated_field_iterator.h"
#include "google/protobuf/hpb/traits.h"
Expand Down Expand Up @@ -120,7 +121,7 @@ class RepeatedFieldProxy
upb_MessageValue message_value;
message_value.msg_val = upb_Message_DeepClone(
::hpb::internal::PrivateAccess::GetInternalMsg(&t),
::hpb::internal::GetMiniTable(&t), this->arena_);
::hpb::interop::upb::GetMiniTable(&t), this->arena_);
upb_Array_Append(this->arr_, message_value, this->arena_);
}

Expand Down
8 changes: 4 additions & 4 deletions hpb_generator/gen_messages.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ void WriteModelProxyDeclaration(const protobuf::Descriptor* descriptor,
friend class ::hpb::Ptr<$0>;
friend class ::hpb::Ptr<const $0>;
static const upb_MiniTable* minitable() { return $0::minitable(); }
friend const upb_MiniTable* ::hpb::internal::GetMiniTable<$0Proxy>(
friend const upb_MiniTable* ::hpb::interop::upb::GetMiniTable<$0Proxy>(
const $0Proxy* message);
friend const upb_MiniTable* ::hpb::internal::GetMiniTable<$0Proxy>(
friend const upb_MiniTable* ::hpb::interop::upb::GetMiniTable<$0Proxy>(
::hpb::Ptr<$0Proxy> message);
friend upb_Arena* ::hpb::internal::GetArena<$2>($2* message);
friend upb_Arena* ::hpb::internal::GetArena<$2>(::hpb::Ptr<$2> message);
Expand Down Expand Up @@ -356,9 +356,9 @@ void WriteModelCProxyDeclaration(const protobuf::Descriptor* descriptor,
friend class ::hpb::Ptr<$0>;
friend class ::hpb::Ptr<const $0>;
static const upb_MiniTable* minitable() { return $0::minitable(); }
friend const upb_MiniTable* ::hpb::internal::GetMiniTable<$0CProxy>(
friend const upb_MiniTable* ::hpb::interop::upb::GetMiniTable<$0CProxy>(
const $0CProxy* message);
friend const upb_MiniTable* ::hpb::internal::GetMiniTable<$0CProxy>(
friend const upb_MiniTable* ::hpb::interop::upb::GetMiniTable<$0CProxy>(
::hpb::Ptr<$0CProxy> message);
static void Rebind($0CProxy& lhs, const $0CProxy& rhs) {
Expand Down
1 change: 1 addition & 0 deletions protos/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# visibility = ["//visibility:public"],
# deps = [
# "//hpb",
# "//hpb/backend/upb:interop",
# ],
# )
# end:google_only
3 changes: 2 additions & 1 deletion protos/protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_PROTOS_PROTOS_H_
#define UPB_PROTOS_PROTOS_H_
#include "google/protobuf/hpb/backend/upb/interop.h"
#include "google/protobuf/hpb/hpb.h"
namespace protos {
namespace internal {
Expand All @@ -16,14 +17,14 @@ using hpb::internal::DeepCopy;
using hpb::internal::ExtensionIdentifier;
using hpb::internal::GetArena;
using hpb::internal::GetInternalMsg;
using hpb::internal::GetMiniTable;
using hpb::internal::GetOrPromoteExtension;
using hpb::internal::GetUpbExtensions;
using hpb::internal::HasExtensionOrUnknown;
using hpb::internal::MoveExtension;
using hpb::internal::PrivateAccess;
using hpb::internal::Serialize;
using hpb::internal::SetExtension;
using hpb::interop::upb::GetMiniTable;
} // namespace internal
using hpb::CreateMessage;
using hpb::Parse;
Expand Down

0 comments on commit 8bb789e

Please sign in to comment.