C++ Cleanup 5/N: Reorganize Utils (#1357)
Summary: X-link: https://github.com/facebook/react-native/pull/39222 Pull Request resolved: https://github.com/facebook/yoga/pull/1357 X-link: https://github.com/facebook/react-native/pull/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. bypass-github-export-checks Reviewed By: shwanton Differential Revision: D48847260 fbshipit-source-id: b99df3029cd66257a7ae64de28c13e8751ceb20c
This commit is contained in:
committed by
Facebook GitHub Bot
parent
b959774af7
commit
c029041707
@@ -5,10 +5,13 @@
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include <yoga/node/Node.h>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <yoga/Utils.h>
|
||||
|
||||
#include <yoga/algorithm/FlexDirection.h>
|
||||
#include <yoga/algorithm/ResolveValue.h>
|
||||
#include <yoga/node/Node.h>
|
||||
#include <yoga/numeric/Comparison.h>
|
||||
|
||||
namespace facebook::yoga {
|
||||
|
||||
@@ -110,7 +113,7 @@ CompactValue Node::computeColumnGap(
|
||||
YGFloatOptional Node::getLeadingPosition(
|
||||
const YGFlexDirection axis,
|
||||
const float axisSize) const {
|
||||
auto leadingPosition = YGFlexDirectionIsRow(axis)
|
||||
auto leadingPosition = isRow(axis)
|
||||
? computeEdgeValueForRow(
|
||||
style_.position(),
|
||||
YGEdgeStart,
|
||||
@@ -118,13 +121,13 @@ YGFloatOptional Node::getLeadingPosition(
|
||||
CompactValue::ofZero())
|
||||
: computeEdgeValueForColumn(
|
||||
style_.position(), leading[axis], CompactValue::ofZero());
|
||||
return YGResolveValue(leadingPosition, axisSize);
|
||||
return yoga::resolveValue(leadingPosition, axisSize);
|
||||
}
|
||||
|
||||
YGFloatOptional Node::getTrailingPosition(
|
||||
const YGFlexDirection axis,
|
||||
const float axisSize) const {
|
||||
auto trailingPosition = YGFlexDirectionIsRow(axis)
|
||||
auto trailingPosition = isRow(axis)
|
||||
? computeEdgeValueForRow(
|
||||
style_.position(),
|
||||
YGEdgeEnd,
|
||||
@@ -132,11 +135,11 @@ YGFloatOptional Node::getTrailingPosition(
|
||||
CompactValue::ofZero())
|
||||
: computeEdgeValueForColumn(
|
||||
style_.position(), trailing[axis], CompactValue::ofZero());
|
||||
return YGResolveValue(trailingPosition, axisSize);
|
||||
return yoga::resolveValue(trailingPosition, axisSize);
|
||||
}
|
||||
|
||||
bool Node::isLeadingPositionDefined(const YGFlexDirection axis) const {
|
||||
auto leadingPosition = YGFlexDirectionIsRow(axis)
|
||||
auto leadingPosition = isRow(axis)
|
||||
? computeEdgeValueForRow(
|
||||
style_.position(),
|
||||
YGEdgeStart,
|
||||
@@ -148,7 +151,7 @@ bool Node::isLeadingPositionDefined(const YGFlexDirection axis) const {
|
||||
}
|
||||
|
||||
bool Node::isTrailingPosDefined(const YGFlexDirection axis) const {
|
||||
auto trailingPosition = YGFlexDirectionIsRow(axis)
|
||||
auto trailingPosition = isRow(axis)
|
||||
? computeEdgeValueForRow(
|
||||
style_.position(),
|
||||
YGEdgeEnd,
|
||||
@@ -162,23 +165,26 @@ bool Node::isTrailingPosDefined(const YGFlexDirection axis) const {
|
||||
YGFloatOptional Node::getLeadingMargin(
|
||||
const YGFlexDirection axis,
|
||||
const float widthSize) const {
|
||||
auto leadingMargin = YGFlexDirectionIsRow(axis)
|
||||
auto leadingMargin = isRow(axis)
|
||||
? computeEdgeValueForRow(
|
||||
style_.margin(), YGEdgeStart, leading[axis], CompactValue::ofZero())
|
||||
: computeEdgeValueForColumn(
|
||||
style_.margin(), leading[axis], CompactValue::ofZero());
|
||||
return YGResolveValueMargin(leadingMargin, widthSize);
|
||||
return leadingMargin.isAuto() ? YGFloatOptional{0}
|
||||
: yoga::resolveValue(leadingMargin, widthSize);
|
||||
}
|
||||
|
||||
YGFloatOptional Node::getTrailingMargin(
|
||||
const YGFlexDirection axis,
|
||||
const float widthSize) const {
|
||||
auto trailingMargin = YGFlexDirectionIsRow(axis)
|
||||
auto trailingMargin = isRow(axis)
|
||||
? computeEdgeValueForRow(
|
||||
style_.margin(), YGEdgeEnd, trailing[axis], CompactValue::ofZero())
|
||||
: computeEdgeValueForColumn(
|
||||
style_.margin(), trailing[axis], CompactValue::ofZero());
|
||||
return YGResolveValueMargin(trailingMargin, widthSize);
|
||||
return trailingMargin.isAuto()
|
||||
? YGFloatOptional{0}
|
||||
: yoga::resolveValue(trailingMargin, widthSize);
|
||||
}
|
||||
|
||||
YGFloatOptional Node::getMarginForAxis(
|
||||
@@ -190,10 +196,10 @@ YGFloatOptional Node::getMarginForAxis(
|
||||
YGFloatOptional Node::getGapForAxis(
|
||||
const YGFlexDirection axis,
|
||||
const float widthSize) const {
|
||||
auto gap = YGFlexDirectionIsRow(axis)
|
||||
auto gap = isRow(axis)
|
||||
? computeColumnGap(style_.gap(), CompactValue::ofZero())
|
||||
: computeRowGap(style_.gap(), CompactValue::ofZero());
|
||||
return YGResolveValue(gap, widthSize);
|
||||
return yoga::resolveValue(gap, widthSize);
|
||||
}
|
||||
|
||||
YGSize Node::measure(
|
||||
@@ -370,9 +376,9 @@ void Node::setPosition(
|
||||
const YGDirection directionRespectingRoot =
|
||||
owner_ != nullptr ? direction : YGDirectionLTR;
|
||||
const YGFlexDirection mainAxis =
|
||||
YGResolveFlexDirection(style_.flexDirection(), directionRespectingRoot);
|
||||
yoga::resolveDirection(style_.flexDirection(), directionRespectingRoot);
|
||||
const YGFlexDirection crossAxis =
|
||||
YGFlexDirectionCross(mainAxis, directionRespectingRoot);
|
||||
yoga::resolveCrossDirection(mainAxis, directionRespectingRoot);
|
||||
|
||||
// Here we should check for `YGPositionTypeStatic` and in this case zero inset
|
||||
// properties (left, right, top, bottom, begin, end).
|
||||
@@ -399,8 +405,7 @@ void Node::setPosition(
|
||||
}
|
||||
|
||||
YGValue Node::marginLeadingValue(const YGFlexDirection axis) const {
|
||||
if (YGFlexDirectionIsRow(axis) &&
|
||||
!style_.margin()[YGEdgeStart].isUndefined()) {
|
||||
if (isRow(axis) && !style_.margin()[YGEdgeStart].isUndefined()) {
|
||||
return style_.margin()[YGEdgeStart];
|
||||
} else {
|
||||
return style_.margin()[leading[axis]];
|
||||
@@ -408,7 +413,7 @@ YGValue Node::marginLeadingValue(const YGFlexDirection axis) const {
|
||||
}
|
||||
|
||||
YGValue Node::marginTrailingValue(const YGFlexDirection axis) const {
|
||||
if (YGFlexDirectionIsRow(axis) && !style_.margin()[YGEdgeEnd].isUndefined()) {
|
||||
if (isRow(axis) && !style_.margin()[YGEdgeEnd].isUndefined()) {
|
||||
return style_.margin()[YGEdgeEnd];
|
||||
} else {
|
||||
return style_.margin()[trailing[axis]];
|
||||
@@ -431,7 +436,8 @@ void Node::resolveDimension() {
|
||||
const Style& style = getStyle();
|
||||
for (auto dim : {YGDimensionWidth, YGDimensionHeight}) {
|
||||
if (!style.maxDimensions()[dim].isUndefined() &&
|
||||
YGValueEqual(style.maxDimensions()[dim], style.minDimensions()[dim])) {
|
||||
yoga::inexactEquals(
|
||||
style.maxDimensions()[dim], style.minDimensions()[dim])) {
|
||||
resolvedDimensions_[dim] = style.maxDimensions()[dim];
|
||||
} else {
|
||||
resolvedDimensions_[dim] = style.dimensions()[dim];
|
||||
@@ -511,7 +517,7 @@ bool Node::isNodeFlexible() {
|
||||
}
|
||||
|
||||
float Node::getLeadingBorder(const YGFlexDirection axis) const {
|
||||
YGValue leadingBorder = YGFlexDirectionIsRow(axis)
|
||||
YGValue leadingBorder = isRow(axis)
|
||||
? computeEdgeValueForRow(
|
||||
style_.border(), YGEdgeStart, leading[axis], CompactValue::ofZero())
|
||||
: computeEdgeValueForColumn(
|
||||
@@ -520,7 +526,7 @@ float Node::getLeadingBorder(const YGFlexDirection axis) const {
|
||||
}
|
||||
|
||||
float Node::getTrailingBorder(const YGFlexDirection axis) const {
|
||||
YGValue trailingBorder = YGFlexDirectionIsRow(axis)
|
||||
YGValue trailingBorder = isRow(axis)
|
||||
? computeEdgeValueForRow(
|
||||
style_.border(), YGEdgeEnd, trailing[axis], CompactValue::ofZero())
|
||||
: computeEdgeValueForColumn(
|
||||
@@ -531,7 +537,7 @@ float Node::getTrailingBorder(const YGFlexDirection axis) const {
|
||||
YGFloatOptional Node::getLeadingPadding(
|
||||
const YGFlexDirection axis,
|
||||
const float widthSize) const {
|
||||
auto leadingPadding = YGFlexDirectionIsRow(axis)
|
||||
auto leadingPadding = isRow(axis)
|
||||
? computeEdgeValueForRow(
|
||||
style_.padding(),
|
||||
YGEdgeStart,
|
||||
@@ -539,20 +545,20 @@ YGFloatOptional Node::getLeadingPadding(
|
||||
CompactValue::ofZero())
|
||||
: computeEdgeValueForColumn(
|
||||
style_.padding(), leading[axis], CompactValue::ofZero());
|
||||
return YGFloatOptionalMax(
|
||||
YGResolveValue(leadingPadding, widthSize), YGFloatOptional(0.0f));
|
||||
return yoga::maxOrDefined(
|
||||
yoga::resolveValue(leadingPadding, widthSize), YGFloatOptional(0.0f));
|
||||
}
|
||||
|
||||
YGFloatOptional Node::getTrailingPadding(
|
||||
const YGFlexDirection axis,
|
||||
const float widthSize) const {
|
||||
auto trailingPadding = YGFlexDirectionIsRow(axis)
|
||||
auto trailingPadding = isRow(axis)
|
||||
? computeEdgeValueForRow(
|
||||
style_.padding(), YGEdgeEnd, trailing[axis], CompactValue::ofZero())
|
||||
: computeEdgeValueForColumn(
|
||||
style_.padding(), trailing[axis], CompactValue::ofZero());
|
||||
return YGFloatOptionalMax(
|
||||
YGResolveValue(trailingPadding, widthSize), YGFloatOptional(0.0f));
|
||||
return yoga::maxOrDefined(
|
||||
yoga::resolveValue(trailingPadding, widthSize), YGFloatOptional(0.0f));
|
||||
}
|
||||
|
||||
YGFloatOptional Node::getLeadingPaddingAndBorder(
|
||||
|
Reference in New Issue
Block a user