From 6d975394ef7fed69876b6ec5581b90f73c31c9e0 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Tue, 29 Aug 2023 21:32:40 -0700 Subject: [PATCH] C++ Cleanup 3/N: Reorganize YGNode (#1350) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Pull Request resolved: https://github.com/facebook/yoga/pull/1350 X-link: https://github.com/facebook/react-native/pull/39170 ## This diff This diff adds a top level `node` directory for code related to Yoga nodes and data structures on them (inc moving `YGLayout` to `LayoutResults`). The public API for config handles is `YGNodeRef`, which is forward declared to be a pointer to a struct named `YGNode`. The existing `YGNode` is split into `yoga::Node`, as the private C++ implementation, inheriting from `YGNode`, a marker type represented as an empty struct. The public API continues to accept `YGNodeRef`, which continues to be `YGNode *`, but it must be cast to its concrete internal representation at the API boundary before doing work on it. This change ends up needing to touch quite a bit, due to the amount of code that mixed and matched private and public APIs. Don't be scared though, because these changes are very mechanical, and Phabricator's line-count is 3x the actual amount due to mirrors and dirsyncs. ## 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: rshest Differential Revision: D48712710 fbshipit-source-id: e8f8d697302415fe8ffd2fd5461ea8b825edcc9b --- java/jni/YGJNIVanilla.cpp | 6 +- java/jni/YGJTypesVanilla.h | 11 +- tests/EventsTest.cpp | 16 +- tests/YGAlignBaselineTest.cpp | 21 +- tests/YGAspectRatioTest.cpp | 3 +- tests/YGBaselineFuncTest.cpp | 7 +- tests/YGDirtiedTest.cpp | 33 +- tests/YGDirtyMarkingTest.cpp | 69 ++-- tests/YGMeasureCacheTest.cpp | 31 +- tests/YGMeasureModeTest.cpp | 46 +-- tests/YGMeasureTest.cpp | 93 +++-- tests/YGNodeCallbackTest.cpp | 94 +++-- tests/YGPersistenceTest.cpp | 10 +- tests/YGRoundingMeasureFuncTest.cpp | 7 +- tests/YGStyleTest.cpp | 15 +- tests/generated/YGConfigTest.cpp | 12 +- tests/util/TestUtil.cpp | 4 +- yoga/Utils.h | 4 +- yoga/YGNodePrint.cpp | 44 +-- yoga/YGNodePrint.h | 3 +- yoga/Yoga-internal.h | 2 - yoga/Yoga.cpp | 364 ++++++++++-------- yoga/Yoga.h | 2 +- yoga/event/event.cpp | 5 +- yoga/event/event.h | 21 +- yoga/log.cpp | 6 +- yoga/log.h | 6 +- yoga/{YGLayout.cpp => node/LayoutResults.cpp} | 10 +- yoga/{YGLayout.h => node/LayoutResults.h} | 10 +- yoga/{YGNode.cpp => node/Node.cpp} | 148 ++++--- yoga/{YGNode.h => node/Node.h} | 100 ++--- 31 files changed, 625 insertions(+), 578 deletions(-) rename yoga/{YGLayout.cpp => node/LayoutResults.cpp} (88%) rename yoga/{YGLayout.h => node/LayoutResults.h} (89%) rename yoga/{YGNode.cpp => node/Node.cpp} (79%) rename yoga/{YGNode.h => node/Node.h} (78%) diff --git a/java/jni/YGJNIVanilla.cpp b/java/jni/YGJNIVanilla.cpp index 5d80278415..b1cdc2dacf 100644 --- a/java/jni/YGJNIVanilla.cpp +++ b/java/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) { - _jlong2YGNodeRef(nativePointer) + static_cast(_jlong2YGNodeRef(nativePointer)) ->setMeasureFunc(hasMeasureFunc ? YGJNIMeasureFunc : nullptr); } @@ -715,7 +715,7 @@ static void jni_YGNodeSetHasBaselineFuncJNI( jobject /*obj*/, jlong nativePointer, jboolean hasBaselineFunc) { - _jlong2YGNodeRef(nativePointer) + static_cast(_jlong2YGNodeRef(nativePointer)) ->setBaselineFunc(hasBaselineFunc ? YGJNIBaselineFunc : nullptr); } diff --git a/java/jni/YGJTypesVanilla.h b/java/jni/YGJTypesVanilla.h index 506c304909..c1534fea69 100644 --- a/java/jni/YGJTypesVanilla.h +++ b/java/jni/YGJTypesVanilla.h @@ -14,11 +14,12 @@ #include "jni.h" class PtrJNodeMapVanilla { - std::map ptrsToIdxs_; - jobjectArray javaNodes_; + std::map ptrsToIdxs_{}; + jobjectArray javaNodes_{}; public: - PtrJNodeMapVanilla() : ptrsToIdxs_{}, javaNodes_{} {} + PtrJNodeMapVanilla() = default; + PtrJNodeMapVanilla(jlongArray javaNativePointers, jobjectArray javaNodes) : javaNodes_{javaNodes} { using namespace facebook::yoga::vanillajni; @@ -30,11 +31,11 @@ class PtrJNodeMapVanilla { javaNativePointers, 0, nativePointersSize, nativePointers.data()); for (size_t i = 0; i < nativePointersSize; ++i) { - ptrsToIdxs_[(YGNodeRef) nativePointers[i]] = i; + ptrsToIdxs_[(YGNodeConstRef) nativePointers[i]] = i; } } - facebook::yoga::vanillajni::ScopedLocalRef ref(YGNodeRef node) { + facebook::yoga::vanillajni::ScopedLocalRef ref(YGNodeConstRef node) { using namespace facebook::yoga::vanillajni; JNIEnv* env = getCurrentEnv(); diff --git a/tests/EventsTest.cpp b/tests/EventsTest.cpp index 648d844d54..11131d4333 100644 --- a/tests/EventsTest.cpp +++ b/tests/EventsTest.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include @@ -30,7 +29,7 @@ struct TypedEventTestData { }; struct EventArgs { - const YGNode* node; + const YGNodeConstRef node; Event::Type type; std::unique_ptr> dataPtr; std::unique_ptr> eventTestDataPtr; @@ -48,7 +47,7 @@ struct EventArgs { class EventTest : public ::testing::Test { ScopedEventSubscription subscription = {&EventTest::listen}; - static void listen(const YGNode&, Event::Type, Event::Data); + static void listen(YGNodeConstRef, Event::Type, Event::Data); public: static std::vector events; @@ -284,16 +283,16 @@ TEST_F(EventTest, baseline_functions_get_wrapped) { namespace { template -EventArgs createArgs(const YGNode& node, const Event::Data data) { +EventArgs createArgs(YGNodeConstRef node, const Event::Data data) { using Data = Event::TypedData; auto deleteData = [](void* x) { delete static_cast(x); }; - return {&node, E, {new Data{(data.get())}, deleteData}, nullptr}; + return {node, E, {new Data{(data.get())}, deleteData}, nullptr}; } template EventArgs createArgs( - const YGNode& node, + YGNodeConstRef node, const Event::Data data, TypedEventTestData eventTestData) { using EventTestData = TypedEventTestData; @@ -309,7 +308,10 @@ EventArgs createArgs( } // namespace -void EventTest::listen(const YGNode& node, Event::Type type, Event::Data data) { +void EventTest::listen( + YGNodeConstRef node, + Event::Type type, + Event::Data data) { switch (type) { case Event::NodeAllocation: events.push_back(createArgs(node, data)); diff --git a/tests/YGAlignBaselineTest.cpp b/tests/YGAlignBaselineTest.cpp index 92885725f0..df64a1517a 100644 --- a/tests/YGAlignBaselineTest.cpp +++ b/tests/YGAlignBaselineTest.cpp @@ -6,7 +6,6 @@ */ #include -#include #include static float _baselineFunc( @@ -197,7 +196,7 @@ TEST(YogaTest, align_baseline_parent_using_child_in_column_as_reference) { const YGNodeRef root_child1_child1 = createYGNode(config, YGFlexDirectionColumn, 500, 400, false); - root_child1_child1->setBaselineFunc(_baselineFunc); + YGNodeSetBaselineFunc(root_child1_child1, _baselineFunc); YGNodeSetIsReferenceBaseline(root_child1_child1, true); YGNodeInsertChild(root_child1, root_child1_child1, 1); @@ -242,7 +241,7 @@ TEST( const YGNodeRef root_child1_child1 = createYGNode(config, YGFlexDirectionColumn, 500, 400, false); - root_child1_child1->setBaselineFunc(_baselineFunc); + YGNodeSetBaselineFunc(root_child1_child1, _baselineFunc); YGNodeSetIsReferenceBaseline(root_child1_child1, true); YGNodeStyleSetPadding(root_child1_child1, YGEdgeLeft, 100); YGNodeStyleSetPadding(root_child1_child1, YGEdgeRight, 100); @@ -295,7 +294,7 @@ TEST( const YGNodeRef root_child1_child1 = createYGNode(config, YGFlexDirectionColumn, 500, 400, false); - root_child1_child1->setBaselineFunc(_baselineFunc); + YGNodeSetBaselineFunc(root_child1_child1, _baselineFunc); YGNodeSetIsReferenceBaseline(root_child1_child1, true); YGNodeInsertChild(root_child1, root_child1_child1, 1); @@ -344,7 +343,7 @@ TEST( const YGNodeRef root_child1_child1 = createYGNode(config, YGFlexDirectionColumn, 500, 400, false); - root_child1_child1->setBaselineFunc(_baselineFunc); + YGNodeSetBaselineFunc(root_child1_child1, _baselineFunc); YGNodeSetIsReferenceBaseline(root_child1_child1, true); YGNodeInsertChild(root_child1, root_child1_child1, 1); @@ -389,7 +388,7 @@ TEST( const YGNodeRef root_child1_child1 = createYGNode(config, YGFlexDirectionColumn, 500, 400, false); - root_child1_child1->setBaselineFunc(_baselineFunc); + YGNodeSetBaselineFunc(root_child1_child1, _baselineFunc); YGNodeSetIsReferenceBaseline(root_child1_child1, true); YGNodeStyleSetMargin(root_child1_child1, YGEdgeLeft, 100); YGNodeStyleSetMargin(root_child1_child1, YGEdgeRight, 100); @@ -436,7 +435,7 @@ TEST(YogaTest, align_baseline_parent_using_child_in_row_as_reference) { const YGNodeRef root_child1_child1 = createYGNode(config, YGFlexDirectionColumn, 500, 400, false); - root_child1_child1->setBaselineFunc(_baselineFunc); + YGNodeSetBaselineFunc(root_child1_child1, _baselineFunc); YGNodeSetIsReferenceBaseline(root_child1_child1, true); YGNodeInsertChild(root_child1, root_child1_child1, 1); @@ -481,7 +480,7 @@ TEST( const YGNodeRef root_child1_child1 = createYGNode(config, YGFlexDirectionColumn, 500, 400, false); - root_child1_child1->setBaselineFunc(_baselineFunc); + YGNodeSetBaselineFunc(root_child1_child1, _baselineFunc); YGNodeSetIsReferenceBaseline(root_child1_child1, true); YGNodeStyleSetPadding(root_child1_child1, YGEdgeLeft, 100); YGNodeStyleSetPadding(root_child1_child1, YGEdgeRight, 100); @@ -530,7 +529,7 @@ TEST( const YGNodeRef root_child1_child1 = createYGNode(config, YGFlexDirectionColumn, 500, 400, false); - root_child1_child1->setBaselineFunc(_baselineFunc); + YGNodeSetBaselineFunc(root_child1_child1, _baselineFunc); YGNodeSetIsReferenceBaseline(root_child1_child1, true); YGNodeStyleSetMargin(root_child1_child1, YGEdgeLeft, 100); YGNodeStyleSetMargin(root_child1_child1, YGEdgeRight, 100); @@ -670,7 +669,7 @@ TEST( const YGNodeRef root_child1_child1 = createYGNode(config, YGFlexDirectionColumn, 500, 400, false); - root_child1_child1->setBaselineFunc(_baselineFunc); + YGNodeSetBaselineFunc(root_child1_child1, _baselineFunc); YGNodeSetIsReferenceBaseline(root_child1_child1, true); YGNodeInsertChild(root_child1, root_child1_child1, 1); @@ -721,7 +720,7 @@ TEST( const YGNodeRef root_child1_child1 = createYGNode(config, YGFlexDirectionColumn, 500, 400, false); - root_child1_child1->setBaselineFunc(_baselineFunc); + YGNodeSetBaselineFunc(root_child1_child1, _baselineFunc); YGNodeSetIsReferenceBaseline(root_child1_child1, true); YGNodeInsertChild(root_child1, root_child1_child1, 1); diff --git a/tests/YGAspectRatioTest.cpp b/tests/YGAspectRatioTest.cpp index 149ff43547..4a5fe60003 100644 --- a/tests/YGAspectRatioTest.cpp +++ b/tests/YGAspectRatioTest.cpp @@ -6,7 +6,6 @@ */ #include -#include #include static YGSize _measure( @@ -449,7 +448,7 @@ TEST(YogaTest, aspect_ratio_with_measure_func) { YGNodeStyleSetHeight(root, 100); const YGNodeRef root_child0 = YGNodeNew(); - root_child0->setMeasureFunc(_measure); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeStyleSetAspectRatio(root_child0, 1); YGNodeInsertChild(root, root_child0, 0); diff --git a/tests/YGBaselineFuncTest.cpp b/tests/YGBaselineFuncTest.cpp index f380100d47..5888dcae88 100644 --- a/tests/YGBaselineFuncTest.cpp +++ b/tests/YGBaselineFuncTest.cpp @@ -6,14 +6,13 @@ */ #include -#include #include static float _baseline( YGNodeRef node, const float /*width*/, const float /*height*/) { - float* baseline = (float*) node->getContext(); + float* baseline = (float*) YGNodeGetContext(node); return *baseline; } @@ -36,9 +35,9 @@ TEST(YogaTest, align_baseline_customer_func) { float baselineValue = 10; const YGNodeRef root_child1_child0 = YGNodeNew(); - root_child1_child0->setContext(&baselineValue); + YGNodeSetContext(root_child1_child0, &baselineValue); YGNodeStyleSetWidth(root_child1_child0, 50); - root_child1_child0->setBaselineFunc(_baseline); + YGNodeSetBaselineFunc(root_child1_child0, _baseline); YGNodeStyleSetHeight(root_child1_child0, 20); YGNodeInsertChild(root_child1, root_child1_child0, 0); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); diff --git a/tests/YGDirtiedTest.cpp b/tests/YGDirtiedTest.cpp index c3e0785115..f996ac5f1b 100644 --- a/tests/YGDirtiedTest.cpp +++ b/tests/YGDirtiedTest.cpp @@ -6,10 +6,13 @@ */ #include -#include +#include +#include + +using namespace facebook; static void _dirtied(YGNodeRef node) { - int* dirtiedCount = (int*) node->getContext(); + int* dirtiedCount = (int*) YGNodeGetContext(node); (*dirtiedCount)++; } @@ -22,17 +25,17 @@ TEST(YogaTest, dirtied) { YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); int dirtiedCount = 0; - root->setContext(&dirtiedCount); - root->setDirtiedFunc(_dirtied); + YGNodeSetContext(root, &dirtiedCount); + YGNodeSetDirtiedFunc(root, _dirtied); ASSERT_EQ(0, dirtiedCount); // `_dirtied` MUST be called in case of explicit dirtying. - root->setDirty(true); + static_cast(root)->setDirty(true); ASSERT_EQ(1, dirtiedCount); // `_dirtied` MUST be called ONCE. - root->setDirty(true); + static_cast(root)->setDirty(true); ASSERT_EQ(1, dirtiedCount); } @@ -55,17 +58,17 @@ TEST(YogaTest, dirtied_propagation) { YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); int dirtiedCount = 0; - root->setContext(&dirtiedCount); - root->setDirtiedFunc(_dirtied); + YGNodeSetContext(root, &dirtiedCount); + YGNodeSetDirtiedFunc(root, _dirtied); ASSERT_EQ(0, dirtiedCount); // `_dirtied` MUST be called for the first time. - root_child0->markDirtyAndPropagate(); + static_cast(root_child0)->markDirtyAndPropagate(); ASSERT_EQ(1, dirtiedCount); // `_dirtied` must NOT be called for the second time. - root_child0->markDirtyAndPropagate(); + static_cast(root_child0)->markDirtyAndPropagate(); ASSERT_EQ(1, dirtiedCount); } @@ -88,20 +91,20 @@ TEST(YogaTest, dirtied_hierarchy) { YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); int dirtiedCount = 0; - root_child0->setContext(&dirtiedCount); - root_child0->setDirtiedFunc(_dirtied); + YGNodeSetContext(root_child0, &dirtiedCount); + YGNodeSetDirtiedFunc(root_child0, _dirtied); ASSERT_EQ(0, dirtiedCount); // `_dirtied` must NOT be called for descendants. - root->markDirtyAndPropagate(); + static_cast(root)->markDirtyAndPropagate(); ASSERT_EQ(0, dirtiedCount); // `_dirtied` must NOT be called for the sibling node. - root_child1->markDirtyAndPropagate(); + static_cast(root_child1)->markDirtyAndPropagate(); ASSERT_EQ(0, dirtiedCount); // `_dirtied` MUST be called in case of explicit dirtying. - root_child0->markDirtyAndPropagate(); + static_cast(root_child0)->markDirtyAndPropagate(); ASSERT_EQ(1, dirtiedCount); } diff --git a/tests/YGDirtyMarkingTest.cpp b/tests/YGDirtyMarkingTest.cpp index 4597f90715..b548a921e9 100644 --- a/tests/YGDirtyMarkingTest.cpp +++ b/tests/YGDirtyMarkingTest.cpp @@ -6,7 +6,7 @@ */ #include -#include +#include TEST(YogaTest, dirty_propagation) { const YGNodeRef root = YGNodeNew(); @@ -28,15 +28,15 @@ TEST(YogaTest, dirty_propagation) { YGNodeStyleSetWidth(root_child0, 20); - EXPECT_TRUE(root_child0->isDirty()); - EXPECT_FALSE(root_child1->isDirty()); - EXPECT_TRUE(root->isDirty()); + EXPECT_TRUE(YGNodeIsDirty(root_child0)); + EXPECT_FALSE(YGNodeIsDirty(root_child1)); + EXPECT_TRUE(YGNodeIsDirty(root)); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); - EXPECT_FALSE(root_child0->isDirty()); - EXPECT_FALSE(root_child1->isDirty()); - EXPECT_FALSE(root->isDirty()); + EXPECT_FALSE(YGNodeIsDirty(root_child0)); + EXPECT_FALSE(YGNodeIsDirty(root_child1)); + EXPECT_FALSE(YGNodeIsDirty(root)); YGNodeFreeRecursive(root); } @@ -61,9 +61,9 @@ TEST(YogaTest, dirty_propagation_only_if_prop_changed) { YGNodeStyleSetWidth(root_child0, 50); - EXPECT_FALSE(root_child0->isDirty()); - EXPECT_FALSE(root_child1->isDirty()); - EXPECT_FALSE(root->isDirty()); + EXPECT_FALSE(YGNodeIsDirty(root_child0)); + EXPECT_FALSE(YGNodeIsDirty(root_child1)); + EXPECT_FALSE(YGNodeIsDirty(root)); YGNodeFreeRecursive(root); } @@ -91,26 +91,26 @@ TEST(YogaTest, dirty_propagation_changing_layout_config) { YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); - EXPECT_FALSE(root->isDirty()); - EXPECT_FALSE(root_child0->isDirty()); - EXPECT_FALSE(root_child1->isDirty()); - EXPECT_FALSE(root_child0_child0->isDirty()); + EXPECT_FALSE(YGNodeIsDirty(root)); + EXPECT_FALSE(YGNodeIsDirty(root_child0)); + EXPECT_FALSE(YGNodeIsDirty(root_child1)); + EXPECT_FALSE(YGNodeIsDirty(root_child0_child0)); YGConfigRef newConfig = YGConfigNew(); YGConfigSetErrata(newConfig, YGErrataStretchFlexBasis); YGNodeSetConfig(root_child0, newConfig); - EXPECT_TRUE(root->isDirty()); - EXPECT_TRUE(root_child0->isDirty()); - EXPECT_FALSE(root_child1->isDirty()); - EXPECT_FALSE(root_child0_child0->isDirty()); + EXPECT_TRUE(YGNodeIsDirty(root)); + EXPECT_TRUE(YGNodeIsDirty(root_child0)); + EXPECT_FALSE(YGNodeIsDirty(root_child1)); + EXPECT_FALSE(YGNodeIsDirty(root_child0_child0)); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); - EXPECT_FALSE(root->isDirty()); - EXPECT_FALSE(root_child0->isDirty()); - EXPECT_FALSE(root_child1->isDirty()); - EXPECT_FALSE(root_child0_child0->isDirty()); + EXPECT_FALSE(YGNodeIsDirty(root)); + EXPECT_FALSE(YGNodeIsDirty(root_child0)); + EXPECT_FALSE(YGNodeIsDirty(root_child1)); + EXPECT_FALSE(YGNodeIsDirty(root_child0_child0)); YGConfigFree(newConfig); YGNodeFreeRecursive(root); @@ -139,10 +139,10 @@ TEST(YogaTest, dirty_propagation_changing_benign_config) { YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); - EXPECT_FALSE(root->isDirty()); - EXPECT_FALSE(root_child0->isDirty()); - EXPECT_FALSE(root_child1->isDirty()); - EXPECT_FALSE(root_child0_child0->isDirty()); + EXPECT_FALSE(YGNodeIsDirty(root)); + EXPECT_FALSE(YGNodeIsDirty(root_child0)); + EXPECT_FALSE(YGNodeIsDirty(root_child1)); + EXPECT_FALSE(YGNodeIsDirty(root_child0_child0)); YGConfigRef newConfig = YGConfigNew(); YGConfigSetLogger( @@ -152,10 +152,10 @@ TEST(YogaTest, dirty_propagation_changing_benign_config) { }); YGNodeSetConfig(root_child0, newConfig); - EXPECT_FALSE(root->isDirty()); - EXPECT_FALSE(root_child0->isDirty()); - EXPECT_FALSE(root_child1->isDirty()); - EXPECT_FALSE(root_child0_child0->isDirty()); + EXPECT_FALSE(YGNodeIsDirty(root)); + EXPECT_FALSE(YGNodeIsDirty(root_child0)); + EXPECT_FALSE(YGNodeIsDirty(root_child1)); + EXPECT_FALSE(YGNodeIsDirty(root_child0_child0)); YGConfigFree(newConfig); YGNodeFreeRecursive(root); @@ -224,11 +224,11 @@ TEST(YogaTest, dirty_node_only_if_children_are_actually_removed) { const YGNodeRef child1 = YGNodeNew(); YGNodeRemoveChild(root, child1); - EXPECT_FALSE(root->isDirty()); + EXPECT_FALSE(YGNodeIsDirty(root)); YGNodeFree(child1); YGNodeRemoveChild(root, child0); - EXPECT_TRUE(root->isDirty()); + EXPECT_TRUE(YGNodeIsDirty(root)); YGNodeFree(child0); YGNodeFreeRecursive(root); @@ -241,12 +241,11 @@ TEST(YogaTest, dirty_node_only_if_undefined_values_gets_set_to_undefined) { YGNodeStyleSetMinWidth(root, YGUndefined); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); - - EXPECT_FALSE(root->isDirty()); + EXPECT_FALSE(YGNodeIsDirty(root)); YGNodeStyleSetMinWidth(root, YGUndefined); - EXPECT_FALSE(root->isDirty()); + EXPECT_FALSE(YGNodeIsDirty(root)); YGNodeFreeRecursive(root); } diff --git a/tests/YGMeasureCacheTest.cpp b/tests/YGMeasureCacheTest.cpp index 86f9f5e644..a973837413 100644 --- a/tests/YGMeasureCacheTest.cpp +++ b/tests/YGMeasureCacheTest.cpp @@ -6,7 +6,6 @@ */ #include -#include #include static YGSize _measureMax( @@ -15,7 +14,7 @@ static YGSize _measureMax( YGMeasureMode widthMode, float height, YGMeasureMode heightMode) { - int* measureCount = (int*) node->getContext(); + int* measureCount = (int*) YGNodeGetContext(node); (*measureCount)++; return YGSize{ @@ -30,7 +29,7 @@ static YGSize _measureMin( YGMeasureMode widthMode, float height, YGMeasureMode heightMode) { - int* measureCount = (int*) node->getContext(); + int* measureCount = (int*) YGNodeGetContext(node); *measureCount = *measureCount + 1; return YGSize{ widthMode == YGMeasureModeUndefined || @@ -50,7 +49,7 @@ static YGSize _measure_84_49( YGMeasureMode /*widthMode*/, float /*height*/, YGMeasureMode /*heightMode*/) { - int* measureCount = (int*) node->getContext(); + int* measureCount = (int*) YGNodeGetContext(node); if (measureCount) { (*measureCount)++; } @@ -67,8 +66,8 @@ TEST(YogaTest, measure_once_single_flexible_child) { const YGNodeRef root_child0 = YGNodeNew(); int measureCount = 0; - root_child0->setContext(&measureCount); - root_child0->setMeasureFunc(_measureMax); + YGNodeSetContext(root_child0, &measureCount); + YGNodeSetMeasureFunc(root_child0, _measureMax); YGNodeStyleSetFlexGrow(root_child0, 1); YGNodeInsertChild(root, root_child0, 0); @@ -84,8 +83,8 @@ TEST(YogaTest, remeasure_with_same_exact_width_larger_than_needed_height) { const YGNodeRef root_child0 = YGNodeNew(); int measureCount = 0; - root_child0->setContext(&measureCount); - root_child0->setMeasureFunc(_measureMin); + YGNodeSetContext(root_child0, &measureCount); + YGNodeSetMeasureFunc(root_child0, _measureMin); YGNodeInsertChild(root, root_child0, 0); YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR); @@ -102,8 +101,8 @@ TEST(YogaTest, remeasure_with_same_atmost_width_larger_than_needed_height) { const YGNodeRef root_child0 = YGNodeNew(); int measureCount = 0; - root_child0->setContext(&measureCount); - root_child0->setMeasureFunc(_measureMin); + YGNodeSetContext(root_child0, &measureCount); + YGNodeSetMeasureFunc(root_child0, _measureMin); YGNodeInsertChild(root, root_child0, 0); YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR); @@ -120,8 +119,8 @@ TEST(YogaTest, remeasure_with_computed_width_larger_than_needed_height) { const YGNodeRef root_child0 = YGNodeNew(); int measureCount = 0; - root_child0->setContext(&measureCount); - root_child0->setMeasureFunc(_measureMin); + YGNodeSetContext(root_child0, &measureCount); + YGNodeSetMeasureFunc(root_child0, _measureMin); YGNodeInsertChild(root, root_child0, 0); YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR); @@ -139,8 +138,8 @@ TEST(YogaTest, remeasure_with_atmost_computed_width_undefined_height) { const YGNodeRef root_child0 = YGNodeNew(); int measureCount = 0; - root_child0->setContext(&measureCount); - root_child0->setMeasureFunc(_measureMin); + YGNodeSetContext(root_child0, &measureCount); + YGNodeSetMeasureFunc(root_child0, _measureMin); YGNodeInsertChild(root, root_child0, 0); YGNodeCalculateLayout(root, 100, YGUndefined, YGDirectionLTR); @@ -167,8 +166,8 @@ TEST( YGNodeInsertChild(root, root_child0, 0); const YGNodeRef root_child0_child0 = YGNodeNew(); - root_child0_child0->setContext(&measureCount); - root_child0_child0->setMeasureFunc(_measure_84_49); + YGNodeSetContext(root_child0_child0, &measureCount); + YGNodeSetMeasureFunc(root_child0_child0, _measure_84_49); YGNodeInsertChild(root_child0, root_child0_child0, 0); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); diff --git a/tests/YGMeasureModeTest.cpp b/tests/YGMeasureModeTest.cpp index 6547ed66a5..e9f977aa4e 100644 --- a/tests/YGMeasureModeTest.cpp +++ b/tests/YGMeasureModeTest.cpp @@ -6,7 +6,6 @@ */ #include -#include #include struct _MeasureConstraint { @@ -28,7 +27,7 @@ static YGSize _measure( float height, YGMeasureMode heightMode) { struct _MeasureConstraintList* constraintList = - (struct _MeasureConstraintList*) node->getContext(); + (struct _MeasureConstraintList*) YGNodeGetContext(node); struct _MeasureConstraint* constraints = constraintList->constraints; uint32_t currentIndex = constraintList->length; (&constraints[currentIndex])->width = width; @@ -55,10 +54,8 @@ TEST(YogaTest, exactly_measure_stretched_child_column) { YGNodeStyleSetHeight(root, 100); const YGNodeRef root_child0 = YGNodeNew(); - // root_child0->setContext(&constraintList); - root_child0->setContext(&constraintList); - root_child0->setMeasureFunc(_measure); - // root_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0, &constraintList); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeInsertChild(root, root_child0, 0); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); @@ -85,9 +82,8 @@ TEST(YogaTest, exactly_measure_stretched_child_row) { YGNodeStyleSetHeight(root, 100); const YGNodeRef root_child0 = YGNodeNew(); - // root_child0->setContext(&constraintList); - root_child0->setContext(&constraintList); - root_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0, &constraintList); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeInsertChild(root, root_child0, 0); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); @@ -113,8 +109,8 @@ TEST(YogaTest, at_most_main_axis_column) { YGNodeStyleSetHeight(root, 100); const YGNodeRef root_child0 = YGNodeNew(); - root_child0->setContext(&constraintList); - root_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0, &constraintList); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeInsertChild(root, root_child0, 0); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); @@ -141,8 +137,8 @@ TEST(YogaTest, at_most_cross_axis_column) { YGNodeStyleSetHeight(root, 100); const YGNodeRef root_child0 = YGNodeNew(); - root_child0->setContext(&constraintList); - root_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0, &constraintList); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeInsertChild(root, root_child0, 0); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); @@ -169,8 +165,8 @@ TEST(YogaTest, at_most_main_axis_row) { YGNodeStyleSetHeight(root, 100); const YGNodeRef root_child0 = YGNodeNew(); - root_child0->setContext(&constraintList); - root_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0, &constraintList); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeInsertChild(root, root_child0, 0); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); @@ -198,8 +194,8 @@ TEST(YogaTest, at_most_cross_axis_row) { YGNodeStyleSetHeight(root, 100); const YGNodeRef root_child0 = YGNodeNew(); - root_child0->setContext(&constraintList); - root_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0, &constraintList); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeInsertChild(root, root_child0, 0); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); @@ -225,8 +221,8 @@ TEST(YogaTest, flex_child) { const YGNodeRef root_child0 = YGNodeNew(); YGNodeStyleSetFlexGrow(root_child0, 1); - root_child0->setContext(&constraintList); - root_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0, &constraintList); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeInsertChild(root, root_child0, 0); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); @@ -256,8 +252,8 @@ TEST(YogaTest, flex_child_with_flex_basis) { const YGNodeRef root_child0 = YGNodeNew(); YGNodeStyleSetFlexGrow(root_child0, 1); YGNodeStyleSetFlexBasis(root_child0, 0); - root_child0->setContext(&constraintList); - root_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0, &constraintList); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeInsertChild(root, root_child0, 0); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); @@ -285,8 +281,8 @@ TEST(YogaTest, overflow_scroll_column) { YGNodeStyleSetWidth(root, 100); const YGNodeRef root_child0 = YGNodeNew(); - root_child0->setContext(&constraintList); - root_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0, &constraintList); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeInsertChild(root, root_child0, 0); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); @@ -318,8 +314,8 @@ TEST(YogaTest, overflow_scroll_row) { YGNodeStyleSetWidth(root, 100); const YGNodeRef root_child0 = YGNodeNew(); - root_child0->setContext(&constraintList); - root_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0, &constraintList); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeInsertChild(root, root_child0, 0); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); diff --git a/tests/YGMeasureTest.cpp b/tests/YGMeasureTest.cpp index bb046b58aa..e6d851cdc2 100644 --- a/tests/YGMeasureTest.cpp +++ b/tests/YGMeasureTest.cpp @@ -6,7 +6,6 @@ */ #include -#include #include static YGSize _measure( @@ -15,7 +14,7 @@ static YGSize _measure( YGMeasureMode /*widthMode*/, float /*height*/, YGMeasureMode /*heightMode*/) { - int* measureCount = (int*) node->getContext(); + int* measureCount = (int*) YGNodeGetContext(node); if (measureCount) { (*measureCount)++; } @@ -56,8 +55,8 @@ TEST(YogaTest, dont_measure_single_grow_shrink_child) { int measureCount = 0; const YGNodeRef root_child0 = YGNodeNew(); - root_child0->setContext(&measureCount); - root_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0, &measureCount); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeStyleSetFlexGrow(root_child0, 1); YGNodeStyleSetFlexShrink(root_child0, 1); YGNodeInsertChild(root, root_child0, 0); @@ -79,8 +78,8 @@ TEST(YogaTest, measure_absolute_child_with_no_constraints) { const YGNodeRef root_child0_child0 = YGNodeNew(); YGNodeStyleSetPositionType(root_child0_child0, YGPositionTypeAbsolute); - root_child0_child0->setContext(&measureCount); - root_child0_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0_child0, &measureCount); + YGNodeSetMeasureFunc(root_child0_child0, _measure); YGNodeInsertChild(root_child0, root_child0_child0, 0); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); @@ -99,8 +98,8 @@ TEST(YogaTest, dont_measure_when_min_equals_max) { int measureCount = 0; const YGNodeRef root_child0 = YGNodeNew(); - root_child0->setContext(&measureCount); - root_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0, &measureCount); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeStyleSetMinWidth(root_child0, 10); YGNodeStyleSetMaxWidth(root_child0, 10); YGNodeStyleSetMinHeight(root_child0, 10); @@ -127,8 +126,8 @@ TEST(YogaTest, dont_measure_when_min_equals_max_percentages) { int measureCount = 0; const YGNodeRef root_child0 = YGNodeNew(); - root_child0->setContext(&measureCount); - root_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0, &measureCount); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeStyleSetMinWidthPercent(root_child0, 10); YGNodeStyleSetMaxWidthPercent(root_child0, 10); YGNodeStyleSetMinHeightPercent(root_child0, 10); @@ -152,7 +151,7 @@ TEST(YogaTest, measure_nodes_with_margin_auto_and_stretch) { YGNodeStyleSetHeight(root, 500); const YGNodeRef root_child0 = YGNodeNew(); - root_child0->setMeasureFunc(_measure); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeStyleSetMarginAuto(root_child0, YGEdgeLeft); YGNodeInsertChild(root, root_child0, 0); @@ -175,8 +174,8 @@ TEST(YogaTest, dont_measure_when_min_equals_max_mixed_width_percent) { int measureCount = 0; const YGNodeRef root_child0 = YGNodeNew(); - root_child0->setContext(&measureCount); - root_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0, &measureCount); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeStyleSetMinWidthPercent(root_child0, 10); YGNodeStyleSetMaxWidthPercent(root_child0, 10); YGNodeStyleSetMinHeight(root_child0, 10); @@ -203,8 +202,8 @@ TEST(YogaTest, dont_measure_when_min_equals_max_mixed_height_percent) { int measureCount = 0; const YGNodeRef root_child0 = YGNodeNew(); - root_child0->setContext(&measureCount); - root_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0, &measureCount); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeStyleSetMinWidth(root_child0, 10); YGNodeStyleSetMaxWidth(root_child0, 10); YGNodeStyleSetMinHeightPercent(root_child0, 10); @@ -228,7 +227,7 @@ TEST(YogaTest, measure_enough_size_should_be_in_single_line) { const YGNodeRef root_child0 = YGNodeNew(); YGNodeStyleSetAlignSelf(root_child0, YGAlignFlexStart); - root_child0->setMeasureFunc(_simulate_wrapping_text); + YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); YGNodeInsertChild(root, root_child0, 0); @@ -247,7 +246,7 @@ TEST(YogaTest, measure_not_enough_size_should_wrap) { const YGNodeRef root_child0 = YGNodeNew(); YGNodeStyleSetAlignSelf(root_child0, YGAlignFlexStart); // YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); - root_child0->setMeasureFunc(_simulate_wrapping_text); + YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); YGNodeInsertChild(root, root_child0, 0); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); @@ -269,8 +268,8 @@ TEST(YogaTest, measure_zero_space_should_grow) { const YGNodeRef root_child0 = YGNodeNew(); YGNodeStyleSetFlexDirection(root_child0, YGFlexDirectionColumn); YGNodeStyleSetPadding(root_child0, YGEdgeAll, 100); - root_child0->setContext(&measureCount); - root_child0->setMeasureFunc(_measure); + YGNodeSetContext(root_child0, &measureCount); + YGNodeSetMeasureFunc(root_child0, _measure); YGNodeInsertChild(root, root_child0, 0); @@ -295,8 +294,7 @@ TEST(YogaTest, measure_flex_direction_row_and_padding) { YGNodeStyleSetHeight(root, 50); const YGNodeRef root_child0 = YGNodeNewWithConfig(config); - root_child0->setMeasureFunc(_simulate_wrapping_text); - // YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); + YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); YGNodeInsertChild(root, root_child0, 0); const YGNodeRef root_child1 = YGNodeNewWithConfig(config); @@ -335,7 +333,7 @@ TEST(YogaTest, measure_flex_direction_column_and_padding) { YGNodeStyleSetHeight(root, 50); const YGNodeRef root_child0 = YGNodeNewWithConfig(config); - root_child0->setMeasureFunc(_simulate_wrapping_text); + YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); // YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); YGNodeInsertChild(root, root_child0, 0); @@ -376,7 +374,7 @@ TEST(YogaTest, measure_flex_direction_row_no_padding) { const YGNodeRef root_child0 = YGNodeNewWithConfig(config); // YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); - root_child0->setMeasureFunc(_simulate_wrapping_text); + YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); YGNodeInsertChild(root, root_child0, 0); const YGNodeRef root_child1 = YGNodeNewWithConfig(config); @@ -416,7 +414,7 @@ TEST(YogaTest, measure_flex_direction_row_no_padding_align_items_flexstart) { YGNodeStyleSetAlignItems(root, YGAlignFlexStart); const YGNodeRef root_child0 = YGNodeNewWithConfig(config); - root_child0->setMeasureFunc(_simulate_wrapping_text); + YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); YGNodeInsertChild(root, root_child0, 0); const YGNodeRef root_child1 = YGNodeNewWithConfig(config); @@ -455,7 +453,7 @@ TEST(YogaTest, measure_with_fixed_size) { YGNodeStyleSetHeight(root, 50); const YGNodeRef root_child0 = YGNodeNewWithConfig(config); - root_child0->setMeasureFunc(_simulate_wrapping_text); + YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); YGNodeStyleSetWidth(root_child0, 10); YGNodeStyleSetHeight(root_child0, 10); YGNodeInsertChild(root, root_child0, 0); @@ -496,7 +494,7 @@ TEST(YogaTest, measure_with_flex_shrink) { YGNodeStyleSetHeight(root, 50); const YGNodeRef root_child0 = YGNodeNewWithConfig(config); - root_child0->setMeasureFunc(_simulate_wrapping_text); + YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); YGNodeStyleSetFlexShrink(root_child0, 1); YGNodeInsertChild(root, root_child0, 0); @@ -535,7 +533,7 @@ TEST(YogaTest, measure_no_padding) { YGNodeStyleSetHeight(root, 50); const YGNodeRef root_child0 = YGNodeNewWithConfig(config); - root_child0->setMeasureFunc(_simulate_wrapping_text); + YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text); YGNodeStyleSetFlexShrink(root_child0, 1); YGNodeInsertChild(root, root_child0, 0); @@ -568,7 +566,7 @@ TEST(YogaTest, measure_no_padding) { #if GTEST_HAS_DEATH_TEST TEST(YogaDeathTest, cannot_add_child_to_node_with_measure_func) { const YGNodeRef root = YGNodeNew(); - root->setMeasureFunc(_measure); + YGNodeSetMeasureFunc(root, _measure); const YGNodeRef root_child0 = YGNodeNew(); #if defined(__cpp_exceptions) @@ -585,9 +583,10 @@ TEST(YogaDeathTest, cannot_add_nonnull_measure_func_to_non_leaf_node) { const YGNodeRef root_child0 = YGNodeNew(); YGNodeInsertChild(root, root_child0, 0); #if defined(__cpp_exceptions) - ASSERT_THROW(root->setMeasureFunc(_measure), std::logic_error); + ASSERT_THROW(YGNodeSetMeasureFunc(root, _measure), std::logic_error); #else // !defined(__cpp_exceptions) - ASSERT_DEATH(root->setMeasureFunc(_measure), "Cannot set measure function.*"); + ASSERT_DEATH( + YGNodeSetMeasureFunc(root, _measure), "Cannot set measure function.*"); #endif // defined(__cpp_exceptions) YGNodeFreeRecursive(root); } @@ -597,8 +596,8 @@ TEST(YogaDeathTest, cannot_add_nonnull_measure_func_to_non_leaf_node) { TEST(YogaTest, can_nullify_measure_func_on_any_node) { const YGNodeRef root = YGNodeNew(); YGNodeInsertChild(root, YGNodeNew(), 0); - root->setMeasureFunc(nullptr); - ASSERT_TRUE(!root->hasMeasureFunc()); + YGNodeSetMeasureFunc(root, nullptr); + ASSERT_TRUE(!YGNodeHasMeasureFunc(root)); YGNodeFreeRecursive(root); } @@ -611,7 +610,7 @@ TEST(YogaTest, cant_call_negative_measure) { YGNodeStyleSetHeight(root, 10); const YGNodeRef root_child0 = YGNodeNewWithConfig(config); - root_child0->setMeasureFunc(_measure_assert_negative); + YGNodeSetMeasureFunc(root_child0, _measure_assert_negative); YGNodeStyleSetMargin(root_child0, YGEdgeTop, 20); YGNodeInsertChild(root, root_child0, 0); @@ -630,7 +629,7 @@ TEST(YogaTest, cant_call_negative_measure_horizontal) { YGNodeStyleSetHeight(root, 20); const YGNodeRef root_child0 = YGNodeNewWithConfig(config); - root_child0->setMeasureFunc(_measure_assert_negative); + YGNodeSetMeasureFunc(root_child0, _measure_assert_negative); YGNodeStyleSetMargin(root_child0, YGEdgeStart, 20); YGNodeInsertChild(root, root_child0, 0); @@ -674,7 +673,7 @@ TEST(YogaTest, percent_with_text_node) { YGNodeInsertChild(root, root_child0, 0); const YGNodeRef root_child1 = YGNodeNewWithConfig(config); - root_child1->setMeasureFunc(_measure_90_10); + YGNodeSetMeasureFunc(root_child1, _measure_90_10); YGNodeStyleSetMaxWidthPercent(root_child1, 50); YGNodeStyleSetPaddingPercent(root_child1, YGEdgeTop, 50); YGNodeInsertChild(root, root_child1, 1); @@ -713,28 +712,28 @@ TEST(YogaTest, percent_margin_with_measure_func) { YGNodeStyleSetWidth(root_child0, 100); YGNodeStyleSetHeight(root_child0, 100); YGNodeStyleSetMargin(root_child0, YGEdgeTop, 0); - root_child0->setMeasureFunc(_measure_100_100); + YGNodeSetMeasureFunc(root_child0, _measure_100_100); YGNodeInsertChild(root, root_child0, 0); const YGNodeRef root_child1 = YGNodeNewWithConfig(config); YGNodeStyleSetWidth(root_child1, 100); YGNodeStyleSetHeight(root_child1, 100); YGNodeStyleSetMargin(root_child1, YGEdgeTop, 100); - root_child1->setMeasureFunc(_measure_100_100); + YGNodeSetMeasureFunc(root_child1, _measure_100_100); YGNodeInsertChild(root, root_child1, 1); const YGNodeRef root_child2 = YGNodeNewWithConfig(config); YGNodeStyleSetWidth(root_child2, 100); YGNodeStyleSetHeight(root_child2, 100); YGNodeStyleSetMarginPercent(root_child2, YGEdgeTop, 10); - root_child2->setMeasureFunc(_measure_100_100); + YGNodeSetMeasureFunc(root_child2, _measure_100_100); YGNodeInsertChild(root, root_child2, 2); const YGNodeRef root_child3 = YGNodeNewWithConfig(config); YGNodeStyleSetWidth(root_child3, 100); YGNodeStyleSetHeight(root_child3, 100); YGNodeStyleSetMarginPercent(root_child3, YGEdgeTop, 20); - root_child3->setMeasureFunc(_measure_100_100); + YGNodeSetMeasureFunc(root_child3, _measure_100_100); YGNodeInsertChild(root, root_child3, 3); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); @@ -783,24 +782,24 @@ TEST(YogaTest, percent_padding_with_measure_func) { YGNodeStyleSetWidth(root_child0, 100); YGNodeStyleSetHeight(root_child0, 100); YGNodeStyleSetPadding(root_child0, YGEdgeTop, 0); - root_child0->setMeasureFunc(_measure_100_100); + YGNodeSetMeasureFunc(root_child0, _measure_100_100); YGNodeInsertChild(root, root_child0, 0); const YGNodeRef root_child1 = YGNodeNewWithConfig(config); YGNodeStyleSetWidth(root_child1, 100); YGNodeStyleSetHeight(root_child1, 100); YGNodeStyleSetPadding(root_child1, YGEdgeTop, 100); - root_child1->setMeasureFunc(_measure_100_100); + YGNodeSetMeasureFunc(root_child1, _measure_100_100); YGNodeInsertChild(root, root_child1, 1); const YGNodeRef root_child2 = YGNodeNewWithConfig(config); YGNodeStyleSetPaddingPercent(root_child2, YGEdgeTop, 10); - root_child2->setMeasureFunc(_measure_100_100); + YGNodeSetMeasureFunc(root_child2, _measure_100_100); YGNodeInsertChild(root, root_child2, 2); const YGNodeRef root_child3 = YGNodeNewWithConfig(config); YGNodeStyleSetPaddingPercent(root_child3, YGEdgeTop, 20); - root_child3->setMeasureFunc(_measure_100_100); + YGNodeSetMeasureFunc(root_child3, _measure_100_100); YGNodeInsertChild(root, root_child3, 3); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); @@ -849,26 +848,26 @@ TEST(YogaTest, percent_padding_and_percent_margin_with_measure_func) { YGNodeStyleSetWidth(root_child0, 100); YGNodeStyleSetHeight(root_child0, 100); YGNodeStyleSetPadding(root_child0, YGEdgeTop, 0); - root_child0->setMeasureFunc(_measure_100_100); + YGNodeSetMeasureFunc(root_child0, _measure_100_100); YGNodeInsertChild(root, root_child0, 0); const YGNodeRef root_child1 = YGNodeNewWithConfig(config); YGNodeStyleSetWidth(root_child1, 100); YGNodeStyleSetHeight(root_child1, 100); YGNodeStyleSetPadding(root_child1, YGEdgeTop, 100); - root_child1->setMeasureFunc(_measure_100_100); + YGNodeSetMeasureFunc(root_child1, _measure_100_100); YGNodeInsertChild(root, root_child1, 1); const YGNodeRef root_child2 = YGNodeNewWithConfig(config); YGNodeStyleSetPaddingPercent(root_child2, YGEdgeTop, 10); YGNodeStyleSetMarginPercent(root_child2, YGEdgeTop, 10); - root_child2->setMeasureFunc(_measure_100_100); + YGNodeSetMeasureFunc(root_child2, _measure_100_100); YGNodeInsertChild(root, root_child2, 2); const YGNodeRef root_child3 = YGNodeNewWithConfig(config); YGNodeStyleSetPaddingPercent(root_child3, YGEdgeTop, 20); YGNodeStyleSetMarginPercent(root_child3, YGEdgeTop, 20); - root_child3->setMeasureFunc(_measure_100_100); + YGNodeSetMeasureFunc(root_child3, _measure_100_100); YGNodeInsertChild(root, root_child3, 3); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); diff --git a/tests/YGNodeCallbackTest.cpp b/tests/YGNodeCallbackTest.cpp index 754c03c619..9a59497563 100644 --- a/tests/YGNodeCallbackTest.cpp +++ b/tests/YGNodeCallbackTest.cpp @@ -6,33 +6,33 @@ */ #include -#include +#include #include +using namespace facebook::yoga; + inline bool operator==(const YGSize& lhs, const YGSize& rhs) { return lhs.width == rhs.width && lhs.height == rhs.height; } -void PrintTo(const YGSize&, std::ostream*); - -TEST(YGNode, hasMeasureFunc_initial) { - auto n = YGNode{}; +TEST(Node, hasMeasureFunc_initial) { + auto n = Node{}; ASSERT_FALSE(n.hasMeasureFunc()); } -TEST(YGNode, hasMeasureFunc_with_measure_fn) { - auto n = YGNode{}; - n.setMeasureFunc([](YGNode*, float, YGMeasureMode, float, YGMeasureMode) { +TEST(Node, hasMeasureFunc_with_measure_fn) { + auto n = Node{}; + n.setMeasureFunc([](YGNodeRef, float, YGMeasureMode, float, YGMeasureMode) { return YGSize{}; }); ASSERT_TRUE(n.hasMeasureFunc()); } -TEST(YGNode, measure_with_measure_fn) { - auto n = YGNode{}; +TEST(Node, measure_with_measure_fn) { + auto n = Node{}; n.setMeasureFunc( - [](YGNode*, float w, YGMeasureMode wm, float h, YGMeasureMode hm) { + [](YGNodeRef, float w, YGMeasureMode wm, float h, YGMeasureMode hm) { return YGSize{w * static_cast(wm), h / static_cast(hm)}; }); @@ -41,10 +41,10 @@ TEST(YGNode, measure_with_measure_fn) { (YGSize{23, 12})); } -TEST(YGNode, measure_with_context_measure_fn) { - auto n = YGNode{}; +TEST(Node, measure_with_context_measure_fn) { + auto n = Node{}; n.setMeasureFunc( - [](YGNode*, float, YGMeasureMode, float, YGMeasureMode, void* ctx) { + [](YGNodeRef, float, YGMeasureMode, float, YGMeasureMode, void* ctx) { return *(YGSize*) ctx; }); @@ -54,14 +54,14 @@ TEST(YGNode, measure_with_context_measure_fn) { result); } -TEST(YGNode, switching_measure_fn_types) { - auto n = YGNode{}; +TEST(Node, switching_measure_fn_types) { + auto n = Node{}; n.setMeasureFunc( - [](YGNode*, float, YGMeasureMode, float, YGMeasureMode, void*) { + [](YGNodeRef, float, YGMeasureMode, float, YGMeasureMode, void*) { return YGSize{}; }); n.setMeasureFunc( - [](YGNode*, float w, YGMeasureMode wm, float h, YGMeasureMode hm) { + [](YGNodeRef, float w, YGMeasureMode wm, float h, YGMeasureMode hm) { return YGSize{w * static_cast(wm), h / static_cast(hm)}; }); @@ -70,9 +70,9 @@ TEST(YGNode, switching_measure_fn_types) { (YGSize{23, 12})); } -TEST(YGNode, hasMeasureFunc_after_unset) { - auto n = YGNode{}; - n.setMeasureFunc([](YGNode*, float, YGMeasureMode, float, YGMeasureMode) { +TEST(Node, hasMeasureFunc_after_unset) { + auto n = Node{}; + n.setMeasureFunc([](YGNodeRef, float, YGMeasureMode, float, YGMeasureMode) { return YGSize{}; }); @@ -80,10 +80,10 @@ TEST(YGNode, hasMeasureFunc_after_unset) { ASSERT_FALSE(n.hasMeasureFunc()); } -TEST(YGNode, hasMeasureFunc_after_unset_context) { - auto n = YGNode{}; +TEST(Node, hasMeasureFunc_after_unset_context) { + auto n = Node{}; n.setMeasureFunc( - [](YGNode*, float, YGMeasureMode, float, YGMeasureMode, void*) { + [](YGNodeRef, float, YGMeasureMode, float, YGMeasureMode, void*) { return YGSize{}; }); @@ -91,27 +91,27 @@ TEST(YGNode, hasMeasureFunc_after_unset_context) { ASSERT_FALSE(n.hasMeasureFunc()); } -TEST(YGNode, hasBaselineFunc_initial) { - auto n = YGNode{}; +TEST(Node, hasBaselineFunc_initial) { + auto n = Node{}; ASSERT_FALSE(n.hasBaselineFunc()); } -TEST(YGNode, hasBaselineFunc_with_baseline_fn) { - auto n = YGNode{}; - n.setBaselineFunc([](YGNode*, float, float) { return 0.0f; }); +TEST(Node, hasBaselineFunc_with_baseline_fn) { + auto n = Node{}; + n.setBaselineFunc([](YGNodeRef, float, float) { return 0.0f; }); ASSERT_TRUE(n.hasBaselineFunc()); } -TEST(YGNode, baseline_with_baseline_fn) { - auto n = YGNode{}; - n.setBaselineFunc([](YGNode*, float w, float h) { return w + h; }); +TEST(Node, baseline_with_baseline_fn) { + auto n = Node{}; + n.setBaselineFunc([](YGNodeRef, float w, float h) { return w + h; }); ASSERT_EQ(n.baseline(1.25f, 2.5f, nullptr), 3.75f); } -TEST(YGNode, baseline_with_context_baseline_fn) { - auto n = YGNode{}; - n.setBaselineFunc([](YGNode*, float w, float h, void* ctx) { +TEST(Node, baseline_with_context_baseline_fn) { + auto n = Node{}; + n.setBaselineFunc([](YGNodeRef, float w, float h, void* ctx) { return w + h + *(float*) ctx; }); @@ -119,29 +119,25 @@ TEST(YGNode, baseline_with_context_baseline_fn) { ASSERT_EQ(n.baseline(1.25f, 2.5f, &ctx), -6.25f); } -TEST(YGNode, hasBaselineFunc_after_unset) { - auto n = YGNode{}; - n.setBaselineFunc([](YGNode*, float, float) { return 0.0f; }); +TEST(Node, hasBaselineFunc_after_unset) { + auto n = Node{}; + n.setBaselineFunc([](YGNodeRef, float, float) { return 0.0f; }); n.setBaselineFunc(nullptr); ASSERT_FALSE(n.hasBaselineFunc()); } -TEST(YGNode, hasBaselineFunc_after_unset_context) { - auto n = YGNode{}; - n.setBaselineFunc([](YGNode*, float, float, void*) { return 0.0f; }); +TEST(Node, hasBaselineFunc_after_unset_context) { + auto n = Node{}; + n.setBaselineFunc([](YGNodeRef, float, float, void*) { return 0.0f; }); n.setMeasureFunc(nullptr); ASSERT_FALSE(n.hasMeasureFunc()); } -TEST(YGNode, switching_baseline_fn_types) { - auto n = YGNode{}; - n.setBaselineFunc([](YGNode*, float, float, void*) { return 0.0f; }); - n.setBaselineFunc([](YGNode*, float, float) { return 1.0f; }); +TEST(Node, switching_baseline_fn_types) { + auto n = Node{}; + n.setBaselineFunc([](YGNodeRef, float, float, void*) { return 0.0f; }); + n.setBaselineFunc([](YGNodeRef, float, float) { return 1.0f; }); ASSERT_EQ(n.baseline(1, 2, nullptr), 1.0f); } - -void PrintTo(const YGSize& size, std::ostream* os) { - *os << "YGSize{" << size.width << ", " << size.height << "}"; -} diff --git a/tests/YGPersistenceTest.cpp b/tests/YGPersistenceTest.cpp index 6bf1e7dac1..a34b6979ec 100644 --- a/tests/YGPersistenceTest.cpp +++ b/tests/YGPersistenceTest.cpp @@ -7,10 +7,11 @@ #include #include -#include +#include #include "util/TestUtil.h" +using namespace facebook; using facebook::yoga::test::TestUtil; TEST(YogaTest, cloning_shared_root) { @@ -273,9 +274,10 @@ TEST(YogaTest, mixed_shared_and_owned_children) { YGNodeInsertChild(root1, root1_child0, 0); YGNodeInsertChild(root1, root1_child2, 1); - auto children = root1->getChildren(); - children.insert(children.begin() + 1, root0_child0); - root1->setChildren(children); + auto children = static_cast(root1)->getChildren(); + children.insert(children.begin() + 1, static_cast(root0_child0)); + static_cast(root1)->setChildren(children); + auto secondChild = YGNodeGetChild(root1, 1); ASSERT_EQ(secondChild, YGNodeGetChild(root0, 0)); ASSERT_EQ(YGNodeGetChild(secondChild, 0), YGNodeGetChild(root0_child0, 0)); diff --git a/tests/YGRoundingMeasureFuncTest.cpp b/tests/YGRoundingMeasureFuncTest.cpp index 745377d0dc..c5c3a3ab6b 100644 --- a/tests/YGRoundingMeasureFuncTest.cpp +++ b/tests/YGRoundingMeasureFuncTest.cpp @@ -6,7 +6,6 @@ */ #include -#include #include static YGSize _measureFloor( @@ -50,7 +49,7 @@ TEST(YogaTest, rounding_feature_with_custom_measure_func_floor) { const YGNodeRef root = YGNodeNewWithConfig(config); const YGNodeRef root_child0 = YGNodeNewWithConfig(config); - root_child0->setMeasureFunc(_measureFloor); + YGNodeSetMeasureFunc(root_child0, _measureFloor); YGNodeInsertChild(root, root_child0, 0); YGConfigSetPointScaleFactor(config, 0.0f); @@ -98,7 +97,7 @@ TEST(YogaTest, rounding_feature_with_custom_measure_func_ceil) { const YGNodeRef root = YGNodeNewWithConfig(config); const YGNodeRef root_child0 = YGNodeNewWithConfig(config); - root_child0->setMeasureFunc(_measureCeil); + YGNodeSetMeasureFunc(root_child0, _measureCeil); YGNodeInsertChild(root, root_child0, 0); YGConfigSetPointScaleFactor(config, 1.0f); @@ -121,7 +120,7 @@ TEST( const YGNodeRef root_child0 = YGNodeNewWithConfig(config); YGNodeStyleSetPosition(root_child0, YGEdgeLeft, 73.625); - root_child0->setMeasureFunc(_measureFractial); + YGNodeSetMeasureFunc(root_child0, _measureFractial); YGNodeInsertChild(root, root_child0, 0); YGConfigSetPointScaleFactor(config, 2.0f); diff --git a/tests/YGStyleTest.cpp b/tests/YGStyleTest.cpp index 1204516784..5651dc31b3 100644 --- a/tests/YGStyleTest.cpp +++ b/tests/YGStyleTest.cpp @@ -6,16 +6,15 @@ */ #include -#include -#include +#include TEST(YogaTest, copy_style_same) { const YGNodeRef node0 = YGNodeNew(); const YGNodeRef node1 = YGNodeNew(); - ASSERT_FALSE(node0->isDirty()); + ASSERT_FALSE(YGNodeIsDirty(node0)); YGNodeCopyStyle(node0, node1); - ASSERT_FALSE(node0->isDirty()); + ASSERT_FALSE(YGNodeIsDirty(node0)); YGNodeFree(node0); YGNodeFree(node1); @@ -23,7 +22,7 @@ TEST(YogaTest, copy_style_same) { TEST(YogaTest, copy_style_modified) { const YGNodeRef node0 = YGNodeNew(); - ASSERT_FALSE(node0->isDirty()); + ASSERT_FALSE(YGNodeIsDirty(node0)); ASSERT_EQ(YGFlexDirectionColumn, YGNodeStyleGetFlexDirection(node0)); ASSERT_FALSE(YGNodeStyleGetMaxHeight(node0).unit != YGUnitUndefined); @@ -32,7 +31,7 @@ TEST(YogaTest, copy_style_modified) { YGNodeStyleSetMaxHeight(node1, 10); YGNodeCopyStyle(node0, node1); - ASSERT_TRUE(node0->isDirty()); + ASSERT_TRUE(YGNodeIsDirty(node0)); ASSERT_EQ(YGFlexDirectionRow, YGNodeStyleGetFlexDirection(node0)); ASSERT_FLOAT_EQ(10, YGNodeStyleGetMaxHeight(node0).value); @@ -45,14 +44,14 @@ TEST(YogaTest, copy_style_modified_same) { YGNodeStyleSetFlexDirection(node0, YGFlexDirectionRow); YGNodeStyleSetMaxHeight(node0, 10); YGNodeCalculateLayout(node0, YGUndefined, YGUndefined, YGDirectionLTR); - ASSERT_FALSE(node0->isDirty()); + ASSERT_FALSE(YGNodeIsDirty(node0)); const YGNodeRef node1 = YGNodeNew(); YGNodeStyleSetFlexDirection(node1, YGFlexDirectionRow); YGNodeStyleSetMaxHeight(node1, 10); YGNodeCopyStyle(node0, node1); - ASSERT_FALSE(node0->isDirty()); + ASSERT_FALSE(YGNodeIsDirty(node0)); YGNodeFree(node0); YGNodeFree(node1); diff --git a/tests/generated/YGConfigTest.cpp b/tests/generated/YGConfigTest.cpp index 7c0f61a31c..8665d50023 100644 --- a/tests/generated/YGConfigTest.cpp +++ b/tests/generated/YGConfigTest.cpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include @@ -22,7 +22,7 @@ struct ConfigCloningTest : public ::testing::Test { void SetUp() override; void TearDown() override; - static YGNode clonedNode; + static yoga::Node clonedNode; static YGNodeRef cloneNode(YGNodeRef, YGNodeRef, int) { return &clonedNode; } static YGNodeRef doNotClone(YGNodeRef, YGNodeRef, int) { return nullptr; } }; @@ -30,7 +30,7 @@ struct ConfigCloningTest : public ::testing::Test { TEST_F(ConfigCloningTest, uses_values_provided_by_cloning_callback) { config->setCloneNodeCallback(cloneNode); - YGNode node{}, owner{}; + yoga::Node node{}, owner{}; auto clone = config->cloneNode(&node, &owner, 0, nullptr); ASSERT_EQ(clone, &clonedNode); @@ -41,7 +41,7 @@ TEST_F( falls_back_to_regular_cloning_if_callback_returns_null) { config->setCloneNodeCallback(doNotClone); - YGNode node{}, owner{}; + yoga::Node node{}, owner{}; auto clone = config->cloneNode(&node, &owner, 0, nullptr); ASSERT_NE(clone, nullptr); @@ -53,7 +53,7 @@ TEST_F(ConfigCloningTest, can_clone_with_context) { return (YGNodeRef) context; }); - YGNode node{}, owner{}, clone{}; + yoga::Node node{}, owner{}, clone{}; ASSERT_EQ(config->cloneNode(&node, &owner, 0, &clone), &clone); } @@ -65,4 +65,4 @@ void ConfigCloningTest::TearDown() { config.reset(); } -YGNode ConfigCloningTest::clonedNode = {}; +yoga::Node ConfigCloningTest::clonedNode = {}; diff --git a/tests/util/TestUtil.cpp b/tests/util/TestUtil.cpp index 2ae34e0f84..16e3fd6a58 100644 --- a/tests/util/TestUtil.cpp +++ b/tests/util/TestUtil.cpp @@ -7,7 +7,7 @@ #include "TestUtil.h" -#include +#include #include namespace facebook::yoga::test { @@ -17,7 +17,7 @@ int nodeInstanceCount = 0; namespace { void yogaEventSubscriber( - const YGNode& /*node*/, + YGNodeConstRef /*node*/, Event::Type eventType, const Event::Data& /*eventData*/) { diff --git a/yoga/Utils.h b/yoga/Utils.h index 78b44de693..bd8ab30922 100644 --- a/yoga/Utils.h +++ b/yoga/Utils.h @@ -7,7 +7,7 @@ #pragma once -#include "YGNode.h" +#include #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/yoga/YGNodePrint.cpp b/yoga/YGNodePrint.cpp index c1205abbdc..d76aa26fb9 100644 --- a/yoga/YGNodePrint.cpp +++ b/yoga/YGNodePrint.cpp @@ -12,9 +12,8 @@ #include #include "YGNodePrint.h" -#include "YGNode.h" #include -#include "Utils.h" +#include namespace facebook::yoga { typedef std::string string; @@ -92,7 +91,7 @@ static void appendEdges( const string& key, const Style::Edges& edges) { if (areFourValuesEqual(edges)) { - auto edgeValue = YGNode::computeEdgeValueForColumn( + auto edgeValue = yoga::Node::computeEdgeValueForColumn( edges, YGEdgeLeft, CompactValue::ofZero()); appendNumberIfNotZero(base, key, edgeValue); } else { @@ -110,16 +109,16 @@ static void appendEdgeIfNotUndefined( const YGEdge edge) { // TODO: this doesn't take RTL / YGEdgeStart / YGEdgeEnd into account auto value = (edge == YGEdgeLeft || edge == YGEdgeRight) - ? YGNode::computeEdgeValueForRow( + ? yoga::Node::computeEdgeValueForRow( edges, edge, edge, CompactValue::ofUndefined()) - : YGNode::computeEdgeValueForColumn( + : yoga::Node::computeEdgeValueForColumn( edges, edge, CompactValue::ofUndefined()); appendNumberIfNotUndefined(base, str, value); } void YGNodeToString( std::string& str, - YGNodeRef node, + yoga::Node* node, YGPrintOptions options, uint32_t level) { indent(str, level); @@ -141,27 +140,27 @@ void YGNodeToString( if (options & YGPrintOptionsStyle) { appendFormattedString(str, "style=\""); const auto& style = node->getStyle(); - if (style.flexDirection() != YGNode().getStyle().flexDirection()) { + if (style.flexDirection() != yoga::Node{}.getStyle().flexDirection()) { appendFormattedString( str, "flex-direction: %s; ", YGFlexDirectionToString(style.flexDirection())); } - if (style.justifyContent() != YGNode().getStyle().justifyContent()) { + if (style.justifyContent() != yoga::Node{}.getStyle().justifyContent()) { appendFormattedString( str, "justify-content: %s; ", YGJustifyToString(style.justifyContent())); } - if (style.alignItems() != YGNode().getStyle().alignItems()) { + if (style.alignItems() != yoga::Node{}.getStyle().alignItems()) { appendFormattedString( str, "align-items: %s; ", YGAlignToString(style.alignItems())); } - if (style.alignContent() != YGNode().getStyle().alignContent()) { + if (style.alignContent() != yoga::Node{}.getStyle().alignContent()) { appendFormattedString( str, "align-content: %s; ", YGAlignToString(style.alignContent())); } - if (style.alignSelf() != YGNode().getStyle().alignSelf()) { + if (style.alignSelf() != yoga::Node{}.getStyle().alignSelf()) { appendFormattedString( str, "align-self: %s; ", YGAlignToString(style.alignSelf())); } @@ -170,17 +169,17 @@ void YGNodeToString( appendNumberIfNotAuto(str, "flex-basis", style.flexBasis()); appendFloatOptionalIfDefined(str, "flex", style.flex()); - if (style.flexWrap() != YGNode().getStyle().flexWrap()) { + if (style.flexWrap() != yoga::Node{}.getStyle().flexWrap()) { appendFormattedString( str, "flex-wrap: %s; ", YGWrapToString(style.flexWrap())); } - if (style.overflow() != YGNode().getStyle().overflow()) { + if (style.overflow() != yoga::Node{}.getStyle().overflow()) { appendFormattedString( str, "overflow: %s; ", YGOverflowToString(style.overflow())); } - if (style.display() != YGNode().getStyle().display()) { + if (style.display() != yoga::Node{}.getStyle().display()) { appendFormattedString( str, "display: %s; ", YGDisplayToString(style.display())); } @@ -188,15 +187,16 @@ void YGNodeToString( appendEdges(str, "padding", style.padding()); appendEdges(str, "border", style.border()); - if (YGNode::computeColumnGap(style.gap(), CompactValue::ofUndefined()) != - YGNode::computeColumnGap( - YGNode().getStyle().gap(), CompactValue::ofUndefined())) { + if (yoga::Node::computeColumnGap( + style.gap(), CompactValue::ofUndefined()) != + yoga::Node::computeColumnGap( + yoga::Node{}.getStyle().gap(), CompactValue::ofUndefined())) { appendNumberIfNotUndefined( str, "column-gap", style.gap()[YGGutterColumn]); } - if (YGNode::computeRowGap(style.gap(), CompactValue::ofUndefined()) != - YGNode::computeRowGap( - YGNode().getStyle().gap(), CompactValue::ofUndefined())) { + if (yoga::Node::computeRowGap(style.gap(), CompactValue::ofUndefined()) != + yoga::Node::computeRowGap( + yoga::Node{}.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() != YGNode().getStyle().positionType()) { + if (style.positionType() != yoga::Node{}.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, YGNodeGetChild(node, i), options, level + 1); + YGNodeToString(str, node->getChild(i), options, level + 1); } appendFormattedString(str, "\n"); indent(str, level); diff --git a/yoga/YGNodePrint.h b/yoga/YGNodePrint.h index 7648f83c7b..b991c41a7f 100644 --- a/yoga/YGNodePrint.h +++ b/yoga/YGNodePrint.h @@ -12,12 +12,13 @@ #include #include +#include namespace facebook::yoga { void YGNodeToString( std::string& str, - YGNodeRef node, + yoga::Node* node, YGPrintOptions options, uint32_t level); diff --git a/yoga/Yoga-internal.h b/yoga/Yoga-internal.h index d4e6df63e5..3fc4f23ac3 100644 --- a/yoga/Yoga-internal.h +++ b/yoga/Yoga-internal.h @@ -16,8 +16,6 @@ #include -using YGVector = std::vector; - YG_EXTERN_C_BEGIN void YGNodeCalculateLayoutWithContext( diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index b52ded0e08..eb84c47a40 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -14,8 +14,8 @@ #include #include "log.h" -#include "Utils.h" -#include "YGNode.h" +#include +#include #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 node->getContext(); + return static_cast(node)->getContext(); } YOGA_EXPORT void YGNodeSetContext(YGNodeRef node, void* context) { - return node->setContext(context); + return static_cast(node)->setContext(context); } YOGA_EXPORT YGConfigRef YGNodeGetConfig(YGNodeRef node) { - return node->getConfig(); + return static_cast(node)->getConfig(); } YOGA_EXPORT void YGNodeSetConfig(YGNodeRef node, YGConfigRef config) { - node->setConfig(static_cast(config)); + static_cast(node)->setConfig(static_cast(config)); } YOGA_EXPORT bool YGNodeHasMeasureFunc(YGNodeRef node) { - return node->hasMeasureFunc(); + return static_cast(node)->hasMeasureFunc(); } YOGA_EXPORT void YGNodeSetMeasureFunc( YGNodeRef node, YGMeasureFunc measureFunc) { - node->setMeasureFunc(measureFunc); + static_cast(node)->setMeasureFunc(measureFunc); } YOGA_EXPORT bool YGNodeHasBaselineFunc(YGNodeRef node) { - return node->hasBaselineFunc(); + return static_cast(node)->hasBaselineFunc(); } YOGA_EXPORT void YGNodeSetBaselineFunc( YGNodeRef node, YGBaselineFunc baselineFunc) { - node->setBaselineFunc(baselineFunc); + static_cast(node)->setBaselineFunc(baselineFunc); } YOGA_EXPORT YGDirtiedFunc YGNodeGetDirtiedFunc(YGNodeRef node) { - return node->getDirtied(); + return static_cast(node)->getDirtied(); } YOGA_EXPORT void YGNodeSetDirtiedFunc( YGNodeRef node, YGDirtiedFunc dirtiedFunc) { - node->setDirtiedFunc(dirtiedFunc); + static_cast(node)->setDirtiedFunc(dirtiedFunc); } YOGA_EXPORT void YGNodeSetPrintFunc(YGNodeRef node, YGPrintFunc printFunc) { - node->setPrintFunc(printFunc); + static_cast(node)->setPrintFunc(printFunc); } YOGA_EXPORT bool YGNodeGetHasNewLayout(YGNodeRef node) { - return node->getHasNewLayout(); + return static_cast(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) { - node->setHasNewLayout(hasNewLayout); + static_cast(node)->setHasNewLayout(hasNewLayout); } YOGA_EXPORT YGNodeType YGNodeGetNodeType(YGNodeRef node) { - return node->getNodeType(); + return static_cast(node)->getNodeType(); } YOGA_EXPORT void YGNodeSetNodeType(YGNodeRef node, YGNodeType nodeType) { - return node->setNodeType(nodeType); + return static_cast(node)->setNodeType(nodeType); } YOGA_EXPORT bool YGNodeIsDirty(YGNodeRef node) { - return node->isDirty(); + return static_cast(node)->isDirty(); } YOGA_EXPORT void YGNodeMarkDirtyAndPropagateToDescendants( const YGNodeRef node) { - return node->markDirtyAndPropagateDownwards(); + return static_cast(node)->markDirtyAndPropagateDownwards(); } int32_t gConfigInstanceCount = 0; YOGA_EXPORT WIN_EXPORT YGNodeRef YGNodeNewWithConfig(const YGConfigRef config) { - const YGNodeRef node = new YGNode{static_cast(config)}; + auto* node = new yoga::Node{static_cast(config)}; YGAssert(config != nullptr, "Tried to construct YGNode with null config"); YGAssertWithConfig( config, node != nullptr, "Could not allocate memory for node"); @@ -207,8 +207,9 @@ YOGA_EXPORT YGNodeRef YGNodeNew(void) { return YGNodeNewWithConfig(YGConfigGetDefault()); } -YOGA_EXPORT YGNodeRef YGNodeClone(YGNodeRef oldNode) { - YGNodeRef node = new YGNode(*oldNode); +YOGA_EXPORT YGNodeRef YGNodeClone(YGNodeRef oldNodeRef) { + auto oldNode = static_cast(oldNodeRef); + auto node = new yoga::Node(*oldNode); YGAssertWithConfig( oldNode->getConfig(), node != nullptr, @@ -218,15 +219,17 @@ YOGA_EXPORT YGNodeRef YGNodeClone(YGNodeRef oldNode) { return node; } -YOGA_EXPORT void YGNodeFree(const YGNodeRef node) { - if (YGNodeRef owner = node->getOwner()) { +YOGA_EXPORT void YGNodeFree(const YGNodeRef nodeRef) { + auto node = static_cast(nodeRef); + + if (auto owner = node->getOwner()) { owner->removeChild(node); node->setOwner(nullptr); } const uint32_t childCount = YGNodeGetChildCount(node); for (uint32_t i = 0; i < childCount; i++) { - const YGNodeRef child = YGNodeGetChild(node, i); + auto child = node->getChild(i); child->setOwner(nullptr); } @@ -235,16 +238,18 @@ YOGA_EXPORT void YGNodeFree(const YGNodeRef node) { } YOGA_EXPORT void YGNodeDeallocate(const YGNodeRef node) { - Event::publish(node, {node->getConfig()}); - delete node; + Event::publish(node, {YGNodeGetConfig(node)}); + delete static_cast(node); } YOGA_EXPORT void YGNodeFreeRecursiveWithCleanupFunc( - const YGNodeRef root, + const YGNodeRef rootRef, YGNodeCleanupFunc cleanup) { + const auto root = static_cast(rootRef); + uint32_t skipped = 0; while (YGNodeGetChildCount(root) > skipped) { - const YGNodeRef child = YGNodeGetChild(root, skipped); + const auto child = root->getChild(skipped); if (child->getOwner() != root) { // Don't free shared nodes that we don't own. skipped += 1; @@ -264,7 +269,7 @@ YOGA_EXPORT void YGNodeFreeRecursive(const YGNodeRef root) { } YOGA_EXPORT void YGNodeReset(YGNodeRef node) { - node->reset(); + static_cast(node)->reset(); } YOGA_EXPORT int32_t YGConfigGetInstanceCount(void) { @@ -287,8 +292,9 @@ YOGA_EXPORT void YGConfigFree(const YGConfigRef config) { } YOGA_EXPORT void YGNodeSetIsReferenceBaseline( - YGNodeRef node, + YGNodeRef nodeRef, bool isReferenceBaseline) { + auto node = static_cast(nodeRef); if (node->isReferenceBaseline() != isReferenceBaseline) { node->setIsReferenceBaseline(isReferenceBaseline); node->markDirtyAndPropagate(); @@ -296,13 +302,16 @@ YOGA_EXPORT void YGNodeSetIsReferenceBaseline( } YOGA_EXPORT bool YGNodeIsReferenceBaseline(YGNodeRef node) { - return node->isReferenceBaseline(); + return static_cast(node)->isReferenceBaseline(); } YOGA_EXPORT void YGNodeInsertChild( - const YGNodeRef owner, - const YGNodeRef child, + const YGNodeRef ownerRef, + const YGNodeRef childRef, const uint32_t index) { + auto owner = static_cast(ownerRef); + auto child = static_cast(childRef); + YGAssertWithNode( owner, child->getOwner() == nullptr, @@ -319,16 +328,22 @@ YOGA_EXPORT void YGNodeInsertChild( } YOGA_EXPORT void YGNodeSwapChild( - const YGNodeRef owner, - const YGNodeRef child, + const YGNodeRef ownerRef, + const YGNodeRef childRef, 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 owner, - const YGNodeRef excludedChild) { + const YGNodeRef ownerRef, + const YGNodeRef excludedChildRef) { + auto owner = static_cast(ownerRef); + auto excludedChild = static_cast(excludedChildRef); + if (YGNodeGetChildCount(owner) == 0) { // This is an empty set. Nothing to remove. return; @@ -347,19 +362,21 @@ YOGA_EXPORT void YGNodeRemoveChild( } } -YOGA_EXPORT void YGNodeRemoveAllChildren(const YGNodeRef owner) { +YOGA_EXPORT void YGNodeRemoveAllChildren(const YGNodeRef ownerRef) { + auto owner = static_cast(ownerRef); + const uint32_t childCount = YGNodeGetChildCount(owner); if (childCount == 0) { // This is an empty set already. Nothing to do. return; } - const YGNodeRef firstChild = YGNodeGetChild(owner, 0); + auto* firstChild = owner->getChild(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++) { - const YGNodeRef oldChild = YGNodeGetChild(owner, i); - oldChild->setLayout(YGNode().getLayout()); // layout is no longer valid + yoga::Node* oldChild = owner->getChild(i); + oldChild->setLayout({}); // layout is no longer valid oldChild->setOwner(nullptr); } owner->clearChildren(); @@ -368,42 +385,45 @@ YOGA_EXPORT void YGNodeRemoveAllChildren(const YGNodeRef owner) { } // Otherwise, we are not the owner of the child set. We don't have to do // anything to clear it. - owner->setChildren(YGVector()); + owner->setChildren({}); owner->markDirtyAndPropagate(); } YOGA_EXPORT void YGNodeSetChildren( - const YGNodeRef owner, - const YGNodeRef* children, + const YGNodeRef ownerRef, + const YGNodeRef* childrenRefs, const uint32_t count) { + auto owner = static_cast(ownerRef); + auto children = reinterpret_cast(childrenRefs); + if (!owner) { return; } - const YGVector childrenVector = {children, children + count}; + const std::vector childrenVector = {children, children + count}; if (childrenVector.size() == 0) { if (YGNodeGetChildCount(owner) > 0) { - for (YGNodeRef const child : owner->getChildren()) { - child->setLayout(YGLayout()); + for (auto* child : owner->getChildren()) { + child->setLayout({}); child->setOwner(nullptr); } - owner->setChildren(YGVector()); + owner->setChildren({}); owner->markDirtyAndPropagate(); } } else { if (YGNodeGetChildCount(owner) > 0) { - for (YGNodeRef const oldChild : owner->getChildren()) { + for (auto* 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(YGLayout()); + oldChild->setLayout({}); oldChild->setOwner(nullptr); } } } owner->setChildren(childrenVector); - for (YGNodeRef child : childrenVector) { + for (yoga::Node* child : childrenVector) { child->setOwner(owner); } owner->markDirtyAndPropagate(); @@ -411,26 +431,31 @@ YOGA_EXPORT void YGNodeSetChildren( } YOGA_EXPORT YGNodeRef -YGNodeGetChild(const YGNodeRef node, const uint32_t index) { +YGNodeGetChild(const YGNodeRef nodeRef, const uint32_t index) { + auto node = static_cast(nodeRef); + if (index < node->getChildren().size()) { return node->getChild(index); } return nullptr; } -YOGA_EXPORT uint32_t YGNodeGetChildCount(const YGNodeRef node) { - return static_cast(node->getChildren().size()); +YOGA_EXPORT uint32_t YGNodeGetChildCount(const YGNodeConstRef node) { + return static_cast( + static_cast(node)->getChildren().size()); } YOGA_EXPORT YGNodeRef YGNodeGetOwner(const YGNodeRef node) { - return node->getOwner(); + return static_cast(node)->getOwner(); } YOGA_EXPORT YGNodeRef YGNodeGetParent(const YGNodeRef node) { - return node->getOwner(); + return static_cast(node)->getOwner(); } -YOGA_EXPORT void YGNodeMarkDirty(const YGNodeRef node) { +YOGA_EXPORT void YGNodeMarkDirty(const YGNodeRef nodeRef) { + auto node = static_cast(nodeRef); + YGAssertWithNode( node, node->hasMeasureFunc(), @@ -441,21 +466,26 @@ YOGA_EXPORT void YGNodeMarkDirty(const YGNodeRef node) { } YOGA_EXPORT void YGNodeCopyStyle( - const YGNodeRef dstNode, - const YGNodeRef srcNode) { + const YGNodeRef dstNodeRef, + const YGNodeRef srcNodeRef) { + auto dstNode = static_cast(dstNodeRef); + auto srcNode = static_cast(srcNodeRef); + if (!(dstNode->getStyle() == srcNode->getStyle())) { dstNode->setStyle(srcNode->getStyle()); dstNode->markDirtyAndPropagate(); } } -YOGA_EXPORT float YGNodeStyleGetFlexGrow(const YGNodeConstRef node) { +YOGA_EXPORT float YGNodeStyleGetFlexGrow(const YGNodeConstRef nodeRef) { + auto node = static_cast(nodeRef); return node->getStyle().flexGrow().isUndefined() ? kDefaultFlexGrow : node->getStyle().flexGrow().unwrap(); } -YOGA_EXPORT float YGNodeStyleGetFlexShrink(const YGNodeConstRef node) { +YOGA_EXPORT float YGNodeStyleGetFlexShrink(const YGNodeConstRef nodeRef) { + auto node = static_cast(nodeRef); return node->getStyle().flexShrink().isUndefined() ? (node->getConfig()->useWebDefaults() ? kWebDefaultFlexShrink : kDefaultFlexShrink) @@ -466,7 +496,7 @@ namespace { template void updateStyle( - YGNode* node, + yoga::Node* node, T value, NeedsUpdate&& needsUpdate, Update&& update) { @@ -477,9 +507,9 @@ void updateStyle( } template -void updateStyle(YGNode* node, Ref (Style::*prop)(), T value) { +void updateStyle(YGNodeRef node, Ref (Style::*prop)(), T value) { updateStyle( - node, + static_cast(node), value, [prop](Style& s, T x) { return (s.*prop)() != x; }, [prop](Style& s, T x) { (s.*prop)() = x; }); @@ -487,12 +517,12 @@ void updateStyle(YGNode* node, Ref (Style::*prop)(), T value) { template void updateIndexedStyleProp( - YGNode* node, + YGNodeRef node, Ref (Style::*prop)(), Idx idx, CompactValue value) { updateStyle( - node, + static_cast(node), value, [idx, prop](Style& s, CompactValue x) { return (s.*prop)()[idx] != x; }, [idx, prop](Style& s, CompactValue x) { (s.*prop)()[idx] = x; }); @@ -513,7 +543,7 @@ YOGA_EXPORT void YGNodeStyleSetDirection( updateStyle(node, &Style::direction, value); } YOGA_EXPORT YGDirection YGNodeStyleGetDirection(const YGNodeConstRef node) { - return node->getStyle().direction(); + return static_cast(node)->getStyle().direction(); } YOGA_EXPORT void YGNodeStyleSetFlexDirection( @@ -524,7 +554,7 @@ YOGA_EXPORT void YGNodeStyleSetFlexDirection( } YOGA_EXPORT YGFlexDirection YGNodeStyleGetFlexDirection(const YGNodeConstRef node) { - return node->getStyle().flexDirection(); + return static_cast(node)->getStyle().flexDirection(); } YOGA_EXPORT void YGNodeStyleSetJustifyContent( @@ -534,7 +564,7 @@ YOGA_EXPORT void YGNodeStyleSetJustifyContent( node, &Style::justifyContent, justifyContent); } YOGA_EXPORT YGJustify YGNodeStyleGetJustifyContent(const YGNodeConstRef node) { - return node->getStyle().justifyContent(); + return static_cast(node)->getStyle().justifyContent(); } YOGA_EXPORT void YGNodeStyleSetAlignContent( @@ -544,7 +574,7 @@ YOGA_EXPORT void YGNodeStyleSetAlignContent( node, &Style::alignContent, alignContent); } YOGA_EXPORT YGAlign YGNodeStyleGetAlignContent(const YGNodeConstRef node) { - return node->getStyle().alignContent(); + return static_cast(node)->getStyle().alignContent(); } YOGA_EXPORT void YGNodeStyleSetAlignItems( @@ -553,7 +583,7 @@ YOGA_EXPORT void YGNodeStyleSetAlignItems( updateStyle(node, &Style::alignItems, alignItems); } YOGA_EXPORT YGAlign YGNodeStyleGetAlignItems(const YGNodeConstRef node) { - return node->getStyle().alignItems(); + return static_cast(node)->getStyle().alignItems(); } YOGA_EXPORT void YGNodeStyleSetAlignSelf( @@ -562,7 +592,7 @@ YOGA_EXPORT void YGNodeStyleSetAlignSelf( updateStyle(node, &Style::alignSelf, alignSelf); } YOGA_EXPORT YGAlign YGNodeStyleGetAlignSelf(const YGNodeConstRef node) { - return node->getStyle().alignSelf(); + return static_cast(node)->getStyle().alignSelf(); } YOGA_EXPORT void YGNodeStyleSetPositionType( @@ -573,7 +603,7 @@ YOGA_EXPORT void YGNodeStyleSetPositionType( } YOGA_EXPORT YGPositionType YGNodeStyleGetPositionType(const YGNodeConstRef node) { - return node->getStyle().positionType(); + return static_cast(node)->getStyle().positionType(); } YOGA_EXPORT void YGNodeStyleSetFlexWrap( @@ -582,7 +612,7 @@ YOGA_EXPORT void YGNodeStyleSetFlexWrap( updateStyle(node, &Style::flexWrap, flexWrap); } YOGA_EXPORT YGWrap YGNodeStyleGetFlexWrap(const YGNodeConstRef node) { - return node->getStyle().flexWrap(); + return static_cast(node)->getStyle().flexWrap(); } YOGA_EXPORT void YGNodeStyleSetOverflow( @@ -591,7 +621,7 @@ YOGA_EXPORT void YGNodeStyleSetOverflow( updateStyle(node, &Style::overflow, overflow); } YOGA_EXPORT YGOverflow YGNodeStyleGetOverflow(const YGNodeConstRef node) { - return node->getStyle().overflow(); + return static_cast(node)->getStyle().overflow(); } YOGA_EXPORT void YGNodeStyleSetDisplay( @@ -600,7 +630,7 @@ YOGA_EXPORT void YGNodeStyleSetDisplay( updateStyle(node, &Style::display, display); } YOGA_EXPORT YGDisplay YGNodeStyleGetDisplay(const YGNodeConstRef node) { - return node->getStyle().display(); + return static_cast(node)->getStyle().display(); } // TODO(T26792433): Change the API to accept YGFloatOptional. @@ -609,7 +639,8 @@ YOGA_EXPORT void YGNodeStyleSetFlex(const YGNodeRef node, const float flex) { } // TODO(T26792433): Change the API to accept YGFloatOptional. -YOGA_EXPORT float YGNodeStyleGetFlex(const YGNodeConstRef node) { +YOGA_EXPORT float YGNodeStyleGetFlex(const YGNodeConstRef nodeRef) { + auto node = static_cast(nodeRef); return node->getStyle().flex().isUndefined() ? YGUndefined : node->getStyle().flex().unwrap(); @@ -632,7 +663,8 @@ YOGA_EXPORT void YGNodeStyleSetFlexShrink( } YOGA_EXPORT YGValue YGNodeStyleGetFlexBasis(const YGNodeConstRef node) { - YGValue flexBasis = node->getStyle().flexBasis(); + YGValue flexBasis = + static_cast(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; @@ -676,7 +708,7 @@ YOGA_EXPORT void YGNodeStyleSetPositionPercent( node, &Style::position, edge, value); } YOGA_EXPORT YGValue YGNodeStyleGetPosition(YGNodeConstRef node, YGEdge edge) { - return node->getStyle().position()[edge]; + return static_cast(node)->getStyle().position()[edge]; } YOGA_EXPORT void YGNodeStyleSetMargin( @@ -698,7 +730,7 @@ YOGA_EXPORT void YGNodeStyleSetMarginAuto(YGNodeRef node, YGEdge edge) { node, &Style::margin, edge, CompactValue::ofAuto()); } YOGA_EXPORT YGValue YGNodeStyleGetMargin(YGNodeConstRef node, YGEdge edge) { - return node->getStyle().margin()[edge]; + return static_cast(node)->getStyle().margin()[edge]; } YOGA_EXPORT void YGNodeStyleSetPadding( @@ -718,7 +750,7 @@ YOGA_EXPORT void YGNodeStyleSetPaddingPercent( node, &Style::padding, edge, value); } YOGA_EXPORT YGValue YGNodeStyleGetPadding(YGNodeConstRef node, YGEdge edge) { - return node->getStyle().padding()[edge]; + return static_cast(node)->getStyle().padding()[edge]; } // TODO(T26792433): Change the API to accept YGFloatOptional. @@ -733,7 +765,7 @@ YOGA_EXPORT void YGNodeStyleSetBorder( YOGA_EXPORT float YGNodeStyleGetBorder( const YGNodeConstRef node, const YGEdge edge) { - auto border = node->getStyle().border()[edge]; + auto border = static_cast(node)->getStyle().border()[edge]; if (border.isUndefined() || border.isAuto()) { // TODO(T26792433): Rather than returning YGUndefined, change the api to // return YGFloatOptional. @@ -754,7 +786,8 @@ YOGA_EXPORT void YGNodeStyleSetGap( YOGA_EXPORT float YGNodeStyleGetGap( const YGNodeConstRef node, const YGGutter gutter) { - auto gapLength = node->getStyle().gap()[gutter]; + auto gapLength = + static_cast(node)->getStyle().gap()[gutter]; if (gapLength.isUndefined() || gapLength.isAuto()) { // TODO(T26792433): Rather than returning YGUndefined, change the api to // return YGFloatOptional. @@ -768,7 +801,8 @@ YOGA_EXPORT float YGNodeStyleGetGap( // TODO(T26792433): Change the API to accept YGFloatOptional. YOGA_EXPORT float YGNodeStyleGetAspectRatio(const YGNodeConstRef node) { - const YGFloatOptional op = node->getStyle().aspectRatio(); + const YGFloatOptional op = + static_cast(node)->getStyle().aspectRatio(); return op.isUndefined() ? YGUndefined : op.unwrap(); } @@ -795,7 +829,9 @@ YOGA_EXPORT void YGNodeStyleSetWidthAuto(YGNodeRef node) { node, &Style::dimensions, YGDimensionWidth, CompactValue::ofAuto()); } YOGA_EXPORT YGValue YGNodeStyleGetWidth(YGNodeConstRef node) { - return node->getStyle().dimensions()[YGDimensionWidth]; + return static_cast(node) + ->getStyle() + .dimensions()[YGDimensionWidth]; } YOGA_EXPORT void YGNodeStyleSetHeight(YGNodeRef node, float points) { @@ -813,7 +849,9 @@ YOGA_EXPORT void YGNodeStyleSetHeightAuto(YGNodeRef node) { node, &Style::dimensions, YGDimensionHeight, CompactValue::ofAuto()); } YOGA_EXPORT YGValue YGNodeStyleGetHeight(YGNodeConstRef node) { - return node->getStyle().dimensions()[YGDimensionHeight]; + return static_cast(node) + ->getStyle() + .dimensions()[YGDimensionHeight]; } YOGA_EXPORT void YGNodeStyleSetMinWidth( @@ -831,7 +869,9 @@ YOGA_EXPORT void YGNodeStyleSetMinWidthPercent( node, &Style::minDimensions, YGDimensionWidth, value); } YOGA_EXPORT YGValue YGNodeStyleGetMinWidth(const YGNodeConstRef node) { - return node->getStyle().minDimensions()[YGDimensionWidth]; + return static_cast(node) + ->getStyle() + .minDimensions()[YGDimensionWidth]; } YOGA_EXPORT void YGNodeStyleSetMinHeight( @@ -849,7 +889,9 @@ YOGA_EXPORT void YGNodeStyleSetMinHeightPercent( node, &Style::minDimensions, YGDimensionHeight, value); } YOGA_EXPORT YGValue YGNodeStyleGetMinHeight(const YGNodeConstRef node) { - return node->getStyle().minDimensions()[YGDimensionHeight]; + return static_cast(node) + ->getStyle() + .minDimensions()[YGDimensionHeight]; } YOGA_EXPORT void YGNodeStyleSetMaxWidth( @@ -867,7 +909,9 @@ YOGA_EXPORT void YGNodeStyleSetMaxWidthPercent( node, &Style::maxDimensions, YGDimensionWidth, value); } YOGA_EXPORT YGValue YGNodeStyleGetMaxWidth(const YGNodeConstRef node) { - return node->getStyle().maxDimensions()[YGDimensionWidth]; + return static_cast(node) + ->getStyle() + .maxDimensions()[YGDimensionWidth]; } YOGA_EXPORT void YGNodeStyleSetMaxHeight( @@ -885,17 +929,20 @@ YOGA_EXPORT void YGNodeStyleSetMaxHeightPercent( node, &Style::maxDimensions, YGDimensionHeight, value); } YOGA_EXPORT YGValue YGNodeStyleGetMaxHeight(const YGNodeConstRef node) { - return node->getStyle().maxDimensions()[YGDimensionHeight]; + return static_cast(node) + ->getStyle() + .maxDimensions()[YGDimensionHeight]; } -#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_PROPERTY_IMPL(type, name, instanceName) \ + YOGA_EXPORT type YGNodeLayoutGet##name(const YGNodeRef node) { \ + return static_cast(node)->getLayout().instanceName; \ } #define YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(type, name, instanceName) \ YOGA_EXPORT type YGNodeLayoutGet##name( \ - const YGNodeRef node, const YGEdge edge) { \ + const YGNodeRef nodeRef, const YGEdge edge) { \ + auto node = static_cast(nodeRef); \ YGAssertWithNode( \ node, \ edge <= YGEdgeEnd, \ @@ -936,7 +983,7 @@ YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Padding, padding) std::atomic gCurrentGenerationCount(0); bool YGLayoutNodeInternal( - const YGNodeRef node, + yoga::Node* const node, const float availableWidth, const float availableHeight, const YGDirection ownerDirection, @@ -953,19 +1000,14 @@ bool YGLayoutNodeInternal( const uint32_t generationCount); #ifdef DEBUG -static void YGNodePrintInternal( - const YGNodeRef node, +YOGA_EXPORT void YGNodePrint( + const YGNodeRef nodeRef, 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 = { @@ -984,7 +1026,7 @@ static const std::array dim = { {YGDimensionHeight, YGDimensionHeight, YGDimensionWidth, YGDimensionWidth}}; static inline float YGNodePaddingAndBorderForAxis( - const YGNodeConstRef node, + const yoga::Node* const node, const YGFlexDirection axis, const float widthSize) { return (node->getLeadingPaddingAndBorder(axis, widthSize) + @@ -992,7 +1034,9 @@ static inline float YGNodePaddingAndBorderForAxis( .unwrap(); } -static inline YGAlign YGNodeAlignItem(const YGNode* node, const YGNode* child) { +static inline YGAlign YGNodeAlignItem( + const yoga::Node* node, + const yoga::Node* child) { const YGAlign align = child->getStyle().alignSelf() == YGAlignAuto ? node->getStyle().alignItems() : child->getStyle().alignSelf(); @@ -1003,7 +1047,7 @@ static inline YGAlign YGNodeAlignItem(const YGNode* node, const YGNode* child) { return align; } -static float YGBaseline(const YGNodeRef node, void* layoutContext) { +static float YGBaseline(yoga::Node* node, void* layoutContext) { if (node->hasBaselineFunc()) { Event::publish(node); @@ -1022,10 +1066,10 @@ static float YGBaseline(const YGNodeRef node, void* layoutContext) { return baseline; } - YGNodeRef baselineChild = nullptr; + yoga::Node* baselineChild = nullptr; const uint32_t childCount = YGNodeGetChildCount(node); for (uint32_t i = 0; i < childCount; i++) { - const YGNodeRef child = YGNodeGetChild(node, i); + auto child = node->getChild(i); if (child->getLineIndex() > 0) { break; } @@ -1051,7 +1095,7 @@ static float YGBaseline(const YGNodeRef node, void* layoutContext) { return baseline + baselineChild->getLayout().position[YGEdgeTop]; } -static bool YGIsBaselineLayout(const YGNodeRef node) { +static bool YGIsBaselineLayout(const yoga::Node* node) { if (YGFlexDirectionIsColumn(node->getStyle().flexDirection())) { return false; } @@ -1060,7 +1104,7 @@ static bool YGIsBaselineLayout(const YGNodeRef node) { } const uint32_t childCount = YGNodeGetChildCount(node); for (uint32_t i = 0; i < childCount; i++) { - const YGNodeRef child = YGNodeGetChild(node, i); + auto child = node->getChild(i); if (child->getStyle().positionType() != YGPositionTypeAbsolute && child->getStyle().alignSelf() == YGAlignBaseline) { return true; @@ -1071,7 +1115,7 @@ static bool YGIsBaselineLayout(const YGNodeRef node) { } static inline float YGNodeDimWithMargin( - const YGNodeRef node, + const yoga::Node* const node, const YGFlexDirection axis, const float widthSize) { return node->getLayout().measuredDimensions[dim[axis]] + @@ -1081,7 +1125,7 @@ static inline float YGNodeDimWithMargin( } static inline bool YGNodeIsStyleDimDefined( - const YGNodeRef node, + const yoga::Node* const node, const YGFlexDirection axis, const float ownerSize) { bool isUndefined = @@ -1098,14 +1142,14 @@ static inline bool YGNodeIsStyleDimDefined( } static inline bool YGNodeIsLayoutDimDefined( - const YGNodeRef node, + const yoga::Node* const node, const YGFlexDirection axis) { const float value = node->getLayout().measuredDimensions[dim[axis]]; return !YGFloatIsUndefined(value) && value >= 0.0f; } static YGFloatOptional YGNodeBoundAxisWithinMinAndMax( - const YGNodeConstRef node, + const yoga::Node* const node, const YGFlexDirection axis, const YGFloatOptional value, const float axisSize) { @@ -1138,7 +1182,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 YGNodeRef node, + const yoga::Node* const node, const YGFlexDirection axis, const float value, const float axisSize, @@ -1151,8 +1195,8 @@ static inline float YGNodeBoundAxis( } static void YGNodeSetChildTrailingPosition( - const YGNodeRef node, - const YGNodeRef child, + const yoga::Node* const node, + yoga::Node* const child, const YGFlexDirection axis) { const float size = child->getLayout().measuredDimensions[dim[axis]]; child->setLayoutPosition( @@ -1162,7 +1206,7 @@ static void YGNodeSetChildTrailingPosition( } static void YGConstrainMaxSizeForMode( - const YGNodeConstRef node, + const yoga::Node* const node, const enum YGFlexDirection axis, const float ownerAxisSize, const float ownerWidth, @@ -1189,8 +1233,8 @@ static void YGConstrainMaxSizeForMode( } static void YGNodeComputeFlexBasisForChild( - const YGNodeRef node, - const YGNodeRef child, + const yoga::Node* const node, + yoga::Node* const child, const float width, const YGMeasureMode widthMode, const float height, @@ -1388,8 +1432,8 @@ static void YGNodeComputeFlexBasisForChild( } static void YGNodeAbsoluteLayoutChild( - const YGNodeRef node, - const YGNodeRef child, + const yoga::Node* const node, + yoga::Node* const child, const float width, const YGMeasureMode widthMode, const float height, @@ -1622,7 +1666,7 @@ static void YGNodeAbsoluteLayoutChild( } static void YGNodeWithMeasureFuncSetMeasuredDimensions( - const YGNodeRef node, + yoga::Node* const node, float availableWidth, float availableHeight, const YGMeasureMode widthMeasureMode, @@ -1729,7 +1773,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( - const YGNodeRef node, + yoga::Node* const node, const float availableWidth, const float availableHeight, const YGMeasureMode widthMeasureMode, @@ -1762,7 +1806,7 @@ static void YGNodeEmptyContainerSetMeasuredDimensions( } static bool YGNodeFixedSizeSetMeasuredDimensions( - const YGNodeRef node, + yoga::Node* const node, const float availableWidth, const float availableHeight, const YGMeasureMode widthMeasureMode, @@ -1807,7 +1851,7 @@ static bool YGNodeFixedSizeSetMeasuredDimensions( } static void YGZeroOutLayoutRecursively( - const YGNodeRef node, + yoga::Node* const node, void* layoutContext) { node->getLayout() = {}; node->setLayoutDimension(0, 0); @@ -1819,7 +1863,7 @@ static void YGZeroOutLayoutRecursively( } static float YGNodeCalculateAvailableInnerDim( - const YGNodeConstRef node, + const yoga::Node* const node, const YGDimension dimension, const float availableDim, const float paddingAndBorder, @@ -1850,7 +1894,7 @@ static float YGNodeCalculateAvailableInnerDim( } static float YGNodeComputeFlexBasisForChildren( - const YGNodeRef node, + yoga::Node* const node, const float availableInnerWidth, const float availableInnerHeight, YGMeasureMode widthMeasureMode, @@ -1865,7 +1909,7 @@ static float YGNodeComputeFlexBasisForChildren( const uint32_t generationCount) { float totalOuterFlexBasis = 0.0f; YGNodeRef singleFlexChild = nullptr; - const YGVector& children = node->getChildren(); + const auto& children = node->getChildren(); YGMeasureMode measureModeMainDim = YGFlexDirectionIsRow(mainAxis) ? widthMeasureMode : heightMeasureMode; // If there is only one child with flexGrow + flexShrink it means we can set @@ -1947,7 +1991,7 @@ static float YGNodeComputeFlexBasisForChildren( // YGNodeComputeFlexBasisForChildren function). This function calculates // YGCollectFlexItemsRowMeasurement static YGCollectFlexItemsRowValues YGCalculateCollectFlexItemsRowValues( - const YGNodeRef& node, + yoga::Node* const node, const YGDirection ownerDirection, const float mainAxisownerSize, const float availableInnerWidth, @@ -1966,7 +2010,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++) { - const YGNodeRef child = node->getChild(endOfLineIndex); + auto child = node->getChild(endOfLineIndex); if (child->getStyle().display() == YGDisplayNone || child->getStyle().positionType() == YGPositionTypeAbsolute) { continue; @@ -2039,7 +2083,7 @@ static YGCollectFlexItemsRowValues YGCalculateCollectFlexItemsRowValues( // please ensure that YGDistributeFreeSpaceFirstPass is called. static float YGDistributeFreeSpaceSecondPass( YGCollectFlexItemsRowValues& collectedFlexItemsValues, - const YGNodeRef node, + yoga::Node* const node, const YGFlexDirection mainAxis, const YGFlexDirection crossAxis, const float mainAxisownerSize, @@ -2342,7 +2386,7 @@ static void YGDistributeFreeSpaceFirstPass( // assigned to them. // static void YGResolveFlexibleLength( - const YGNodeRef node, + yoga::Node* const node, YGCollectFlexItemsRowValues& collectedFlexItemsValues, const YGFlexDirection mainAxis, const YGFlexDirection crossAxis, @@ -2393,7 +2437,7 @@ static void YGResolveFlexibleLength( } static void YGJustifyMainAxis( - const YGNodeRef node, + yoga::Node* const node, YGCollectFlexItemsRowValues& collectedFlexItemsValues, const uint32_t startOfLineIndex, const YGFlexDirection mainAxis, @@ -2445,7 +2489,7 @@ static void YGJustifyMainAxis( for (uint32_t i = startOfLineIndex; i < collectedFlexItemsValues.endOfLineIndex; i++) { - const YGNodeRef child = node->getChild(i); + auto child = node->getChild(i); if (child->getStyle().positionType() != YGPositionTypeAbsolute) { if (child->marginLeadingValue(mainAxis).unit == YGUnitAuto) { numberOfAutoMarginsOnCurrentLine++; @@ -2505,9 +2549,9 @@ static void YGJustifyMainAxis( for (uint32_t i = startOfLineIndex; i < collectedFlexItemsValues.endOfLineIndex; i++) { - const YGNodeRef child = node->getChild(i); + const auto child = node->getChild(i); const Style& childStyle = child->getStyle(); - const YGLayout childLayout = child->getLayout(); + const LayoutResults& childLayout = child->getLayout(); const bool isLastChild = i == collectedFlexItemsValues.endOfLineIndex - 1; // remove the gap if it is the last element of the line if (isLastChild) { @@ -2679,7 +2723,7 @@ static void YGJustifyMainAxis( // mode of YGMeasureModeUndefined in that dimension. // static void YGNodelayoutImpl( - const YGNodeRef node, + yoga::Node* const node, const float availableWidth, const float availableHeight, const YGDirection ownerDirection, @@ -3077,7 +3121,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 YGNodeRef child = node->getChild(i); + const auto child = node->getChild(i); if (child->getStyle().display() == YGDisplayNone) { continue; } @@ -3279,7 +3323,7 @@ static void YGNodelayoutImpl( float maxAscentForCurrentLine = 0; float maxDescentForCurrentLine = 0; for (ii = startIndex; ii < childCount; ii++) { - const YGNodeRef child = node->getChild(ii); + const auto child = node->getChild(ii); if (child->getStyle().display() == YGDisplayNone) { continue; } @@ -3322,7 +3366,7 @@ static void YGNodelayoutImpl( if (performLayout) { for (ii = startIndex; ii < endIndex; ii++) { - const YGNodeRef child = node->getChild(ii); + const auto child = node->getChild(ii); if (child->getStyle().display() == YGDisplayNone) { continue; } @@ -3516,7 +3560,7 @@ static void YGNodelayoutImpl( // positions on wrap-reverse. if (performLayout && node->getStyle().flexWrap() == YGWrapWrapReverse) { for (uint32_t i = 0; i < childCount; i++) { - const YGNodeRef child = YGNodeGetChild(node, i); + const auto child = node->getChild(i); if (child->getStyle().positionType() != YGPositionTypeAbsolute) { child->setLayoutPosition( node->getLayout().measuredDimensions[dim[crossAxis]] - @@ -3565,7 +3609,7 @@ static void YGNodelayoutImpl( // Set trailing position if necessary. if (needsMainTrailingPos || needsCrossTrailingPos) { for (uint32_t i = 0; i < childCount; i++) { - const YGNodeRef child = node->getChild(i); + const auto child = node->getChild(i); if (child->getStyle().display() == YGDisplayNone) { continue; } @@ -3776,7 +3820,7 @@ YOGA_EXPORT bool YGNodeCanUseCachedMeasurement( // Return parameter is true if layout was performed, false if skipped // bool YGLayoutNodeInternal( - const YGNodeRef node, + yoga::Node* const node, const float availableWidth, const float availableHeight, const YGDirection ownerDirection, @@ -3791,7 +3835,7 @@ bool YGLayoutNodeInternal( void* const layoutContext, uint32_t depth, const uint32_t generationCount) { - YGLayout* layout = &node->getLayout(); + LayoutResults* layout = &node->getLayout(); depth++; @@ -4065,7 +4109,7 @@ YOGA_EXPORT float YGConfigGetPointScaleFactor(const YGConfigRef config) { } static void YGRoundToPixelGrid( - const YGNodeRef node, + yoga::Node* const node, const double pointScaleFactor, const double absoluteLeft, const double absoluteTop) { @@ -4130,23 +4174,21 @@ static void YGRoundToPixelGrid( const uint32_t childCount = YGNodeGetChildCount(node); for (uint32_t i = 0; i < childCount; i++) { YGRoundToPixelGrid( - YGNodeGetChild(node, i), - pointScaleFactor, - absoluteNodeLeft, - absoluteNodeTop); + node->getChild(i), pointScaleFactor, absoluteNodeLeft, absoluteNodeTop); } } YOGA_EXPORT void YGNodeCalculateLayoutWithContext( - const YGNodeRef node, + const YGNodeRef nodeRef, const float ownerWidth, const float ownerHeight, const YGDirection ownerDirection, void* layoutContext) { - - Event::publish(node, {layoutContext}); + Event::publish(nodeRef, {layoutContext}); LayoutData markerData = {}; + const auto node = static_cast(nodeRef); + // Increment the generation count. This will force the recursive routine to // visit all dirty nodes at least once. Subsequent visits will be skipped if // the input parameters don't change. @@ -4248,7 +4290,12 @@ YOGA_EXPORT void YGConfigSetLogger(const YGConfigRef config, YGLogger logger) { void YGAssert(const bool condition, const char* message) { if (!condition) { - Log::log(YGNodeRef{nullptr}, YGLogLevelFatal, nullptr, "%s\n", message); + Log::log( + static_cast(nullptr), + YGLogLevelFatal, + nullptr, + "%s\n", + message); throwLogicalErrorWithMessage(message); } } @@ -4258,7 +4305,12 @@ void YGAssertWithNode( const bool condition, const char* message) { if (!condition) { - Log::log(node, YGLogLevelFatal, nullptr, "%s\n", message); + Log::log( + static_cast(node), + YGLogLevelFatal, + nullptr, + "%s\n", + message); throwLogicalErrorWithMessage(message); } } diff --git a/yoga/Yoga.h b/yoga/Yoga.h index e8bf0afffd..0c8986896c 100644 --- a/yoga/Yoga.h +++ b/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(YGNodeRef node); +WIN_EXPORT uint32_t YGNodeGetChildCount(YGNodeConstRef node); WIN_EXPORT void YGNodeSetChildren( YGNodeRef owner, const YGNodeRef* children, diff --git a/yoga/event/event.cpp b/yoga/event/event.cpp index ee47331d85..b47e1fab7f 100644 --- a/yoga/event/event.cpp +++ b/yoga/event/event.cpp @@ -73,7 +73,10 @@ void Event::subscribe(std::function&& subscriber) { push(new Node{std::move(subscriber)}); } -void Event::publish(const YGNode& node, Type eventType, const Data& eventData) { +void Event::publish( + YGNodeConstRef node, + Type eventType, + const Data& eventData) { for (auto subscriber = subscribers.load(std::memory_order_relaxed); subscriber != nullptr; subscriber = subscriber->next) { diff --git a/yoga/event/event.h b/yoga/event/event.h index 4c34fa66d6..3798e4663e 100644 --- a/yoga/event/event.h +++ b/yoga/event/event.h @@ -7,15 +7,13 @@ #pragma once +#include + #include #include #include -#include #include -struct YGConfig; -struct YGNode; - namespace facebook::yoga { enum struct LayoutType : int { @@ -63,7 +61,7 @@ struct YOGA_EXPORT Event { NodeBaselineEnd, }; class Data; - using Subscriber = void(const YGNode&, Type, Data); + using Subscriber = void(YGNodeConstRef, Type, Data); using Subscribers = std::vector>; template @@ -87,27 +85,22 @@ struct YOGA_EXPORT Event { static void subscribe(std::function&& subscriber); template - static void publish(const YGNode& node, const TypedData& eventData = {}) { + static void publish(YGNodeConstRef 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(const YGNode&, Type, const Data&); + static void publish(YGNodeConstRef, Type, const Data&); }; template <> struct Event::TypedData { - YGConfig* config; + YGConfigRef config; }; template <> struct Event::TypedData { - YGConfig* config; + YGConfigRef config; }; template <> diff --git a/yoga/log.cpp b/yoga/log.cpp index eb776629dd..3dc62c517d 100644 --- a/yoga/log.cpp +++ b/yoga/log.cpp @@ -9,7 +9,7 @@ #include "log.h" #include -#include "YGNode.h" +#include namespace facebook::yoga::detail { @@ -17,7 +17,7 @@ namespace { void vlog( yoga::Config* config, - YGNode* node, + yoga::Node* node, YGLogLevel level, void* context, const char* format, @@ -30,7 +30,7 @@ void vlog( } // namespace YOGA_EXPORT void Log::log( - YGNode* node, + yoga::Node* node, YGLogLevel level, void* context, const char* format, diff --git a/yoga/log.h b/yoga/log.h index ad0fe4d6f5..3d468ae10c 100644 --- a/yoga/log.h +++ b/yoga/log.h @@ -8,16 +8,14 @@ #pragma once #include +#include #include -struct YGNode; -struct YGConfig; - namespace facebook::yoga::detail { struct Log { static void log( - YGNode* node, + yoga::Node* node, YGLogLevel level, void*, const char* message, diff --git a/yoga/YGLayout.cpp b/yoga/node/LayoutResults.cpp similarity index 88% rename from yoga/YGLayout.cpp rename to yoga/node/LayoutResults.cpp index 9aadcd4aec..d6611aa159 100644 --- a/yoga/YGLayout.cpp +++ b/yoga/node/LayoutResults.cpp @@ -5,12 +5,12 @@ * LICENSE file in the root directory of this source tree. */ -#include "YGLayout.h" -#include "Utils.h" +#include +#include -using namespace facebook; +namespace facebook::yoga { -bool YGLayout::operator==(YGLayout layout) const { +bool LayoutResults::operator==(LayoutResults layout) const { bool isEqual = YGFloatArrayEqual(position, layout.position) && YGFloatArrayEqual(dimensions, layout.dimensions) && YGFloatArrayEqual(margin, layout.margin) && @@ -40,3 +40,5 @@ bool YGLayout::operator==(YGLayout layout) const { return isEqual; } + +} // namespace facebook::yoga diff --git a/yoga/YGLayout.h b/yoga/node/LayoutResults.h similarity index 89% rename from yoga/YGLayout.h rename to yoga/node/LayoutResults.h index 78a1bcf191..18cc135f2c 100644 --- a/yoga/YGLayout.h +++ b/yoga/node/LayoutResults.h @@ -11,7 +11,9 @@ #include #include -struct YGLayout { +namespace facebook::yoga { + +struct LayoutResults { std::array position = {}; std::array dimensions = {{YGUndefined, YGUndefined}}; std::array margin = {}; @@ -58,6 +60,8 @@ struct YGLayout { flags, hadOverflowOffset, hadOverflow); } - bool operator==(YGLayout layout) const; - bool operator!=(YGLayout layout) const { return !(*this == layout); } + bool operator==(LayoutResults layout) const; + bool operator!=(LayoutResults layout) const { return !(*this == layout); } }; + +} // namespace facebook::yoga diff --git a/yoga/YGNode.cpp b/yoga/node/Node.cpp similarity index 79% rename from yoga/YGNode.cpp rename to yoga/node/Node.cpp index 7389354e68..f0b355cf35 100644 --- a/yoga/YGNode.cpp +++ b/yoga/node/Node.cpp @@ -5,18 +5,15 @@ * LICENSE file in the root directory of this source tree. */ -#include "YGNode.h" +#include #include #include -#include "Utils.h" +#include -using namespace facebook; -using namespace facebook::yoga; -using facebook::yoga::CompactValue; +namespace facebook::yoga { -YGNode::YGNode(yoga::Config* config) : config_{config} { - YGAssert( - config != nullptr, "Attempting to construct YGNode with null config"); +Node::Node(yoga::Config* config) : config_{config} { + YGAssert(config != nullptr, "Attempting to construct Node with null config"); flags_.hasNewLayout = true; if (config->useWebDefaults()) { @@ -24,7 +21,7 @@ YGNode::YGNode(yoga::Config* config) : config_{config} { } } -YGNode::YGNode(YGNode&& node) { +Node::Node(Node&& node) { context_ = node.context_; flags_ = node.flags_; measure_ = node.measure_; @@ -43,7 +40,7 @@ YGNode::YGNode(YGNode&& node) { } } -void YGNode::print(void* printContext) { +void Node::print(void* printContext) { if (print_.noContext != nullptr) { if (flags_.printUsesContext) { print_.withContext(this, printContext); @@ -53,7 +50,7 @@ void YGNode::print(void* printContext) { } } -CompactValue YGNode::computeEdgeValueForRow( +CompactValue Node::computeEdgeValueForRow( const Style::Edges& edges, YGEdge rowEdge, YGEdge edge, @@ -71,7 +68,7 @@ CompactValue YGNode::computeEdgeValueForRow( } } -CompactValue YGNode::computeEdgeValueForColumn( +CompactValue Node::computeEdgeValueForColumn( const Style::Edges& edges, YGEdge edge, CompactValue defaultValue) { @@ -86,7 +83,7 @@ CompactValue YGNode::computeEdgeValueForColumn( } } -CompactValue YGNode::computeRowGap( +CompactValue Node::computeRowGap( const Style::Gutters& gutters, CompactValue defaultValue) { if (!gutters[YGGutterRow].isUndefined()) { @@ -98,7 +95,7 @@ CompactValue YGNode::computeRowGap( } } -CompactValue YGNode::computeColumnGap( +CompactValue Node::computeColumnGap( const Style::Gutters& gutters, CompactValue defaultValue) { if (!gutters[YGGutterColumn].isUndefined()) { @@ -110,7 +107,7 @@ CompactValue YGNode::computeColumnGap( } } -YGFloatOptional YGNode::getLeadingPosition( +YGFloatOptional Node::getLeadingPosition( const YGFlexDirection axis, const float axisSize) const { auto leadingPosition = YGFlexDirectionIsRow(axis) @@ -124,7 +121,7 @@ YGFloatOptional YGNode::getLeadingPosition( return YGResolveValue(leadingPosition, axisSize); } -YGFloatOptional YGNode::getTrailingPosition( +YGFloatOptional Node::getTrailingPosition( const YGFlexDirection axis, const float axisSize) const { auto trailingPosition = YGFlexDirectionIsRow(axis) @@ -138,7 +135,7 @@ YGFloatOptional YGNode::getTrailingPosition( return YGResolveValue(trailingPosition, axisSize); } -bool YGNode::isLeadingPositionDefined(const YGFlexDirection axis) const { +bool Node::isLeadingPositionDefined(const YGFlexDirection axis) const { auto leadingPosition = YGFlexDirectionIsRow(axis) ? computeEdgeValueForRow( style_.position(), @@ -150,7 +147,7 @@ bool YGNode::isLeadingPositionDefined(const YGFlexDirection axis) const { return !leadingPosition.isUndefined(); } -bool YGNode::isTrailingPosDefined(const YGFlexDirection axis) const { +bool Node::isTrailingPosDefined(const YGFlexDirection axis) const { auto trailingPosition = YGFlexDirectionIsRow(axis) ? computeEdgeValueForRow( style_.position(), @@ -162,7 +159,7 @@ bool YGNode::isTrailingPosDefined(const YGFlexDirection axis) const { return !trailingPosition.isUndefined(); } -YGFloatOptional YGNode::getLeadingMargin( +YGFloatOptional Node::getLeadingMargin( const YGFlexDirection axis, const float widthSize) const { auto leadingMargin = YGFlexDirectionIsRow(axis) @@ -173,7 +170,7 @@ YGFloatOptional YGNode::getLeadingMargin( return YGResolveValueMargin(leadingMargin, widthSize); } -YGFloatOptional YGNode::getTrailingMargin( +YGFloatOptional Node::getTrailingMargin( const YGFlexDirection axis, const float widthSize) const { auto trailingMargin = YGFlexDirectionIsRow(axis) @@ -184,13 +181,13 @@ YGFloatOptional YGNode::getTrailingMargin( return YGResolveValueMargin(trailingMargin, widthSize); } -YGFloatOptional YGNode::getMarginForAxis( +YGFloatOptional Node::getMarginForAxis( const YGFlexDirection axis, const float widthSize) const { return getLeadingMargin(axis, widthSize) + getTrailingMargin(axis, widthSize); } -YGFloatOptional YGNode::getGapForAxis( +YGFloatOptional Node::getGapForAxis( const YGFlexDirection axis, const float widthSize) const { auto gap = YGFlexDirectionIsRow(axis) @@ -199,7 +196,7 @@ YGFloatOptional YGNode::getGapForAxis( return YGResolveValue(gap, widthSize); } -YGSize YGNode::measure( +YGSize Node::measure( float width, YGMeasureMode widthMode, float height, @@ -211,7 +208,7 @@ YGSize YGNode::measure( : measure_.noContext(this, width, widthMode, height, heightMode); } -float YGNode::baseline(float width, float height, void* layoutContext) { +float Node::baseline(float width, float height, void* layoutContext) { return flags_.baselineUsesContext ? baseline_.withContext(this, width, height, layoutContext) : baseline_.noContext(this, width, height); @@ -219,7 +216,7 @@ float YGNode::baseline(float width, float height, void* layoutContext) { // Setters -void YGNode::setMeasureFunc(decltype(YGNode::measure_) measureFunc) { +void Node::setMeasureFunc(decltype(Node::measure_) measureFunc) { if (measureFunc.noContext == nullptr) { // TODO: t18095186 Move nodeType to opt-in function and mark appropriate // places in Litho @@ -238,38 +235,38 @@ void YGNode::setMeasureFunc(decltype(YGNode::measure_) measureFunc) { measure_ = measureFunc; } -void YGNode::setMeasureFunc(YGMeasureFunc measureFunc) { +void Node::setMeasureFunc(YGMeasureFunc measureFunc) { flags_.measureUsesContext = false; - decltype(YGNode::measure_) m; + decltype(Node::measure_) m; m.noContext = measureFunc; setMeasureFunc(m); } -YOGA_EXPORT void YGNode::setMeasureFunc(MeasureWithContextFn measureFunc) { +YOGA_EXPORT void Node::setMeasureFunc(MeasureWithContextFn measureFunc) { flags_.measureUsesContext = true; - decltype(YGNode::measure_) m; + decltype(Node::measure_) m; m.withContext = measureFunc; setMeasureFunc(m); } -void YGNode::replaceChild(YGNodeRef child, uint32_t index) { +void Node::replaceChild(Node* child, uint32_t index) { children_[index] = child; } -void YGNode::replaceChild(YGNodeRef oldChild, YGNodeRef newChild) { +void Node::replaceChild(Node* oldChild, Node* newChild) { std::replace(children_.begin(), children_.end(), oldChild, newChild); } -void YGNode::insertChild(YGNodeRef child, uint32_t index) { +void Node::insertChild(Node* child, uint32_t index) { children_.insert(children_.begin() + index, child); } -void YGNode::setConfig(yoga::Config* config) { - YGAssert(config != nullptr, "Attempting to set a null config on a YGNode"); +void Node::setConfig(yoga::Config* config) { + YGAssert(config != nullptr, "Attempting to set a null config on a Node"); YGAssertWithConfig( config, config->useWebDefaults() == config_->useWebDefaults(), - "UseWebDefaults may not be changed after constructing a YGNode"); + "UseWebDefaults may not be changed after constructing a Node"); if (yoga::configUpdateInvalidatesLayout(config_, config)) { markDirtyAndPropagate(); @@ -278,7 +275,7 @@ void YGNode::setConfig(yoga::Config* config) { config_ = config; } -void YGNode::setDirty(bool isDirty) { +void Node::setDirty(bool isDirty) { if (isDirty == flags_.isDirty) { return; } @@ -288,8 +285,8 @@ void YGNode::setDirty(bool isDirty) { } } -bool YGNode::removeChild(YGNodeRef child) { - std::vector::iterator p = +bool Node::removeChild(Node* child) { + std::vector::iterator p = std::find(children_.begin(), children_.end(), child); if (p != children_.end()) { children_.erase(p); @@ -298,59 +295,58 @@ bool YGNode::removeChild(YGNodeRef child) { return false; } -void YGNode::removeChild(uint32_t index) { +void Node::removeChild(uint32_t index) { children_.erase(children_.begin() + index); } -void YGNode::setLayoutDirection(YGDirection direction) { +void Node::setLayoutDirection(YGDirection direction) { layout_.setDirection(direction); } -void YGNode::setLayoutMargin(float margin, int index) { +void Node::setLayoutMargin(float margin, int index) { layout_.margin[index] = margin; } -void YGNode::setLayoutBorder(float border, int index) { +void Node::setLayoutBorder(float border, int index) { layout_.border[index] = border; } -void YGNode::setLayoutPadding(float padding, int index) { +void Node::setLayoutPadding(float padding, int index) { layout_.padding[index] = padding; } -void YGNode::setLayoutLastOwnerDirection(YGDirection direction) { +void Node::setLayoutLastOwnerDirection(YGDirection direction) { layout_.lastOwnerDirection = direction; } -void YGNode::setLayoutComputedFlexBasis( - const YGFloatOptional computedFlexBasis) { +void Node::setLayoutComputedFlexBasis(const YGFloatOptional computedFlexBasis) { layout_.computedFlexBasis = computedFlexBasis; } -void YGNode::setLayoutPosition(float position, int index) { +void Node::setLayoutPosition(float position, int index) { layout_.position[index] = position; } -void YGNode::setLayoutComputedFlexBasisGeneration( +void Node::setLayoutComputedFlexBasisGeneration( uint32_t computedFlexBasisGeneration) { layout_.computedFlexBasisGeneration = computedFlexBasisGeneration; } -void YGNode::setLayoutMeasuredDimension(float measuredDimension, int index) { +void Node::setLayoutMeasuredDimension(float measuredDimension, int index) { layout_.measuredDimensions[index] = measuredDimension; } -void YGNode::setLayoutHadOverflow(bool hadOverflow) { +void Node::setLayoutHadOverflow(bool hadOverflow) { layout_.setHadOverflow(hadOverflow); } -void YGNode::setLayoutDimension(float dimension, int index) { +void Node::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 YGNode::relativePosition( +YGFloatOptional Node::relativePosition( const YGFlexDirection axis, const float axisSize) const { if (isLeadingPositionDefined(axis)) { @@ -364,7 +360,7 @@ YGFloatOptional YGNode::relativePosition( return trailingPosition; } -void YGNode::setPosition( +void Node::setPosition( const YGDirection direction, const float mainSize, const float crossSize, @@ -402,7 +398,7 @@ void YGNode::setPosition( trailing[crossAxis]); } -YGValue YGNode::marginLeadingValue(const YGFlexDirection axis) const { +YGValue Node::marginLeadingValue(const YGFlexDirection axis) const { if (YGFlexDirectionIsRow(axis) && !style_.margin()[YGEdgeStart].isUndefined()) { return style_.margin()[YGEdgeStart]; @@ -411,7 +407,7 @@ YGValue YGNode::marginLeadingValue(const YGFlexDirection axis) const { } } -YGValue YGNode::marginTrailingValue(const YGFlexDirection axis) const { +YGValue Node::marginTrailingValue(const YGFlexDirection axis) const { if (YGFlexDirectionIsRow(axis) && !style_.margin()[YGEdgeEnd].isUndefined()) { return style_.margin()[YGEdgeEnd]; } else { @@ -419,7 +415,7 @@ YGValue YGNode::marginTrailingValue(const YGFlexDirection axis) const { } } -YGValue YGNode::resolveFlexBasisPtr() const { +YGValue Node::resolveFlexBasisPtr() const { YGValue flexBasis = style_.flexBasis(); if (flexBasis.unit != YGUnitAuto && flexBasis.unit != YGUnitUndefined) { return flexBasis; @@ -430,7 +426,7 @@ YGValue YGNode::resolveFlexBasisPtr() const { return YGValueAuto; } -void YGNode::resolveDimension() { +void Node::resolveDimension() { using namespace yoga; const Style& style = getStyle(); for (auto dim : {YGDimensionWidth, YGDimensionHeight}) { @@ -443,7 +439,7 @@ void YGNode::resolveDimension() { } } -YGDirection YGNode::resolveDirection(const YGDirection ownerDirection) { +YGDirection Node::resolveDirection(const YGDirection ownerDirection) { if (style_.direction() == YGDirectionInherit) { return ownerDirection > YGDirectionInherit ? ownerDirection : YGDirectionLTR; @@ -452,18 +448,18 @@ YGDirection YGNode::resolveDirection(const YGDirection ownerDirection) { } } -YOGA_EXPORT void YGNode::clearChildren() { +YOGA_EXPORT void Node::clearChildren() { children_.clear(); children_.shrink_to_fit(); } // Other Methods -void YGNode::cloneChildrenIfNeeded(void* cloneContext) { - iterChildrenAfterCloningIfNeeded([](YGNodeRef, void*) {}, cloneContext); +void Node::cloneChildrenIfNeeded(void* cloneContext) { + iterChildrenAfterCloningIfNeeded([](Node*, void*) {}, cloneContext); } -void YGNode::markDirtyAndPropagate() { +void Node::markDirtyAndPropagate() { if (!flags_.isDirty) { setDirty(true); setLayoutComputedFlexBasis(YGFloatOptional()); @@ -473,14 +469,14 @@ void YGNode::markDirtyAndPropagate() { } } -void YGNode::markDirtyAndPropagateDownwards() { +void Node::markDirtyAndPropagateDownwards() { flags_.isDirty = true; - for_each(children_.begin(), children_.end(), [](YGNodeRef childNode) { + for_each(children_.begin(), children_.end(), [](Node* childNode) { childNode->markDirtyAndPropagateDownwards(); }); } -float YGNode::resolveFlexGrow() const { +float Node::resolveFlexGrow() const { // Root nodes flexGrow should always be 0 if (owner_ == nullptr) { return 0.0; @@ -494,7 +490,7 @@ float YGNode::resolveFlexGrow() const { return kDefaultFlexGrow; } -float YGNode::resolveFlexShrink() const { +float Node::resolveFlexShrink() const { if (owner_ == nullptr) { return 0.0; } @@ -508,13 +504,13 @@ float YGNode::resolveFlexShrink() const { return config_->useWebDefaults() ? kWebDefaultFlexShrink : kDefaultFlexShrink; } -bool YGNode::isNodeFlexible() { +bool Node::isNodeFlexible() { return ( (style_.positionType() != YGPositionTypeAbsolute) && (resolveFlexGrow() != 0 || resolveFlexShrink() != 0)); } -float YGNode::getLeadingBorder(const YGFlexDirection axis) const { +float Node::getLeadingBorder(const YGFlexDirection axis) const { YGValue leadingBorder = YGFlexDirectionIsRow(axis) ? computeEdgeValueForRow( style_.border(), YGEdgeStart, leading[axis], CompactValue::ofZero()) @@ -523,7 +519,7 @@ float YGNode::getLeadingBorder(const YGFlexDirection axis) const { return fmaxf(leadingBorder.value, 0.0f); } -float YGNode::getTrailingBorder(const YGFlexDirection axis) const { +float Node::getTrailingBorder(const YGFlexDirection axis) const { YGValue trailingBorder = YGFlexDirectionIsRow(axis) ? computeEdgeValueForRow( style_.border(), YGEdgeEnd, trailing[axis], CompactValue::ofZero()) @@ -532,7 +528,7 @@ float YGNode::getTrailingBorder(const YGFlexDirection axis) const { return fmaxf(trailingBorder.value, 0.0f); } -YGFloatOptional YGNode::getLeadingPadding( +YGFloatOptional Node::getLeadingPadding( const YGFlexDirection axis, const float widthSize) const { auto leadingPadding = YGFlexDirectionIsRow(axis) @@ -547,7 +543,7 @@ YGFloatOptional YGNode::getLeadingPadding( YGResolveValue(leadingPadding, widthSize), YGFloatOptional(0.0f)); } -YGFloatOptional YGNode::getTrailingPadding( +YGFloatOptional Node::getTrailingPadding( const YGFlexDirection axis, const float widthSize) const { auto trailingPadding = YGFlexDirectionIsRow(axis) @@ -559,21 +555,21 @@ YGFloatOptional YGNode::getTrailingPadding( YGResolveValue(trailingPadding, widthSize), YGFloatOptional(0.0f)); } -YGFloatOptional YGNode::getLeadingPaddingAndBorder( +YGFloatOptional Node::getLeadingPaddingAndBorder( const YGFlexDirection axis, const float widthSize) const { return getLeadingPadding(axis, widthSize) + YGFloatOptional(getLeadingBorder(axis)); } -YGFloatOptional YGNode::getTrailingPaddingAndBorder( +YGFloatOptional Node::getTrailingPaddingAndBorder( const YGFlexDirection axis, const float widthSize) const { return getTrailingPadding(axis, widthSize) + YGFloatOptional(getTrailingBorder(axis)); } -void YGNode::reset() { +void Node::reset() { YGAssertWithNode( this, children_.size() == 0, @@ -581,5 +577,7 @@ void YGNode::reset() { YGAssertWithNode( this, owner_ == nullptr, "Cannot reset a node still attached to a owner"); - *this = YGNode{getConfig()}; + *this = Node{getConfig()}; } + +} // namespace facebook::yoga diff --git a/yoga/YGNode.h b/yoga/node/Node.h similarity index 78% rename from yoga/YGNode.h rename to yoga/node/Node.h index 218cb989ff..abb1535aed 100644 --- a/yoga/YGNode.h +++ b/yoga/node/Node.h @@ -10,15 +10,20 @@ #include #include #include -#include "YGLayout.h" +#include #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 YGNodeFlags { +struct NodeFlags { bool hasNewLayout : 1; bool isReferenceBaseline : 1; bool isDirty : 1; @@ -29,7 +34,8 @@ struct YGNodeFlags { }; #pragma pack(pop) -struct YOGA_EXPORT YGNode { +class YOGA_EXPORT Node : public ::YGNode { +public: using MeasureWithContextFn = YGSize (*)(YGNode*, float, YGMeasureMode, float, YGMeasureMode, void*); using BaselineWithContextFn = float (*)(YGNode*, float, float, void*); @@ -37,7 +43,7 @@ struct YOGA_EXPORT YGNode { private: void* context_ = nullptr; - YGNodeFlags flags_ = {}; + NodeFlags flags_ = {}; union { YGMeasureFunc noContext; MeasureWithContextFn withContext; @@ -51,12 +57,12 @@ struct YOGA_EXPORT YGNode { PrintWithContextFn withContext; } print_ = {nullptr}; YGDirtiedFunc dirtied_ = nullptr; - facebook::yoga::Style style_ = {}; - YGLayout layout_ = {}; + Style style_ = {}; + LayoutResults layout_ = {}; uint32_t lineIndex_ = 0; - YGNodeRef owner_ = nullptr; - YGVector children_ = {}; - facebook::yoga::Config* config_; + Node* owner_ = nullptr; + std::vector children_ = {}; + Config* config_; std::array resolvedDimensions_ = { {YGValueUndefined, YGValueUndefined}}; @@ -77,27 +83,24 @@ struct YOGA_EXPORT YGNode { // 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! - YGNode& operator=(YGNode&&) = default; - - using CompactValue = facebook::yoga::CompactValue; + Node& operator=(Node&&) = default; public: - YGNode() - : YGNode{static_cast(YGConfigGetDefault())} { + Node() : Node{static_cast(YGConfigGetDefault())} { flags_.hasNewLayout = true; } - explicit YGNode(facebook::yoga::Config* config); - ~YGNode() = default; // cleanup of owner/children relationships in YGNodeFree + explicit Node(Config* config); + ~Node() = default; // cleanup of owner/children relationships in YGNodeFree - YGNode(YGNode&&); + Node(Node&&); // Does not expose true value semantics, as children are not cloned eagerly. // Should we remove this? - YGNode(const YGNode& node) = default; + Node(const Node& node) = default; // assignment means potential leaks of existing children, or alternatively // freeing unowned memory, double free, or freeing stack memory. - YGNode& operator=(const YGNode&) = delete; + Node& operator=(const Node&) = delete; // Getters void* getContext() const { return context_; } @@ -125,38 +128,39 @@ struct YOGA_EXPORT YGNode { YGDirtiedFunc getDirtied() const { return dirtied_; } // For Performance reasons passing as reference. - facebook::yoga::Style& getStyle() { return style_; } + Style& getStyle() { return style_; } - const facebook::yoga::Style& getStyle() const { return style_; } + const Style& getStyle() const { return style_; } // For Performance reasons passing as reference. - YGLayout& getLayout() { return layout_; } + LayoutResults& getLayout() { return layout_; } - const YGLayout& getLayout() const { return layout_; } + const LayoutResults& getLayout() const { return layout_; } uint32_t getLineIndex() const { return lineIndex_; } bool isReferenceBaseline() { return flags_.isReferenceBaseline; } - // 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_; } + // 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_; } // Deprecated, use getOwner() instead. - YGNodeRef getParent() const { return getOwner(); } + Node* getParent() const { return getOwner(); } - const YGVector& getChildren() const { return children_; } + const std::vector& 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 (YGNodeRef& child : children_) { + for (Node*& child : children_) { if (child->getOwner() != this) { - child = config_->cloneNode(child, this, i, cloneContext); + child = static_cast( + config_->cloneNode(child, this, i, cloneContext)); child->setOwner(this); } i += 1; @@ -165,9 +169,9 @@ struct YOGA_EXPORT YGNode { } } - YGNodeRef getChild(uint32_t index) const { return children_.at(index); } + Node* getChild(uint32_t index) const { return children_.at(index); } - facebook::yoga::Config* getConfig() const { return config_; } + Config* getConfig() const { return config_; } bool isDirty() const { return flags_.isDirty; } @@ -180,22 +184,22 @@ struct YOGA_EXPORT YGNode { } static CompactValue computeEdgeValueForColumn( - const facebook::yoga::Style::Edges& edges, + const Style::Edges& edges, YGEdge edge, CompactValue defaultValue); static CompactValue computeEdgeValueForRow( - const facebook::yoga::Style::Edges& edges, + const Style::Edges& edges, YGEdge rowEdge, YGEdge edge, CompactValue defaultValue); static CompactValue computeRowGap( - const facebook::yoga::Style::Gutters& gutters, + const Style::Gutters& gutters, CompactValue defaultValue); static CompactValue computeColumnGap( - const facebook::yoga::Style::Gutters& gutters, + const Style::Gutters& gutters, CompactValue defaultValue); // Methods related to positions, margin, padding and border @@ -275,9 +279,9 @@ struct YOGA_EXPORT YGNode { void setDirtiedFunc(YGDirtiedFunc dirtiedFunc) { dirtied_ = dirtiedFunc; } - void setStyle(const facebook::yoga::Style& style) { style_ = style; } + void setStyle(const Style& style) { style_ = style; } - void setLayout(const YGLayout& layout) { layout_ = layout; } + void setLayout(const LayoutResults& layout) { layout_ = layout; } void setLineIndex(uint32_t lineIndex) { lineIndex_ = lineIndex; } @@ -285,13 +289,13 @@ struct YOGA_EXPORT YGNode { flags_.isReferenceBaseline = isReferenceBaseline; } - void setOwner(YGNodeRef owner) { owner_ = owner; } + void setOwner(Node* owner) { owner_ = owner; } - void setChildren(const YGVector& children) { children_ = children; } + void setChildren(const std::vector& children) { children_ = children; } // TODO: rvalue override for setChildren - void setConfig(facebook::yoga::Config* config); + void setConfig(Config* config); void setDirty(bool isDirty); void setLayoutLastOwnerDirection(YGDirection direction); @@ -321,11 +325,11 @@ struct YOGA_EXPORT YGNode { YGDirection resolveDirection(const YGDirection ownerDirection); void clearChildren(); /// Replaces the occurrences of oldChild with newChild - void replaceChild(YGNodeRef oldChild, YGNodeRef newChild); - void replaceChild(YGNodeRef child, uint32_t index); - void insertChild(YGNodeRef child, uint32_t index); + void replaceChild(Node* oldChild, Node* newChild); + void replaceChild(Node* child, uint32_t index); + void insertChild(Node* child, uint32_t index); /// Removes the first occurrence of child - bool removeChild(YGNodeRef child); + bool removeChild(Node* child); void removeChild(uint32_t index); void cloneChildrenIfNeeded(void*); @@ -335,3 +339,5 @@ struct YOGA_EXPORT YGNode { bool isNodeFlexible(); void reset(); }; + +} // namespace facebook::yoga