Skip to content

Commit

Permalink
C++ style enums 1/N: Generator (#39446)
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/yoga#1384


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

Reviewed By: rozele

Differential Revision: D49267996
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Sep 15, 2023
1 parent 598b7ed commit 215cad3
Show file tree
Hide file tree
Showing 21 changed files with 864 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/react-native/ReactCommon/yoga/yoga/YGEnums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

// @generated by enums.py

// clang-format off
#include <yoga/YGEnums.h>

const char* YGAlignToString(const YGAlign value) {
Expand Down
5 changes: 1 addition & 4 deletions packages/react-native/ReactCommon/yoga/yoga/YGEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
*/

// @generated by enums.py

// clang-format off
#pragma once
#include <yoga/YGMacros.h>

// clang-format off


YG_EXTERN_C_BEGIN

YG_ENUM_SEQ_DECL(
Expand Down
51 changes: 51 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/enums/Align.h
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <yoga/YGEnums.h>
#include <yoga/enums/YogaEnums.h>

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<Align>() {
return 8;
}

template <>
constexpr inline int32_t bitCount<Align>() {
return 3;
}

constexpr inline Align scopedEnum(YGAlign unscoped) {
return static_cast<Align>(unscoped);
}

constexpr inline YGAlign unscopedEnum(Align scoped) {
return static_cast<YGAlign>(scoped);
}

inline const char* toString(Align e) {
return YGAlignToString(unscopedEnum(e));
}

} // namespace facebook::yoga
45 changes: 45 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/enums/Dimension.h
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <yoga/YGEnums.h>
#include <yoga/enums/YogaEnums.h>

namespace facebook::yoga {

enum class Dimension : uint8_t {
Width = YGDimensionWidth,
Height = YGDimensionHeight,
};

template <>
constexpr inline int32_t ordinalCount<Dimension>() {
return 2;
}

template <>
constexpr inline int32_t bitCount<Dimension>() {
return 1;
}

constexpr inline Dimension scopedEnum(YGDimension unscoped) {
return static_cast<Dimension>(unscoped);
}

constexpr inline YGDimension unscopedEnum(Dimension scoped) {
return static_cast<YGDimension>(scoped);
}

inline const char* toString(Dimension e) {
return YGDimensionToString(unscopedEnum(e));
}

} // namespace facebook::yoga
46 changes: 46 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/enums/Direction.h
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <yoga/YGEnums.h>
#include <yoga/enums/YogaEnums.h>

namespace facebook::yoga {

enum class Direction : uint8_t {
Inherit = YGDirectionInherit,
LTR = YGDirectionLTR,
RTL = YGDirectionRTL,
};

template <>
constexpr inline int32_t ordinalCount<Direction>() {
return 3;
}

template <>
constexpr inline int32_t bitCount<Direction>() {
return 2;
}

constexpr inline Direction scopedEnum(YGDirection unscoped) {
return static_cast<Direction>(unscoped);
}

constexpr inline YGDirection unscopedEnum(Direction scoped) {
return static_cast<YGDirection>(scoped);
}

inline const char* toString(Direction e) {
return YGDirectionToString(unscopedEnum(e));
}

} // namespace facebook::yoga
45 changes: 45 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/enums/Display.h
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <yoga/YGEnums.h>
#include <yoga/enums/YogaEnums.h>

namespace facebook::yoga {

enum class Display : uint8_t {
Flex = YGDisplayFlex,
None = YGDisplayNone,
};

template <>
constexpr inline int32_t ordinalCount<Display>() {
return 2;
}

template <>
constexpr inline int32_t bitCount<Display>() {
return 1;
}

constexpr inline Display scopedEnum(YGDisplay unscoped) {
return static_cast<Display>(unscoped);
}

constexpr inline YGDisplay unscopedEnum(Display scoped) {
return static_cast<YGDisplay>(scoped);
}

inline const char* toString(Display e) {
return YGDisplayToString(unscopedEnum(e));
}

} // namespace facebook::yoga
52 changes: 52 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/enums/Edge.h
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <yoga/YGEnums.h>
#include <yoga/enums/YogaEnums.h>

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<Edge>() {
return 9;
}

template <>
constexpr inline int32_t bitCount<Edge>() {
return 4;
}

constexpr inline Edge scopedEnum(YGEdge unscoped) {
return static_cast<Edge>(unscoped);
}

constexpr inline YGEdge unscopedEnum(Edge scoped) {
return static_cast<YGEdge>(scoped);
}

inline const char* toString(Edge e) {
return YGEdgeToString(unscopedEnum(e));
}

} // namespace facebook::yoga
47 changes: 47 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/enums/Errata.h
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <yoga/YGEnums.h>
#include <yoga/enums/YogaEnums.h>

namespace facebook::yoga {

enum class Errata : uint32_t {
None = YGErrataNone,
StretchFlexBasis = YGErrataStretchFlexBasis,
All = YGErrataAll,
Classic = YGErrataClassic,
};

template <>
constexpr inline int32_t ordinalCount<Errata>() {
return 4;
}

template <>
constexpr inline int32_t bitCount<Errata>() {
return 2;
}

constexpr inline Errata scopedEnum(YGErrata unscoped) {
return static_cast<Errata>(unscoped);
}

constexpr inline YGErrata unscopedEnum(Errata scoped) {
return static_cast<YGErrata>(scoped);
}

inline const char* toString(Errata e) {
return YGErrataToString(unscopedEnum(e));
}

} // namespace facebook::yoga
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <yoga/YGEnums.h>
#include <yoga/enums/YogaEnums.h>

namespace facebook::yoga {

enum class ExperimentalFeature : uint8_t {
WebFlexBasis = YGExperimentalFeatureWebFlexBasis,
AbsolutePercentageAgainstPaddingEdge = YGExperimentalFeatureAbsolutePercentageAgainstPaddingEdge,
};

template <>
constexpr inline int32_t ordinalCount<ExperimentalFeature>() {
return 2;
}

template <>
constexpr inline int32_t bitCount<ExperimentalFeature>() {
return 1;
}

constexpr inline ExperimentalFeature scopedEnum(YGExperimentalFeature unscoped) {
return static_cast<ExperimentalFeature>(unscoped);
}

constexpr inline YGExperimentalFeature unscopedEnum(ExperimentalFeature scoped) {
return static_cast<YGExperimentalFeature>(scoped);
}

inline const char* toString(ExperimentalFeature e) {
return YGExperimentalFeatureToString(unscopedEnum(e));
}

} // namespace facebook::yoga
Loading

0 comments on commit 215cad3

Please sign in to comment.