diff --git a/.clang-format-ignore b/.clang-format-ignore deleted file mode 100644 index 51430a0623..0000000000 --- a/.clang-format-ignore +++ /dev/null @@ -1 +0,0 @@ -^lib/.* diff --git a/enums.py b/enums.py index 0ae51722bc..c51ee41a27 100755 --- a/enums.py +++ b/enums.py @@ -4,6 +4,7 @@ # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. +import math import os ENUMS = { @@ -88,7 +89,7 @@ def get_license(ext): */ // @{"generated"} by enums.py - +{"// clang-format off" if ext == "cpp" else ""} """ @@ -121,7 +122,6 @@ def to_hyphenated_lower(symbol): f.write(get_license("cpp")) f.write("#pragma once\n") f.write("#include \n\n") - f.write("// clang-format off\n\n\n") f.write("YG_EXTERN_C_BEGIN\n\n") items = sorted(ENUMS.items()) @@ -146,6 +146,52 @@ def to_hyphenated_lower(symbol): f.write("\n") f.write("YG_EXTERN_C_END\n") +# Write out C++ scoped enums +for name, values in sorted(ENUMS.items()): + with open(f"{root}/yoga/enums/{name}.h", "w") as f: + f.write(get_license("cpp")) + f.write("#pragma once\n\n") + + f.write("#include \n") + f.write("#include \n") + f.write("#include \n\n") + + f.write("namespace facebook::yoga {\n\n") + + width = "uint32_t" if name in BITSET_ENUMS else "uint8_t" + f.write(f"enum class {name} : {width} {{\n") + for value in values: + ordinal = value[0] if isinstance(value, tuple) else value + f.write(f" {ordinal} = YG{name}{ordinal},\n") + f.write("};\n\n") + f.write( + f"YG_DEFINE_ENUM_FLAG_OPERATORS({name})\n\n" if name in BITSET_ENUMS else "" + ) + + f.write("template <>\n") + f.write(f"constexpr inline int32_t ordinalCount<{name}>() {{\n") + f.write(f" return {len(values)};\n") + f.write("} \n\n") + + f.write("template <>\n") + f.write(f"constexpr inline int32_t bitCount<{name}>() {{\n") + f.write(f" return {math.ceil(math.log(len(values), 2))};\n") + f.write("} \n\n") + + f.write(f"constexpr inline {name} scopedEnum(YG{name} unscoped) {{\n") + f.write(f" return static_cast<{name}>(unscoped);\n") + f.write("}\n\n") + + f.write(f"constexpr inline YG{name} unscopedEnum({name} scoped) {{\n") + f.write(f" return static_cast(scoped);\n") + f.write("}\n\n") + + f.write(f"inline const char* toString({name} e) {{\n") + f.write(f" return YG{name}ToString(unscopedEnum(e));\n") + f.write("}\n\n") + + f.write("} // namespace facebook::yoga\n") + # write out C body for printing with open(root + "/yoga/YGEnums.cpp", "w") as f: f.write(get_license("cpp")) diff --git a/tests/YGNodeCallbackTest.cpp b/tests/YGNodeCallbackTest.cpp index 4f68871c98..f40d7e0fa4 100644 --- a/tests/YGNodeCallbackTest.cpp +++ b/tests/YGNodeCallbackTest.cpp @@ -38,7 +38,7 @@ TEST(Node, measure_with_measure_fn) { }); ASSERT_EQ( - n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost), + n.measure(23, MeasureMode::Exactly, 24, MeasureMode::AtMost), (YGSize{23, 12})); } diff --git a/yoga/YGEnums.cpp b/yoga/YGEnums.cpp index f7220eff72..a7b1990eed 100644 --- a/yoga/YGEnums.cpp +++ b/yoga/YGEnums.cpp @@ -6,7 +6,7 @@ */ // @generated by enums.py - +// clang-format off #include const char* YGAlignToString(const YGAlign value) { diff --git a/yoga/YGEnums.h b/yoga/YGEnums.h index 7abe5d92ec..53d245315e 100644 --- a/yoga/YGEnums.h +++ b/yoga/YGEnums.h @@ -6,13 +6,10 @@ */ // @generated by enums.py - +// clang-format off #pragma once #include -// clang-format off - - YG_EXTERN_C_BEGIN YG_ENUM_SEQ_DECL( diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index 34ef7f9f30..6992ca0f55 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -81,11 +81,11 @@ void YGNodeSetHasNewLayout(YGNodeRef node, bool hasNewLayout) { } YGNodeType YGNodeGetNodeType(YGNodeConstRef node) { - return resolveRef(node)->getNodeType(); + return unscopedEnum(resolveRef(node)->getNodeType()); } void YGNodeSetNodeType(YGNodeRef node, YGNodeType nodeType) { - return resolveRef(node)->setNodeType(nodeType); + return resolveRef(node)->setNodeType(scopedEnum(nodeType)); } bool YGNodeIsDirty(YGNodeConstRef node) { @@ -801,11 +801,8 @@ YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Border, border) YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Padding, padding) #ifdef DEBUG -void YGNodePrint(const YGNodeConstRef nodeRef, const YGPrintOptions options) { - const auto node = resolveRef(nodeRef); - std::string str; - yoga::nodeToString(str, node, options, 0); - yoga::log(node, YGLogLevelDebug, str.c_str()); +void YGNodePrint(const YGNodeConstRef node, const YGPrintOptions options) { + yoga::print(resolveRef(node), scopedEnum(options)); } #endif @@ -851,13 +848,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) { @@ -877,11 +875,11 @@ void* YGConfigGetContext(const YGConfigConstRef config) { } void YGConfigSetErrata(YGConfigRef config, YGErrata errata) { - resolveRef(config)->setErrata(errata); + resolveRef(config)->setErrata(scopedEnum(errata)); } YGErrata YGConfigGetErrata(YGConfigConstRef config) { - return resolveRef(config)->getErrata(); + return unscopedEnum(resolveRef(config)->getErrata()); } void YGConfigSetCloneNodeFunc( @@ -907,13 +905,13 @@ bool YGNodeCanUseCachedMeasurement( float marginColumn, YGConfigRef config) { return yoga::canUseCachedMeasurement( - widthMode, + scopedEnum(widthMode), availableWidth, - heightMode, + scopedEnum(heightMode), availableHeight, - lastWidthMode, + scopedEnum(lastWidthMode), lastAvailableWidth, - lastHeightMode, + scopedEnum(lastHeightMode), lastAvailableHeight, lastComputedWidth, lastComputedHeight, diff --git a/yoga/algorithm/Cache.cpp b/yoga/algorithm/Cache.cpp index 8cabe4dac2..7496e873e0 100644 --- a/yoga/algorithm/Cache.cpp +++ b/yoga/algorithm/Cache.cpp @@ -5,8 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -#include - #include #include #include @@ -14,43 +12,43 @@ namespace facebook::yoga { static inline bool sizeIsExactAndMatchesOldMeasuredSize( - YGMeasureMode sizeMode, + MeasureMode sizeMode, float size, float lastComputedSize) { - return sizeMode == YGMeasureModeExactly && + return sizeMode == MeasureMode::Exactly && yoga::inexactEquals(size, lastComputedSize); } static inline bool oldSizeIsUnspecifiedAndStillFits( - YGMeasureMode sizeMode, + MeasureMode sizeMode, float size, - YGMeasureMode lastSizeMode, + MeasureMode lastSizeMode, float lastComputedSize) { - return sizeMode == YGMeasureModeAtMost && - lastSizeMode == YGMeasureModeUndefined && + return sizeMode == MeasureMode::AtMost && + lastSizeMode == MeasureMode::Undefined && (size >= lastComputedSize || yoga::inexactEquals(size, lastComputedSize)); } static inline bool newMeasureSizeIsStricterAndStillValid( - YGMeasureMode sizeMode, + MeasureMode sizeMode, float size, - YGMeasureMode lastSizeMode, + MeasureMode lastSizeMode, float lastSize, float lastComputedSize) { - return lastSizeMode == YGMeasureModeAtMost && - sizeMode == YGMeasureModeAtMost && !std::isnan(lastSize) && + return lastSizeMode == MeasureMode::AtMost && + sizeMode == MeasureMode::AtMost && !std::isnan(lastSize) && !std::isnan(size) && !std::isnan(lastComputedSize) && lastSize > size && (lastComputedSize <= size || yoga::inexactEquals(size, lastComputedSize)); } bool canUseCachedMeasurement( - const YGMeasureMode widthMode, + const MeasureMode widthMode, const float availableWidth, - const YGMeasureMode heightMode, + const MeasureMode heightMode, const float availableHeight, - const YGMeasureMode lastWidthMode, + const MeasureMode lastWidthMode, const float lastAvailableWidth, - const YGMeasureMode lastHeightMode, + const MeasureMode lastHeightMode, const float lastAvailableHeight, const float lastComputedWidth, const float lastComputedHeight, diff --git a/yoga/algorithm/Cache.h b/yoga/algorithm/Cache.h index 76643242dc..31b91d5f79 100644 --- a/yoga/algorithm/Cache.h +++ b/yoga/algorithm/Cache.h @@ -7,19 +7,19 @@ #pragma once -#include #include +#include namespace facebook::yoga { bool canUseCachedMeasurement( - YGMeasureMode widthMode, + MeasureMode widthMode, float availableWidth, - YGMeasureMode heightMode, + MeasureMode heightMode, float availableHeight, - YGMeasureMode lastWidthMode, + MeasureMode lastWidthMode, float lastAvailableWidth, - YGMeasureMode lastHeightMode, + MeasureMode lastHeightMode, float lastAvailableHeight, float lastComputedWidth, float lastComputedHeight, diff --git a/yoga/algorithm/CalculateLayout.cpp b/yoga/algorithm/CalculateLayout.cpp index a8cb925f5f..3f70ee3b06 100644 --- a/yoga/algorithm/CalculateLayout.cpp +++ b/yoga/algorithm/CalculateLayout.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -38,8 +39,8 @@ bool calculateLayoutInternal( const float availableWidth, const float availableHeight, const YGDirection ownerDirection, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode, + const MeasureMode widthMeasureMode, + const MeasureMode heightMeasureMode, const float ownerWidth, const float ownerHeight, const bool performLayout, @@ -98,22 +99,22 @@ static void constrainMaxSizeForMode( const enum YGFlexDirection axis, const float ownerAxisSize, const float ownerWidth, - YGMeasureMode* mode, + MeasureMode* mode, float* size) { const FloatOptional maxSize = yoga::resolveValue( node->getStyle().maxDimensions()[dimension(axis)], ownerAxisSize) + FloatOptional(node->getMarginForAxis(axis, ownerWidth)); switch (*mode) { - case YGMeasureModeExactly: - case YGMeasureModeAtMost: + case MeasureMode::Exactly: + case MeasureMode::AtMost: *size = (maxSize.isUndefined() || *size < maxSize.unwrap()) ? *size : maxSize.unwrap(); break; - case YGMeasureModeUndefined: + case MeasureMode::Undefined: if (!maxSize.isUndefined()) { - *mode = YGMeasureModeAtMost; + *mode = MeasureMode::AtMost; *size = maxSize.unwrap(); } break; @@ -124,11 +125,11 @@ static void computeFlexBasisForChild( const yoga::Node* const node, yoga::Node* const child, const float width, - const YGMeasureMode widthMode, + const MeasureMode widthMode, const float height, const float ownerWidth, const float ownerHeight, - const YGMeasureMode heightMode, + const MeasureMode heightMode, const YGDirection direction, LayoutData& layoutMarkerData, const uint32_t depth, @@ -141,8 +142,8 @@ static void computeFlexBasisForChild( float childWidth; float childHeight; - YGMeasureMode childWidthMeasureMode; - YGMeasureMode childHeightMeasureMode; + MeasureMode childWidthMeasureMode; + MeasureMode childHeightMeasureMode; const FloatOptional resolvedFlexBasis = yoga::resolveValue(child->resolveFlexBasisPtr(), mainAxisownerSize); @@ -154,7 +155,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)); @@ -183,8 +184,8 @@ static void computeFlexBasisForChild( // basis). childWidth = YGUndefined; childHeight = YGUndefined; - childWidthMeasureMode = YGMeasureModeUndefined; - childHeightMeasureMode = YGMeasureModeUndefined; + childWidthMeasureMode = MeasureMode::Undefined; + childHeightMeasureMode = MeasureMode::Undefined; auto marginRow = child->getMarginForAxis(YGFlexDirectionRow, ownerWidth).unwrap(); @@ -197,7 +198,7 @@ static void computeFlexBasisForChild( child->getResolvedDimensions()[YGDimensionWidth], ownerWidth) .unwrap() + marginRow; - childWidthMeasureMode = YGMeasureModeExactly; + childWidthMeasureMode = MeasureMode::Exactly; } if (isColumnStyleDimDefined) { childHeight = @@ -205,7 +206,7 @@ static void computeFlexBasisForChild( child->getResolvedDimensions()[YGDimensionHeight], ownerHeight) .unwrap() + marginColumn; - childHeightMeasureMode = YGMeasureModeExactly; + childHeightMeasureMode = MeasureMode::Exactly; } // The W3C spec doesn't say anything about the 'overflow' property, but all @@ -214,7 +215,7 @@ static void computeFlexBasisForChild( node->getStyle().overflow() != YGOverflowScroll) { if (yoga::isUndefined(childWidth) && !yoga::isUndefined(width)) { childWidth = width; - childWidthMeasureMode = YGMeasureModeAtMost; + childWidthMeasureMode = MeasureMode::AtMost; } } @@ -222,21 +223,21 @@ static void computeFlexBasisForChild( node->getStyle().overflow() != YGOverflowScroll) { if (yoga::isUndefined(childHeight) && !yoga::isUndefined(height)) { childHeight = height; - childHeightMeasureMode = YGMeasureModeAtMost; + childHeightMeasureMode = MeasureMode::AtMost; } } const auto& childStyle = child->getStyle(); if (!childStyle.aspectRatio().isUndefined()) { - if (!isMainAxisRow && childWidthMeasureMode == YGMeasureModeExactly) { + if (!isMainAxisRow && childWidthMeasureMode == MeasureMode::Exactly) { childHeight = marginColumn + (childWidth - marginRow) / childStyle.aspectRatio().unwrap(); - childHeightMeasureMode = YGMeasureModeExactly; + childHeightMeasureMode = MeasureMode::Exactly; } else if ( - isMainAxisRow && childHeightMeasureMode == YGMeasureModeExactly) { + isMainAxisRow && childHeightMeasureMode == MeasureMode::Exactly) { childWidth = marginRow + (childHeight - marginColumn) * childStyle.aspectRatio().unwrap(); - childWidthMeasureMode = YGMeasureModeExactly; + childWidthMeasureMode = MeasureMode::Exactly; } } @@ -244,35 +245,35 @@ static void computeFlexBasisForChild( // the cross axis to be measured exactly with the available inner width const bool hasExactWidth = - !yoga::isUndefined(width) && widthMode == YGMeasureModeExactly; + !yoga::isUndefined(width) && widthMode == MeasureMode::Exactly; const bool childWidthStretch = resolveChildAlignment(node, child) == YGAlignStretch && - childWidthMeasureMode != YGMeasureModeExactly; + childWidthMeasureMode != MeasureMode::Exactly; if (!isMainAxisRow && !isRowStyleDimDefined && hasExactWidth && childWidthStretch) { childWidth = width; - childWidthMeasureMode = YGMeasureModeExactly; + childWidthMeasureMode = MeasureMode::Exactly; if (!childStyle.aspectRatio().isUndefined()) { childHeight = (childWidth - marginRow) / childStyle.aspectRatio().unwrap(); - childHeightMeasureMode = YGMeasureModeExactly; + childHeightMeasureMode = MeasureMode::Exactly; } } const bool hasExactHeight = - !yoga::isUndefined(height) && heightMode == YGMeasureModeExactly; + !yoga::isUndefined(height) && heightMode == MeasureMode::Exactly; const bool childHeightStretch = resolveChildAlignment(node, child) == YGAlignStretch && - childHeightMeasureMode != YGMeasureModeExactly; + childHeightMeasureMode != MeasureMode::Exactly; if (isMainAxisRow && !isColumnStyleDimDefined && hasExactHeight && childHeightStretch) { childHeight = height; - childHeightMeasureMode = YGMeasureModeExactly; + childHeightMeasureMode = MeasureMode::Exactly; if (!childStyle.aspectRatio().isUndefined()) { childWidth = (childHeight - marginColumn) * childStyle.aspectRatio().unwrap(); - childWidthMeasureMode = YGMeasureModeExactly; + childWidthMeasureMode = MeasureMode::Exactly; } } @@ -318,7 +319,7 @@ static void layoutAbsoluteChild( const yoga::Node* const node, yoga::Node* const child, const float width, - const YGMeasureMode widthMode, + const MeasureMode widthMode, const float height, const YGDirection direction, LayoutData& layoutMarkerData, @@ -331,8 +332,8 @@ static void layoutAbsoluteChild( float childWidth = YGUndefined; float childHeight = YGUndefined; - YGMeasureMode childWidthMeasureMode = YGMeasureModeUndefined; - YGMeasureMode childHeightMeasureMode = YGMeasureModeUndefined; + MeasureMode childWidthMeasureMode = MeasureMode::Undefined; + MeasureMode childHeightMeasureMode = MeasureMode::Undefined; auto marginRow = child->getMarginForAxis(YGFlexDirectionRow, width).unwrap(); auto marginColumn = @@ -399,21 +400,21 @@ static void layoutAbsoluteChild( // If we're still missing one or the other dimension, measure the content. if (yoga::isUndefined(childWidth) || yoga::isUndefined(childHeight)) { childWidthMeasureMode = yoga::isUndefined(childWidth) - ? YGMeasureModeUndefined - : YGMeasureModeExactly; + ? MeasureMode::Undefined + : MeasureMode::Exactly; childHeightMeasureMode = yoga::isUndefined(childHeight) - ? YGMeasureModeUndefined - : YGMeasureModeExactly; + ? MeasureMode::Undefined + : MeasureMode::Exactly; // If the size of the owner is defined then try to constrain the absolute // child to that size as well. This allows text within the absolute child to // wrap to the size of its owner. This is the same behavior as many browsers // implement. if (!isMainAxisRow && yoga::isUndefined(childWidth) && - widthMode != YGMeasureModeUndefined && !yoga::isUndefined(width) && + widthMode != MeasureMode::Undefined && !yoga::isUndefined(width) && width > 0) { childWidth = width; - childWidthMeasureMode = YGMeasureModeAtMost; + childWidthMeasureMode = MeasureMode::AtMost; } calculateLayoutInternal( @@ -441,8 +442,8 @@ static void layoutAbsoluteChild( childWidth, childHeight, direction, - YGMeasureModeExactly, - YGMeasureModeExactly, + MeasureMode::Exactly, + MeasureMode::Exactly, childWidth, childHeight, true, @@ -479,7 +480,7 @@ static void layoutAbsoluteChild( leadingEdge(mainAxis)); } else if ( node->getConfig()->isExperimentalFeatureEnabled( - YGExperimentalFeatureAbsolutePercentageAgainstPaddingEdge) && + ExperimentalFeature::AbsolutePercentageAgainstPaddingEdge) && child->isLeadingPositionDefined(mainAxis)) { child->setLayoutPosition( child->getLeadingPosition( @@ -526,7 +527,7 @@ static void layoutAbsoluteChild( leadingEdge(crossAxis)); } else if ( node->getConfig()->isExperimentalFeatureEnabled( - YGExperimentalFeatureAbsolutePercentageAgainstPaddingEdge) && + ExperimentalFeature::AbsolutePercentageAgainstPaddingEdge) && child->isLeadingPositionDefined(crossAxis)) { child->setLayoutPosition( child->getLeadingPosition( @@ -547,8 +548,8 @@ static void measureNodeWithMeasureFunc( yoga::Node* const node, float availableWidth, float availableHeight, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode, + const MeasureMode widthMeasureMode, + const MeasureMode heightMeasureMode, const float ownerWidth, const float ownerHeight, LayoutData& layoutMarkerData, @@ -558,10 +559,10 @@ static void measureNodeWithMeasureFunc( node->hasMeasureFunc(), "Expected node to have custom measure function"); - if (widthMeasureMode == YGMeasureModeUndefined) { + if (widthMeasureMode == MeasureMode::Undefined) { availableWidth = YGUndefined; } - if (heightMeasureMode == YGMeasureModeUndefined) { + if (heightMeasureMode == MeasureMode::Undefined) { availableHeight = YGUndefined; } @@ -580,8 +581,8 @@ static void measureNodeWithMeasureFunc( ? availableHeight : yoga::maxOrDefined(0, availableHeight - paddingAndBorderAxisColumn); - if (widthMeasureMode == YGMeasureModeExactly && - heightMeasureMode == YGMeasureModeExactly) { + if (widthMeasureMode == MeasureMode::Exactly && + heightMeasureMode == MeasureMode::Exactly) { // Don't bother sizing the text if both dimensions are already defined. node->setLayoutMeasuredDimension( boundAxis( @@ -609,9 +610,9 @@ static void measureNodeWithMeasureFunc( Event::publish( node, {innerWidth, - widthMeasureMode, + unscopedEnum(widthMeasureMode), innerHeight, - heightMeasureMode, + unscopedEnum(heightMeasureMode), measuredSize.width, measuredSize.height, reason}); @@ -620,8 +621,8 @@ static void measureNodeWithMeasureFunc( boundAxis( node, YGFlexDirectionRow, - (widthMeasureMode == YGMeasureModeUndefined || - widthMeasureMode == YGMeasureModeAtMost) + (widthMeasureMode == MeasureMode::Undefined || + widthMeasureMode == MeasureMode::AtMost) ? measuredSize.width + paddingAndBorderAxisRow : availableWidth, ownerWidth, @@ -632,8 +633,8 @@ static void measureNodeWithMeasureFunc( boundAxis( node, YGFlexDirectionColumn, - (heightMeasureMode == YGMeasureModeUndefined || - heightMeasureMode == YGMeasureModeAtMost) + (heightMeasureMode == MeasureMode::Undefined || + heightMeasureMode == MeasureMode::AtMost) ? measuredSize.height + paddingAndBorderAxisColumn : availableHeight, ownerHeight, @@ -648,16 +649,16 @@ static void measureNodeWithoutChildren( yoga::Node* const node, const float availableWidth, const float availableHeight, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode, + const MeasureMode widthMeasureMode, + const MeasureMode heightMeasureMode, const float ownerWidth, const float ownerHeight) { const auto& padding = node->getLayout().padding; const auto& border = node->getLayout().border; float width = availableWidth; - if (widthMeasureMode == YGMeasureModeUndefined || - widthMeasureMode == YGMeasureModeAtMost) { + if (widthMeasureMode == MeasureMode::Undefined || + widthMeasureMode == MeasureMode::AtMost) { width = padding[YGEdgeLeft] + padding[YGEdgeRight] + border[YGEdgeLeft] + border[YGEdgeRight]; } @@ -666,8 +667,8 @@ static void measureNodeWithoutChildren( YGDimensionWidth); float height = availableHeight; - if (heightMeasureMode == YGMeasureModeUndefined || - heightMeasureMode == YGMeasureModeAtMost) { + if (heightMeasureMode == MeasureMode::Undefined || + heightMeasureMode == MeasureMode::AtMost) { height = padding[YGEdgeTop] + padding[YGEdgeBottom] + border[YGEdgeTop] + border[YGEdgeBottom]; } @@ -680,22 +681,22 @@ static bool measureNodeWithFixedSize( yoga::Node* const node, const float availableWidth, const float availableHeight, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode, + const MeasureMode widthMeasureMode, + const MeasureMode heightMeasureMode, const float ownerWidth, const float ownerHeight) { if ((!yoga::isUndefined(availableWidth) && - widthMeasureMode == YGMeasureModeAtMost && availableWidth <= 0.0f) || + widthMeasureMode == MeasureMode::AtMost && availableWidth <= 0.0f) || (!yoga::isUndefined(availableHeight) && - heightMeasureMode == YGMeasureModeAtMost && availableHeight <= 0.0f) || - (widthMeasureMode == YGMeasureModeExactly && - heightMeasureMode == YGMeasureModeExactly)) { + heightMeasureMode == MeasureMode::AtMost && availableHeight <= 0.0f) || + (widthMeasureMode == MeasureMode::Exactly && + heightMeasureMode == MeasureMode::Exactly)) { node->setLayoutMeasuredDimension( boundAxis( node, YGFlexDirectionRow, yoga::isUndefined(availableWidth) || - (widthMeasureMode == YGMeasureModeAtMost && + (widthMeasureMode == MeasureMode::AtMost && availableWidth < 0.0f) ? 0.0f : availableWidth, @@ -708,7 +709,7 @@ static bool measureNodeWithFixedSize( node, YGFlexDirectionColumn, yoga::isUndefined(availableHeight) || - (heightMeasureMode == YGMeasureModeAtMost && + (heightMeasureMode == MeasureMode::AtMost && availableHeight < 0.0f) ? 0.0f : availableHeight, @@ -768,8 +769,8 @@ static float computeFlexBasisForChildren( yoga::Node* const node, const float availableInnerWidth, const float availableInnerHeight, - YGMeasureMode widthMeasureMode, - YGMeasureMode heightMeasureMode, + MeasureMode widthMeasureMode, + MeasureMode heightMeasureMode, YGDirection direction, YGFlexDirection mainAxis, bool performLayout, @@ -779,12 +780,12 @@ static float computeFlexBasisForChildren( float totalOuterFlexBasis = 0.0f; YGNodeRef singleFlexChild = nullptr; const auto& children = node->getChildren(); - YGMeasureMode measureModeMainDim = + MeasureMode measureModeMainDim = isRow(mainAxis) ? widthMeasureMode : heightMeasureMode; // If there is only one child with flexGrow + flexShrink it means we can set // the computedFlexBasis to 0 instead of measuring and shrinking / flexing the // child to exactly match the remaining space - if (measureModeMainDim == YGMeasureModeExactly) { + if (measureModeMainDim == MeasureMode::Exactly) { for (auto child : children) { if (child->isNodeFlexible()) { if (singleFlexChild != nullptr || @@ -866,7 +867,7 @@ static float distributeFreeSpaceSecondPass( const float availableInnerWidth, const float availableInnerHeight, const bool mainAxisOverflows, - const YGMeasureMode measureModeCrossDim, + const MeasureMode measureModeCrossDim, const bool performLayout, LayoutData& layoutMarkerData, const uint32_t depth, @@ -941,34 +942,34 @@ static float distributeFreeSpaceSecondPass( float childCrossSize; float childMainSize = updatedMainSize + marginMain; - YGMeasureMode childCrossMeasureMode; - YGMeasureMode childMainMeasureMode = YGMeasureModeExactly; + MeasureMode childCrossMeasureMode; + MeasureMode childMainMeasureMode = MeasureMode::Exactly; const auto& childStyle = currentLineChild->getStyle(); if (!childStyle.aspectRatio().isUndefined()) { childCrossSize = isMainAxisRow ? (childMainSize - marginMain) / childStyle.aspectRatio().unwrap() : (childMainSize - marginMain) * childStyle.aspectRatio().unwrap(); - childCrossMeasureMode = YGMeasureModeExactly; + childCrossMeasureMode = MeasureMode::Exactly; childCrossSize += marginCross; } else if ( !std::isnan(availableInnerCrossDim) && !styleDefinesDimension( currentLineChild, crossAxis, availableInnerCrossDim) && - measureModeCrossDim == YGMeasureModeExactly && + measureModeCrossDim == MeasureMode::Exactly && !(isNodeFlexWrap && mainAxisOverflows) && resolveChildAlignment(node, currentLineChild) == YGAlignStretch && currentLineChild->marginLeadingValue(crossAxis).unit != YGUnitAuto && currentLineChild->marginTrailingValue(crossAxis).unit != YGUnitAuto) { childCrossSize = availableInnerCrossDim; - childCrossMeasureMode = YGMeasureModeExactly; + childCrossMeasureMode = MeasureMode::Exactly; } else if (!styleDefinesDimension( currentLineChild, crossAxis, availableInnerCrossDim)) { childCrossSize = availableInnerCrossDim; childCrossMeasureMode = yoga::isUndefined(childCrossSize) - ? YGMeasureModeUndefined - : YGMeasureModeAtMost; + ? MeasureMode::Undefined + : MeasureMode::AtMost; } else { childCrossSize = yoga::resolveValue( @@ -979,11 +980,11 @@ static float distributeFreeSpaceSecondPass( const bool isLoosePercentageMeasurement = currentLineChild->getResolvedDimension(dimension(crossAxis)).unit == YGUnitPercent && - measureModeCrossDim != YGMeasureModeExactly; + measureModeCrossDim != MeasureMode::Exactly; childCrossMeasureMode = yoga::isUndefined(childCrossSize) || isLoosePercentageMeasurement - ? YGMeasureModeUndefined - : YGMeasureModeExactly; + ? MeasureMode::Undefined + : MeasureMode::Exactly; } constrainMaxSizeForMode( @@ -1011,9 +1012,9 @@ static float distributeFreeSpaceSecondPass( const float childWidth = isMainAxisRow ? childMainSize : childCrossSize; const float childHeight = !isMainAxisRow ? childMainSize : childCrossSize; - const YGMeasureMode childWidthMeasureMode = + const MeasureMode childWidthMeasureMode = isMainAxisRow ? childMainMeasureMode : childCrossMeasureMode; - const YGMeasureMode childHeightMeasureMode = + const MeasureMode childHeightMeasureMode = !isMainAxisRow ? childMainMeasureMode : childCrossMeasureMode; const bool isLayoutPass = performLayout && !requiresStretchLayout; @@ -1160,7 +1161,7 @@ static void resolveFlexibleLength( const float availableInnerWidth, const float availableInnerHeight, const bool mainAxisOverflows, - const YGMeasureMode measureModeCrossDim, + const MeasureMode measureModeCrossDim, const bool performLayout, LayoutData& layoutMarkerData, const uint32_t depth, @@ -1201,8 +1202,8 @@ static void YGJustifyMainAxis( const size_t startOfLineIndex, const YGFlexDirection mainAxis, const YGFlexDirection crossAxis, - const YGMeasureMode measureModeMainDim, - const YGMeasureMode measureModeCrossDim, + const MeasureMode measureModeMainDim, + const MeasureMode measureModeCrossDim, const float mainAxisownerSize, const float ownerWidth, const float availableInnerMainDim, @@ -1217,7 +1218,7 @@ static void YGJustifyMainAxis( const float gap = node->getGapForAxis(mainAxis, ownerWidth).unwrap(); // If we are using "at most" rules in the main axis, make sure that // remainingFreeSpace is 0 when min main dimension is not given - if (measureModeMainDim == YGMeasureModeAtMost && + if (measureModeMainDim == MeasureMode::AtMost && flexLine.layout.remainingFreeSpace > 0) { if (!style.minDimensions()[dimension(mainAxis)].isUndefined() && !yoga::resolveValue( @@ -1348,7 +1349,7 @@ static void YGJustifyMainAxis( static_cast(numberOfAutoMarginsOnCurrentLine); } bool canSkipFlex = - !performLayout && measureModeCrossDim == YGMeasureModeExactly; + !performLayout && measureModeCrossDim == MeasureMode::Exactly; if (canSkipFlex) { // If we skipped the flex step, then we can't rely on the measuredDims // because they weren't computed. This means we can't call @@ -1465,21 +1466,21 @@ static void YGJustifyMainAxis( // content" because we don't support default minimum main sizes (see above // for details). Each of our measure modes maps to a layout mode from the // spec (https://www.w3.org/TR/CSS3-sizing/#terms): -// - YGMeasureModeUndefined: max content -// - YGMeasureModeExactly: fill available -// - YGMeasureModeAtMost: fit content +// - MeasureMode::Undefined: max content +// - MeasureMode::Exactly: fill available +// - MeasureMode::AtMost: fit content // // When calling calculateLayoutImpl and calculateLayoutInternal, if the // caller passes an available size of undefined then it must also pass a -// measure mode of YGMeasureModeUndefined in that dimension. +// measure mode of MeasureMode::Undefined in that dimension. // static void calculateLayoutImpl( yoga::Node* const node, const float availableWidth, const float availableHeight, const YGDirection ownerDirection, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode, + const MeasureMode widthMeasureMode, + const MeasureMode heightMeasureMode, const float ownerWidth, const float ownerHeight, const bool performLayout, @@ -1490,17 +1491,17 @@ static void calculateLayoutImpl( yoga::assertFatalWithNode( node, yoga::isUndefined(availableWidth) - ? widthMeasureMode == YGMeasureModeUndefined + ? widthMeasureMode == MeasureMode::Undefined : true, "availableWidth is indefinite so widthMeasureMode must be " - "YGMeasureModeUndefined"); + "MeasureMode::Undefined"); yoga::assertFatalWithNode( node, yoga::isUndefined(availableHeight) - ? heightMeasureMode == YGMeasureModeUndefined + ? heightMeasureMode == MeasureMode::Undefined : true, "availableHeight is indefinite so heightMeasureMode must be " - "YGMeasureModeUndefined"); + "MeasureMode::Undefined"); (performLayout ? layoutMarkerData.layouts : layoutMarkerData.measures) += 1; @@ -1617,9 +1618,9 @@ static void calculateLayoutImpl( const float paddingAndBorderAxisCross = leadingPaddingAndBorderCross + trailingPaddingAndBorderCross; - YGMeasureMode measureModeMainDim = + MeasureMode measureModeMainDim = isMainAxisRow ? widthMeasureMode : heightMeasureMode; - YGMeasureMode measureModeCrossDim = + MeasureMode measureModeCrossDim = isMainAxisRow ? heightMeasureMode : widthMeasureMode; const float paddingAndBorderAxisRow = @@ -1671,12 +1672,12 @@ static void calculateLayoutImpl( } const bool mainAxisOverflows = - (measureModeMainDim != YGMeasureModeUndefined) && + (measureModeMainDim != MeasureMode::Undefined) && totalMainDim > availableInnerMainDim; if (isNodeFlexWrap && mainAxisOverflows && - measureModeMainDim == YGMeasureModeAtMost) { - measureModeMainDim = YGMeasureModeExactly; + measureModeMainDim == MeasureMode::AtMost) { + measureModeMainDim = MeasureMode::Exactly; } // STEP 4: COLLECT FLEX ITEMS INTO FLEX LINES @@ -1711,7 +1712,7 @@ static void calculateLayoutImpl( // If we don't need to measure the cross axis, we can skip the entire flex // step. const bool canSkipFlex = - !performLayout && measureModeCrossDim == YGMeasureModeExactly; + !performLayout && measureModeCrossDim == MeasureMode::Exactly; // STEP 5: RESOLVING FLEXIBLE LENGTHS ON MAIN AXIS // Calculate the remaining available space that needs to be allocated. If @@ -1721,7 +1722,7 @@ static void calculateLayoutImpl( bool sizeBasedOnContent = false; // If we don't measure with exact main dimension we want to ensure we don't // violate min and max - if (measureModeMainDim != YGMeasureModeExactly) { + if (measureModeMainDim != MeasureMode::Exactly) { const auto& minDimensions = node->getStyle().minDimensions(); const auto& maxDimensions = node->getStyle().maxDimensions(); const float minInnerWidth = @@ -1755,7 +1756,7 @@ static void calculateLayoutImpl( availableInnerMainDim = maxInnerMainDim; } else { bool useLegacyStretchBehaviour = - node->hasErrata(YGErrataStretchFlexBasis); + node->hasErrata(Errata::StretchFlexBasis); if (!useLegacyStretchBehaviour && ((!yoga::isUndefined(flexLine.layout.totalFlexGrowFactors) && @@ -1829,8 +1830,8 @@ static void calculateLayoutImpl( performLayout); float containerCrossAxis = availableInnerCrossDim; - if (measureModeCrossDim == YGMeasureModeUndefined || - measureModeCrossDim == YGMeasureModeAtMost) { + if (measureModeCrossDim == MeasureMode::Undefined || + measureModeCrossDim == MeasureMode::AtMost) { // Compute the cross axis from the max cross dimension of the children. containerCrossAxis = boundAxis( @@ -1843,7 +1844,7 @@ static void calculateLayoutImpl( } // If there's no flex wrap, the cross dimension is defined by the container. - if (!isNodeFlexWrap && measureModeCrossDim == YGMeasureModeExactly) { + if (!isNodeFlexWrap && measureModeCrossDim == MeasureMode::Exactly) { flexLine.layout.crossDim = availableInnerCrossDim; } @@ -1924,8 +1925,8 @@ static void calculateLayoutImpl( child->getMarginForAxis(mainAxis, availableInnerWidth) .unwrap(); - YGMeasureMode childMainMeasureMode = YGMeasureModeExactly; - YGMeasureMode childCrossMeasureMode = YGMeasureModeExactly; + MeasureMode childMainMeasureMode = MeasureMode::Exactly; + MeasureMode childCrossMeasureMode = MeasureMode::Exactly; constrainMaxSizeForMode( child, mainAxis, @@ -1949,16 +1950,16 @@ static void calculateLayoutImpl( auto alignContent = node->getStyle().alignContent(); auto crossAxisDoesNotGrow = alignContent != YGAlignStretch && isNodeFlexWrap; - const YGMeasureMode childWidthMeasureMode = + const MeasureMode childWidthMeasureMode = yoga::isUndefined(childWidth) || (!isMainAxisRow && crossAxisDoesNotGrow) - ? YGMeasureModeUndefined - : YGMeasureModeExactly; - const YGMeasureMode childHeightMeasureMode = + ? MeasureMode::Undefined + : MeasureMode::Exactly; + const MeasureMode childHeightMeasureMode = yoga::isUndefined(childHeight) || (isMainAxisRow && crossAxisDoesNotGrow) - ? YGMeasureModeUndefined - : YGMeasureModeExactly; + ? MeasureMode::Undefined + : MeasureMode::Exactly; calculateLayoutInternal( child, @@ -2181,8 +2182,8 @@ static void calculateLayoutImpl( childWidth, childHeight, direction, - YGMeasureModeExactly, - YGMeasureModeExactly, + MeasureMode::Exactly, + MeasureMode::Exactly, availableInnerWidth, availableInnerHeight, true, @@ -2240,9 +2241,9 @@ static void calculateLayoutImpl( // If the user didn't specify a width or height for the node, set the // dimensions based on the children. - if (measureModeMainDim == YGMeasureModeUndefined || + if (measureModeMainDim == MeasureMode::Undefined || (node->getStyle().overflow() != YGOverflowScroll && - measureModeMainDim == YGMeasureModeAtMost)) { + measureModeMainDim == MeasureMode::AtMost)) { // Clamp the size to the min/max size, if specified, and make sure it // doesn't go below the padding and border amount. node->setLayoutMeasuredDimension( @@ -2251,7 +2252,7 @@ static void calculateLayoutImpl( dimension(mainAxis)); } else if ( - measureModeMainDim == YGMeasureModeAtMost && + measureModeMainDim == MeasureMode::AtMost && node->getStyle().overflow() == YGOverflowScroll) { node->setLayoutMeasuredDimension( yoga::maxOrDefined( @@ -2267,9 +2268,9 @@ static void calculateLayoutImpl( dimension(mainAxis)); } - if (measureModeCrossDim == YGMeasureModeUndefined || + if (measureModeCrossDim == MeasureMode::Undefined || (node->getStyle().overflow() != YGOverflowScroll && - measureModeCrossDim == YGMeasureModeAtMost)) { + measureModeCrossDim == MeasureMode::AtMost)) { // Clamp the size to the min/max size, if specified, and make sure it // doesn't go below the padding and border amount. node->setLayoutMeasuredDimension( @@ -2282,7 +2283,7 @@ static void calculateLayoutImpl( dimension(crossAxis)); } else if ( - measureModeCrossDim == YGMeasureModeAtMost && + measureModeCrossDim == MeasureMode::AtMost && node->getStyle().overflow() == YGOverflowScroll) { node->setLayoutMeasuredDimension( yoga::maxOrDefined( @@ -2323,7 +2324,7 @@ static void calculateLayoutImpl( } const bool absolutePercentageAgainstPaddingEdge = node->getConfig()->isExperimentalFeatureEnabled( - YGExperimentalFeatureAbsolutePercentageAgainstPaddingEdge); + ExperimentalFeature::AbsolutePercentageAgainstPaddingEdge); layoutAbsoluteChild( node, @@ -2382,18 +2383,17 @@ static const char* spacerWithLength(const unsigned long level) { } static const char* measureModeName( - const YGMeasureMode mode, + const MeasureMode mode, const bool performLayout) { - constexpr auto N = enums::count(); - const char* kMeasureModeNames[N] = {"UNDEFINED", "EXACTLY", "AT_MOST"}; - const char* kLayoutModeNames[N] = { - "LAY_UNDEFINED", "LAY_EXACTLY", "LAY_AT_MOST"}; - - if (mode >= N) { - return ""; + switch (mode) { + case MeasureMode::Undefined: + return performLayout ? "LAY_UNDEFINED" : "UNDEFINED"; + case MeasureMode::Exactly: + return performLayout ? "LAY_EXACTLY" : "EXACTLY"; + case MeasureMode::AtMost: + return performLayout ? "LAY_AT_MOST" : "AT_MOST"; } - - return performLayout ? kLayoutModeNames[mode] : kMeasureModeNames[mode]; + return ""; } // @@ -2409,8 +2409,8 @@ bool calculateLayoutInternal( const float availableWidth, const float availableHeight, const YGDirection ownerDirection, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode, + const MeasureMode widthMeasureMode, + const MeasureMode heightMeasureMode, const float ownerWidth, const float ownerHeight, const bool performLayout, @@ -2431,8 +2431,8 @@ bool calculateLayoutInternal( layout->nextCachedMeasurementsIndex = 0; layout->cachedLayout.availableWidth = -1; layout->cachedLayout.availableHeight = -1; - layout->cachedLayout.widthMeasureMode = YGMeasureModeUndefined; - layout->cachedLayout.heightMeasureMode = YGMeasureModeUndefined; + layout->cachedLayout.widthMeasureMode = MeasureMode::Undefined; + layout->cachedLayout.heightMeasureMode = MeasureMode::Undefined; layout->cachedLayout.computedWidth = -1; layout->cachedLayout.computedHeight = -1; } @@ -2526,14 +2526,14 @@ bool calculateLayoutInternal( if (gPrintChanges && gPrintSkips) { yoga::log( node, - YGLogLevelVerbose, + LogLevel::Verbose, "%s%d.{[skipped] ", spacerWithLength(depth), depth); node->print(); yoga::log( node, - YGLogLevelVerbose, + LogLevel::Verbose, "wm: %s, hm: %s, aw: %f ah: %f => d: (%f, %f) %s\n", measureModeName(widthMeasureMode, performLayout), measureModeName(heightMeasureMode, performLayout), @@ -2547,7 +2547,7 @@ bool calculateLayoutInternal( if (gPrintChanges) { yoga::log( node, - YGLogLevelVerbose, + LogLevel::Verbose, "%s%d.{%s", spacerWithLength(depth), depth, @@ -2555,7 +2555,7 @@ bool calculateLayoutInternal( node->print(); yoga::log( node, - YGLogLevelVerbose, + LogLevel::Verbose, "wm: %s, hm: %s, aw: %f ah: %f %s\n", measureModeName(widthMeasureMode, performLayout), measureModeName(heightMeasureMode, performLayout), @@ -2582,7 +2582,7 @@ bool calculateLayoutInternal( if (gPrintChanges) { yoga::log( node, - YGLogLevelVerbose, + LogLevel::Verbose, "%s%d.}%s", spacerWithLength(depth), depth, @@ -2590,7 +2590,7 @@ bool calculateLayoutInternal( node->print(); yoga::log( node, - YGLogLevelVerbose, + LogLevel::Verbose, "wm: %s, hm: %s, d: (%f, %f) %s\n", measureModeName(widthMeasureMode, performLayout), measureModeName(heightMeasureMode, performLayout), @@ -2609,7 +2609,7 @@ bool calculateLayoutInternal( if (layout->nextCachedMeasurementsIndex == LayoutResults::MaxCachedMeasurements) { if (gPrintChanges) { - yoga::log(node, YGLogLevelVerbose, "Out of cache entries!\n"); + yoga::log(node, LogLevel::Verbose, "Out of cache entries!\n"); } layout->nextCachedMeasurementsIndex = 0; } @@ -2678,7 +2678,7 @@ void calculateLayout( gCurrentGenerationCount.fetch_add(1, std::memory_order_relaxed); node->resolveDimension(); float width = YGUndefined; - YGMeasureMode widthMeasureMode = YGMeasureModeUndefined; + MeasureMode widthMeasureMode = MeasureMode::Undefined; const auto& maxDimensions = node->getStyle().maxDimensions(); if (styleDefinesDimension(node, YGFlexDirectionRow, ownerWidth)) { width = (yoga::resolveValue( @@ -2686,36 +2686,36 @@ void calculateLayout( ownerWidth) + node->getMarginForAxis(YGFlexDirectionRow, ownerWidth)) .unwrap(); - widthMeasureMode = YGMeasureModeExactly; + widthMeasureMode = MeasureMode::Exactly; } else if (!yoga::resolveValue(maxDimensions[YGDimensionWidth], ownerWidth) .isUndefined()) { width = yoga::resolveValue(maxDimensions[YGDimensionWidth], ownerWidth) .unwrap(); - widthMeasureMode = YGMeasureModeAtMost; + widthMeasureMode = MeasureMode::AtMost; } else { width = ownerWidth; - widthMeasureMode = yoga::isUndefined(width) ? YGMeasureModeUndefined - : YGMeasureModeExactly; + widthMeasureMode = yoga::isUndefined(width) ? MeasureMode::Undefined + : MeasureMode::Exactly; } float height = YGUndefined; - YGMeasureMode heightMeasureMode = YGMeasureModeUndefined; + MeasureMode heightMeasureMode = MeasureMode::Undefined; if (styleDefinesDimension(node, YGFlexDirectionColumn, ownerHeight)) { height = (yoga::resolveValue( node->getResolvedDimension(dimension(YGFlexDirectionColumn)), ownerHeight) + node->getMarginForAxis(YGFlexDirectionColumn, ownerWidth)) .unwrap(); - heightMeasureMode = YGMeasureModeExactly; + heightMeasureMode = MeasureMode::Exactly; } else if (!yoga::resolveValue(maxDimensions[YGDimensionHeight], ownerHeight) .isUndefined()) { height = yoga::resolveValue(maxDimensions[YGDimensionHeight], ownerHeight) .unwrap(); - heightMeasureMode = YGMeasureModeAtMost; + heightMeasureMode = MeasureMode::AtMost; } else { height = ownerHeight; - heightMeasureMode = yoga::isUndefined(height) ? YGMeasureModeUndefined - : YGMeasureModeExactly; + heightMeasureMode = yoga::isUndefined(height) ? MeasureMode::Undefined + : MeasureMode::Exactly; } if (calculateLayoutInternal( node, @@ -2737,9 +2737,9 @@ void calculateLayout( #ifdef DEBUG if (node->getConfig()->shouldPrintTree()) { - YGNodePrint( + yoga::print( node, - (YGPrintOptions)(YGPrintOptionsLayout | YGPrintOptionsChildren | YGPrintOptionsStyle)); + PrintOptions::Layout | PrintOptions::Children | PrintOptions::Style); } #endif } diff --git a/yoga/algorithm/PixelGrid.cpp b/yoga/algorithm/PixelGrid.cpp index a9594b24d1..dce87347f5 100644 --- a/yoga/algorithm/PixelGrid.cpp +++ b/yoga/algorithm/PixelGrid.cpp @@ -83,7 +83,7 @@ void roundLayoutResultsToPixelGrid( if (pointScaleFactor != 0.0f) { // If a node has a custom measure function we never want to round down its // size as this could lead to unwanted text truncation. - const bool textRounding = node->getNodeType() == YGNodeTypeText; + const bool textRounding = node->getNodeType() == NodeType::Text; node->setLayoutPosition( roundValueToPixelGrid(nodeLeft, pointScaleFactor, false, textRounding), diff --git a/yoga/config/Config.cpp b/yoga/config/Config.cpp index e86c76017b..721588ee0a 100644 --- a/yoga/config/Config.cpp +++ b/yoga/config/Config.cpp @@ -41,37 +41,37 @@ 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_; } -void Config::setErrata(YGErrata errata) { +void Config::setErrata(Errata errata) { errata_ = errata; } -void Config::addErrata(YGErrata errata) { +void Config::addErrata(Errata errata) { errata_ |= errata; } -void Config::removeErrata(YGErrata errata) { +void Config::removeErrata(Errata errata) { errata_ &= (~errata); } -YGErrata Config::getErrata() const { +Errata Config::getErrata() const { return errata_; } -bool Config::hasErrata(YGErrata errata) const { - return (errata_ & errata) != YGErrataNone; +bool Config::hasErrata(Errata errata) const { + return (errata_ & errata) != Errata::None; } void Config::setPointScaleFactor(float pointScaleFactor) { @@ -96,10 +96,10 @@ void Config::setLogger(YGLogger logger) { void Config::log( const yoga::Node* node, - YGLogLevel logLevel, + LogLevel logLevel, const char* format, va_list args) const { - logger_(this, node, logLevel, format, args); + logger_(this, node, unscopedEnum(logLevel), format, args); } void Config::setCloneNodeCallback(YGCloneNodeFunc cloneNode) { diff --git a/yoga/config/Config.h b/yoga/config/Config.h index 4590fcdf2f..227aa598bf 100644 --- a/yoga/config/Config.h +++ b/yoga/config/Config.h @@ -7,8 +7,12 @@ #pragma once +#include + #include -#include +#include +#include +#include // Tag struct used to form the opaque YGConfigRef for the public C API struct YGConfig {}; @@ -18,6 +22,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,17 +49,15 @@ 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); - void removeErrata(YGErrata errata); - YGErrata getErrata() const; - bool hasErrata(YGErrata errata) const; + void setErrata(Errata errata); + void addErrata(Errata errata); + void removeErrata(Errata errata); + Errata getErrata() const; + bool hasErrata(Errata errata) const; void setPointScaleFactor(float pointScaleFactor); float getPointScaleFactor() const; @@ -64,7 +68,7 @@ class YG_EXPORT Config : public ::YGConfig { void setLogger(YGLogger logger); void log( const yoga::Node* node, - YGLogLevel logLevel, + LogLevel logLevel, const char* format, va_list args) const; @@ -79,8 +83,8 @@ class YG_EXPORT Config : public ::YGConfig { YGLogger logger_; ConfigFlags flags_{}; - EnumBitset experimentalFeatures_{}; - YGErrata errata_ = YGErrataNone; + ExperimentalFeatureSet experimentalFeatures_{}; + Errata errata_ = Errata::None; float pointScaleFactor_ = 1.0f; void* context_ = nullptr; }; diff --git a/yoga/debug/AssertFatal.cpp b/yoga/debug/AssertFatal.cpp index d83c8cc56e..2388a74453 100644 --- a/yoga/debug/AssertFatal.cpp +++ b/yoga/debug/AssertFatal.cpp @@ -22,7 +22,7 @@ namespace facebook::yoga { void assertFatal(const bool condition, const char* message) { if (!condition) { - yoga::log(YGLogLevelFatal, "%s\n", message); + yoga::log(LogLevel::Fatal, "%s\n", message); fatalWithMessage(message); } } @@ -32,7 +32,7 @@ void assertFatalWithNode( const bool condition, const char* message) { if (!condition) { - yoga::log(node, YGLogLevelFatal, "%s\n", message); + yoga::log(node, LogLevel::Fatal, "%s\n", message); fatalWithMessage(message); } } @@ -42,7 +42,7 @@ void assertFatalWithConfig( const bool condition, const char* message) { if (!condition) { - yoga::log(config, YGLogLevelFatal, "%s\n", message); + yoga::log(config, LogLevel::Fatal, "%s\n", message); fatalWithMessage(message); } } diff --git a/yoga/debug/Log.cpp b/yoga/debug/Log.cpp index c16547ae34..9b525d8a8e 100644 --- a/yoga/debug/Log.cpp +++ b/yoga/debug/Log.cpp @@ -18,18 +18,18 @@ namespace { void vlog( const yoga::Config* config, const yoga::Node* node, - YGLogLevel level, + LogLevel level, const char* format, va_list args) { if (config == nullptr) { - getDefaultLogger()(nullptr, node, level, format, args); + getDefaultLogger()(nullptr, node, unscopedEnum(level), format, args); } else { config->log(node, level, format, args); } } } // namespace -void log(YGLogLevel level, const char* format, ...) noexcept { +void log(LogLevel level, const char* format, ...) noexcept { va_list args; va_start(args, format); vlog(nullptr, nullptr, level, format, args); @@ -38,7 +38,7 @@ void log(YGLogLevel level, const char* format, ...) noexcept { void log( const yoga::Node* node, - YGLogLevel level, + LogLevel level, const char* format, ...) noexcept { va_list args; @@ -50,7 +50,7 @@ void log( void log( const yoga::Config* config, - YGLogLevel level, + LogLevel level, const char* format, ...) noexcept { va_list args; diff --git a/yoga/debug/Log.h b/yoga/debug/Log.h index 59a8b2e0db..94dc0e0ced 100644 --- a/yoga/debug/Log.h +++ b/yoga/debug/Log.h @@ -9,23 +9,23 @@ #include -#include #include +#include #include namespace facebook::yoga { -void log(YGLogLevel level, const char* format, ...) noexcept; +void log(LogLevel level, const char* format, ...) noexcept; void log( const yoga::Node* node, - YGLogLevel level, + LogLevel level, const char* message, ...) noexcept; void log( const yoga::Config* config, - YGLogLevel level, + LogLevel level, const char* format, ...) noexcept; diff --git a/yoga/debug/NodeToString.cpp b/yoga/debug/NodeToString.cpp index 787ec976e4..8b60c5fdd4 100644 --- a/yoga/debug/NodeToString.cpp +++ b/yoga/debug/NodeToString.cpp @@ -11,6 +11,7 @@ #include +#include #include #include @@ -118,12 +119,12 @@ static void appendEdgeIfNotUndefined( void nodeToString( std::string& str, const yoga::Node* node, - YGPrintOptions options, + PrintOptions options, uint32_t level) { indent(str, level); appendFormattedString(str, "
getLayout().dimensions[YGDimensionWidth]); @@ -136,7 +137,7 @@ void nodeToString( appendFormattedString(str, "\" "); } - if (options & YGPrintOptionsStyle) { + if ((options & PrintOptions::Style) == PrintOptions::Style) { appendFormattedString(str, "style=\""); const auto& style = node->getStyle(); if (style.flexDirection() != yoga::Node{}.getStyle().flexDirection()) { @@ -228,7 +229,8 @@ void nodeToString( appendFormattedString(str, ">"); const size_t childCount = node->getChildCount(); - if (options & YGPrintOptionsChildren && childCount > 0) { + if ((options & PrintOptions::Children) == PrintOptions::Children && + childCount > 0) { for (size_t i = 0; i < childCount; i++) { appendFormattedString(str, "\n"); nodeToString(str, node->getChild(i), options, level + 1); @@ -239,5 +241,11 @@ void nodeToString( appendFormattedString(str, "
"); } +void print(const yoga::Node* node, PrintOptions options) { + std::string str; + yoga::nodeToString(str, node, options, 0); + yoga::log(node, LogLevel::Debug, str.c_str()); +} + } // namespace facebook::yoga #endif diff --git a/yoga/debug/NodeToString.h b/yoga/debug/NodeToString.h index 07f5941ab2..268d5ac927 100644 --- a/yoga/debug/NodeToString.h +++ b/yoga/debug/NodeToString.h @@ -11,7 +11,7 @@ #include -#include +#include #include namespace facebook::yoga { @@ -19,9 +19,11 @@ namespace facebook::yoga { void nodeToString( std::string& str, const yoga::Node* node, - YGPrintOptions options, + PrintOptions options, uint32_t level); +void print(const yoga::Node* node, PrintOptions options); + } // namespace facebook::yoga #endif diff --git a/yoga/enums/Align.h b/yoga/enums/Align.h new file mode 100644 index 0000000000..95df12a790 --- /dev/null +++ b/yoga/enums/Align.h @@ -0,0 +1,51 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class Align : uint8_t { + Auto = YGAlignAuto, + FlexStart = YGAlignFlexStart, + Center = YGAlignCenter, + FlexEnd = YGAlignFlexEnd, + Stretch = YGAlignStretch, + Baseline = YGAlignBaseline, + SpaceBetween = YGAlignSpaceBetween, + SpaceAround = YGAlignSpaceAround, +}; + +template <> +constexpr inline int32_t ordinalCount() { + return 8; +} + +template <> +constexpr inline int32_t bitCount() { + return 3; +} + +constexpr inline Align scopedEnum(YGAlign unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGAlign unscopedEnum(Align scoped) { + return static_cast(scoped); +} + +inline const char* toString(Align e) { + return YGAlignToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/Dimension.h b/yoga/enums/Dimension.h new file mode 100644 index 0000000000..2274a980a5 --- /dev/null +++ b/yoga/enums/Dimension.h @@ -0,0 +1,45 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class Dimension : uint8_t { + Width = YGDimensionWidth, + Height = YGDimensionHeight, +}; + +template <> +constexpr inline int32_t ordinalCount() { + return 2; +} + +template <> +constexpr inline int32_t bitCount() { + return 1; +} + +constexpr inline Dimension scopedEnum(YGDimension unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGDimension unscopedEnum(Dimension scoped) { + return static_cast(scoped); +} + +inline const char* toString(Dimension e) { + return YGDimensionToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/Direction.h b/yoga/enums/Direction.h new file mode 100644 index 0000000000..1b5e19c93c --- /dev/null +++ b/yoga/enums/Direction.h @@ -0,0 +1,46 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class Direction : uint8_t { + Inherit = YGDirectionInherit, + LTR = YGDirectionLTR, + RTL = YGDirectionRTL, +}; + +template <> +constexpr inline int32_t ordinalCount() { + return 3; +} + +template <> +constexpr inline int32_t bitCount() { + return 2; +} + +constexpr inline Direction scopedEnum(YGDirection unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGDirection unscopedEnum(Direction scoped) { + return static_cast(scoped); +} + +inline const char* toString(Direction e) { + return YGDirectionToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/Display.h b/yoga/enums/Display.h new file mode 100644 index 0000000000..c1d8c7a82e --- /dev/null +++ b/yoga/enums/Display.h @@ -0,0 +1,45 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class Display : uint8_t { + Flex = YGDisplayFlex, + None = YGDisplayNone, +}; + +template <> +constexpr inline int32_t ordinalCount() { + return 2; +} + +template <> +constexpr inline int32_t bitCount() { + return 1; +} + +constexpr inline Display scopedEnum(YGDisplay unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGDisplay unscopedEnum(Display scoped) { + return static_cast(scoped); +} + +inline const char* toString(Display e) { + return YGDisplayToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/Edge.h b/yoga/enums/Edge.h new file mode 100644 index 0000000000..d6d2b0ee66 --- /dev/null +++ b/yoga/enums/Edge.h @@ -0,0 +1,52 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class Edge : uint8_t { + Left = YGEdgeLeft, + Top = YGEdgeTop, + Right = YGEdgeRight, + Bottom = YGEdgeBottom, + Start = YGEdgeStart, + End = YGEdgeEnd, + Horizontal = YGEdgeHorizontal, + Vertical = YGEdgeVertical, + All = YGEdgeAll, +}; + +template <> +constexpr inline int32_t ordinalCount() { + return 9; +} + +template <> +constexpr inline int32_t bitCount() { + return 4; +} + +constexpr inline Edge scopedEnum(YGEdge unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGEdge unscopedEnum(Edge scoped) { + return static_cast(scoped); +} + +inline const char* toString(Edge e) { + return YGEdgeToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/Errata.h b/yoga/enums/Errata.h new file mode 100644 index 0000000000..b0cf6210f9 --- /dev/null +++ b/yoga/enums/Errata.h @@ -0,0 +1,49 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class Errata : uint32_t { + None = YGErrataNone, + StretchFlexBasis = YGErrataStretchFlexBasis, + All = YGErrataAll, + Classic = YGErrataClassic, +}; + +YG_DEFINE_ENUM_FLAG_OPERATORS(Errata) + +template <> +constexpr inline int32_t ordinalCount() { + return 4; +} + +template <> +constexpr inline int32_t bitCount() { + return 2; +} + +constexpr inline Errata scopedEnum(YGErrata unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGErrata unscopedEnum(Errata scoped) { + return static_cast(scoped); +} + +inline const char* toString(Errata e) { + return YGErrataToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/ExperimentalFeature.h b/yoga/enums/ExperimentalFeature.h new file mode 100644 index 0000000000..6ed358155b --- /dev/null +++ b/yoga/enums/ExperimentalFeature.h @@ -0,0 +1,45 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class ExperimentalFeature : uint8_t { + WebFlexBasis = YGExperimentalFeatureWebFlexBasis, + AbsolutePercentageAgainstPaddingEdge = YGExperimentalFeatureAbsolutePercentageAgainstPaddingEdge, +}; + +template <> +constexpr inline int32_t ordinalCount() { + return 2; +} + +template <> +constexpr inline int32_t bitCount() { + return 1; +} + +constexpr inline ExperimentalFeature scopedEnum(YGExperimentalFeature unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGExperimentalFeature unscopedEnum(ExperimentalFeature scoped) { + return static_cast(scoped); +} + +inline const char* toString(ExperimentalFeature e) { + return YGExperimentalFeatureToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/FlexDirection.h b/yoga/enums/FlexDirection.h new file mode 100644 index 0000000000..45f6497ca4 --- /dev/null +++ b/yoga/enums/FlexDirection.h @@ -0,0 +1,47 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class FlexDirection : uint8_t { + Column = YGFlexDirectionColumn, + ColumnReverse = YGFlexDirectionColumnReverse, + Row = YGFlexDirectionRow, + RowReverse = YGFlexDirectionRowReverse, +}; + +template <> +constexpr inline int32_t ordinalCount() { + return 4; +} + +template <> +constexpr inline int32_t bitCount() { + return 2; +} + +constexpr inline FlexDirection scopedEnum(YGFlexDirection unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGFlexDirection unscopedEnum(FlexDirection scoped) { + return static_cast(scoped); +} + +inline const char* toString(FlexDirection e) { + return YGFlexDirectionToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/Gutter.h b/yoga/enums/Gutter.h new file mode 100644 index 0000000000..5c0f4a254e --- /dev/null +++ b/yoga/enums/Gutter.h @@ -0,0 +1,46 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class Gutter : uint8_t { + Column = YGGutterColumn, + Row = YGGutterRow, + All = YGGutterAll, +}; + +template <> +constexpr inline int32_t ordinalCount() { + return 3; +} + +template <> +constexpr inline int32_t bitCount() { + return 2; +} + +constexpr inline Gutter scopedEnum(YGGutter unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGGutter unscopedEnum(Gutter scoped) { + return static_cast(scoped); +} + +inline const char* toString(Gutter e) { + return YGGutterToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/Justify.h b/yoga/enums/Justify.h new file mode 100644 index 0000000000..870a3cb2a7 --- /dev/null +++ b/yoga/enums/Justify.h @@ -0,0 +1,49 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class Justify : uint8_t { + FlexStart = YGJustifyFlexStart, + Center = YGJustifyCenter, + FlexEnd = YGJustifyFlexEnd, + SpaceBetween = YGJustifySpaceBetween, + SpaceAround = YGJustifySpaceAround, + SpaceEvenly = YGJustifySpaceEvenly, +}; + +template <> +constexpr inline int32_t ordinalCount() { + return 6; +} + +template <> +constexpr inline int32_t bitCount() { + return 3; +} + +constexpr inline Justify scopedEnum(YGJustify unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGJustify unscopedEnum(Justify scoped) { + return static_cast(scoped); +} + +inline const char* toString(Justify e) { + return YGJustifyToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/LogLevel.h b/yoga/enums/LogLevel.h new file mode 100644 index 0000000000..45cac9fe39 --- /dev/null +++ b/yoga/enums/LogLevel.h @@ -0,0 +1,49 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class LogLevel : uint8_t { + Error = YGLogLevelError, + Warn = YGLogLevelWarn, + Info = YGLogLevelInfo, + Debug = YGLogLevelDebug, + Verbose = YGLogLevelVerbose, + Fatal = YGLogLevelFatal, +}; + +template <> +constexpr inline int32_t ordinalCount() { + return 6; +} + +template <> +constexpr inline int32_t bitCount() { + return 3; +} + +constexpr inline LogLevel scopedEnum(YGLogLevel unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGLogLevel unscopedEnum(LogLevel scoped) { + return static_cast(scoped); +} + +inline const char* toString(LogLevel e) { + return YGLogLevelToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/MeasureMode.h b/yoga/enums/MeasureMode.h new file mode 100644 index 0000000000..80fbcc599f --- /dev/null +++ b/yoga/enums/MeasureMode.h @@ -0,0 +1,46 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class MeasureMode : uint8_t { + Undefined = YGMeasureModeUndefined, + Exactly = YGMeasureModeExactly, + AtMost = YGMeasureModeAtMost, +}; + +template <> +constexpr inline int32_t ordinalCount() { + return 3; +} + +template <> +constexpr inline int32_t bitCount() { + return 2; +} + +constexpr inline MeasureMode scopedEnum(YGMeasureMode unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGMeasureMode unscopedEnum(MeasureMode scoped) { + return static_cast(scoped); +} + +inline const char* toString(MeasureMode e) { + return YGMeasureModeToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/NodeType.h b/yoga/enums/NodeType.h new file mode 100644 index 0000000000..782b163c2e --- /dev/null +++ b/yoga/enums/NodeType.h @@ -0,0 +1,45 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class NodeType : uint8_t { + Default = YGNodeTypeDefault, + Text = YGNodeTypeText, +}; + +template <> +constexpr inline int32_t ordinalCount() { + return 2; +} + +template <> +constexpr inline int32_t bitCount() { + return 1; +} + +constexpr inline NodeType scopedEnum(YGNodeType unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGNodeType unscopedEnum(NodeType scoped) { + return static_cast(scoped); +} + +inline const char* toString(NodeType e) { + return YGNodeTypeToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/Overflow.h b/yoga/enums/Overflow.h new file mode 100644 index 0000000000..1d29103e62 --- /dev/null +++ b/yoga/enums/Overflow.h @@ -0,0 +1,46 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class Overflow : uint8_t { + Visible = YGOverflowVisible, + Hidden = YGOverflowHidden, + Scroll = YGOverflowScroll, +}; + +template <> +constexpr inline int32_t ordinalCount() { + return 3; +} + +template <> +constexpr inline int32_t bitCount() { + return 2; +} + +constexpr inline Overflow scopedEnum(YGOverflow unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGOverflow unscopedEnum(Overflow scoped) { + return static_cast(scoped); +} + +inline const char* toString(Overflow e) { + return YGOverflowToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/PositionType.h b/yoga/enums/PositionType.h new file mode 100644 index 0000000000..bde2b1896f --- /dev/null +++ b/yoga/enums/PositionType.h @@ -0,0 +1,46 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class PositionType : uint8_t { + Static = YGPositionTypeStatic, + Relative = YGPositionTypeRelative, + Absolute = YGPositionTypeAbsolute, +}; + +template <> +constexpr inline int32_t ordinalCount() { + return 3; +} + +template <> +constexpr inline int32_t bitCount() { + return 2; +} + +constexpr inline PositionType scopedEnum(YGPositionType unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGPositionType unscopedEnum(PositionType scoped) { + return static_cast(scoped); +} + +inline const char* toString(PositionType e) { + return YGPositionTypeToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/PrintOptions.h b/yoga/enums/PrintOptions.h new file mode 100644 index 0000000000..c61fbc6383 --- /dev/null +++ b/yoga/enums/PrintOptions.h @@ -0,0 +1,48 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class PrintOptions : uint32_t { + Layout = YGPrintOptionsLayout, + Style = YGPrintOptionsStyle, + Children = YGPrintOptionsChildren, +}; + +YG_DEFINE_ENUM_FLAG_OPERATORS(PrintOptions) + +template <> +constexpr inline int32_t ordinalCount() { + return 3; +} + +template <> +constexpr inline int32_t bitCount() { + return 2; +} + +constexpr inline PrintOptions scopedEnum(YGPrintOptions unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGPrintOptions unscopedEnum(PrintOptions scoped) { + return static_cast(scoped); +} + +inline const char* toString(PrintOptions e) { + return YGPrintOptionsToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/Unit.h b/yoga/enums/Unit.h new file mode 100644 index 0000000000..36efe95aaf --- /dev/null +++ b/yoga/enums/Unit.h @@ -0,0 +1,47 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class Unit : uint8_t { + Undefined = YGUnitUndefined, + Point = YGUnitPoint, + Percent = YGUnitPercent, + Auto = YGUnitAuto, +}; + +template <> +constexpr inline int32_t ordinalCount() { + return 4; +} + +template <> +constexpr inline int32_t bitCount() { + return 2; +} + +constexpr inline Unit scopedEnum(YGUnit unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGUnit unscopedEnum(Unit scoped) { + return static_cast(scoped); +} + +inline const char* toString(Unit e) { + return YGUnitToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/enums/Wrap.h b/yoga/enums/Wrap.h new file mode 100644 index 0000000000..ae380a4448 --- /dev/null +++ b/yoga/enums/Wrap.h @@ -0,0 +1,46 @@ +/* + * 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. + */ + +// @generated by enums.py +// clang-format off +#pragma once + +#include +#include +#include + +namespace facebook::yoga { + +enum class Wrap : uint8_t { + NoWrap = YGWrapNoWrap, + Wrap = YGWrapWrap, + WrapReverse = YGWrapWrapReverse, +}; + +template <> +constexpr inline int32_t ordinalCount() { + return 3; +} + +template <> +constexpr inline int32_t bitCount() { + return 2; +} + +constexpr inline Wrap scopedEnum(YGWrap unscoped) { + return static_cast(unscoped); +} + +constexpr inline YGWrap unscopedEnum(Wrap scoped) { + return static_cast(scoped); +} + +inline const char* toString(Wrap e) { + return YGWrapToString(unscopedEnum(e)); +} + +} // namespace facebook::yoga diff --git a/yoga/bits/EnumBitset.h b/yoga/enums/YogaEnums.h similarity index 57% rename from yoga/bits/EnumBitset.h rename to yoga/enums/YogaEnums.h index 79ee1e7446..9621244813 100644 --- a/yoga/bits/EnumBitset.h +++ b/yoga/enums/YogaEnums.h @@ -7,13 +7,12 @@ #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()>; +template +constexpr inline int32_t ordinalCount(); + +template +constexpr inline int32_t bitCount(); } // namespace facebook::yoga diff --git a/yoga/node/CachedMeasurement.h b/yoga/node/CachedMeasurement.h index b2695bf1a2..4d2c222600 100644 --- a/yoga/node/CachedMeasurement.h +++ b/yoga/node/CachedMeasurement.h @@ -10,6 +10,8 @@ #include #include + +#include #include namespace facebook::yoga { @@ -17,8 +19,8 @@ namespace facebook::yoga { struct CachedMeasurement { float availableWidth{-1}; float availableHeight{-1}; - YGMeasureMode widthMeasureMode{YGMeasureModeUndefined}; - YGMeasureMode heightMeasureMode{YGMeasureModeUndefined}; + MeasureMode widthMeasureMode{MeasureMode::Undefined}; + MeasureMode heightMeasureMode{MeasureMode::Undefined}; float computedWidth{-1}; float computedHeight{-1}; diff --git a/yoga/node/Node.cpp b/yoga/node/Node.cpp index da3438d2f3..6f18c5768b 100644 --- a/yoga/node/Node.cpp +++ b/yoga/node/Node.cpp @@ -211,10 +211,11 @@ FloatOptional Node::getGapForAxis( YGSize Node::measure( float width, - YGMeasureMode widthMode, + MeasureMode widthMode, float height, - YGMeasureMode heightMode) { - return measureFunc_(this, width, widthMode, height, heightMode); + MeasureMode heightMode) { + return measureFunc_( + this, width, unscopedEnum(widthMode), height, unscopedEnum(heightMode)); } float Node::baseline(float width, float height) const { @@ -227,7 +228,7 @@ void Node::setMeasureFunc(YGMeasureFunc measureFunc) { if (measureFunc == nullptr) { // TODO: t18095186 Move nodeType to opt-in function and mark appropriate // places in Litho - setNodeType(YGNodeTypeDefault); + setNodeType(NodeType::Default); } else { yoga::assertFatalWithNode( this, @@ -236,7 +237,7 @@ void Node::setMeasureFunc(YGMeasureFunc measureFunc) { "children."); // TODO: t18095186 Move nodeType to opt-in function and mark appropriate // places in Litho - setNodeType(YGNodeTypeText); + setNodeType(NodeType::Text); } measureFunc_ = measureFunc; diff --git a/yoga/node/Node.h b/yoga/node/Node.h index 17df371c00..e6aff53482 100644 --- a/yoga/node/Node.h +++ b/yoga/node/Node.h @@ -14,6 +14,9 @@ #include #include +#include +#include +#include #include #include #include @@ -29,7 +32,7 @@ struct NodeFlags { bool hasNewLayout : 1; bool isReferenceBaseline : 1; bool isDirty : 1; - uint32_t nodeType : 1; + NodeType nodeType : bitCount(); }; #pragma pack(pop) @@ -92,15 +95,15 @@ class YG_EXPORT Node : public ::YGNode { return flags_.hasNewLayout; } - YGNodeType getNodeType() const { - return static_cast(flags_.nodeType); + NodeType getNodeType() const { + return flags_.nodeType; } bool hasMeasureFunc() const noexcept { return measureFunc_ != nullptr; } - YGSize measure(float, YGMeasureMode, float, YGMeasureMode); + YGSize measure(float, MeasureMode, float, MeasureMode); bool hasBaselineFunc() const noexcept { return baselineFunc_ != nullptr; @@ -108,7 +111,7 @@ class YG_EXPORT Node : public ::YGNode { float baseline(float width, float height) const; - bool hasErrata(YGErrata errata) const { + bool hasErrata(Errata errata) const { return config_->hasErrata(errata); } @@ -250,8 +253,8 @@ class YG_EXPORT Node : public ::YGNode { flags_.hasNewLayout = hasNewLayout; } - void setNodeType(YGNodeType nodeType) { - flags_.nodeType = static_cast(nodeType) & 0x01; + void setNodeType(NodeType nodeType) { + flags_.nodeType = nodeType; } void setMeasureFunc(YGMeasureFunc measureFunc);