YGValue.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the LICENSE
  5. * file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <math.h>
  9. #include "YGEnums.h"
  10. #include "YGMacros.h"
  11. YG_EXTERN_C_BEGIN
  12. // Not defined in MSVC++
  13. #ifndef NAN
  14. static const uint32_t __nan = 0x7fc00000;
  15. #define NAN (*(const float*) __nan)
  16. #endif
  17. #define YGUndefined NAN
  18. typedef struct YGValue {
  19. float value;
  20. YGUnit unit;
  21. } YGValue;
  22. extern const YGValue YGValueAuto;
  23. extern const YGValue YGValueUndefined;
  24. extern const YGValue YGValueZero;
  25. YG_EXTERN_C_END
  26. #ifdef __cplusplus
  27. inline bool operator==(const YGValue& lhs, const YGValue& rhs) {
  28. if (lhs.unit != rhs.unit) {
  29. return false;
  30. }
  31. switch (lhs.unit) {
  32. case YGUnitUndefined:
  33. case YGUnitAuto:
  34. return true;
  35. case YGUnitPoint:
  36. case YGUnitPercent:
  37. return lhs.value == rhs.value;
  38. }
  39. return false;
  40. }
  41. inline bool operator!=(const YGValue& lhs, const YGValue& rhs) {
  42. return !(lhs == rhs);
  43. }
  44. inline YGValue operator-(const YGValue& value) {
  45. return {-value.value, value.unit};
  46. }
  47. namespace facebook {
  48. namespace yoga {
  49. namespace literals {
  50. inline YGValue operator"" _pt(long double value) {
  51. return YGValue{static_cast<float>(value), YGUnitPoint};
  52. }
  53. inline YGValue operator"" _pt(unsigned long long value) {
  54. return operator"" _pt(static_cast<long double>(value));
  55. }
  56. inline YGValue operator"" _percent(long double value) {
  57. return YGValue{static_cast<float>(value), YGUnitPercent};
  58. }
  59. inline YGValue operator"" _percent(unsigned long long value) {
  60. return operator"" _percent(static_cast<long double>(value));
  61. }
  62. } // namespace literals
  63. } // namespace yoga
  64. } // namespace facebook
  65. #endif