YGFloatOptional: Move binary operators to free functions

Summary:
@public

Having binary operators as member functions has disadvantages:

- the left hand side cannot be converted to `YGFloatOptional` implicitly (which we need for `YGStyle` refs)
- Operators are not necessarily commutative.

By moving these operators into free functions, and adding overloads for both variants if one operand is `float`, we get these properties.

Reviewed By: SidharthGuglani

Differential Revision: D15078962

fbshipit-source-id: 2e228a2ef90a8083c91788caa9eedfd4d140677f
This commit is contained in:
David Aurelio
2019-04-29 09:18:57 -07:00
committed by Facebook Github Bot
parent 2eed95f3e4
commit 98fda9c587

View File

@@ -22,25 +22,48 @@ public:
constexpr float unwrap() const { return value_; }
bool isUndefined() const { return std::isnan(value_); }
YGFloatOptional operator+(YGFloatOptional op) const {
return YGFloatOptional{value_ + op.value_};
}
bool operator>(YGFloatOptional op) const { return value_ > op.value_; }
bool operator<(YGFloatOptional op) const { return value_ < op.value_; }
bool operator>=(YGFloatOptional op) const {
return *this > op || *this == op;
}
bool operator<=(YGFloatOptional op) const {
return *this < op || *this == op;
}
bool operator==(YGFloatOptional op) const {
return value_ == op.value_ || (isUndefined() && op.isUndefined());
}
bool operator!=(YGFloatOptional op) const { return !(*this == op); }
bool operator==(float val) const {
return value_ == val || (isUndefined() && yoga::isUndefined(val));
}
bool operator!=(float val) const { return !(*this == val); }
};
// operators take YGFloatOptional by value, as it is a 32bit value
inline bool operator==(YGFloatOptional lhs, YGFloatOptional rhs) {
return lhs.unwrap() == rhs.unwrap() ||
(lhs.isUndefined() && rhs.isUndefined());
}
inline bool operator!=(YGFloatOptional lhs, YGFloatOptional rhs) {
return !(lhs == rhs);
}
inline bool operator==(YGFloatOptional lhs, float rhs) {
return lhs == YGFloatOptional{rhs};
}
inline bool operator!=(YGFloatOptional lhs, float rhs) {
return !(lhs == rhs);
}
inline bool operator==(float lhs, YGFloatOptional rhs) {
return rhs == lhs;
}
inline bool operator!=(float lhs, YGFloatOptional rhs) {
return !(lhs == rhs);
}
inline YGFloatOptional operator+(YGFloatOptional lhs, YGFloatOptional rhs) {
return YGFloatOptional{lhs.unwrap() + rhs.unwrap()};
}
inline bool operator>(YGFloatOptional lhs, YGFloatOptional rhs) {
return lhs.unwrap() > rhs.unwrap();
}
inline bool operator<(YGFloatOptional lhs, YGFloatOptional rhs) {
return lhs.unwrap() < rhs.unwrap();
}
inline bool operator>=(YGFloatOptional lhs, YGFloatOptional rhs) {
return lhs > rhs || lhs == rhs;
}
inline bool operator<=(YGFloatOptional lhs, YGFloatOptional rhs) {
return lhs < rhs || lhs == rhs;
}