Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions hpb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,19 @@ cc_library(
":arena",
":multibackend",
":ptr",
"//hpb/backend/upb:extension",
"//hpb/backend/upb:interop",
"//hpb/internal:message_lock",
"//hpb/internal:template_help",
"//upb/message",
"//upb/mini_table",
"@abseil-cpp//absl/base:core_headers",
],
] + select({
"//hpb:hpb_backend_cpp": [
],
"//hpb:hpb_backend_upb": [
"//hpb/backend/upb:extension",
"//hpb/backend/upb:interop",
],
}),
)

cc_library(
Expand Down
2 changes: 2 additions & 0 deletions hpb/backend/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ cc_library(
] + select({
"//hpb:hpb_backend_cpp": [
"//hpb/backend/cpp:error",
"//hpb/backend/cpp:internal",
"//src/google/protobuf:arena",
],
"//hpb:hpb_backend_upb": [
"//hpb/backend/upb:error",
"//hpb/backend/upb:internal",
"//upb/mem",
],
}),
Expand Down
6 changes: 6 additions & 0 deletions hpb/backend/cpp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ cc_library(
visibility = ["//hpb:__subpackages__"],
)

cc_library(
name = "internal",
hdrs = ["internal.h"],
visibility = ["//hpb:__subpackages__"],
)

cc_test(
name = "cpp_test",
srcs = ["cpp_test.cc"],
Expand Down
15 changes: 15 additions & 0 deletions hpb/backend/cpp/internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2025 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_CPP_INTERNAL_H__
#define GOOGLE_PROTOBUF_HPB_BACKEND_CPP_INTERNAL_H__

namespace hpb::internal {
struct PrivateAccess {};
} // namespace hpb::internal

#endif // GOOGLE_PROTOBUF_HPB_BACKEND_CPP_INTERNAL_H__
2 changes: 2 additions & 0 deletions hpb/backend/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
#include "hpb/multibackend.h"
#if HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_UPB
#include "hpb/backend/upb/error.h"
#include "hpb/backend/upb/internal.h"
#include "upb/mem/arena.hpp"
#elif HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_CPP
#include "google/protobuf/arena.h"
#include "hpb/backend/cpp/error.h"
#include "hpb/backend/cpp/internal.h"
#endif

namespace hpb {
Expand Down
11 changes: 11 additions & 0 deletions hpb/backend/upb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ cc_library(
],
deps = [
"//hpb:ptr",
"//hpb/backend:types",
"//hpb/internal",
"//upb/base",
"//upb/mem",
Expand All @@ -53,6 +54,16 @@ cc_library(
],
)

cc_library(
name = "internal",
hdrs = ["internal.h"],
visibility = ["//hpb:__subpackages__"],
deps = [
"//upb/mem",
"//upb/message",
],
)

cc_test(
name = "interop_test",
srcs = ["interop_test.cc"],
Expand Down
65 changes: 65 additions & 0 deletions hpb/backend/upb/internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2025 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_INTERNAL_H__
#define GOOGLE_PROTOBUF_HPB_BACKEND_UPB_INTERNAL_H__

#include <cstdint>
#include <utility>
#include "upb/mem/arena.h"
#include "upb/message/message.h"

namespace hpb::internal {

struct PrivateAccess {
template <typename T>
static auto* GetInternalMsg(T&& message) {
return message->msg();
}
template <typename T>
static auto* GetInternalArena(T&& message) {
return message->arena();
}
template <typename T>
static auto* GetInternalUPBArena(T&& arena) {
return arena.arena_.ptr();
}
template <typename T>
static auto Proxy(upb_Message* p, upb_Arena* arena) {
return typename T::Proxy(p, arena);
}
template <typename T>
static auto CProxy(const upb_Message* p, upb_Arena* arena) {
return typename T::CProxy(p, arena);
}
template <typename T>
static auto CreateMessage(upb_Arena* arena) {
return typename T::Proxy(upb_Message_New(T::minitable(), arena), arena);
}

template <typename T, typename... Args>
static constexpr auto InvokeConstructor(Args&&... args) {
return T(std::forward<Args>(args)...);
}

template <typename ExtensionId>
static constexpr uint32_t GetExtensionNumber(const ExtensionId& id) {
return id.number();
}

template <typename ExtensionId>
static decltype(auto) GetDefaultValue(const ExtensionId& id) {
return id.default_value();
}
};

template <typename T>
struct AssociatedUpbTypes;

} // namespace hpb::internal

#endif // GOOGLE_PROTOBUF_HPB_BACKEND_UPB_INTERNAL_H__
9 changes: 7 additions & 2 deletions hpb/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@

#include "absl/base/attributes.h"
#include "hpb/arena.h"
#include "hpb/backend/upb/extension.h"
#include "hpb/backend/upb/interop.h"
#include "hpb/internal/message_lock.h"
#include "hpb/internal/template_help.h"
#include "hpb/multibackend.h"
#include "hpb/ptr.h"

#if HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_UPB
#include "hpb/backend/upb/extension.h"
#include "hpb/backend/upb/interop.h"
#include "upb/message/accessors.h"
#include "upb/mini_table/extension_registry.h"
#endif

namespace hpb {
// upb has a notion of an ExtensionRegistry. We expect most callers to use
Expand Down Expand Up @@ -87,6 +90,7 @@ class ExtensionRegistry {
explicit ExtensionRegistry() = default;
};

#if HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_UPB
template <typename T, typename Extendee, typename Extension,
typename = hpb::internal::EnableIfHpbClassThatHasExtensions<T>>
ABSL_MUST_USE_RESULT bool HasExtension(
Expand Down Expand Up @@ -186,6 +190,7 @@ constexpr uint32_t ExtensionNumber(
const internal::ExtensionIdentifier<T, Extension>& id) {
return internal::PrivateAccess::GetExtensionNumber(id);
}
#endif

} // namespace hpb

Expand Down
12 changes: 9 additions & 3 deletions hpb/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ cc_library(
"//hpb:__subpackages__",
],
deps = [
"//upb/mem",
"//upb/message",
],
"//hpb:multibackend",
] + select({
"//hpb:hpb_backend_upb": [
"//hpb/backend/upb:internal",
],
"//hpb:hpb_backend_cpp": [
"//hpb/backend/cpp:internal",
],
}),
)

cc_library(
Expand Down
62 changes: 8 additions & 54 deletions hpb/internal/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,13 @@
#ifndef GOOGLE_PROTOBUF_HPB_INTERNAL_INTERNAL_H__
#define GOOGLE_PROTOBUF_HPB_INTERNAL_INTERNAL_H__

#include <cstdint>
#include <utility>

#include "upb/mem/arena.h"
#include "upb/message/message.h"

namespace hpb::internal {

struct PrivateAccess {
template <typename T>
static auto* GetInternalMsg(T&& message) {
return message->msg();
}
template <typename T>
static auto* GetInternalArena(T&& message) {
return message->arena();
}
template <typename T>
static auto* GetInternalUPBArena(T&& arena) {
return arena.arena_.ptr();
}
template <typename T>
static auto Proxy(upb_Message* p, upb_Arena* arena) {
return typename T::Proxy(p, arena);
}
template <typename T>
static auto CProxy(const upb_Message* p, upb_Arena* arena) {
return typename T::CProxy(p, arena);
}
template <typename T>
static auto CreateMessage(upb_Arena* arena) {
return typename T::Proxy(upb_Message_New(T::minitable(), arena), arena);
}

template <typename T, typename... Args>
static constexpr auto InvokeConstructor(Args&&... args) {
return T(std::forward<Args>(args)...);
}

template <typename ExtensionId>
static constexpr uint32_t GetExtensionNumber(const ExtensionId& id) {
return id.number();
}

template <typename ExtensionId>
static decltype(auto) GetDefaultValue(const ExtensionId& id) {
return id.default_value();
}
};

template <typename T>
struct AssociatedUpbTypes;

} // namespace hpb::internal
#include "hpb/multibackend.h"
#if HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_UPB
#include "hpb/backend/upb/internal.h"
#elif HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_CPP
#include "hpb/backend/cpp/internal.h"
#else
#error "Unsupported hpb backend"
#endif

#endif // GOOGLE_PROTOBUF_HPB_INTERNAL_INTERNAL_H__
Loading