Add constructor in YGLayout

Summary: Add default constructor in YGLayout and moved to separate file. This diff also addresses https://github.com/facebook/yoga/pull/700

Reviewed By: emilsjolander

Differential Revision: D7019653

fbshipit-source-id: 5a2655626db0915fcebe7d4517e2d0b2e2484460
This commit is contained in:
Pritesh Nandgaonkar
2018-02-20 05:41:50 -08:00
committed by Facebook Github Bot
parent 52f2cf319a
commit 91d3c08248
6 changed files with 138 additions and 108 deletions

73
yoga/YGLayout.cpp Normal file
View File

@@ -0,0 +1,73 @@
/**
* 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 "YGLayout.h"
const std::array<float, 2> kYGDefaultDimensionValues = {
{YGUndefined, YGUndefined}};
YGLayout::YGLayout()
: position(),
dimensions(kYGDefaultDimensionValues),
margin(),
border(),
padding(),
direction(YGDirectionInherit),
computedFlexBasisGeneration(0),
computedFlexBasis(YGUndefined),
hadOverflow(false),
generationCount(0),
lastParentDirection((YGDirection)-1),
nextCachedMeasurementsIndex(0),
cachedMeasurements(),
measuredDimensions(kYGDefaultDimensionValues),
cachedLayout({
.availableWidth = 0,
.availableHeight = 0,
.widthMeasureMode = (YGMeasureMode)-1,
.heightMeasureMode = (YGMeasureMode)-1,
.computedWidth = -1,
.computedHeight = -1,
}),
didUseLegacyFlag(false),
doesLegacyStretchFlagAffectsLayout(false) {}
bool YGLayout::operator==(YGLayout layout) const {
bool isEqual = position == layout.position &&
dimensions == layout.dimensions && margin == layout.margin &&
border == layout.border && padding == layout.padding &&
direction == layout.direction && hadOverflow == layout.hadOverflow &&
lastParentDirection == layout.lastParentDirection &&
nextCachedMeasurementsIndex == layout.nextCachedMeasurementsIndex &&
cachedLayout == layout.cachedLayout;
for (uint32_t i = 0; i < YG_MAX_CACHED_RESULT_COUNT && isEqual; ++i) {
isEqual = isEqual && cachedMeasurements[i] == layout.cachedMeasurements[i];
}
if (!YGFloatIsUndefined(computedFlexBasis) ||
!YGFloatIsUndefined(layout.computedFlexBasis)) {
isEqual = isEqual && (computedFlexBasis == layout.computedFlexBasis);
}
if (!YGFloatIsUndefined(measuredDimensions[0]) ||
!YGFloatIsUndefined(layout.measuredDimensions[0])) {
isEqual =
isEqual && (measuredDimensions[0] == layout.measuredDimensions[0]);
}
if (!YGFloatIsUndefined(measuredDimensions[1]) ||
!YGFloatIsUndefined(layout.measuredDimensions[1])) {
isEqual =
isEqual && (measuredDimensions[1] == layout.measuredDimensions[1]);
}
return isEqual;
}
bool YGLayout::operator!=(YGLayout layout) const {
return !(*this == layout);
}

42
yoga/YGLayout.h Normal file
View File

@@ -0,0 +1,42 @@
/**
* 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"
struct YGLayout {
std::array<float, 4> position;
std::array<float, 2> dimensions;
std::array<float, 6> margin;
std::array<float, 6> border;
std::array<float, 6> padding;
YGDirection direction;
uint32_t computedFlexBasisGeneration;
float computedFlexBasis;
bool hadOverflow;
// Instead of recomputing the entire layout every single time, we
// cache some information to break early when nothing changed
uint32_t generationCount;
YGDirection lastParentDirection;
uint32_t nextCachedMeasurementsIndex;
std::array<YGCachedMeasurement, YG_MAX_CACHED_RESULT_COUNT>
cachedMeasurements;
std::array<float, 2> measuredDimensions;
YGCachedMeasurement cachedLayout;
bool didUseLegacyFlag;
bool doesLegacyStretchFlagAffectsLayout;
YGLayout();
bool operator==(YGLayout layout) const;
bool operator!=(YGLayout layout) const;
};

View File

@@ -383,7 +383,7 @@ YGNode::YGNode()
baseline_(nullptr), baseline_(nullptr),
dirtied_(nullptr), dirtied_(nullptr),
style_(YGStyle()), style_(YGStyle()),
layout_(gYGNodeLayoutDefaults), layout_(YGLayout()),
lineIndex_(0), lineIndex_(0),
parent_(nullptr), parent_(nullptr),
children_(YGVector()), children_(YGVector()),

View File

@@ -7,6 +7,7 @@
#pragma once #pragma once
#include <stdio.h> #include <stdio.h>
#include "YGLayout.h"
#include "YGStyle.h" #include "YGStyle.h"
#include "Yoga-internal.h" #include "Yoga-internal.h"

View File

@@ -8,6 +8,27 @@
*/ */
#include "YGStyle.h" #include "YGStyle.h"
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}};
YGStyle::YGStyle() YGStyle::YGStyle()
: direction(YGDirectionInherit), : direction(YGDirectionInherit),
flexDirection(YGFlexDirectionColumn), flexDirection(YGFlexDirectionColumn),

View File

@@ -41,23 +41,6 @@ bool YGValueArrayEqual(
return areEqual; 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 { struct YGCachedMeasurement {
float availableWidth; float availableWidth;
float availableHeight; float availableHeight;
@@ -95,68 +78,6 @@ struct YGCachedMeasurement {
// layouts should not require more than 16 entries to fit within the cache. // layouts should not require more than 16 entries to fit within the cache.
#define YG_MAX_CACHED_RESULT_COUNT 16 #define YG_MAX_CACHED_RESULT_COUNT 16
struct YGLayout {
std::array<float, 4> position;
std::array<float, 2> dimensions;
std::array<float, 6> margin;
std::array<float, 6> border;
std::array<float, 6> padding;
YGDirection direction;
uint32_t computedFlexBasisGeneration;
float computedFlexBasis;
bool hadOverflow;
// Instead of recomputing the entire layout every single time, we
// cache some information to break early when nothing changed
uint32_t generationCount;
YGDirection lastParentDirection;
uint32_t nextCachedMeasurementsIndex;
YGCachedMeasurement cachedMeasurements[YG_MAX_CACHED_RESULT_COUNT];
std::array<float, 2> measuredDimensions;
YGCachedMeasurement cachedLayout;
bool didUseLegacyFlag;
bool doesLegacyStretchFlagAffectsLayout;
bool operator==(YGLayout layout) const {
bool isEqual = position == layout.position &&
dimensions == layout.dimensions && margin == layout.margin &&
border == layout.border && padding == layout.padding &&
direction == layout.direction && hadOverflow == layout.hadOverflow &&
lastParentDirection == layout.lastParentDirection &&
nextCachedMeasurementsIndex == layout.nextCachedMeasurementsIndex &&
cachedLayout == layout.cachedLayout;
for (uint32_t i = 0; i < YG_MAX_CACHED_RESULT_COUNT && isEqual; ++i) {
isEqual =
isEqual && cachedMeasurements[i] == layout.cachedMeasurements[i];
}
if (!YGFloatIsUndefined(computedFlexBasis) ||
!YGFloatIsUndefined(layout.computedFlexBasis)) {
isEqual = isEqual && (computedFlexBasis == layout.computedFlexBasis);
}
if (!YGFloatIsUndefined(measuredDimensions[0]) ||
!YGFloatIsUndefined(layout.measuredDimensions[0])) {
isEqual =
isEqual && (measuredDimensions[0] == layout.measuredDimensions[0]);
}
if (!YGFloatIsUndefined(measuredDimensions[1]) ||
!YGFloatIsUndefined(layout.measuredDimensions[1])) {
isEqual =
isEqual && (measuredDimensions[1] == layout.measuredDimensions[1]);
}
return isEqual;
}
bool operator!=(YGLayout layout) const {
return !(*this == layout);
}
};
struct YGConfig { struct YGConfig {
bool experimentalFeatures[YGExperimentalFeatureCount + 1]; bool experimentalFeatures[YGExperimentalFeatureCount + 1];
bool useWebDefaults; bool useWebDefaults;
@@ -172,34 +93,6 @@ static const float kDefaultFlexGrow = 0.0f;
static const float kDefaultFlexShrink = 0.0f; static const float kDefaultFlexShrink = 0.0f;
static const float kWebDefaultFlexShrink = 1.0f; static const float kWebDefaultFlexShrink = 1.0f;
static const YGLayout gYGNodeLayoutDefaults = {
.position = {},
.dimensions = {{YGUndefined, YGUndefined}},
.margin = {},
.border = {},
.padding = {},
.direction = YGDirectionInherit,
.computedFlexBasisGeneration = 0,
.computedFlexBasis = YGUndefined,
.hadOverflow = false,
.generationCount = 0,
.lastParentDirection = (YGDirection)-1,
.nextCachedMeasurementsIndex = 0,
.cachedMeasurements = {},
.measuredDimensions = {{YGUndefined, YGUndefined}},
.cachedLayout =
{
.availableWidth = 0,
.availableHeight = 0,
.widthMeasureMode = (YGMeasureMode)-1,
.heightMeasureMode = (YGMeasureMode)-1,
.computedWidth = -1,
.computedHeight = -1,
},
.didUseLegacyFlag = false,
.doesLegacyStretchFlagAffectsLayout = false,
};
extern bool YGFloatsEqual(const float a, const float b); extern bool YGFloatsEqual(const float a, const float b);
extern bool YGValueEqual(const YGValue a, const YGValue b); extern bool YGValueEqual(const YGValue a, const YGValue b);
extern const YGValue* YGComputedEdgeValue( extern const YGValue* YGComputedEdgeValue(