|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its 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 <math.h> |
| 11 | +#include "YGEnums.h" |
| 12 | +#include "YGMacros.h" |
| 13 | + |
| 14 | +#if defined(_MSC_VER) && defined(__clang__) |
| 15 | +#define COMPILING_WITH_CLANG_ON_WINDOWS |
| 16 | +#endif |
| 17 | +#if defined(COMPILING_WITH_CLANG_ON_WINDOWS) |
| 18 | +#include <limits> |
| 19 | +constexpr float YGUndefined = std::numeric_limits<float>::quiet_NaN(); |
| 20 | +#else |
| 21 | +YG_EXTERN_C_BEGIN |
| 22 | + |
| 23 | +#if defined(_MSC_VER) |
| 24 | +#define YGUndefined __builtin_nanf("0") |
| 25 | +#else |
| 26 | +#define YGUndefined NAN |
| 27 | +#endif |
| 28 | + |
| 29 | +#endif |
| 30 | + |
| 31 | +typedef struct YGValue { |
| 32 | + float value; |
| 33 | + YGUnit unit; |
| 34 | +} YGValue; |
| 35 | + |
| 36 | +YOGA_EXPORT extern const YGValue YGValueAuto; |
| 37 | +YOGA_EXPORT extern const YGValue YGValueUndefined; |
| 38 | +YOGA_EXPORT extern const YGValue YGValueZero; |
| 39 | + |
| 40 | +#if !defined(COMPILING_WITH_CLANG_ON_WINDOWS) |
| 41 | +YG_EXTERN_C_END |
| 42 | +#endif |
| 43 | +#undef COMPILING_WITH_CLANG_ON_WINDOWS |
| 44 | + |
| 45 | +#ifdef __cplusplus |
| 46 | + |
| 47 | +inline bool operator==(const YGValue& lhs, const YGValue& rhs) { |
| 48 | + if (lhs.unit != rhs.unit) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + switch (lhs.unit) { |
| 53 | + case YGUnitUndefined: |
| 54 | + case YGUnitAuto: |
| 55 | + return true; |
| 56 | + case YGUnitPoint: |
| 57 | + case YGUnitPercent: |
| 58 | + return lhs.value == rhs.value; |
| 59 | + } |
| 60 | + |
| 61 | + return false; |
| 62 | +} |
| 63 | + |
| 64 | +inline bool operator!=(const YGValue& lhs, const YGValue& rhs) { |
| 65 | + return !(lhs == rhs); |
| 66 | +} |
| 67 | + |
| 68 | +inline YGValue operator-(const YGValue& value) { |
| 69 | + return {-value.value, value.unit}; |
| 70 | +} |
| 71 | + |
| 72 | +namespace facebook { |
| 73 | +namespace yoga { |
| 74 | +namespace literals { |
| 75 | + |
| 76 | +inline YGValue operator"" _pt(long double value) { |
| 77 | + return YGValue{static_cast<float>(value), YGUnitPoint}; |
| 78 | +} |
| 79 | +inline YGValue operator"" _pt(unsigned long long value) { |
| 80 | + return operator"" _pt(static_cast<long double>(value)); |
| 81 | +} |
| 82 | + |
| 83 | +inline YGValue operator"" _percent(long double value) { |
| 84 | + return YGValue{static_cast<float>(value), YGUnitPercent}; |
| 85 | +} |
| 86 | +inline YGValue operator"" _percent(unsigned long long value) { |
| 87 | + return operator"" _percent(static_cast<long double>(value)); |
| 88 | +} |
| 89 | + |
| 90 | +} // namespace literals |
| 91 | +} // namespace yoga |
| 92 | +} // namespace facebook |
| 93 | + |
| 94 | +#endif |
0 commit comments