YGFloatOptional.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 <cmath>
  9. #include <limits>
  10. #include "Yoga-internal.h"
  11. struct YGFloatOptional {
  12. private:
  13. float value_ = std::numeric_limits<float>::quiet_NaN();
  14. public:
  15. explicit constexpr YGFloatOptional(float value) : value_(value) {}
  16. constexpr YGFloatOptional() = default;
  17. // returns the wrapped value, or a value x with YGIsUndefined(x) == true
  18. constexpr float unwrap() const {
  19. return value_;
  20. }
  21. bool isUndefined() const {
  22. return std::isnan(value_);
  23. }
  24. YGFloatOptional operator+(YGFloatOptional op) const {
  25. return YGFloatOptional{value_ + op.value_};
  26. }
  27. bool operator>(YGFloatOptional op) const {
  28. return value_ > op.value_;
  29. }
  30. bool operator<(YGFloatOptional op) const {
  31. return value_ < op.value_;
  32. }
  33. bool operator>=(YGFloatOptional op) const {
  34. return *this > op || *this == op;
  35. }
  36. bool operator<=(YGFloatOptional op) const {
  37. return *this < op || *this == op;
  38. }
  39. bool operator==(YGFloatOptional op) const {
  40. return value_ == op.value_ || (isUndefined() && op.isUndefined());
  41. }
  42. bool operator!=(YGFloatOptional op) const {
  43. return !(*this == op);
  44. }
  45. bool operator==(float val) const {
  46. return value_ == val || (isUndefined() && yoga::isUndefined(val));
  47. }
  48. bool operator!=(float val) const {
  49. return !(*this == val);
  50. }
  51. };