Eliminate YGFloatOptional::getValue()

Summary:
@public

Replace `YGFloatOptional::getValue()` with `YGFloatOptional::unwrap()`.

`YGFloatOptional::getValue()` has the unfortunate property of calling `std::exit` if the wrapped value is undefined.

Here, we eliminate the method, and just call `.unwrap()` everywhere.

Reviewed By: shergin

Differential Revision: D13439608

fbshipit-source-id: 5ae82b170537d0a10c301412567a7a66fd50bab4
This commit is contained in:
David Aurelio 2018-12-13 07:09:30 -08:00 committed by Facebook Github Bot
parent 110c1c260f
commit ada4831580
9 changed files with 46 additions and 59 deletions

View File

@ -40,7 +40,7 @@ inline Float floatFromYogaOptionalFloat(YGFloatOptional value) {
return kFloatUndefined; return kFloatUndefined;
} }
return floatFromYogaFloat(value.getValue()); return floatFromYogaFloat(value.unwrap());
} }
inline YGFloatOptional yogaOptionalFloatFromFloat(Float value) { inline YGFloatOptional yogaOptionalFloatFromFloat(Float value) {
@ -580,7 +580,7 @@ inline std::string toString(const YGFloatOptional &value) {
return "undefined"; return "undefined";
} }
return folly::to<std::string>(floatFromYogaFloat(value.getValue())); return folly::to<std::string>(floatFromYogaFloat(value.unwrap()));
} }
inline std::string toString( inline std::string toString(

View File

@ -55,11 +55,12 @@ float YGFloatSanitize(const float val) {
return yoga::isUndefined(val) ? 0 : val; return yoga::isUndefined(val) ? 0 : val;
} }
YGFloatOptional YGFloatOptionalMax( YGFloatOptional YGFloatOptionalMax(YGFloatOptional op1, YGFloatOptional op2) {
const YGFloatOptional& op1, if (op1 >= op2) {
const YGFloatOptional& op2) { return op1;
if (!op1.isUndefined() && !op2.isUndefined()) { }
return op1.getValue() > op2.getValue() ? op1 : op2; if (op2 > op1) {
return op2;
} }
return op1.isUndefined() ? op2 : op1; return op1.isUndefined() ? op2 : op1;
} }

View File

@ -60,8 +60,8 @@ bool YGFloatsEqual(const float a, const float b);
float YGFloatMax(const float a, const float b); float YGFloatMax(const float a, const float b);
YGFloatOptional YGFloatOptionalMax( YGFloatOptional YGFloatOptionalMax(
const YGFloatOptional& op1, const YGFloatOptional op1,
const YGFloatOptional& op2); const YGFloatOptional op2);
float YGFloatMin(const float a, const float b); float YGFloatMin(const float a, const float b);

View File

@ -12,15 +12,6 @@
using namespace facebook; using namespace facebook;
float YGFloatOptional::getValue() const {
if (isUndefined()) {
// Abort, accessing a value of an undefined float optional
std::cerr << "Tried to get value of an undefined YGFloatOptional\n";
std::exit(EXIT_FAILURE);
}
return value_;
}
bool YGFloatOptional::operator==(YGFloatOptional op) const { bool YGFloatOptional::operator==(YGFloatOptional op) const {
return value_ == op.value_ || (isUndefined() && op.isUndefined()); return value_ == op.value_ || (isUndefined() && op.isUndefined());
} }

View File

@ -17,11 +17,6 @@ struct YGFloatOptional {
explicit constexpr YGFloatOptional(float value) : value_(value) {} explicit constexpr YGFloatOptional(float value) : value_(value) {}
constexpr YGFloatOptional() = default; constexpr YGFloatOptional() = default;
// Program will terminate if the value of an undefined is accessed. Please
// make sure to check if the optional is defined before calling this function.
// To check if float optional is defined, use `isUndefined()`.
float getValue() const;
// returns the wrapped value, or a value x with YGIsUndefined(x) == true // returns the wrapped value, or a value x with YGIsUndefined(x) == true
float unwrap() const { float unwrap() const {
return value_; return value_;

View File

@ -211,7 +211,7 @@ YGFloatOptional YGNode::relativePosition(
YGFloatOptional trailingPosition = getTrailingPosition(axis, axisSize); YGFloatOptional trailingPosition = getTrailingPosition(axis, axisSize);
if (!trailingPosition.isUndefined()) { if (!trailingPosition.isUndefined()) {
trailingPosition = YGFloatOptional{-1 * trailingPosition.getValue()}; trailingPosition = YGFloatOptional{-1 * trailingPosition.unwrap()};
} }
return trailingPosition; return trailingPosition;
} }
@ -302,7 +302,7 @@ YGValue YGNode::resolveFlexBasisPtr() const {
if (flexBasis.unit != YGUnitAuto && flexBasis.unit != YGUnitUndefined) { if (flexBasis.unit != YGUnitAuto && flexBasis.unit != YGUnitUndefined) {
return flexBasis; return flexBasis;
} }
if (!style_.flex.isUndefined() && style_.flex.getValue() > 0.0f) { if (!style_.flex.isUndefined() && style_.flex.unwrap() > 0.0f) {
return config_->useWebDefaults ? YGValueAuto : YGValueZero; return config_->useWebDefaults ? YGValueAuto : YGValueZero;
} }
return YGValueAuto; return YGValueAuto;
@ -393,10 +393,10 @@ float YGNode::resolveFlexGrow() {
return 0.0; return 0.0;
} }
if (!style_.flexGrow.isUndefined()) { if (!style_.flexGrow.isUndefined()) {
return style_.flexGrow.getValue(); return style_.flexGrow.unwrap();
} }
if (!style_.flex.isUndefined() && style_.flex.getValue() > 0.0f) { if (!style_.flex.isUndefined() && style_.flex.unwrap() > 0.0f) {
return style_.flex.getValue(); return style_.flex.unwrap();
} }
return kDefaultFlexGrow; return kDefaultFlexGrow;
} }
@ -406,11 +406,11 @@ float YGNode::resolveFlexShrink() {
return 0.0; return 0.0;
} }
if (!style_.flexShrink.isUndefined()) { if (!style_.flexShrink.isUndefined()) {
return style_.flexShrink.getValue(); return style_.flexShrink.unwrap();
} }
if (!config_->useWebDefaults && !style_.flex.isUndefined() && if (!config_->useWebDefaults && !style_.flex.isUndefined() &&
style_.flex.getValue() < 0.0f) { style_.flex.unwrap() < 0.0f) {
return -style_.flex.getValue(); return -style_.flex.unwrap();
} }
return config_->useWebDefaults ? kWebDefaultFlexShrink : kDefaultFlexShrink; return config_->useWebDefaults ? kWebDefaultFlexShrink : kDefaultFlexShrink;
} }
@ -455,7 +455,7 @@ YGFloatOptional YGNode::getLeadingPadding(
YGResolveValue(style_.padding[YGEdgeStart], widthSize); YGResolveValue(style_.padding[YGEdgeStart], widthSize);
if (YGFlexDirectionIsRow(axis) && if (YGFlexDirectionIsRow(axis) &&
style_.padding[YGEdgeStart].unit != YGUnitUndefined && style_.padding[YGEdgeStart].unit != YGUnitUndefined &&
!paddingEdgeStart.isUndefined() && paddingEdgeStart.getValue() >= 0.0f) { !paddingEdgeStart.isUndefined() && paddingEdgeStart.unwrap() >= 0.0f) {
return paddingEdgeStart; return paddingEdgeStart;
} }
@ -471,7 +471,7 @@ YGFloatOptional YGNode::getTrailingPadding(
if (YGFlexDirectionIsRow(axis) && if (YGFlexDirectionIsRow(axis) &&
style_.padding[YGEdgeEnd].unit != YGUnitUndefined && style_.padding[YGEdgeEnd].unit != YGUnitUndefined &&
!YGResolveValue(style_.padding[YGEdgeEnd], widthSize).isUndefined() && !YGResolveValue(style_.padding[YGEdgeEnd], widthSize).isUndefined() &&
YGResolveValue(style_.padding[YGEdgeEnd], widthSize).getValue() >= 0.0f) { YGResolveValue(style_.padding[YGEdgeEnd], widthSize).unwrap() >= 0.0f) {
return YGResolveValue(style_.padding[YGEdgeEnd], widthSize); return YGResolveValue(style_.padding[YGEdgeEnd], widthSize);
} }

View File

@ -43,7 +43,7 @@ static void appendFloatOptionalIfDefined(
const string key, const string key,
const YGFloatOptional num) { const YGFloatOptional num) {
if (!num.isUndefined()) { if (!num.isUndefined()) {
appendFormatedString(base, "%s: %g; ", key.c_str(), num.getValue()); appendFormatedString(base, "%s: %g; ", key.c_str(), num.unwrap());
} }
} }

View File

@ -28,26 +28,26 @@ bool YGStyle::operator==(const YGStyle& style) {
if (areNonFloatValuesEqual && !flex.isUndefined() && if (areNonFloatValuesEqual && !flex.isUndefined() &&
!style.flex.isUndefined()) { !style.flex.isUndefined()) {
areNonFloatValuesEqual = areNonFloatValuesEqual =
areNonFloatValuesEqual && flex.getValue() == style.flex.getValue(); areNonFloatValuesEqual && flex == style.flex;
} }
areNonFloatValuesEqual = areNonFloatValuesEqual && areNonFloatValuesEqual = areNonFloatValuesEqual &&
flexGrow.isUndefined() == style.flexGrow.isUndefined(); flexGrow.isUndefined() == style.flexGrow.isUndefined();
if (areNonFloatValuesEqual && !flexGrow.isUndefined()) { if (areNonFloatValuesEqual && !flexGrow.isUndefined()) {
areNonFloatValuesEqual = areNonFloatValuesEqual && areNonFloatValuesEqual = areNonFloatValuesEqual &&
flexGrow.getValue() == style.flexGrow.getValue(); flexGrow == style.flexGrow;
} }
areNonFloatValuesEqual = areNonFloatValuesEqual && areNonFloatValuesEqual = areNonFloatValuesEqual &&
flexShrink.isUndefined() == style.flexShrink.isUndefined(); flexShrink.isUndefined() == style.flexShrink.isUndefined();
if (areNonFloatValuesEqual && !style.flexShrink.isUndefined()) { if (areNonFloatValuesEqual && !style.flexShrink.isUndefined()) {
areNonFloatValuesEqual = areNonFloatValuesEqual && areNonFloatValuesEqual = areNonFloatValuesEqual &&
flexShrink.getValue() == style.flexShrink.getValue(); flexShrink == style.flexShrink;
} }
if (!(aspectRatio.isUndefined() && style.aspectRatio.isUndefined())) { if (!(aspectRatio.isUndefined() && style.aspectRatio.isUndefined())) {
areNonFloatValuesEqual = areNonFloatValuesEqual && areNonFloatValuesEqual = areNonFloatValuesEqual &&
aspectRatio.getValue() == style.aspectRatio.getValue(); aspectRatio == style.aspectRatio;
} }
return areNonFloatValuesEqual; return areNonFloatValuesEqual;

View File

@ -581,14 +581,14 @@ void YGNodeCopyStyle(const YGNodeRef dstNode, const YGNodeRef srcNode) {
float YGNodeStyleGetFlexGrow(const YGNodeRef node) { float YGNodeStyleGetFlexGrow(const YGNodeRef node) {
return node->getStyle().flexGrow.isUndefined() return node->getStyle().flexGrow.isUndefined()
? kDefaultFlexGrow ? kDefaultFlexGrow
: node->getStyle().flexGrow.getValue(); : node->getStyle().flexGrow.unwrap();
} }
float YGNodeStyleGetFlexShrink(const YGNodeRef node) { float YGNodeStyleGetFlexShrink(const YGNodeRef node) {
return node->getStyle().flexShrink.isUndefined() return node->getStyle().flexShrink.isUndefined()
? (node->getConfig()->useWebDefaults ? kWebDefaultFlexShrink ? (node->getConfig()->useWebDefaults ? kWebDefaultFlexShrink
: kDefaultFlexShrink) : kDefaultFlexShrink)
: node->getStyle().flexShrink.getValue(); : node->getStyle().flexShrink.unwrap();
} }
namespace { namespace {
@ -857,7 +857,7 @@ void YGNodeStyleSetFlex(const YGNodeRef node, const float flex) {
// TODO(T26792433): Change the API to accept YGFloatOptional. // TODO(T26792433): Change the API to accept YGFloatOptional.
float YGNodeStyleGetFlex(const YGNodeRef node) { float YGNodeStyleGetFlex(const YGNodeRef node) {
return node->getStyle().flex.isUndefined() ? YGUndefined return node->getStyle().flex.isUndefined() ? YGUndefined
: node->getStyle().flex.getValue(); : node->getStyle().flex.unwrap();
} }
// TODO(T26792433): Change the API to accept YGFloatOptional. // TODO(T26792433): Change the API to accept YGFloatOptional.
@ -959,7 +959,7 @@ float YGNodeStyleGetBorder(const YGNodeRef node, const YGEdge edge) {
// TODO(T26792433): Change the API to accept YGFloatOptional. // TODO(T26792433): Change the API to accept YGFloatOptional.
float YGNodeStyleGetAspectRatio(const YGNodeRef node) { float YGNodeStyleGetAspectRatio(const YGNodeRef node) {
const YGFloatOptional op = node->getStyle().aspectRatio; const YGFloatOptional op = node->getStyle().aspectRatio;
return op.isUndefined() ? YGUndefined : op.getValue(); return op.isUndefined() ? YGUndefined : op.unwrap();
} }
// TODO(T26792433): Change the API to accept YGFloatOptional. // TODO(T26792433): Change the API to accept YGFloatOptional.
@ -1229,11 +1229,11 @@ static YGFloatOptional YGNodeBoundAxisWithinMinAndMax(
node->getStyle().maxDimensions[YGDimensionWidth], axisSize); node->getStyle().maxDimensions[YGDimensionWidth], axisSize);
} }
if (!max.isUndefined() && max.getValue() >= 0 && value > max.getValue()) { if (!max.isUndefined() && max.unwrap() >= 0 && value > max.unwrap()) {
return max; return max;
} }
if (!min.isUndefined() && min.getValue() >= 0 && value < min.getValue()) { if (!min.isUndefined() && min.unwrap() >= 0 && value < min.unwrap()) {
return min; return min;
} }
@ -1277,14 +1277,14 @@ static void YGConstrainMaxSizeForMode(
switch (*mode) { switch (*mode) {
case YGMeasureModeExactly: case YGMeasureModeExactly:
case YGMeasureModeAtMost: case YGMeasureModeAtMost:
*size = (maxSize.isUndefined() || *size < maxSize.getValue()) *size = (maxSize.isUndefined() || *size < maxSize.unwrap())
? *size ? *size
: maxSize.getValue(); : maxSize.unwrap();
break; break;
case YGMeasureModeUndefined: case YGMeasureModeUndefined:
if (!maxSize.isUndefined()) { if (!maxSize.isUndefined()) {
*mode = YGMeasureModeAtMost; *mode = YGMeasureModeAtMost;
*size = maxSize.getValue(); *size = maxSize.unwrap();
} }
break; break;
} }
@ -1399,13 +1399,13 @@ static void YGNodeComputeFlexBasisForChild(
if (!child->getStyle().aspectRatio.isUndefined()) { if (!child->getStyle().aspectRatio.isUndefined()) {
if (!isMainAxisRow && childWidthMeasureMode == YGMeasureModeExactly) { if (!isMainAxisRow && childWidthMeasureMode == YGMeasureModeExactly) {
childHeight = marginColumn + childHeight = marginColumn +
(childWidth - marginRow) / child->getStyle().aspectRatio.getValue(); (childWidth - marginRow) / child->getStyle().aspectRatio.unwrap();
childHeightMeasureMode = YGMeasureModeExactly; childHeightMeasureMode = YGMeasureModeExactly;
} else if ( } else if (
isMainAxisRow && childHeightMeasureMode == YGMeasureModeExactly) { isMainAxisRow && childHeightMeasureMode == YGMeasureModeExactly) {
childWidth = marginRow + childWidth = marginRow +
(childHeight - marginColumn) * (childHeight - marginColumn) *
child->getStyle().aspectRatio.getValue(); child->getStyle().aspectRatio.unwrap();
childWidthMeasureMode = YGMeasureModeExactly; childWidthMeasureMode = YGMeasureModeExactly;
} }
} }
@ -1425,7 +1425,7 @@ static void YGNodeComputeFlexBasisForChild(
childWidthMeasureMode = YGMeasureModeExactly; childWidthMeasureMode = YGMeasureModeExactly;
if (!child->getStyle().aspectRatio.isUndefined()) { if (!child->getStyle().aspectRatio.isUndefined()) {
childHeight = childHeight =
(childWidth - marginRow) / child->getStyle().aspectRatio.getValue(); (childWidth - marginRow) / child->getStyle().aspectRatio.unwrap();
childHeightMeasureMode = YGMeasureModeExactly; childHeightMeasureMode = YGMeasureModeExactly;
} }
} }
@ -1442,7 +1442,7 @@ static void YGNodeComputeFlexBasisForChild(
if (!child->getStyle().aspectRatio.isUndefined()) { if (!child->getStyle().aspectRatio.isUndefined()) {
childWidth = (childHeight - marginColumn) * childWidth = (childHeight - marginColumn) *
child->getStyle().aspectRatio.getValue(); child->getStyle().aspectRatio.unwrap();
childWidthMeasureMode = YGMeasureModeExactly; childWidthMeasureMode = YGMeasureModeExactly;
} }
} }
@ -1557,10 +1557,10 @@ static void YGNodeAbsoluteLayoutChild(
if (YGFloatIsUndefined(childWidth)) { if (YGFloatIsUndefined(childWidth)) {
childWidth = marginRow + childWidth = marginRow +
(childHeight - marginColumn) * (childHeight - marginColumn) *
child->getStyle().aspectRatio.getValue(); child->getStyle().aspectRatio.unwrap();
} else if (YGFloatIsUndefined(childHeight)) { } else if (YGFloatIsUndefined(childHeight)) {
childHeight = marginColumn + childHeight = marginColumn +
(childWidth - marginRow) / child->getStyle().aspectRatio.getValue(); (childWidth - marginRow) / child->getStyle().aspectRatio.unwrap();
} }
} }
} }
@ -1886,14 +1886,14 @@ static float YGNodeCalculateAvailableInnerDim(
YGResolveValue(node->getStyle().minDimensions[dimension], ownerDim); YGResolveValue(node->getStyle().minDimensions[dimension], ownerDim);
const float minInnerDim = minDimensionOptional.isUndefined() const float minInnerDim = minDimensionOptional.isUndefined()
? 0.0f ? 0.0f
: minDimensionOptional.getValue() - paddingAndBorder; : minDimensionOptional.unwrap() - paddingAndBorder;
const YGFloatOptional maxDimensionOptional = const YGFloatOptional maxDimensionOptional =
YGResolveValue(node->getStyle().maxDimensions[dimension], ownerDim); YGResolveValue(node->getStyle().maxDimensions[dimension], ownerDim);
const float maxInnerDim = maxDimensionOptional.isUndefined() const float maxInnerDim = maxDimensionOptional.isUndefined()
? FLT_MAX ? FLT_MAX
: maxDimensionOptional.getValue() - paddingAndBorder; : maxDimensionOptional.unwrap() - paddingAndBorder;
availableInnerDim = availableInnerDim =
YGFloatMax(YGFloatMin(availableInnerDim, maxInnerDim), minInnerDim); YGFloatMax(YGFloatMin(availableInnerDim, maxInnerDim), minInnerDim);
} }
@ -2166,9 +2166,9 @@ static float YGDistributeFreeSpaceSecondPass(
if (!currentRelativeChild->getStyle().aspectRatio.isUndefined()) { if (!currentRelativeChild->getStyle().aspectRatio.isUndefined()) {
childCrossSize = isMainAxisRow ? (childMainSize - marginMain) / childCrossSize = isMainAxisRow ? (childMainSize - marginMain) /
currentRelativeChild->getStyle().aspectRatio.getValue() currentRelativeChild->getStyle().aspectRatio.unwrap()
: (childMainSize - marginMain) * : (childMainSize - marginMain) *
currentRelativeChild->getStyle().aspectRatio.getValue(); currentRelativeChild->getStyle().aspectRatio.unwrap();
childCrossMeasureMode = YGMeasureModeExactly; childCrossMeasureMode = YGMeasureModeExactly;
childCrossSize += marginCross; childCrossSize += marginCross;
@ -3135,9 +3135,9 @@ static void YGNodelayoutImpl(
? child->getMarginForAxis(crossAxis, availableInnerWidth) ? child->getMarginForAxis(crossAxis, availableInnerWidth)
.unwrap() + .unwrap() +
(isMainAxisRow ? childMainSize / (isMainAxisRow ? childMainSize /
child->getStyle().aspectRatio.getValue() child->getStyle().aspectRatio.unwrap()
: childMainSize * : childMainSize *
child->getStyle().aspectRatio.getValue()) child->getStyle().aspectRatio.unwrap())
: collectedFlexItemsValues.crossDim; : collectedFlexItemsValues.crossDim;
childMainSize += childMainSize +=