Skip to content

Commit

Permalink
Remove enum count macros
Browse files Browse the repository at this point in the history
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
  • Loading branch information
davidaurelio authored and facebook-github-bot committed Jan 11, 2019
1 parent 866bf10 commit 4840495
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 48 deletions.
3 changes: 2 additions & 1 deletion yoga/YGConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#include "Yoga.h"

struct YGConfig {
std::array<bool, YGExperimentalFeatureCount> experimentalFeatures = {};
std::array<bool, facebook::yoga::enums::count<YGExperimentalFeature>()>
experimentalFeatures = {};
bool useWebDefaults = false;
bool useLegacyStretchBehaviour = false;
bool shouldDiffLayoutWithoutLegacyStretchBehaviour = false;
Expand Down
99 changes: 68 additions & 31 deletions yoga/YGEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,52 @@

#include "YGMacros.h"

#ifdef __cplusplus
namespace facebook {
namespace yoga {
namespace enums {

template <typename T>
constexpr int count(); // can't use `= delete` due to a defect in clang < 3.9

namespace detail {
template <int... xs>
constexpr int n() {
return sizeof...(xs);
}
} // namespace detail

} // namespace enums
} // namespace yoga
} // namespace facebook
#endif

#define YG_ENUM_DECL(NAME, ...) \
typedef YG_ENUM_BEGIN(NAME){__VA_ARGS__} YG_ENUM_END(NAME); \
WIN_EXPORT const char* NAME##ToString(NAME);

#ifdef __cplusplus
#define YG_ENUM_SEQ_DECL(NAME, ...) \
YG_ENUM_DECL(NAME, __VA_ARGS__) \
YG_EXTERN_C_END \
namespace facebook { \
namespace yoga { \
namespace enums { \
template <> \
constexpr int count<NAME>() { \
return detail::n<__VA_ARGS__>(); \
} \
} \
} \
} \
YG_EXTERN_C_BEGIN
#else
#define YG_ENUM_SEQ_DECL YG_ENUM_DECL
#endif

YG_EXTERN_C_BEGIN

#define YGAlignCount 8
YG_ENUM_DECL(
YG_ENUM_SEQ_DECL(
YGAlign,
YGAlignAuto,
YGAlignFlexStart,
Expand All @@ -26,17 +64,17 @@ YG_ENUM_DECL(
YGAlignSpaceBetween,
YGAlignSpaceAround);

#define YGDimensionCount 2
YG_ENUM_DECL(YGDimension, YGDimensionWidth, YGDimensionHeight)
YG_ENUM_SEQ_DECL(YGDimension, YGDimensionWidth, YGDimensionHeight)

#define YGDirectionCount 3
YG_ENUM_DECL(YGDirection, YGDirectionInherit, YGDirectionLTR, YGDirectionRTL)
YG_ENUM_SEQ_DECL(
YGDirection,
YGDirectionInherit,
YGDirectionLTR,
YGDirectionRTL)

#define YGDisplayCount 2
YG_ENUM_DECL(YGDisplay, YGDisplayFlex, YGDisplayNone)
YG_ENUM_SEQ_DECL(YGDisplay, YGDisplayFlex, YGDisplayNone)

#define YGEdgeCount 9
YG_ENUM_DECL(
YG_ENUM_SEQ_DECL(
YGEdge,
YGEdgeLeft,
YGEdgeTop,
Expand All @@ -48,19 +86,16 @@ YG_ENUM_DECL(
YGEdgeVertical,
YGEdgeAll)

#define YGExperimentalFeatureCount 1
YG_ENUM_DECL(YGExperimentalFeature, YGExperimentalFeatureWebFlexBasis)
YG_ENUM_SEQ_DECL(YGExperimentalFeature, YGExperimentalFeatureWebFlexBasis)

#define YGFlexDirectionCount 4
YG_ENUM_DECL(
YG_ENUM_SEQ_DECL(
YGFlexDirection,
YGFlexDirectionColumn,
YGFlexDirectionColumnReverse,
YGFlexDirectionRow,
YGFlexDirectionRowReverse)

#define YGJustifyCount 6
YG_ENUM_DECL(
YG_ENUM_SEQ_DECL(
YGJustify,
YGJustifyFlexStart,
YGJustifyCenter,
Expand All @@ -69,8 +104,7 @@ YG_ENUM_DECL(
YGJustifySpaceAround,
YGJustifySpaceEvenly)

#define YGLogLevelCount 6
YG_ENUM_DECL(
YG_ENUM_SEQ_DECL(
YGLogLevel,
YGLogLevelError,
YGLogLevelWarn,
Expand All @@ -79,35 +113,38 @@ YG_ENUM_DECL(
YGLogLevelVerbose,
YGLogLevelFatal)

#define YGMeasureModeCount 3
YG_ENUM_DECL(
YG_ENUM_SEQ_DECL(
YGMeasureMode,
YGMeasureModeUndefined,
YGMeasureModeExactly,
YGMeasureModeAtMost)

#define YGNodeTypeCount 2
YG_ENUM_DECL(YGNodeType, YGNodeTypeDefault, YGNodeTypeText)
YG_ENUM_SEQ_DECL(YGNodeType, YGNodeTypeDefault, YGNodeTypeText)

#define YGOverflowCount 3
YG_ENUM_DECL(YGOverflow, YGOverflowVisible, YGOverflowHidden, YGOverflowScroll)
YG_ENUM_SEQ_DECL(
YGOverflow,
YGOverflowVisible,
YGOverflowHidden,
YGOverflowScroll)

#define YGPositionTypeCount 2
YG_ENUM_DECL(YGPositionType, YGPositionTypeRelative, YGPositionTypeAbsolute)
YG_ENUM_SEQ_DECL(YGPositionType, YGPositionTypeRelative, YGPositionTypeAbsolute)

#define YGPrintOptionsCount 3
YG_ENUM_DECL(
YGPrintOptions,
YGPrintOptionsLayout = 1,
YGPrintOptionsStyle = 2,
YGPrintOptionsChildren = 4)

#define YGUnitCount 4
YG_ENUM_DECL(YGUnit, YGUnitUndefined, YGUnitPoint, YGUnitPercent, YGUnitAuto)
YG_ENUM_SEQ_DECL(
YGUnit,
YGUnitUndefined,
YGUnitPoint,
YGUnitPercent,
YGUnitAuto)

#define YGWrapCount 3
YG_ENUM_DECL(YGWrap, YGWrapNoWrap, YGWrapWrap, YGWrapWrapReverse)
YG_ENUM_SEQ_DECL(YGWrap, YGWrapNoWrap, YGWrapWrap, YGWrapWrapReverse)

YG_EXTERN_C_END

#undef YG_ENUM_DECL
#undef YG_ENUM_SEQ_DECL
3 changes: 2 additions & 1 deletion yoga/YGNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ YGValue YGNode::resolveFlexBasisPtr() const {
}

void YGNode::resolveDimension() {
for (uint32_t dim = YGDimensionWidth; dim < YGDimensionCount; dim++) {
using namespace yoga;
for (int dim = YGDimensionWidth; dim < enums::count<YGDimension>(); dim++) {
if (!getStyle().maxDimensions[dim].isUndefined() &&
YGValueEqual(
getStyle().maxDimensions[dim], style_.minDimensions[dim])) {
Expand Down
7 changes: 3 additions & 4 deletions yoga/YGNodePrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ static void indent(string& base, uint32_t level) {
}
}

static bool areFourValuesEqual(
const facebook::yoga::detail::Values<YGEdgeCount>& four) {
static bool areFourValuesEqual(const YGStyle::Edges& four) {
return YGValueEqual(four[0], four[1]) && YGValueEqual(four[0], four[2]) &&
YGValueEqual(four[0], four[3]);
}
Expand Down Expand Up @@ -86,7 +85,7 @@ static void appendNumberIfNotZero(
static void appendEdges(
string& base,
const string& key,
const facebook::yoga::detail::Values<YGEdgeCount>& edges) {
const YGStyle::Edges& edges) {
if (areFourValuesEqual(edges)) {
appendNumberIfNotZero(base, key, edges[YGEdgeLeft]);
} else {
Expand All @@ -100,7 +99,7 @@ static void appendEdges(
static void appendEdgeIfNotUndefined(
string& base,
const string& str,
const facebook::yoga::detail::Values<YGEdgeCount>& edges,
const YGStyle::Edges& edges,
const YGEdge edge) {
appendNumberIfNotUndefined(
base,
Expand Down
4 changes: 3 additions & 1 deletion yoga/YGStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <array>
#include <initializer_list>
#include "CompactValue.h"
#include "YGEnums.h"
#include "YGFloatOptional.h"
#include "Yoga-internal.h"
#include "Yoga.h"
Expand All @@ -29,7 +30,8 @@ struct YGStyle {

public:
using Dimensions = facebook::yoga::detail::Values<2>;
using Edges = facebook::yoga::detail::Values<YGEdgeCount>;
using Edges =
facebook::yoga::detail::Values<facebook::yoga::enums::count<YGEdge>()>;

/* Some platforms don't support enum bitfields,
so please use BITFIELD_ENUM_SIZED(BITS_COUNT) */
Expand Down
3 changes: 2 additions & 1 deletion yoga/Yoga-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ static const float kWebDefaultFlexShrink = 1.0f;
extern bool YGFloatsEqual(const float a, const float b);
extern bool YGValueEqual(const YGValue a, const YGValue b);
extern facebook::yoga::detail::CompactValue YGComputedEdgeValue(
const facebook::yoga::detail::Values<YGEdgeCount>& edges,
const facebook::yoga::detail::Values<
facebook::yoga::enums::count<YGEdge>()>& edges,
YGEdge edge,
facebook::yoga::detail::CompactValue defaultValue);
16 changes: 7 additions & 9 deletions yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ bool YGFloatIsUndefined(const float value) {
}

detail::CompactValue YGComputedEdgeValue(
const facebook::yoga::detail::Values<YGEdgeCount>& edges,
const YGStyle::Edges& edges,
YGEdge edge,
detail::CompactValue defaultValue) {
if (!edges[edge].isUndefined()) {
Expand Down Expand Up @@ -3546,14 +3546,12 @@ static const char* YGSpacer(const unsigned long level) {
static const char* YGMeasureModeName(
const YGMeasureMode mode,
const bool performLayout) {
const char* kMeasureModeNames[YGMeasureModeCount] = {
"UNDEFINED", "EXACTLY", "AT_MOST"};
const char* kLayoutModeNames[YGMeasureModeCount] = {"LAY_UNDEFINED",
"LAY_EXACTLY",
"LAY_AT_"
"MOST"};

if (mode >= YGMeasureModeCount) {
constexpr auto N = enums::count<YGMeasureMode>();
const char* kMeasureModeNames[N] = {"UNDEFINED", "EXACTLY", "AT_MOST"};
const char* kLayoutModeNames[N] = {
"LAY_UNDEFINED", "LAY_EXACTLY", "LAY_AT_MOST"};

if (mode >= N) {
return "";
}

Expand Down

0 comments on commit 4840495

Please sign in to comment.