Skip to content

Commit

Permalink
Revert D50998164: Allow lazy resolution of edge dimension values
Browse files Browse the repository at this point in the history
Differential Revision:
D50998164

Original commit changeset: 248396f9587e

Original Phabricator Diff: D50998164

fbshipit-source-id: 4f592158324d758bb9e3731ced36b8e3587c459c
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Nov 16, 2023
1 parent f2c8aca commit 27af596
Show file tree
Hide file tree
Showing 9 changed files with 416 additions and 124 deletions.
216 changes: 216 additions & 0 deletions tests/YGStyleAccessorsTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
* 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.
*/

#include <gtest/gtest.h>
#include <yoga/Yoga.h>
#include <yoga/style/Style.h>
#include <cstdint>
#include <type_traits>

#define ACCESSOR_TESTS_1(NAME, X) \
style.NAME() = X; \
ASSERT_EQ(style.NAME(), X);
#define ACCESSOR_TESTS_2(NAME, X, ...) \
ACCESSOR_TESTS_1(NAME, X); \
ACCESSOR_TESTS_1(NAME, __VA_ARGS__);
#define ACCESSOR_TESTS_3(NAME, X, ...) \
ACCESSOR_TESTS_1(NAME, X); \
ACCESSOR_TESTS_2(NAME, __VA_ARGS__);
#define ACCESSOR_TESTS_4(NAME, X, ...) \
ACCESSOR_TESTS_1(NAME, X); \
ACCESSOR_TESTS_3(NAME, __VA_ARGS__);
#define ACCESSOR_TESTS_5(NAME, X, ...) \
ACCESSOR_TESTS_1(NAME, X); \
ACCESSOR_TESTS_4(NAME, __VA_ARGS__)

#define ACCESSOR_TESTS_N(a, b, c, d, e, COUNT, ...) ACCESSOR_TESTS_##COUNT
#define ACCESSOR_TESTS(...) ACCESSOR_TESTS_N(__VA_ARGS__, 5, 4, 3, 2, 1)

#define INDEX_ACCESSOR_TESTS_1(NAME, IDX, X) \
{ \
auto style = Style{}; \
style.NAME()[IDX] = X; \
ASSERT_EQ(style.NAME()[IDX], X); \
auto asArray = decltype(std::declval<const Style&>().NAME()){X}; \
style.NAME() = asArray; \
ASSERT_EQ(static_cast<decltype(asArray)>(style.NAME()), asArray); \
}

#define INDEX_ACCESSOR_TESTS_2(NAME, IDX, X, Y) \
INDEX_ACCESSOR_TESTS_1(NAME, IDX, X) \
INDEX_ACCESSOR_TESTS_1(NAME, IDX, Y)

#define INDEX_ACCESSOR_TESTS_3(NAME, IDX, X, ...) \
INDEX_ACCESSOR_TESTS_1(NAME, IDX, X) \
INDEX_ACCESSOR_TESTS_2(NAME, IDX, __VA_ARGS__)

#define INDEX_ACCESSOR_TESTS_4(NAME, IDX, X, ...) \
INDEX_ACCESSOR_TESTS_1(NAME, IDX, X) \
INDEX_ACCESSOR_TESTS_3(NAME, IDX, __VA_ARGS__)

#define INDEX_ACCESSOR_TESTS_5(NAME, IDX, X, ...) \
INDEX_ACCESSOR_TESTS_1(NAME, IDX, X) \
INDEX_ACCESSOR_TESTS_4(NAME, IDX, __VA_ARGS__)

#define INDEX_ACCESSOR_TESTS_N(a, b, c, d, e, COUNT, ...) \
INDEX_ACCESSOR_TESTS_##COUNT
#define INDEX_ACCESSOR_TESTS(...) \
INDEX_ACCESSOR_TESTS_N(__VA_ARGS__, 5, 4, 3, 2, 1)

// test macro for up to 5 values. If more are needed, extend the macros above.
#define ACCESSOR_TEST(NAME, DEFAULT_VAL, ...) \
TEST(Style, style_##NAME##_access) { \
auto style = Style{}; \
ASSERT_EQ(style.NAME(), DEFAULT_VAL); \
ACCESSOR_TESTS(__VA_ARGS__)(NAME, __VA_ARGS__) \
}

#define INDEX_ACCESSOR_TEST(NAME, DEFAULT_VAL, IDX, ...) \
TEST(Style, style_##NAME##_access) { \
ASSERT_EQ(Style{}.NAME()[IDX], DEFAULT_VAL); \
INDEX_ACCESSOR_TESTS(__VA_ARGS__)(NAME, IDX, __VA_ARGS__) \
}

namespace facebook::yoga {

// TODO: MSVC doesn't like the macros
#ifndef _MSC_VER

ACCESSOR_TEST(
direction,
Direction::Inherit,
Direction::LTR,
Direction::RTL,
Direction::Inherit);

ACCESSOR_TEST(
flexDirection,
FlexDirection::Column,
FlexDirection::ColumnReverse,
FlexDirection::RowReverse,
FlexDirection::Row)

ACCESSOR_TEST(
justifyContent,
Justify::FlexStart,
Justify::FlexEnd,
Justify::SpaceAround,
Justify::FlexStart,
Justify::SpaceEvenly)

ACCESSOR_TEST(
alignContent,
Align::FlexStart,
Align::Auto,
Align::FlexStart,
Align::Center,
Align::FlexEnd,
Align::Stretch)

ACCESSOR_TEST(
alignItems,
Align::Stretch,
Align::FlexStart,
Align::FlexEnd,
Align::Baseline,
Align::SpaceBetween,
Align::SpaceAround)

ACCESSOR_TEST(
alignSelf,
Align::Auto,
Align::FlexStart,
Align::Center,
Align::Auto,
Align::FlexEnd,
Align::Stretch)

ACCESSOR_TEST(
positionType,
PositionType::Static,
PositionType::Absolute,
PositionType::Relative)

ACCESSOR_TEST(flexWrap, Wrap::NoWrap, Wrap::Wrap, Wrap::WrapReverse)

ACCESSOR_TEST(overflow, Overflow::Visible, Overflow::Hidden, Overflow::Scroll)

ACCESSOR_TEST(display, Display::Flex, Display::None, Display::Flex)

ACCESSOR_TEST(
flex,
FloatOptional{},
FloatOptional{123.45f},
FloatOptional{-9.87f},
FloatOptional{})

ACCESSOR_TEST(
flexGrow,
FloatOptional{},
FloatOptional{123.45f},
FloatOptional{-9.87f},
FloatOptional{})

ACCESSOR_TEST(
flexShrink,
FloatOptional{},
FloatOptional{123.45f},
FloatOptional{-9.87f},
FloatOptional{})

ACCESSOR_TEST(
flexBasis,
CompactValue::ofAuto(),
CompactValue::ofUndefined(),
CompactValue::ofAuto(),
CompactValue::of<YGUnitPoint>(7777.77f),
CompactValue::of<YGUnitPercent>(-100.0f))

INDEX_ACCESSOR_TEST(
position,
CompactValue::ofUndefined(),
YGEdgeBottom,
CompactValue::ofAuto(),
CompactValue::ofUndefined(),
CompactValue::of<YGUnitPoint>(7777.77f),
CompactValue::of<YGUnitPercent>(-100.0f))

INDEX_ACCESSOR_TEST(
margin,
CompactValue::ofUndefined(),
YGEdgeTop,
CompactValue::ofAuto(),
CompactValue::ofUndefined(),
CompactValue::of<YGUnitPoint>(7777.77f),
CompactValue::of<YGUnitPercent>(-100.0f))

INDEX_ACCESSOR_TEST(
padding,
CompactValue::ofUndefined(),
YGEdgeAll,
CompactValue::of<YGUnitPoint>(7777.77f),
CompactValue::ofUndefined(),
CompactValue::of<YGUnitPercent>(-100.0f))

INDEX_ACCESSOR_TEST(
border,
CompactValue::ofUndefined(),
YGEdgeHorizontal,
CompactValue::of<YGUnitPoint>(-7777.77f),
CompactValue::ofUndefined())

ACCESSOR_TEST(
aspectRatio,
FloatOptional{},
FloatOptional{-123.45f},
FloatOptional{9876.5f},
FloatOptional{0.0f},
FloatOptional{});

#endif

} // namespace facebook::yoga
47 changes: 30 additions & 17 deletions yoga/YGNodeStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ void updateStyle(YGNodeRef node, Ref (Style::*prop)(), T value) {
[prop](Style& s, T x) { (s.*prop)() = x; });
}

template <typename Ref, typename Idx>
void updateIndexedStyleProp(
YGNodeRef node,
Ref (Style::*prop)(),
Idx idx,
CompactValue value) {
updateStyle(
resolveRef(node),
value,
[idx, prop](Style& s, CompactValue x) { return (s.*prop)()[idx] != x; },
[idx, prop](Style& s, CompactValue x) { (s.*prop)()[idx] = x; });
}

template <auto GetterT, auto SetterT, typename IdxT>
void updateIndexedStyleProp(YGNodeRef node, IdxT idx, CompactValue value) {
updateStyle(
Expand Down Expand Up @@ -224,65 +237,65 @@ YGValue YGNodeStyleGetFlexBasis(const YGNodeConstRef node) {

void YGNodeStyleSetPosition(YGNodeRef node, YGEdge edge, float points) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<&Style::position, &Style::setPosition>(
node, edge, value);
updateIndexedStyleProp<MSVC_HINT(position)>(
node, &Style::position, edge, value);
}

void YGNodeStyleSetPositionPercent(YGNodeRef node, YGEdge edge, float percent) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<&Style::position, &Style::setPosition>(
node, edge, value);
updateIndexedStyleProp<MSVC_HINT(position)>(
node, &Style::position, edge, value);
}

YGValue YGNodeStyleGetPosition(YGNodeConstRef node, YGEdge edge) {
return resolveRef(node)->getStyle().position(edge);
return resolveRef(node)->getStyle().position()[edge];
}

void YGNodeStyleSetMargin(YGNodeRef node, YGEdge edge, float points) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<&Style::margin, &Style::setMargin>(node, edge, value);
updateIndexedStyleProp<MSVC_HINT(margin)>(node, &Style::margin, edge, value);
}

void YGNodeStyleSetMarginPercent(YGNodeRef node, YGEdge edge, float percent) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<&Style::margin, &Style::setMargin>(node, edge, value);
updateIndexedStyleProp<MSVC_HINT(margin)>(node, &Style::margin, edge, value);
}

void YGNodeStyleSetMarginAuto(YGNodeRef node, YGEdge edge) {
updateIndexedStyleProp<&Style::margin, &Style::setMargin>(
node, edge, CompactValue::ofAuto());
updateIndexedStyleProp<MSVC_HINT(margin)>(
node, &Style::margin, edge, CompactValue::ofAuto());
}

YGValue YGNodeStyleGetMargin(YGNodeConstRef node, YGEdge edge) {
return resolveRef(node)->getStyle().margin(edge);
return resolveRef(node)->getStyle().margin()[edge];
}

void YGNodeStyleSetPadding(YGNodeRef node, YGEdge edge, float points) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<&Style::padding, &Style::setPadding>(
node, edge, value);
updateIndexedStyleProp<MSVC_HINT(padding)>(
node, &Style::padding, edge, value);
}

void YGNodeStyleSetPaddingPercent(YGNodeRef node, YGEdge edge, float percent) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<&Style::padding, &Style::setPadding>(
node, edge, value);
updateIndexedStyleProp<MSVC_HINT(padding)>(
node, &Style::padding, edge, value);
}

YGValue YGNodeStyleGetPadding(YGNodeConstRef node, YGEdge edge) {
return resolveRef(node)->getStyle().padding(edge);
return resolveRef(node)->getStyle().padding()[edge];
}

void YGNodeStyleSetBorder(
const YGNodeRef node,
const YGEdge edge,
const float border) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(border);
updateIndexedStyleProp<&Style::border, &Style::setBorder>(node, edge, value);
updateIndexedStyleProp<MSVC_HINT(border)>(node, &Style::border, edge, value);
}

float YGNodeStyleGetBorder(const YGNodeConstRef node, const YGEdge edge) {
auto border = resolveRef(node)->getStyle().border(edge);
auto border = resolveRef(node)->getStyle().border()[edge];
if (border.isUndefined() || border.isAuto()) {
return YGUndefined;
}
Expand Down
1 change: 0 additions & 1 deletion yoga/algorithm/FlexDirection.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include <yoga/debug/AssertFatal.h>
#include <yoga/enums/Dimension.h>
#include <yoga/enums/Direction.h>
#include <yoga/enums/FlexDirection.h>

namespace facebook::yoga {
Expand Down
2 changes: 0 additions & 2 deletions yoga/debug/AssertFatal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

#include <stdexcept>

#include <yoga/config/Config.h>
#include <yoga/debug/AssertFatal.h>
#include <yoga/debug/Log.h>
#include <yoga/node/Node.h>

namespace facebook::yoga {

Expand Down
5 changes: 2 additions & 3 deletions yoga/debug/AssertFatal.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
#pragma once

#include <yoga/Yoga.h>
#include <yoga/config/Config.h>
#include <yoga/node/Node.h>

namespace facebook::yoga {

class Node;
class Config;

[[noreturn]] void fatalWithMessage(const char* message);

void assertFatal(bool condition, const char* message);
Expand Down
Loading

0 comments on commit 27af596

Please sign in to comment.