Skip to content

Commit

Permalink
Extract hpb::ExtensionRegistry, hpb::internal::{ExtensionMiniTablePro…
Browse files Browse the repository at this point in the history
…vider, ExtensionIdentifer} to extension.h

PiperOrigin-RevId: 668643168
  • Loading branch information
honglooker authored and copybara-github committed Aug 28, 2024
1 parent 0e95c9d commit 1b44ce0
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 92 deletions.
57 changes: 35 additions & 22 deletions hpb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,6 @@ load(

licenses(["notice"])

cc_library(
name = "repeated_field",
hdrs = [
"repeated_field.h",
"repeated_field_iterator.h",
],
compatible_with = ["//buildenv/target:non_prod"],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//visibility:public"],
deps = [
":hpb",
":traits",
"//hpb/backend/upb:interop",
"//upb:base",
"//upb:mem",
"//upb:message",
"//upb:message_copy",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/strings",
],
)

cc_library(
name = "hpb",
srcs = [
Expand All @@ -55,6 +33,7 @@ cc_library(
defines = ["HPB_BACKEND_UPB"],
visibility = ["//visibility:public"],
deps = [
"//hpb:extension",
"//hpb:ptr",
"//hpb/backend/upb",
"//hpb/backend/upb:interop",
Expand All @@ -76,6 +55,28 @@ cc_library(
],
)

cc_library(
name = "repeated_field",
hdrs = [
"repeated_field.h",
"repeated_field_iterator.h",
],
compatible_with = ["//buildenv/target:non_prod"],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//visibility:public"],
deps = [
":hpb",
":traits",
"//hpb/backend/upb:interop",
"//upb:base",
"//upb:mem",
"//upb:message",
"//upb:message_copy",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/strings",
],
)

# Internally used type traits.
cc_library(
name = "traits",
Expand Down Expand Up @@ -166,3 +167,15 @@ cc_library(
compatible_with = ["//buildenv/target:non_prod"],
visibility = ["//visibility:public"],
)

cc_library(
name = "extension",
srcs = ["extension.cc"],
hdrs = ["extension.h"],
compatible_with = ["//buildenv/target:non_prod"],
visibility = ["//visibility:public"],
deps = [
"//upb:mem",
"//upb:mini_table",
],
)
20 changes: 20 additions & 0 deletions hpb/extension.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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

#include "google/protobuf/hpb/extension.h"

#include "upb/mini_table/extension_registry.h"

namespace hpb {
namespace internal {
upb_ExtensionRegistry* GetUpbExtensions(
const ExtensionRegistry& extension_registry) {
return extension_registry.registry_;
}

} // namespace internal
} // namespace hpb
92 changes: 92 additions & 0 deletions hpb/extension.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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_EXTENSION_H__
#define GOOGLE_PROTOBUF_HPB_EXTENSION_H__

#include <cstdint>
#include <vector>

#include "upb/mem/arena.hpp"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/extension_registry.h"

namespace hpb {
class ExtensionRegistry;

namespace internal {

class ExtensionMiniTableProvider {
public:
constexpr explicit ExtensionMiniTableProvider(
const upb_MiniTableExtension* mini_table_ext)
: mini_table_ext_(mini_table_ext) {}
const upb_MiniTableExtension* mini_table_ext() const {
return mini_table_ext_;
}

private:
const upb_MiniTableExtension* mini_table_ext_;
};

// -------------------------------------------------------------------
// ExtensionIdentifier
// This is the type of actual extension objects. E.g. if you have:
// extend Foo {
// optional MyExtension bar = 1234;
// }
// then "bar" will be defined in C++ as:
// ExtensionIdentifier<Foo, MyExtension> bar(&namespace_bar_ext);
template <typename ExtendeeType, typename ExtensionType>
class ExtensionIdentifier : public ExtensionMiniTableProvider {
public:
using Extension = ExtensionType;
using Extendee = ExtendeeType;

constexpr explicit ExtensionIdentifier(
const upb_MiniTableExtension* mini_table_ext)
: ExtensionMiniTableProvider(mini_table_ext) {}

private:
constexpr uint32_t number() const {
return upb_MiniTableExtension_Number(mini_table_ext());
}
friend struct PrivateAccess;
};

upb_ExtensionRegistry* GetUpbExtensions(
const ExtensionRegistry& extension_registry);

} // namespace internal

class ExtensionRegistry {
public:
ExtensionRegistry(
const std::vector<const internal::ExtensionMiniTableProvider*>&
extensions,
const upb::Arena& arena)
: registry_(upb_ExtensionRegistry_New(arena.ptr())) {
if (registry_) {
for (const auto& ext_provider : extensions) {
const auto* ext = ext_provider->mini_table_ext();
bool success = upb_ExtensionRegistry_AddArray(registry_, &ext, 1);
if (!success) {
registry_ = nullptr;
break;
}
}
}
}

private:
friend upb_ExtensionRegistry* ::hpb::internal::GetUpbExtensions(
const ExtensionRegistry& extension_registry);
upb_ExtensionRegistry* registry_;
};
} // namespace hpb

#endif // GOOGLE_PROTOBUF_HPB_EXTENSION_H__
5 changes: 0 additions & 5 deletions hpb/hpb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ absl::Status MessageDecodeError(upb_DecodeStatus status, SourceLocation loc

namespace internal {

upb_ExtensionRegistry* GetUpbExtensions(
const ExtensionRegistry& extension_registry) {
return extension_registry.registry_;
}

/**
* MessageLock(msg) acquires lock on msg when constructed and releases it when
* destroyed.
Expand Down
66 changes: 1 addition & 65 deletions hpb/hpb.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/hpb/backend/upb/interop.h"
#include "google/protobuf/hpb/extension.h"
#include "google/protobuf/hpb/internal/internal.h"
#include "google/protobuf/hpb/internal/template_help.h"
#include "google/protobuf/hpb/ptr.h"
Expand Down Expand Up @@ -77,43 +78,6 @@ absl::Status MessageEncodeError(upb_EncodeStatus status,
SourceLocation loc = SourceLocation::current());

namespace internal {
class ExtensionMiniTableProvider {
public:
constexpr explicit ExtensionMiniTableProvider(
const upb_MiniTableExtension* mini_table_ext)
: mini_table_ext_(mini_table_ext) {}
const upb_MiniTableExtension* mini_table_ext() const {
return mini_table_ext_;
}

private:
const upb_MiniTableExtension* mini_table_ext_;
};

// -------------------------------------------------------------------
// ExtensionIdentifier
// This is the type of actual extension objects. E.g. if you have:
// extend Foo {
// optional MyExtension bar = 1234;
// }
// then "bar" will be defined in C++ as:
// ExtensionIdentifier<Foo, MyExtension> bar(&namespace_bar_ext);
template <typename ExtendeeType, typename ExtensionType>
class ExtensionIdentifier : public ExtensionMiniTableProvider {
public:
using Extension = ExtensionType;
using Extendee = ExtendeeType;

constexpr explicit ExtensionIdentifier(
const upb_MiniTableExtension* mini_table_ext)
: ExtensionMiniTableProvider(mini_table_ext) {}

private:
constexpr uint32_t number() const {
return upb_MiniTableExtension_Number(mini_table_ext());
}
friend class PrivateAccess;
};

template <typename T>
upb_Arena* GetArena(Ptr<T> message) {
Expand All @@ -125,9 +89,6 @@ upb_Arena* GetArena(T* message) {
return static_cast<upb_Arena*>(message->GetInternalArena());
}

upb_ExtensionRegistry* GetUpbExtensions(
const ExtensionRegistry& extension_registry);

absl::StatusOr<absl::string_view> Serialize(const upb_Message* message,
const upb_MiniTable* mini_table,
upb_Arena* arena, int options);
Expand Down Expand Up @@ -158,31 +119,6 @@ absl::Status SetExtension(upb_Message* message, upb_Arena* message_arena,
namespace backend = ::hpb::internal::backend::upb;
#endif

class ExtensionRegistry {
public:
ExtensionRegistry(
const std::vector<const internal::ExtensionMiniTableProvider*>&
extensions,
const upb::Arena& arena)
: registry_(upb_ExtensionRegistry_New(arena.ptr())) {
if (registry_) {
for (const auto& ext_provider : extensions) {
const auto* ext = ext_provider->mini_table_ext();
bool success = upb_ExtensionRegistry_AddArray(registry_, &ext, 1);
if (!success) {
registry_ = nullptr;
break;
}
}
}
}

private:
friend upb_ExtensionRegistry* ::hpb::internal::GetUpbExtensions(
const ExtensionRegistry& extension_registry);
upb_ExtensionRegistry* registry_;
};

template <typename T, typename Extendee, typename Extension,
typename = hpb::internal::EnableIfHpbClass<T>>
ABSL_MUST_USE_RESULT bool HasExtension(
Expand Down

0 comments on commit 1b44ce0

Please sign in to comment.