/src/yoga/yoga/numeric/FloatOptional.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <yoga/numeric/Comparison.h> |
11 | | #include <limits> |
12 | | |
13 | | namespace facebook::yoga { |
14 | | |
15 | | struct FloatOptional { |
16 | | private: |
17 | | float value_ = std::numeric_limits<float>::quiet_NaN(); |
18 | | |
19 | | public: |
20 | 462M | explicit constexpr FloatOptional(float value) : value_(value) {} |
21 | 1.76G | constexpr FloatOptional() = default; |
22 | | |
23 | | // returns the wrapped value, or a value x with YGIsUndefined(x) == true |
24 | 1.74G | constexpr float unwrap() const { |
25 | 1.74G | return value_; |
26 | 1.74G | } |
27 | | |
28 | 446M | constexpr float unwrapOrDefault(float defaultValue) const { |
29 | 446M | return isUndefined() ? defaultValue : value_; |
30 | 446M | } |
31 | | |
32 | 944M | constexpr bool isUndefined() const { |
33 | 944M | return yoga::isUndefined(value_); |
34 | 944M | } |
35 | | |
36 | 234M | constexpr bool isDefined() const { |
37 | 234M | return yoga::isDefined(value_); |
38 | 234M | } |
39 | | }; |
40 | | |
41 | | // operators take FloatOptional by value, as it is a 32bit value |
42 | | |
43 | 213M | constexpr bool operator==(FloatOptional lhs, FloatOptional rhs) { |
44 | 213M | return lhs.unwrap() == rhs.unwrap() || |
45 | 213M | (lhs.isUndefined() && rhs.isUndefined()); |
46 | 213M | } |
47 | | |
48 | 0 | constexpr bool operator==(FloatOptional lhs, float rhs) { |
49 | 0 | return lhs == FloatOptional{rhs}; |
50 | 0 | } |
51 | | |
52 | 0 | constexpr bool operator==(float lhs, FloatOptional rhs) { |
53 | 0 | return rhs == lhs; |
54 | 0 | } |
55 | | |
56 | 58.4M | constexpr FloatOptional operator+(FloatOptional lhs, FloatOptional rhs) { |
57 | 58.4M | return FloatOptional{lhs.unwrap() + rhs.unwrap()}; |
58 | 58.4M | } |
59 | | |
60 | 209M | constexpr bool operator>(FloatOptional lhs, FloatOptional rhs) { |
61 | 209M | return lhs.unwrap() > rhs.unwrap(); |
62 | 209M | } |
63 | | |
64 | 0 | constexpr bool operator<(FloatOptional lhs, FloatOptional rhs) { |
65 | 0 | return lhs.unwrap() < rhs.unwrap(); |
66 | 0 | } |
67 | | |
68 | 209M | constexpr bool operator>=(FloatOptional lhs, FloatOptional rhs) { |
69 | 209M | return lhs > rhs || lhs == rhs; |
70 | 209M | } |
71 | | |
72 | 0 | constexpr bool operator<=(FloatOptional lhs, FloatOptional rhs) { |
73 | 0 | return lhs < rhs || lhs == rhs; |
74 | 0 | } |
75 | | |
76 | 940k | constexpr FloatOptional maxOrDefined(FloatOptional lhs, FloatOptional rhs) { |
77 | 940k | return FloatOptional{yoga::maxOrDefined(lhs.unwrap(), rhs.unwrap())}; |
78 | 940k | } |
79 | | |
80 | 0 | inline bool inexactEquals(FloatOptional lhs, FloatOptional rhs) { |
81 | 0 | return yoga::inexactEquals(lhs.unwrap(), rhs.unwrap()); |
82 | 0 | } |
83 | | |
84 | | } // namespace facebook::yoga |