From 3df41aefdbec2772c36261a93966de8c0b8a6e08 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Fri, 14 Dec 2018 09:20:27 -0800 Subject: [PATCH] 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 --- yoga/YGStyle.cpp | 26 +++++++---------- yoga/YGStyle.h | 34 ++++++++-------------- yoga/YGValue.h | 2 ++ yoga/Yoga-internal.h | 69 +++++++++++++++++++++++++++++++++++++------- 4 files changed, 83 insertions(+), 48 deletions(-) diff --git a/yoga/YGStyle.cpp b/yoga/YGStyle.cpp index 78df8b01..1c06cd35 100644 --- a/yoga/YGStyle.cpp +++ b/yoga/YGStyle.cpp @@ -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; diff --git a/yoga/YGStyle.h b/yoga/YGStyle.h index c4f7e3c7..63b255de 100644 --- a/yoga/YGStyle.h +++ b/yoga/YGStyle.h @@ -5,6 +5,9 @@ * file in the root directory of this source tree. */ #pragma once +#include +#include +#include #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 kYGDefaultEdgeValuesUnit = { - {kYGValueUndefined, - kYGValueUndefined, - kYGValueUndefined, - kYGValueUndefined, - kYGValueUndefined, - kYGValueUndefined, - kYGValueUndefined, - kYGValueUndefined, - kYGValueUndefined}}; - -constexpr std::array kYGDefaultDimensionValuesUnit = { - {kYGValueUndefined, kYGValueUndefined}}; - struct YGStyle { - using Dimensions = std::array; + using Dimensions = facebook::yoga::detail::Values<2>; + using Edges = facebook::yoga::detail::Values; YGDirection direction : 2; YGFlexDirection flexDirection : 2; @@ -44,13 +34,13 @@ struct YGStyle { YGFloatOptional flexGrow = {}; YGFloatOptional flexShrink = {}; YGValue flexBasis = kYGValueAuto; - std::array margin = kYGDefaultEdgeValuesUnit; - std::array position = kYGDefaultEdgeValuesUnit; - std::array padding = kYGDefaultEdgeValuesUnit; - std::array 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 = {}; diff --git a/yoga/YGValue.h b/yoga/YGValue.h index 59440be3..c3df5fc9 100644 --- a/yoga/YGValue.h +++ b/yoga/YGValue.h @@ -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) { diff --git a/yoga/Yoga-internal.h b/yoga/Yoga-internal.h index ef5e5d8c..cc4da004 100644 --- a/yoga/Yoga-internal.h +++ b/yoga/Yoga-internal.h @@ -42,17 +42,6 @@ extern const YGValue YGValueUndefined; extern const YGValue YGValueAuto; extern const YGValue YGValueZero; -template -bool YGValueArrayEqual( - const std::array val1, - const std::array 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 +class Values { + private: + std::array values_; + + public: + Values() = default; + explicit Values(const YGValue& defaultValue) noexcept { + values_.fill(defaultValue); + } + + operator const std::array&() const noexcept { + return values_; + } + operator std::array&() noexcept { + return values_; + } + const YGValue& operator[](size_t i) const noexcept { + return values_[i]; + } + YGValue& operator[](size_t i) noexcept { + return values_[i]; + } + + template + YGValue get() const noexcept { + return std::get(values_); + } + + template + void set(YGValue& value) noexcept { + std::get(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& 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;