Skip to content

Commit cd4270d

Browse files
davidaureliofacebook-github-bot
authored andcommitted
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
1 parent ffa3b0d commit cd4270d

File tree

1 file changed

+44
-21
lines changed

1 file changed

+44
-21
lines changed

ReactCommon/yoga/yoga/YGFloatOptional.h

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,48 @@ struct YGFloatOptional {
2222
constexpr float unwrap() const { return value_; }
2323

2424
bool isUndefined() const { return std::isnan(value_); }
25-
26-
YGFloatOptional operator+(YGFloatOptional op) const {
27-
return YGFloatOptional{value_ + op.value_};
28-
}
29-
bool operator>(YGFloatOptional op) const { return value_ > op.value_; }
30-
bool operator<(YGFloatOptional op) const { return value_ < op.value_; }
31-
bool operator>=(YGFloatOptional op) const {
32-
return *this > op || *this == op;
33-
}
34-
bool operator<=(YGFloatOptional op) const {
35-
return *this < op || *this == op;
36-
}
37-
bool operator==(YGFloatOptional op) const {
38-
return value_ == op.value_ || (isUndefined() && op.isUndefined());
39-
}
40-
bool operator!=(YGFloatOptional op) const { return !(*this == op); }
41-
42-
bool operator==(float val) const {
43-
return value_ == val || (isUndefined() && yoga::isUndefined(val));
44-
}
45-
bool operator!=(float val) const { return !(*this == val); }
4625
};
26+
27+
// operators take YGFloatOptional by value, as it is a 32bit value
28+
29+
inline bool operator==(YGFloatOptional lhs, YGFloatOptional rhs) {
30+
return lhs.unwrap() == rhs.unwrap() ||
31+
(lhs.isUndefined() && rhs.isUndefined());
32+
}
33+
inline bool operator!=(YGFloatOptional lhs, YGFloatOptional rhs) {
34+
return !(lhs == rhs);
35+
}
36+
37+
inline bool operator==(YGFloatOptional lhs, float rhs) {
38+
return lhs == YGFloatOptional{rhs};
39+
}
40+
inline bool operator!=(YGFloatOptional lhs, float rhs) {
41+
return !(lhs == rhs);
42+
}
43+
44+
inline bool operator==(float lhs, YGFloatOptional rhs) {
45+
return rhs == lhs;
46+
}
47+
inline bool operator!=(float lhs, YGFloatOptional rhs) {
48+
return !(lhs == rhs);
49+
}
50+
51+
inline YGFloatOptional operator+(YGFloatOptional lhs, YGFloatOptional rhs) {
52+
return YGFloatOptional{lhs.unwrap() + rhs.unwrap()};
53+
}
54+
55+
inline bool operator>(YGFloatOptional lhs, YGFloatOptional rhs) {
56+
return lhs.unwrap() > rhs.unwrap();
57+
}
58+
59+
inline bool operator<(YGFloatOptional lhs, YGFloatOptional rhs) {
60+
return lhs.unwrap() < rhs.unwrap();
61+
}
62+
63+
inline bool operator>=(YGFloatOptional lhs, YGFloatOptional rhs) {
64+
return lhs > rhs || lhs == rhs;
65+
}
66+
67+
inline bool operator<=(YGFloatOptional lhs, YGFloatOptional rhs) {
68+
return lhs < rhs || lhs == rhs;
69+
}

0 commit comments

Comments
 (0)