Make YGNode as c++ struct with properties exposed through accessors

Summary: Moved c implementation of `YGNode` to C++ struct. Not moving to C++ class as the React Classes dependent on `Yoga.h` assume it to be C. Thats why keeping `Yoga.h` C compatible. Sorry for the long diff, didn't thought that it will turn out to be this much big.Will keep an eye on number of lines next time 😉

Reviewed By: emilsjolander

Differential Revision: D6592257

fbshipit-source-id: 641e8b9462ad00731a094511f9f5608b23a6bb21
This commit is contained in:
Pritesh Nandgaonkar
2017-12-19 11:18:00 -08:00
committed by Facebook Github Bot
parent dbf6a12134
commit fbd332dee8
16 changed files with 1957 additions and 1091 deletions

View File

@@ -10,6 +10,7 @@
#include "YGNodePrint.h"
#include <stdarg.h>
#include "YGEnums.h"
#include "YGNode.h"
#include "Yoga-internal.h"
namespace facebook {
@@ -22,7 +23,7 @@ static void indent(string* base, uint32_t level) {
}
}
static bool areFourValuesEqual(const YGValue four[4]) {
static bool areFourValuesEqual(const std::array<YGValue, YGEdgeCount>& four) {
return YGValueEqual(four[0], four[1]) && YGValueEqual(four[0], four[2]) &&
YGValueEqual(four[0], four[3]);
}
@@ -50,54 +51,53 @@ appendFloatIfNotUndefined(string* base, const string key, const float num) {
static void appendNumberIfNotUndefined(
string* base,
const string key,
const YGValue* const number) {
if (number->unit != YGUnitUndefined) {
if (number->unit == YGUnitAuto) {
const YGValue number) {
if (number.unit != YGUnitUndefined) {
if (number.unit == YGUnitAuto) {
base->append(key + ": auto; ");
} else {
string unit = number->unit == YGUnitPoint ? "px" : "%%";
string unit = number.unit == YGUnitPoint ? "px" : "%%";
appendFormatedString(
base, "%s: %g%s; ", key.c_str(), number->value, unit.c_str());
base, "%s: %g%s; ", key.c_str(), number.value, unit.c_str());
}
}
}
static void appendNumberIfNotAuto(
string* base,
const string key,
const YGValue* const number) {
if (number->unit != YGUnitAuto) {
static void
appendNumberIfNotAuto(string* base, const string& key, const YGValue number) {
if (number.unit != YGUnitAuto) {
appendNumberIfNotUndefined(base, key, number);
}
}
static void appendNumberIfNotZero(
string* base,
const string str,
const YGValue* const number) {
if (!YGFloatsEqual(number->value, 0)) {
static void
appendNumberIfNotZero(string* base, const string& str, const YGValue number) {
if (!YGFloatsEqual(number.value, 0)) {
appendNumberIfNotUndefined(base, str, number);
}
}
static void appendEdges(string* base, const string key, const YGValue* edges) {
static void appendEdges(
string* base,
const string& key,
const std::array<YGValue, YGEdgeCount>& edges) {
if (areFourValuesEqual(edges)) {
appendNumberIfNotZero(base, key, &edges[YGEdgeLeft]);
appendNumberIfNotZero(base, key, edges[YGEdgeLeft]);
} else {
for (int edge = YGEdgeLeft; edge != YGEdgeAll; ++edge) {
string str = key + "-" + YGEdgeToString(static_cast<YGEdge>(edge));
appendNumberIfNotZero(base, str, &edges[edge]);
appendNumberIfNotZero(base, str, edges[edge]);
}
}
}
static void appendEdgeIfNotUndefined(
string* base,
const string str,
const YGValue* edges,
const string& str,
const std::array<YGValue, YGEdgeCount>& edges,
const YGEdge edge) {
appendNumberIfNotUndefined(
base, str, YGComputedEdgeValue(edges, edge, &YGValueUndefined));
base, str, *YGComputedEdgeValue(edges, edge, &YGValueUndefined));
}
void YGNodeToString(
@@ -107,105 +107,112 @@ void YGNodeToString(
uint32_t level) {
indent(str, level);
appendFormatedString(str, "<div ");
if (node->print != nullptr) {
node->print(node);
if (node->getPrintFunc() != nullptr) {
node->getPrintFunc()(node);
}
if (options & YGPrintOptionsLayout) {
appendFormatedString(str, "layout=\"");
appendFormatedString(
str, "width: %g; ", node->layout.dimensions[YGDimensionWidth]);
str, "width: %g; ", node->getLayout().dimensions[YGDimensionWidth]);
appendFormatedString(
str, "height: %g; ", node->layout.dimensions[YGDimensionHeight]);
appendFormatedString(str, "top: %g; ", node->layout.position[YGEdgeTop]);
appendFormatedString(str, "left: %g;", node->layout.position[YGEdgeLeft]);
str, "height: %g; ", node->getLayout().dimensions[YGDimensionHeight]);
appendFormatedString(
str, "top: %g; ", node->getLayout().position[YGEdgeTop]);
appendFormatedString(
str, "left: %g;", node->getLayout().position[YGEdgeLeft]);
appendFormatedString(str, "\" ");
}
if (options & YGPrintOptionsStyle) {
appendFormatedString(str, "style=\"");
if (node->style.flexDirection != gYGNodeDefaults.style.flexDirection) {
if (node->getStyle().flexDirection != YGNode().getStyle().flexDirection) {
appendFormatedString(
str,
"flex-direction: %s; ",
YGFlexDirectionToString(node->style.flexDirection));
YGFlexDirectionToString(node->getStyle().flexDirection));
}
if (node->style.justifyContent != gYGNodeDefaults.style.justifyContent) {
if (node->getStyle().justifyContent != YGNode().getStyle().justifyContent) {
appendFormatedString(
str,
"justify-content: %s; ",
YGJustifyToString(node->style.justifyContent));
YGJustifyToString(node->getStyle().justifyContent));
}
if (node->style.alignItems != gYGNodeDefaults.style.alignItems) {
if (node->getStyle().alignItems != YGNode().getStyle().alignItems) {
appendFormatedString(
str, "align-items: %s; ", YGAlignToString(node->style.alignItems));
str,
"align-items: %s; ",
YGAlignToString(node->getStyle().alignItems));
}
if (node->style.alignContent != gYGNodeDefaults.style.alignContent) {
if (node->getStyle().alignContent != YGNode().getStyle().alignContent) {
appendFormatedString(
str,
"align-content: %s; ",
YGAlignToString(node->style.alignContent));
YGAlignToString(node->getStyle().alignContent));
}
if (node->style.alignSelf != gYGNodeDefaults.style.alignSelf) {
if (node->getStyle().alignSelf != YGNode().getStyle().alignSelf) {
appendFormatedString(
str, "align-self: %s; ", YGAlignToString(node->style.alignSelf));
str, "align-self: %s; ", YGAlignToString(node->getStyle().alignSelf));
}
appendFloatIfNotUndefined(str, "flex-grow", node->style.flexGrow);
appendFloatIfNotUndefined(str, "flex-shrink", node->style.flexShrink);
appendNumberIfNotAuto(str, "flex-basis", &node->style.flexBasis);
appendFloatIfNotUndefined(str, "flex", node->style.flex);
appendFloatIfNotUndefined(str, "flex-grow", node->getStyle().flexGrow);
appendFloatIfNotUndefined(str, "flex-shrink", node->getStyle().flexShrink);
appendNumberIfNotAuto(str, "flex-basis", node->getStyle().flexBasis);
appendFloatIfNotUndefined(str, "flex", node->getStyle().flex);
if (node->style.flexWrap != gYGNodeDefaults.style.flexWrap) {
if (node->getStyle().flexWrap != YGNode().getStyle().flexWrap) {
appendFormatedString(
str, "flexWrap: %s; ", YGWrapToString(node->style.flexWrap));
str, "flexWrap: %s; ", YGWrapToString(node->getStyle().flexWrap));
}
if (node->style.overflow != gYGNodeDefaults.style.overflow) {
if (node->getStyle().overflow != YGNode().getStyle().overflow) {
appendFormatedString(
str, "overflow: %s; ", YGOverflowToString(node->style.overflow));
str, "overflow: %s; ", YGOverflowToString(node->getStyle().overflow));
}
if (node->style.display != gYGNodeDefaults.style.display) {
if (node->getStyle().display != YGNode().getStyle().display) {
appendFormatedString(
str, "display: %s; ", YGDisplayToString(node->style.display));
str, "display: %s; ", YGDisplayToString(node->getStyle().display));
}
appendEdges(str, "margin", node->style.margin);
appendEdges(str, "padding", node->style.padding);
appendEdges(str, "border", node->style.border);
appendEdges(str, "margin", node->getStyle().margin);
appendEdges(str, "padding", node->getStyle().padding);
appendEdges(str, "border", node->getStyle().border);
appendNumberIfNotAuto(
str, "width", &node->style.dimensions[YGDimensionWidth]);
str, "width", node->getStyle().dimensions[YGDimensionWidth]);
appendNumberIfNotAuto(
str, "height", &node->style.dimensions[YGDimensionHeight]);
str, "height", node->getStyle().dimensions[YGDimensionHeight]);
appendNumberIfNotAuto(
str, "max-width", &node->style.maxDimensions[YGDimensionWidth]);
str, "max-width", node->getStyle().maxDimensions[YGDimensionWidth]);
appendNumberIfNotAuto(
str, "max-height", &node->style.maxDimensions[YGDimensionHeight]);
str, "max-height", node->getStyle().maxDimensions[YGDimensionHeight]);
appendNumberIfNotAuto(
str, "min-width", &node->style.minDimensions[YGDimensionWidth]);
str, "min-width", node->getStyle().minDimensions[YGDimensionWidth]);
appendNumberIfNotAuto(
str, "min-height", &node->style.minDimensions[YGDimensionHeight]);
str, "min-height", node->getStyle().minDimensions[YGDimensionHeight]);
if (node->style.positionType != gYGNodeDefaults.style.positionType) {
if (node->getStyle().positionType != YGNode().getStyle().positionType) {
appendFormatedString(
str,
"position: %s; ",
YGPositionTypeToString(node->style.positionType));
YGPositionTypeToString(node->getStyle().positionType));
}
appendEdgeIfNotUndefined(str, "left", node->style.position, YGEdgeLeft);
appendEdgeIfNotUndefined(str, "right", node->style.position, YGEdgeRight);
appendEdgeIfNotUndefined(str, "top", node->style.position, YGEdgeTop);
appendEdgeIfNotUndefined(str, "bottom", node->style.position, YGEdgeBottom);
appendEdgeIfNotUndefined(
str, "left", node->getStyle().position, YGEdgeLeft);
appendEdgeIfNotUndefined(
str, "right", node->getStyle().position, YGEdgeRight);
appendEdgeIfNotUndefined(str, "top", node->getStyle().position, YGEdgeTop);
appendEdgeIfNotUndefined(
str, "bottom", node->getStyle().position, YGEdgeBottom);
appendFormatedString(str, "\" ");
if (node->measure != nullptr) {
if (node->getMeasure() != nullptr) {
appendFormatedString(str, "has-custom-measure=\"true\"");
}
}
appendFormatedString(str, ">");
const uint32_t childCount = node->children.size();
const uint32_t childCount = node->getChildren().size();
if (options & YGPrintOptionsChildren && childCount > 0) {
for (uint32_t i = 0; i < childCount; i++) {
appendFormatedString(str, "\n");