Skip to content

Commit

Permalink
C++ style enums 8/N: Direction (facebook#1392)
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/react-native#39483

Pull Request resolved: facebook#1392

Moves internal usages of YGDirection to Direction.

Changelog: [Internal]

Reviewed By: rshest

Differential Revision: D49335231

fbshipit-source-id: 86b40bb17fa7bf332964fa9d41bf60e6e446cc82
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Sep 19, 2023
1 parent ea3869f commit 6856e9f
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 108 deletions.
8 changes: 4 additions & 4 deletions tests/YGStyleAccessorsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ namespace facebook::yoga {

ACCESSOR_TEST(
direction,
YGDirectionInherit,
YGDirectionLTR,
YGDirectionRTL,
YGDirectionInherit);
Direction::Inherit,
Direction::LTR,
Direction::RTL,
Direction::Inherit);

ACCESSOR_TEST(
flexDirection,
Expand Down
27 changes: 14 additions & 13 deletions yoga/YGMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@

#ifdef __cplusplus

namespace facebook::yoga::enums {
namespace facebook::yoga {

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

namespace detail {
template <int... xs>
Expand All @@ -102,24 +103,24 @@ constexpr int n() {
}
} // namespace detail

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

#define YG_ENUM_DECL(NAME, ...) \
typedef YG_ENUM_BEGIN(NAME){__VA_ARGS__} YG_ENUM_END(NAME); \
YG_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::yoga::enums { \
template <> \
constexpr int count<NAME>() { \
return detail::n<__VA_ARGS__>(); \
} \
} \
#define YG_ENUM_SEQ_DECL(NAME, ...) \
YG_ENUM_DECL(NAME, __VA_ARGS__) \
YG_EXTERN_C_END \
\
namespace facebook::yoga { \
template <> \
constexpr int ordinalCount<NAME>() { \
return detail::n<__VA_ARGS__>(); \
} \
} \
YG_EXTERN_C_BEGIN
#else
#define YG_ENUM_SEQ_DECL YG_ENUM_DECL
Expand Down
117 changes: 74 additions & 43 deletions yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,10 @@ void updateIndexedStyleProp(
#define MSVC_HINT(PROP) decltype(Style{}.PROP())

void YGNodeStyleSetDirection(const YGNodeRef node, const YGDirection value) {
updateStyle<MSVC_HINT(direction)>(node, &Style::direction, value);
updateStyle<MSVC_HINT(direction)>(node, &Style::direction, scopedEnum(value));
}
YGDirection YGNodeStyleGetDirection(const YGNodeConstRef node) {
return resolveRef(node)->getStyle().direction();
return unscopedEnum(resolveRef(node)->getStyle().direction());
}

void YGNodeStyleSetFlexDirection(
Expand Down Expand Up @@ -754,51 +754,82 @@ YGValue YGNodeStyleGetMaxHeight(const YGNodeConstRef node) {
return resolveRef(node)->getStyle().maxDimensions()[YGDimensionHeight];
}

#define YG_NODE_LAYOUT_PROPERTY_IMPL(type, name, instanceName) \
type YGNodeLayoutGet##name(const YGNodeConstRef node) { \
return resolveRef(node)->getLayout().instanceName; \
namespace {

template <auto LayoutMember>
float getResolvedLayoutProperty(
const YGNodeConstRef nodeRef,
const YGEdge edge) {
const auto node = resolveRef(nodeRef);
yoga::assertFatalWithNode(
node,
edge <= YGEdgeEnd,
"Cannot get layout properties of multi-edge shorthands");

if (edge == YGEdgeStart) {
if (node->getLayout().direction() == Direction::RTL) {
return (node->getLayout().*LayoutMember)[YGEdgeRight];
} else {
return (node->getLayout().*LayoutMember)[YGEdgeLeft];
}
}

#define YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(type, name, instanceName) \
type YGNodeLayoutGet##name( \
const YGNodeConstRef nodeRef, const YGEdge edge) { \
const auto node = resolveRef(nodeRef); \
yoga::assertFatalWithNode( \
node, \
edge <= YGEdgeEnd, \
"Cannot get layout properties of multi-edge shorthands"); \
\
if (edge == YGEdgeStart) { \
if (node->getLayout().direction() == YGDirectionRTL) { \
return node->getLayout().instanceName[YGEdgeRight]; \
} else { \
return node->getLayout().instanceName[YGEdgeLeft]; \
} \
} \
\
if (edge == YGEdgeEnd) { \
if (node->getLayout().direction() == YGDirectionRTL) { \
return node->getLayout().instanceName[YGEdgeLeft]; \
} else { \
return node->getLayout().instanceName[YGEdgeRight]; \
} \
} \
\
return node->getLayout().instanceName[edge]; \
if (edge == YGEdgeEnd) {
if (node->getLayout().direction() == Direction::RTL) {
return (node->getLayout().*LayoutMember)[YGEdgeLeft];
} else {
return (node->getLayout().*LayoutMember)[YGEdgeRight];
}
}

YG_NODE_LAYOUT_PROPERTY_IMPL(float, Left, position[YGEdgeLeft])
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Top, position[YGEdgeTop])
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Right, position[YGEdgeRight])
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Bottom, position[YGEdgeBottom])
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Width, dimensions[YGDimensionWidth])
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Height, dimensions[YGDimensionHeight])
YG_NODE_LAYOUT_PROPERTY_IMPL(YGDirection, Direction, direction())
YG_NODE_LAYOUT_PROPERTY_IMPL(bool, HadOverflow, hadOverflow())
return (node->getLayout().*LayoutMember)[edge];
}

} // namespace

float YGNodeLayoutGetLeft(const YGNodeConstRef node) {
return resolveRef(node)->getLayout().position[YGEdgeLeft];
}

float YGNodeLayoutGetTop(const YGNodeConstRef node) {
return resolveRef(node)->getLayout().position[YGEdgeTop];
}

float YGNodeLayoutGetRight(const YGNodeConstRef node) {
return resolveRef(node)->getLayout().position[YGEdgeRight];
}

YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Margin, margin)
YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Border, border)
YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Padding, padding)
float YGNodeLayoutGetBottom(const YGNodeConstRef node) {
return resolveRef(node)->getLayout().position[YGEdgeBottom];
}

float YGNodeLayoutGetWidth(const YGNodeConstRef node) {
return resolveRef(node)->getLayout().dimensions[YGDimensionWidth];
}

float YGNodeLayoutGetHeight(const YGNodeConstRef node) {
return resolveRef(node)->getLayout().dimensions[YGDimensionHeight];
}

YGDirection YGNodeLayoutGetDirection(const YGNodeConstRef node) {
return unscopedEnum(resolveRef(node)->getLayout().direction());
}

bool YGNodeLayoutGetHadOverflow(const YGNodeConstRef node) {
return resolveRef(node)->getLayout().hadOverflow();
}

float YGNodeLayoutGetMargin(YGNodeConstRef node, YGEdge edge) {
return getResolvedLayoutProperty<&LayoutResults::margin>(node, edge);
}

float YGNodeLayoutGetBorder(YGNodeConstRef node, YGEdge edge) {
return getResolvedLayoutProperty<&LayoutResults::border>(node, edge);
}

float YGNodeLayoutGetPadding(YGNodeConstRef node, YGEdge edge) {
return getResolvedLayoutProperty<&LayoutResults::padding>(node, edge);
}

#ifdef DEBUG
void YGNodePrint(const YGNodeConstRef node, const YGPrintOptions options) {
Expand Down Expand Up @@ -926,5 +957,5 @@ void YGNodeCalculateLayout(
const float ownerHeight,
const YGDirection ownerDirection) {
yoga::calculateLayout(
resolveRef(node), ownerWidth, ownerHeight, ownerDirection);
resolveRef(node), ownerWidth, ownerHeight, scopedEnum(ownerDirection));
}
22 changes: 11 additions & 11 deletions yoga/algorithm/CalculateLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ bool calculateLayoutInternal(
yoga::Node* const node,
const float availableWidth,
const float availableHeight,
const YGDirection ownerDirection,
const Direction ownerDirection,
const MeasureMode widthMeasureMode,
const MeasureMode heightMeasureMode,
const float ownerWidth,
Expand Down Expand Up @@ -130,7 +130,7 @@ static void computeFlexBasisForChild(
const float ownerWidth,
const float ownerHeight,
const MeasureMode heightMode,
const YGDirection direction,
const Direction direction,
LayoutData& layoutMarkerData,
const uint32_t depth,
const uint32_t generationCount) {
Expand Down Expand Up @@ -321,7 +321,7 @@ static void layoutAbsoluteChild(
const float width,
const MeasureMode widthMode,
const float height,
const YGDirection direction,
const Direction direction,
LayoutData& layoutMarkerData,
const uint32_t depth,
const uint32_t generationCount) {
Expand Down Expand Up @@ -771,7 +771,7 @@ static float computeFlexBasisForChildren(
const float availableInnerHeight,
MeasureMode widthMeasureMode,
MeasureMode heightMeasureMode,
YGDirection direction,
Direction direction,
YGFlexDirection mainAxis,
bool performLayout,
LayoutData& layoutMarkerData,
Expand Down Expand Up @@ -812,7 +812,7 @@ static float computeFlexBasisForChildren(
}
if (performLayout) {
// Set the initial position (relative to the owner).
const YGDirection childDirection = child->resolveDirection(direction);
const Direction childDirection = child->resolveDirection(direction);
const float mainDim =
isRow(mainAxis) ? availableInnerWidth : availableInnerHeight;
const float crossDim =
Expand Down Expand Up @@ -1478,7 +1478,7 @@ static void calculateLayoutImpl(
yoga::Node* const node,
const float availableWidth,
const float availableHeight,
const YGDirection ownerDirection,
const Direction ownerDirection,
const MeasureMode widthMeasureMode,
const MeasureMode heightMeasureMode,
const float ownerWidth,
Expand Down Expand Up @@ -1506,7 +1506,7 @@ static void calculateLayoutImpl(
(performLayout ? layoutMarkerData.layouts : layoutMarkerData.measures) += 1;

// Set the resolved resolution in the node's layout.
const YGDirection direction = node->resolveDirection(ownerDirection);
const Direction direction = node->resolveDirection(ownerDirection);
node->setLayoutDirection(direction);

const YGFlexDirection flexRowDirection =
Expand All @@ -1515,8 +1515,8 @@ static void calculateLayoutImpl(
resolveDirection(YGFlexDirectionColumn, direction);

const YGEdge startEdge =
direction == YGDirectionLTR ? YGEdgeLeft : YGEdgeRight;
const YGEdge endEdge = direction == YGDirectionLTR ? YGEdgeRight : YGEdgeLeft;
direction == Direction::LTR ? YGEdgeLeft : YGEdgeRight;
const YGEdge endEdge = direction == Direction::LTR ? YGEdgeRight : YGEdgeLeft;

const float marginRowLeading =
node->getLeadingMargin(flexRowDirection, ownerWidth).unwrap();
Expand Down Expand Up @@ -2408,7 +2408,7 @@ bool calculateLayoutInternal(
yoga::Node* const node,
const float availableWidth,
const float availableHeight,
const YGDirection ownerDirection,
const Direction ownerDirection,
const MeasureMode widthMeasureMode,
const MeasureMode heightMeasureMode,
const float ownerWidth,
Expand Down Expand Up @@ -2668,7 +2668,7 @@ void calculateLayout(
yoga::Node* const node,
const float ownerWidth,
const float ownerHeight,
const YGDirection ownerDirection) {
const Direction ownerDirection) {
Event::publish<Event::LayoutPassStart>(node);
LayoutData markerData = {};

Expand Down
2 changes: 1 addition & 1 deletion yoga/algorithm/CalculateLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ void calculateLayout(
yoga::Node* const node,
const float ownerWidth,
const float ownerHeight,
const YGDirection ownerDirection);
const Direction ownerDirection);

} // namespace facebook::yoga
6 changes: 3 additions & 3 deletions yoga/algorithm/FlexDirection.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ inline bool isColumn(const YGFlexDirection flexDirection) {

inline YGFlexDirection resolveDirection(
const YGFlexDirection flexDirection,
const YGDirection direction) {
if (direction == YGDirectionRTL) {
const Direction direction) {
if (direction == Direction::RTL) {
if (flexDirection == YGFlexDirectionRow) {
return YGFlexDirectionRowReverse;
} else if (flexDirection == YGFlexDirectionRowReverse) {
Expand All @@ -39,7 +39,7 @@ inline YGFlexDirection resolveDirection(

inline YGFlexDirection resolveCrossDirection(
const YGFlexDirection flexDirection,
const YGDirection direction) {
const Direction direction) {
return isColumn(flexDirection)
? resolveDirection(YGFlexDirectionRow, direction)
: YGFlexDirectionColumn;
Expand Down
2 changes: 1 addition & 1 deletion yoga/algorithm/FlexLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace facebook::yoga {

FlexLine calculateFlexLine(
yoga::Node* const node,
const YGDirection ownerDirection,
const Direction ownerDirection,
const float mainAxisownerSize,
const float availableInnerWidth,
const float availableInnerMainDim,
Expand Down
2 changes: 1 addition & 1 deletion yoga/algorithm/FlexLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct FlexLine {
// computeFlexBasisForChildren function).
FlexLine calculateFlexLine(
yoga::Node* const node,
YGDirection ownerDirection,
Direction ownerDirection,
float mainAxisownerSize,
float availableInnerWidth,
float availableInnerMainDim,
Expand Down
18 changes: 11 additions & 7 deletions yoga/bits/NumericBitfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#include <bitset>
#include <cstdint>
#include <cstdio>
#include <type_traits>

#include <yoga/YGEnums.h>
#include <yoga/enums/YogaEnums.h>

namespace facebook::yoga::details {

Expand All @@ -27,12 +29,13 @@ constexpr uint32_t mask(uint8_t bitWidth, uint8_t index) {

namespace facebook::yoga {


// The number of bits necessary to represent enums defined with YG_ENUM_SEQ_DECL
template <typename Enum>
template <
typename Enum,
std::enable_if_t<(ordinalCount<Enum>() > 0), bool> = true>
constexpr uint8_t minimumBitCount() {
static_assert(
enums::count<Enum>() > 0, "Enums must have at least one entries");
return details::log2ceilFn(enums::count<Enum>() - 1);
return details::log2ceilFn(ordinalCount<Enum>() - 1);
}

template <typename Enum>
Expand All @@ -41,12 +44,13 @@ constexpr Enum getEnumData(uint32_t flags, uint8_t index) {
(flags & details::mask(minimumBitCount<Enum>(), index)) >> index);
}

template <typename Enum>
void setEnumData(uint32_t& flags, uint8_t index, uint32_t newValue) {
template <typename Enum, typename Value>
void setEnumData(uint32_t& flags, uint8_t index, Value newValue) {
flags =
(flags &
~static_cast<uint32_t>(details::mask(minimumBitCount<Enum>(), index))) |
((newValue << index) & (details::mask(minimumBitCount<Enum>(), index)));
((static_cast<uint32_t>(newValue) << index) &
(details::mask(minimumBitCount<Enum>(), index)));
}

constexpr bool getBooleanData(uint32_t flags, uint8_t index) {
Expand Down
Loading

0 comments on commit 6856e9f

Please sign in to comment.