From ce457faafc29bfd6c62d622a4553c9a71e197471 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Thu, 28 Feb 2019 06:22:17 -0800 Subject: [PATCH] Move reset logic to `YGNode::reset()` Summary: @public Moving logic from free C functions to the C++ layer. This will allow us to get rid of the dangerous copy / move assignment operators of `YGNode`. Reviewed By: SidharthGuglani Differential Revision: D14241564 fbshipit-source-id: aae9f2a7ffd23bb839f1747e4a0694578bae86ae --- ReactCommon/yoga/yoga/YGNode.cpp | 19 +++++++++++++++++++ ReactCommon/yoga/yoga/YGNode.h | 1 + ReactCommon/yoga/yoga/Yoga.cpp | 21 ++------------------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/ReactCommon/yoga/yoga/YGNode.cpp b/ReactCommon/yoga/yoga/YGNode.cpp index d160127256..7b741acfae 100644 --- a/ReactCommon/yoga/yoga/YGNode.cpp +++ b/ReactCommon/yoga/yoga/YGNode.cpp @@ -571,3 +571,22 @@ bool YGNode::isLayoutTreeEqualToNode(const YGNode& node) const { } return isLayoutTreeEqual; } + +void YGNode::reset() { + YGAssertWithNode( + this, + children_.size() == 0, + "Cannot reset a node which still has children attached"); + YGAssertWithNode( + this, owner_ == nullptr, "Cannot reset a node still attached to a owner"); + + clearChildren(); + + auto config = getConfig(); + *this = YGNode{}; + if (config->useWebDefaults) { + setStyleFlexDirection(YGFlexDirectionRow); + setStyleAlignContent(YGAlignStretch); + } + setConfig(config); +} diff --git a/ReactCommon/yoga/yoga/YGNode.h b/ReactCommon/yoga/yoga/YGNode.h index cf2367284a..42c99f005a 100644 --- a/ReactCommon/yoga/yoga/YGNode.h +++ b/ReactCommon/yoga/yoga/YGNode.h @@ -342,4 +342,5 @@ public: bool isNodeFlexible(); bool didUseLegacyFlag(); bool isLayoutTreeEqualToNode(const YGNode& node) const; + void reset(); }; diff --git a/ReactCommon/yoga/yoga/Yoga.cpp b/ReactCommon/yoga/yoga/Yoga.cpp index bbdf724e9b..27072c1c38 100644 --- a/ReactCommon/yoga/yoga/Yoga.cpp +++ b/ReactCommon/yoga/yoga/Yoga.cpp @@ -323,25 +323,8 @@ void YGNodeFreeRecursive(const YGNodeRef root) { return YGNodeFreeRecursiveWithCleanupFunc(root, nullptr); } -void YGNodeReset(const YGNodeRef node) { - YGAssertWithNode( - node, - YGNodeGetChildCount(node) == 0, - "Cannot reset a node which still has children attached"); - YGAssertWithNode( - node, - node->getOwner() == nullptr, - "Cannot reset a node still attached to a owner"); - - node->clearChildren(); - - const YGConfigRef config = node->getConfig(); - *node = YGNode(); - if (config->useWebDefaults) { - node->setStyleFlexDirection(YGFlexDirectionRow); - node->setStyleAlignContent(YGAlignStretch); - } - node->setConfig(config); +void YGNodeReset(YGNodeRef node) { + node->reset(); } int32_t YGNodeGetInstanceCount(void) {