Migrate all of `protos::internal` into `hpb::internal`

`hpb.cc` has also been transformed to be 100% hpb.

PiperOrigin-RevId: 657183219
pull/17594/head
Hong Shin 4 months ago committed by Copybara-Service
parent 5854e2b6a8
commit 9f5aed39ea
  1. 2
      hpb/extension_lock_test.cc
  2. 8
      hpb/hpb.cc
  3. 155
      hpb/hpb.h
  4. 4
      hpb/repeated_field.h
  5. 29
      hpb_generator/gen_messages.cc
  6. 6
      hpb_generator/tests/test_generated.cc
  7. 10
      protos/protos.h

@ -54,7 +54,7 @@ void unlock_func(const void* msg) { m[absl::HashOf(msg) & 0x7].unlock(); }
return &unlock_func;
}
void TestConcurrentExtensionAccess(::protos::ExtensionRegistry registry) {
void TestConcurrentExtensionAccess(::hpb::ExtensionRegistry registry) {
::hpb::internal::upb_extension_locker_global.store(&lock_func,
std::memory_order_release);
const std::string payload = GenerateTestData();

@ -80,15 +80,9 @@ absl::Status MessageDecodeError(upb_DecodeStatus status, SourceLocation loc
// return absl::Status(absl::StatusCode::kUnknown, "Upb message parse error");
// }
// end:github_only
} // namespace hpb
namespace protos {
namespace internal {
using ::hpb::internal::upb_extension_locker_global;
using ::hpb::internal::UpbExtensionLocker;
using ::hpb::internal::UpbExtensionUnlocker;
upb_ExtensionRegistry* GetUpbExtensions(
const ExtensionRegistry& extension_registry) {
return extension_registry.registry_;
@ -188,4 +182,4 @@ absl::Status SetExtension(upb_Message* message, upb_Arena* message_arena,
} // namespace internal
} // namespace protos
} // namespace hpb

@ -23,6 +23,7 @@
#include "upb/wire/encode.h"
namespace hpb {
class ExtensionRegistry;
using Arena = ::upb::Arena;
template <typename T>
@ -232,22 +233,6 @@ class ExtensionIdentifier : public ExtensionMiniTableProvider {
friend class PrivateAccess;
};
} // namespace internal
} // namespace hpb
namespace protos {
using hpb::Arena;
using hpb::ExtensionNotFoundError;
using hpb::MessageAllocationError;
using hpb::MessageDecodeError;
using hpb::MessageEncodeError;
using hpb::Ptr;
using hpb::SourceLocation;
class ExtensionRegistry;
namespace internal {
template <typename T>
upb_Arena* GetArena(Ptr<T> message) {
return static_cast<upb_Arena*>(message->GetInternalArena());
@ -294,8 +279,45 @@ absl::Status MoveExtension(upb_Message* message, upb_Arena* message_arena,
absl::Status SetExtension(upb_Message* message, upb_Arena* message_arena,
const upb_MiniTableExtension* ext,
const upb_Message* extension);
} // 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
namespace protos {
using hpb::Arena;
using hpb::ExtensionNotFoundError;
using hpb::MessageAllocationError;
using hpb::MessageDecodeError;
using hpb::MessageEncodeError;
using hpb::Ptr;
using hpb::SourceLocation;
template <typename T>
typename T::Proxy CreateMessage(::hpb::Arena& arena) {
return typename T::Proxy(upb_Message_New(T::minitable(), arena.ptr()),
@ -305,7 +327,7 @@ typename T::Proxy CreateMessage(::hpb::Arena& arena) {
template <typename T>
void DeepCopy(Ptr<const T> source_message, Ptr<T> target_message) {
static_assert(!std::is_const_v<T>);
::protos::internal::DeepCopy(
::hpb::internal::DeepCopy(
hpb::internal::GetInternalMsg(target_message),
hpb::internal::GetInternalMsg(source_message), T::minitable(),
static_cast<upb_Arena*>(target_message->GetInternalArena()));
@ -313,9 +335,9 @@ void DeepCopy(Ptr<const T> source_message, Ptr<T> target_message) {
template <typename T>
typename T::Proxy CloneMessage(Ptr<T> message, upb_Arena* arena) {
return hpb::internal::PrivateAccess::Proxy<T>(
::protos::internal::DeepClone(hpb::internal::GetInternalMsg(message),
T::minitable(), arena),
return ::hpb::internal::PrivateAccess::Proxy<T>(
::hpb::internal::DeepClone(::hpb::internal::GetInternalMsg(message),
T::minitable(), arena),
arena);
}
@ -340,41 +362,16 @@ 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 = internal::GetMiniTable(ptr);
auto minitable = hpb::internal::GetMiniTable(ptr);
upb_Message_Clear(hpb::internal::GetInternalMsg(ptr), minitable);
}
class ExtensionRegistry {
public:
ExtensionRegistry(
const std::vector<const ::hpb::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* ::protos::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(
Ptr<T> message,
const ::hpb::internal::ExtensionIdentifier<Extendee, Extension>& id) {
return ::protos::internal::HasExtensionOrUnknown(
return ::hpb::internal::HasExtensionOrUnknown(
::hpb::internal::GetInternalMsg(message), id.mini_table_ext());
}
@ -413,9 +410,9 @@ absl::Status SetExtension(
const Extension& value) {
static_assert(!std::is_const_v<T>);
auto* message_arena = static_cast<upb_Arena*>(message->GetInternalArena());
return ::protos::internal::SetExtension(
hpb::internal::GetInternalMsg(message), message_arena,
id.mini_table_ext(), hpb::internal::GetInternalMsg(&value));
return ::hpb::internal::SetExtension(hpb::internal::GetInternalMsg(message),
message_arena, id.mini_table_ext(),
hpb::internal::GetInternalMsg(&value));
}
template <typename T, typename Extension,
@ -427,9 +424,9 @@ absl::Status SetExtension(
Ptr<Extension> value) {
static_assert(!std::is_const_v<T>);
auto* message_arena = static_cast<upb_Arena*>(message->GetInternalArena());
return ::protos::internal::SetExtension(
hpb::internal::GetInternalMsg(message), message_arena,
id.mini_table_ext(), hpb::internal::GetInternalMsg(value));
return ::hpb::internal::SetExtension(hpb::internal::GetInternalMsg(message),
message_arena, id.mini_table_ext(),
hpb::internal::GetInternalMsg(value));
}
template <typename T, typename Extension,
@ -443,10 +440,10 @@ absl::Status SetExtension(
static_assert(!std::is_const_v<T>);
auto* message_arena = static_cast<upb_Arena*>(message->GetInternalArena());
auto* extension_arena = static_cast<upb_Arena*>(ext.GetInternalArena());
return ::protos::internal::MoveExtension(
hpb::internal::GetInternalMsg(message), message_arena,
id.mini_table_ext(), hpb::internal::GetInternalMsg(&ext),
extension_arena);
return ::hpb::internal::MoveExtension(hpb::internal::GetInternalMsg(message),
message_arena, id.mini_table_ext(),
hpb::internal::GetInternalMsg(&ext),
extension_arena);
}
template <typename T, typename Extension,
@ -481,15 +478,15 @@ absl::StatusOr<Ptr<const Extension>> GetExtension(
const ::hpb::internal::ExtensionIdentifier<Extendee, Extension>& id) {
// TODO: Fix const correctness issues.
upb_MessageValue value;
const bool ok = ::protos::internal::GetOrPromoteExtension(
const bool ok = ::hpb::internal::GetOrPromoteExtension(
const_cast<upb_Message*>(::hpb::internal::GetInternalMsg(message)),
id.mini_table_ext(), ::protos::internal::GetArena(message), &value);
id.mini_table_ext(), ::hpb::internal::GetArena(message), &value);
if (!ok) {
return ExtensionNotFoundError(
upb_MiniTableExtension_Number(id.mini_table_ext()));
}
return Ptr<const Extension>(::hpb::internal::CreateMessage<Extension>(
(upb_Message*)value.msg_val, ::protos::internal::GetArena(message)));
(upb_Message*)value.msg_val, ::hpb::internal::GetArena(message)));
}
template <typename T, typename Extendee, typename Extension,
@ -504,11 +501,11 @@ 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),
::protos::internal::GetMiniTable(message));
::hpb::internal::GetMiniTable(message));
auto* arena = static_cast<upb_Arena*>(message->GetInternalArena());
return upb_Decode(bytes.data(), bytes.size(),
::hpb::internal::GetInternalMsg(message),
::protos::internal::GetMiniTable(message),
::hpb::internal::GetMiniTable(message),
/* extreg= */ nullptr, /* options= */ 0,
arena) == kUpb_DecodeStatus_Ok;
}
@ -516,23 +513,23 @@ ABSL_MUST_USE_RESULT bool Parse(Ptr<T> message, absl::string_view bytes) {
template <typename T>
ABSL_MUST_USE_RESULT bool Parse(
Ptr<T> message, absl::string_view bytes,
const ::protos::ExtensionRegistry& extension_registry) {
const ::hpb::ExtensionRegistry& extension_registry) {
static_assert(!std::is_const_v<T>);
upb_Message_Clear(::hpb::internal::GetInternalMsg(message),
::protos::internal::GetMiniTable(message));
::hpb::internal::GetMiniTable(message));
auto* arena = static_cast<upb_Arena*>(message->GetInternalArena());
return upb_Decode(bytes.data(), bytes.size(),
::hpb::internal::GetInternalMsg(message),
::protos::internal::GetMiniTable(message),
::hpb::internal::GetMiniTable(message),
/* extreg= */
::protos::internal::GetUpbExtensions(extension_registry),
::hpb::internal::GetUpbExtensions(extension_registry),
/* options= */ 0, arena) == kUpb_DecodeStatus_Ok;
}
template <typename T>
ABSL_MUST_USE_RESULT bool Parse(
T* message, absl::string_view bytes,
const ::protos::ExtensionRegistry& extension_registry) {
const ::hpb::ExtensionRegistry& extension_registry) {
static_assert(!std::is_const_v<T>);
return Parse(Ptr(message, bytes, extension_registry));
}
@ -541,11 +538,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),
::protos::internal::GetMiniTable(message));
::hpb::internal::GetMiniTable(message));
auto* arena = static_cast<upb_Arena*>(message->GetInternalArena());
return upb_Decode(bytes.data(), bytes.size(),
::hpb::internal::GetInternalMsg(message),
::protos::internal::GetMiniTable(message),
::hpb::internal::GetMiniTable(message),
/* extreg= */ nullptr, /* options= */ 0,
arena) == kUpb_DecodeStatus_Ok;
}
@ -556,7 +553,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(),
::protos::internal::GetMiniTable(&message),
::hpb::internal::GetMiniTable(&message),
/* extreg= */ nullptr, /* options= */ 0, arena);
if (status == kUpb_DecodeStatus_Ok) {
return message;
@ -566,14 +563,14 @@ absl::StatusOr<T> Parse(absl::string_view bytes, int options = 0) {
template <typename T>
absl::StatusOr<T> Parse(absl::string_view bytes,
const ::protos::ExtensionRegistry& extension_registry,
const ::hpb::ExtensionRegistry& extension_registry,
int options = 0) {
T message;
auto* arena = static_cast<upb_Arena*>(message.GetInternalArena());
upb_DecodeStatus status =
upb_Decode(bytes.data(), bytes.size(), message.msg(),
::protos::internal::GetMiniTable(&message),
::protos::internal::GetUpbExtensions(extension_registry),
::hpb::internal::GetMiniTable(&message),
::hpb::internal::GetUpbExtensions(extension_registry),
/* options= */ 0, arena);
if (status == kUpb_DecodeStatus_Ok) {
return message;
@ -584,17 +581,17 @@ absl::StatusOr<T> Parse(absl::string_view bytes,
template <typename T>
absl::StatusOr<absl::string_view> Serialize(const T* message, upb::Arena& arena,
int options = 0) {
return ::protos::internal::Serialize(
::hpb::internal::GetInternalMsg(message),
::protos::internal::GetMiniTable(message), arena.ptr(), options);
return ::hpb::internal::Serialize(::hpb::internal::GetInternalMsg(message),
::hpb::internal::GetMiniTable(message),
arena.ptr(), options);
}
template <typename T>
absl::StatusOr<absl::string_view> Serialize(Ptr<T> message, upb::Arena& arena,
int options = 0) {
return ::protos::internal::Serialize(
::hpb::internal::GetInternalMsg(message),
::protos::internal::GetMiniTable(message), arena.ptr(), options);
return ::hpb::internal::Serialize(::hpb::internal::GetInternalMsg(message),
::hpb::internal::GetMiniTable(message),
arena.ptr(), options);
}
template <typename T, typename Extension>

@ -120,7 +120,7 @@ class RepeatedFieldProxy
upb_MessageValue message_value;
message_value.msg_val = upb_Message_DeepClone(
::hpb::internal::PrivateAccess::GetInternalMsg(&t),
::protos::internal::GetMiniTable(&t), this->arena_);
::hpb::internal::GetMiniTable(&t), this->arena_);
upb_Array_Append(this->arr_, message_value, this->arena_);
}
@ -131,7 +131,7 @@ class RepeatedFieldProxy
upb_MessageValue message_value;
message_value.msg_val =
::hpb::internal::PrivateAccess::GetInternalMsg(&msg);
upb_Arena_Fuse(::protos::internal::GetArena(&msg), this->arena_);
upb_Arena_Fuse(::hpb::internal::GetArena(&msg), this->arena_);
upb_Array_Append(this->arr_, message_value, this->arena_);
T moved_msg = std::move(msg);
}

@ -245,10 +245,9 @@ void WriteModelPublicDeclaration(
int options));
friend absl::StatusOr<$2>(::protos::Parse<$2>(
absl::string_view bytes,
const ::protos::ExtensionRegistry& extension_registry,
int options));
friend upb_Arena* ::protos::internal::GetArena<$0>($0* message);
friend upb_Arena* ::protos::internal::GetArena<$0>(::hpb::Ptr<$0> message);
const ::hpb::ExtensionRegistry& extension_registry, int options));
friend upb_Arena* ::hpb::internal::GetArena<$0>($0* message);
friend upb_Arena* ::hpb::internal::GetArena<$0>(::hpb::Ptr<$0> message);
friend $0(::hpb::internal::MoveMessage<$0>(upb_Message* msg, upb_Arena* arena));
)cc",
ClassName(descriptor), MessageName(descriptor),
@ -303,12 +302,12 @@ 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* ::protos::internal::GetMiniTable<$0Proxy>(
friend const upb_MiniTable* ::hpb::internal::GetMiniTable<$0Proxy>(
const $0Proxy* message);
friend const upb_MiniTable* ::protos::internal::GetMiniTable<$0Proxy>(
friend const upb_MiniTable* ::hpb::internal::GetMiniTable<$0Proxy>(
::hpb::Ptr<$0Proxy> message);
friend upb_Arena* ::protos::internal::GetArena<$2>($2* message);
friend upb_Arena* ::protos::internal::GetArena<$2>(::hpb::Ptr<$2> message);
friend upb_Arena* ::hpb::internal::GetArena<$2>($2* message);
friend upb_Arena* ::hpb::internal::GetArena<$2>(::hpb::Ptr<$2> message);
static void Rebind($0Proxy& lhs, const $0Proxy& rhs) {
lhs.msg_ = rhs.msg_;
lhs.arena_ = rhs.arena_;
@ -329,7 +328,7 @@ void WriteModelCProxyDeclaration(const protobuf::Descriptor* descriptor,
public:
$0CProxy() = delete;
$0CProxy(const $0* m)
: internal::$0Access(m->msg_, ::protos::internal::GetArena(m)) {}
: internal::$0Access(m->msg_, ::hpb::internal::GetArena(m)) {}
$0CProxy($0Proxy m);
using $0Access::GetInternalArena;
)cc",
@ -352,9 +351,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* ::protos::internal::GetMiniTable<$0CProxy>(
friend const upb_MiniTable* ::hpb::internal::GetMiniTable<$0CProxy>(
const $0CProxy* message);
friend const upb_MiniTable* ::protos::internal::GetMiniTable<$0CProxy>(
friend const upb_MiniTable* ::hpb::internal::GetMiniTable<$0CProxy>(
::hpb::Ptr<$0CProxy> message);
static void Rebind($0CProxy& lhs, const $0CProxy& rhs) {
@ -388,11 +387,11 @@ void WriteMessageImplementation(
}
$0::$0(const $0& from) : $0Access() {
arena_ = owned_arena_.ptr();
msg_ = ($1*)::protos::internal::DeepClone(UPB_UPCAST(from.msg_), &$2, arena_);
msg_ = ($1*)::hpb::internal::DeepClone(UPB_UPCAST(from.msg_), &$2, arena_);
}
$0::$0(const CProxy& from) : $0Access() {
arena_ = owned_arena_.ptr();
msg_ = ($1*)::protos::internal::DeepClone(
msg_ = ($1*)::hpb::internal::DeepClone(
::hpb::internal::GetInternalMsg(&from), &$2, arena_);
}
$0::$0(const Proxy& from) : $0(static_cast<const CProxy&>(from)) {}
@ -402,12 +401,12 @@ void WriteMessageImplementation(
}
$0& $0::operator=(const $3& from) {
arena_ = owned_arena_.ptr();
msg_ = ($1*)::protos::internal::DeepClone(UPB_UPCAST(from.msg_), &$2, arena_);
msg_ = ($1*)::hpb::internal::DeepClone(UPB_UPCAST(from.msg_), &$2, arena_);
return *this;
}
$0& $0::operator=(const CProxy& from) {
arena_ = owned_arena_.ptr();
msg_ = ($1*)::protos::internal::DeepClone(
msg_ = ($1*)::hpb::internal::DeepClone(
::hpb::internal::GetInternalMsg(&from), &$2, arena_);
return *this;
}

@ -797,7 +797,7 @@ TEST(CppGeneratedCode, SetExtensionFusingFailureShouldCopy) {
ThemeExtension extension1;
extension1.set_ext_name("Hello World");
ASSERT_FALSE(
upb_Arena_Fuse(arena.ptr(), ::protos::internal::GetArena(&extension1)));
upb_Arena_Fuse(arena.ptr(), ::hpb::internal::GetArena(&extension1)));
EXPECT_FALSE(::protos::HasExtension(model, theme));
auto status = ::protos::SetExtension(model, theme, std::move(extension1));
EXPECT_TRUE(status.ok());
@ -965,7 +965,7 @@ TEST(CppGeneratedCode, ParseWithExtensionRegistry) {
::upb::Arena arena;
auto bytes = ::protos::Serialize(&model, arena);
EXPECT_EQ(true, bytes.ok());
::protos::ExtensionRegistry extensions(
::hpb::ExtensionRegistry extensions(
{&theme, &other_ext, &ThemeExtension::theme_extension}, arena);
TestModel parsed_model =
::protos::Parse<TestModel>(bytes.value(), extensions).value();
@ -1230,7 +1230,7 @@ TEST(CppGeneratedCode, HasExtensionAndRegistry) {
std::string data = std::string(::protos::Serialize(&source, arena).value());
// Test with ExtensionRegistry
::protos::ExtensionRegistry extensions({&theme}, arena);
::hpb::ExtensionRegistry extensions({&theme}, arena);
TestModel parsed_model = ::protos::Parse<TestModel>(data, extensions).value();
EXPECT_TRUE(::protos::HasExtension(&parsed_model, theme));
}

@ -12,9 +12,19 @@ namespace protos {
namespace internal {
using hpb::internal::CreateMessage;
using hpb::internal::CreateMessageProxy;
using hpb::internal::DeepClone;
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;
} // namespace internal
} // namespace protos
#endif

Loading…
Cancel
Save