Eliminate YGFloatOptional::getValue()

Summary:
@public

`YGFloatOptional::getValue()` has the unfortunate property of calling `std::exit` if the wrapped value is undefined.
That forces `x.isUndefined() ? fallback : x.getValue()` as access pattern.

Here, we replace that by introducing `YGFloatOptional::orElse(float)` which encapsulates that pattern. Other additions are `orElseGet([] { … })` and some extra operators.

Reviewed By: SidharthGuglani

Differential Revision: D13209152

fbshipit-source-id: 4e5deceaaaaf8eaed44846a8c152cc8b235e815c
This commit is contained in:
David Aurelio
2018-12-06 07:35:10 -08:00
committed by Facebook Github Bot
parent ed3b54b603
commit 50ec35575f
8 changed files with 141 additions and 134 deletions

View File

@@ -6,7 +6,6 @@
*/
#pragma once
#include <cmath>
#include <limits>
struct YGFloatOptional {
@@ -17,16 +16,28 @@ struct YGFloatOptional {
explicit constexpr YGFloatOptional(float value) : value_(value) {}
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;
bool isUndefined() const {
return std::isnan(value_);
// returns the wrapped value, or a value x with YGIsUndefined(x) == true
float unwrap() const {
return value_;
}
constexpr bool isUndefined() const {
// std::isnan is not constexpr
return !(value_ == value_);
}
constexpr float orElse(float other) const {
return isUndefined() ? other : value_;
}
template <typename Factory>
constexpr float orElseGet(Factory&& f) const {
return isUndefined() ? f() : value_;
}
YGFloatOptional operator-() const;
YGFloatOptional operator+(YGFloatOptional op) const;
YGFloatOptional operator-(YGFloatOptional op) const;
bool operator>(YGFloatOptional op) const;
bool operator<(YGFloatOptional op) const;
bool operator>=(YGFloatOptional op) const;