From 816355667f3fc01f64225c6b7495e08b5b4ad0a4 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Thu, 14 Sep 2023 20:50:17 -0700 Subject: [PATCH 1/5] C++ style enums 1/N: Generator Summary: This adds logic to the enum generator to generate C++ style scoped enums. This gives us a few nicities over C enums, even if both must exist: 1. We can add types and keep unsgined enums directly in bitfields 2. Style/readability 3. Avoiding implicit int conversion Differential Revision: D49267996 fbshipit-source-id: a136d6813d393f9874ec88b958071c13b42f865c --- .../ReactCommon/yoga/yoga/YGEnums.cpp | 2 +- .../ReactCommon/yoga/yoga/YGEnums.h | 5 +- .../ReactCommon/yoga/yoga/enums/Align.h | 51 ++++++++++++++++++ .../ReactCommon/yoga/yoga/enums/Dimension.h | 45 ++++++++++++++++ .../ReactCommon/yoga/yoga/enums/Direction.h | 46 ++++++++++++++++ .../ReactCommon/yoga/yoga/enums/Display.h | 45 ++++++++++++++++ .../ReactCommon/yoga/yoga/enums/Edge.h | 52 +++++++++++++++++++ .../ReactCommon/yoga/yoga/enums/Errata.h | 47 +++++++++++++++++ .../yoga/yoga/enums/ExperimentalFeature.h | 45 ++++++++++++++++ .../yoga/yoga/enums/FlexDirection.h | 47 +++++++++++++++++ .../ReactCommon/yoga/yoga/enums/Gutter.h | 46 ++++++++++++++++ .../ReactCommon/yoga/yoga/enums/Justify.h | 49 +++++++++++++++++ .../ReactCommon/yoga/yoga/enums/LogLevel.h | 49 +++++++++++++++++ .../ReactCommon/yoga/yoga/enums/MeasureMode.h | 46 ++++++++++++++++ .../ReactCommon/yoga/yoga/enums/NodeType.h | 45 ++++++++++++++++ .../ReactCommon/yoga/yoga/enums/Overflow.h | 46 ++++++++++++++++ .../yoga/yoga/enums/PositionType.h | 46 ++++++++++++++++ .../yoga/yoga/enums/PrintOptions.h | 46 ++++++++++++++++ .../ReactCommon/yoga/yoga/enums/Unit.h | 47 +++++++++++++++++ .../ReactCommon/yoga/yoga/enums/Wrap.h | 46 ++++++++++++++++ .../ReactCommon/yoga/yoga/enums/YogaEnums.h | 18 +++++++ 21 files changed, 864 insertions(+), 5 deletions(-) create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/Align.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/Dimension.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/Direction.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/Display.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/Edge.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/Errata.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/ExperimentalFeature.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/FlexDirection.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/Gutter.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/Justify.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/LogLevel.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/MeasureMode.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/NodeType.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/Overflow.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/PositionType.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/PrintOptions.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/Unit.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/Wrap.h create mode 100644 packages/react-native/ReactCommon/yoga/yoga/enums/YogaEnums.h diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGEnums.cpp b/packages/react-native/ReactCommon/yoga/yoga/YGEnums.cpp index f7220eff720ca9..a7b1990eed6e7f 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGEnums.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/YGEnums.cpp @@ -6,7 +6,7 @@ */ // @generated by enums.py - +// clang-format off #include const char* YGAlignToString(const YGAlign value) { diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGEnums.h b/packages/react-native/ReactCommon/yoga/yoga/YGEnums.h index 7abe5d92ecb10f..53d245315ef369 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGEnums.h +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/Align.h b/packages/react-native/ReactCommon/yoga/yoga/enums/Align.h new file mode 100644 index 00000000000000..95df12a79033c4 --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/Dimension.h b/packages/react-native/ReactCommon/yoga/yoga/enums/Dimension.h new file mode 100644 index 00000000000000..2274a980a516ce --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/Direction.h b/packages/react-native/ReactCommon/yoga/yoga/enums/Direction.h new file mode 100644 index 00000000000000..1b5e19c93c3ad2 --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/Display.h b/packages/react-native/ReactCommon/yoga/yoga/enums/Display.h new file mode 100644 index 00000000000000..c1d8c7a82e5397 --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/Edge.h b/packages/react-native/ReactCommon/yoga/yoga/enums/Edge.h new file mode 100644 index 00000000000000..d6d2b0ee663f20 --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/Errata.h b/packages/react-native/ReactCommon/yoga/yoga/enums/Errata.h new file mode 100644 index 00000000000000..a96b2967b121f8 --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/yoga/enums/Errata.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 Errata : uint32_t { + None = YGErrataNone, + StretchFlexBasis = YGErrataStretchFlexBasis, + All = YGErrataAll, + Classic = YGErrataClassic, +}; + +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/packages/react-native/ReactCommon/yoga/yoga/enums/ExperimentalFeature.h b/packages/react-native/ReactCommon/yoga/yoga/enums/ExperimentalFeature.h new file mode 100644 index 00000000000000..6ed358155b0c87 --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/FlexDirection.h b/packages/react-native/ReactCommon/yoga/yoga/enums/FlexDirection.h new file mode 100644 index 00000000000000..45f6497ca45230 --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/Gutter.h b/packages/react-native/ReactCommon/yoga/yoga/enums/Gutter.h new file mode 100644 index 00000000000000..5c0f4a254e5f6b --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/Justify.h b/packages/react-native/ReactCommon/yoga/yoga/enums/Justify.h new file mode 100644 index 00000000000000..870a3cb2a70029 --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/LogLevel.h b/packages/react-native/ReactCommon/yoga/yoga/enums/LogLevel.h new file mode 100644 index 00000000000000..45cac9fe3974ec --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/MeasureMode.h b/packages/react-native/ReactCommon/yoga/yoga/enums/MeasureMode.h new file mode 100644 index 00000000000000..80fbcc599f4c90 --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/NodeType.h b/packages/react-native/ReactCommon/yoga/yoga/enums/NodeType.h new file mode 100644 index 00000000000000..782b163c2e110d --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/Overflow.h b/packages/react-native/ReactCommon/yoga/yoga/enums/Overflow.h new file mode 100644 index 00000000000000..1d29103e62f790 --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/PositionType.h b/packages/react-native/ReactCommon/yoga/yoga/enums/PositionType.h new file mode 100644 index 00000000000000..bde2b1896fbe37 --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/PrintOptions.h b/packages/react-native/ReactCommon/yoga/yoga/enums/PrintOptions.h new file mode 100644 index 00000000000000..603c91aa71417f --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/yoga/enums/PrintOptions.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 PrintOptions : uint32_t { + Layout = YGPrintOptionsLayout, + Style = YGPrintOptionsStyle, + Children = YGPrintOptionsChildren, +}; + +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/packages/react-native/ReactCommon/yoga/yoga/enums/Unit.h b/packages/react-native/ReactCommon/yoga/yoga/enums/Unit.h new file mode 100644 index 00000000000000..36efe95aaf654a --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/Wrap.h b/packages/react-native/ReactCommon/yoga/yoga/enums/Wrap.h new file mode 100644 index 00000000000000..ae380a4448d828 --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/YogaEnums.h b/packages/react-native/ReactCommon/yoga/yoga/enums/YogaEnums.h new file mode 100644 index 00000000000000..cc7223bf9c1585 --- /dev/null +++ b/packages/react-native/ReactCommon/yoga/yoga/enums/YogaEnums.h @@ -0,0 +1,18 @@ +/* + * 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 + +namespace facebook::yoga { + +template +constexpr inline int32_t ordinalCount() = delete; + +template +constexpr inline int32_t bitCount() = delete; + +} // namespace facebook::yoga From a66fbd27376c6a4ec44112f11da1c151702f20fe Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Thu, 14 Sep 2023 20:50:17 -0700 Subject: [PATCH 2/5] C++ style enums 2/N: NodeType Differential Revision: D49269117 fbshipit-source-id: 1ffafa9405cdf3818562f4605f28f14e18c05c0f --- packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp | 4 ++-- .../ReactCommon/yoga/yoga/algorithm/PixelGrid.cpp | 2 +- .../react-native/ReactCommon/yoga/yoga/node/Node.cpp | 4 ++-- .../react-native/ReactCommon/yoga/yoga/node/Node.h | 11 ++++++----- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp b/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp index 34ef7f9f304c3c..80d7b257d200d8 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp +++ b/packages/react-native/ReactCommon/yoga/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) { diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/PixelGrid.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/PixelGrid.cpp index a9594b24d193e2..dce87347f5f161 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/PixelGrid.cpp +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp b/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp index da3438d2f3fab1..e2b022e024066a 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp @@ -227,7 +227,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 +236,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/packages/react-native/ReactCommon/yoga/yoga/node/Node.h b/packages/react-native/ReactCommon/yoga/yoga/node/Node.h index 17df371c000017..e088ab9946f81a 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h +++ b/packages/react-native/ReactCommon/yoga/yoga/node/Node.h @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -29,7 +30,7 @@ struct NodeFlags { bool hasNewLayout : 1; bool isReferenceBaseline : 1; bool isDirty : 1; - uint32_t nodeType : 1; + NodeType nodeType : bitCount(); }; #pragma pack(pop) @@ -92,8 +93,8 @@ 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 { @@ -250,8 +251,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); From 12188523063b6dbfc27d1b0eb6dd3b77b46bd55c Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Thu, 14 Sep 2023 20:50:17 -0700 Subject: [PATCH 3/5] C++ style enums 3/N: ExperimentalFeature Differential Revision: D49269440 fbshipit-source-id: 58d262cc6a2bf46dd4aad8254aeea788c208f2ce --- .../ReactCommon/yoga/yoga/Yoga.cpp | 5 +++-- .../yoga/yoga/algorithm/CalculateLayout.cpp | 8 ++++---- .../ReactCommon/yoga/yoga/bits/EnumBitset.h | 19 ------------------- .../ReactCommon/yoga/yoga/config/Config.cpp | 10 +++++----- .../ReactCommon/yoga/yoga/config/Config.h | 16 +++++++++------- .../ReactCommon/yoga/yoga/enums/YogaEnums.h | 4 ++-- 6 files changed, 23 insertions(+), 39 deletions(-) delete mode 100644 packages/react-native/ReactCommon/yoga/yoga/bits/EnumBitset.h diff --git a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp b/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp index 80d7b257d200d8..d1f9a4f86106cd 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index a8cb925f5f0ebb..55fb9cd8eb8ddd 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/bits/EnumBitset.h b/packages/react-native/ReactCommon/yoga/yoga/bits/EnumBitset.h deleted file mode 100644 index 79ee1e7446fc2b..00000000000000 --- a/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp b/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp index e86c76017bb488..a3e48d34369f6f 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/config/Config.h b/packages/react-native/ReactCommon/yoga/yoga/config/Config.h index 4590fcdf2fc396..bc5a0ef2e9e56a 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/config/Config.h +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/enums/YogaEnums.h b/packages/react-native/ReactCommon/yoga/yoga/enums/YogaEnums.h index cc7223bf9c1585..9621244813e35c 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/enums/YogaEnums.h +++ b/packages/react-native/ReactCommon/yoga/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 From 8497cae6c07385f25f33fcd1b481af9498b7144d Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Thu, 14 Sep 2023 20:50:17 -0700 Subject: [PATCH 4/5] C++ style enums 4/N: Errata Summary: This converts usages of YGErrata to Errata Differential Revision: D49270354 fbshipit-source-id: db855c732477bcb7a00474cbe090d30e137d88e5 --- .../react-native/ReactCommon/yoga/yoga/Yoga.cpp | 4 ++-- .../yoga/yoga/algorithm/CalculateLayout.cpp | 2 +- .../ReactCommon/yoga/yoga/config/Config.cpp | 12 ++++++------ .../ReactCommon/yoga/yoga/config/Config.h | 13 +++++++------ .../ReactCommon/yoga/yoga/enums/Errata.h | 2 ++ .../ReactCommon/yoga/yoga/enums/PrintOptions.h | 2 ++ .../react-native/ReactCommon/yoga/yoga/node/Node.h | 3 ++- 7 files changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp b/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp index d1f9a4f86106cd..63678fa63d7600 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp @@ -878,11 +878,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( diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index 55fb9cd8eb8ddd..f39c6285de7d79 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -1755,7 +1755,7 @@ static void calculateLayoutImpl( availableInnerMainDim = maxInnerMainDim; } else { bool useLegacyStretchBehaviour = - node->hasErrata(YGErrataStretchFlexBasis); + node->hasErrata(Errata::StretchFlexBasis); if (!useLegacyStretchBehaviour && ((!yoga::isUndefined(flexLine.layout.totalFlexGrowFactors) && diff --git a/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp b/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp index a3e48d34369f6f..85f715bcd8dfe9 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp @@ -54,24 +54,24 @@ 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) { diff --git a/packages/react-native/ReactCommon/yoga/yoga/config/Config.h b/packages/react-native/ReactCommon/yoga/yoga/config/Config.h index bc5a0ef2e9e56a..e910ea04bcfecb 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/config/Config.h +++ b/packages/react-native/ReactCommon/yoga/yoga/config/Config.h @@ -10,6 +10,7 @@ #include #include +#include #include // Tag struct used to form the opaque YGConfigRef for the public C API @@ -51,11 +52,11 @@ class YG_EXPORT Config : public ::YGConfig { 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; @@ -82,7 +83,7 @@ class YG_EXPORT Config : public ::YGConfig { ConfigFlags flags_{}; ExperimentalFeatureSet experimentalFeatures_{}; - YGErrata errata_ = YGErrataNone; + Errata errata_ = Errata::None; float pointScaleFactor_ = 1.0f; void* context_ = nullptr; }; diff --git a/packages/react-native/ReactCommon/yoga/yoga/enums/Errata.h b/packages/react-native/ReactCommon/yoga/yoga/enums/Errata.h index a96b2967b121f8..b0cf6210f928de 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/enums/Errata.h +++ b/packages/react-native/ReactCommon/yoga/yoga/enums/Errata.h @@ -22,6 +22,8 @@ enum class Errata : uint32_t { Classic = YGErrataClassic, }; +YG_DEFINE_ENUM_FLAG_OPERATORS(Errata) + template <> constexpr inline int32_t ordinalCount() { return 4; diff --git a/packages/react-native/ReactCommon/yoga/yoga/enums/PrintOptions.h b/packages/react-native/ReactCommon/yoga/yoga/enums/PrintOptions.h index 603c91aa71417f..c61fbc6383a279 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/enums/PrintOptions.h +++ b/packages/react-native/ReactCommon/yoga/yoga/enums/PrintOptions.h @@ -21,6 +21,8 @@ enum class PrintOptions : uint32_t { Children = YGPrintOptionsChildren, }; +YG_DEFINE_ENUM_FLAG_OPERATORS(PrintOptions) + template <> constexpr inline int32_t ordinalCount() { return 3; diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h b/packages/react-native/ReactCommon/yoga/yoga/node/Node.h index e088ab9946f81a..91d14445f44a45 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h +++ b/packages/react-native/ReactCommon/yoga/yoga/node/Node.h @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -109,7 +110,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); } From 4b21a5fb2e1e92ab6b4569987bc312d456df302a Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Thu, 14 Sep 2023 20:50:37 -0700 Subject: [PATCH 5/5] C++ style enums 5/N: LogLevel (#39447) Summary: X-link: https://github.com/facebook/yoga/pull/1387 Pull Request resolved: https://github.com/facebook/react-native/pull/39447 This converts usages of YGLogLevel to LogLevel Reviewed By: rozele Differential Revision: D49270695 fbshipit-source-id: 20603e1a05b8200fed98e22e2bf8a34674a4d484 --- .../react-native/ReactCommon/yoga/yoga/Yoga.cpp | 2 +- .../yoga/yoga/algorithm/CalculateLayout.cpp | 14 +++++++------- .../ReactCommon/yoga/yoga/config/Config.cpp | 4 ++-- .../ReactCommon/yoga/yoga/config/Config.h | 3 ++- .../ReactCommon/yoga/yoga/debug/AssertFatal.cpp | 6 +++--- .../ReactCommon/yoga/yoga/debug/Log.cpp | 10 +++++----- .../react-native/ReactCommon/yoga/yoga/debug/Log.h | 8 ++++---- 7 files changed, 24 insertions(+), 23 deletions(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp b/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp index 63678fa63d7600..f144c1508ba848 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp @@ -805,7 +805,7 @@ 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()); + yoga::log(node, LogLevel::Debug, str.c_str()); } #endif diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index f39c6285de7d79..a7c21d081f73a3 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -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; } diff --git a/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp b/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp index 85f715bcd8dfe9..721588ee0a89d2 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp @@ -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/packages/react-native/ReactCommon/yoga/yoga/config/Config.h b/packages/react-native/ReactCommon/yoga/yoga/config/Config.h index e910ea04bcfecb..227aa598bf9d09 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/config/Config.h +++ b/packages/react-native/ReactCommon/yoga/yoga/config/Config.h @@ -12,6 +12,7 @@ #include #include #include +#include // Tag struct used to form the opaque YGConfigRef for the public C API struct YGConfig {}; @@ -67,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; diff --git a/packages/react-native/ReactCommon/yoga/yoga/debug/AssertFatal.cpp b/packages/react-native/ReactCommon/yoga/yoga/debug/AssertFatal.cpp index d83c8cc56e857d..2388a744539dbe 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/debug/AssertFatal.cpp +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/debug/Log.cpp b/packages/react-native/ReactCommon/yoga/yoga/debug/Log.cpp index c16547ae345675..9b525d8a8e2175 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/debug/Log.cpp +++ b/packages/react-native/ReactCommon/yoga/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/packages/react-native/ReactCommon/yoga/yoga/debug/Log.h b/packages/react-native/ReactCommon/yoga/yoga/debug/Log.h index 59a8b2e0db8742..94dc0e0cedf38b 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/debug/Log.h +++ b/packages/react-native/ReactCommon/yoga/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;