hpb/cpp: get cpp backend compiling again

Stubbed out repeated fields,  extensions, and error for hpb(cpp).

PiperOrigin-RevId: 820213542
This commit is contained in:
Hong Shin 2025-10-16 06:52:45 -07:00 committed by Copybara-Service
parent ef890c3d0c
commit 37bb5d7a13
12 changed files with 121 additions and 9 deletions

View file

@ -95,6 +95,7 @@ cc_library(
"@abseil-cpp//absl/strings",
] + select({
":hpb_backend_cpp": [
"//hpb/backend/cpp:repeated_field",
],
":hpb_backend_upb": [
"//hpb/backend/upb:repeated_field",

View file

@ -17,6 +17,7 @@ cc_library(
"//hpb:multibackend",
] + select({
"//hpb:hpb_backend_cpp": [
"//hpb/backend/cpp:error",
"//src/google/protobuf:arena",
],
"//hpb:hpb_backend_upb": [

View file

@ -5,6 +5,7 @@
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
load("@rules_cc//cc:cc_test.bzl", "cc_test")
load("@rules_cc//cc:defs.bzl", "cc_library")
package(default_applicable_licenses = ["//:license"])
@ -30,3 +31,28 @@ cc_library(
"//hpb/internal",
],
)
cc_library(
name = "error",
hdrs = ["error.h"],
visibility = ["//hpb:__subpackages__"],
)
cc_library(
name = "repeated_field",
hdrs = ["repeated_field.h"],
visibility = ["//hpb:__subpackages__"],
)
cc_test(
name = "cpp_test",
srcs = ["cpp_test.cc"],
deps = [
"//hpb",
"//hpb:arena",
"//hpb:ptr",
"//hpb_generator/tests:test_model_hpb_proto",
"@googletest//:gtest",
"@googletest//:gtest_main",
],
)

View file

@ -20,12 +20,12 @@ namespace hpb::internal::backend::cpp {
// hpb(cpp) backend stubs.
template <typename T>
typename T::Proxy CreateMessage(Arena& arena) {
typename T::Proxy CreateMessage(hpb::Arena& arena) {
return typename T::Proxy();
}
template <typename T>
typename T::Proxy CloneMessage(Ptr<T> message, Arena& arena) {
typename T::Proxy CloneMessage(Ptr<T> message, hpb::Arena& arena) {
abort();
}
@ -40,7 +40,7 @@ void DeepCopy(Ptr<const T> source_message, Ptr<T> target_message) {
}
template <typename T>
absl::string_view Serialize(PtrOrRaw<T> message, Arena& arena) {
absl::string_view Serialize(PtrOrRaw<T> message, hpb::Arena& arena) {
abort();
}

View file

@ -0,0 +1,25 @@
// 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
#include <gtest/gtest.h>
#include "hpb_generator/tests/test_model.hpb.h"
#include "hpb/arena.h"
#include "hpb/hpb.h"
#include "hpb/ptr.h"
namespace hpb::testing {
namespace {
using ::hpb_unittest::protos::TestModel;
TEST(CppBackend, CanCreateMessage) {
hpb::Arena arena;
hpb::Ptr<TestModel> test_model_ptr = hpb::CreateMessage<TestModel>(arena);
(void)test_model_ptr;
}
} // namespace
} // namespace hpb::testing

21
hpb/backend/cpp/error.h Normal file
View file

@ -0,0 +1,21 @@
// 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_ERROR_H__
#define GOOGLE_PROTOBUF_HPB_BACKEND_CPP_ERROR_H__
namespace hpb {
namespace internal {
namespace backend {
namespace cpp {
class Error {};
} // namespace cpp
} // namespace backend
} // namespace internal
} // namespace hpb
#endif // GOOGLE_PROTOBUF_HPB_BACKEND_CPP_ERROR_H__

View file

@ -0,0 +1,24 @@
// 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_REPEATED_FIELD_H__
#define GOOGLE_PROTOBUF_HPB_BACKEND_CPP_REPEATED_FIELD_H__
namespace hpb::internal {
template <typename T>
class RepeatedFieldScalarProxy {};
template <typename T>
class RepeatedFieldStringProxy {};
template <typename T>
class RepeatedFieldProxy {};
} // namespace hpb::internal
#endif // GOOGLE_PROTOBUF_HPB_BACKEND_CPP_REPEATED_FIELD_H__

View file

@ -14,6 +14,7 @@
#include "upb/mem/arena.hpp"
#elif HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_CPP
#include "google/protobuf/arena.h"
#include "hpb/backend/cpp/error.h"
#endif
namespace hpb {
@ -25,6 +26,7 @@ namespace upb {
using Arena = ::upb::Arena;
}
#elif HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_CPP
using Error = ::hpb::internal::backend::cpp::Error;
namespace cpp {
using Arena = google::protobuf::Arena;
}

View file

@ -7,6 +7,9 @@
#include "hpb/extension.h"
#include "hpb/multibackend.h"
#if HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_UPB
#include "upb/mini_table/extension_registry.h"
namespace hpb {
@ -15,6 +18,6 @@ upb_ExtensionRegistry* GetUpbExtensions(
const ExtensionRegistry& extension_registry) {
return extension_registry.registry_;
}
} // namespace internal
} // namespace hpb
#endif

View file

@ -64,6 +64,13 @@ void ClearMessage(internal::PtrOrRawMutable<T> message) {
backend::ClearMessage(message);
}
// Note that the default extension registry is the the generated registry.
template <typename T>
hpb::StatusOr<T> Parse(absl::string_view bytes, ParseOptions options) {
return backend::Parse<T>(bytes, options);
}
#if HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_UPB
template <typename T>
ABSL_MUST_USE_RESULT bool Parse(internal::PtrOrRaw<T> message,
absl::string_view bytes,
@ -72,11 +79,6 @@ ABSL_MUST_USE_RESULT bool Parse(internal::PtrOrRaw<T> message,
return backend::Parse(message, bytes, extension_registry);
}
// Note that the default extension registry is the the generated registry.
template <typename T>
hpb::StatusOr<T> Parse(absl::string_view bytes, ParseOptions options) {
return backend::Parse<T>(bytes, options);
}
// Deprecated. Use the overload that returns hpb::StatusOr<T> instead.
// Note that the default extension registry is the empty registry.
@ -87,6 +89,7 @@ absl::StatusOr<T> Parse(absl::string_view bytes,
ExtensionRegistry::empty_registry()) {
return backend::Parse<T>(bytes, extension_registry);
}
#endif
template <typename T>
absl::StatusOr<absl::string_view> Serialize(internal::PtrOrRaw<T> message,

View file

@ -18,6 +18,10 @@
#if HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_UPB
#include "hpb/backend/upb/repeated_field.h"
#elif HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_CPP
#include "hpb/backend/cpp/repeated_field.h"
#else
#error "Unsupported backend"
#endif
namespace hpb {

View file

@ -65,6 +65,8 @@ void WriteHeader(const google::protobuf::FileDescriptor* file, Context& ctx) {
{{".", "::"}}),
"::protos")}},
R"cc(
#include "hpb/internal/internal.h"
// message stubs
namespace $namespace$ {