From 69503b0c5dc509b669ea13c4f855afd935178526 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Wed, 30 Aug 2023 15:12:21 -0700 Subject: [PATCH 1/2] C++ Cleanup 1/N: Reorganize YGStyle (#39171) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: X-link: https://github.com/facebook/yoga/pull/1349 Pull Request resolved: https://github.com/facebook/react-native/pull/39171 ## This diff This diff adds a `style` directory for code related to storing and manipulating styles. `YGStyle`, which is not a public API, is renamed to `yoga::Style` and moved into this folder, alongside `CompactValue`. We will eventually add `ValuePool` alongside this for the next generation style representation. ## This stack The organization of the C++ internals of Yoga are in need of attention. 1. Some of the C++ internals are namespaced, but others not. 2. Some of the namespaces include `detail`, but are meant to be used outside of the translation unit (FB Clang Tidy rules warn on any usage of these) 2. Most of the files are in a flat hierarchy, except for event tracing in its own folder 3. Some files and functions begin with YG, others don’t 4. Some functions are uppercase, others are not 5. Almost all of the interesting logic is in Yoga.cpp, and the file is too large to reason about 6. There are multiple grab bag files where folks put random functions they need in (Utils, BitUtils, Yoga-Internal.h) 7. There is no clear indication from file structure or type naming what is private vs not 8. Handles like `YGNodeRef` and `YGConfigRef` can be used to access internals just by importing headers This stack does some much needed spring cleaning: 1. All non-public headers and C++ implementation details are in separate folders from the root level `yoga`. This will give us room to split up logic and add more files without too large a flat hierarchy 3. All private C++ internals are under the `facebook::yoga` namespace. Details namespaces are only ever used within the same header, as they are intended 4. Utils files are split 5. Most C++ internals drop the YG prefix 6. Most C++ internal function names are all lower camel case 7. We start to split up Yoga.cpp 8. Every header beginning with YG or at the top-level directory is public and C only, with the exception of Yoga-Internal.h which has non-public functions for bindings 9. It is not possible to use private APIs without static casting handles to internal classes This will give us more leeway to continue splitting monolithic files, and consistent guidelines for style in new files as well. These changes should not be breaking to any project using only public Yoga headers. This includes every usage of Yoga in fbsource except for RN Fabric which is currently tied to internals. This refactor should make that boundary clearer. Changelog: [Internal] Differential Revision: D48847261 fbshipit-source-id: 363d3603000cec0ea91a418cf79cb4c2a8ff8006 --- .../jni/react/fabric/viewPropConversions.h | 2 +- .../AndroidTextInputComponentDescriptor.h | 10 +- .../components/view/ViewShadowNode.cpp | 2 +- .../view/YogaLayoutableShadowNode.cpp | 14 +- .../view/YogaLayoutableShadowNode.h | 10 +- .../components/view/YogaStylableProps.cpp | 4 +- .../components/view/YogaStylableProps.h | 6 +- .../renderer/components/view/conversions.h | 8 +- .../view/YogaStylablePropsMapBuffer.cpp | 2 +- .../components/view/propsConversions.h | 24 +-- .../ReactCommon/yoga/Yoga.podspec | 1 + .../ReactCommon/yoga/yoga/Utils.h | 12 +- .../ReactCommon/yoga/yoga/YGConfig.h | 4 +- .../ReactCommon/yoga/yoga/YGFloatOptional.h | 2 +- .../ReactCommon/yoga/yoga/YGLayout.h | 6 +- .../ReactCommon/yoga/yoga/YGNode.cpp | 13 +- .../ReactCommon/yoga/yoga/YGNode.h | 25 +-- .../ReactCommon/yoga/yoga/YGNodePrint.cpp | 24 ++- .../ReactCommon/yoga/yoga/Yoga-internal.h | 2 +- .../ReactCommon/yoga/yoga/Yoga.cpp | 154 ++++++++---------- .../yoga/yoga/{ => style}/CompactValue.h | 4 +- .../yoga/{YGStyle.cpp => style/Style.cpp} | 10 +- .../yoga/yoga/{YGStyle.h => style/Style.h} | 63 +++---- 23 files changed, 199 insertions(+), 203 deletions(-) rename packages/react-native/ReactCommon/yoga/yoga/{ => style}/CompactValue.h (98%) rename packages/react-native/ReactCommon/yoga/yoga/{YGStyle.cpp => style/Style.cpp} (93%) rename packages/react-native/ReactCommon/yoga/yoga/{YGStyle.h => style/Style.h} (80%) diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/viewPropConversions.h b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/viewPropConversions.h index 19381f8d585a79..c34c1d22485e5d 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/viewPropConversions.h +++ b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/viewPropConversions.h @@ -142,7 +142,7 @@ MapBuffer convertBorderRadii(CascadedBorderRadii const &radii) { return builder.build(); } -MapBuffer convertBorderWidths(YGStyle::Edges const &border) { +MapBuffer convertBorderWidths(yoga::Style::Edges const &border) { MapBufferBuilder builder(7); putOptionalFloat( builder, EDGE_TOP, optionalFloatFromYogaValue(border[YGEdgeTop])); diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/androidtextinput/react/renderer/components/androidtextinput/AndroidTextInputComponentDescriptor.h b/packages/react-native/ReactCommon/react/renderer/components/textinput/androidtextinput/react/renderer/components/androidtextinput/AndroidTextInputComponentDescriptor.h index 7ea08be94c9589..9954603b8ba4ba 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/textinput/androidtextinput/react/renderer/components/androidtextinput/AndroidTextInputComponentDescriptor.h +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/androidtextinput/react/renderer/components/androidtextinput/AndroidTextInputComponentDescriptor.h @@ -11,9 +11,9 @@ #include -#include #include #include +#include #include @@ -38,7 +38,7 @@ class AndroidTextInputComponentDescriptor final ShadowNodeFamily::Shared const &family) const override { int surfaceId = family->getSurfaceId(); - YGStyle::Edges theme; + yoga::Style::Edges theme; // TODO: figure out RTL/start/end/left/right stuff here if (surfaceIdToThemePaddingMap_.find(surfaceId) != surfaceIdToThemePaddingMap_.end()) { @@ -97,7 +97,7 @@ class AndroidTextInputComponentDescriptor final int surfaceId = textInputShadowNode.getSurfaceId(); if (surfaceIdToThemePaddingMap_.find(surfaceId) != surfaceIdToThemePaddingMap_.end()) { - YGStyle::Edges theme = surfaceIdToThemePaddingMap_[surfaceId]; + yoga::Style::Edges theme = surfaceIdToThemePaddingMap_[surfaceId]; auto &textInputProps = textInputShadowNode.getConcreteProps(); @@ -106,7 +106,7 @@ class AndroidTextInputComponentDescriptor final // TODO: T62959168 account for RTL and paddingLeft when setting default // paddingStart, and vice-versa with paddingRight/paddingEnd. // For now this assumes no RTL. - YGStyle::Edges result = textInputProps.yogaStyle.padding(); + yoga::Style::Edges result = textInputProps.yogaStyle.padding(); bool changedPadding = false; if (!textInputProps.hasPadding && !textInputProps.hasPaddingStart && !textInputProps.hasPaddingLeft && @@ -171,7 +171,7 @@ class AndroidTextInputComponentDescriptor final "com/facebook/react/fabric/FabricUIManager"; SharedTextLayoutManager textLayoutManager_; - mutable butter::map surfaceIdToThemePaddingMap_; + mutable butter::map surfaceIdToThemePaddingMap_; }; } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/ViewShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/ViewShadowNode.cpp index 2375c2dea84cca..ac018bfadaff95 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/ViewShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/ViewShadowNode.cpp @@ -60,7 +60,7 @@ void ViewShadowNode::initialize() noexcept { bool formsView = formsStackingContext || isColorMeaningful(viewProps.backgroundColor) || - !(viewProps.yogaStyle.border() == YGStyle::Edges{}) || + !(viewProps.yogaStyle.border() == yoga::Style::Edges{}) || !viewProps.testId.empty() || HostPlatformViewTraitsInitializer::formsView(viewProps); diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp index 6d0a2f4807ac15..a5efc5b40a2a5c 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp @@ -378,10 +378,10 @@ void YogaLayoutableShadowNode::updateYogaProps() { yogaNode_.setStyle(styleResult); } -/*static*/ YGStyle YogaLayoutableShadowNode::applyAliasedProps( - const YGStyle &baseStyle, +/*static*/ yoga::Style YogaLayoutableShadowNode::applyAliasedProps( + const yoga::Style &baseStyle, const YogaStylableProps &props) { - YGStyle result{baseStyle}; + yoga::Style result{baseStyle}; // Aliases with precedence if (!props.inset.isUndefined()) { @@ -864,9 +864,9 @@ void YogaLayoutableShadowNode::swapLeftAndRightInYogaStyleProps( YogaLayoutableShadowNode const &shadowNode) { auto yogaStyle = shadowNode.yogaNode_.getStyle(); - YGStyle::Edges const &position = yogaStyle.position(); - YGStyle::Edges const &padding = yogaStyle.padding(); - YGStyle::Edges const &margin = yogaStyle.margin(); + yoga::Style::Edges const &position = yogaStyle.position(); + yoga::Style::Edges const &padding = yogaStyle.padding(); + yoga::Style::Edges const &margin = yogaStyle.margin(); // Swap Yoga node values, position, padding and margin. @@ -950,7 +950,7 @@ void YogaLayoutableShadowNode::swapLeftAndRightInViewProps( props.borderStyles.right.reset(); } - YGStyle::Edges const &border = props.yogaStyle.border(); + yoga::Style::Edges const &border = props.yogaStyle.border(); if (props.yogaStyle.border()[YGEdgeLeft] != YGValueUndefined) { props.yogaStyle.border()[YGEdgeStart] = border[YGEdgeLeft]; diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h index fee4c635810244..dbb46a0161ec79 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h @@ -22,7 +22,7 @@ namespace facebook::react { class YogaLayoutableShadowNode : public LayoutableShadowNode { - using CompactValue = facebook::yoga::detail::CompactValue; + using CompactValue = facebook::yoga::CompactValue; public: using Shared = std::shared_ptr; @@ -202,11 +202,11 @@ class YogaLayoutableShadowNode : public LayoutableShadowNode { YogaLayoutableShadowNode const &shadowNode); /* - * Combine a base YGStyle with aliased properties which should be flattened - * into it. E.g. reconciling "marginInlineStart" and "marginStart". + * Combine a base yoga::Style with aliased properties which should be + * flattened into it. E.g. reconciling "marginInlineStart" and "marginStart". */ - static YGStyle applyAliasedProps( - const YGStyle &baseStyle, + static yoga::Style applyAliasedProps( + const yoga::Style &baseStyle, const YogaStylableProps &props); #pragma mark - Consistency Ensuring Helpers 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 b92ae8c68a6316..903d4e2db21e21 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaStylableProps.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaStylableProps.cpp @@ -104,7 +104,7 @@ void YogaStylableProps::setProp( RawPropsPropNameHash hash, const char *propName, RawValue const &value) { - static const auto ygDefaults = YGStyle{}; + static const auto ygDefaults = yoga::Style{}; static const auto defaults = YogaStylableProps{}; Props::setProp(context, hash, propName, value); @@ -161,7 +161,7 @@ void YogaStylableProps::setProp( #if RN_DEBUG_STRING_CONVERTIBLE SharedDebugStringConvertibleList YogaStylableProps::getDebugProps() const { - auto const defaultYogaStyle = YGStyle{}; + auto const defaultYogaStyle = yoga::Style{}; return { debugStringConvertibleItem( "direction", yogaStyle.direction(), defaultYogaStyle.direction()), diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/YogaStylableProps.h b/packages/react-native/ReactCommon/react/renderer/components/view/YogaStylableProps.h index b3bc8c6af497fc..46d414722f4e8c 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaStylableProps.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaStylableProps.h @@ -7,7 +7,7 @@ #pragma once -#include +#include #include #include @@ -16,7 +16,7 @@ namespace facebook::react { class YogaStylableProps : public Props { - using CompactValue = facebook::yoga::detail::CompactValue; + using CompactValue = facebook::yoga::CompactValue; public: YogaStylableProps() = default; @@ -38,7 +38,7 @@ class YogaStylableProps : public Props { #endif #pragma mark - Props - YGStyle yogaStyle{}; + yoga::Style yogaStyle{}; // Duplicates of existing properties with different names, taking // precedence. E.g. "marginBlock" instead of "marginVertical" diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h b/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h index 7d3d9d0586f46b..5bd66eea65a5d3 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h @@ -406,7 +406,7 @@ inline void fromRawValue( inline void fromRawValue( const PropsParserContext &context, const RawValue &value, - YGStyle::ValueRepr &result) { + yoga::Style::ValueRepr &result) { if (value.hasType()) { result = yogaStyleValueFromFloat((Float)value); return; @@ -440,7 +440,7 @@ inline void fromRawValue( const PropsParserContext &context, const RawValue &value, YGValue &result) { - YGStyle::ValueRepr ygValue{}; + yoga::Style::ValueRepr ygValue{}; fromRawValue(context, value, ygValue); result = ygValue; } @@ -826,11 +826,11 @@ inline std::string toString(const YGFloatOptional &value) { return folly::to(floatFromYogaFloat(value.unwrap())); } -inline std::string toString(const YGStyle::Dimensions &value) { +inline std::string toString(const yoga::Style::Dimensions &value) { return "{" + toString(value[0]) + ", " + toString(value[1]) + "}"; } -inline std::string toString(const YGStyle::Edges &value) { +inline std::string toString(const yoga::Style::Edges &value) { static std::array()> names = { {"left", "top", diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view/YogaStylablePropsMapBuffer.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view/YogaStylablePropsMapBuffer.cpp index 5d5ece381b2330..b59fff538719f1 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view/YogaStylablePropsMapBuffer.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view/YogaStylablePropsMapBuffer.cpp @@ -13,7 +13,7 @@ namespace facebook::react { -MapBuffer convertBorderWidths(YGStyle::Edges const &border) { +MapBuffer convertBorderWidths(yoga::Style::Edges const &border) { MapBufferBuilder builder(7); putOptionalFloat( builder, EDGE_TOP, optionalFloatFromYogaValue(border[YGEdgeTop])); 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 6c65803c1b5db8..dd768b4aa1b8ec 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/propsConversions.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/propsConversions.h @@ -17,13 +17,13 @@ namespace facebook::react { // Nearly this entire file can be deleted when iterator-style Prop parsing // ships fully for View -static inline YGStyle::Dimensions convertRawProp( +static inline yoga::Style::Dimensions convertRawProp( const PropsParserContext &context, RawProps const &rawProps, char const *widthName, char const *heightName, - YGStyle::Dimensions const &sourceValue, - YGStyle::Dimensions const &defaultValue) { + yoga::Style::Dimensions const &sourceValue, + yoga::Style::Dimensions const &defaultValue) { auto dimensions = defaultValue; dimensions[YGDimensionWidth] = convertRawProp( context, @@ -40,13 +40,13 @@ static inline YGStyle::Dimensions convertRawProp( return dimensions; } -static inline YGStyle::Edges convertRawProp( +static inline yoga::Style::Edges convertRawProp( const PropsParserContext &context, RawProps const &rawProps, char const *prefix, char const *suffix, - YGStyle::Edges const &sourceValue, - YGStyle::Edges const &defaultValue) { + yoga::Style::Edges const &sourceValue, + yoga::Style::Edges const &defaultValue) { auto result = defaultValue; result[YGEdgeLeft] = convertRawProp( context, @@ -123,11 +123,11 @@ static inline YGStyle::Edges convertRawProp( return result; } -static inline YGStyle::Edges convertRawProp( +static inline yoga::Style::Edges convertRawProp( const PropsParserContext &context, RawProps const &rawProps, - YGStyle::Edges const &sourceValue, - YGStyle::Edges const &defaultValue) { + yoga::Style::Edges const &sourceValue, + yoga::Style::Edges const &defaultValue) { auto result = defaultValue; result[YGEdgeLeft] = convertRawProp( context, @@ -168,11 +168,11 @@ static inline YGStyle::Edges convertRawProp( return result; } -static inline YGStyle convertRawProp( +static inline yoga::Style convertRawProp( const PropsParserContext &context, RawProps const &rawProps, - YGStyle const &sourceValue) { - auto yogaStyle = YGStyle{}; + yoga::Style const &sourceValue) { + auto yogaStyle = yoga::Style{}; yogaStyle.direction() = convertRawProp( context, rawProps, diff --git a/packages/react-native/ReactCommon/yoga/Yoga.podspec b/packages/react-native/ReactCommon/yoga/Yoga.podspec index 7e68f6dc4fa5c4..5442f0afc34e95 100644 --- a/packages/react-native/ReactCommon/yoga/Yoga.podspec +++ b/packages/react-native/ReactCommon/yoga/Yoga.podspec @@ -50,6 +50,7 @@ Pod::Spec.new do |spec| source_files = 'yoga/**/*.{cpp,h}' source_files = File.join('ReactCommon/yoga', source_files) if ENV['INSTALL_YOGA_WITHOUT_PATH_OPTION'] spec.source_files = source_files + spec.header_mappings_dir = 'yoga' public_header_files = 'yoga/{Yoga,YGEnums,YGMacros,YGValue}.h' public_header_files = File.join('ReactCommon/yoga', public_header_files) if ENV['INSTALL_YOGA_WITHOUT_PATH_OPTION'] diff --git a/packages/react-native/ReactCommon/yoga/yoga/Utils.h b/packages/react-native/ReactCommon/yoga/yoga/Utils.h index 376c4fb55c7e7e..78b44de6939264 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Utils.h +++ b/packages/react-native/ReactCommon/yoga/yoga/Utils.h @@ -8,8 +8,8 @@ #pragma once #include "YGNode.h" -#include "Yoga-internal.h" -#include "CompactValue.h" +#include +#include // This struct is an helper model to hold the data for step 4 of flexbox algo, // which is collecting the flex items in a line. @@ -56,8 +56,8 @@ struct YGCollectFlexItemsRowValues { bool YGValueEqual(const YGValue& a, const YGValue& b); inline bool YGValueEqual( - facebook::yoga::detail::CompactValue a, - facebook::yoga::detail::CompactValue b) { + facebook::yoga::CompactValue a, + facebook::yoga::CompactValue b) { return YGValueEqual((YGValue) a, (YGValue) b); } @@ -115,7 +115,7 @@ inline YGFloatOptional YGResolveValue( } inline YGFloatOptional YGResolveValue( - facebook::yoga::detail::CompactValue value, + facebook::yoga::CompactValue value, float ownerSize) { return YGResolveValue((YGValue) value, ownerSize); } @@ -140,7 +140,7 @@ inline YGFlexDirection YGResolveFlexDirection( } inline YGFloatOptional YGResolveValueMargin( - facebook::yoga::detail::CompactValue value, + facebook::yoga::CompactValue value, const float ownerSize) { return value.isAuto() ? YGFloatOptional{0} : YGResolveValue(value, ownerSize); } diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGConfig.h b/packages/react-native/ReactCommon/yoga/yoga/YGConfig.h index 391e250080de00..fa530fab05606b 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGConfig.h +++ b/packages/react-native/ReactCommon/yoga/yoga/YGConfig.h @@ -9,8 +9,8 @@ #include -#include "BitUtils.h" -#include "Yoga-internal.h" +#include +#include namespace facebook::yoga { diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGFloatOptional.h b/packages/react-native/ReactCommon/yoga/yoga/YGFloatOptional.h index 4aa9e76e2993fb..c4c18311d53cb6 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGFloatOptional.h +++ b/packages/react-native/ReactCommon/yoga/yoga/YGFloatOptional.h @@ -9,7 +9,7 @@ #include #include -#include "Yoga-internal.h" +#include struct YGFloatOptional { private: diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGLayout.h b/packages/react-native/ReactCommon/yoga/yoga/YGLayout.h index 166eabb64d9106..78a1bcf19184f5 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGLayout.h +++ b/packages/react-native/ReactCommon/yoga/yoga/YGLayout.h @@ -7,9 +7,9 @@ #pragma once -#include "BitUtils.h" -#include "YGFloatOptional.h" -#include "Yoga-internal.h" +#include +#include +#include struct YGLayout { std::array position = {}; diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp b/packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp index cd323b54122ee9..db537576ce5b63 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp @@ -11,7 +11,8 @@ #include "Utils.h" using namespace facebook; -using facebook::yoga::detail::CompactValue; +using namespace facebook::yoga; +using facebook::yoga::CompactValue; YGNode::YGNode(const YGConfigRef config) : config_{config} { YGAssert( @@ -53,7 +54,7 @@ void YGNode::print(void* printContext) { } CompactValue YGNode::computeEdgeValueForRow( - const YGStyle::Edges& edges, + const Style::Edges& edges, YGEdge rowEdge, YGEdge edge, CompactValue defaultValue) { @@ -71,7 +72,7 @@ CompactValue YGNode::computeEdgeValueForRow( } CompactValue YGNode::computeEdgeValueForColumn( - const YGStyle::Edges& edges, + const Style::Edges& edges, YGEdge edge, CompactValue defaultValue) { if (!edges[edge].isUndefined()) { @@ -86,7 +87,7 @@ CompactValue YGNode::computeEdgeValueForColumn( } CompactValue YGNode::computeRowGap( - const YGStyle::Gutters& gutters, + const Style::Gutters& gutters, CompactValue defaultValue) { if (!gutters[YGGutterRow].isUndefined()) { return gutters[YGGutterRow]; @@ -98,7 +99,7 @@ CompactValue YGNode::computeRowGap( } CompactValue YGNode::computeColumnGap( - const YGStyle::Gutters& gutters, + const Style::Gutters& gutters, CompactValue defaultValue) { if (!gutters[YGGutterColumn].isUndefined()) { return gutters[YGGutterColumn]; @@ -431,7 +432,7 @@ YGValue YGNode::resolveFlexBasisPtr() const { void YGNode::resolveDimension() { using namespace yoga; - const YGStyle& style = getStyle(); + const Style& style = getStyle(); for (auto dim : {YGDimensionWidth, YGDimensionHeight}) { if (!style.maxDimensions()[dim].isUndefined() && YGValueEqual(style.maxDimensions()[dim], style.minDimensions()[dim])) { diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGNode.h b/packages/react-native/ReactCommon/yoga/yoga/YGNode.h index e3c202f2b9f684..b46ce4aa1dc0d0 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGNode.h +++ b/packages/react-native/ReactCommon/yoga/yoga/YGNode.h @@ -9,11 +9,12 @@ #include #include -#include "CompactValue.h" #include "YGConfig.h" #include "YGLayout.h" -#include "YGStyle.h" -#include "Yoga-internal.h" +#include + +#include +#include YGConfigRef YGConfigGetDefault(); @@ -52,7 +53,7 @@ struct YOGA_EXPORT YGNode { PrintWithContextFn withContext; } print_ = {nullptr}; YGDirtiedFunc dirtied_ = nullptr; - YGStyle style_ = {}; + facebook::yoga::Style style_ = {}; YGLayout layout_ = {}; uint32_t lineIndex_ = 0; YGNodeRef owner_ = nullptr; @@ -80,7 +81,7 @@ struct YOGA_EXPORT YGNode { // DO NOT CHANGE THE VISIBILITY OF THIS METHOD! YGNode& operator=(YGNode&&) = default; - using CompactValue = facebook::yoga::detail::CompactValue; + using CompactValue = facebook::yoga::CompactValue; public: YGNode() : YGNode{YGConfigGetDefault()} { flags_.hasNewLayout = true; } @@ -123,9 +124,9 @@ struct YOGA_EXPORT YGNode { YGDirtiedFunc getDirtied() const { return dirtied_; } // For Performance reasons passing as reference. - YGStyle& getStyle() { return style_; } + facebook::yoga::Style& getStyle() { return style_; } - const YGStyle& getStyle() const { return style_; } + const facebook::yoga::Style& getStyle() const { return style_; } // For Performance reasons passing as reference. YGLayout& getLayout() { return layout_; } @@ -178,22 +179,22 @@ struct YOGA_EXPORT YGNode { } static CompactValue computeEdgeValueForColumn( - const YGStyle::Edges& edges, + const facebook::yoga::Style::Edges& edges, YGEdge edge, CompactValue defaultValue); static CompactValue computeEdgeValueForRow( - const YGStyle::Edges& edges, + const facebook::yoga::Style::Edges& edges, YGEdge rowEdge, YGEdge edge, CompactValue defaultValue); static CompactValue computeRowGap( - const YGStyle::Gutters& gutters, + const facebook::yoga::Style::Gutters& gutters, CompactValue defaultValue); static CompactValue computeColumnGap( - const YGStyle::Gutters& gutters, + const facebook::yoga::Style::Gutters& gutters, CompactValue defaultValue); // Methods related to positions, margin, padding and border @@ -273,7 +274,7 @@ struct YOGA_EXPORT YGNode { void setDirtiedFunc(YGDirtiedFunc dirtiedFunc) { dirtied_ = dirtiedFunc; } - void setStyle(const YGStyle& style) { style_ = style; } + void setStyle(const facebook::yoga::Style& style) { style_ = style; } void setLayout(const YGLayout& layout) { layout_ = layout; } diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGNodePrint.cpp b/packages/react-native/ReactCommon/yoga/yoga/YGNodePrint.cpp index 7aa50f07d02176..c1205abbdc7251 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGNodePrint.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/YGNodePrint.cpp @@ -13,7 +13,7 @@ #include "YGNodePrint.h" #include "YGNode.h" -#include "Yoga-internal.h" +#include #include "Utils.h" namespace facebook::yoga { @@ -25,7 +25,7 @@ static void indent(string& base, uint32_t level) { } } -static bool areFourValuesEqual(const YGStyle::Edges& four) { +static bool areFourValuesEqual(const Style::Edges& four) { return YGValueEqual(four[0], four[1]) && YGValueEqual(four[0], four[2]) && YGValueEqual(four[0], four[3]); } @@ -90,10 +90,10 @@ static void appendNumberIfNotZero( static void appendEdges( string& base, const string& key, - const YGStyle::Edges& edges) { + const Style::Edges& edges) { if (areFourValuesEqual(edges)) { auto edgeValue = YGNode::computeEdgeValueForColumn( - edges, YGEdgeLeft, detail::CompactValue::ofZero()); + edges, YGEdgeLeft, CompactValue::ofZero()); appendNumberIfNotZero(base, key, edgeValue); } else { for (int edge = YGEdgeLeft; edge != YGEdgeAll; ++edge) { @@ -106,14 +106,14 @@ static void appendEdges( static void appendEdgeIfNotUndefined( string& base, const string& str, - const YGStyle::Edges& edges, + const Style::Edges& edges, const YGEdge edge) { // TODO: this doesn't take RTL / YGEdgeStart / YGEdgeEnd into account auto value = (edge == YGEdgeLeft || edge == YGEdgeRight) ? YGNode::computeEdgeValueForRow( - edges, edge, edge, detail::CompactValue::ofUndefined()) + edges, edge, edge, CompactValue::ofUndefined()) : YGNode::computeEdgeValueForColumn( - edges, edge, detail::CompactValue::ofUndefined()); + edges, edge, CompactValue::ofUndefined()); appendNumberIfNotUndefined(base, str, value); } @@ -188,17 +188,15 @@ void YGNodeToString( appendEdges(str, "padding", style.padding()); appendEdges(str, "border", style.border()); - if (YGNode::computeColumnGap( - style.gap(), detail::CompactValue::ofUndefined()) != + if (YGNode::computeColumnGap(style.gap(), CompactValue::ofUndefined()) != YGNode::computeColumnGap( - YGNode().getStyle().gap(), detail::CompactValue::ofUndefined())) { + YGNode().getStyle().gap(), CompactValue::ofUndefined())) { appendNumberIfNotUndefined( str, "column-gap", style.gap()[YGGutterColumn]); } - if (YGNode::computeRowGap( - style.gap(), detail::CompactValue::ofUndefined()) != + if (YGNode::computeRowGap(style.gap(), CompactValue::ofUndefined()) != YGNode::computeRowGap( - YGNode().getStyle().gap(), detail::CompactValue::ofUndefined())) { + YGNode().getStyle().gap(), CompactValue::ofUndefined())) { appendNumberIfNotUndefined(str, "row-gap", style.gap()[YGGutterRow]); } diff --git a/packages/react-native/ReactCommon/yoga/yoga/Yoga-internal.h b/packages/react-native/ReactCommon/yoga/yoga/Yoga-internal.h index c16c2ad5775c2b..d4e6df63e54c1f 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Yoga-internal.h +++ b/packages/react-native/ReactCommon/yoga/yoga/Yoga-internal.h @@ -14,7 +14,7 @@ #include -#include "CompactValue.h" +#include using YGVector = std::vector; diff --git a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp b/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp index ab0aa82526dbae..e8756ddfce1e6b 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp @@ -17,7 +17,7 @@ #include "Utils.h" #include "YGNode.h" #include "YGNodePrint.h" -#include "Yoga-internal.h" +#include #include "event/event.h" using namespace facebook::yoga; @@ -480,26 +480,25 @@ void updateStyle( } template -void updateStyle(YGNode* node, Ref (YGStyle::*prop)(), T value) { +void updateStyle(YGNode* node, Ref (Style::*prop)(), T value) { updateStyle( node, value, - [prop](YGStyle& s, T x) { return (s.*prop)() != x; }, - [prop](YGStyle& s, T x) { (s.*prop)() = x; }); + [prop](Style& s, T x) { return (s.*prop)() != x; }, + [prop](Style& s, T x) { (s.*prop)() = x; }); } template void updateIndexedStyleProp( YGNode* node, - Ref (YGStyle::*prop)(), + Ref (Style::*prop)(), Idx idx, - detail::CompactValue value) { - using detail::CompactValue; + CompactValue value) { updateStyle( node, value, - [idx, prop](YGStyle& s, CompactValue x) { return (s.*prop)()[idx] != x; }, - [idx, prop](YGStyle& s, CompactValue x) { (s.*prop)()[idx] = x; }); + [idx, prop](Style& s, CompactValue x) { return (s.*prop)()[idx] != x; }, + [idx, prop](Style& s, CompactValue x) { (s.*prop)()[idx] = x; }); } } // namespace @@ -509,12 +508,12 @@ void updateIndexedStyleProp( // 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(YGStyle{}.PROP()) +#define MSVC_HINT(PROP) decltype(Style{}.PROP()) YOGA_EXPORT void YGNodeStyleSetDirection( const YGNodeRef node, const YGDirection value) { - updateStyle(node, &YGStyle::direction, value); + updateStyle(node, &Style::direction, value); } YOGA_EXPORT YGDirection YGNodeStyleGetDirection(const YGNodeConstRef node) { return node->getStyle().direction(); @@ -524,7 +523,7 @@ YOGA_EXPORT void YGNodeStyleSetFlexDirection( const YGNodeRef node, const YGFlexDirection flexDirection) { updateStyle( - node, &YGStyle::flexDirection, flexDirection); + node, &Style::flexDirection, flexDirection); } YOGA_EXPORT YGFlexDirection YGNodeStyleGetFlexDirection(const YGNodeConstRef node) { @@ -535,7 +534,7 @@ YOGA_EXPORT void YGNodeStyleSetJustifyContent( const YGNodeRef node, const YGJustify justifyContent) { updateStyle( - node, &YGStyle::justifyContent, justifyContent); + node, &Style::justifyContent, justifyContent); } YOGA_EXPORT YGJustify YGNodeStyleGetJustifyContent(const YGNodeConstRef node) { return node->getStyle().justifyContent(); @@ -545,7 +544,7 @@ YOGA_EXPORT void YGNodeStyleSetAlignContent( const YGNodeRef node, const YGAlign alignContent) { updateStyle( - node, &YGStyle::alignContent, alignContent); + node, &Style::alignContent, alignContent); } YOGA_EXPORT YGAlign YGNodeStyleGetAlignContent(const YGNodeConstRef node) { return node->getStyle().alignContent(); @@ -554,7 +553,7 @@ YOGA_EXPORT YGAlign YGNodeStyleGetAlignContent(const YGNodeConstRef node) { YOGA_EXPORT void YGNodeStyleSetAlignItems( const YGNodeRef node, const YGAlign alignItems) { - updateStyle(node, &YGStyle::alignItems, alignItems); + updateStyle(node, &Style::alignItems, alignItems); } YOGA_EXPORT YGAlign YGNodeStyleGetAlignItems(const YGNodeConstRef node) { return node->getStyle().alignItems(); @@ -563,7 +562,7 @@ YOGA_EXPORT YGAlign YGNodeStyleGetAlignItems(const YGNodeConstRef node) { YOGA_EXPORT void YGNodeStyleSetAlignSelf( const YGNodeRef node, const YGAlign alignSelf) { - updateStyle(node, &YGStyle::alignSelf, alignSelf); + updateStyle(node, &Style::alignSelf, alignSelf); } YOGA_EXPORT YGAlign YGNodeStyleGetAlignSelf(const YGNodeConstRef node) { return node->getStyle().alignSelf(); @@ -573,7 +572,7 @@ YOGA_EXPORT void YGNodeStyleSetPositionType( const YGNodeRef node, const YGPositionType positionType) { updateStyle( - node, &YGStyle::positionType, positionType); + node, &Style::positionType, positionType); } YOGA_EXPORT YGPositionType YGNodeStyleGetPositionType(const YGNodeConstRef node) { @@ -583,7 +582,7 @@ YGNodeStyleGetPositionType(const YGNodeConstRef node) { YOGA_EXPORT void YGNodeStyleSetFlexWrap( const YGNodeRef node, const YGWrap flexWrap) { - updateStyle(node, &YGStyle::flexWrap, flexWrap); + updateStyle(node, &Style::flexWrap, flexWrap); } YOGA_EXPORT YGWrap YGNodeStyleGetFlexWrap(const YGNodeConstRef node) { return node->getStyle().flexWrap(); @@ -592,7 +591,7 @@ YOGA_EXPORT YGWrap YGNodeStyleGetFlexWrap(const YGNodeConstRef node) { YOGA_EXPORT void YGNodeStyleSetOverflow( const YGNodeRef node, const YGOverflow overflow) { - updateStyle(node, &YGStyle::overflow, overflow); + updateStyle(node, &Style::overflow, overflow); } YOGA_EXPORT YGOverflow YGNodeStyleGetOverflow(const YGNodeConstRef node) { return node->getStyle().overflow(); @@ -601,7 +600,7 @@ YOGA_EXPORT YGOverflow YGNodeStyleGetOverflow(const YGNodeConstRef node) { YOGA_EXPORT void YGNodeStyleSetDisplay( const YGNodeRef node, const YGDisplay display) { - updateStyle(node, &YGStyle::display, display); + updateStyle(node, &Style::display, display); } YOGA_EXPORT YGDisplay YGNodeStyleGetDisplay(const YGNodeConstRef node) { return node->getStyle().display(); @@ -609,7 +608,7 @@ YOGA_EXPORT YGDisplay YGNodeStyleGetDisplay(const YGNodeConstRef node) { // TODO(T26792433): Change the API to accept YGFloatOptional. YOGA_EXPORT void YGNodeStyleSetFlex(const YGNodeRef node, const float flex) { - updateStyle(node, &YGStyle::flex, YGFloatOptional{flex}); + updateStyle(node, &Style::flex, YGFloatOptional{flex}); } // TODO(T26792433): Change the API to accept YGFloatOptional. @@ -624,7 +623,7 @@ YOGA_EXPORT void YGNodeStyleSetFlexGrow( const YGNodeRef node, const float flexGrow) { updateStyle( - node, &YGStyle::flexGrow, YGFloatOptional{flexGrow}); + node, &Style::flexGrow, YGFloatOptional{flexGrow}); } // TODO(T26792433): Change the API to accept YGFloatOptional. @@ -632,7 +631,7 @@ YOGA_EXPORT void YGNodeStyleSetFlexShrink( const YGNodeRef node, const float flexShrink) { updateStyle( - node, &YGStyle::flexShrink, YGFloatOptional{flexShrink}); + node, &Style::flexShrink, YGFloatOptional{flexShrink}); } YOGA_EXPORT YGValue YGNodeStyleGetFlexBasis(const YGNodeConstRef node) { @@ -647,37 +646,37 @@ YOGA_EXPORT YGValue YGNodeStyleGetFlexBasis(const YGNodeConstRef node) { YOGA_EXPORT void YGNodeStyleSetFlexBasis( const YGNodeRef node, const float flexBasis) { - auto value = detail::CompactValue::ofMaybe(flexBasis); - updateStyle(node, &YGStyle::flexBasis, value); + auto value = CompactValue::ofMaybe(flexBasis); + updateStyle(node, &Style::flexBasis, value); } YOGA_EXPORT void YGNodeStyleSetFlexBasisPercent( const YGNodeRef node, const float flexBasisPercent) { - auto value = detail::CompactValue::ofMaybe(flexBasisPercent); - updateStyle(node, &YGStyle::flexBasis, value); + auto value = CompactValue::ofMaybe(flexBasisPercent); + updateStyle(node, &Style::flexBasis, value); } YOGA_EXPORT void YGNodeStyleSetFlexBasisAuto(const YGNodeRef node) { updateStyle( - node, &YGStyle::flexBasis, detail::CompactValue::ofAuto()); + node, &Style::flexBasis, CompactValue::ofAuto()); } YOGA_EXPORT void YGNodeStyleSetPosition( YGNodeRef node, YGEdge edge, float points) { - auto value = detail::CompactValue::ofMaybe(points); + auto value = CompactValue::ofMaybe(points); updateIndexedStyleProp( - node, &YGStyle::position, edge, value); + node, &Style::position, edge, value); } YOGA_EXPORT void YGNodeStyleSetPositionPercent( YGNodeRef node, YGEdge edge, float percent) { - auto value = detail::CompactValue::ofMaybe(percent); + auto value = CompactValue::ofMaybe(percent); updateIndexedStyleProp( - node, &YGStyle::position, edge, value); + node, &Style::position, edge, value); } YOGA_EXPORT YGValue YGNodeStyleGetPosition(YGNodeConstRef node, YGEdge edge) { return node->getStyle().position()[edge]; @@ -687,21 +686,19 @@ YOGA_EXPORT void YGNodeStyleSetMargin( YGNodeRef node, YGEdge edge, float points) { - auto value = detail::CompactValue::ofMaybe(points); - updateIndexedStyleProp( - node, &YGStyle::margin, edge, value); + auto value = CompactValue::ofMaybe(points); + updateIndexedStyleProp(node, &Style::margin, edge, value); } YOGA_EXPORT void YGNodeStyleSetMarginPercent( YGNodeRef node, YGEdge edge, float percent) { - auto value = detail::CompactValue::ofMaybe(percent); - updateIndexedStyleProp( - node, &YGStyle::margin, edge, value); + auto value = CompactValue::ofMaybe(percent); + updateIndexedStyleProp(node, &Style::margin, edge, value); } YOGA_EXPORT void YGNodeStyleSetMarginAuto(YGNodeRef node, YGEdge edge) { updateIndexedStyleProp( - node, &YGStyle::margin, edge, detail::CompactValue::ofAuto()); + node, &Style::margin, edge, CompactValue::ofAuto()); } YOGA_EXPORT YGValue YGNodeStyleGetMargin(YGNodeConstRef node, YGEdge edge) { return node->getStyle().margin()[edge]; @@ -711,17 +708,17 @@ YOGA_EXPORT void YGNodeStyleSetPadding( YGNodeRef node, YGEdge edge, float points) { - auto value = detail::CompactValue::ofMaybe(points); + auto value = CompactValue::ofMaybe(points); updateIndexedStyleProp( - node, &YGStyle::padding, edge, value); + node, &Style::padding, edge, value); } YOGA_EXPORT void YGNodeStyleSetPaddingPercent( YGNodeRef node, YGEdge edge, float percent) { - auto value = detail::CompactValue::ofMaybe(percent); + auto value = CompactValue::ofMaybe(percent); updateIndexedStyleProp( - node, &YGStyle::padding, edge, value); + node, &Style::padding, edge, value); } YOGA_EXPORT YGValue YGNodeStyleGetPadding(YGNodeConstRef node, YGEdge edge) { return node->getStyle().padding()[edge]; @@ -732,9 +729,8 @@ YOGA_EXPORT void YGNodeStyleSetBorder( const YGNodeRef node, const YGEdge edge, const float border) { - auto value = detail::CompactValue::ofMaybe(border); - updateIndexedStyleProp( - node, &YGStyle::border, edge, value); + auto value = CompactValue::ofMaybe(border); + updateIndexedStyleProp(node, &Style::border, edge, value); } YOGA_EXPORT float YGNodeStyleGetBorder( @@ -754,8 +750,8 @@ YOGA_EXPORT void YGNodeStyleSetGap( const YGNodeRef node, const YGGutter gutter, const float gapLength) { - auto length = detail::CompactValue::ofMaybe(gapLength); - updateIndexedStyleProp(node, &YGStyle::gap, gutter, length); + auto length = CompactValue::ofMaybe(gapLength); + updateIndexedStyleProp(node, &Style::gap, gutter, length); } YOGA_EXPORT float YGNodeStyleGetGap( @@ -784,46 +780,40 @@ YOGA_EXPORT void YGNodeStyleSetAspectRatio( const YGNodeRef node, const float aspectRatio) { updateStyle( - node, &YGStyle::aspectRatio, YGFloatOptional{aspectRatio}); + node, &Style::aspectRatio, YGFloatOptional{aspectRatio}); } YOGA_EXPORT void YGNodeStyleSetWidth(YGNodeRef node, float points) { - auto value = detail::CompactValue::ofMaybe(points); + auto value = CompactValue::ofMaybe(points); updateIndexedStyleProp( - node, &YGStyle::dimensions, YGDimensionWidth, value); + node, &Style::dimensions, YGDimensionWidth, value); } YOGA_EXPORT void YGNodeStyleSetWidthPercent(YGNodeRef node, float percent) { - auto value = detail::CompactValue::ofMaybe(percent); + auto value = CompactValue::ofMaybe(percent); updateIndexedStyleProp( - node, &YGStyle::dimensions, YGDimensionWidth, value); + node, &Style::dimensions, YGDimensionWidth, value); } YOGA_EXPORT void YGNodeStyleSetWidthAuto(YGNodeRef node) { updateIndexedStyleProp( - node, - &YGStyle::dimensions, - YGDimensionWidth, - detail::CompactValue::ofAuto()); + node, &Style::dimensions, YGDimensionWidth, CompactValue::ofAuto()); } YOGA_EXPORT YGValue YGNodeStyleGetWidth(YGNodeConstRef node) { return node->getStyle().dimensions()[YGDimensionWidth]; } YOGA_EXPORT void YGNodeStyleSetHeight(YGNodeRef node, float points) { - auto value = detail::CompactValue::ofMaybe(points); + auto value = CompactValue::ofMaybe(points); updateIndexedStyleProp( - node, &YGStyle::dimensions, YGDimensionHeight, value); + node, &Style::dimensions, YGDimensionHeight, value); } YOGA_EXPORT void YGNodeStyleSetHeightPercent(YGNodeRef node, float percent) { - auto value = detail::CompactValue::ofMaybe(percent); + auto value = CompactValue::ofMaybe(percent); updateIndexedStyleProp( - node, &YGStyle::dimensions, YGDimensionHeight, value); + node, &Style::dimensions, YGDimensionHeight, value); } YOGA_EXPORT void YGNodeStyleSetHeightAuto(YGNodeRef node) { updateIndexedStyleProp( - node, - &YGStyle::dimensions, - YGDimensionHeight, - detail::CompactValue::ofAuto()); + node, &Style::dimensions, YGDimensionHeight, CompactValue::ofAuto()); } YOGA_EXPORT YGValue YGNodeStyleGetHeight(YGNodeConstRef node) { return node->getStyle().dimensions()[YGDimensionHeight]; @@ -832,16 +822,16 @@ YOGA_EXPORT YGValue YGNodeStyleGetHeight(YGNodeConstRef node) { YOGA_EXPORT void YGNodeStyleSetMinWidth( const YGNodeRef node, const float minWidth) { - auto value = detail::CompactValue::ofMaybe(minWidth); + auto value = CompactValue::ofMaybe(minWidth); updateIndexedStyleProp( - node, &YGStyle::minDimensions, YGDimensionWidth, value); + node, &Style::minDimensions, YGDimensionWidth, value); } YOGA_EXPORT void YGNodeStyleSetMinWidthPercent( const YGNodeRef node, const float minWidth) { - auto value = detail::CompactValue::ofMaybe(minWidth); + auto value = CompactValue::ofMaybe(minWidth); updateIndexedStyleProp( - node, &YGStyle::minDimensions, YGDimensionWidth, value); + node, &Style::minDimensions, YGDimensionWidth, value); } YOGA_EXPORT YGValue YGNodeStyleGetMinWidth(const YGNodeConstRef node) { return node->getStyle().minDimensions()[YGDimensionWidth]; @@ -850,16 +840,16 @@ YOGA_EXPORT YGValue YGNodeStyleGetMinWidth(const YGNodeConstRef node) { YOGA_EXPORT void YGNodeStyleSetMinHeight( const YGNodeRef node, const float minHeight) { - auto value = detail::CompactValue::ofMaybe(minHeight); + auto value = CompactValue::ofMaybe(minHeight); updateIndexedStyleProp( - node, &YGStyle::minDimensions, YGDimensionHeight, value); + node, &Style::minDimensions, YGDimensionHeight, value); } YOGA_EXPORT void YGNodeStyleSetMinHeightPercent( const YGNodeRef node, const float minHeight) { - auto value = detail::CompactValue::ofMaybe(minHeight); + auto value = CompactValue::ofMaybe(minHeight); updateIndexedStyleProp( - node, &YGStyle::minDimensions, YGDimensionHeight, value); + node, &Style::minDimensions, YGDimensionHeight, value); } YOGA_EXPORT YGValue YGNodeStyleGetMinHeight(const YGNodeConstRef node) { return node->getStyle().minDimensions()[YGDimensionHeight]; @@ -868,16 +858,16 @@ YOGA_EXPORT YGValue YGNodeStyleGetMinHeight(const YGNodeConstRef node) { YOGA_EXPORT void YGNodeStyleSetMaxWidth( const YGNodeRef node, const float maxWidth) { - auto value = detail::CompactValue::ofMaybe(maxWidth); + auto value = CompactValue::ofMaybe(maxWidth); updateIndexedStyleProp( - node, &YGStyle::maxDimensions, YGDimensionWidth, value); + node, &Style::maxDimensions, YGDimensionWidth, value); } YOGA_EXPORT void YGNodeStyleSetMaxWidthPercent( const YGNodeRef node, const float maxWidth) { - auto value = detail::CompactValue::ofMaybe(maxWidth); + auto value = CompactValue::ofMaybe(maxWidth); updateIndexedStyleProp( - node, &YGStyle::maxDimensions, YGDimensionWidth, value); + node, &Style::maxDimensions, YGDimensionWidth, value); } YOGA_EXPORT YGValue YGNodeStyleGetMaxWidth(const YGNodeConstRef node) { return node->getStyle().maxDimensions()[YGDimensionWidth]; @@ -886,16 +876,16 @@ YOGA_EXPORT YGValue YGNodeStyleGetMaxWidth(const YGNodeConstRef node) { YOGA_EXPORT void YGNodeStyleSetMaxHeight( const YGNodeRef node, const float maxHeight) { - auto value = detail::CompactValue::ofMaybe(maxHeight); + auto value = CompactValue::ofMaybe(maxHeight); updateIndexedStyleProp( - node, &YGStyle::maxDimensions, YGDimensionHeight, value); + node, &Style::maxDimensions, YGDimensionHeight, value); } YOGA_EXPORT void YGNodeStyleSetMaxHeightPercent( const YGNodeRef node, const float maxHeight) { - auto value = detail::CompactValue::ofMaybe(maxHeight); + auto value = CompactValue::ofMaybe(maxHeight); updateIndexedStyleProp( - node, &YGStyle::maxDimensions, YGDimensionHeight, value); + node, &Style::maxDimensions, YGDimensionHeight, value); } YOGA_EXPORT YGValue YGNodeStyleGetMaxHeight(const YGNodeConstRef node) { return node->getStyle().maxDimensions()[YGDimensionHeight]; @@ -2519,7 +2509,7 @@ static void YGJustifyMainAxis( i < collectedFlexItemsValues.endOfLineIndex; i++) { const YGNodeRef child = node->getChild(i); - const YGStyle& childStyle = child->getStyle(); + const Style& childStyle = child->getStyle(); const YGLayout childLayout = child->getLayout(); const bool isLastChild = i == collectedFlexItemsValues.endOfLineIndex - 1; // remove the gap if it is the last element of the line diff --git a/packages/react-native/ReactCommon/yoga/yoga/CompactValue.h b/packages/react-native/ReactCommon/yoga/yoga/style/CompactValue.h similarity index 98% rename from packages/react-native/ReactCommon/yoga/yoga/CompactValue.h rename to packages/react-native/ReactCommon/yoga/yoga/style/CompactValue.h index 9d494500111f5f..498b984a70bfbb 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/CompactValue.h +++ b/packages/react-native/ReactCommon/yoga/yoga/style/CompactValue.h @@ -38,7 +38,7 @@ static_assert( #define VISIBLE_FOR_TESTING private: #endif -namespace facebook::yoga::detail { +namespace facebook::yoga { // This class stores YGValue in 32 bits. // - The value does not matter for Undefined and Auto. NaNs are used for their @@ -207,4 +207,4 @@ constexpr bool operator!=(CompactValue a, CompactValue b) noexcept { return !(a == b); } -} // namespace facebook::yoga::detail +} // namespace facebook::yoga diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGStyle.cpp b/packages/react-native/ReactCommon/yoga/yoga/style/Style.cpp similarity index 93% rename from packages/react-native/ReactCommon/yoga/yoga/YGStyle.cpp rename to packages/react-native/ReactCommon/yoga/yoga/style/Style.cpp index 37153f2e9276f3..806f24289b835e 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGStyle.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/style/Style.cpp @@ -5,11 +5,13 @@ * LICENSE file in the root directory of this source tree. */ -#include "YGStyle.h" -#include "Utils.h" +#include +#include + +namespace facebook::yoga { // Yoga specific properties, not compatible with flexbox specification -bool operator==(const YGStyle& lhs, const YGStyle& rhs) { +bool operator==(const Style& lhs, const Style& rhs) { bool areNonFloatValuesEqual = lhs.direction() == rhs.direction() && lhs.flexDirection() == rhs.flexDirection() && lhs.justifyContent() == rhs.justifyContent() && @@ -54,3 +56,5 @@ bool operator==(const YGStyle& lhs, const YGStyle& rhs) { return areNonFloatValuesEqual; } + +} // namespace facebook::yoga diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGStyle.h b/packages/react-native/ReactCommon/yoga/yoga/style/Style.h similarity index 80% rename from packages/react-native/ReactCommon/yoga/yoga/YGStyle.h rename to packages/react-native/ReactCommon/yoga/yoga/style/Style.h index 4f182d0691ec3f..c323346e45dad0 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGStyle.h +++ b/packages/react-native/ReactCommon/yoga/yoga/style/Style.h @@ -13,17 +13,17 @@ #include #include +#include +#include +#include -#include "CompactValue.h" -#include "YGFloatOptional.h" -#include "Yoga-internal.h" -#include "BitUtils.h" +#include -class YOGA_EXPORT YGStyle { +namespace facebook::yoga { + +class YOGA_EXPORT Style { template - using Values = - facebook::yoga::detail::Values()>; - using CompactValue = facebook::yoga::detail::CompactValue; + using Values = detail::Values()>; public: using Dimensions = Values; @@ -32,7 +32,7 @@ class YOGA_EXPORT YGStyle { template struct BitfieldRef { - YGStyle& style; + Style& style; size_t offset; operator T() const { return facebook::yoga::detail::getEnumData(style.flags, offset); @@ -43,9 +43,9 @@ class YOGA_EXPORT YGStyle { } }; - template + template struct Ref { - YGStyle& style; + Style& style; operator T() const { return style.*Prop; } Ref& operator=(T value) { style.*Prop = value; @@ -53,10 +53,10 @@ class YOGA_EXPORT YGStyle { } }; - template YGStyle::*Prop> + template Style::*Prop> struct IdxRef { struct Ref { - YGStyle& style; + Style& style; Idx idx; operator CompactValue() const { return (style.*Prop)[idx]; } operator YGValue() const { return (style.*Prop)[idx]; } @@ -66,7 +66,7 @@ class YOGA_EXPORT YGStyle { } }; - YGStyle& style; + Style& style; IdxRef& operator=(const Values& values) { style.*Prop = values; return *this; @@ -76,11 +76,11 @@ class YOGA_EXPORT YGStyle { CompactValue operator[](Idx idx) const { return (style.*Prop)[idx]; } }; - YGStyle() { + Style() { alignContent() = YGAlignFlexStart; alignItems() = YGAlignStretch; } - ~YGStyle() = default; + ~Style() = default; private: static constexpr size_t directionOffset = 0; @@ -188,51 +188,52 @@ class YOGA_EXPORT YGStyle { BitfieldRef display() { return {*this, displayOffset}; } YGFloatOptional flex() const { return flex_; } - Ref flex() { return {*this}; } + Ref flex() { return {*this}; } YGFloatOptional flexGrow() const { return flexGrow_; } - Ref flexGrow() { return {*this}; } + Ref flexGrow() { return {*this}; } YGFloatOptional flexShrink() const { return flexShrink_; } - Ref flexShrink() { return {*this}; } + Ref flexShrink() { return {*this}; } CompactValue flexBasis() const { return flexBasis_; } - Ref flexBasis() { return {*this}; } + Ref flexBasis() { return {*this}; } const Edges& margin() const { return margin_; } - IdxRef margin() { return {*this}; } + IdxRef margin() { return {*this}; } const Edges& position() const { return position_; } - IdxRef position() { return {*this}; } + IdxRef position() { return {*this}; } const Edges& padding() const { return padding_; } - IdxRef padding() { return {*this}; } + IdxRef padding() { return {*this}; } const Edges& border() const { return border_; } - IdxRef border() { return {*this}; } + IdxRef border() { return {*this}; } const Gutters& gap() const { return gap_; } - IdxRef gap() { return {*this}; } + IdxRef gap() { return {*this}; } const Dimensions& dimensions() const { return dimensions_; } - IdxRef dimensions() { return {*this}; } + IdxRef dimensions() { return {*this}; } const Dimensions& minDimensions() const { return minDimensions_; } - IdxRef minDimensions() { + IdxRef minDimensions() { return {*this}; } const Dimensions& maxDimensions() const { return maxDimensions_; } - IdxRef maxDimensions() { + IdxRef maxDimensions() { return {*this}; } // Yoga specific properties, not compatible with flexbox specification YGFloatOptional aspectRatio() const { return aspectRatio_; } - Ref aspectRatio() { return {*this}; } + Ref aspectRatio() { return {*this}; } }; -YOGA_EXPORT bool operator==(const YGStyle& lhs, const YGStyle& rhs); -YOGA_EXPORT inline bool operator!=(const YGStyle& lhs, const YGStyle& rhs) { +YOGA_EXPORT bool operator==(const Style& lhs, const Style& rhs); +YOGA_EXPORT inline bool operator!=(const Style& lhs, const Style& rhs) { return !(lhs == rhs); } +} // namespace facebook::yoga From 06a57f7b6bf3813e3ae2989395cf4a1fa10ee2a6 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Wed, 30 Aug 2023 15:12:38 -0700 Subject: [PATCH 2/2] C++ Cleanup 2/N: Reorganize YGConfig (#39169) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/39169 X-link: https://github.com/facebook/yoga/pull/1348 ## This diff This diff adds a top level `config` directory for code related to configuring Yoga and Yoga Nodes. The public API for config handles is `YGConfigRef`, which is forward declared to be a pointer to a struct named `YGConfig`. The existing `YGConfig` is split into `yoga::Config`, as the private C++ implementation, inheriting from `YGConfig`, a marker type represented as an empty struct. The public API continues to accept `YGConfigRef`, which continues to be `YGConfig *`, but it must be cast to its concrete internal representation at the API boundary before doing work on it. ## This stack The organization of the C++ internals of Yoga are in need of attention. 1. Some of the C++ internals are namespaced, but others not. 2. Some of the namespaces include `detail`, but are meant to be used outside of the translation unit (FB Clang Tidy rules warn on any usage of these) 2. Most of the files are in a flat hierarchy, except for event tracing in its own folder 3. Some files and functions begin with YG, others don’t 4. Some functions are uppercase, others are not 5. Almost all of the interesting logic is in Yoga.cpp, and the file is too large to reason about 6. There are multiple grab bag files where folks put random functions they need in (Utils, BitUtils, Yoga-Internal.h) 7. There is no clear indication from file structure or type naming what is private vs not 8. Handles like `YGNodeRef` and `YGConfigRef` can be used to access internals just by importing headers This stack does some much needed spring cleaning: 1. All non-public headers and C++ implementation details are in separate folders from the root level `yoga`. This will give us room to split up logic and add more files without too large a flat hierarchy 3. All private C++ internals are under the `facebook::yoga` namespace. Details namespaces are only ever used within the same header, as they are intended 4. Utils files are split 5. Most C++ internals drop the YG prefix 6. Most C++ internal function names are all lower camel case 7. We start to split up Yoga.cpp 8. Every header beginning with YG or at the top-level directory is public and C only, with the exception of Yoga-Internal.h which has non-public functions for bindings 9. It is not possible to use private APIs without static casting handles to internal classes This will give us more leeway to continue splitting monolithic files, and consistent guidelines for style in new files as well. These changes should not be breaking to any project using only public Yoga headers. This includes every usage of Yoga in fbsource except for RN Fabric which is currently tied to internals. This refactor should make that boundary clearer. Changelog: [Internal] Reviewed By: shwanton Differential Revision: D48847257 fbshipit-source-id: 83141f65706973a4452303615acd16c1f1d2cac8 --- .../first-party/yogajni/jni/YGJNIVanilla.cpp | 4 +- .../view/YogaLayoutableShadowNode.cpp | 6 +- .../view/YogaLayoutableShadowNode.h | 6 +- .../ReactCommon/yoga/yoga/YGNode.cpp | 4 +- .../ReactCommon/yoga/yoga/YGNode.h | 17 +++-- .../ReactCommon/yoga/yoga/Yoga.cpp | 73 ++++++++++--------- .../react-native/ReactCommon/yoga/yoga/Yoga.h | 1 - .../yoga/{YGConfig.cpp => config/Config.cpp} | 70 +++++++++--------- .../yoga/yoga/{YGConfig.h => config/Config.h} | 37 ++++++---- .../ReactCommon/yoga/yoga/log.cpp | 12 +-- .../react-native/ReactCommon/yoga/yoga/log.h | 3 +- 11 files changed, 123 insertions(+), 110 deletions(-) rename packages/react-native/ReactCommon/yoga/yoga/{YGConfig.cpp => config/Config.cpp} (56%) rename packages/react-native/ReactCommon/yoga/yoga/{YGConfig.h => config/Config.h} (75%) diff --git a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp index bc108b5dafd323..5d802784154970 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp @@ -19,6 +19,8 @@ // API and use that #include +using namespace facebook; +using namespace facebook::yoga; using namespace facebook::yoga::vanillajni; static inline ScopedLocalRef YGNodeJobject( @@ -194,7 +196,7 @@ static void jni_YGConfigSetLoggerJNI( } *context = newGlobalRef(env, logger); - config->setLogger(YGJNILogFunc); + static_cast(config)->setLogger(YGJNILogFunc); } else { if (context != nullptr) { delete context; diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp index a5efc5b40a2a5c..a3f2b4f5a6ef4b 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp @@ -473,7 +473,7 @@ void YogaLayoutableShadowNode::configureYogaTree( const auto &child = *yogaLayoutableChildren_[i]; auto childLayoutMetrics = child.getLayoutMetrics(); auto childErrata = - YGConfigGetErrata(const_cast(&child.yogaConfig_)); + YGConfigGetErrata(const_cast(&child.yogaConfig_)); if (child.yogaTreeHasBeenConfigured_ && childLayoutMetrics.pointScaleFactor == pointScaleFactor && @@ -834,8 +834,8 @@ YogaLayoutableShadowNode &YogaLayoutableShadowNode::shadowNodeFromContext( *static_cast(yogaNode->getContext())); } -YGConfig &YogaLayoutableShadowNode::initializeYogaConfig( - YGConfig &config, +yoga::Config &YogaLayoutableShadowNode::initializeYogaConfig( + yoga::Config &config, const YGConfigRef previousConfig) { YGConfigSetCloneNodeFunc( &config, YogaLayoutableShadowNode::yogaNodeCloneCallbackConnector); diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h index dbb46a0161ec79..7257c7bd36b961 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h @@ -93,7 +93,7 @@ class YogaLayoutableShadowNode : public LayoutableShadowNode { /* * Yoga config associated (only) with this particular node. */ - YGConfig yogaConfig_; + yoga::Config yogaConfig_; /* * All Yoga functions only accept non-const arguments, so we have to mark @@ -153,8 +153,8 @@ class YogaLayoutableShadowNode : public LayoutableShadowNode { */ YogaLayoutableShadowNode &cloneChildInPlace(int32_t layoutableChildIndex); - static YGConfig &initializeYogaConfig( - YGConfig &config, + static yoga::Config &initializeYogaConfig( + yoga::Config &config, YGConfigRef previousConfig = nullptr); static YGNode *yogaNodeCloneCallbackConnector( YGNode *oldYogaNode, diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp b/packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp index db537576ce5b63..7389354e68de19 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp @@ -14,7 +14,7 @@ using namespace facebook; using namespace facebook::yoga; using facebook::yoga::CompactValue; -YGNode::YGNode(const YGConfigRef config) : config_{config} { +YGNode::YGNode(yoga::Config* config) : config_{config} { YGAssert( config != nullptr, "Attempting to construct YGNode with null config"); @@ -264,7 +264,7 @@ void YGNode::insertChild(YGNodeRef child, uint32_t index) { children_.insert(children_.begin() + index, child); } -void YGNode::setConfig(YGConfigRef config) { +void YGNode::setConfig(yoga::Config* config) { YGAssert(config != nullptr, "Attempting to set a null config on a YGNode"); YGAssertWithConfig( config, diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGNode.h b/packages/react-native/ReactCommon/yoga/yoga/YGNode.h index b46ce4aa1dc0d0..218cb989ffed53 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGNode.h +++ b/packages/react-native/ReactCommon/yoga/yoga/YGNode.h @@ -9,15 +9,13 @@ #include #include -#include "YGConfig.h" +#include #include "YGLayout.h" #include #include #include -YGConfigRef YGConfigGetDefault(); - #pragma pack(push) #pragma pack(1) struct YGNodeFlags { @@ -58,7 +56,7 @@ struct YOGA_EXPORT YGNode { uint32_t lineIndex_ = 0; YGNodeRef owner_ = nullptr; YGVector children_ = {}; - YGConfigRef config_; + facebook::yoga::Config* config_; std::array resolvedDimensions_ = { {YGValueUndefined, YGValueUndefined}}; @@ -84,8 +82,11 @@ struct YOGA_EXPORT YGNode { using CompactValue = facebook::yoga::CompactValue; public: - YGNode() : YGNode{YGConfigGetDefault()} { flags_.hasNewLayout = true; } - explicit YGNode(const YGConfigRef config); + YGNode() + : YGNode{static_cast(YGConfigGetDefault())} { + flags_.hasNewLayout = true; + } + explicit YGNode(facebook::yoga::Config* config); ~YGNode() = default; // cleanup of owner/children relationships in YGNodeFree YGNode(YGNode&&); @@ -166,7 +167,7 @@ struct YOGA_EXPORT YGNode { YGNodeRef getChild(uint32_t index) const { return children_.at(index); } - YGConfigRef getConfig() const { return config_; } + facebook::yoga::Config* getConfig() const { return config_; } bool isDirty() const { return flags_.isDirty; } @@ -290,7 +291,7 @@ struct YOGA_EXPORT YGNode { // TODO: rvalue override for setChildren - void setConfig(YGConfigRef config); + void setConfig(facebook::yoga::Config* config); void setDirty(bool isDirty); void setLayoutLastOwnerDirection(YGDirection direction); diff --git a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp b/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp index e8756ddfce1e6b..b52ded0e0899c5 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp @@ -20,6 +20,7 @@ #include #include "event/event.h" +using namespace facebook; using namespace facebook::yoga; using detail::Log; @@ -119,7 +120,7 @@ YOGA_EXPORT YGConfigRef YGNodeGetConfig(YGNodeRef node) { } YOGA_EXPORT void YGNodeSetConfig(YGNodeRef node, YGConfigRef config) { - node->setConfig(config); + node->setConfig(static_cast(config)); } YOGA_EXPORT bool YGNodeHasMeasureFunc(YGNodeRef node) { @@ -161,7 +162,7 @@ YOGA_EXPORT bool YGNodeGetHasNewLayout(YGNodeRef node) { } YOGA_EXPORT void YGConfigSetPrintTreeFlag(YGConfigRef config, bool enabled) { - config->setShouldPrintTree(enabled); + static_cast(config)->setShouldPrintTree(enabled); } YOGA_EXPORT void YGNodeSetHasNewLayout(YGNodeRef node, bool hasNewLayout) { @@ -188,7 +189,7 @@ YOGA_EXPORT void YGNodeMarkDirtyAndPropagateToDescendants( int32_t gConfigInstanceCount = 0; YOGA_EXPORT WIN_EXPORT YGNodeRef YGNodeNewWithConfig(const YGConfigRef config) { - const YGNodeRef node = new YGNode{config}; + const YGNodeRef node = new YGNode{static_cast(config)}; YGAssert(config != nullptr, "Tried to construct YGNode with null config"); YGAssertWithConfig( config, node != nullptr, "Could not allocate memory for node"); @@ -272,23 +273,19 @@ YOGA_EXPORT int32_t YGConfigGetInstanceCount(void) { YOGA_EXPORT YGConfigRef YGConfigNew(void) { #ifdef ANDROID - const YGConfigRef config = new YGConfig(YGAndroidLog); + const YGConfigRef config = new yoga::Config(YGAndroidLog); #else - const YGConfigRef config = new YGConfig(YGDefaultLog); + const YGConfigRef config = new yoga::Config(YGDefaultLog); #endif gConfigInstanceCount++; return config; } YOGA_EXPORT void YGConfigFree(const YGConfigRef config) { - delete config; + delete static_cast(config); gConfigInstanceCount--; } -void YGConfigCopy(const YGConfigRef dest, const YGConfigRef src) { - memcpy(dest, src, sizeof(YGConfig)); -} - YOGA_EXPORT void YGNodeSetIsReferenceBaseline( YGNodeRef node, bool isReferenceBaseline) { @@ -3715,22 +3712,22 @@ YOGA_EXPORT bool YGNodeCanUseCachedMeasurement( return false; } bool useRoundedComparison = - config != nullptr && config->getPointScaleFactor() != 0; + config != nullptr && YGConfigGetPointScaleFactor(config) != 0; const float effectiveWidth = useRoundedComparison ? YGRoundValueToPixelGrid( - width, config->getPointScaleFactor(), false, false) + width, YGConfigGetPointScaleFactor(config), false, false) : width; const float effectiveHeight = useRoundedComparison ? YGRoundValueToPixelGrid( - height, config->getPointScaleFactor(), false, false) + height, YGConfigGetPointScaleFactor(config), false, false) : height; const float effectiveLastWidth = useRoundedComparison ? YGRoundValueToPixelGrid( - lastWidth, config->getPointScaleFactor(), false, false) + lastWidth, YGConfigGetPointScaleFactor(config), false, false) : lastWidth; const float effectiveLastHeight = useRoundedComparison ? YGRoundValueToPixelGrid( - lastHeight, config->getPointScaleFactor(), false, false) + lastHeight, YGConfigGetPointScaleFactor(config), false, false) : lastHeight; const bool hasSameWidthSpec = lastWidthMode == widthMode && @@ -4057,14 +4054,14 @@ YOGA_EXPORT void YGConfigSetPointScaleFactor( // We store points for Pixel as we will use it for rounding if (pixelsInPoint == 0.0f) { // Zero is used to skip rounding - config->setPointScaleFactor(0.0f); + static_cast(config)->setPointScaleFactor(0.0f); } else { - config->setPointScaleFactor(pixelsInPoint); + static_cast(config)->setPointScaleFactor(pixelsInPoint); } } YOGA_EXPORT float YGConfigGetPointScaleFactor(const YGConfigRef config) { - return config->getPointScaleFactor(); + return static_cast(config)->getPointScaleFactor(); } static void YGRoundToPixelGrid( @@ -4239,12 +4236,12 @@ YOGA_EXPORT void YGNodeCalculateLayout( YOGA_EXPORT void YGConfigSetLogger(const YGConfigRef config, YGLogger logger) { if (logger != nullptr) { - config->setLogger(logger); + static_cast(config)->setLogger(logger); } else { #ifdef ANDROID - config->setLogger(&YGAndroidLog); + static_cast(config)->setLogger(&YGAndroidLog); #else - config->setLogger(&YGDefaultLog); + static_cast(config)->setLogger(&YGDefaultLog); #endif } } @@ -4271,7 +4268,12 @@ void YGAssertWithConfig( const bool condition, const char* message) { if (!condition) { - Log::log(config, YGLogLevelFatal, nullptr, "%s\n", message); + Log::log( + static_cast(config), + YGLogLevelFatal, + nullptr, + "%s\n", + message); throwLogicalErrorWithMessage(message); } } @@ -4280,58 +4282,61 @@ YOGA_EXPORT void YGConfigSetExperimentalFeatureEnabled( const YGConfigRef config, const YGExperimentalFeature feature, const bool enabled) { - config->setExperimentalFeatureEnabled(feature, enabled); + static_cast(config)->setExperimentalFeatureEnabled( + feature, enabled); } YOGA_EXPORT bool YGConfigIsExperimentalFeatureEnabled( const YGConfigRef config, const YGExperimentalFeature feature) { - return config->isExperimentalFeatureEnabled(feature); + return static_cast(config)->isExperimentalFeatureEnabled( + feature); } YOGA_EXPORT void YGConfigSetUseWebDefaults( const YGConfigRef config, const bool enabled) { - config->setUseWebDefaults(enabled); + static_cast(config)->setUseWebDefaults(enabled); } YOGA_EXPORT bool YGConfigGetUseLegacyStretchBehaviour( const YGConfigRef config) { - return config->hasErrata(YGErrataStretchFlexBasis); + return static_cast(config)->hasErrata( + YGErrataStretchFlexBasis); } YOGA_EXPORT void YGConfigSetUseLegacyStretchBehaviour( const YGConfigRef config, const bool useLegacyStretchBehaviour) { if (useLegacyStretchBehaviour) { - config->addErrata(YGErrataStretchFlexBasis); + static_cast(config)->addErrata(YGErrataStretchFlexBasis); } else { - config->removeErrata(YGErrataStretchFlexBasis); + static_cast(config)->removeErrata(YGErrataStretchFlexBasis); } } bool YGConfigGetUseWebDefaults(const YGConfigRef config) { - return config->useWebDefaults(); + return static_cast(config)->useWebDefaults(); } YOGA_EXPORT void YGConfigSetContext(const YGConfigRef config, void* context) { - config->setContext(context); + static_cast(config)->setContext(context); } YOGA_EXPORT void* YGConfigGetContext(const YGConfigRef config) { - return config->getContext(); + return static_cast(config)->getContext(); } YOGA_EXPORT void YGConfigSetErrata(YGConfigRef config, YGErrata errata) { - config->setErrata(errata); + static_cast(config)->setErrata(errata); } YOGA_EXPORT YGErrata YGConfigGetErrata(YGConfigRef config) { - return config->getErrata(); + return static_cast(config)->getErrata(); } YOGA_EXPORT void YGConfigSetCloneNodeFunc( const YGConfigRef config, const YGCloneNodeFunc callback) { - config->setCloneNodeCallback(callback); + static_cast(config)->setCloneNodeCallback(callback); } diff --git a/packages/react-native/ReactCommon/yoga/yoga/Yoga.h b/packages/react-native/ReactCommon/yoga/yoga/Yoga.h index de07aeac8b712a..e8bf0afffd26a6 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Yoga.h +++ b/packages/react-native/ReactCommon/yoga/yoga/Yoga.h @@ -343,7 +343,6 @@ void YGConfigSetUseLegacyStretchBehaviour( // YGConfig WIN_EXPORT YGConfigRef YGConfigNew(void); WIN_EXPORT void YGConfigFree(YGConfigRef config); -WIN_EXPORT void YGConfigCopy(YGConfigRef dest, YGConfigRef src); WIN_EXPORT int32_t YGConfigGetInstanceCount(void); WIN_EXPORT void YGConfigSetExperimentalFeatureEnabled( diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGConfig.cpp b/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp similarity index 56% rename from packages/react-native/ReactCommon/yoga/yoga/YGConfig.cpp rename to packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp index 93c16c69bf3ab4..2f07454b976020 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGConfig.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp @@ -5,133 +5,129 @@ * LICENSE file in the root directory of this source tree. */ -#include "YGConfig.h" - -using namespace facebook::yoga; +#include namespace facebook::yoga { -bool configUpdateInvalidatesLayout(YGConfigRef a, YGConfigRef b) { + +bool configUpdateInvalidatesLayout(Config* a, Config* b) { return a->getErrata() != b->getErrata() || a->getEnabledExperiments() != b->getEnabledExperiments() || a->getPointScaleFactor() != b->getPointScaleFactor() || a->useWebDefaults() != b->useWebDefaults(); } -} // namespace facebook::yoga -YGConfig::YGConfig(YGLogger logger) : cloneNodeCallback_{nullptr} { +Config::Config(YGLogger logger) : cloneNodeCallback_{nullptr} { setLogger(logger); } -void YGConfig::setUseWebDefaults(bool useWebDefaults) { +void Config::setUseWebDefaults(bool useWebDefaults) { flags_.useWebDefaults = useWebDefaults; } -bool YGConfig::useWebDefaults() const { +bool Config::useWebDefaults() const { return flags_.useWebDefaults; } -void YGConfig::setShouldPrintTree(bool printTree) { +void Config::setShouldPrintTree(bool printTree) { flags_.printTree = printTree; } -bool YGConfig::shouldPrintTree() const { +bool Config::shouldPrintTree() const { return flags_.printTree; } -void YGConfig::setExperimentalFeatureEnabled( +void Config::setExperimentalFeatureEnabled( YGExperimentalFeature feature, bool enabled) { experimentalFeatures_.set(feature, enabled); } -bool YGConfig::isExperimentalFeatureEnabled( - YGExperimentalFeature feature) const { +bool Config::isExperimentalFeatureEnabled(YGExperimentalFeature feature) const { return experimentalFeatures_.test(feature); } -ExperimentalFeatureSet YGConfig::getEnabledExperiments() const { +ExperimentalFeatureSet Config::getEnabledExperiments() const { return experimentalFeatures_; } -void YGConfig::setErrata(YGErrata errata) { +void Config::setErrata(YGErrata errata) { errata_ = errata; } -void YGConfig::addErrata(YGErrata errata) { +void Config::addErrata(YGErrata errata) { errata_ |= errata; } -void YGConfig::removeErrata(YGErrata errata) { +void Config::removeErrata(YGErrata errata) { errata_ &= (~errata); } -YGErrata YGConfig::getErrata() const { +YGErrata Config::getErrata() const { return errata_; } -bool YGConfig::hasErrata(YGErrata errata) const { +bool Config::hasErrata(YGErrata errata) const { return (errata_ & errata) != YGErrataNone; } -void YGConfig::setPointScaleFactor(float pointScaleFactor) { +void Config::setPointScaleFactor(float pointScaleFactor) { pointScaleFactor_ = pointScaleFactor; } -float YGConfig::getPointScaleFactor() const { +float Config::getPointScaleFactor() const { return pointScaleFactor_; } -void YGConfig::setContext(void* context) { +void Config::setContext(void* context) { context_ = context; } -void* YGConfig::getContext() const { +void* Config::getContext() const { return context_; } -void YGConfig::setLogger(YGLogger logger) { +void Config::setLogger(YGLogger logger) { logger_.noContext = logger; flags_.loggerUsesContext = false; } -void YGConfig::setLogger(LogWithContextFn logger) { +void Config::setLogger(LogWithContextFn logger) { logger_.withContext = logger; flags_.loggerUsesContext = true; } -void YGConfig::setLogger(std::nullptr_t) { +void Config::setLogger(std::nullptr_t) { setLogger(YGLogger{nullptr}); } -void YGConfig::log( - YGConfig* config, - YGNode* node, +void Config::log( + YGNodeRef node, YGLogLevel logLevel, void* logContext, const char* format, - va_list args) const { + va_list args) { if (flags_.loggerUsesContext) { - logger_.withContext(config, node, logLevel, logContext, format, args); + logger_.withContext(this, node, logLevel, logContext, format, args); } else { - logger_.noContext(config, node, logLevel, format, args); + logger_.noContext(this, node, logLevel, format, args); } } -void YGConfig::setCloneNodeCallback(YGCloneNodeFunc cloneNode) { +void Config::setCloneNodeCallback(YGCloneNodeFunc cloneNode) { cloneNodeCallback_.noContext = cloneNode; flags_.cloneNodeUsesContext = false; } -void YGConfig::setCloneNodeCallback(CloneWithContextFn cloneNode) { +void Config::setCloneNodeCallback(CloneWithContextFn cloneNode) { cloneNodeCallback_.withContext = cloneNode; flags_.cloneNodeUsesContext = true; } -void YGConfig::setCloneNodeCallback(std::nullptr_t) { +void Config::setCloneNodeCallback(std::nullptr_t) { setCloneNodeCallback(YGCloneNodeFunc{nullptr}); } -YGNodeRef YGConfig::cloneNode( +YGNodeRef Config::cloneNode( YGNodeRef node, YGNodeRef owner, int childIndex, @@ -147,3 +143,5 @@ YGNodeRef YGConfig::cloneNode( } return clone; } + +} // namespace facebook::yoga diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGConfig.h b/packages/react-native/ReactCommon/yoga/yoga/config/Config.h similarity index 75% rename from packages/react-native/ReactCommon/yoga/yoga/YGConfig.h rename to packages/react-native/ReactCommon/yoga/yoga/config/Config.h index fa530fab05606b..6470ebde4dbee5 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGConfig.h +++ b/packages/react-native/ReactCommon/yoga/yoga/config/Config.h @@ -12,11 +12,16 @@ #include #include +// Tag struct used to form the opaque YGConfigRef for the public C API +struct YGConfig {}; + namespace facebook::yoga { +class Config; + // Whether moving a node from config "a" to config "b" should dirty previously // calculated layout results. -bool configUpdateInvalidatesLayout(YGConfigRef a, YGConfigRef b); +bool configUpdateInvalidatesLayout(Config* a, Config* b); // Internal variants of log functions, currently used only by JNI bindings. // TODO: Reconcile this with the public API @@ -33,13 +38,12 @@ using CloneWithContextFn = YGNodeRef (*)( int childIndex, void* cloneContext); -using ExperimentalFeatureSet = - facebook::yoga::detail::EnumBitset; +using ExperimentalFeatureSet = detail::EnumBitset; #pragma pack(push) #pragma pack(1) // Packed structure of <32-bit options to miminize size per node. -struct YGConfigFlags { +struct ConfigFlags { bool useWebDefaults : 1; bool printTree : 1; bool cloneNodeUsesContext : 1; @@ -47,10 +51,9 @@ struct YGConfigFlags { }; #pragma pack(pop) -} // namespace facebook::yoga - -struct YOGA_EXPORT YGConfig { - YGConfig(YGLogger logger); +class YOGA_EXPORT Config : public ::YGConfig { +public: + Config(YGLogger logger); void setUseWebDefaults(bool useWebDefaults); bool useWebDefaults() const; @@ -62,7 +65,7 @@ struct YOGA_EXPORT YGConfig { YGExperimentalFeature feature, bool enabled); bool isExperimentalFeatureEnabled(YGExperimentalFeature feature) const; - facebook::yoga::ExperimentalFeatureSet getEnabledExperiments() const; + ExperimentalFeatureSet getEnabledExperiments() const; void setErrata(YGErrata errata); void addErrata(YGErrata errata); @@ -77,12 +80,12 @@ struct YOGA_EXPORT YGConfig { void* getContext() const; void setLogger(YGLogger logger); - void setLogger(facebook::yoga::LogWithContextFn logger); + void setLogger(LogWithContextFn logger); void setLogger(std::nullptr_t); - void log(YGConfig*, YGNode*, YGLogLevel, void*, const char*, va_list) const; + void log(YGNodeRef, YGLogLevel, void*, const char*, va_list); void setCloneNodeCallback(YGCloneNodeFunc cloneNode); - void setCloneNodeCallback(facebook::yoga::CloneWithContextFn cloneNode); + void setCloneNodeCallback(CloneWithContextFn cloneNode); void setCloneNodeCallback(std::nullptr_t); YGNodeRef cloneNode( YGNodeRef node, @@ -92,17 +95,19 @@ struct YOGA_EXPORT YGConfig { private: union { - facebook::yoga::CloneWithContextFn withContext; + CloneWithContextFn withContext; YGCloneNodeFunc noContext; } cloneNodeCallback_; union { - facebook::yoga::LogWithContextFn withContext; + LogWithContextFn withContext; YGLogger noContext; } logger_; - facebook::yoga::YGConfigFlags flags_{}; - facebook::yoga::ExperimentalFeatureSet experimentalFeatures_{}; + ConfigFlags flags_{}; + ExperimentalFeatureSet experimentalFeatures_{}; YGErrata errata_ = YGErrataNone; float pointScaleFactor_ = 1.0f; void* context_ = nullptr; }; + +} // namespace facebook::yoga diff --git a/packages/react-native/ReactCommon/yoga/yoga/log.cpp b/packages/react-native/ReactCommon/yoga/yoga/log.cpp index 51040592a27955..eb776629ddb8a3 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/log.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/log.cpp @@ -8,7 +8,7 @@ #include #include "log.h" -#include "YGConfig.h" +#include #include "YGNode.h" namespace facebook::yoga::detail { @@ -16,14 +16,16 @@ namespace facebook::yoga::detail { namespace { void vlog( - YGConfig* config, + yoga::Config* config, YGNode* node, YGLogLevel level, void* context, const char* format, va_list args) { - YGConfig* logConfig = config != nullptr ? config : YGConfigGetDefault(); - logConfig->log(logConfig, node, level, context, format, args); + yoga::Config* logConfig = config != nullptr + ? config + : static_cast(YGConfigGetDefault()); + logConfig->log(node, level, context, format, args); } } // namespace @@ -46,7 +48,7 @@ YOGA_EXPORT void Log::log( } void Log::log( - YGConfig* config, + yoga::Config* config, YGLogLevel level, void* context, const char* format, diff --git a/packages/react-native/ReactCommon/yoga/yoga/log.h b/packages/react-native/ReactCommon/yoga/yoga/log.h index 070a164da387ce..ad0fe4d6f52eef 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/log.h +++ b/packages/react-native/ReactCommon/yoga/yoga/log.h @@ -8,6 +8,7 @@ #pragma once #include +#include struct YGNode; struct YGConfig; @@ -23,7 +24,7 @@ struct Log { ...) noexcept; static void log( - YGConfig* config, + yoga::Config* config, YGLogLevel level, void*, const char* format,