Skip to content

Commit

Permalink
Remove yoga::Style::BitfieldRef (#1459)
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/react-native#41393

Pull Request resolved: #1459

Removes the last of the non setter-style style setters.

Changelog: [Internal]

Reviewed By: javache

Differential Revision: D51155925

fbshipit-source-id: 2921c87d95ad36495b7013e592d5169015321545
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Nov 28, 2023
1 parent ed6e914 commit 9b0fd09
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 129 deletions.
132 changes: 52 additions & 80 deletions yoga/YGNodeStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,26 @@ using namespace facebook::yoga;

namespace {

template <typename T, typename NeedsUpdate, typename Update>
void updateStyle(
yoga::Node* node,
T value,
NeedsUpdate&& needsUpdate,
Update&& update) {
if (needsUpdate(node->getStyle(), value)) {
update(node->getStyle(), value);
node->markDirtyAndPropagate();
}
}

template <typename Ref, typename ValueT>
void updateStyle(YGNodeRef node, Ref (Style::*prop)(), ValueT value) {
updateStyle(
resolveRef(node),
value,
[prop](Style& s, ValueT x) { return (s.*prop)() != x; },
[prop](Style& s, ValueT x) { (s.*prop)() = x; });
}

template <auto GetterT, auto SetterT, typename ValueT>
void updateStyle(YGNodeRef node, ValueT value) {
updateStyle(
resolveRef(node),
value,
[](Style& s, ValueT x) { return (s.*GetterT)() != x; },
[](Style& s, ValueT x) { (s.*SetterT)(x); });
auto& style = resolveRef(node)->getStyle();
if ((style.*GetterT)() != value) {
(style.*SetterT)(value);
resolveRef(node)->markDirtyAndPropagate();
}
}

template <auto GetterT, auto SetterT, typename IdxT, typename ValueT>
void updateIndexedStyleProp(YGNodeRef node, IdxT idx, ValueT value) {
updateStyle(
resolveRef(node),
value,
[idx](Style& s, ValueT x) { return (s.*GetterT)(idx) != x; },
[idx](Style& s, ValueT x) { (s.*SetterT)(idx, x); });
void updateStyle(YGNodeRef node, IdxT idx, ValueT value) {
auto& style = resolveRef(node)->getStyle();
if ((style.*GetterT)(idx) != value) {
(style.*SetterT)(idx, value);
resolveRef(node)->markDirtyAndPropagate();
}
}

} // namespace

// MSVC has trouble inferring the return type of pointer to member functions
// with const and non-const overloads, instead of preferring the non-const
// overload like clang and GCC. For the purposes of updateStyle(), we can help
// MSVC by specifying that return type explicitly. In combination with
// decltype, MSVC will prefer the non-const version.
#define MSVC_HINT(PROP) decltype(Style{}.PROP())

void YGNodeCopyStyle(
const YGNodeRef dstNodeRef,
const YGNodeConstRef srcNodeRef) {
Expand All @@ -75,7 +47,7 @@ void YGNodeCopyStyle(
}

void YGNodeStyleSetDirection(const YGNodeRef node, const YGDirection value) {
updateStyle<MSVC_HINT(direction)>(node, &Style::direction, scopedEnum(value));
updateStyle<&Style::direction, &Style::setDirection>(node, scopedEnum(value));
}

YGDirection YGNodeStyleGetDirection(const YGNodeConstRef node) {
Expand All @@ -85,8 +57,8 @@ YGDirection YGNodeStyleGetDirection(const YGNodeConstRef node) {
void YGNodeStyleSetFlexDirection(
const YGNodeRef node,
const YGFlexDirection flexDirection) {
updateStyle<MSVC_HINT(flexDirection)>(
node, &Style::flexDirection, scopedEnum(flexDirection));
updateStyle<&Style::flexDirection, &Style::setFlexDirection>(
node, scopedEnum(flexDirection));
}

YGFlexDirection YGNodeStyleGetFlexDirection(const YGNodeConstRef node) {
Expand All @@ -96,8 +68,8 @@ YGFlexDirection YGNodeStyleGetFlexDirection(const YGNodeConstRef node) {
void YGNodeStyleSetJustifyContent(
const YGNodeRef node,
const YGJustify justifyContent) {
updateStyle<MSVC_HINT(justifyContent)>(
node, &Style::justifyContent, scopedEnum(justifyContent));
updateStyle<&Style::justifyContent, &Style::setJustifyContent>(
node, scopedEnum(justifyContent));
}

YGJustify YGNodeStyleGetJustifyContent(const YGNodeConstRef node) {
Expand All @@ -107,26 +79,26 @@ YGJustify YGNodeStyleGetJustifyContent(const YGNodeConstRef node) {
void YGNodeStyleSetAlignContent(
const YGNodeRef node,
const YGAlign alignContent) {
updateStyle<MSVC_HINT(alignContent)>(
node, &Style::alignContent, scopedEnum(alignContent));
updateStyle<&Style::alignContent, &Style::setAlignContent>(
node, scopedEnum(alignContent));
}

YGAlign YGNodeStyleGetAlignContent(const YGNodeConstRef node) {
return unscopedEnum(resolveRef(node)->getStyle().alignContent());
}

void YGNodeStyleSetAlignItems(const YGNodeRef node, const YGAlign alignItems) {
updateStyle<MSVC_HINT(alignItems)>(
node, &Style::alignItems, scopedEnum(alignItems));
updateStyle<&Style::alignItems, &Style::setAlignItems>(
node, scopedEnum(alignItems));
}

YGAlign YGNodeStyleGetAlignItems(const YGNodeConstRef node) {
return unscopedEnum(resolveRef(node)->getStyle().alignItems());
}

void YGNodeStyleSetAlignSelf(const YGNodeRef node, const YGAlign alignSelf) {
updateStyle<MSVC_HINT(alignSelf)>(
node, &Style::alignSelf, scopedEnum(alignSelf));
updateStyle<&Style::alignSelf, &Style::setAlignSelf>(
node, scopedEnum(alignSelf));
}

YGAlign YGNodeStyleGetAlignSelf(const YGNodeConstRef node) {
Expand All @@ -136,34 +108,34 @@ YGAlign YGNodeStyleGetAlignSelf(const YGNodeConstRef node) {
void YGNodeStyleSetPositionType(
const YGNodeRef node,
const YGPositionType positionType) {
updateStyle<MSVC_HINT(positionType)>(
node, &Style::positionType, scopedEnum(positionType));
updateStyle<&Style::positionType, &Style::setPositionType>(
node, scopedEnum(positionType));
}

YGPositionType YGNodeStyleGetPositionType(const YGNodeConstRef node) {
return unscopedEnum(resolveRef(node)->getStyle().positionType());
}

void YGNodeStyleSetFlexWrap(const YGNodeRef node, const YGWrap flexWrap) {
updateStyle<MSVC_HINT(flexWrap)>(
node, &Style::flexWrap, scopedEnum(flexWrap));
updateStyle<&Style::flexWrap, &Style::setFlexWrap>(
node, scopedEnum(flexWrap));
}

YGWrap YGNodeStyleGetFlexWrap(const YGNodeConstRef node) {
return unscopedEnum(resolveRef(node)->getStyle().flexWrap());
}

void YGNodeStyleSetOverflow(const YGNodeRef node, const YGOverflow overflow) {
updateStyle<MSVC_HINT(overflow)>(
node, &Style::overflow, scopedEnum(overflow));
updateStyle<&Style::overflow, &Style::setOverflow>(
node, scopedEnum(overflow));
}

YGOverflow YGNodeStyleGetOverflow(const YGNodeConstRef node) {
return unscopedEnum(resolveRef(node)->getStyle().overflow());
}

void YGNodeStyleSetDisplay(const YGNodeRef node, const YGDisplay display) {
updateStyle<MSVC_HINT(display)>(node, &Style::display, scopedEnum(display));
updateStyle<&Style::display, &Style::setDisplay>(node, scopedEnum(display));
}

YGDisplay YGNodeStyleGetDisplay(const YGNodeConstRef node) {
Expand Down Expand Up @@ -231,12 +203,12 @@ YGValue YGNodeStyleGetFlexBasis(const YGNodeConstRef node) {
}

void YGNodeStyleSetPosition(YGNodeRef node, YGEdge edge, float points) {
updateIndexedStyleProp<&Style::position, &Style::setPosition>(
updateStyle<&Style::position, &Style::setPosition>(
node, scopedEnum(edge), value::points(points));
}

void YGNodeStyleSetPositionPercent(YGNodeRef node, YGEdge edge, float percent) {
updateIndexedStyleProp<&Style::position, &Style::setPosition>(
updateStyle<&Style::position, &Style::setPosition>(
node, scopedEnum(edge), value::percent(percent));
}

Expand All @@ -245,17 +217,17 @@ YGValue YGNodeStyleGetPosition(YGNodeConstRef node, YGEdge edge) {
}

void YGNodeStyleSetMargin(YGNodeRef node, YGEdge edge, float points) {
updateIndexedStyleProp<&Style::margin, &Style::setMargin>(
updateStyle<&Style::margin, &Style::setMargin>(
node, scopedEnum(edge), value::points(points));
}

void YGNodeStyleSetMarginPercent(YGNodeRef node, YGEdge edge, float percent) {
updateIndexedStyleProp<&Style::margin, &Style::setMargin>(
updateStyle<&Style::margin, &Style::setMargin>(
node, scopedEnum(edge), value::percent(percent));
}

void YGNodeStyleSetMarginAuto(YGNodeRef node, YGEdge edge) {
updateIndexedStyleProp<&Style::margin, &Style::setMargin>(
updateStyle<&Style::margin, &Style::setMargin>(
node, scopedEnum(edge), value::ofAuto());
}

Expand All @@ -264,12 +236,12 @@ YGValue YGNodeStyleGetMargin(YGNodeConstRef node, YGEdge edge) {
}

void YGNodeStyleSetPadding(YGNodeRef node, YGEdge edge, float points) {
updateIndexedStyleProp<&Style::padding, &Style::setPadding>(
updateStyle<&Style::padding, &Style::setPadding>(
node, scopedEnum(edge), value::points(points));
}

void YGNodeStyleSetPaddingPercent(YGNodeRef node, YGEdge edge, float percent) {
updateIndexedStyleProp<&Style::padding, &Style::setPadding>(
updateStyle<&Style::padding, &Style::setPadding>(
node, scopedEnum(edge), value::percent(percent));
}

Expand All @@ -281,7 +253,7 @@ void YGNodeStyleSetBorder(
const YGNodeRef node,
const YGEdge edge,
const float border) {
updateIndexedStyleProp<&Style::border, &Style::setBorder>(
updateStyle<&Style::border, &Style::setBorder>(
node, scopedEnum(edge), value::points(border));
}

Expand All @@ -298,7 +270,7 @@ void YGNodeStyleSetGap(
const YGNodeRef node,
const YGGutter gutter,
const float gapLength) {
updateIndexedStyleProp<&Style::gap, &Style::setGap>(
updateStyle<&Style::gap, &Style::setGap>(
node, scopedEnum(gutter), value::points(gapLength));
}

Expand All @@ -322,17 +294,17 @@ float YGNodeStyleGetAspectRatio(const YGNodeConstRef node) {
}

void YGNodeStyleSetWidth(YGNodeRef node, float points) {
updateIndexedStyleProp<&Style::dimension, &Style::setDimension>(
updateStyle<&Style::dimension, &Style::setDimension>(
node, Dimension::Width, value::points(points));
}

void YGNodeStyleSetWidthPercent(YGNodeRef node, float percent) {
updateIndexedStyleProp<&Style::dimension, &Style::setDimension>(
updateStyle<&Style::dimension, &Style::setDimension>(
node, Dimension::Width, value::percent(percent));
}

void YGNodeStyleSetWidthAuto(YGNodeRef node) {
updateIndexedStyleProp<&Style::dimension, &Style::setDimension>(
updateStyle<&Style::dimension, &Style::setDimension>(
node, Dimension::Width, value::ofAuto());
}

Expand All @@ -341,17 +313,17 @@ YGValue YGNodeStyleGetWidth(YGNodeConstRef node) {
}

void YGNodeStyleSetHeight(YGNodeRef node, float points) {
updateIndexedStyleProp<&Style::dimension, &Style::setDimension>(
updateStyle<&Style::dimension, &Style::setDimension>(
node, Dimension::Height, value::points(points));
}

void YGNodeStyleSetHeightPercent(YGNodeRef node, float percent) {
updateIndexedStyleProp<&Style::dimension, &Style::setDimension>(
updateStyle<&Style::dimension, &Style::setDimension>(
node, Dimension::Height, value::percent(percent));
}

void YGNodeStyleSetHeightAuto(YGNodeRef node) {
updateIndexedStyleProp<&Style::dimension, &Style::setDimension>(
updateStyle<&Style::dimension, &Style::setDimension>(
node, Dimension::Height, value::ofAuto());
}

Expand All @@ -360,12 +332,12 @@ YGValue YGNodeStyleGetHeight(YGNodeConstRef node) {
}

void YGNodeStyleSetMinWidth(const YGNodeRef node, const float minWidth) {
updateIndexedStyleProp<&Style::minDimension, &Style::setMinDimension>(
updateStyle<&Style::minDimension, &Style::setMinDimension>(
node, Dimension::Width, value::points(minWidth));
}

void YGNodeStyleSetMinWidthPercent(const YGNodeRef node, const float minWidth) {
updateIndexedStyleProp<&Style::minDimension, &Style::setMinDimension>(
updateStyle<&Style::minDimension, &Style::setMinDimension>(
node, Dimension::Width, value::percent(minWidth));
}

Expand All @@ -374,14 +346,14 @@ YGValue YGNodeStyleGetMinWidth(const YGNodeConstRef node) {
}

void YGNodeStyleSetMinHeight(const YGNodeRef node, const float minHeight) {
updateIndexedStyleProp<&Style::minDimension, &Style::setMinDimension>(
updateStyle<&Style::minDimension, &Style::setMinDimension>(
node, Dimension::Height, value::points(minHeight));
}

void YGNodeStyleSetMinHeightPercent(
const YGNodeRef node,
const float minHeight) {
updateIndexedStyleProp<&Style::minDimension, &Style::setMinDimension>(
updateStyle<&Style::minDimension, &Style::setMinDimension>(
node, Dimension::Height, value::percent(minHeight));
}

Expand All @@ -390,12 +362,12 @@ YGValue YGNodeStyleGetMinHeight(const YGNodeConstRef node) {
}

void YGNodeStyleSetMaxWidth(const YGNodeRef node, const float maxWidth) {
updateIndexedStyleProp<&Style::maxDimension, &Style::setMaxDimension>(
updateStyle<&Style::maxDimension, &Style::setMaxDimension>(
node, Dimension::Width, value::points(maxWidth));
}

void YGNodeStyleSetMaxWidthPercent(const YGNodeRef node, const float maxWidth) {
updateIndexedStyleProp<&Style::maxDimension, &Style::setMaxDimension>(
updateStyle<&Style::maxDimension, &Style::setMaxDimension>(
node, Dimension::Width, value::percent(maxWidth));
}

Expand All @@ -404,14 +376,14 @@ YGValue YGNodeStyleGetMaxWidth(const YGNodeConstRef node) {
}

void YGNodeStyleSetMaxHeight(const YGNodeRef node, const float maxHeight) {
updateIndexedStyleProp<&Style::maxDimension, &Style::setMaxDimension>(
updateStyle<&Style::maxDimension, &Style::setMaxDimension>(
node, Dimension::Height, value::points(maxHeight));
}

void YGNodeStyleSetMaxHeightPercent(
const YGNodeRef node,
const float maxHeight) {
updateIndexedStyleProp<&Style::maxDimension, &Style::setMaxDimension>(
updateStyle<&Style::maxDimension, &Style::setMaxDimension>(
node, Dimension::Height, value::percent(maxHeight));
}

Expand Down
4 changes: 2 additions & 2 deletions yoga/node/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class YG_EXPORT Node : public ::YGNode {
Direction direction) const;

void useWebDefaults() {
style_.flexDirection() = FlexDirection::Row;
style_.alignContent() = Align::Stretch;
style_.setFlexDirection(FlexDirection::Row);
style_.setAlignContent(Align::Stretch);
}

template <auto Field>
Expand Down
Loading

0 comments on commit 9b0fd09

Please sign in to comment.