Skip to content

Commit

Permalink
Remove C++ form of YGNodeSetChildren (#37013)
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/react-native#37013

Pull Request resolved: facebook#1254

Brings Yoga public interface back to a nice pure C ABI.

Changelog: [Internal]

Reviewed By: rshest

Differential Revision: D45138827

fbshipit-source-id: 8df7e4fd03afcda9a714d193b0430c122a7a7574
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Apr 21, 2023
1 parent 7afddfd commit 85ff2f0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 46 deletions.
32 changes: 17 additions & 15 deletions tests/YGTreeMutationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ TEST(YogaTest, set_children_adds_children_to_parent) {
YGNodeRef const root_child0 = YGNodeNew();
YGNodeRef const root_child1 = YGNodeNew();

YGNodeSetChildren(root, {root_child0, root_child1});
YGNodeRef children[] = {root_child0, root_child1};
YGNodeSetChildren(root, children, 2);

const std::vector<YGNodeRef> children = getChildren(root);
const std::vector<YGNodeRef> expectedChildren = {root_child0, root_child1};
ASSERT_EQ(children, expectedChildren);
ASSERT_EQ(getChildren(root), expectedChildren);

const std::vector<YGNodeRef> owners = {
YGNodeGetOwner(root_child0), YGNodeGetOwner(root_child1)};
Expand All @@ -42,12 +42,12 @@ TEST(YogaTest, set_children_to_empty_removes_old_children) {
YGNodeRef const root_child0 = YGNodeNew();
YGNodeRef const root_child1 = YGNodeNew();

YGNodeSetChildren(root, {root_child0, root_child1});
YGNodeSetChildren(root, {});
YGNodeRef children[] = {root_child0, root_child1};
YGNodeSetChildren(root, children, 2);
YGNodeSetChildren(root, nullptr, 0);

const std::vector<YGNodeRef> children = getChildren(root);
const std::vector<YGNodeRef> expectedChildren = {};
ASSERT_EQ(children, expectedChildren);
ASSERT_EQ(getChildren(root), expectedChildren);

const std::vector<YGNodeRef> owners = {
YGNodeGetOwner(root_child0), YGNodeGetOwner(root_child1)};
Expand All @@ -62,16 +62,17 @@ TEST(YogaTest, set_children_replaces_non_common_children) {
YGNodeRef const root_child0 = YGNodeNew();
YGNodeRef const root_child1 = YGNodeNew();

YGNodeSetChildren(root, {root_child0, root_child1});
YGNodeRef children1[] = {root_child0, root_child1};
YGNodeSetChildren(root, children1, 2);

YGNodeRef const root_child2 = YGNodeNew();
YGNodeRef const root_child3 = YGNodeNew();

YGNodeSetChildren(root, {root_child2, root_child3});
YGNodeRef children2[] = {root_child2, root_child3};
YGNodeSetChildren(root, children2, 2);

const std::vector<YGNodeRef> children = getChildren(root);
const std::vector<YGNodeRef> expectedChildren = {root_child2, root_child3};
ASSERT_EQ(children, expectedChildren);
ASSERT_EQ(getChildren(root), expectedChildren);

const std::vector<YGNodeRef> owners = {
YGNodeGetOwner(root_child0), YGNodeGetOwner(root_child1)};
Expand All @@ -89,16 +90,17 @@ TEST(YogaTest, set_children_keeps_and_reorders_common_children) {
YGNodeRef const root_child1 = YGNodeNew();
YGNodeRef const root_child2 = YGNodeNew();

YGNodeSetChildren(root, {root_child0, root_child1, root_child2});
YGNodeRef children1[] = {root_child0, root_child1, root_child2};
YGNodeSetChildren(root, children1, 3);

YGNodeRef const root_child3 = YGNodeNew();

YGNodeSetChildren(root, {root_child2, root_child1, root_child3});
YGNodeRef children2[] = {root_child2, root_child1, root_child3};
YGNodeSetChildren(root, children2, 3);

const std::vector<YGNodeRef> children = getChildren(root);
const std::vector<YGNodeRef> expectedChildren = {
root_child2, root_child1, root_child3};
ASSERT_EQ(children, expectedChildren);
ASSERT_EQ(getChildren(root), expectedChildren);

const std::vector<YGNodeRef> owners = {
YGNodeGetOwner(root_child0),
Expand Down
33 changes: 11 additions & 22 deletions yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,16 @@ YOGA_EXPORT void YGNodeRemoveAllChildren(const YGNodeRef owner) {
owner->markDirtyAndPropagate();
}

static void YGNodeSetChildrenInternal(
YGNodeRef const owner,
const std::vector<YGNodeRef>& children) {
YOGA_EXPORT void YGNodeSetChildren(
const YGNodeRef owner,
const YGNodeRef* children,
const uint32_t count) {
if (!owner) {
return;
}
if (children.size() == 0) {

const YGVector childrenVector = {children, children + count};
if (childrenVector.size() == 0) {
if (YGNodeGetChildCount(owner) > 0) {
for (YGNodeRef const child : owner->getChildren()) {
child->setLayout(YGLayout());
Expand All @@ -399,35 +402,21 @@ static void YGNodeSetChildrenInternal(
for (YGNodeRef const oldChild : owner->getChildren()) {
// Our new children may have nodes in common with the old children. We
// don't reset these common nodes.
if (std::find(children.begin(), children.end(), oldChild) ==
children.end()) {
if (std::find(childrenVector.begin(), childrenVector.end(), oldChild) ==
childrenVector.end()) {
oldChild->setLayout(YGLayout());
oldChild->setOwner(nullptr);
}
}
}
owner->setChildren(children);
for (YGNodeRef child : children) {
owner->setChildren(childrenVector);
for (YGNodeRef child : childrenVector) {
child->setOwner(owner);
}
owner->markDirtyAndPropagate();
}
}

YOGA_EXPORT void YGNodeSetChildren(
const YGNodeRef owner,
const YGNodeRef c[],
const uint32_t count) {
const YGVector children = {c, c + count};
YGNodeSetChildrenInternal(owner, children);
}

YOGA_EXPORT void YGNodeSetChildren(
YGNodeRef const owner,
const std::vector<YGNodeRef>& children) {
YGNodeSetChildrenInternal(owner, children);
}

YOGA_EXPORT YGNodeRef
YGNodeGetChild(const YGNodeRef node, const uint32_t index) {
if (index < node->getChildren().size()) {
Expand Down
10 changes: 1 addition & 9 deletions yoga/Yoga.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ WIN_EXPORT YGNodeRef YGNodeGetParent(YGNodeRef node);
WIN_EXPORT uint32_t YGNodeGetChildCount(YGNodeRef node);
WIN_EXPORT void YGNodeSetChildren(
YGNodeRef owner,
const YGNodeRef children[],
const YGNodeRef* children,
uint32_t count);

WIN_EXPORT void YGNodeSetIsReferenceBaseline(
Expand Down Expand Up @@ -364,11 +364,3 @@ WIN_EXPORT float YGRoundValueToPixelGrid(
bool forceFloor);

YG_EXTERN_C_END

#ifdef __cplusplus

#include <vector>

void YGNodeSetChildren(YGNodeRef owner, const std::vector<YGNodeRef>& children);

#endif

0 comments on commit 85ff2f0

Please sign in to comment.