From 75d29071731ab950539b2f9113cc23992251a0ac Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Thu, 14 Sep 2023 23:06:34 -0700 Subject: [PATCH] C++ style enums 3/N: ExperimentalFeature Summary: X-link: https://github.com/facebook/react-native/pull/39448 X-link: https://github.com/facebook/yoga/pull/1386 This converts usages of YGExperimentalFeature to ExperimentalFeature Reviewed By: rozele Differential Revision: D49269440 fbshipit-source-id: 0fcb4f380e214a6aadcac457df5a989789bb05d2 --- lib/yoga/src/main/cpp/yoga/Yoga.cpp | 5 +++-- .../cpp/yoga/algorithm/CalculateLayout.cpp | 8 ++++---- lib/yoga/src/main/cpp/yoga/bits/EnumBitset.h | 19 ------------------- lib/yoga/src/main/cpp/yoga/config/Config.cpp | 10 +++++----- lib/yoga/src/main/cpp/yoga/config/Config.h | 16 +++++++++------- lib/yoga/src/main/cpp/yoga/enums/YogaEnums.h | 4 ++-- 6 files changed, 23 insertions(+), 39 deletions(-) delete mode 100644 lib/yoga/src/main/cpp/yoga/bits/EnumBitset.h diff --git a/lib/yoga/src/main/cpp/yoga/Yoga.cpp b/lib/yoga/src/main/cpp/yoga/Yoga.cpp index 80d7b257d20..d1f9a4f8610 100644 --- a/lib/yoga/src/main/cpp/yoga/Yoga.cpp +++ b/lib/yoga/src/main/cpp/yoga/Yoga.cpp @@ -851,13 +851,14 @@ void YGConfigSetExperimentalFeatureEnabled( const YGConfigRef config, const YGExperimentalFeature feature, const bool enabled) { - resolveRef(config)->setExperimentalFeatureEnabled(feature, enabled); + resolveRef(config)->setExperimentalFeatureEnabled( + scopedEnum(feature), enabled); } bool YGConfigIsExperimentalFeatureEnabled( const YGConfigConstRef config, const YGExperimentalFeature feature) { - return resolveRef(config)->isExperimentalFeatureEnabled(feature); + return resolveRef(config)->isExperimentalFeatureEnabled(scopedEnum(feature)); } void YGConfigSetUseWebDefaults(const YGConfigRef config, const bool enabled) { diff --git a/lib/yoga/src/main/cpp/yoga/algorithm/CalculateLayout.cpp b/lib/yoga/src/main/cpp/yoga/algorithm/CalculateLayout.cpp index a8cb925f5f0..55fb9cd8eb8 100644 --- a/lib/yoga/src/main/cpp/yoga/algorithm/CalculateLayout.cpp +++ b/lib/yoga/src/main/cpp/yoga/algorithm/CalculateLayout.cpp @@ -154,7 +154,7 @@ static void computeFlexBasisForChild( if (!resolvedFlexBasis.isUndefined() && !yoga::isUndefined(mainAxisSize)) { if (child->getLayout().computedFlexBasis.isUndefined() || (child->getConfig()->isExperimentalFeatureEnabled( - YGExperimentalFeatureWebFlexBasis) && + ExperimentalFeature::WebFlexBasis) && child->getLayout().computedFlexBasisGeneration != generationCount)) { const FloatOptional paddingAndBorder = FloatOptional(paddingAndBorderForAxis(child, mainAxis, ownerWidth)); @@ -479,7 +479,7 @@ static void layoutAbsoluteChild( leadingEdge(mainAxis)); } else if ( node->getConfig()->isExperimentalFeatureEnabled( - YGExperimentalFeatureAbsolutePercentageAgainstPaddingEdge) && + ExperimentalFeature::AbsolutePercentageAgainstPaddingEdge) && child->isLeadingPositionDefined(mainAxis)) { child->setLayoutPosition( child->getLeadingPosition( @@ -526,7 +526,7 @@ static void layoutAbsoluteChild( leadingEdge(crossAxis)); } else if ( node->getConfig()->isExperimentalFeatureEnabled( - YGExperimentalFeatureAbsolutePercentageAgainstPaddingEdge) && + ExperimentalFeature::AbsolutePercentageAgainstPaddingEdge) && child->isLeadingPositionDefined(crossAxis)) { child->setLayoutPosition( child->getLeadingPosition( @@ -2323,7 +2323,7 @@ static void calculateLayoutImpl( } const bool absolutePercentageAgainstPaddingEdge = node->getConfig()->isExperimentalFeatureEnabled( - YGExperimentalFeatureAbsolutePercentageAgainstPaddingEdge); + ExperimentalFeature::AbsolutePercentageAgainstPaddingEdge); layoutAbsoluteChild( node, diff --git a/lib/yoga/src/main/cpp/yoga/bits/EnumBitset.h b/lib/yoga/src/main/cpp/yoga/bits/EnumBitset.h deleted file mode 100644 index 79ee1e7446f..00000000000 --- a/lib/yoga/src/main/cpp/yoga/bits/EnumBitset.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include -#include - -namespace facebook::yoga { - -// std::bitset with one bit for each option defined in YG_ENUM_SEQ_DECL -template -using EnumBitset = std::bitset()>; - -} // namespace facebook::yoga diff --git a/lib/yoga/src/main/cpp/yoga/config/Config.cpp b/lib/yoga/src/main/cpp/yoga/config/Config.cpp index e86c76017bb..a3e48d34369 100644 --- a/lib/yoga/src/main/cpp/yoga/config/Config.cpp +++ b/lib/yoga/src/main/cpp/yoga/config/Config.cpp @@ -41,16 +41,16 @@ bool Config::shouldPrintTree() const { } void Config::setExperimentalFeatureEnabled( - YGExperimentalFeature feature, + ExperimentalFeature feature, bool enabled) { - experimentalFeatures_.set(feature, enabled); + experimentalFeatures_.set(static_cast(feature), enabled); } -bool Config::isExperimentalFeatureEnabled(YGExperimentalFeature feature) const { - return experimentalFeatures_.test(feature); +bool Config::isExperimentalFeatureEnabled(ExperimentalFeature feature) const { + return experimentalFeatures_.test(static_cast(feature)); } -EnumBitset Config::getEnabledExperiments() const { +ExperimentalFeatureSet Config::getEnabledExperiments() const { return experimentalFeatures_; } diff --git a/lib/yoga/src/main/cpp/yoga/config/Config.h b/lib/yoga/src/main/cpp/yoga/config/Config.h index 4590fcdf2fc..bc5a0ef2e9e 100644 --- a/lib/yoga/src/main/cpp/yoga/config/Config.h +++ b/lib/yoga/src/main/cpp/yoga/config/Config.h @@ -7,8 +7,10 @@ #pragma once +#include + #include -#include +#include // Tag struct used to form the opaque YGConfigRef for the public C API struct YGConfig {}; @@ -18,6 +20,8 @@ namespace facebook::yoga { class Config; class Node; +using ExperimentalFeatureSet = std::bitset()>; + // Whether moving a node from an old to new config should dirty previously // calculated layout results. bool configUpdateInvalidatesLayout( @@ -43,11 +47,9 @@ class YG_EXPORT Config : public ::YGConfig { void setShouldPrintTree(bool printTree); bool shouldPrintTree() const; - void setExperimentalFeatureEnabled( - YGExperimentalFeature feature, - bool enabled); - bool isExperimentalFeatureEnabled(YGExperimentalFeature feature) const; - EnumBitset getEnabledExperiments() const; + void setExperimentalFeatureEnabled(ExperimentalFeature feature, bool enabled); + bool isExperimentalFeatureEnabled(ExperimentalFeature feature) const; + ExperimentalFeatureSet getEnabledExperiments() const; void setErrata(YGErrata errata); void addErrata(YGErrata errata); @@ -79,7 +81,7 @@ class YG_EXPORT Config : public ::YGConfig { YGLogger logger_; ConfigFlags flags_{}; - EnumBitset experimentalFeatures_{}; + ExperimentalFeatureSet experimentalFeatures_{}; YGErrata errata_ = YGErrataNone; float pointScaleFactor_ = 1.0f; void* context_ = nullptr; diff --git a/lib/yoga/src/main/cpp/yoga/enums/YogaEnums.h b/lib/yoga/src/main/cpp/yoga/enums/YogaEnums.h index cc7223bf9c1..9621244813e 100644 --- a/lib/yoga/src/main/cpp/yoga/enums/YogaEnums.h +++ b/lib/yoga/src/main/cpp/yoga/enums/YogaEnums.h @@ -10,9 +10,9 @@ namespace facebook::yoga { template -constexpr inline int32_t ordinalCount() = delete; +constexpr inline int32_t ordinalCount(); template -constexpr inline int32_t bitCount() = delete; +constexpr inline int32_t bitCount(); } // namespace facebook::yoga