Changes in float-family conversion infra

Summary:
Different frameworks use different kinds of floats, optional floats, and floats with assigned unit names. All those approaches use different ways to represent undefined and empty values. To deal with it we need to have some helper functions.

So, this diff changes some ways that we convert some corner values (like NaN and empty value). That change is motivated by recent personal discoveries in this field that shifted my vision on that. E.g. ComponentKit does not use `CGFloatMax` value as `Infinite` value. UIKit is also (surprisingly to me) okay with using `Infitite` instead of `CGFloatMax`. And, in general, seems using really conceptually appropriate values (instead of UIKit-inspired ones) it's the right thing to do.

Reviewed By: mdvacca

Differential Revision: D15155189

fbshipit-source-id: 33e15141f1ca3efb400a7160811224335de34ba1
This commit is contained in:
Valentin Shergin authored and Facebook Github Bot committed 2019-04-30 16:38:26 -07:00
1 parent 0196654f29
commit 1d65244376
1 file changed
+40 -9
@@ -24,44 +24,75 @@
namespace facebook {
namespace react {
/*
* Yoga's `float` <-> React Native's `Float` (can be `double` or `float`)
*
* Regular Yoga `float` values represent some onscreen-position-related values.
* They can be real numbers or special value `YGUndefined` (which actually is
* `NaN`). Conceptually, layout computation process inside Yoga should never
* produce `NaN` values from non-`NaN` values. At the same time, ` YGUndefined`
* values have special "no limit" meaning in Yoga, therefore ` YGUndefined`
* usually corresponds to `Infinity` value.
*/
inline Float floatFromYogaFloat(float value) {
if (value == YGUndefined) {
return kFloatUndefined;
static_assert(
YGUndefined != YGUndefined,
"The code of this function assumes that YGUndefined is NaN.");
if (std::isnan(value) /* means: `value == YGUndefined` */) {
return std::numeric_limits<Float>::infinity();
}
return (Float)value;
}
inline float yogaFloatFromFloat(Float value) {
if (value == kFloatUndefined) {
if (std::isinf(value)) {
return YGUndefined;
}
return (float)value;
}
/*
* `YGFloatOptional` <-> React Native's `Float`
*
* `YGFloatOptional` represents optional dimensionless float values in Yoga
* Style object (e.g. `flex`). The most suitable analogy to empty
* `YGFloatOptional` is `NaN` value.
* `YGFloatOptional` values are usually parsed from some outside data source
* which usually has some special corresponding representation for an empty
* value.
*/
inline Float floatFromYogaOptionalFloat(YGFloatOptional value) {
if (value.isUndefined()) {
return kFloatUndefined;
return std::numeric_limits<Float>::quiet_NaN();
}
return floatFromYogaFloat(value.unwrap());
}
inline YGFloatOptional yogaOptionalFloatFromFloat(Float value) {
if (value == kFloatUndefined) {
if (std::isnan(value)) {
return YGFloatOptional();
}
return YGFloatOptional(yogaFloatFromFloat(value));
return YGFloatOptional((float)value);
}
inline YGValue yogaStyleValueFromFloat(const Float &value) {
if (std::isnan(value) || value == kFloatUndefined) {
/*
* `YGValue` <-> `React Native's `Float`
*
* `YGValue` represents optional dimensionful (a real number and some unit, e.g.
* pixels).
*/
inline YGValue yogaStyleValueFromFloat(
const Float &value,
YGUnit unit = YGUnitPoint) {
if (std::isnan(value)) {
return YGValueUndefined;
}
return {(float)value, YGUnitPoint};
return {(float)value, unit};
}
inline folly::Optional<Float> optionalFloatFromYogaValue(