-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C++ style enums 1/N: Generator (#39446)
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
1 parent
598b7ed
commit 215cad3
Showing
21 changed files
with
864 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
packages/react-native/ReactCommon/yoga/yoga/enums/Dimension.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
packages/react-native/ReactCommon/yoga/yoga/enums/Direction.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
packages/react-native/ReactCommon/yoga/yoga/enums/Display.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
packages/react-native/ReactCommon/yoga/yoga/enums/Errata.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
45 changes: 45 additions & 0 deletions
45
packages/react-native/ReactCommon/yoga/yoga/enums/ExperimentalFeature.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.