Move YGStyle to seperate file and add constructors

Summary: Moved YGStyle to separate file and added default constructor to it. This diff also gets rid off of the default values, which were earlier declared in Yoga-interna.h and instead uses default constructor. This diff also addresses https://github.com/facebook/yoga/pull/700

Reviewed By: emilsjolander

Differential Revision: D7016575

fbshipit-source-id: eb28df0ffb4cc813b23edaff80d7d4ebc56ce6af
This commit is contained in:
Pritesh Nandgaonkar
2018-02-20 05:41:48 -08:00
committed by Facebook Github Bot
parent bbdb62e654
commit 52f2cf319a
8 changed files with 262 additions and 155 deletions

View File

@@ -0,0 +1,121 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#pragma once
#include <algorithm>
#include <array>
#include <cmath>
#include <vector>
#include "Yoga.h"
using YGVector = std::vector<YGNodeRef>;
YG_EXTERN_C_BEGIN
WIN_EXPORT float YGRoundValueToPixelGrid(
const float value,
const float pointScaleFactor,
const bool forceCeil,
const bool forceFloor);
YG_EXTERN_C_END
extern const std::array<YGEdge, 4> trailing;
extern const std::array<YGEdge, 4> leading;
extern bool YGValueEqual(const YGValue a, const YGValue b);
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;
}
const YGValue kYGValueUndefined = {YGUndefined, YGUnitUndefined};
const YGValue kYGValueAuto = {YGUndefined, YGUnitAuto};
const std::array<YGValue, YGEdgeCount> kYGDefaultEdgeValuesUnit = {
{kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined}};
const std::array<YGValue, 2> kYGDefaultDimensionValuesAutoUnit = {
{kYGValueAuto, kYGValueAuto}};
const std::array<YGValue, 2> kYGDefaultDimensionValuesUnit = {
{kYGValueUndefined, kYGValueUndefined}};
struct YGCachedMeasurement {
float availableWidth;
float availableHeight;
YGMeasureMode widthMeasureMode;
YGMeasureMode heightMeasureMode;
float computedWidth;
float computedHeight;
bool operator==(YGCachedMeasurement measurement) const {
bool isEqual = widthMeasureMode == measurement.widthMeasureMode &&
heightMeasureMode == measurement.heightMeasureMode;
if (!std::isnan(availableWidth) ||
!std::isnan(measurement.availableWidth)) {
isEqual = isEqual && availableWidth == measurement.availableWidth;
}
if (!std::isnan(availableHeight) ||
!std::isnan(measurement.availableHeight)) {
isEqual = isEqual && availableHeight == measurement.availableHeight;
}
if (!std::isnan(computedWidth) || !std::isnan(measurement.computedWidth)) {
isEqual = isEqual && computedWidth == measurement.computedWidth;
}
if (!std::isnan(computedHeight) ||
!std::isnan(measurement.computedHeight)) {
isEqual = isEqual && computedHeight == measurement.computedHeight;
}
return isEqual;
}
};
// This value was chosen based on empiracle data. Even the most complicated
// layouts should not require more than 16 entries to fit within the cache.
#define YG_MAX_CACHED_RESULT_COUNT 16
struct YGConfig {
bool experimentalFeatures[YGExperimentalFeatureCount + 1];
bool useWebDefaults;
bool useLegacyStretchBehaviour;
bool shouldDiffLayoutWithoutLegacyStretchBehaviour;
float pointScaleFactor;
YGLogger logger;
YGNodeClonedFunc cloneNodeCallback;
void* context;
};
static const float kDefaultFlexGrow = 0.0f;
static const float kDefaultFlexShrink = 0.0f;
static const float kWebDefaultFlexShrink = 1.0f;
extern bool YGFloatsEqual(const float a, const float b);
extern bool YGValueEqual(const YGValue a, const YGValue b);
extern const YGValue* YGComputedEdgeValue(
const std::array<YGValue, YGEdgeCount>& edges,
const YGEdge edge,
const YGValue* const defaultValue);

View File

View File

@@ -7,6 +7,7 @@
#include <gtest/gtest.h>
#include <yoga/YGNode.h>
#include <iostream>
TEST(YogaTest, copy_style_same) {
const YGNodeRef node0 = YGNodeNew();

View File

@@ -382,7 +382,7 @@ YGNode::YGNode()
measure_(nullptr),
baseline_(nullptr),
dirtied_(nullptr),
style_(gYGNodeStyleDefaults),
style_(YGStyle()),
layout_(gYGNodeLayoutDefaults),
lineIndex_(0),
parent_(nullptr),

View File

@@ -7,7 +7,7 @@
#pragma once
#include <stdio.h>
#include "YGStyle.h"
#include "Yoga-internal.h"
struct YGNode {

78
yoga/YGStyle.cpp Normal file
View File

@@ -0,0 +1,78 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include "YGStyle.h"
YGStyle::YGStyle()
: direction(YGDirectionInherit),
flexDirection(YGFlexDirectionColumn),
justifyContent(YGJustifyFlexStart),
alignContent(YGAlignFlexStart),
alignItems(YGAlignStretch),
alignSelf(YGAlignAuto),
positionType(YGPositionTypeRelative),
flexWrap(YGWrapNoWrap),
overflow(YGOverflowVisible),
display(YGDisplayFlex),
flex(YGUndefined),
flexGrow(YGUndefined),
flexShrink(YGUndefined),
flexBasis(kYGValueAuto),
margin(kYGDefaultEdgeValuesUnit),
position(kYGDefaultEdgeValuesUnit),
padding(kYGDefaultEdgeValuesUnit),
border(kYGDefaultEdgeValuesUnit),
dimensions(kYGDefaultDimensionValuesAutoUnit),
minDimensions(kYGDefaultDimensionValuesUnit),
maxDimensions(kYGDefaultDimensionValuesUnit),
aspectRatio(YGUndefined) {}
// Yoga specific properties, not compatible with flexbox specification
bool YGStyle::operator==(const YGStyle& style) {
bool areNonFloatValuesEqual = direction == style.direction &&
flexDirection == style.flexDirection &&
justifyContent == style.justifyContent &&
alignContent == style.alignContent && alignItems == style.alignItems &&
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);
if (!(std::isnan(flex) && std::isnan(style.flex))) {
areNonFloatValuesEqual = areNonFloatValuesEqual && flex == style.flex;
}
if (!(std::isnan(flexGrow) && std::isnan(style.flexGrow))) {
areNonFloatValuesEqual =
areNonFloatValuesEqual && flexGrow == style.flexGrow;
}
if (!(std::isnan(flexShrink) && std::isnan(style.flexShrink))) {
areNonFloatValuesEqual =
areNonFloatValuesEqual && flexShrink == style.flexShrink;
}
if (!(std::isnan(aspectRatio) && std::isnan(style.aspectRatio))) {
areNonFloatValuesEqual =
areNonFloatValuesEqual && aspectRatio == style.aspectRatio;
}
return areNonFloatValuesEqual;
}
bool YGStyle::operator!=(YGStyle style) {
return !(*this == style);
}
YGStyle::~YGStyle() {}

43
yoga/YGStyle.h Normal file
View File

@@ -0,0 +1,43 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#pragma once
#include "Yoga-internal.h"
#include "Yoga.h"
struct YGStyle {
YGDirection direction;
YGFlexDirection flexDirection;
YGJustify justifyContent;
YGAlign alignContent;
YGAlign alignItems;
YGAlign alignSelf;
YGPositionType positionType;
YGWrap flexWrap;
YGOverflow overflow;
YGDisplay display;
float flex;
float flexGrow;
float flexShrink;
YGValue flexBasis;
std::array<YGValue, YGEdgeCount> margin;
std::array<YGValue, YGEdgeCount> position;
std::array<YGValue, YGEdgeCount> padding;
std::array<YGValue, YGEdgeCount> border;
std::array<YGValue, 2> dimensions;
std::array<YGValue, 2> minDimensions;
std::array<YGValue, 2> maxDimensions;
float aspectRatio;
YGStyle();
// Yoga specific properties, not compatible with flexbox specification
bool operator==(const YGStyle& style);
bool operator!=(YGStyle style);
~YGStyle();
};

View File

@@ -10,7 +10,6 @@
#include <array>
#include <cmath>
#include <vector>
#include "Yoga.h"
using YGVector = std::vector<YGNodeRef>;
@@ -42,6 +41,23 @@ bool YGValueArrayEqual(
return areEqual;
}
const YGValue kYGValueUndefined = {YGUndefined, YGUnitUndefined};
const YGValue kYGValueAuto = {YGUndefined, YGUnitAuto};
const std::array<YGValue, YGEdgeCount> kYGDefaultEdgeValuesUnit = {
{kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined,
kYGValueUndefined}};
const std::array<YGValue, 2> kYGDefaultDimensionValuesAutoUnit = {
{kYGValueAuto, kYGValueAuto}};
const std::array<YGValue, 2> kYGDefaultDimensionValuesUnit = {
{kYGValueUndefined, kYGValueUndefined}};
struct YGCachedMeasurement {
float availableWidth;
float availableHeight;
@@ -141,74 +157,6 @@ struct YGLayout {
}
};
struct YGStyle {
YGDirection direction;
YGFlexDirection flexDirection;
YGJustify justifyContent;
YGAlign alignContent;
YGAlign alignItems;
YGAlign alignSelf;
YGPositionType positionType;
YGWrap flexWrap;
YGOverflow overflow;
YGDisplay display;
float flex;
float flexGrow;
float flexShrink;
YGValue flexBasis;
std::array<YGValue, YGEdgeCount> margin;
std::array<YGValue, YGEdgeCount> position;
std::array<YGValue, YGEdgeCount> padding;
std::array<YGValue, YGEdgeCount> border;
std::array<YGValue, 2> dimensions;
std::array<YGValue, 2> minDimensions;
std::array<YGValue, 2> maxDimensions;
// Yoga specific properties, not compatible with flexbox specification
float aspectRatio;
bool operator==(YGStyle style) {
bool areNonFloatValuesEqual = direction == style.direction &&
flexDirection == style.flexDirection &&
justifyContent == style.justifyContent &&
alignContent == style.alignContent && alignItems == style.alignItems &&
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);
if (!(std::isnan(flex) && std::isnan(style.flex))) {
areNonFloatValuesEqual = areNonFloatValuesEqual && flex == style.flex;
}
if (!(std::isnan(flexGrow) && std::isnan(style.flexGrow))) {
areNonFloatValuesEqual =
areNonFloatValuesEqual && flexGrow == style.flexGrow;
}
if (!(std::isnan(flexShrink) && std::isnan(style.flexShrink))) {
areNonFloatValuesEqual =
areNonFloatValuesEqual && flexShrink == style.flexShrink;
}
if (!(std::isnan(aspectRatio) && std::isnan(style.aspectRatio))) {
areNonFloatValuesEqual =
areNonFloatValuesEqual && aspectRatio == style.aspectRatio;
}
return areNonFloatValuesEqual;
}
bool operator!=(YGStyle style) {
return !(*this == style);
}
};
struct YGConfig {
bool experimentalFeatures[YGExperimentalFeatureCount + 1];
bool useWebDefaults;
@@ -220,94 +168,10 @@ struct YGConfig {
void* context;
};
#define YG_UNDEFINED_VALUES \
{ .value = YGUndefined, .unit = YGUnitUndefined }
#define YG_AUTO_VALUES \
{ .value = YGUndefined, .unit = YGUnitAuto }
#define YG_DEFAULT_EDGE_VALUES_UNIT \
{ \
[YGEdgeLeft] = YG_UNDEFINED_VALUES, [YGEdgeTop] = YG_UNDEFINED_VALUES, \
[YGEdgeRight] = YG_UNDEFINED_VALUES, [YGEdgeBottom] = YG_UNDEFINED_VALUES, \
[YGEdgeStart] = YG_UNDEFINED_VALUES, [YGEdgeEnd] = YG_UNDEFINED_VALUES, \
[YGEdgeHorizontal] = YG_UNDEFINED_VALUES, \
[YGEdgeVertical] = YG_UNDEFINED_VALUES, [YGEdgeAll] = YG_UNDEFINED_VALUES, \
}
#define YG_DEFAULT_DIMENSION_VALUES \
{ [YGDimensionWidth] = YGUndefined, [YGDimensionHeight] = YGUndefined, }
#define YG_DEFAULT_DIMENSION_VALUES_UNIT \
{ \
[YGDimensionWidth] = YG_UNDEFINED_VALUES, \
[YGDimensionHeight] = YG_UNDEFINED_VALUES, \
}
#define YG_DEFAULT_DIMENSION_VALUES_AUTO_UNIT \
{ [YGDimensionWidth] = YG_AUTO_VALUES, [YGDimensionHeight] = YG_AUTO_VALUES, }
static const float kDefaultFlexGrow = 0.0f;
static const float kDefaultFlexShrink = 0.0f;
static const float kWebDefaultFlexShrink = 1.0f;
static const YGStyle gYGNodeStyleDefaults = {
.direction = YGDirectionInherit,
.flexDirection = YGFlexDirectionColumn,
.justifyContent = YGJustifyFlexStart,
.alignContent = YGAlignFlexStart,
.alignItems = YGAlignStretch,
.alignSelf = YGAlignAuto,
.positionType = YGPositionTypeRelative,
.flexWrap = YGWrapNoWrap,
.overflow = YGOverflowVisible,
.display = YGDisplayFlex,
.flex = YGUndefined,
.flexGrow = YGUndefined,
.flexShrink = YGUndefined,
.flexBasis = YG_AUTO_VALUES,
.margin = {{YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES}},
.position = {{YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES}},
.padding = {{YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES}},
.border = {{YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES,
YG_UNDEFINED_VALUES}},
.dimensions = {{YG_AUTO_VALUES, YG_AUTO_VALUES}},
.minDimensions = {{YG_UNDEFINED_VALUES, YG_UNDEFINED_VALUES}},
.maxDimensions = {{YG_UNDEFINED_VALUES, YG_UNDEFINED_VALUES}},
.aspectRatio = YGUndefined,
};
static const YGLayout gYGNodeLayoutDefaults = {
.position = {},
.dimensions = {{YGUndefined, YGUndefined}},