C++ style enums 3/N: ExperimentalFeature

Differential Revision: D49269440

fbshipit-source-id: 17582f27ed995f861deeed6bcb0c0a00d12da47e
This commit is contained in:
Nick Gerleman
2023-09-14 20:50:01 -07:00
committed by Facebook GitHub Bot
parent f8331b5a48
commit e85cb99267
6 changed files with 23 additions and 39 deletions

View File

@@ -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) {

View File

@@ -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,

View File

@@ -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 <yoga/YGEnums.h>
#include <bitset>
namespace facebook::yoga {
// std::bitset with one bit for each option defined in YG_ENUM_SEQ_DECL
template <typename Enum>
using EnumBitset = std::bitset<enums::count<Enum>()>;
} // namespace facebook::yoga

View File

@@ -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<size_t>(feature), enabled);
}
bool Config::isExperimentalFeatureEnabled(YGExperimentalFeature feature) const {
return experimentalFeatures_.test(feature);
bool Config::isExperimentalFeatureEnabled(ExperimentalFeature feature) const {
return experimentalFeatures_.test(static_cast<size_t>(feature));
}
EnumBitset<YGExperimentalFeature> Config::getEnabledExperiments() const {
ExperimentalFeatureSet Config::getEnabledExperiments() const {
return experimentalFeatures_;
}

View File

@@ -7,8 +7,10 @@
#pragma once
#include <bitset>
#include <yoga/Yoga.h>
#include <yoga/bits/EnumBitset.h>
#include <yoga/enums/ExperimentalFeature.h>
// 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<ordinalCount<ExperimentalFeature>()>;
// 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<YGExperimentalFeature> 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<YGExperimentalFeature> experimentalFeatures_{};
ExperimentalFeatureSet experimentalFeatures_{};
YGErrata errata_ = YGErrataNone;
float pointScaleFactor_ = 1.0f;
void* context_ = nullptr;

View File

@@ -10,9 +10,9 @@
namespace facebook::yoga {
template <typename EnumT>
constexpr inline int32_t ordinalCount() = delete;
constexpr inline int32_t ordinalCount();
template <typename EnumT>
constexpr inline int32_t bitCount() = delete;
constexpr inline int32_t bitCount();
} // namespace facebook::yoga