Skip to content

Commit

Permalink
C++ Cleanup 6/N: YGFloatOptional
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/react-native#39196

## This diff

This renames YGFloatOptional to FloatOptional, adds it to a namespace, and moves it to a subdirectory. This needs Fabric updates because Fabric uses Yoga internals for props storage.

## This stack

The organization of the C++ internals of Yoga are in need of attention.
1. Some of the C++ internals are namespaced, but others not.
2. Some of the namespaces include `detail`, but are meant to be used outside of the translation unit (FB Clang Tidy rules warn on any usage of these)
2. Most of the files are in a flat hierarchy, except for event tracing in its own folder
3. Some files and functions begin with YG, others don’t
4. Some functions are uppercase, others are not
5. Almost all of the interesting logic is in Yoga.cpp, and the file is too large to reason about
6. There are multiple grab bag files where folks put random functions they need in (Utils, BitUtils, Yoga-Internal.h)
7. There is no clear indication from file structure or type naming what is private vs not
8. Handles like `YGNodeRef` and `YGConfigRef` can be used to access internals just by importing headers

This stack does some much needed spring cleaning:
1. All non-public headers and C++ implementation details are in separate folders from the root level `yoga`. This will give us room to split up logic and add more files without too large a flat hierarchy
3. All private C++ internals are under the `facebook::yoga` namespace. Details namespaces are only ever used within the same header, as they are intended
4. Utils files are split
5. Most C++ internals drop the YG prefix
6. Most C++ internal function names are all lower camel case
7. We start to split up Yoga.cpp
8. Every header beginning with YG or at the top-level directory is public and C only, with the exception of Yoga-Internal.h which has non-public functions for bindings
9. It is not possible to use private APIs without static casting handles to internal classes

This will give us more leeway to continue splitting monolithic files, and consistent guidelines for style in new files as well.

These changes should not be breaking to any project using only public Yoga headers. This includes every usage of Yoga in fbsource except for RN Fabric which is currently tied to internals. This refactor should make that boundary clearer.

Changelog: [Internal]

Reviewed By: rshest

Differential Revision: D48767992

fbshipit-source-id: 32c95367a599040c781f5fabf4cfb64e609bde9b
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Aug 30, 2023
1 parent 05b6265 commit ba25328
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 214 deletions.
59 changes: 30 additions & 29 deletions tests/YGFloatOptionalTest.cpp → tests/FloatOptionalTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@
#include <gtest/gtest.h>

#include <yoga/numeric/Comparison.h>
#include <yoga/YGFloatOptional.h>
#include <yoga/numeric/FloatOptional.h>
#include <yoga/YGValue.h>

using namespace facebook;
using namespace facebook::yoga;

constexpr auto empty = YGFloatOptional{};
constexpr auto zero = YGFloatOptional{0.0f};
constexpr auto one = YGFloatOptional{1.0f};
constexpr auto positive = YGFloatOptional{1234.5f};
constexpr auto negative = YGFloatOptional{-9876.5f};
constexpr auto empty = FloatOptional{};
constexpr auto zero = FloatOptional{0.0f};
constexpr auto one = FloatOptional{1.0f};
constexpr auto positive = FloatOptional{1234.5f};
constexpr auto negative = FloatOptional{-9876.5f};

TEST(YGFloatOptional, value) {
TEST(FloatOptional, value) {
ASSERT_TRUE(YGFloatIsUndefined(empty.unwrap()));
ASSERT_EQ(zero.unwrap(), 0.0f);
ASSERT_EQ(one.unwrap(), 1.0f);
Expand All @@ -33,7 +34,7 @@ TEST(YGFloatOptional, value) {
ASSERT_FALSE(negative.isUndefined());
}

TEST(YGFloatOptional, equality) {
TEST(FloatOptional, equality) {
ASSERT_TRUE(empty == empty);
ASSERT_TRUE(empty == YGUndefined);
ASSERT_FALSE(empty == zero);
Expand All @@ -58,7 +59,7 @@ TEST(YGFloatOptional, equality) {
ASSERT_FALSE(negative == zero);
}

TEST(YGFloatOptional, inequality) {
TEST(FloatOptional, inequality) {
ASSERT_FALSE(empty != empty);
ASSERT_FALSE(empty != YGUndefined);
ASSERT_TRUE(empty != zero);
Expand All @@ -83,7 +84,7 @@ TEST(YGFloatOptional, inequality) {
ASSERT_TRUE(negative != zero);
}

TEST(YGFloatOptional, greater_than_with_undefined) {
TEST(FloatOptional, greater_than_with_undefined) {
ASSERT_FALSE(empty > empty);
ASSERT_FALSE(empty > zero);
ASSERT_FALSE(empty > one);
Expand All @@ -95,7 +96,7 @@ TEST(YGFloatOptional, greater_than_with_undefined) {
ASSERT_FALSE(negative > empty);
}

TEST(YGFloatOptional, greater_than) {
TEST(FloatOptional, greater_than) {
ASSERT_TRUE(zero > negative);
ASSERT_FALSE(zero > zero);
ASSERT_FALSE(zero > positive);
Expand All @@ -105,10 +106,10 @@ TEST(YGFloatOptional, greater_than) {
ASSERT_TRUE(one > zero);
ASSERT_FALSE(one > positive);

ASSERT_TRUE(negative > YGFloatOptional{-INFINITY});
ASSERT_TRUE(negative > FloatOptional{-INFINITY});
}

TEST(YGFloatOptional, less_than_with_undefined) {
TEST(FloatOptional, less_than_with_undefined) {
ASSERT_FALSE(empty < empty);
ASSERT_FALSE(zero < empty);
ASSERT_FALSE(one < empty);
Expand All @@ -120,7 +121,7 @@ TEST(YGFloatOptional, less_than_with_undefined) {
ASSERT_FALSE(empty < negative);
}

TEST(YGFloatOptional, less_than) {
TEST(FloatOptional, less_than) {
ASSERT_TRUE(negative < zero);
ASSERT_FALSE(zero < zero);
ASSERT_FALSE(positive < zero);
Expand All @@ -130,10 +131,10 @@ TEST(YGFloatOptional, less_than) {
ASSERT_TRUE(zero < one);
ASSERT_FALSE(positive < one);

ASSERT_TRUE(YGFloatOptional{-INFINITY} < negative);
ASSERT_TRUE(FloatOptional{-INFINITY} < negative);
}

TEST(YGFloatOptional, greater_than_equals_with_undefined) {
TEST(FloatOptional, greater_than_equals_with_undefined) {
ASSERT_TRUE(empty >= empty);
ASSERT_FALSE(empty >= zero);
ASSERT_FALSE(empty >= one);
Expand All @@ -145,7 +146,7 @@ TEST(YGFloatOptional, greater_than_equals_with_undefined) {
ASSERT_FALSE(negative >= empty);
}

TEST(YGFloatOptional, greater_than_equals) {
TEST(FloatOptional, greater_than_equals) {
ASSERT_TRUE(zero >= negative);
ASSERT_TRUE(zero >= zero);
ASSERT_FALSE(zero >= positive);
Expand All @@ -155,10 +156,10 @@ TEST(YGFloatOptional, greater_than_equals) {
ASSERT_TRUE(one >= zero);
ASSERT_FALSE(one >= positive);

ASSERT_TRUE(negative >= YGFloatOptional{-INFINITY});
ASSERT_TRUE(negative >= FloatOptional{-INFINITY});
}

TEST(YGFloatOptional, less_than_equals_with_undefined) {
TEST(FloatOptional, less_than_equals_with_undefined) {
ASSERT_TRUE(empty <= empty);
ASSERT_FALSE(zero <= empty);
ASSERT_FALSE(one <= empty);
Expand All @@ -170,7 +171,7 @@ TEST(YGFloatOptional, less_than_equals_with_undefined) {
ASSERT_FALSE(empty <= negative);
}

TEST(YGFloatOptional, less_than_equals) {
TEST(FloatOptional, less_than_equals) {
ASSERT_TRUE(negative <= zero);
ASSERT_TRUE(zero <= zero);
ASSERT_FALSE(positive <= zero);
Expand All @@ -180,32 +181,32 @@ TEST(YGFloatOptional, less_than_equals) {
ASSERT_TRUE(zero <= one);
ASSERT_FALSE(positive <= one);

ASSERT_TRUE(YGFloatOptional{-INFINITY} <= negative);
ASSERT_TRUE(FloatOptional{-INFINITY} <= negative);
}

TEST(YGFloatOptional, addition) {
TEST(FloatOptional, addition) {
auto n = negative.unwrap();
auto p = positive.unwrap();

ASSERT_EQ(zero + one, one);
ASSERT_EQ(negative + positive, YGFloatOptional{n + p});
ASSERT_EQ(negative + positive, FloatOptional{n + p});
ASSERT_EQ(empty + zero, empty);
ASSERT_EQ(empty + empty, empty);
ASSERT_EQ(negative + empty, empty);
}

TEST(YGFloatOptionalTest, maxOrDefined) {
TEST(YGFloatOptiona, maxOrDefined) {
ASSERT_EQ(yoga::maxOrDefined(empty, empty), empty);
ASSERT_EQ(yoga::maxOrDefined(empty, positive), positive);
ASSERT_EQ(yoga::maxOrDefined(negative, empty), negative);
ASSERT_EQ(yoga::maxOrDefined(negative, YGFloatOptional{-INFINITY}), negative);
ASSERT_EQ(yoga::maxOrDefined(negative, FloatOptional{-INFINITY}), negative);
ASSERT_EQ(
yoga::maxOrDefined(YGFloatOptional{1.0f}, YGFloatOptional{1.125f}),
YGFloatOptional{1.125f});
yoga::maxOrDefined(FloatOptional{1.0f}, FloatOptional{1.125f}),
FloatOptional{1.125f});
}

TEST(YGFloatOptionalTest, unwrap) {
TEST(FloatOptional, unwrap) {
ASSERT_TRUE(YGFloatIsUndefined(empty.unwrap()));
ASSERT_EQ(zero.unwrap(), 0.0f);
ASSERT_EQ(YGFloatOptional{123456.78f}.unwrap(), 123456.78f);
ASSERT_EQ(FloatOptional{123456.78f}.unwrap(), 123456.78f);
}
34 changes: 17 additions & 17 deletions tests/YGStyleAccessorsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,24 @@ ACCESSOR_TEST(display, YGDisplayFlex, YGDisplayNone, YGDisplayFlex)

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

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

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

ACCESSOR_TEST(
flexBasis,
Expand Down Expand Up @@ -243,11 +243,11 @@ INDEX_ACCESSOR_TEST(

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

#endif

Expand Down
70 changes: 0 additions & 70 deletions yoga/YGFloatOptional.h

This file was deleted.

Loading

0 comments on commit ba25328

Please sign in to comment.