YGFloatOptional.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2014-present, Facebook, Inc.
  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. */
  8. #include "YGFloatOptional.h"
  9. #include <cstdlib>
  10. #include <iostream>
  11. #include "Yoga.h"
  12. YGFloatOptional::YGFloatOptional(float value) {
  13. if (YGFloatIsUndefined(value)) {
  14. isUndefined_ = true;
  15. value_ = 0;
  16. } else {
  17. value_ = value;
  18. isUndefined_ = false;
  19. }
  20. }
  21. YGFloatOptional::YGFloatOptional() : value_(0), isUndefined_(true) {}
  22. const float& YGFloatOptional::getValue() const {
  23. if (isUndefined_) {
  24. // Abort, accessing a value of an undefined float optional
  25. std::cerr << "Tried to get value of an undefined YGFloatOptional\n";
  26. std::exit(EXIT_FAILURE);
  27. }
  28. return value_;
  29. }
  30. bool YGFloatOptional::operator==(const YGFloatOptional& op) const {
  31. if (isUndefined_ == op.isUndefined()) {
  32. return isUndefined_ || value_ == op.getValue();
  33. }
  34. return false;
  35. }
  36. bool YGFloatOptional::operator!=(const YGFloatOptional& op) const {
  37. return !(*this == op);
  38. }
  39. bool YGFloatOptional::operator==(float val) const {
  40. if (YGFloatIsUndefined(val) == isUndefined_) {
  41. return isUndefined_ || val == value_;
  42. }
  43. return false;
  44. }
  45. bool YGFloatOptional::operator!=(float val) const {
  46. return !(*this == val);
  47. }
  48. YGFloatOptional YGFloatOptional::operator+(const YGFloatOptional& op) {
  49. if (!isUndefined_ && !op.isUndefined_) {
  50. return YGFloatOptional(value_ + op.value_);
  51. }
  52. return YGFloatOptional();
  53. }
  54. bool YGFloatOptional::operator>(const YGFloatOptional& op) const {
  55. if (isUndefined_ || op.isUndefined_) {
  56. return false;
  57. }
  58. return value_ > op.value_;
  59. }
  60. bool YGFloatOptional::operator<(const YGFloatOptional& op) const {
  61. if (isUndefined_ || op.isUndefined_) {
  62. return false;
  63. }
  64. return value_ < op.value_;
  65. }
  66. bool YGFloatOptional::operator>=(const YGFloatOptional& op) const {
  67. return *this == op || *this > op;
  68. }
  69. bool YGFloatOptional::operator<=(const YGFloatOptional& op) const {
  70. return *this == op || *this < op;
  71. }