Skip to content

Commit 4840495

Browse files
davidaureliofacebook-github-bot
authored andcommitted
Remove enum count macros
Summary: @public Removes all `YG...Count` macros for enums and replaces them with `facebook::yoga::enums::count<YG...>()`. This removes the need to manually maintain enum counts. Same as D13597449, working around a defect in clang < 3.9 Reviewed By: amir-shalem Differential Revision: D13634622 fbshipit-source-id: 344dc70e167b0caf746fe396cedd200f54e52219
1 parent 866bf10 commit 4840495

File tree

7 files changed

+87
-48
lines changed

7 files changed

+87
-48
lines changed

yoga/YGConfig.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#include "Yoga.h"
1111

1212
struct YGConfig {
13-
std::array<bool, YGExperimentalFeatureCount> experimentalFeatures = {};
13+
std::array<bool, facebook::yoga::enums::count<YGExperimentalFeature>()>
14+
experimentalFeatures = {};
1415
bool useWebDefaults = false;
1516
bool useLegacyStretchBehaviour = false;
1617
bool shouldDiffLayoutWithoutLegacyStretchBehaviour = false;

yoga/YGEnums.h

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,52 @@
88

99
#include "YGMacros.h"
1010

11+
#ifdef __cplusplus
12+
namespace facebook {
13+
namespace yoga {
14+
namespace enums {
15+
16+
template <typename T>
17+
constexpr int count(); // can't use `= delete` due to a defect in clang < 3.9
18+
19+
namespace detail {
20+
template <int... xs>
21+
constexpr int n() {
22+
return sizeof...(xs);
23+
}
24+
} // namespace detail
25+
26+
} // namespace enums
27+
} // namespace yoga
28+
} // namespace facebook
29+
#endif
30+
1131
#define YG_ENUM_DECL(NAME, ...) \
1232
typedef YG_ENUM_BEGIN(NAME){__VA_ARGS__} YG_ENUM_END(NAME); \
1333
WIN_EXPORT const char* NAME##ToString(NAME);
1434

35+
#ifdef __cplusplus
36+
#define YG_ENUM_SEQ_DECL(NAME, ...) \
37+
YG_ENUM_DECL(NAME, __VA_ARGS__) \
38+
YG_EXTERN_C_END \
39+
namespace facebook { \
40+
namespace yoga { \
41+
namespace enums { \
42+
template <> \
43+
constexpr int count<NAME>() { \
44+
return detail::n<__VA_ARGS__>(); \
45+
} \
46+
} \
47+
} \
48+
} \
49+
YG_EXTERN_C_BEGIN
50+
#else
51+
#define YG_ENUM_SEQ_DECL YG_ENUM_DECL
52+
#endif
53+
1554
YG_EXTERN_C_BEGIN
1655

17-
#define YGAlignCount 8
18-
YG_ENUM_DECL(
56+
YG_ENUM_SEQ_DECL(
1957
YGAlign,
2058
YGAlignAuto,
2159
YGAlignFlexStart,
@@ -26,17 +64,17 @@ YG_ENUM_DECL(
2664
YGAlignSpaceBetween,
2765
YGAlignSpaceAround);
2866

29-
#define YGDimensionCount 2
30-
YG_ENUM_DECL(YGDimension, YGDimensionWidth, YGDimensionHeight)
67+
YG_ENUM_SEQ_DECL(YGDimension, YGDimensionWidth, YGDimensionHeight)
3168

32-
#define YGDirectionCount 3
33-
YG_ENUM_DECL(YGDirection, YGDirectionInherit, YGDirectionLTR, YGDirectionRTL)
69+
YG_ENUM_SEQ_DECL(
70+
YGDirection,
71+
YGDirectionInherit,
72+
YGDirectionLTR,
73+
YGDirectionRTL)
3474

35-
#define YGDisplayCount 2
36-
YG_ENUM_DECL(YGDisplay, YGDisplayFlex, YGDisplayNone)
75+
YG_ENUM_SEQ_DECL(YGDisplay, YGDisplayFlex, YGDisplayNone)
3776

38-
#define YGEdgeCount 9
39-
YG_ENUM_DECL(
77+
YG_ENUM_SEQ_DECL(
4078
YGEdge,
4179
YGEdgeLeft,
4280
YGEdgeTop,
@@ -48,19 +86,16 @@ YG_ENUM_DECL(
4886
YGEdgeVertical,
4987
YGEdgeAll)
5088

51-
#define YGExperimentalFeatureCount 1
52-
YG_ENUM_DECL(YGExperimentalFeature, YGExperimentalFeatureWebFlexBasis)
89+
YG_ENUM_SEQ_DECL(YGExperimentalFeature, YGExperimentalFeatureWebFlexBasis)
5390

54-
#define YGFlexDirectionCount 4
55-
YG_ENUM_DECL(
91+
YG_ENUM_SEQ_DECL(
5692
YGFlexDirection,
5793
YGFlexDirectionColumn,
5894
YGFlexDirectionColumnReverse,
5995
YGFlexDirectionRow,
6096
YGFlexDirectionRowReverse)
6197

62-
#define YGJustifyCount 6
63-
YG_ENUM_DECL(
98+
YG_ENUM_SEQ_DECL(
6499
YGJustify,
65100
YGJustifyFlexStart,
66101
YGJustifyCenter,
@@ -69,8 +104,7 @@ YG_ENUM_DECL(
69104
YGJustifySpaceAround,
70105
YGJustifySpaceEvenly)
71106

72-
#define YGLogLevelCount 6
73-
YG_ENUM_DECL(
107+
YG_ENUM_SEQ_DECL(
74108
YGLogLevel,
75109
YGLogLevelError,
76110
YGLogLevelWarn,
@@ -79,35 +113,38 @@ YG_ENUM_DECL(
79113
YGLogLevelVerbose,
80114
YGLogLevelFatal)
81115

82-
#define YGMeasureModeCount 3
83-
YG_ENUM_DECL(
116+
YG_ENUM_SEQ_DECL(
84117
YGMeasureMode,
85118
YGMeasureModeUndefined,
86119
YGMeasureModeExactly,
87120
YGMeasureModeAtMost)
88121

89-
#define YGNodeTypeCount 2
90-
YG_ENUM_DECL(YGNodeType, YGNodeTypeDefault, YGNodeTypeText)
122+
YG_ENUM_SEQ_DECL(YGNodeType, YGNodeTypeDefault, YGNodeTypeText)
91123

92-
#define YGOverflowCount 3
93-
YG_ENUM_DECL(YGOverflow, YGOverflowVisible, YGOverflowHidden, YGOverflowScroll)
124+
YG_ENUM_SEQ_DECL(
125+
YGOverflow,
126+
YGOverflowVisible,
127+
YGOverflowHidden,
128+
YGOverflowScroll)
94129

95-
#define YGPositionTypeCount 2
96-
YG_ENUM_DECL(YGPositionType, YGPositionTypeRelative, YGPositionTypeAbsolute)
130+
YG_ENUM_SEQ_DECL(YGPositionType, YGPositionTypeRelative, YGPositionTypeAbsolute)
97131

98-
#define YGPrintOptionsCount 3
99132
YG_ENUM_DECL(
100133
YGPrintOptions,
101134
YGPrintOptionsLayout = 1,
102135
YGPrintOptionsStyle = 2,
103136
YGPrintOptionsChildren = 4)
104137

105-
#define YGUnitCount 4
106-
YG_ENUM_DECL(YGUnit, YGUnitUndefined, YGUnitPoint, YGUnitPercent, YGUnitAuto)
138+
YG_ENUM_SEQ_DECL(
139+
YGUnit,
140+
YGUnitUndefined,
141+
YGUnitPoint,
142+
YGUnitPercent,
143+
YGUnitAuto)
107144

108-
#define YGWrapCount 3
109-
YG_ENUM_DECL(YGWrap, YGWrapNoWrap, YGWrapWrap, YGWrapWrapReverse)
145+
YG_ENUM_SEQ_DECL(YGWrap, YGWrapNoWrap, YGWrapWrap, YGWrapWrapReverse)
110146

111147
YG_EXTERN_C_END
112148

113149
#undef YG_ENUM_DECL
150+
#undef YG_ENUM_SEQ_DECL

yoga/YGNode.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ YGValue YGNode::resolveFlexBasisPtr() const {
313313
}
314314

315315
void YGNode::resolveDimension() {
316-
for (uint32_t dim = YGDimensionWidth; dim < YGDimensionCount; dim++) {
316+
using namespace yoga;
317+
for (int dim = YGDimensionWidth; dim < enums::count<YGDimension>(); dim++) {
317318
if (!getStyle().maxDimensions[dim].isUndefined() &&
318319
YGValueEqual(
319320
getStyle().maxDimensions[dim], style_.minDimensions[dim])) {

yoga/YGNodePrint.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ static void indent(string& base, uint32_t level) {
2020
}
2121
}
2222

23-
static bool areFourValuesEqual(
24-
const facebook::yoga::detail::Values<YGEdgeCount>& four) {
23+
static bool areFourValuesEqual(const YGStyle::Edges& four) {
2524
return YGValueEqual(four[0], four[1]) && YGValueEqual(four[0], four[2]) &&
2625
YGValueEqual(four[0], four[3]);
2726
}
@@ -86,7 +85,7 @@ static void appendNumberIfNotZero(
8685
static void appendEdges(
8786
string& base,
8887
const string& key,
89-
const facebook::yoga::detail::Values<YGEdgeCount>& edges) {
88+
const YGStyle::Edges& edges) {
9089
if (areFourValuesEqual(edges)) {
9190
appendNumberIfNotZero(base, key, edges[YGEdgeLeft]);
9291
} else {
@@ -100,7 +99,7 @@ static void appendEdges(
10099
static void appendEdgeIfNotUndefined(
101100
string& base,
102101
const string& str,
103-
const facebook::yoga::detail::Values<YGEdgeCount>& edges,
102+
const YGStyle::Edges& edges,
104103
const YGEdge edge) {
105104
appendNumberIfNotUndefined(
106105
base,

yoga/YGStyle.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <array>
1010
#include <initializer_list>
1111
#include "CompactValue.h"
12+
#include "YGEnums.h"
1213
#include "YGFloatOptional.h"
1314
#include "Yoga-internal.h"
1415
#include "Yoga.h"
@@ -29,7 +30,8 @@ struct YGStyle {
2930

3031
public:
3132
using Dimensions = facebook::yoga::detail::Values<2>;
32-
using Edges = facebook::yoga::detail::Values<YGEdgeCount>;
33+
using Edges =
34+
facebook::yoga::detail::Values<facebook::yoga::enums::count<YGEdge>()>;
3335

3436
/* Some platforms don't support enum bitfields,
3537
so please use BITFIELD_ENUM_SIZED(BITS_COUNT) */

yoga/Yoga-internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ static const float kWebDefaultFlexShrink = 1.0f;
149149
extern bool YGFloatsEqual(const float a, const float b);
150150
extern bool YGValueEqual(const YGValue a, const YGValue b);
151151
extern facebook::yoga::detail::CompactValue YGComputedEdgeValue(
152-
const facebook::yoga::detail::Values<YGEdgeCount>& edges,
152+
const facebook::yoga::detail::Values<
153+
facebook::yoga::enums::count<YGEdge>()>& edges,
153154
YGEdge edge,
154155
facebook::yoga::detail::CompactValue defaultValue);

yoga/Yoga.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ bool YGFloatIsUndefined(const float value) {
105105
}
106106

107107
detail::CompactValue YGComputedEdgeValue(
108-
const facebook::yoga::detail::Values<YGEdgeCount>& edges,
108+
const YGStyle::Edges& edges,
109109
YGEdge edge,
110110
detail::CompactValue defaultValue) {
111111
if (!edges[edge].isUndefined()) {
@@ -3546,14 +3546,12 @@ static const char* YGSpacer(const unsigned long level) {
35463546
static const char* YGMeasureModeName(
35473547
const YGMeasureMode mode,
35483548
const bool performLayout) {
3549-
const char* kMeasureModeNames[YGMeasureModeCount] = {
3550-
"UNDEFINED", "EXACTLY", "AT_MOST"};
3551-
const char* kLayoutModeNames[YGMeasureModeCount] = {"LAY_UNDEFINED",
3552-
"LAY_EXACTLY",
3553-
"LAY_AT_"
3554-
"MOST"};
3555-
3556-
if (mode >= YGMeasureModeCount) {
3549+
constexpr auto N = enums::count<YGMeasureMode>();
3550+
const char* kMeasureModeNames[N] = {"UNDEFINED", "EXACTLY", "AT_MOST"};
3551+
const char* kLayoutModeNames[N] = {
3552+
"LAY_UNDEFINED", "LAY_EXACTLY", "LAY_AT_MOST"};
3553+
3554+
if (mode >= N) {
35573555
return "";
35583556
}
35593557

0 commit comments

Comments
 (0)