From 8af724451d2b2717a9b9ebe4f3e58d25f2569f40 Mon Sep 17 00:00:00 2001
From: Nick Gerleman <ngerlem@fb.com>
Date: Mon, 27 Nov 2023 14:51:32 -0800
Subject: [PATCH] Remove yoga::Style::Ref (#41389)

Summary:
X-link: https://github.com/facebook/yoga/pull/1462


Moves the last usages of `yoga::Style::Ref` to setters.

Changelog: [Internal]

Reviewed By: joevilches

Differential Revision: D51154501
---
 .../components/view/YogaStylableProps.cpp     | 16 ++++++---
 .../components/view/propsConversions.h        | 24 ++++++++------
 .../ReactCommon/yoga/yoga/YGNodeStyle.cpp     | 33 ++++++++++++-------
 .../ReactCommon/yoga/yoga/style/Style.h       | 33 ++++++-------------
 4 files changed, 56 insertions(+), 50 deletions(-)

diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/YogaStylableProps.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/YogaStylableProps.cpp
index f3bce4b95c392d..064d9ecf93f444 100644
--- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaStylableProps.cpp
+++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaStylableProps.cpp
@@ -59,6 +59,12 @@ static inline T const getFieldValue(
 #define REBUILD_FIELD_SWITCH_CASE_YSP(field) \
   REBUILD_FIELD_SWITCH_CASE2(field, #field)
 
+#define REBUILD_FIELD_SWITCH_CASE_YSP_SETTER(field, setter)              \
+  case CONSTEXPR_RAW_PROPS_KEY_HASH(#field): {                           \
+    yogaStyle.setter(getFieldValue(context, value, ygDefaults.field())); \
+    return;                                                              \
+  }
+
 #define REBUILD_YG_FIELD_SWITCH_CASE_INDEXED(field, setter, index, fieldName) \
   case CONSTEXPR_RAW_PROPS_KEY_HASH(fieldName): {                             \
     yogaStyle.setter(                                                         \
@@ -141,13 +147,13 @@ void YogaStylableProps::setProp(
     REBUILD_FIELD_SWITCH_CASE_YSP(flexWrap);
     REBUILD_FIELD_SWITCH_CASE_YSP(overflow);
     REBUILD_FIELD_SWITCH_CASE_YSP(display);
-    REBUILD_FIELD_SWITCH_CASE_YSP(flex);
-    REBUILD_FIELD_SWITCH_CASE_YSP(flexGrow);
-    REBUILD_FIELD_SWITCH_CASE_YSP(flexShrink);
-    REBUILD_FIELD_SWITCH_CASE_YSP(flexBasis);
+    REBUILD_FIELD_SWITCH_CASE_YSP_SETTER(flex, setFlex);
+    REBUILD_FIELD_SWITCH_CASE_YSP_SETTER(flexGrow, setFlexGrow);
+    REBUILD_FIELD_SWITCH_CASE_YSP_SETTER(flexShrink, setFlexShrink);
+    REBUILD_FIELD_SWITCH_CASE_YSP_SETTER(flexBasis, setFlexBasis);
     REBUILD_FIELD_SWITCH_CASE2(positionType, "position");
     REBUILD_FIELD_YG_GUTTER(gap, setGap, "rowGap", "columnGap", "gap");
-    REBUILD_FIELD_SWITCH_CASE_YSP(aspectRatio);
+    REBUILD_FIELD_SWITCH_CASE_YSP_SETTER(aspectRatio, setAspectRatio);
     REBUILD_FIELD_YG_DIMENSION(dimension, setDimension, "width", "height");
     REBUILD_FIELD_YG_DIMENSION(
         minDimension, setMinDimension, "minWidth", "minHeight");
diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/propsConversions.h b/packages/react-native/ReactCommon/react/renderer/components/view/propsConversions.h
index 529f3236454a2f..fc236455fa8f8e 100644
--- a/packages/react-native/ReactCommon/react/renderer/components/view/propsConversions.h
+++ b/packages/react-native/ReactCommon/react/renderer/components/view/propsConversions.h
@@ -79,26 +79,30 @@ static inline yoga::Style convertRawProp(
       yogaStyle.overflow());
   yogaStyle.display() = convertRawProp(
       context, rawProps, "display", sourceValue.display(), yogaStyle.display());
-  yogaStyle.flex() = convertRawProp(
-      context, rawProps, "flex", sourceValue.flex(), yogaStyle.flex());
-  yogaStyle.flexGrow() = convertRawProp(
+
+  yogaStyle.setFlex(convertRawProp(
+      context, rawProps, "flex", sourceValue.flex(), yogaStyle.flex()));
+
+  yogaStyle.setFlexGrow(convertRawProp(
       context,
       rawProps,
       "flexGrow",
       sourceValue.flexGrow(),
-      yogaStyle.flexGrow());
-  yogaStyle.flexShrink() = convertRawProp(
+      yogaStyle.flexGrow()));
+
+  yogaStyle.setFlexShrink(convertRawProp(
       context,
       rawProps,
       "flexShrink",
       sourceValue.flexShrink(),
-      yogaStyle.flexShrink());
-  yogaStyle.flexBasis() = convertRawProp(
+      yogaStyle.flexShrink()));
+
+  yogaStyle.setFlexBasis(convertRawProp(
       context,
       rawProps,
       "flexBasis",
       sourceValue.flexBasis(),
-      yogaStyle.flexBasis());
+      yogaStyle.flexBasis()));
 
   yogaStyle.setMargin(
       yoga::Edge::Left,
@@ -502,12 +506,12 @@ static inline yoga::Style convertRawProp(
           sourceValue.maxDimension(yoga::Dimension::Height),
           {}));
 
-  yogaStyle.aspectRatio() = convertRawProp(
+  yogaStyle.setAspectRatio(convertRawProp(
       context,
       rawProps,
       "aspectRatio",
       sourceValue.aspectRatio(),
-      yogaStyle.aspectRatio());
+      yogaStyle.aspectRatio()));
 
   return yogaStyle;
 }
diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGNodeStyle.cpp b/packages/react-native/ReactCommon/yoga/yoga/YGNodeStyle.cpp
index c39372a33ac10c..441b7797f776fb 100644
--- a/packages/react-native/ReactCommon/yoga/yoga/YGNodeStyle.cpp
+++ b/packages/react-native/ReactCommon/yoga/yoga/YGNodeStyle.cpp
@@ -35,6 +35,15 @@ void updateStyle(YGNodeRef node, Ref (Style::*prop)(), ValueT value) {
       [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); });
+}
+
 template <auto GetterT, auto SetterT, typename IdxT, typename ValueT>
 void updateIndexedStyleProp(YGNodeRef node, IdxT idx, ValueT value) {
   updateStyle(
@@ -162,7 +171,7 @@ YGDisplay YGNodeStyleGetDisplay(const YGNodeConstRef node) {
 }
 
 void YGNodeStyleSetFlex(const YGNodeRef node, const float flex) {
-  updateStyle<MSVC_HINT(flex)>(node, &Style::flex, FloatOptional{flex});
+  updateStyle<&Style::flex, &Style::setFlex>(node, FloatOptional{flex});
 }
 
 float YGNodeStyleGetFlex(const YGNodeConstRef nodeRef) {
@@ -173,8 +182,8 @@ float YGNodeStyleGetFlex(const YGNodeConstRef nodeRef) {
 }
 
 void YGNodeStyleSetFlexGrow(const YGNodeRef node, const float flexGrow) {
-  updateStyle<MSVC_HINT(flexGrow)>(
-      node, &Style::flexGrow, FloatOptional{flexGrow});
+  updateStyle<&Style::flexGrow, &Style::setFlexGrow>(
+      node, FloatOptional{flexGrow});
 }
 
 float YGNodeStyleGetFlexGrow(const YGNodeConstRef nodeRef) {
@@ -185,8 +194,8 @@ float YGNodeStyleGetFlexGrow(const YGNodeConstRef nodeRef) {
 }
 
 void YGNodeStyleSetFlexShrink(const YGNodeRef node, const float flexShrink) {
-  updateStyle<MSVC_HINT(flexShrink)>(
-      node, &Style::flexShrink, FloatOptional{flexShrink});
+  updateStyle<&Style::flexShrink, &Style::setFlexShrink>(
+      node, FloatOptional{flexShrink});
 }
 
 float YGNodeStyleGetFlexShrink(const YGNodeConstRef nodeRef) {
@@ -198,19 +207,19 @@ float YGNodeStyleGetFlexShrink(const YGNodeConstRef nodeRef) {
 }
 
 void YGNodeStyleSetFlexBasis(const YGNodeRef node, const float flexBasis) {
-  updateStyle<MSVC_HINT(flexBasis)>(
-      node, &Style::flexBasis, value::points(flexBasis));
+  updateStyle<&Style::flexBasis, &Style::setFlexBasis>(
+      node, value::points(flexBasis));
 }
 
 void YGNodeStyleSetFlexBasisPercent(
     const YGNodeRef node,
     const float flexBasisPercent) {
-  updateStyle<MSVC_HINT(flexBasis)>(
-      node, &Style::flexBasis, value::percent(flexBasisPercent));
+  updateStyle<&Style::flexBasis, &Style::setFlexBasis>(
+      node, value::percent(flexBasisPercent));
 }
 
 void YGNodeStyleSetFlexBasisAuto(const YGNodeRef node) {
-  updateStyle<MSVC_HINT(flexBasis)>(node, &Style::flexBasis, value::ofAuto());
+  updateStyle<&Style::flexBasis, &Style::setFlexBasis>(node, value::ofAuto());
 }
 
 YGValue YGNodeStyleGetFlexBasis(const YGNodeConstRef node) {
@@ -303,8 +312,8 @@ float YGNodeStyleGetGap(const YGNodeConstRef node, const YGGutter gutter) {
 }
 
 void YGNodeStyleSetAspectRatio(const YGNodeRef node, const float aspectRatio) {
-  updateStyle<MSVC_HINT(aspectRatio)>(
-      node, &Style::aspectRatio, FloatOptional{aspectRatio});
+  updateStyle<&Style::aspectRatio, &Style::setAspectRatio>(
+      node, FloatOptional{aspectRatio});
 }
 
 float YGNodeStyleGetAspectRatio(const YGNodeConstRef node) {
diff --git a/packages/react-native/ReactCommon/yoga/yoga/style/Style.h b/packages/react-native/ReactCommon/yoga/yoga/style/Style.h
index e40f1c4fb644f7..09f5c3c1fb88ca 100644
--- a/packages/react-native/ReactCommon/yoga/yoga/style/Style.h
+++ b/packages/react-native/ReactCommon/yoga/yoga/style/Style.h
@@ -68,18 +68,6 @@ class YG_EXPORT Style {
     }
   };
 
-  template <typename T, T Style::*Prop>
-  struct Ref {
-    Style& style;
-    operator T() const {
-      return style.*Prop;
-    }
-    Ref<T, Prop>& operator=(T value) {
-      style.*Prop = value;
-      return *this;
-    }
-  };
-
   Style() {
     alignContent() = Align::FlexStart;
     alignItems() = Align::Stretch;
@@ -202,29 +190,29 @@ class YG_EXPORT Style {
   FloatOptional flex() const {
     return flex_;
   }
-  Ref<FloatOptional, &Style::flex_> flex() {
-    return {*this};
+  void setFlex(FloatOptional value) {
+    flex_ = value;
   }
 
   FloatOptional flexGrow() const {
     return flexGrow_;
   }
-  Ref<FloatOptional, &Style::flexGrow_> flexGrow() {
-    return {*this};
+  void setFlexGrow(FloatOptional value) {
+    flexGrow_ = value;
   }
 
   FloatOptional flexShrink() const {
     return flexShrink_;
   }
-  Ref<FloatOptional, &Style::flexShrink_> flexShrink() {
-    return {*this};
+  void setFlexShrink(FloatOptional value) {
+    flexShrink_ = value;
   }
 
   Style::Length flexBasis() const {
     return flexBasis_;
   }
-  Ref<Style::Length, &Style::flexBasis_> flexBasis() {
-    return {*this};
+  void setFlexBasis(Style::Length value) {
+    flexBasis_ = value;
   }
 
   Style::Length margin(Edge edge) const {
@@ -283,12 +271,11 @@ class YG_EXPORT Style {
     maxDimensions_[yoga::to_underlying(axis)] = value;
   }
 
-  // Yoga specific properties, not compatible with flexbox specification
   FloatOptional aspectRatio() const {
     return aspectRatio_;
   }
-  Ref<FloatOptional, &Style::aspectRatio_> aspectRatio() {
-    return {*this};
+  void setAspectRatio(FloatOptional value) {
+    aspectRatio_ = value;
   }
 
   Length resolveColumnGap() const {