Skip to content

Commit

Permalink
Automated rollback of commit 9a9e5cf.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 673940081
  • Loading branch information
honglooker authored and copybara-github committed Sep 12, 2024
1 parent 1142838 commit 4c13f48
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions hpb/hpb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ absl::StatusOr<absl::string_view> Serialize(const upb_Message* message,
return MessageEncodeError(status);
}

void DeepCopy(upb_Message* target, const upb_Message* source,
const upb_MiniTable* mini_table, upb_Arena* arena) {
MessageLock msg_lock(source);
upb_Message_DeepCopy(target, source, mini_table, arena);
}

upb_Message* DeepClone(const upb_Message* source,
const upb_MiniTable* mini_table, upb_Arena* arena) {
MessageLock msg_lock(source);
Expand Down
30 changes: 30 additions & 0 deletions hpb/hpb.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ bool HasExtensionOrUnknown(const upb_Message* msg,
bool GetOrPromoteExtension(upb_Message* msg, const upb_MiniTableExtension* eid,
upb_Arena* arena, upb_MessageValue* value);

void DeepCopy(upb_Message* target, const upb_Message* source,
const upb_MiniTable* mini_table, upb_Arena* arena);

upb_Message* DeepClone(const upb_Message* source,
const upb_MiniTable* mini_table, upb_Arena* arena);

Expand Down Expand Up @@ -250,6 +253,33 @@ typename T::Proxy CloneMessage(Ptr<T> message, upb_Arena* arena) {
arena);
}

template <typename T>
void DeepCopy(Ptr<const T> source_message, Ptr<T> target_message) {
static_assert(!std::is_const_v<T>);
::hpb::internal::DeepCopy(
hpb::interop::upb::GetMessage(target_message),
hpb::interop::upb::GetMessage(source_message), T::minitable(),
static_cast<upb_Arena*>(target_message->GetInternalArena()));
}

template <typename T>
void DeepCopy(Ptr<const T> source_message, T* target_message) {
static_assert(!std::is_const_v<T>);
DeepCopy(source_message, Ptr(target_message));
}

template <typename T>
void DeepCopy(const T* source_message, Ptr<T> target_message) {
static_assert(!std::is_const_v<T>);
DeepCopy(Ptr(source_message), target_message);
}

template <typename T>
void DeepCopy(const T* source_message, T* target_message) {
static_assert(!std::is_const_v<T>);
DeepCopy(Ptr(source_message), Ptr(target_message));
}

template <typename T>
void ClearMessage(hpb::internal::PtrOrRaw<T> message) {
backend::ClearMessage(message);
Expand Down
19 changes: 19 additions & 0 deletions hpb_generator/tests/test_generated.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,25 @@ TEST(CppGeneratedCode, CannotInvokeClearMessageWithConstRawPtr) {
EXPECT_FALSE(CanCallClearMessage<const TestModel*>());
}

TEST(CppGeneratedCode, DeepCopy) {
// Fill model.
TestModel model;
model.set_int64(5);
model.set_str2("Hello");
auto new_child = model.add_child_models();
ASSERT_TRUE(new_child.ok());
new_child.value()->set_child_str1("text in child");
ThemeExtension extension1;
extension1.set_ext_name("name in extension");
EXPECT_TRUE(::hpb::SetExtension(&model, theme, extension1).ok());
TestModel target;
target.set_b1(true);
::hpb::DeepCopy(&model, &target);
EXPECT_FALSE(target.b1()) << "Target was not cleared before copying content ";
EXPECT_EQ(target.str2(), "Hello");
EXPECT_TRUE(::hpb::HasExtension(&target, theme));
}

TEST(CppGeneratedCode, HasExtensionAndRegistry) {
// Fill model.
TestModel source;
Expand Down

0 comments on commit 4c13f48

Please sign in to comment.