Fix compilation on MSVC by moving YGConfig to C++

Summary:
This PR fixes the compilation on MSVC. I moved the `YGConfig` creation to a C++ constructor.

Addionally it removes the "dot" notation on `YGValue`, I didn't want to change that type to a C++ constructor, because I think this will break the ABI.
Closes https://github.com/facebook/yoga/pull/746

Differential Revision: D7498141

Pulled By: emilsjolander

fbshipit-source-id: 5f5308ff838dcd803065785ddc08b2404524acb9
This commit is contained in:
Lukas Wöhrl 2018-04-04 09:24:34 -07:00 committed by Facebook Github Bot
parent c041c9fd62
commit 24f574032a
5 changed files with 171 additions and 159 deletions

View File

@ -0,0 +1,19 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "YGConfig.h"
const std::array<bool, YGExperimentalFeatureCount>
kYGDefaultExperimentalFeatures = {{false}};
YGConfig::YGConfig(YGLogger logger)
: experimentalFeatures(kYGDefaultExperimentalFeatures),
useWebDefaults(false),
useLegacyStretchBehaviour(false),
shouldDiffLayoutWithoutLegacyStretchBehaviour(false),
pointScaleFactor(1.0f), logger(logger), cloneNodeCallback(nullptr),
context(nullptr) {}

View File

@ -0,0 +1,23 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include "Yoga-internal.h"
#include "Yoga.h"
struct YGConfig {
std::array<bool, YGExperimentalFeatureCount> experimentalFeatures;
bool useWebDefaults;
bool useLegacyStretchBehaviour;
bool shouldDiffLayoutWithoutLegacyStretchBehaviour;
float pointScaleFactor;
YGLogger logger;
YGCloneNodeFunc cloneNodeCallback;
void* context;
YGConfig(YGLogger logger);
};

View File

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

View File

@ -87,16 +87,6 @@ struct YGCachedMeasurement {
// 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;
YGCloneNodeFunc cloneNodeCallback;
void* context;
};
static const float kDefaultFlexGrow = 0.0f;
static const float kDefaultFlexShrink = 0.0f;

View File

@ -41,25 +41,7 @@ static int YGDefaultLog(const YGConfigRef config,
va_list args);
#endif
static YGConfig gYGConfigDefaults = {
.experimentalFeatures =
{
[YGExperimentalFeatureWebFlexBasis] = false,
},
.useWebDefaults = false,
.useLegacyStretchBehaviour = false,
.shouldDiffLayoutWithoutLegacyStretchBehaviour = false,
.pointScaleFactor = 1.0f,
#ifdef ANDROID
.logger = &YGAndroidLog,
#else
.logger = &YGDefaultLog,
#endif
.cloneNodeCallback = nullptr,
.context = nullptr,
};
const YGValue YGValueZero = {.value = 0, .unit = YGUnitPoint};
const YGValue YGValueZero = {0, YGUnitPoint};
const YGValue YGValueUndefined = {YGUndefined, YGUnitUndefined};
const YGValue YGValueAuto = {YGUndefined, YGUnitAuto};
@ -246,8 +228,13 @@ WIN_EXPORT YGNodeRef YGNodeNewWithConfig(const YGConfigRef config) {
return node;
}
YGConfigRef YGConfigGetDefault() {
static YGConfigRef defaultConfig = YGConfigNew();
return defaultConfig;
}
YGNodeRef YGNodeNew(void) {
return YGNodeNewWithConfig(&gYGConfigDefaults);
return YGNodeNewWithConfig(YGConfigGetDefault());
}
YGNodeRef YGNodeClone(YGNodeRef oldNode) {
@ -363,19 +350,13 @@ int32_t YGConfigGetInstanceCount(void) {
return gConfigInstanceCount;
}
// Export only for C#
YGConfigRef YGConfigGetDefault() {
return &gYGConfigDefaults;
}
YGConfigRef YGConfigNew(void) {
const YGConfigRef config = (const YGConfigRef)malloc(sizeof(YGConfig));
YGAssert(config != nullptr, "Could not allocate memory for config");
if (config == nullptr) {
abort();
}
#ifdef ANDROID
const YGConfigRef config = new YGConfig(YGAndroidLog);
#else
const YGConfigRef config = new YGConfig(YGDefaultLog);
#endif
gConfigInstanceCount++;
memcpy(config, &gYGConfigDefaults, sizeof(YGConfig));
return config;
}
@ -590,79 +571,78 @@ float YGNodeStyleGetFlexShrink(const YGNodeRef node) {
} \
}
#define YG_NODE_STYLE_PROPERTY_SETTER_UNIT_IMPL( \
type, name, paramName, instanceName) \
void YGNodeStyleSet##name(const YGNodeRef node, const type paramName) { \
YGValue value = { \
.value = YGFloatSanitize(paramName), \
.unit = YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPoint, \
}; \
if ((node->getStyle().instanceName.value != value.value && \
value.unit != YGUnitUndefined) || \
node->getStyle().instanceName.unit != value.unit) { \
YGStyle style = node->getStyle(); \
style.instanceName = value; \
node->setStyle(style); \
node->markDirtyAndPropogate(); \
} \
} \
\
void YGNodeStyleSet##name##Percent( \
const YGNodeRef node, const type paramName) { \
YGValue value = { \
.value = YGFloatSanitize(paramName), \
.unit = \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPercent, \
}; \
if ((node->getStyle().instanceName.value != value.value && \
value.unit != YGUnitUndefined) || \
node->getStyle().instanceName.unit != value.unit) { \
YGStyle style = node->getStyle(); \
\
style.instanceName = value; \
node->setStyle(style); \
node->markDirtyAndPropogate(); \
} \
#define YG_NODE_STYLE_PROPERTY_SETTER_UNIT_IMPL( \
type, name, paramName, instanceName) \
void YGNodeStyleSet##name(const YGNodeRef node, const type paramName) { \
YGValue value = { \
YGFloatSanitize(paramName), \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPoint, \
}; \
if ((node->getStyle().instanceName.value != value.value && \
value.unit != YGUnitUndefined) || \
node->getStyle().instanceName.unit != value.unit) { \
YGStyle style = node->getStyle(); \
style.instanceName = value; \
node->setStyle(style); \
node->markDirtyAndPropogate(); \
} \
} \
\
void YGNodeStyleSet##name##Percent( \
const YGNodeRef node, const type paramName) { \
YGValue value = { \
YGFloatSanitize(paramName), \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPercent, \
}; \
if ((node->getStyle().instanceName.value != value.value && \
value.unit != YGUnitUndefined) || \
node->getStyle().instanceName.unit != value.unit) { \
YGStyle style = node->getStyle(); \
\
style.instanceName = value; \
node->setStyle(style); \
node->markDirtyAndPropogate(); \
} \
}
#define YG_NODE_STYLE_PROPERTY_SETTER_UNIT_AUTO_IMPL( \
type, name, paramName, instanceName) \
void YGNodeStyleSet##name(const YGNodeRef node, const type paramName) { \
YGValue value = { \
.value = YGFloatSanitize(paramName), \
.unit = YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPoint, \
}; \
if ((node->getStyle().instanceName.value != value.value && \
value.unit != YGUnitUndefined) || \
node->getStyle().instanceName.unit != value.unit) { \
YGStyle style = node->getStyle(); \
style.instanceName = value; \
node->setStyle(style); \
node->markDirtyAndPropogate(); \
} \
} \
\
void YGNodeStyleSet##name##Percent( \
const YGNodeRef node, const type paramName) { \
if (node->getStyle().instanceName.value != YGFloatSanitize(paramName) || \
node->getStyle().instanceName.unit != YGUnitPercent) { \
YGStyle style = node->getStyle(); \
style.instanceName.value = YGFloatSanitize(paramName); \
style.instanceName.unit = \
YGFloatIsUndefined(paramName) ? YGUnitAuto : YGUnitPercent; \
node->setStyle(style); \
node->markDirtyAndPropogate(); \
} \
} \
\
void YGNodeStyleSet##name##Auto(const YGNodeRef node) { \
if (node->getStyle().instanceName.unit != YGUnitAuto) { \
YGStyle style = node->getStyle(); \
style.instanceName.value = 0; \
style.instanceName.unit = YGUnitAuto; \
node->setStyle(style); \
node->markDirtyAndPropogate(); \
} \
#define YG_NODE_STYLE_PROPERTY_SETTER_UNIT_AUTO_IMPL( \
type, name, paramName, instanceName) \
void YGNodeStyleSet##name(const YGNodeRef node, const type paramName) { \
YGValue value = { \
YGFloatSanitize(paramName), \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPoint, \
}; \
if ((node->getStyle().instanceName.value != value.value && \
value.unit != YGUnitUndefined) || \
node->getStyle().instanceName.unit != value.unit) { \
YGStyle style = node->getStyle(); \
style.instanceName = value; \
node->setStyle(style); \
node->markDirtyAndPropogate(); \
} \
} \
\
void YGNodeStyleSet##name##Percent( \
const YGNodeRef node, const type paramName) { \
if (node->getStyle().instanceName.value != YGFloatSanitize(paramName) || \
node->getStyle().instanceName.unit != YGUnitPercent) { \
YGStyle style = node->getStyle(); \
style.instanceName.value = YGFloatSanitize(paramName); \
style.instanceName.unit = \
YGFloatIsUndefined(paramName) ? YGUnitAuto : YGUnitPercent; \
node->setStyle(style); \
node->markDirtyAndPropogate(); \
} \
} \
\
void YGNodeStyleSet##name##Auto(const YGNodeRef node) { \
if (node->getStyle().instanceName.unit != YGUnitAuto) { \
YGStyle style = node->getStyle(); \
style.instanceName.value = 0; \
style.instanceName.unit = YGUnitAuto; \
node->setStyle(style); \
node->markDirtyAndPropogate(); \
} \
}
#define YG_NODE_STYLE_PROPERTY_IMPL(type, name, paramName, instanceName) \
@ -708,48 +688,47 @@ float YGNodeStyleGetFlexShrink(const YGNodeRef node) {
} \
}
#define YG_NODE_STYLE_EDGE_PROPERTY_UNIT_IMPL( \
type, name, paramName, instanceName) \
void YGNodeStyleSet##name( \
const YGNodeRef node, const YGEdge edge, const float paramName) { \
YGValue value = { \
.value = YGFloatSanitize(paramName), \
.unit = YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPoint, \
}; \
if ((node->getStyle().instanceName[edge].value != value.value && \
value.unit != YGUnitUndefined) || \
node->getStyle().instanceName[edge].unit != value.unit) { \
YGStyle style = node->getStyle(); \
style.instanceName[edge] = value; \
node->setStyle(style); \
node->markDirtyAndPropogate(); \
} \
} \
\
void YGNodeStyleSet##name##Percent( \
const YGNodeRef node, const YGEdge edge, const float paramName) { \
YGValue value = { \
.value = YGFloatSanitize(paramName), \
.unit = \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPercent, \
}; \
if ((node->getStyle().instanceName[edge].value != value.value && \
value.unit != YGUnitUndefined) || \
node->getStyle().instanceName[edge].unit != value.unit) { \
YGStyle style = node->getStyle(); \
style.instanceName[edge] = value; \
node->setStyle(style); \
node->markDirtyAndPropogate(); \
} \
} \
\
WIN_STRUCT(type) \
YGNodeStyleGet##name(const YGNodeRef node, const YGEdge edge) { \
YGValue value = node->getStyle().instanceName[edge]; \
if (value.unit == YGUnitUndefined || value.unit == YGUnitAuto) { \
value.value = YGUndefined; \
} \
return WIN_STRUCT_REF(value); \
#define YG_NODE_STYLE_EDGE_PROPERTY_UNIT_IMPL( \
type, name, paramName, instanceName) \
void YGNodeStyleSet##name( \
const YGNodeRef node, const YGEdge edge, const float paramName) { \
YGValue value = { \
YGFloatSanitize(paramName), \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPoint, \
}; \
if ((node->getStyle().instanceName[edge].value != value.value && \
value.unit != YGUnitUndefined) || \
node->getStyle().instanceName[edge].unit != value.unit) { \
YGStyle style = node->getStyle(); \
style.instanceName[edge] = value; \
node->setStyle(style); \
node->markDirtyAndPropogate(); \
} \
} \
\
void YGNodeStyleSet##name##Percent( \
const YGNodeRef node, const YGEdge edge, const float paramName) { \
YGValue value = { \
YGFloatSanitize(paramName), \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPercent, \
}; \
if ((node->getStyle().instanceName[edge].value != value.value && \
value.unit != YGUnitUndefined) || \
node->getStyle().instanceName[edge].unit != value.unit) { \
YGStyle style = node->getStyle(); \
style.instanceName[edge] = value; \
node->setStyle(style); \
node->markDirtyAndPropogate(); \
} \
} \
\
WIN_STRUCT(type) \
YGNodeStyleGet##name(const YGNodeRef node, const YGEdge edge) { \
YGValue value = node->getStyle().instanceName[edge]; \
if (value.unit == YGUnitUndefined || value.unit == YGUnitAuto) { \
value.value = YGUndefined; \
} \
return WIN_STRUCT_REF(value); \
}
#define YG_NODE_LAYOUT_PROPERTY_IMPL(type, name, instanceName) \
@ -858,8 +837,8 @@ YGValue YGNodeStyleGetFlexBasis(const YGNodeRef node) {
void YGNodeStyleSetFlexBasis(const YGNodeRef node, const float flexBasis) {
YGValue value = {
.value = YGFloatSanitize(flexBasis),
.unit = YGFloatIsUndefined(flexBasis) ? YGUnitUndefined : YGUnitPoint,
YGFloatSanitize(flexBasis),
YGFloatIsUndefined(flexBasis) ? YGUnitUndefined : YGUnitPoint,
};
if ((node->getStyle().flexBasis.value != value.value &&
value.unit != YGUnitUndefined) ||
@ -906,8 +885,8 @@ void YGNodeStyleSetBorder(
const YGEdge edge,
const float border) {
YGValue value = {
.value = YGFloatSanitize(border),
.unit = YGFloatIsUndefined(border) ? YGUnitUndefined : YGUnitPoint,
YGFloatSanitize(border),
YGFloatIsUndefined(border) ? YGUnitUndefined : YGUnitPoint,
};
if ((node->getStyle().border[edge].value != value.value &&
value.unit != YGUnitUndefined) ||
@ -3977,7 +3956,7 @@ static void YGVLog(const YGConfigRef config,
YGLogLevel level,
const char *format,
va_list args) {
const YGConfigRef logConfig = config != nullptr ? config : &gYGConfigDefaults;
const YGConfigRef logConfig = config != nullptr ? config : YGConfigGetDefault();
logConfig->logger(logConfig, node, level, format, args);
if (level == YGLogLevelFatal) {