Skip to content

Commit

Permalink
C++ Cleanup 5/N: Reorganize Utils (facebook#1357)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#1357

X-link: facebook/react-native#39199

## This diff

This splits `Utils.h` and `Utils.cpp`, and tweaks naming and namespaces.

## 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.

Reviewed By: rshest

Differential Revision: D48767465

fbshipit-source-id: 5be8ed5d5062072e8544c13b0833ad15c684ca23
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Aug 30, 2023
1 parent e33e511 commit f2f3ed8
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 398 deletions.
16 changes: 9 additions & 7 deletions tests/YGFloatOptionalTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

#include <gtest/gtest.h>

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

using namespace facebook;

constexpr auto empty = YGFloatOptional{};
constexpr auto zero = YGFloatOptional{0.0f};
constexpr auto one = YGFloatOptional{1.0f};
Expand Down Expand Up @@ -192,13 +194,13 @@ TEST(YGFloatOptional, addition) {
ASSERT_EQ(negative + empty, empty);
}

TEST(YGFloatOptionalTest, YGFloatOptionalMax) {
ASSERT_EQ(YGFloatOptionalMax(empty, empty), empty);
ASSERT_EQ(YGFloatOptionalMax(empty, positive), positive);
ASSERT_EQ(YGFloatOptionalMax(negative, empty), negative);
ASSERT_EQ(YGFloatOptionalMax(negative, YGFloatOptional{-INFINITY}), negative);
TEST(YGFloatOptionalTest, 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(
YGFloatOptionalMax(YGFloatOptional{1.0f}, YGFloatOptional{1.125f}),
yoga::maxOrDefined(YGFloatOptional{1.0f}, YGFloatOptional{1.125f}),
YGFloatOptional{1.125f});
}

Expand Down
83 changes: 0 additions & 83 deletions yoga/Utils.cpp

This file was deleted.

146 changes: 0 additions & 146 deletions yoga/Utils.h

This file was deleted.

4 changes: 0 additions & 4 deletions yoga/Yoga-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ inline bool isUndefined(double value) {
return std::isnan(value);
}

void throwLogicalErrorWithMessage(const char* message);

} // namespace facebook::yoga

extern const std::array<YGEdge, 4> trailing;
Expand Down Expand Up @@ -149,5 +147,3 @@ class Values {
static const float kDefaultFlexGrow = 0.0f;
static const float kDefaultFlexShrink = 0.0f;
static const float kWebDefaultFlexShrink = 1.0f;

extern bool YGFloatsEqual(const float a, const float b);
Loading

0 comments on commit f2f3ed8

Please sign in to comment.