Skip to content

Commit e59cc1d

Browse files
honglookercopybara-github
authored andcommitted
hpb/multi: properly split up internal.h based on backend
PiperOrigin-RevId: 825088652
1 parent ec86098 commit e59cc1d

File tree

10 files changed

+133
-62
lines changed

10 files changed

+133
-62
lines changed

hpb/BUILD

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,19 @@ cc_library(
187187
":arena",
188188
":multibackend",
189189
":ptr",
190-
"//hpb/backend/upb:extension",
191-
"//hpb/backend/upb:interop",
192190
"//hpb/internal:message_lock",
193191
"//hpb/internal:template_help",
194192
"//upb/message",
195193
"//upb/mini_table",
196194
"@abseil-cpp//absl/base:core_headers",
197-
],
195+
] + select({
196+
"//hpb:hpb_backend_cpp": [
197+
],
198+
"//hpb:hpb_backend_upb": [
199+
"//hpb/backend/upb:extension",
200+
"//hpb/backend/upb:interop",
201+
],
202+
}),
198203
)
199204

200205
cc_library(

hpb/backend/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ cc_library(
1818
] + select({
1919
"//hpb:hpb_backend_cpp": [
2020
"//hpb/backend/cpp:error",
21+
"//hpb/backend/cpp:internal",
2122
"//src/google/protobuf:arena",
2223
],
2324
"//hpb:hpb_backend_upb": [
2425
"//hpb/backend/upb:error",
26+
"//hpb/backend/upb:internal",
2527
"//upb/mem",
2628
],
2729
}),

hpb/backend/cpp/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ cc_library(
4444
visibility = ["//hpb:__subpackages__"],
4545
)
4646

47+
cc_library(
48+
name = "internal",
49+
hdrs = ["internal.h"],
50+
visibility = ["//hpb:__subpackages__"],
51+
)
52+
4753
cc_test(
4854
name = "cpp_test",
4955
srcs = ["cpp_test.cc"],

hpb/backend/cpp/internal.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2025 Google LLC. All rights reserved.
3+
//
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file or at
6+
// https://developers.google.com/open-source/licenses/bsd
7+
8+
#ifndef GOOGLE_PROTOBUF_HPB_BACKEND_CPP_INTERNAL_H__
9+
#define GOOGLE_PROTOBUF_HPB_BACKEND_CPP_INTERNAL_H__
10+
11+
namespace hpb::internal {
12+
struct PrivateAccess {};
13+
} // namespace hpb::internal
14+
15+
#endif // GOOGLE_PROTOBUF_HPB_BACKEND_CPP_INTERNAL_H__

hpb/backend/types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
#include "hpb/multibackend.h"
1212
#if HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_UPB
1313
#include "hpb/backend/upb/error.h"
14+
#include "hpb/backend/upb/internal.h"
1415
#include "upb/mem/arena.hpp"
1516
#elif HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_CPP
1617
#include "google/protobuf/arena.h"
1718
#include "hpb/backend/cpp/error.h"
19+
#include "hpb/backend/cpp/internal.h"
1820
#endif
1921

2022
namespace hpb {

hpb/backend/upb/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ cc_library(
4343
],
4444
deps = [
4545
"//hpb:ptr",
46+
"//hpb/backend:types",
4647
"//hpb/internal",
4748
"//upb/base",
4849
"//upb/mem",
@@ -53,6 +54,16 @@ cc_library(
5354
],
5455
)
5556

57+
cc_library(
58+
name = "internal",
59+
hdrs = ["internal.h"],
60+
visibility = ["//hpb:__subpackages__"],
61+
deps = [
62+
"//upb/mem",
63+
"//upb/message",
64+
],
65+
)
66+
5667
cc_test(
5768
name = "interop_test",
5869
srcs = ["interop_test.cc"],

hpb/backend/upb/internal.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2025 Google LLC. All rights reserved.
3+
//
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file or at
6+
// https://developers.google.com/open-source/licenses/bsd
7+
8+
#ifndef GOOGLE_PROTOBUF_HPB_BACKEND_UPB_INTERNAL_H__
9+
#define GOOGLE_PROTOBUF_HPB_BACKEND_UPB_INTERNAL_H__
10+
11+
#include <cstdint>
12+
#include <utility>
13+
#include "upb/mem/arena.h"
14+
#include "upb/message/message.h"
15+
16+
namespace hpb::internal {
17+
18+
struct PrivateAccess {
19+
template <typename T>
20+
static auto* GetInternalMsg(T&& message) {
21+
return message->msg();
22+
}
23+
template <typename T>
24+
static auto* GetInternalArena(T&& message) {
25+
return message->arena();
26+
}
27+
template <typename T>
28+
static auto* GetInternalUPBArena(T&& arena) {
29+
return arena.arena_.ptr();
30+
}
31+
template <typename T>
32+
static auto Proxy(upb_Message* p, upb_Arena* arena) {
33+
return typename T::Proxy(p, arena);
34+
}
35+
template <typename T>
36+
static auto CProxy(const upb_Message* p, upb_Arena* arena) {
37+
return typename T::CProxy(p, arena);
38+
}
39+
template <typename T>
40+
static auto CreateMessage(upb_Arena* arena) {
41+
return typename T::Proxy(upb_Message_New(T::minitable(), arena), arena);
42+
}
43+
44+
template <typename T, typename... Args>
45+
static constexpr auto InvokeConstructor(Args&&... args) {
46+
return T(std::forward<Args>(args)...);
47+
}
48+
49+
template <typename ExtensionId>
50+
static constexpr uint32_t GetExtensionNumber(const ExtensionId& id) {
51+
return id.number();
52+
}
53+
54+
template <typename ExtensionId>
55+
static decltype(auto) GetDefaultValue(const ExtensionId& id) {
56+
return id.default_value();
57+
}
58+
};
59+
60+
template <typename T>
61+
struct AssociatedUpbTypes;
62+
63+
} // namespace hpb::internal
64+
65+
#endif // GOOGLE_PROTOBUF_HPB_BACKEND_UPB_INTERNAL_H__

hpb/extension.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313

1414
#include "absl/base/attributes.h"
1515
#include "hpb/arena.h"
16-
#include "hpb/backend/upb/extension.h"
17-
#include "hpb/backend/upb/interop.h"
1816
#include "hpb/internal/message_lock.h"
1917
#include "hpb/internal/template_help.h"
2018
#include "hpb/multibackend.h"
2119
#include "hpb/ptr.h"
20+
21+
#if HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_UPB
22+
#include "hpb/backend/upb/extension.h"
23+
#include "hpb/backend/upb/interop.h"
2224
#include "upb/message/accessors.h"
2325
#include "upb/mini_table/extension_registry.h"
26+
#endif
2427

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

93+
#if HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_UPB
9094
template <typename T, typename Extendee, typename Extension,
9195
typename = hpb::internal::EnableIfHpbClassThatHasExtensions<T>>
9296
ABSL_MUST_USE_RESULT bool HasExtension(
@@ -186,6 +190,7 @@ constexpr uint32_t ExtensionNumber(
186190
const internal::ExtensionIdentifier<T, Extension>& id) {
187191
return internal::PrivateAccess::GetExtensionNumber(id);
188192
}
193+
#endif
189194

190195
} // namespace hpb
191196

hpb/internal/BUILD

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,15 @@ cc_library(
4949
"//hpb:__subpackages__",
5050
],
5151
deps = [
52-
"//upb/mem",
53-
"//upb/message",
54-
],
52+
"//hpb:multibackend",
53+
] + select({
54+
"//hpb:hpb_backend_upb": [
55+
"//hpb/backend/upb:internal",
56+
],
57+
"//hpb:hpb_backend_cpp": [
58+
"//hpb/backend/cpp:internal",
59+
],
60+
}),
5561
)
5662

5763
cc_library(

hpb/internal/internal.h

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,13 @@
88
#ifndef GOOGLE_PROTOBUF_HPB_INTERNAL_INTERNAL_H__
99
#define GOOGLE_PROTOBUF_HPB_INTERNAL_INTERNAL_H__
1010

11-
#include <cstdint>
12-
#include <utility>
13-
14-
#include "upb/mem/arena.h"
15-
#include "upb/message/message.h"
16-
17-
namespace hpb::internal {
18-
19-
struct PrivateAccess {
20-
template <typename T>
21-
static auto* GetInternalMsg(T&& message) {
22-
return message->msg();
23-
}
24-
template <typename T>
25-
static auto* GetInternalArena(T&& message) {
26-
return message->arena();
27-
}
28-
template <typename T>
29-
static auto* GetInternalUPBArena(T&& arena) {
30-
return arena.arena_.ptr();
31-
}
32-
template <typename T>
33-
static auto Proxy(upb_Message* p, upb_Arena* arena) {
34-
return typename T::Proxy(p, arena);
35-
}
36-
template <typename T>
37-
static auto CProxy(const upb_Message* p, upb_Arena* arena) {
38-
return typename T::CProxy(p, arena);
39-
}
40-
template <typename T>
41-
static auto CreateMessage(upb_Arena* arena) {
42-
return typename T::Proxy(upb_Message_New(T::minitable(), arena), arena);
43-
}
44-
45-
template <typename T, typename... Args>
46-
static constexpr auto InvokeConstructor(Args&&... args) {
47-
return T(std::forward<Args>(args)...);
48-
}
49-
50-
template <typename ExtensionId>
51-
static constexpr uint32_t GetExtensionNumber(const ExtensionId& id) {
52-
return id.number();
53-
}
54-
55-
template <typename ExtensionId>
56-
static decltype(auto) GetDefaultValue(const ExtensionId& id) {
57-
return id.default_value();
58-
}
59-
};
60-
61-
template <typename T>
62-
struct AssociatedUpbTypes;
63-
64-
} // namespace hpb::internal
11+
#include "hpb/multibackend.h"
12+
#if HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_UPB
13+
#include "hpb/backend/upb/internal.h"
14+
#elif HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_CPP
15+
#include "hpb/backend/cpp/internal.h"
16+
#else
17+
#error "Unsupported hpb backend"
18+
#endif
6519

6620
#endif // GOOGLE_PROTOBUF_HPB_INTERNAL_INTERNAL_H__

0 commit comments

Comments
 (0)