Encapsulate arrays of `YGValue` within `YGStyle`

Summary:
@public

Enforce more encapsulation of arrays of `YGValue` in `YGSty;e`.
This will allow us to use `CompactValue` in `YGStyle` while (mostly) retaining API compatibility.

Reviewed By: SidharthGuglani

Differential Revision: D13452042

fbshipit-source-id: 382b1c7245c4bea4280126ab1413e7e931b62eaa
This commit is contained in:
David Aurelio 2018-12-14 09:20:27 -08:00 committed by Facebook Github Bot
parent 94dd6025d3
commit dac59586ed
4 changed files with 83 additions and 48 deletions

View File

@ -15,39 +15,35 @@ bool YGStyle::operator==(const YGStyle& style) {
alignSelf == style.alignSelf && positionType == style.positionType &&
flexWrap == style.flexWrap && overflow == style.overflow &&
display == style.display && YGValueEqual(flexBasis, style.flexBasis) &&
YGValueArrayEqual(margin, style.margin) &&
YGValueArrayEqual(position, style.position) &&
YGValueArrayEqual(padding, style.padding) &&
YGValueArrayEqual(border, style.border) &&
YGValueArrayEqual(dimensions, style.dimensions) &&
YGValueArrayEqual(minDimensions, style.minDimensions) &&
YGValueArrayEqual(maxDimensions, style.maxDimensions);
margin == style.margin && position == style.position &&
padding == style.padding && border == style.border &&
dimensions == style.dimensions && minDimensions == style.minDimensions &&
maxDimensions == style.maxDimensions;
areNonFloatValuesEqual =
areNonFloatValuesEqual && flex.isUndefined() == style.flex.isUndefined();
if (areNonFloatValuesEqual && !flex.isUndefined() &&
!style.flex.isUndefined()) {
areNonFloatValuesEqual =
areNonFloatValuesEqual && flex == style.flex;
areNonFloatValuesEqual = areNonFloatValuesEqual && flex == style.flex;
}
areNonFloatValuesEqual = areNonFloatValuesEqual &&
flexGrow.isUndefined() == style.flexGrow.isUndefined();
if (areNonFloatValuesEqual && !flexGrow.isUndefined()) {
areNonFloatValuesEqual = areNonFloatValuesEqual &&
flexGrow == style.flexGrow;
areNonFloatValuesEqual =
areNonFloatValuesEqual && flexGrow == style.flexGrow;
}
areNonFloatValuesEqual = areNonFloatValuesEqual &&
flexShrink.isUndefined() == style.flexShrink.isUndefined();
if (areNonFloatValuesEqual && !style.flexShrink.isUndefined()) {
areNonFloatValuesEqual = areNonFloatValuesEqual &&
flexShrink == style.flexShrink;
areNonFloatValuesEqual =
areNonFloatValuesEqual && flexShrink == style.flexShrink;
}
if (!(aspectRatio.isUndefined() && style.aspectRatio.isUndefined())) {
areNonFloatValuesEqual = areNonFloatValuesEqual &&
aspectRatio == style.aspectRatio;
areNonFloatValuesEqual =
areNonFloatValuesEqual && aspectRatio == style.aspectRatio;
}
return areNonFloatValuesEqual;

View File

@ -5,6 +5,9 @@
* file in the root directory of this source tree.
*/
#pragma once
#include <algorithm>
#include <array>
#include <initializer_list>
#include "YGFloatOptional.h"
#include "Yoga-internal.h"
#include "Yoga.h"
@ -13,22 +16,9 @@ constexpr YGValue kYGValueUndefined = {0, YGUnitUndefined};
constexpr YGValue kYGValueAuto = {0, YGUnitAuto};
constexpr std::array<YGValue, YGEdgeCount> kYGDefaultEdgeValuesUnit = {
{kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined}};
constexpr std::array<YGValue, 2> kYGDefaultDimensionValuesUnit = {
{kYGValueUndefined, kYGValueUndefined}};
struct YGStyle {
using Dimensions = std::array<YGValue, 2>;
using Dimensions = facebook::yoga::detail::Values<2>;
using Edges = facebook::yoga::detail::Values<YGEdgeCount>;
YGDirection direction : 2;
YGFlexDirection flexDirection : 2;
@ -44,13 +34,13 @@ struct YGStyle {
YGFloatOptional flexGrow = {};
YGFloatOptional flexShrink = {};
YGValue flexBasis = kYGValueAuto;
std::array<YGValue, YGEdgeCount> margin = kYGDefaultEdgeValuesUnit;
std::array<YGValue, YGEdgeCount> position = kYGDefaultEdgeValuesUnit;
std::array<YGValue, YGEdgeCount> padding = kYGDefaultEdgeValuesUnit;
std::array<YGValue, YGEdgeCount> border = kYGDefaultEdgeValuesUnit;
Dimensions dimensions = {{kYGValueAuto, kYGValueAuto}};
Dimensions minDimensions = kYGDefaultDimensionValuesUnit;
Dimensions maxDimensions = kYGDefaultDimensionValuesUnit;
Edges margin{kYGValueUndefined};
Edges position{kYGValueUndefined};
Edges padding{kYGValueUndefined};
Edges border{kYGValueUndefined};
Dimensions dimensions{kYGValueAuto};
Dimensions minDimensions{kYGValueUndefined};
Dimensions maxDimensions{kYGValueUndefined};
// Yoga specific properties, not compatible with flexbox specification
YGFloatOptional aspectRatio = {};

View File

@ -46,6 +46,8 @@ inline bool operator==(const YGValue& lhs, const YGValue& rhs) {
case YGUnitPercent:
return lhs.value == rhs.value;
}
return false;
}
inline bool operator!=(const YGValue& lhs, const YGValue& rhs) {

View File

@ -42,17 +42,6 @@ extern const YGValue YGValueUndefined;
extern const YGValue YGValueAuto;
extern const YGValue YGValueZero;
template <std::size_t size>
bool YGValueArrayEqual(
const std::array<YGValue, size> val1,
const std::array<YGValue, size> val2) {
bool areEqual = true;
for (uint32_t i = 0; i < size && areEqual; ++i) {
areEqual = YGValueEqual(val1[i], val2[i]);
}
return areEqual;
}
struct YGCachedMeasurement {
float availableWidth;
float availableHeight;
@ -99,6 +88,64 @@ struct YGCachedMeasurement {
// layouts should not require more than 16 entries to fit within the cache.
#define YG_MAX_CACHED_RESULT_COUNT 16
namespace facebook {
namespace yoga {
namespace detail {
template <size_t Size>
class Values {
private:
std::array<YGValue, Size> values_;
public:
Values() = default;
explicit Values(const YGValue& defaultValue) noexcept {
values_.fill(defaultValue);
}
operator const std::array<YGValue, Size>&() const noexcept {
return values_;
}
operator std::array<YGValue, Size>&() noexcept {
return values_;
}
const YGValue& operator[](size_t i) const noexcept {
return values_[i];
}
YGValue& operator[](size_t i) noexcept {
return values_[i];
}
template <size_t I>
YGValue get() const noexcept {
return std::get<I>(values_);
}
template <size_t I>
void set(YGValue& value) noexcept {
std::get<I>(values_) = value;
}
bool operator==(const Values& other) const noexcept {
for (size_t i = 0; i < Size; ++i) {
if (values_[i] != other.values_[i]) {
return false;
}
}
return true;
}
Values& operator=(const Values& other) = default;
Values& operator=(const std::array<YGValue, Size>& other) noexcept {
values_ = other;
return *this;
}
};
} // namespace detail
} // namespace yoga
} // namespace facebook
static const float kDefaultFlexGrow = 0.0f;
static const float kDefaultFlexShrink = 0.0f;
static const float kWebDefaultFlexShrink = 1.0f;