From 8da095ca56035ac2fa47820e45162821bc34e95c Mon Sep 17 00:00:00 2001 From: Zhiyao Zhou Date: Tue, 29 Aug 2023 23:27:25 -0700 Subject: [PATCH] Revert D48712710: C++ Cleanup 3/N: Reorganize YGNode Differential Revision: D48712710 Original commit changeset: d28eae38469a Original Phabricator Diff: D48712710 fbshipit-source-id: 7a10b071edcf045ce98bbf8f9deca0d0e2e80a14 --- .../first-party/yogajni/jni/YGJNIVanilla.cpp | 6 +- .../first-party/yogajni/jni/YGJTypesVanilla.h | 11 +- .../view/YogaLayoutableShadowNode.cpp | 15 +- .../view/YogaLayoutableShadowNode.h | 14 +- .../components/view/YogaStylableProps.cpp | 1 + .../renderer/components/view/conversions.h | 4 +- .../react/renderer/core/ShadowNodeTraits.h | 2 +- .../ReactCommon/yoga/yoga/Utils.h | 4 +- .../{node/LayoutResults.cpp => YGLayout.cpp} | 10 +- .../yoga/{node/LayoutResults.h => YGLayout.h} | 10 +- .../yoga/yoga/{node/Node.cpp => YGNode.cpp} | 148 +++---- .../yoga/yoga/{node/Node.h => YGNode.h} | 100 +++-- .../ReactCommon/yoga/yoga/YGNodePrint.cpp | 44 +-- .../ReactCommon/yoga/yoga/YGNodePrint.h | 3 +- .../ReactCommon/yoga/yoga/Yoga-internal.h | 2 + .../ReactCommon/yoga/yoga/Yoga.cpp | 364 ++++++++---------- .../react-native/ReactCommon/yoga/yoga/Yoga.h | 2 +- .../ReactCommon/yoga/yoga/event/event.cpp | 5 +- .../ReactCommon/yoga/yoga/event/event.h | 21 +- .../ReactCommon/yoga/yoga/log.cpp | 6 +- .../react-native/ReactCommon/yoga/yoga/log.h | 6 +- 21 files changed, 360 insertions(+), 418 deletions(-) rename packages/react-native/ReactCommon/yoga/yoga/{node/LayoutResults.cpp => YGLayout.cpp} (88%) rename packages/react-native/ReactCommon/yoga/yoga/{node/LayoutResults.h => YGLayout.h} (89%) rename packages/react-native/ReactCommon/yoga/yoga/{node/Node.cpp => YGNode.cpp} (79%) rename packages/react-native/ReactCommon/yoga/yoga/{node/Node.h => YGNode.h} (78%) 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 b1cdc2dacf0..5d802784154 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 @@ -17,7 +17,7 @@ // TODO: Reconcile missing layoutContext functionality from callbacks in the C // API and use that -#include +#include using namespace facebook; using namespace facebook::yoga; @@ -688,7 +688,7 @@ static void jni_YGNodeSetHasMeasureFuncJNI( jobject /*obj*/, jlong nativePointer, jboolean hasMeasureFunc) { - static_cast(_jlong2YGNodeRef(nativePointer)) + _jlong2YGNodeRef(nativePointer) ->setMeasureFunc(hasMeasureFunc ? YGJNIMeasureFunc : nullptr); } @@ -715,7 +715,7 @@ static void jni_YGNodeSetHasBaselineFuncJNI( jobject /*obj*/, jlong nativePointer, jboolean hasBaselineFunc) { - static_cast(_jlong2YGNodeRef(nativePointer)) + _jlong2YGNodeRef(nativePointer) ->setBaselineFunc(hasBaselineFunc ? YGJNIBaselineFunc : nullptr); } diff --git a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJTypesVanilla.h b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJTypesVanilla.h index c1534fea692..506c304909a 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJTypesVanilla.h +++ b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJTypesVanilla.h @@ -14,12 +14,11 @@ #include "jni.h" class PtrJNodeMapVanilla { - std::map ptrsToIdxs_{}; - jobjectArray javaNodes_{}; + std::map ptrsToIdxs_; + jobjectArray javaNodes_; public: - PtrJNodeMapVanilla() = default; - + PtrJNodeMapVanilla() : ptrsToIdxs_{}, javaNodes_{} {} PtrJNodeMapVanilla(jlongArray javaNativePointers, jobjectArray javaNodes) : javaNodes_{javaNodes} { using namespace facebook::yoga::vanillajni; @@ -31,11 +30,11 @@ public: javaNativePointers, 0, nativePointersSize, nativePointers.data()); for (size_t i = 0; i < nativePointersSize; ++i) { - ptrsToIdxs_[(YGNodeConstRef) nativePointers[i]] = i; + ptrsToIdxs_[(YGNodeRef) nativePointers[i]] = i; } } - facebook::yoga::vanillajni::ScopedLocalRef ref(YGNodeConstRef node) { + facebook::yoga::vanillajni::ScopedLocalRef ref(YGNodeRef node) { using namespace facebook::yoga::vanillajni; JNIEnv* env = getCurrentEnv(); 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 d162487eb69..a3f2b4f5a6e 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp @@ -82,7 +82,7 @@ YogaLayoutableShadowNode::YogaLayoutableShadowNode( yogaNode_.setContext(this); // Newly created node must be `dirty` just because it is new. - // This is not a default for `yoga::Node`. + // This is not a default for `YGNode`. yogaNode_.setDirty(true); if (getTraits().check(ShadowNodeTraits::Trait::MeasurableYogaNode)) { @@ -106,7 +106,7 @@ YogaLayoutableShadowNode::YogaLayoutableShadowNode( yogaConfig_(FabricDefaultYogaLog), yogaNode_(static_cast(sourceShadowNode) .yogaNode_) { - // Note, cloned `yoga::Node` instance (copied using copy-constructor) inherits + // Note, cloned `YGNode` instance (copied using copy-constructor) inherits // dirty flag, measure function, and other properties being set originally in // the `YogaLayoutableShadowNode` constructor above. @@ -321,8 +321,7 @@ bool YogaLayoutableShadowNode::doesOwn( void YogaLayoutableShadowNode::updateYogaChildrenOwnersIfNeeded() { for (auto &childYogaNode : yogaNode_.getChildren()) { if (childYogaNode->getOwner() == &yogaNode_) { - childYogaNode->setOwner( - reinterpret_cast(0xBADC0FFEE0DDF00D)); + childYogaNode->setOwner(reinterpret_cast(0xBADC0FFEE0DDF00D)); } } } @@ -337,9 +336,7 @@ void YogaLayoutableShadowNode::updateYogaChildren() { bool isClean = !yogaNode_.isDirty() && getChildren().size() == yogaNode_.getChildren().size(); - auto oldYogaChildren = - isClean ? yogaNode_.getChildren() : std::vector{}; - + auto oldYogaChildren = isClean ? yogaNode_.getChildren() : YGVector{}; yogaNode_.setChildren({}); yogaLayoutableChildren_.clear(); @@ -832,9 +829,9 @@ YGSize YogaLayoutableShadowNode::yogaNodeMeasureCallbackConnector( } YogaLayoutableShadowNode &YogaLayoutableShadowNode::shadowNodeFromContext( - YGNodeRef yogaNode) { + YGNode *yogaNode) { return traitCast( - *static_cast(YGNodeGetContext(yogaNode))); + *static_cast(yogaNode->getContext())); } yoga::Config &YogaLayoutableShadowNode::initializeYogaConfig( 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 7a4c7e2f286..7257c7bd36b 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h @@ -10,7 +10,7 @@ #include #include -#include +#include #include #include @@ -100,7 +100,7 @@ class YogaLayoutableShadowNode : public LayoutableShadowNode { * Yoga node as `mutable` here to avoid `static_cast`ing the pointer to this * all the time. */ - mutable yoga::Node yogaNode_; + mutable YGNode yogaNode_; private: /* @@ -156,17 +156,17 @@ class YogaLayoutableShadowNode : public LayoutableShadowNode { static yoga::Config &initializeYogaConfig( yoga::Config &config, YGConfigRef previousConfig = nullptr); - static YGNodeRef yogaNodeCloneCallbackConnector( - YGNodeRef oldYogaNode, - YGNodeRef parentYogaNode, + static YGNode *yogaNodeCloneCallbackConnector( + YGNode *oldYogaNode, + YGNode *parentYogaNode, int childIndex); static YGSize yogaNodeMeasureCallbackConnector( - YGNodeRef yogaNode, + YGNode *yogaNode, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode); - static YogaLayoutableShadowNode &shadowNodeFromContext(YGNodeRef yogaNode); + static YogaLayoutableShadowNode &shadowNodeFromContext(YGNode *yogaNode); #pragma mark - RTL Legacy Autoflip 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 c84757f983a..903d4e2db21 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaStylableProps.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaStylableProps.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include "conversions.h" 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 c744d49a6ff..5bd66eea65a 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h @@ -18,8 +18,8 @@ #include #include #include +#include #include -#include #include #include @@ -113,7 +113,7 @@ inline std::optional optionalFloatFromYogaValue( } } -inline LayoutMetrics layoutMetricsFromYogaNode(yoga::Node &yogaNode) { +inline LayoutMetrics layoutMetricsFromYogaNode(YGNode &yogaNode) { auto layoutMetrics = LayoutMetrics{}; layoutMetrics.frame = Rect{ diff --git a/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeTraits.h b/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeTraits.h index d3fd255326d..9bb4d77a9cf 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeTraits.h +++ b/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeTraits.h @@ -60,7 +60,7 @@ class ShadowNodeTraits { // Any Yoga node (not only Leaf ones) can have this trait. DirtyYogaNode = 1 << 7, - // Inherits `YogaLayoutableShadowNode` and enforces that the yoga node is a + // Inherits `YogaLayoutableShadowNode` and enforces that the `YGNode` is a // leaf. LeafYogaNode = 1 << 8, diff --git a/packages/react-native/ReactCommon/yoga/yoga/Utils.h b/packages/react-native/ReactCommon/yoga/yoga/Utils.h index bd8ab309225..78b44de6939 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Utils.h +++ b/packages/react-native/ReactCommon/yoga/yoga/Utils.h @@ -7,7 +7,7 @@ #pragma once -#include +#include "YGNode.h" #include #include @@ -43,7 +43,7 @@ struct YGCollectFlexItemsRowValues { float totalFlexGrowFactors; float totalFlexShrinkScaledFactors; uint32_t endOfLineIndex; - std::vector relativeChildren; + std::vector relativeChildren; float remainingFreeSpace; // The size of the mainDim for the row after considering size, padding, margin // and border of flex items. This is used to calculate maxLineDim after going diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/LayoutResults.cpp b/packages/react-native/ReactCommon/yoga/yoga/YGLayout.cpp similarity index 88% rename from packages/react-native/ReactCommon/yoga/yoga/node/LayoutResults.cpp rename to packages/react-native/ReactCommon/yoga/yoga/YGLayout.cpp index d6611aa159b..9aadcd4aecd 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/LayoutResults.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/YGLayout.cpp @@ -5,12 +5,12 @@ * LICENSE file in the root directory of this source tree. */ -#include -#include +#include "YGLayout.h" +#include "Utils.h" -namespace facebook::yoga { +using namespace facebook; -bool LayoutResults::operator==(LayoutResults layout) const { +bool YGLayout::operator==(YGLayout layout) const { bool isEqual = YGFloatArrayEqual(position, layout.position) && YGFloatArrayEqual(dimensions, layout.dimensions) && YGFloatArrayEqual(margin, layout.margin) && @@ -40,5 +40,3 @@ bool LayoutResults::operator==(LayoutResults layout) const { return isEqual; } - -} // namespace facebook::yoga diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/LayoutResults.h b/packages/react-native/ReactCommon/yoga/yoga/YGLayout.h similarity index 89% rename from packages/react-native/ReactCommon/yoga/yoga/node/LayoutResults.h rename to packages/react-native/ReactCommon/yoga/yoga/YGLayout.h index 18cc135f2c6..78a1bcf1918 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/LayoutResults.h +++ b/packages/react-native/ReactCommon/yoga/yoga/YGLayout.h @@ -11,9 +11,7 @@ #include #include -namespace facebook::yoga { - -struct LayoutResults { +struct YGLayout { std::array position = {}; std::array dimensions = {{YGUndefined, YGUndefined}}; std::array margin = {}; @@ -60,8 +58,6 @@ public: flags, hadOverflowOffset, hadOverflow); } - bool operator==(LayoutResults layout) const; - bool operator!=(LayoutResults layout) const { return !(*this == layout); } + bool operator==(YGLayout layout) const; + bool operator!=(YGLayout layout) const { return !(*this == layout); } }; - -} // namespace facebook::yoga diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp b/packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp similarity index 79% rename from packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp rename to packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp index f0b355cf353..7389354e68d 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp @@ -5,15 +5,18 @@ * LICENSE file in the root directory of this source tree. */ -#include +#include "YGNode.h" #include #include -#include +#include "Utils.h" -namespace facebook::yoga { +using namespace facebook; +using namespace facebook::yoga; +using facebook::yoga::CompactValue; -Node::Node(yoga::Config* config) : config_{config} { - YGAssert(config != nullptr, "Attempting to construct Node with null config"); +YGNode::YGNode(yoga::Config* config) : config_{config} { + YGAssert( + config != nullptr, "Attempting to construct YGNode with null config"); flags_.hasNewLayout = true; if (config->useWebDefaults()) { @@ -21,7 +24,7 @@ Node::Node(yoga::Config* config) : config_{config} { } } -Node::Node(Node&& node) { +YGNode::YGNode(YGNode&& node) { context_ = node.context_; flags_ = node.flags_; measure_ = node.measure_; @@ -40,7 +43,7 @@ Node::Node(Node&& node) { } } -void Node::print(void* printContext) { +void YGNode::print(void* printContext) { if (print_.noContext != nullptr) { if (flags_.printUsesContext) { print_.withContext(this, printContext); @@ -50,7 +53,7 @@ void Node::print(void* printContext) { } } -CompactValue Node::computeEdgeValueForRow( +CompactValue YGNode::computeEdgeValueForRow( const Style::Edges& edges, YGEdge rowEdge, YGEdge edge, @@ -68,7 +71,7 @@ CompactValue Node::computeEdgeValueForRow( } } -CompactValue Node::computeEdgeValueForColumn( +CompactValue YGNode::computeEdgeValueForColumn( const Style::Edges& edges, YGEdge edge, CompactValue defaultValue) { @@ -83,7 +86,7 @@ CompactValue Node::computeEdgeValueForColumn( } } -CompactValue Node::computeRowGap( +CompactValue YGNode::computeRowGap( const Style::Gutters& gutters, CompactValue defaultValue) { if (!gutters[YGGutterRow].isUndefined()) { @@ -95,7 +98,7 @@ CompactValue Node::computeRowGap( } } -CompactValue Node::computeColumnGap( +CompactValue YGNode::computeColumnGap( const Style::Gutters& gutters, CompactValue defaultValue) { if (!gutters[YGGutterColumn].isUndefined()) { @@ -107,7 +110,7 @@ CompactValue Node::computeColumnGap( } } -YGFloatOptional Node::getLeadingPosition( +YGFloatOptional YGNode::getLeadingPosition( const YGFlexDirection axis, const float axisSize) const { auto leadingPosition = YGFlexDirectionIsRow(axis) @@ -121,7 +124,7 @@ YGFloatOptional Node::getLeadingPosition( return YGResolveValue(leadingPosition, axisSize); } -YGFloatOptional Node::getTrailingPosition( +YGFloatOptional YGNode::getTrailingPosition( const YGFlexDirection axis, const float axisSize) const { auto trailingPosition = YGFlexDirectionIsRow(axis) @@ -135,7 +138,7 @@ YGFloatOptional Node::getTrailingPosition( return YGResolveValue(trailingPosition, axisSize); } -bool Node::isLeadingPositionDefined(const YGFlexDirection axis) const { +bool YGNode::isLeadingPositionDefined(const YGFlexDirection axis) const { auto leadingPosition = YGFlexDirectionIsRow(axis) ? computeEdgeValueForRow( style_.position(), @@ -147,7 +150,7 @@ bool Node::isLeadingPositionDefined(const YGFlexDirection axis) const { return !leadingPosition.isUndefined(); } -bool Node::isTrailingPosDefined(const YGFlexDirection axis) const { +bool YGNode::isTrailingPosDefined(const YGFlexDirection axis) const { auto trailingPosition = YGFlexDirectionIsRow(axis) ? computeEdgeValueForRow( style_.position(), @@ -159,7 +162,7 @@ bool Node::isTrailingPosDefined(const YGFlexDirection axis) const { return !trailingPosition.isUndefined(); } -YGFloatOptional Node::getLeadingMargin( +YGFloatOptional YGNode::getLeadingMargin( const YGFlexDirection axis, const float widthSize) const { auto leadingMargin = YGFlexDirectionIsRow(axis) @@ -170,7 +173,7 @@ YGFloatOptional Node::getLeadingMargin( return YGResolveValueMargin(leadingMargin, widthSize); } -YGFloatOptional Node::getTrailingMargin( +YGFloatOptional YGNode::getTrailingMargin( const YGFlexDirection axis, const float widthSize) const { auto trailingMargin = YGFlexDirectionIsRow(axis) @@ -181,13 +184,13 @@ YGFloatOptional Node::getTrailingMargin( return YGResolveValueMargin(trailingMargin, widthSize); } -YGFloatOptional Node::getMarginForAxis( +YGFloatOptional YGNode::getMarginForAxis( const YGFlexDirection axis, const float widthSize) const { return getLeadingMargin(axis, widthSize) + getTrailingMargin(axis, widthSize); } -YGFloatOptional Node::getGapForAxis( +YGFloatOptional YGNode::getGapForAxis( const YGFlexDirection axis, const float widthSize) const { auto gap = YGFlexDirectionIsRow(axis) @@ -196,7 +199,7 @@ YGFloatOptional Node::getGapForAxis( return YGResolveValue(gap, widthSize); } -YGSize Node::measure( +YGSize YGNode::measure( float width, YGMeasureMode widthMode, float height, @@ -208,7 +211,7 @@ YGSize Node::measure( : measure_.noContext(this, width, widthMode, height, heightMode); } -float Node::baseline(float width, float height, void* layoutContext) { +float YGNode::baseline(float width, float height, void* layoutContext) { return flags_.baselineUsesContext ? baseline_.withContext(this, width, height, layoutContext) : baseline_.noContext(this, width, height); @@ -216,7 +219,7 @@ float Node::baseline(float width, float height, void* layoutContext) { // Setters -void Node::setMeasureFunc(decltype(Node::measure_) measureFunc) { +void YGNode::setMeasureFunc(decltype(YGNode::measure_) measureFunc) { if (measureFunc.noContext == nullptr) { // TODO: t18095186 Move nodeType to opt-in function and mark appropriate // places in Litho @@ -235,38 +238,38 @@ void Node::setMeasureFunc(decltype(Node::measure_) measureFunc) { measure_ = measureFunc; } -void Node::setMeasureFunc(YGMeasureFunc measureFunc) { +void YGNode::setMeasureFunc(YGMeasureFunc measureFunc) { flags_.measureUsesContext = false; - decltype(Node::measure_) m; + decltype(YGNode::measure_) m; m.noContext = measureFunc; setMeasureFunc(m); } -YOGA_EXPORT void Node::setMeasureFunc(MeasureWithContextFn measureFunc) { +YOGA_EXPORT void YGNode::setMeasureFunc(MeasureWithContextFn measureFunc) { flags_.measureUsesContext = true; - decltype(Node::measure_) m; + decltype(YGNode::measure_) m; m.withContext = measureFunc; setMeasureFunc(m); } -void Node::replaceChild(Node* child, uint32_t index) { +void YGNode::replaceChild(YGNodeRef child, uint32_t index) { children_[index] = child; } -void Node::replaceChild(Node* oldChild, Node* newChild) { +void YGNode::replaceChild(YGNodeRef oldChild, YGNodeRef newChild) { std::replace(children_.begin(), children_.end(), oldChild, newChild); } -void Node::insertChild(Node* child, uint32_t index) { +void YGNode::insertChild(YGNodeRef child, uint32_t index) { children_.insert(children_.begin() + index, child); } -void Node::setConfig(yoga::Config* config) { - YGAssert(config != nullptr, "Attempting to set a null config on a Node"); +void YGNode::setConfig(yoga::Config* config) { + YGAssert(config != nullptr, "Attempting to set a null config on a YGNode"); YGAssertWithConfig( config, config->useWebDefaults() == config_->useWebDefaults(), - "UseWebDefaults may not be changed after constructing a Node"); + "UseWebDefaults may not be changed after constructing a YGNode"); if (yoga::configUpdateInvalidatesLayout(config_, config)) { markDirtyAndPropagate(); @@ -275,7 +278,7 @@ void Node::setConfig(yoga::Config* config) { config_ = config; } -void Node::setDirty(bool isDirty) { +void YGNode::setDirty(bool isDirty) { if (isDirty == flags_.isDirty) { return; } @@ -285,8 +288,8 @@ void Node::setDirty(bool isDirty) { } } -bool Node::removeChild(Node* child) { - std::vector::iterator p = +bool YGNode::removeChild(YGNodeRef child) { + std::vector::iterator p = std::find(children_.begin(), children_.end(), child); if (p != children_.end()) { children_.erase(p); @@ -295,58 +298,59 @@ bool Node::removeChild(Node* child) { return false; } -void Node::removeChild(uint32_t index) { +void YGNode::removeChild(uint32_t index) { children_.erase(children_.begin() + index); } -void Node::setLayoutDirection(YGDirection direction) { +void YGNode::setLayoutDirection(YGDirection direction) { layout_.setDirection(direction); } -void Node::setLayoutMargin(float margin, int index) { +void YGNode::setLayoutMargin(float margin, int index) { layout_.margin[index] = margin; } -void Node::setLayoutBorder(float border, int index) { +void YGNode::setLayoutBorder(float border, int index) { layout_.border[index] = border; } -void Node::setLayoutPadding(float padding, int index) { +void YGNode::setLayoutPadding(float padding, int index) { layout_.padding[index] = padding; } -void Node::setLayoutLastOwnerDirection(YGDirection direction) { +void YGNode::setLayoutLastOwnerDirection(YGDirection direction) { layout_.lastOwnerDirection = direction; } -void Node::setLayoutComputedFlexBasis(const YGFloatOptional computedFlexBasis) { +void YGNode::setLayoutComputedFlexBasis( + const YGFloatOptional computedFlexBasis) { layout_.computedFlexBasis = computedFlexBasis; } -void Node::setLayoutPosition(float position, int index) { +void YGNode::setLayoutPosition(float position, int index) { layout_.position[index] = position; } -void Node::setLayoutComputedFlexBasisGeneration( +void YGNode::setLayoutComputedFlexBasisGeneration( uint32_t computedFlexBasisGeneration) { layout_.computedFlexBasisGeneration = computedFlexBasisGeneration; } -void Node::setLayoutMeasuredDimension(float measuredDimension, int index) { +void YGNode::setLayoutMeasuredDimension(float measuredDimension, int index) { layout_.measuredDimensions[index] = measuredDimension; } -void Node::setLayoutHadOverflow(bool hadOverflow) { +void YGNode::setLayoutHadOverflow(bool hadOverflow) { layout_.setHadOverflow(hadOverflow); } -void Node::setLayoutDimension(float dimension, int index) { +void YGNode::setLayoutDimension(float dimension, int index) { layout_.dimensions[index] = dimension; } // If both left and right are defined, then use left. Otherwise return +left or // -right depending on which is defined. -YGFloatOptional Node::relativePosition( +YGFloatOptional YGNode::relativePosition( const YGFlexDirection axis, const float axisSize) const { if (isLeadingPositionDefined(axis)) { @@ -360,7 +364,7 @@ YGFloatOptional Node::relativePosition( return trailingPosition; } -void Node::setPosition( +void YGNode::setPosition( const YGDirection direction, const float mainSize, const float crossSize, @@ -398,7 +402,7 @@ void Node::setPosition( trailing[crossAxis]); } -YGValue Node::marginLeadingValue(const YGFlexDirection axis) const { +YGValue YGNode::marginLeadingValue(const YGFlexDirection axis) const { if (YGFlexDirectionIsRow(axis) && !style_.margin()[YGEdgeStart].isUndefined()) { return style_.margin()[YGEdgeStart]; @@ -407,7 +411,7 @@ YGValue Node::marginLeadingValue(const YGFlexDirection axis) const { } } -YGValue Node::marginTrailingValue(const YGFlexDirection axis) const { +YGValue YGNode::marginTrailingValue(const YGFlexDirection axis) const { if (YGFlexDirectionIsRow(axis) && !style_.margin()[YGEdgeEnd].isUndefined()) { return style_.margin()[YGEdgeEnd]; } else { @@ -415,7 +419,7 @@ YGValue Node::marginTrailingValue(const YGFlexDirection axis) const { } } -YGValue Node::resolveFlexBasisPtr() const { +YGValue YGNode::resolveFlexBasisPtr() const { YGValue flexBasis = style_.flexBasis(); if (flexBasis.unit != YGUnitAuto && flexBasis.unit != YGUnitUndefined) { return flexBasis; @@ -426,7 +430,7 @@ YGValue Node::resolveFlexBasisPtr() const { return YGValueAuto; } -void Node::resolveDimension() { +void YGNode::resolveDimension() { using namespace yoga; const Style& style = getStyle(); for (auto dim : {YGDimensionWidth, YGDimensionHeight}) { @@ -439,7 +443,7 @@ void Node::resolveDimension() { } } -YGDirection Node::resolveDirection(const YGDirection ownerDirection) { +YGDirection YGNode::resolveDirection(const YGDirection ownerDirection) { if (style_.direction() == YGDirectionInherit) { return ownerDirection > YGDirectionInherit ? ownerDirection : YGDirectionLTR; @@ -448,18 +452,18 @@ YGDirection Node::resolveDirection(const YGDirection ownerDirection) { } } -YOGA_EXPORT void Node::clearChildren() { +YOGA_EXPORT void YGNode::clearChildren() { children_.clear(); children_.shrink_to_fit(); } // Other Methods -void Node::cloneChildrenIfNeeded(void* cloneContext) { - iterChildrenAfterCloningIfNeeded([](Node*, void*) {}, cloneContext); +void YGNode::cloneChildrenIfNeeded(void* cloneContext) { + iterChildrenAfterCloningIfNeeded([](YGNodeRef, void*) {}, cloneContext); } -void Node::markDirtyAndPropagate() { +void YGNode::markDirtyAndPropagate() { if (!flags_.isDirty) { setDirty(true); setLayoutComputedFlexBasis(YGFloatOptional()); @@ -469,14 +473,14 @@ void Node::markDirtyAndPropagate() { } } -void Node::markDirtyAndPropagateDownwards() { +void YGNode::markDirtyAndPropagateDownwards() { flags_.isDirty = true; - for_each(children_.begin(), children_.end(), [](Node* childNode) { + for_each(children_.begin(), children_.end(), [](YGNodeRef childNode) { childNode->markDirtyAndPropagateDownwards(); }); } -float Node::resolveFlexGrow() const { +float YGNode::resolveFlexGrow() const { // Root nodes flexGrow should always be 0 if (owner_ == nullptr) { return 0.0; @@ -490,7 +494,7 @@ float Node::resolveFlexGrow() const { return kDefaultFlexGrow; } -float Node::resolveFlexShrink() const { +float YGNode::resolveFlexShrink() const { if (owner_ == nullptr) { return 0.0; } @@ -504,13 +508,13 @@ float Node::resolveFlexShrink() const { return config_->useWebDefaults() ? kWebDefaultFlexShrink : kDefaultFlexShrink; } -bool Node::isNodeFlexible() { +bool YGNode::isNodeFlexible() { return ( (style_.positionType() != YGPositionTypeAbsolute) && (resolveFlexGrow() != 0 || resolveFlexShrink() != 0)); } -float Node::getLeadingBorder(const YGFlexDirection axis) const { +float YGNode::getLeadingBorder(const YGFlexDirection axis) const { YGValue leadingBorder = YGFlexDirectionIsRow(axis) ? computeEdgeValueForRow( style_.border(), YGEdgeStart, leading[axis], CompactValue::ofZero()) @@ -519,7 +523,7 @@ float Node::getLeadingBorder(const YGFlexDirection axis) const { return fmaxf(leadingBorder.value, 0.0f); } -float Node::getTrailingBorder(const YGFlexDirection axis) const { +float YGNode::getTrailingBorder(const YGFlexDirection axis) const { YGValue trailingBorder = YGFlexDirectionIsRow(axis) ? computeEdgeValueForRow( style_.border(), YGEdgeEnd, trailing[axis], CompactValue::ofZero()) @@ -528,7 +532,7 @@ float Node::getTrailingBorder(const YGFlexDirection axis) const { return fmaxf(trailingBorder.value, 0.0f); } -YGFloatOptional Node::getLeadingPadding( +YGFloatOptional YGNode::getLeadingPadding( const YGFlexDirection axis, const float widthSize) const { auto leadingPadding = YGFlexDirectionIsRow(axis) @@ -543,7 +547,7 @@ YGFloatOptional Node::getLeadingPadding( YGResolveValue(leadingPadding, widthSize), YGFloatOptional(0.0f)); } -YGFloatOptional Node::getTrailingPadding( +YGFloatOptional YGNode::getTrailingPadding( const YGFlexDirection axis, const float widthSize) const { auto trailingPadding = YGFlexDirectionIsRow(axis) @@ -555,21 +559,21 @@ YGFloatOptional Node::getTrailingPadding( YGResolveValue(trailingPadding, widthSize), YGFloatOptional(0.0f)); } -YGFloatOptional Node::getLeadingPaddingAndBorder( +YGFloatOptional YGNode::getLeadingPaddingAndBorder( const YGFlexDirection axis, const float widthSize) const { return getLeadingPadding(axis, widthSize) + YGFloatOptional(getLeadingBorder(axis)); } -YGFloatOptional Node::getTrailingPaddingAndBorder( +YGFloatOptional YGNode::getTrailingPaddingAndBorder( const YGFlexDirection axis, const float widthSize) const { return getTrailingPadding(axis, widthSize) + YGFloatOptional(getTrailingBorder(axis)); } -void Node::reset() { +void YGNode::reset() { YGAssertWithNode( this, children_.size() == 0, @@ -577,7 +581,5 @@ void Node::reset() { YGAssertWithNode( this, owner_ == nullptr, "Cannot reset a node still attached to a owner"); - *this = Node{getConfig()}; + *this = YGNode{getConfig()}; } - -} // namespace facebook::yoga diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h b/packages/react-native/ReactCommon/yoga/yoga/YGNode.h similarity index 78% rename from packages/react-native/ReactCommon/yoga/yoga/node/Node.h rename to packages/react-native/ReactCommon/yoga/yoga/YGNode.h index abb1535aed5..218cb989ffe 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h +++ b/packages/react-native/ReactCommon/yoga/yoga/YGNode.h @@ -10,20 +10,15 @@ #include #include #include -#include +#include "YGLayout.h" #include #include #include -// Tag struct used to form the opaque YGNodeRef for the public C API -struct YGNode {}; - -namespace facebook::yoga { - #pragma pack(push) #pragma pack(1) -struct NodeFlags { +struct YGNodeFlags { bool hasNewLayout : 1; bool isReferenceBaseline : 1; bool isDirty : 1; @@ -34,8 +29,7 @@ struct NodeFlags { }; #pragma pack(pop) -class YOGA_EXPORT Node : public ::YGNode { -public: +struct YOGA_EXPORT YGNode { using MeasureWithContextFn = YGSize (*)(YGNode*, float, YGMeasureMode, float, YGMeasureMode, void*); using BaselineWithContextFn = float (*)(YGNode*, float, float, void*); @@ -43,7 +37,7 @@ public: private: void* context_ = nullptr; - NodeFlags flags_ = {}; + YGNodeFlags flags_ = {}; union { YGMeasureFunc noContext; MeasureWithContextFn withContext; @@ -57,12 +51,12 @@ private: PrintWithContextFn withContext; } print_ = {nullptr}; YGDirtiedFunc dirtied_ = nullptr; - Style style_ = {}; - LayoutResults layout_ = {}; + facebook::yoga::Style style_ = {}; + YGLayout layout_ = {}; uint32_t lineIndex_ = 0; - Node* owner_ = nullptr; - std::vector children_ = {}; - Config* config_; + YGNodeRef owner_ = nullptr; + YGVector children_ = {}; + facebook::yoga::Config* config_; std::array resolvedDimensions_ = { {YGValueUndefined, YGValueUndefined}}; @@ -83,24 +77,27 @@ private: // them (potentially incorrect) or ignore them (danger of leaks). Only ever // use this after checking that there are no children. // DO NOT CHANGE THE VISIBILITY OF THIS METHOD! - Node& operator=(Node&&) = default; + YGNode& operator=(YGNode&&) = default; + + using CompactValue = facebook::yoga::CompactValue; public: - Node() : Node{static_cast(YGConfigGetDefault())} { + YGNode() + : YGNode{static_cast(YGConfigGetDefault())} { flags_.hasNewLayout = true; } - explicit Node(Config* config); - ~Node() = default; // cleanup of owner/children relationships in YGNodeFree + explicit YGNode(facebook::yoga::Config* config); + ~YGNode() = default; // cleanup of owner/children relationships in YGNodeFree - Node(Node&&); + YGNode(YGNode&&); // Does not expose true value semantics, as children are not cloned eagerly. // Should we remove this? - Node(const Node& node) = default; + YGNode(const YGNode& node) = default; // assignment means potential leaks of existing children, or alternatively // freeing unowned memory, double free, or freeing stack memory. - Node& operator=(const Node&) = delete; + YGNode& operator=(const YGNode&) = delete; // Getters void* getContext() const { return context_; } @@ -128,39 +125,38 @@ public: YGDirtiedFunc getDirtied() const { return dirtied_; } // For Performance reasons passing as reference. - Style& getStyle() { return style_; } + facebook::yoga::Style& getStyle() { return style_; } - const Style& getStyle() const { return style_; } + const facebook::yoga::Style& getStyle() const { return style_; } // For Performance reasons passing as reference. - LayoutResults& getLayout() { return layout_; } + YGLayout& getLayout() { return layout_; } - const LayoutResults& getLayout() const { return layout_; } + const YGLayout& getLayout() const { return layout_; } uint32_t getLineIndex() const { return lineIndex_; } bool isReferenceBaseline() { return flags_.isReferenceBaseline; } - // returns the Node that owns this Node. An owner is used to identify - // the YogaTree that a Node belongs to. This method will return the parent - // of the Node when a Node only belongs to one YogaTree or nullptr when - // the Node is shared between two or more YogaTrees. - Node* getOwner() const { return owner_; } + // returns the YGNodeRef that owns this YGNode. An owner is used to identify + // the YogaTree that a YGNode belongs to. This method will return the parent + // of the YGNode when a YGNode only belongs to one YogaTree or nullptr when + // the YGNode is shared between two or more YogaTrees. + YGNodeRef getOwner() const { return owner_; } // Deprecated, use getOwner() instead. - Node* getParent() const { return getOwner(); } + YGNodeRef getParent() const { return getOwner(); } - const std::vector& getChildren() const { return children_; } + const YGVector& getChildren() const { return children_; } // Applies a callback to all children, after cloning them if they are not // owned. template void iterChildrenAfterCloningIfNeeded(T callback, void* cloneContext) { int i = 0; - for (Node*& child : children_) { + for (YGNodeRef& child : children_) { if (child->getOwner() != this) { - child = static_cast( - config_->cloneNode(child, this, i, cloneContext)); + child = config_->cloneNode(child, this, i, cloneContext); child->setOwner(this); } i += 1; @@ -169,9 +165,9 @@ public: } } - Node* getChild(uint32_t index) const { return children_.at(index); } + YGNodeRef getChild(uint32_t index) const { return children_.at(index); } - Config* getConfig() const { return config_; } + facebook::yoga::Config* getConfig() const { return config_; } bool isDirty() const { return flags_.isDirty; } @@ -184,22 +180,22 @@ public: } static CompactValue computeEdgeValueForColumn( - const Style::Edges& edges, + const facebook::yoga::Style::Edges& edges, YGEdge edge, CompactValue defaultValue); static CompactValue computeEdgeValueForRow( - const Style::Edges& edges, + const facebook::yoga::Style::Edges& edges, YGEdge rowEdge, YGEdge edge, CompactValue defaultValue); static CompactValue computeRowGap( - const Style::Gutters& gutters, + const facebook::yoga::Style::Gutters& gutters, CompactValue defaultValue); static CompactValue computeColumnGap( - const Style::Gutters& gutters, + const facebook::yoga::Style::Gutters& gutters, CompactValue defaultValue); // Methods related to positions, margin, padding and border @@ -279,9 +275,9 @@ public: void setDirtiedFunc(YGDirtiedFunc dirtiedFunc) { dirtied_ = dirtiedFunc; } - void setStyle(const Style& style) { style_ = style; } + void setStyle(const facebook::yoga::Style& style) { style_ = style; } - void setLayout(const LayoutResults& layout) { layout_ = layout; } + void setLayout(const YGLayout& layout) { layout_ = layout; } void setLineIndex(uint32_t lineIndex) { lineIndex_ = lineIndex; } @@ -289,13 +285,13 @@ public: flags_.isReferenceBaseline = isReferenceBaseline; } - void setOwner(Node* owner) { owner_ = owner; } + void setOwner(YGNodeRef owner) { owner_ = owner; } - void setChildren(const std::vector& children) { children_ = children; } + void setChildren(const YGVector& children) { children_ = children; } // TODO: rvalue override for setChildren - void setConfig(Config* config); + void setConfig(facebook::yoga::Config* config); void setDirty(bool isDirty); void setLayoutLastOwnerDirection(YGDirection direction); @@ -325,11 +321,11 @@ public: YGDirection resolveDirection(const YGDirection ownerDirection); void clearChildren(); /// Replaces the occurrences of oldChild with newChild - void replaceChild(Node* oldChild, Node* newChild); - void replaceChild(Node* child, uint32_t index); - void insertChild(Node* child, uint32_t index); + void replaceChild(YGNodeRef oldChild, YGNodeRef newChild); + void replaceChild(YGNodeRef child, uint32_t index); + void insertChild(YGNodeRef child, uint32_t index); /// Removes the first occurrence of child - bool removeChild(Node* child); + bool removeChild(YGNodeRef child); void removeChild(uint32_t index); void cloneChildrenIfNeeded(void*); @@ -339,5 +335,3 @@ public: bool isNodeFlexible(); void reset(); }; - -} // namespace facebook::yoga diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGNodePrint.cpp b/packages/react-native/ReactCommon/yoga/yoga/YGNodePrint.cpp index d76aa26fb97..c1205abbdc7 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGNodePrint.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/YGNodePrint.cpp @@ -12,8 +12,9 @@ #include #include "YGNodePrint.h" +#include "YGNode.h" #include -#include +#include "Utils.h" namespace facebook::yoga { typedef std::string string; @@ -91,7 +92,7 @@ static void appendEdges( const string& key, const Style::Edges& edges) { if (areFourValuesEqual(edges)) { - auto edgeValue = yoga::Node::computeEdgeValueForColumn( + auto edgeValue = YGNode::computeEdgeValueForColumn( edges, YGEdgeLeft, CompactValue::ofZero()); appendNumberIfNotZero(base, key, edgeValue); } else { @@ -109,16 +110,16 @@ static void appendEdgeIfNotUndefined( const YGEdge edge) { // TODO: this doesn't take RTL / YGEdgeStart / YGEdgeEnd into account auto value = (edge == YGEdgeLeft || edge == YGEdgeRight) - ? yoga::Node::computeEdgeValueForRow( + ? YGNode::computeEdgeValueForRow( edges, edge, edge, CompactValue::ofUndefined()) - : yoga::Node::computeEdgeValueForColumn( + : YGNode::computeEdgeValueForColumn( edges, edge, CompactValue::ofUndefined()); appendNumberIfNotUndefined(base, str, value); } void YGNodeToString( std::string& str, - yoga::Node* node, + YGNodeRef node, YGPrintOptions options, uint32_t level) { indent(str, level); @@ -140,27 +141,27 @@ void YGNodeToString( if (options & YGPrintOptionsStyle) { appendFormattedString(str, "style=\""); const auto& style = node->getStyle(); - if (style.flexDirection() != yoga::Node{}.getStyle().flexDirection()) { + if (style.flexDirection() != YGNode().getStyle().flexDirection()) { appendFormattedString( str, "flex-direction: %s; ", YGFlexDirectionToString(style.flexDirection())); } - if (style.justifyContent() != yoga::Node{}.getStyle().justifyContent()) { + if (style.justifyContent() != YGNode().getStyle().justifyContent()) { appendFormattedString( str, "justify-content: %s; ", YGJustifyToString(style.justifyContent())); } - if (style.alignItems() != yoga::Node{}.getStyle().alignItems()) { + if (style.alignItems() != YGNode().getStyle().alignItems()) { appendFormattedString( str, "align-items: %s; ", YGAlignToString(style.alignItems())); } - if (style.alignContent() != yoga::Node{}.getStyle().alignContent()) { + if (style.alignContent() != YGNode().getStyle().alignContent()) { appendFormattedString( str, "align-content: %s; ", YGAlignToString(style.alignContent())); } - if (style.alignSelf() != yoga::Node{}.getStyle().alignSelf()) { + if (style.alignSelf() != YGNode().getStyle().alignSelf()) { appendFormattedString( str, "align-self: %s; ", YGAlignToString(style.alignSelf())); } @@ -169,17 +170,17 @@ void YGNodeToString( appendNumberIfNotAuto(str, "flex-basis", style.flexBasis()); appendFloatOptionalIfDefined(str, "flex", style.flex()); - if (style.flexWrap() != yoga::Node{}.getStyle().flexWrap()) { + if (style.flexWrap() != YGNode().getStyle().flexWrap()) { appendFormattedString( str, "flex-wrap: %s; ", YGWrapToString(style.flexWrap())); } - if (style.overflow() != yoga::Node{}.getStyle().overflow()) { + if (style.overflow() != YGNode().getStyle().overflow()) { appendFormattedString( str, "overflow: %s; ", YGOverflowToString(style.overflow())); } - if (style.display() != yoga::Node{}.getStyle().display()) { + if (style.display() != YGNode().getStyle().display()) { appendFormattedString( str, "display: %s; ", YGDisplayToString(style.display())); } @@ -187,16 +188,15 @@ void YGNodeToString( appendEdges(str, "padding", style.padding()); appendEdges(str, "border", style.border()); - if (yoga::Node::computeColumnGap( - style.gap(), CompactValue::ofUndefined()) != - yoga::Node::computeColumnGap( - yoga::Node{}.getStyle().gap(), CompactValue::ofUndefined())) { + if (YGNode::computeColumnGap(style.gap(), CompactValue::ofUndefined()) != + YGNode::computeColumnGap( + YGNode().getStyle().gap(), CompactValue::ofUndefined())) { appendNumberIfNotUndefined( str, "column-gap", style.gap()[YGGutterColumn]); } - if (yoga::Node::computeRowGap(style.gap(), CompactValue::ofUndefined()) != - yoga::Node::computeRowGap( - yoga::Node{}.getStyle().gap(), CompactValue::ofUndefined())) { + if (YGNode::computeRowGap(style.gap(), CompactValue::ofUndefined()) != + YGNode::computeRowGap( + YGNode().getStyle().gap(), CompactValue::ofUndefined())) { appendNumberIfNotUndefined(str, "row-gap", style.gap()[YGGutterRow]); } @@ -211,7 +211,7 @@ void YGNodeToString( appendNumberIfNotAuto( str, "min-height", style.minDimensions()[YGDimensionHeight]); - if (style.positionType() != yoga::Node{}.getStyle().positionType()) { + if (style.positionType() != YGNode().getStyle().positionType()) { appendFormattedString( str, "position: %s; ", YGPositionTypeToString(style.positionType())); } @@ -232,7 +232,7 @@ void YGNodeToString( if (options & YGPrintOptionsChildren && childCount > 0) { for (uint32_t i = 0; i < childCount; i++) { appendFormattedString(str, "\n"); - YGNodeToString(str, node->getChild(i), options, level + 1); + YGNodeToString(str, YGNodeGetChild(node, i), options, level + 1); } appendFormattedString(str, "\n"); indent(str, level); diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGNodePrint.h b/packages/react-native/ReactCommon/yoga/yoga/YGNodePrint.h index b991c41a7f3..7648f83c7bb 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGNodePrint.h +++ b/packages/react-native/ReactCommon/yoga/yoga/YGNodePrint.h @@ -12,13 +12,12 @@ #include #include -#include namespace facebook::yoga { void YGNodeToString( std::string& str, - yoga::Node* node, + YGNodeRef node, YGPrintOptions options, uint32_t level); diff --git a/packages/react-native/ReactCommon/yoga/yoga/Yoga-internal.h b/packages/react-native/ReactCommon/yoga/yoga/Yoga-internal.h index 3fc4f23ac3e..d4e6df63e54 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Yoga-internal.h +++ b/packages/react-native/ReactCommon/yoga/yoga/Yoga-internal.h @@ -16,6 +16,8 @@ #include +using YGVector = std::vector; + YG_EXTERN_C_BEGIN void YGNodeCalculateLayoutWithContext( diff --git a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp b/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp index eb84c47a407..b52ded0e089 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp @@ -14,8 +14,8 @@ #include #include "log.h" -#include -#include +#include "Utils.h" +#include "YGNode.h" #include "YGNodePrint.h" #include #include "event/event.h" @@ -108,57 +108,57 @@ YOGA_EXPORT bool YGFloatIsUndefined(const float value) { } YOGA_EXPORT void* YGNodeGetContext(YGNodeRef node) { - return static_cast(node)->getContext(); + return node->getContext(); } YOGA_EXPORT void YGNodeSetContext(YGNodeRef node, void* context) { - return static_cast(node)->setContext(context); + return node->setContext(context); } YOGA_EXPORT YGConfigRef YGNodeGetConfig(YGNodeRef node) { - return static_cast(node)->getConfig(); + return node->getConfig(); } YOGA_EXPORT void YGNodeSetConfig(YGNodeRef node, YGConfigRef config) { - static_cast(node)->setConfig(static_cast(config)); + node->setConfig(static_cast(config)); } YOGA_EXPORT bool YGNodeHasMeasureFunc(YGNodeRef node) { - return static_cast(node)->hasMeasureFunc(); + return node->hasMeasureFunc(); } YOGA_EXPORT void YGNodeSetMeasureFunc( YGNodeRef node, YGMeasureFunc measureFunc) { - static_cast(node)->setMeasureFunc(measureFunc); + node->setMeasureFunc(measureFunc); } YOGA_EXPORT bool YGNodeHasBaselineFunc(YGNodeRef node) { - return static_cast(node)->hasBaselineFunc(); + return node->hasBaselineFunc(); } YOGA_EXPORT void YGNodeSetBaselineFunc( YGNodeRef node, YGBaselineFunc baselineFunc) { - static_cast(node)->setBaselineFunc(baselineFunc); + node->setBaselineFunc(baselineFunc); } YOGA_EXPORT YGDirtiedFunc YGNodeGetDirtiedFunc(YGNodeRef node) { - return static_cast(node)->getDirtied(); + return node->getDirtied(); } YOGA_EXPORT void YGNodeSetDirtiedFunc( YGNodeRef node, YGDirtiedFunc dirtiedFunc) { - static_cast(node)->setDirtiedFunc(dirtiedFunc); + node->setDirtiedFunc(dirtiedFunc); } YOGA_EXPORT void YGNodeSetPrintFunc(YGNodeRef node, YGPrintFunc printFunc) { - static_cast(node)->setPrintFunc(printFunc); + node->setPrintFunc(printFunc); } YOGA_EXPORT bool YGNodeGetHasNewLayout(YGNodeRef node) { - return static_cast(node)->getHasNewLayout(); + return node->getHasNewLayout(); } YOGA_EXPORT void YGConfigSetPrintTreeFlag(YGConfigRef config, bool enabled) { @@ -166,30 +166,30 @@ YOGA_EXPORT void YGConfigSetPrintTreeFlag(YGConfigRef config, bool enabled) { } YOGA_EXPORT void YGNodeSetHasNewLayout(YGNodeRef node, bool hasNewLayout) { - static_cast(node)->setHasNewLayout(hasNewLayout); + node->setHasNewLayout(hasNewLayout); } YOGA_EXPORT YGNodeType YGNodeGetNodeType(YGNodeRef node) { - return static_cast(node)->getNodeType(); + return node->getNodeType(); } YOGA_EXPORT void YGNodeSetNodeType(YGNodeRef node, YGNodeType nodeType) { - return static_cast(node)->setNodeType(nodeType); + return node->setNodeType(nodeType); } YOGA_EXPORT bool YGNodeIsDirty(YGNodeRef node) { - return static_cast(node)->isDirty(); + return node->isDirty(); } YOGA_EXPORT void YGNodeMarkDirtyAndPropagateToDescendants( const YGNodeRef node) { - return static_cast(node)->markDirtyAndPropagateDownwards(); + return node->markDirtyAndPropagateDownwards(); } int32_t gConfigInstanceCount = 0; YOGA_EXPORT WIN_EXPORT YGNodeRef YGNodeNewWithConfig(const YGConfigRef config) { - auto* node = new yoga::Node{static_cast(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"); @@ -207,9 +207,8 @@ YOGA_EXPORT YGNodeRef YGNodeNew(void) { return YGNodeNewWithConfig(YGConfigGetDefault()); } -YOGA_EXPORT YGNodeRef YGNodeClone(YGNodeRef oldNodeRef) { - auto oldNode = static_cast(oldNodeRef); - auto node = new yoga::Node(*oldNode); +YOGA_EXPORT YGNodeRef YGNodeClone(YGNodeRef oldNode) { + YGNodeRef node = new YGNode(*oldNode); YGAssertWithConfig( oldNode->getConfig(), node != nullptr, @@ -219,17 +218,15 @@ YOGA_EXPORT YGNodeRef YGNodeClone(YGNodeRef oldNodeRef) { return node; } -YOGA_EXPORT void YGNodeFree(const YGNodeRef nodeRef) { - auto node = static_cast(nodeRef); - - if (auto owner = node->getOwner()) { +YOGA_EXPORT void YGNodeFree(const YGNodeRef node) { + if (YGNodeRef owner = node->getOwner()) { owner->removeChild(node); node->setOwner(nullptr); } const uint32_t childCount = YGNodeGetChildCount(node); for (uint32_t i = 0; i < childCount; i++) { - auto child = node->getChild(i); + const YGNodeRef child = YGNodeGetChild(node, i); child->setOwner(nullptr); } @@ -238,18 +235,16 @@ YOGA_EXPORT void YGNodeFree(const YGNodeRef nodeRef) { } YOGA_EXPORT void YGNodeDeallocate(const YGNodeRef node) { - Event::publish(node, {YGNodeGetConfig(node)}); - delete static_cast(node); + Event::publish(node, {node->getConfig()}); + delete node; } YOGA_EXPORT void YGNodeFreeRecursiveWithCleanupFunc( - const YGNodeRef rootRef, + const YGNodeRef root, YGNodeCleanupFunc cleanup) { - const auto root = static_cast(rootRef); - uint32_t skipped = 0; while (YGNodeGetChildCount(root) > skipped) { - const auto child = root->getChild(skipped); + const YGNodeRef child = YGNodeGetChild(root, skipped); if (child->getOwner() != root) { // Don't free shared nodes that we don't own. skipped += 1; @@ -269,7 +264,7 @@ YOGA_EXPORT void YGNodeFreeRecursive(const YGNodeRef root) { } YOGA_EXPORT void YGNodeReset(YGNodeRef node) { - static_cast(node)->reset(); + node->reset(); } YOGA_EXPORT int32_t YGConfigGetInstanceCount(void) { @@ -292,9 +287,8 @@ YOGA_EXPORT void YGConfigFree(const YGConfigRef config) { } YOGA_EXPORT void YGNodeSetIsReferenceBaseline( - YGNodeRef nodeRef, + YGNodeRef node, bool isReferenceBaseline) { - auto node = static_cast(nodeRef); if (node->isReferenceBaseline() != isReferenceBaseline) { node->setIsReferenceBaseline(isReferenceBaseline); node->markDirtyAndPropagate(); @@ -302,16 +296,13 @@ YOGA_EXPORT void YGNodeSetIsReferenceBaseline( } YOGA_EXPORT bool YGNodeIsReferenceBaseline(YGNodeRef node) { - return static_cast(node)->isReferenceBaseline(); + return node->isReferenceBaseline(); } YOGA_EXPORT void YGNodeInsertChild( - const YGNodeRef ownerRef, - const YGNodeRef childRef, + const YGNodeRef owner, + const YGNodeRef child, const uint32_t index) { - auto owner = static_cast(ownerRef); - auto child = static_cast(childRef); - YGAssertWithNode( owner, child->getOwner() == nullptr, @@ -328,22 +319,16 @@ YOGA_EXPORT void YGNodeInsertChild( } YOGA_EXPORT void YGNodeSwapChild( - const YGNodeRef ownerRef, - const YGNodeRef childRef, + const YGNodeRef owner, + const YGNodeRef child, const uint32_t index) { - auto owner = static_cast(ownerRef); - auto child = static_cast(childRef); - owner->replaceChild(child, index); child->setOwner(owner); } YOGA_EXPORT void YGNodeRemoveChild( - const YGNodeRef ownerRef, - const YGNodeRef excludedChildRef) { - auto owner = static_cast(ownerRef); - auto excludedChild = static_cast(excludedChildRef); - + const YGNodeRef owner, + const YGNodeRef excludedChild) { if (YGNodeGetChildCount(owner) == 0) { // This is an empty set. Nothing to remove. return; @@ -362,21 +347,19 @@ YOGA_EXPORT void YGNodeRemoveChild( } } -YOGA_EXPORT void YGNodeRemoveAllChildren(const YGNodeRef ownerRef) { - auto owner = static_cast(ownerRef); - +YOGA_EXPORT void YGNodeRemoveAllChildren(const YGNodeRef owner) { const uint32_t childCount = YGNodeGetChildCount(owner); if (childCount == 0) { // This is an empty set already. Nothing to do. return; } - auto* firstChild = owner->getChild(0); + const YGNodeRef firstChild = YGNodeGetChild(owner, 0); if (firstChild->getOwner() == owner) { // If the first child has this node as its owner, we assume that this child // set is unique. for (uint32_t i = 0; i < childCount; i++) { - yoga::Node* oldChild = owner->getChild(i); - oldChild->setLayout({}); // layout is no longer valid + const YGNodeRef oldChild = YGNodeGetChild(owner, i); + oldChild->setLayout(YGNode().getLayout()); // layout is no longer valid oldChild->setOwner(nullptr); } owner->clearChildren(); @@ -385,45 +368,42 @@ YOGA_EXPORT void YGNodeRemoveAllChildren(const YGNodeRef ownerRef) { } // Otherwise, we are not the owner of the child set. We don't have to do // anything to clear it. - owner->setChildren({}); + owner->setChildren(YGVector()); owner->markDirtyAndPropagate(); } YOGA_EXPORT void YGNodeSetChildren( - const YGNodeRef ownerRef, - const YGNodeRef* childrenRefs, + const YGNodeRef owner, + const YGNodeRef* children, const uint32_t count) { - auto owner = static_cast(ownerRef); - auto children = reinterpret_cast(childrenRefs); - if (!owner) { return; } - const std::vector childrenVector = {children, children + count}; + const YGVector childrenVector = {children, children + count}; if (childrenVector.size() == 0) { if (YGNodeGetChildCount(owner) > 0) { - for (auto* child : owner->getChildren()) { - child->setLayout({}); + for (YGNodeRef const child : owner->getChildren()) { + child->setLayout(YGLayout()); child->setOwner(nullptr); } - owner->setChildren({}); + owner->setChildren(YGVector()); owner->markDirtyAndPropagate(); } } else { if (YGNodeGetChildCount(owner) > 0) { - for (auto* oldChild : owner->getChildren()) { + for (YGNodeRef const oldChild : owner->getChildren()) { // Our new children may have nodes in common with the old children. We // don't reset these common nodes. if (std::find(childrenVector.begin(), childrenVector.end(), oldChild) == childrenVector.end()) { - oldChild->setLayout({}); + oldChild->setLayout(YGLayout()); oldChild->setOwner(nullptr); } } } owner->setChildren(childrenVector); - for (yoga::Node* child : childrenVector) { + for (YGNodeRef child : childrenVector) { child->setOwner(owner); } owner->markDirtyAndPropagate(); @@ -431,31 +411,26 @@ YOGA_EXPORT void YGNodeSetChildren( } YOGA_EXPORT YGNodeRef -YGNodeGetChild(const YGNodeRef nodeRef, const uint32_t index) { - auto node = static_cast(nodeRef); - +YGNodeGetChild(const YGNodeRef node, const uint32_t index) { if (index < node->getChildren().size()) { return node->getChild(index); } return nullptr; } -YOGA_EXPORT uint32_t YGNodeGetChildCount(const YGNodeConstRef node) { - return static_cast( - static_cast(node)->getChildren().size()); +YOGA_EXPORT uint32_t YGNodeGetChildCount(const YGNodeRef node) { + return static_cast(node->getChildren().size()); } YOGA_EXPORT YGNodeRef YGNodeGetOwner(const YGNodeRef node) { - return static_cast(node)->getOwner(); + return node->getOwner(); } YOGA_EXPORT YGNodeRef YGNodeGetParent(const YGNodeRef node) { - return static_cast(node)->getOwner(); + return node->getOwner(); } -YOGA_EXPORT void YGNodeMarkDirty(const YGNodeRef nodeRef) { - auto node = static_cast(nodeRef); - +YOGA_EXPORT void YGNodeMarkDirty(const YGNodeRef node) { YGAssertWithNode( node, node->hasMeasureFunc(), @@ -466,26 +441,21 @@ YOGA_EXPORT void YGNodeMarkDirty(const YGNodeRef nodeRef) { } YOGA_EXPORT void YGNodeCopyStyle( - const YGNodeRef dstNodeRef, - const YGNodeRef srcNodeRef) { - auto dstNode = static_cast(dstNodeRef); - auto srcNode = static_cast(srcNodeRef); - + const YGNodeRef dstNode, + const YGNodeRef srcNode) { if (!(dstNode->getStyle() == srcNode->getStyle())) { dstNode->setStyle(srcNode->getStyle()); dstNode->markDirtyAndPropagate(); } } -YOGA_EXPORT float YGNodeStyleGetFlexGrow(const YGNodeConstRef nodeRef) { - auto node = static_cast(nodeRef); +YOGA_EXPORT float YGNodeStyleGetFlexGrow(const YGNodeConstRef node) { return node->getStyle().flexGrow().isUndefined() ? kDefaultFlexGrow : node->getStyle().flexGrow().unwrap(); } -YOGA_EXPORT float YGNodeStyleGetFlexShrink(const YGNodeConstRef nodeRef) { - auto node = static_cast(nodeRef); +YOGA_EXPORT float YGNodeStyleGetFlexShrink(const YGNodeConstRef node) { return node->getStyle().flexShrink().isUndefined() ? (node->getConfig()->useWebDefaults() ? kWebDefaultFlexShrink : kDefaultFlexShrink) @@ -496,7 +466,7 @@ namespace { template void updateStyle( - yoga::Node* node, + YGNode* node, T value, NeedsUpdate&& needsUpdate, Update&& update) { @@ -507,9 +477,9 @@ void updateStyle( } template -void updateStyle(YGNodeRef node, Ref (Style::*prop)(), T value) { +void updateStyle(YGNode* node, Ref (Style::*prop)(), T value) { updateStyle( - static_cast(node), + node, value, [prop](Style& s, T x) { return (s.*prop)() != x; }, [prop](Style& s, T x) { (s.*prop)() = x; }); @@ -517,12 +487,12 @@ void updateStyle(YGNodeRef node, Ref (Style::*prop)(), T value) { template void updateIndexedStyleProp( - YGNodeRef node, + YGNode* node, Ref (Style::*prop)(), Idx idx, CompactValue value) { updateStyle( - static_cast(node), + node, value, [idx, prop](Style& s, CompactValue x) { return (s.*prop)()[idx] != x; }, [idx, prop](Style& s, CompactValue x) { (s.*prop)()[idx] = x; }); @@ -543,7 +513,7 @@ YOGA_EXPORT void YGNodeStyleSetDirection( updateStyle(node, &Style::direction, value); } YOGA_EXPORT YGDirection YGNodeStyleGetDirection(const YGNodeConstRef node) { - return static_cast(node)->getStyle().direction(); + return node->getStyle().direction(); } YOGA_EXPORT void YGNodeStyleSetFlexDirection( @@ -554,7 +524,7 @@ YOGA_EXPORT void YGNodeStyleSetFlexDirection( } YOGA_EXPORT YGFlexDirection YGNodeStyleGetFlexDirection(const YGNodeConstRef node) { - return static_cast(node)->getStyle().flexDirection(); + return node->getStyle().flexDirection(); } YOGA_EXPORT void YGNodeStyleSetJustifyContent( @@ -564,7 +534,7 @@ YOGA_EXPORT void YGNodeStyleSetJustifyContent( node, &Style::justifyContent, justifyContent); } YOGA_EXPORT YGJustify YGNodeStyleGetJustifyContent(const YGNodeConstRef node) { - return static_cast(node)->getStyle().justifyContent(); + return node->getStyle().justifyContent(); } YOGA_EXPORT void YGNodeStyleSetAlignContent( @@ -574,7 +544,7 @@ YOGA_EXPORT void YGNodeStyleSetAlignContent( node, &Style::alignContent, alignContent); } YOGA_EXPORT YGAlign YGNodeStyleGetAlignContent(const YGNodeConstRef node) { - return static_cast(node)->getStyle().alignContent(); + return node->getStyle().alignContent(); } YOGA_EXPORT void YGNodeStyleSetAlignItems( @@ -583,7 +553,7 @@ YOGA_EXPORT void YGNodeStyleSetAlignItems( updateStyle(node, &Style::alignItems, alignItems); } YOGA_EXPORT YGAlign YGNodeStyleGetAlignItems(const YGNodeConstRef node) { - return static_cast(node)->getStyle().alignItems(); + return node->getStyle().alignItems(); } YOGA_EXPORT void YGNodeStyleSetAlignSelf( @@ -592,7 +562,7 @@ YOGA_EXPORT void YGNodeStyleSetAlignSelf( updateStyle(node, &Style::alignSelf, alignSelf); } YOGA_EXPORT YGAlign YGNodeStyleGetAlignSelf(const YGNodeConstRef node) { - return static_cast(node)->getStyle().alignSelf(); + return node->getStyle().alignSelf(); } YOGA_EXPORT void YGNodeStyleSetPositionType( @@ -603,7 +573,7 @@ YOGA_EXPORT void YGNodeStyleSetPositionType( } YOGA_EXPORT YGPositionType YGNodeStyleGetPositionType(const YGNodeConstRef node) { - return static_cast(node)->getStyle().positionType(); + return node->getStyle().positionType(); } YOGA_EXPORT void YGNodeStyleSetFlexWrap( @@ -612,7 +582,7 @@ YOGA_EXPORT void YGNodeStyleSetFlexWrap( updateStyle(node, &Style::flexWrap, flexWrap); } YOGA_EXPORT YGWrap YGNodeStyleGetFlexWrap(const YGNodeConstRef node) { - return static_cast(node)->getStyle().flexWrap(); + return node->getStyle().flexWrap(); } YOGA_EXPORT void YGNodeStyleSetOverflow( @@ -621,7 +591,7 @@ YOGA_EXPORT void YGNodeStyleSetOverflow( updateStyle(node, &Style::overflow, overflow); } YOGA_EXPORT YGOverflow YGNodeStyleGetOverflow(const YGNodeConstRef node) { - return static_cast(node)->getStyle().overflow(); + return node->getStyle().overflow(); } YOGA_EXPORT void YGNodeStyleSetDisplay( @@ -630,7 +600,7 @@ YOGA_EXPORT void YGNodeStyleSetDisplay( updateStyle(node, &Style::display, display); } YOGA_EXPORT YGDisplay YGNodeStyleGetDisplay(const YGNodeConstRef node) { - return static_cast(node)->getStyle().display(); + return node->getStyle().display(); } // TODO(T26792433): Change the API to accept YGFloatOptional. @@ -639,8 +609,7 @@ YOGA_EXPORT void YGNodeStyleSetFlex(const YGNodeRef node, const float flex) { } // TODO(T26792433): Change the API to accept YGFloatOptional. -YOGA_EXPORT float YGNodeStyleGetFlex(const YGNodeConstRef nodeRef) { - auto node = static_cast(nodeRef); +YOGA_EXPORT float YGNodeStyleGetFlex(const YGNodeConstRef node) { return node->getStyle().flex().isUndefined() ? YGUndefined : node->getStyle().flex().unwrap(); @@ -663,8 +632,7 @@ YOGA_EXPORT void YGNodeStyleSetFlexShrink( } YOGA_EXPORT YGValue YGNodeStyleGetFlexBasis(const YGNodeConstRef node) { - YGValue flexBasis = - static_cast(node)->getStyle().flexBasis(); + YGValue flexBasis = node->getStyle().flexBasis(); if (flexBasis.unit == YGUnitUndefined || flexBasis.unit == YGUnitAuto) { // TODO(T26792433): Get rid off the use of YGUndefined at client side flexBasis.value = YGUndefined; @@ -708,7 +676,7 @@ YOGA_EXPORT void YGNodeStyleSetPositionPercent( node, &Style::position, edge, value); } YOGA_EXPORT YGValue YGNodeStyleGetPosition(YGNodeConstRef node, YGEdge edge) { - return static_cast(node)->getStyle().position()[edge]; + return node->getStyle().position()[edge]; } YOGA_EXPORT void YGNodeStyleSetMargin( @@ -730,7 +698,7 @@ YOGA_EXPORT void YGNodeStyleSetMarginAuto(YGNodeRef node, YGEdge edge) { node, &Style::margin, edge, CompactValue::ofAuto()); } YOGA_EXPORT YGValue YGNodeStyleGetMargin(YGNodeConstRef node, YGEdge edge) { - return static_cast(node)->getStyle().margin()[edge]; + return node->getStyle().margin()[edge]; } YOGA_EXPORT void YGNodeStyleSetPadding( @@ -750,7 +718,7 @@ YOGA_EXPORT void YGNodeStyleSetPaddingPercent( node, &Style::padding, edge, value); } YOGA_EXPORT YGValue YGNodeStyleGetPadding(YGNodeConstRef node, YGEdge edge) { - return static_cast(node)->getStyle().padding()[edge]; + return node->getStyle().padding()[edge]; } // TODO(T26792433): Change the API to accept YGFloatOptional. @@ -765,7 +733,7 @@ YOGA_EXPORT void YGNodeStyleSetBorder( YOGA_EXPORT float YGNodeStyleGetBorder( const YGNodeConstRef node, const YGEdge edge) { - auto border = static_cast(node)->getStyle().border()[edge]; + auto border = node->getStyle().border()[edge]; if (border.isUndefined() || border.isAuto()) { // TODO(T26792433): Rather than returning YGUndefined, change the api to // return YGFloatOptional. @@ -786,8 +754,7 @@ YOGA_EXPORT void YGNodeStyleSetGap( YOGA_EXPORT float YGNodeStyleGetGap( const YGNodeConstRef node, const YGGutter gutter) { - auto gapLength = - static_cast(node)->getStyle().gap()[gutter]; + auto gapLength = node->getStyle().gap()[gutter]; if (gapLength.isUndefined() || gapLength.isAuto()) { // TODO(T26792433): Rather than returning YGUndefined, change the api to // return YGFloatOptional. @@ -801,8 +768,7 @@ YOGA_EXPORT float YGNodeStyleGetGap( // TODO(T26792433): Change the API to accept YGFloatOptional. YOGA_EXPORT float YGNodeStyleGetAspectRatio(const YGNodeConstRef node) { - const YGFloatOptional op = - static_cast(node)->getStyle().aspectRatio(); + const YGFloatOptional op = node->getStyle().aspectRatio(); return op.isUndefined() ? YGUndefined : op.unwrap(); } @@ -829,9 +795,7 @@ YOGA_EXPORT void YGNodeStyleSetWidthAuto(YGNodeRef node) { node, &Style::dimensions, YGDimensionWidth, CompactValue::ofAuto()); } YOGA_EXPORT YGValue YGNodeStyleGetWidth(YGNodeConstRef node) { - return static_cast(node) - ->getStyle() - .dimensions()[YGDimensionWidth]; + return node->getStyle().dimensions()[YGDimensionWidth]; } YOGA_EXPORT void YGNodeStyleSetHeight(YGNodeRef node, float points) { @@ -849,9 +813,7 @@ YOGA_EXPORT void YGNodeStyleSetHeightAuto(YGNodeRef node) { node, &Style::dimensions, YGDimensionHeight, CompactValue::ofAuto()); } YOGA_EXPORT YGValue YGNodeStyleGetHeight(YGNodeConstRef node) { - return static_cast(node) - ->getStyle() - .dimensions()[YGDimensionHeight]; + return node->getStyle().dimensions()[YGDimensionHeight]; } YOGA_EXPORT void YGNodeStyleSetMinWidth( @@ -869,9 +831,7 @@ YOGA_EXPORT void YGNodeStyleSetMinWidthPercent( node, &Style::minDimensions, YGDimensionWidth, value); } YOGA_EXPORT YGValue YGNodeStyleGetMinWidth(const YGNodeConstRef node) { - return static_cast(node) - ->getStyle() - .minDimensions()[YGDimensionWidth]; + return node->getStyle().minDimensions()[YGDimensionWidth]; } YOGA_EXPORT void YGNodeStyleSetMinHeight( @@ -889,9 +849,7 @@ YOGA_EXPORT void YGNodeStyleSetMinHeightPercent( node, &Style::minDimensions, YGDimensionHeight, value); } YOGA_EXPORT YGValue YGNodeStyleGetMinHeight(const YGNodeConstRef node) { - return static_cast(node) - ->getStyle() - .minDimensions()[YGDimensionHeight]; + return node->getStyle().minDimensions()[YGDimensionHeight]; } YOGA_EXPORT void YGNodeStyleSetMaxWidth( @@ -909,9 +867,7 @@ YOGA_EXPORT void YGNodeStyleSetMaxWidthPercent( node, &Style::maxDimensions, YGDimensionWidth, value); } YOGA_EXPORT YGValue YGNodeStyleGetMaxWidth(const YGNodeConstRef node) { - return static_cast(node) - ->getStyle() - .maxDimensions()[YGDimensionWidth]; + return node->getStyle().maxDimensions()[YGDimensionWidth]; } YOGA_EXPORT void YGNodeStyleSetMaxHeight( @@ -929,20 +885,17 @@ YOGA_EXPORT void YGNodeStyleSetMaxHeightPercent( node, &Style::maxDimensions, YGDimensionHeight, value); } YOGA_EXPORT YGValue YGNodeStyleGetMaxHeight(const YGNodeConstRef node) { - return static_cast(node) - ->getStyle() - .maxDimensions()[YGDimensionHeight]; + return node->getStyle().maxDimensions()[YGDimensionHeight]; } -#define YG_NODE_LAYOUT_PROPERTY_IMPL(type, name, instanceName) \ - YOGA_EXPORT type YGNodeLayoutGet##name(const YGNodeRef node) { \ - return static_cast(node)->getLayout().instanceName; \ +#define YG_NODE_LAYOUT_PROPERTY_IMPL(type, name, instanceName) \ + YOGA_EXPORT type YGNodeLayoutGet##name(const YGNodeRef node) { \ + return node->getLayout().instanceName; \ } #define YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(type, name, instanceName) \ YOGA_EXPORT type YGNodeLayoutGet##name( \ - const YGNodeRef nodeRef, const YGEdge edge) { \ - auto node = static_cast(nodeRef); \ + const YGNodeRef node, const YGEdge edge) { \ YGAssertWithNode( \ node, \ edge <= YGEdgeEnd, \ @@ -983,7 +936,7 @@ YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Padding, padding) std::atomic gCurrentGenerationCount(0); bool YGLayoutNodeInternal( - yoga::Node* const node, + const YGNodeRef node, const float availableWidth, const float availableHeight, const YGDirection ownerDirection, @@ -1000,14 +953,19 @@ bool YGLayoutNodeInternal( const uint32_t generationCount); #ifdef DEBUG -YOGA_EXPORT void YGNodePrint( - const YGNodeRef nodeRef, +static void YGNodePrintInternal( + const YGNodeRef node, const YGPrintOptions options) { - const auto node = static_cast(nodeRef); std::string str; facebook::yoga::YGNodeToString(str, node, options, 0); Log::log(node, YGLogLevelDebug, nullptr, str.c_str()); } + +YOGA_EXPORT void YGNodePrint( + const YGNodeRef node, + const YGPrintOptions options) { + YGNodePrintInternal(node, options); +} #endif const std::array leading = { @@ -1026,7 +984,7 @@ static const std::array dim = { {YGDimensionHeight, YGDimensionHeight, YGDimensionWidth, YGDimensionWidth}}; static inline float YGNodePaddingAndBorderForAxis( - const yoga::Node* const node, + const YGNodeConstRef node, const YGFlexDirection axis, const float widthSize) { return (node->getLeadingPaddingAndBorder(axis, widthSize) + @@ -1034,9 +992,7 @@ static inline float YGNodePaddingAndBorderForAxis( .unwrap(); } -static inline YGAlign YGNodeAlignItem( - const yoga::Node* node, - const yoga::Node* child) { +static inline YGAlign YGNodeAlignItem(const YGNode* node, const YGNode* child) { const YGAlign align = child->getStyle().alignSelf() == YGAlignAuto ? node->getStyle().alignItems() : child->getStyle().alignSelf(); @@ -1047,7 +1003,7 @@ static inline YGAlign YGNodeAlignItem( return align; } -static float YGBaseline(yoga::Node* node, void* layoutContext) { +static float YGBaseline(const YGNodeRef node, void* layoutContext) { if (node->hasBaselineFunc()) { Event::publish(node); @@ -1066,10 +1022,10 @@ static float YGBaseline(yoga::Node* node, void* layoutContext) { return baseline; } - yoga::Node* baselineChild = nullptr; + YGNodeRef baselineChild = nullptr; const uint32_t childCount = YGNodeGetChildCount(node); for (uint32_t i = 0; i < childCount; i++) { - auto child = node->getChild(i); + const YGNodeRef child = YGNodeGetChild(node, i); if (child->getLineIndex() > 0) { break; } @@ -1095,7 +1051,7 @@ static float YGBaseline(yoga::Node* node, void* layoutContext) { return baseline + baselineChild->getLayout().position[YGEdgeTop]; } -static bool YGIsBaselineLayout(const yoga::Node* node) { +static bool YGIsBaselineLayout(const YGNodeRef node) { if (YGFlexDirectionIsColumn(node->getStyle().flexDirection())) { return false; } @@ -1104,7 +1060,7 @@ static bool YGIsBaselineLayout(const yoga::Node* node) { } const uint32_t childCount = YGNodeGetChildCount(node); for (uint32_t i = 0; i < childCount; i++) { - auto child = node->getChild(i); + const YGNodeRef child = YGNodeGetChild(node, i); if (child->getStyle().positionType() != YGPositionTypeAbsolute && child->getStyle().alignSelf() == YGAlignBaseline) { return true; @@ -1115,7 +1071,7 @@ static bool YGIsBaselineLayout(const yoga::Node* node) { } static inline float YGNodeDimWithMargin( - const yoga::Node* const node, + const YGNodeRef node, const YGFlexDirection axis, const float widthSize) { return node->getLayout().measuredDimensions[dim[axis]] + @@ -1125,7 +1081,7 @@ static inline float YGNodeDimWithMargin( } static inline bool YGNodeIsStyleDimDefined( - const yoga::Node* const node, + const YGNodeRef node, const YGFlexDirection axis, const float ownerSize) { bool isUndefined = @@ -1142,14 +1098,14 @@ static inline bool YGNodeIsStyleDimDefined( } static inline bool YGNodeIsLayoutDimDefined( - const yoga::Node* const node, + const YGNodeRef node, const YGFlexDirection axis) { const float value = node->getLayout().measuredDimensions[dim[axis]]; return !YGFloatIsUndefined(value) && value >= 0.0f; } static YGFloatOptional YGNodeBoundAxisWithinMinAndMax( - const yoga::Node* const node, + const YGNodeConstRef node, const YGFlexDirection axis, const YGFloatOptional value, const float axisSize) { @@ -1182,7 +1138,7 @@ static YGFloatOptional YGNodeBoundAxisWithinMinAndMax( // Like YGNodeBoundAxisWithinMinAndMax but also ensures that the value doesn't // go below the padding and border amount. static inline float YGNodeBoundAxis( - const yoga::Node* const node, + const YGNodeRef node, const YGFlexDirection axis, const float value, const float axisSize, @@ -1195,8 +1151,8 @@ static inline float YGNodeBoundAxis( } static void YGNodeSetChildTrailingPosition( - const yoga::Node* const node, - yoga::Node* const child, + const YGNodeRef node, + const YGNodeRef child, const YGFlexDirection axis) { const float size = child->getLayout().measuredDimensions[dim[axis]]; child->setLayoutPosition( @@ -1206,7 +1162,7 @@ static void YGNodeSetChildTrailingPosition( } static void YGConstrainMaxSizeForMode( - const yoga::Node* const node, + const YGNodeConstRef node, const enum YGFlexDirection axis, const float ownerAxisSize, const float ownerWidth, @@ -1233,8 +1189,8 @@ static void YGConstrainMaxSizeForMode( } static void YGNodeComputeFlexBasisForChild( - const yoga::Node* const node, - yoga::Node* const child, + const YGNodeRef node, + const YGNodeRef child, const float width, const YGMeasureMode widthMode, const float height, @@ -1432,8 +1388,8 @@ static void YGNodeComputeFlexBasisForChild( } static void YGNodeAbsoluteLayoutChild( - const yoga::Node* const node, - yoga::Node* const child, + const YGNodeRef node, + const YGNodeRef child, const float width, const YGMeasureMode widthMode, const float height, @@ -1666,7 +1622,7 @@ static void YGNodeAbsoluteLayoutChild( } static void YGNodeWithMeasureFuncSetMeasuredDimensions( - yoga::Node* const node, + const YGNodeRef node, float availableWidth, float availableHeight, const YGMeasureMode widthMeasureMode, @@ -1773,7 +1729,7 @@ static void YGNodeWithMeasureFuncSetMeasuredDimensions( // For nodes with no children, use the available values if they were provided, // or the minimum size as indicated by the padding and border sizes. static void YGNodeEmptyContainerSetMeasuredDimensions( - yoga::Node* const node, + const YGNodeRef node, const float availableWidth, const float availableHeight, const YGMeasureMode widthMeasureMode, @@ -1806,7 +1762,7 @@ static void YGNodeEmptyContainerSetMeasuredDimensions( } static bool YGNodeFixedSizeSetMeasuredDimensions( - yoga::Node* const node, + const YGNodeRef node, const float availableWidth, const float availableHeight, const YGMeasureMode widthMeasureMode, @@ -1851,7 +1807,7 @@ static bool YGNodeFixedSizeSetMeasuredDimensions( } static void YGZeroOutLayoutRecursively( - yoga::Node* const node, + const YGNodeRef node, void* layoutContext) { node->getLayout() = {}; node->setLayoutDimension(0, 0); @@ -1863,7 +1819,7 @@ static void YGZeroOutLayoutRecursively( } static float YGNodeCalculateAvailableInnerDim( - const yoga::Node* const node, + const YGNodeConstRef node, const YGDimension dimension, const float availableDim, const float paddingAndBorder, @@ -1894,7 +1850,7 @@ static float YGNodeCalculateAvailableInnerDim( } static float YGNodeComputeFlexBasisForChildren( - yoga::Node* const node, + const YGNodeRef node, const float availableInnerWidth, const float availableInnerHeight, YGMeasureMode widthMeasureMode, @@ -1909,7 +1865,7 @@ static float YGNodeComputeFlexBasisForChildren( const uint32_t generationCount) { float totalOuterFlexBasis = 0.0f; YGNodeRef singleFlexChild = nullptr; - const auto& children = node->getChildren(); + const YGVector& children = node->getChildren(); YGMeasureMode measureModeMainDim = YGFlexDirectionIsRow(mainAxis) ? widthMeasureMode : heightMeasureMode; // If there is only one child with flexGrow + flexShrink it means we can set @@ -1991,7 +1947,7 @@ static float YGNodeComputeFlexBasisForChildren( // YGNodeComputeFlexBasisForChildren function). This function calculates // YGCollectFlexItemsRowMeasurement static YGCollectFlexItemsRowValues YGCalculateCollectFlexItemsRowValues( - yoga::Node* const node, + const YGNodeRef& node, const YGDirection ownerDirection, const float mainAxisownerSize, const float availableInnerWidth, @@ -2010,7 +1966,7 @@ static YGCollectFlexItemsRowValues YGCalculateCollectFlexItemsRowValues( // Add items to the current line until it's full or we run out of items. uint32_t endOfLineIndex = startOfLineIndex; for (; endOfLineIndex < node->getChildren().size(); endOfLineIndex++) { - auto child = node->getChild(endOfLineIndex); + const YGNodeRef child = node->getChild(endOfLineIndex); if (child->getStyle().display() == YGDisplayNone || child->getStyle().positionType() == YGPositionTypeAbsolute) { continue; @@ -2083,7 +2039,7 @@ static YGCollectFlexItemsRowValues YGCalculateCollectFlexItemsRowValues( // please ensure that YGDistributeFreeSpaceFirstPass is called. static float YGDistributeFreeSpaceSecondPass( YGCollectFlexItemsRowValues& collectedFlexItemsValues, - yoga::Node* const node, + const YGNodeRef node, const YGFlexDirection mainAxis, const YGFlexDirection crossAxis, const float mainAxisownerSize, @@ -2386,7 +2342,7 @@ static void YGDistributeFreeSpaceFirstPass( // assigned to them. // static void YGResolveFlexibleLength( - yoga::Node* const node, + const YGNodeRef node, YGCollectFlexItemsRowValues& collectedFlexItemsValues, const YGFlexDirection mainAxis, const YGFlexDirection crossAxis, @@ -2437,7 +2393,7 @@ static void YGResolveFlexibleLength( } static void YGJustifyMainAxis( - yoga::Node* const node, + const YGNodeRef node, YGCollectFlexItemsRowValues& collectedFlexItemsValues, const uint32_t startOfLineIndex, const YGFlexDirection mainAxis, @@ -2489,7 +2445,7 @@ static void YGJustifyMainAxis( for (uint32_t i = startOfLineIndex; i < collectedFlexItemsValues.endOfLineIndex; i++) { - auto child = node->getChild(i); + const YGNodeRef child = node->getChild(i); if (child->getStyle().positionType() != YGPositionTypeAbsolute) { if (child->marginLeadingValue(mainAxis).unit == YGUnitAuto) { numberOfAutoMarginsOnCurrentLine++; @@ -2549,9 +2505,9 @@ static void YGJustifyMainAxis( for (uint32_t i = startOfLineIndex; i < collectedFlexItemsValues.endOfLineIndex; i++) { - const auto child = node->getChild(i); + const YGNodeRef child = node->getChild(i); const Style& childStyle = child->getStyle(); - const LayoutResults& childLayout = child->getLayout(); + const YGLayout childLayout = child->getLayout(); const bool isLastChild = i == collectedFlexItemsValues.endOfLineIndex - 1; // remove the gap if it is the last element of the line if (isLastChild) { @@ -2723,7 +2679,7 @@ static void YGJustifyMainAxis( // mode of YGMeasureModeUndefined in that dimension. // static void YGNodelayoutImpl( - yoga::Node* const node, + const YGNodeRef node, const float availableWidth, const float availableHeight, const YGDirection ownerDirection, @@ -3121,7 +3077,7 @@ static void YGNodelayoutImpl( // We can skip child alignment if we're just measuring the container. if (performLayout) { for (uint32_t i = startOfLineIndex; i < endOfLineIndex; i++) { - const auto child = node->getChild(i); + const YGNodeRef child = node->getChild(i); if (child->getStyle().display() == YGDisplayNone) { continue; } @@ -3323,7 +3279,7 @@ static void YGNodelayoutImpl( float maxAscentForCurrentLine = 0; float maxDescentForCurrentLine = 0; for (ii = startIndex; ii < childCount; ii++) { - const auto child = node->getChild(ii); + const YGNodeRef child = node->getChild(ii); if (child->getStyle().display() == YGDisplayNone) { continue; } @@ -3366,7 +3322,7 @@ static void YGNodelayoutImpl( if (performLayout) { for (ii = startIndex; ii < endIndex; ii++) { - const auto child = node->getChild(ii); + const YGNodeRef child = node->getChild(ii); if (child->getStyle().display() == YGDisplayNone) { continue; } @@ -3560,7 +3516,7 @@ static void YGNodelayoutImpl( // positions on wrap-reverse. if (performLayout && node->getStyle().flexWrap() == YGWrapWrapReverse) { for (uint32_t i = 0; i < childCount; i++) { - const auto child = node->getChild(i); + const YGNodeRef child = YGNodeGetChild(node, i); if (child->getStyle().positionType() != YGPositionTypeAbsolute) { child->setLayoutPosition( node->getLayout().measuredDimensions[dim[crossAxis]] - @@ -3609,7 +3565,7 @@ static void YGNodelayoutImpl( // Set trailing position if necessary. if (needsMainTrailingPos || needsCrossTrailingPos) { for (uint32_t i = 0; i < childCount; i++) { - const auto child = node->getChild(i); + const YGNodeRef child = node->getChild(i); if (child->getStyle().display() == YGDisplayNone) { continue; } @@ -3820,7 +3776,7 @@ YOGA_EXPORT bool YGNodeCanUseCachedMeasurement( // Return parameter is true if layout was performed, false if skipped // bool YGLayoutNodeInternal( - yoga::Node* const node, + const YGNodeRef node, const float availableWidth, const float availableHeight, const YGDirection ownerDirection, @@ -3835,7 +3791,7 @@ bool YGLayoutNodeInternal( void* const layoutContext, uint32_t depth, const uint32_t generationCount) { - LayoutResults* layout = &node->getLayout(); + YGLayout* layout = &node->getLayout(); depth++; @@ -4109,7 +4065,7 @@ YOGA_EXPORT float YGConfigGetPointScaleFactor(const YGConfigRef config) { } static void YGRoundToPixelGrid( - yoga::Node* const node, + const YGNodeRef node, const double pointScaleFactor, const double absoluteLeft, const double absoluteTop) { @@ -4174,20 +4130,22 @@ static void YGRoundToPixelGrid( const uint32_t childCount = YGNodeGetChildCount(node); for (uint32_t i = 0; i < childCount; i++) { YGRoundToPixelGrid( - node->getChild(i), pointScaleFactor, absoluteNodeLeft, absoluteNodeTop); + YGNodeGetChild(node, i), + pointScaleFactor, + absoluteNodeLeft, + absoluteNodeTop); } } YOGA_EXPORT void YGNodeCalculateLayoutWithContext( - const YGNodeRef nodeRef, + const YGNodeRef node, const float ownerWidth, const float ownerHeight, const YGDirection ownerDirection, void* layoutContext) { - Event::publish(nodeRef, {layoutContext}); - LayoutData markerData = {}; - const auto node = static_cast(nodeRef); + Event::publish(node, {layoutContext}); + LayoutData markerData = {}; // Increment the generation count. This will force the recursive routine to // visit all dirty nodes at least once. Subsequent visits will be skipped if @@ -4290,12 +4248,7 @@ YOGA_EXPORT void YGConfigSetLogger(const YGConfigRef config, YGLogger logger) { void YGAssert(const bool condition, const char* message) { if (!condition) { - Log::log( - static_cast(nullptr), - YGLogLevelFatal, - nullptr, - "%s\n", - message); + Log::log(YGNodeRef{nullptr}, YGLogLevelFatal, nullptr, "%s\n", message); throwLogicalErrorWithMessage(message); } } @@ -4305,12 +4258,7 @@ void YGAssertWithNode( const bool condition, const char* message) { if (!condition) { - Log::log( - static_cast(node), - YGLogLevelFatal, - nullptr, - "%s\n", - message); + Log::log(node, YGLogLevelFatal, nullptr, "%s\n", message); throwLogicalErrorWithMessage(message); } } diff --git a/packages/react-native/ReactCommon/yoga/yoga/Yoga.h b/packages/react-native/ReactCommon/yoga/yoga/Yoga.h index 0c8986896c8..e8bf0afffd2 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Yoga.h +++ b/packages/react-native/ReactCommon/yoga/yoga/Yoga.h @@ -79,7 +79,7 @@ WIN_EXPORT void YGNodeRemoveAllChildren(YGNodeRef node); WIN_EXPORT YGNodeRef YGNodeGetChild(YGNodeRef node, uint32_t index); WIN_EXPORT YGNodeRef YGNodeGetOwner(YGNodeRef node); WIN_EXPORT YGNodeRef YGNodeGetParent(YGNodeRef node); -WIN_EXPORT uint32_t YGNodeGetChildCount(YGNodeConstRef node); +WIN_EXPORT uint32_t YGNodeGetChildCount(YGNodeRef node); WIN_EXPORT void YGNodeSetChildren( YGNodeRef owner, const YGNodeRef* children, diff --git a/packages/react-native/ReactCommon/yoga/yoga/event/event.cpp b/packages/react-native/ReactCommon/yoga/yoga/event/event.cpp index b47e1fab7fd..ee47331d85a 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/event/event.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/event/event.cpp @@ -73,10 +73,7 @@ void Event::subscribe(std::function&& subscriber) { push(new Node{std::move(subscriber)}); } -void Event::publish( - YGNodeConstRef node, - Type eventType, - const Data& eventData) { +void Event::publish(const YGNode& node, Type eventType, const Data& eventData) { for (auto subscriber = subscribers.load(std::memory_order_relaxed); subscriber != nullptr; subscriber = subscriber->next) { diff --git a/packages/react-native/ReactCommon/yoga/yoga/event/event.h b/packages/react-native/ReactCommon/yoga/yoga/event/event.h index 3798e4663e7..4c34fa66d60 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/event/event.h +++ b/packages/react-native/ReactCommon/yoga/yoga/event/event.h @@ -7,13 +7,15 @@ #pragma once -#include - #include #include #include +#include #include +struct YGConfig; +struct YGNode; + namespace facebook::yoga { enum struct LayoutType : int { @@ -61,7 +63,7 @@ struct YOGA_EXPORT Event { NodeBaselineEnd, }; class Data; - using Subscriber = void(YGNodeConstRef, Type, Data); + using Subscriber = void(const YGNode&, Type, Data); using Subscribers = std::vector>; template @@ -85,22 +87,27 @@ struct YOGA_EXPORT Event { static void subscribe(std::function&& subscriber); template - static void publish(YGNodeConstRef node, const TypedData& eventData = {}) { + static void publish(const YGNode& node, const TypedData& eventData = {}) { publish(node, E, Data{eventData}); } + template + static void publish(const YGNode* node, const TypedData& eventData = {}) { + publish(*node, eventData); + } + private: - static void publish(YGNodeConstRef, Type, const Data&); + static void publish(const YGNode&, Type, const Data&); }; template <> struct Event::TypedData { - YGConfigRef config; + YGConfig* config; }; template <> struct Event::TypedData { - YGConfigRef config; + YGConfig* config; }; template <> diff --git a/packages/react-native/ReactCommon/yoga/yoga/log.cpp b/packages/react-native/ReactCommon/yoga/yoga/log.cpp index 3dc62c517d9..eb776629ddb 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/log.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/log.cpp @@ -9,7 +9,7 @@ #include "log.h" #include -#include +#include "YGNode.h" namespace facebook::yoga::detail { @@ -17,7 +17,7 @@ namespace { void vlog( yoga::Config* config, - yoga::Node* node, + YGNode* node, YGLogLevel level, void* context, const char* format, @@ -30,7 +30,7 @@ void vlog( } // namespace YOGA_EXPORT void Log::log( - yoga::Node* node, + YGNode* node, 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 3d468ae10c2..ad0fe4d6f52 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/log.h +++ b/packages/react-native/ReactCommon/yoga/yoga/log.h @@ -8,14 +8,16 @@ #pragma once #include -#include #include +struct YGNode; +struct YGConfig; + namespace facebook::yoga::detail { struct Log { static void log( - yoga::Node* node, + YGNode* node, YGLogLevel level, void*, const char* message,