Files
yoga/yoga/YGFloatOptional.cpp
David Aurelio 50ec35575f Eliminate YGFloatOptional::getValue()
Summary:
@public

`YGFloatOptional::getValue()` has the unfortunate property of calling `std::exit` if the wrapped value is undefined.
That forces `x.isUndefined() ? fallback : x.getValue()` as access pattern.

Here, we replace that by introducing `YGFloatOptional::orElse(float)` which encapsulates that pattern. Other additions are `orElseGet([] { … })` and some extra operators.

Reviewed By: SidharthGuglani

Differential Revision: D13209152

fbshipit-source-id: 4e5deceaaaaf8eaed44846a8c152cc8b235e815c
2018-12-06 07:46:24 -08:00

58 lines
1.4 KiB
C++

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*/
#include "YGFloatOptional.h"
#include <cstdlib>
#include <iostream>
#include "Yoga-internal.h"
#include "Yoga.h"
using namespace facebook;
bool YGFloatOptional::operator==(YGFloatOptional op) const {
return value_ == op.value_ || (isUndefined() && op.isUndefined());
}
bool YGFloatOptional::operator!=(YGFloatOptional op) const {
return !(*this == op);
}
bool YGFloatOptional::operator==(float val) const {
return value_ == val || (isUndefined() && yoga::isUndefined(val));
}
bool YGFloatOptional::operator!=(float val) const {
return !(*this == val);
}
YGFloatOptional YGFloatOptional::operator-() const {
return YGFloatOptional{-value_};
}
YGFloatOptional YGFloatOptional::operator+(YGFloatOptional op) const {
return YGFloatOptional{value_ + op.value_};
}
YGFloatOptional YGFloatOptional::operator-(YGFloatOptional op) const {
return YGFloatOptional{value_ - op.value_};
}
bool YGFloatOptional::operator>(YGFloatOptional op) const {
return value_ > op.value_;
}
bool YGFloatOptional::operator<(YGFloatOptional op) const {
return value_ < op.value_;
}
bool YGFloatOptional::operator>=(YGFloatOptional op) const {
return *this > op || *this == op;
}
bool YGFloatOptional::operator<=(YGFloatOptional op) const {
return *this < op || *this == op;
}