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 9550126f76
commit 6b08db68bb
7 changed files with 241 additions and 175 deletions

View File

@@ -227,19 +227,31 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\..\yoga\Yoga.h" />
<ClInclude Include="..\..\yoga\Utils.h" />
<ClInclude Include="..\..\yoga\YGConfig.h" />
<ClInclude Include="..\..\yoga\YGEnums.h" />
<ClInclude Include="..\..\yoga\YGFloatOptional.h" />
<ClInclude Include="..\..\yoga\YGLayout.h" />
<ClInclude Include="..\..\yoga\YGMacros.h" />
<ClInclude Include="..\..\yoga\YGNodeList.h" />
<ClInclude Include="..\..\yoga\YGNode.h" />
<ClInclude Include="..\..\yoga\YGNodePrint.h" />
<ClInclude Include="..\..\yoga\YGStyle.h" />
<ClInclude Include="..\..\yoga\Yoga-internal.h" />
<ClInclude Include="..\..\yoga\Yoga.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="YGInterop.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\yoga\Yoga.c" />
<ClCompile Include="..\..\yoga\YGEnums.c" />
<ClCompile Include="..\..\yoga\YGNodeList.c" />
<ClCompile Include="..\..\yoga\Utils.cpp" />
<ClCompile Include="..\..\yoga\YGConfig.cpp" />
<ClCompile Include="..\..\yoga\YGEnums.cpp" />
<ClCompile Include="..\..\yoga\YGFloatOptional.cpp" />
<ClCompile Include="..\..\yoga\YGLayout.cpp" />
<ClCompile Include="..\..\yoga\YGNode.cpp" />
<ClCompile Include="..\..\yoga\YGNodePrint.cpp" />
<ClCompile Include="..\..\yoga\YGStyle.cpp" />
<ClCompile Include="..\..\yoga\Yoga.cpp" />
<ClCompile Include="YGInterop.cpp" />
<ClCompile Include="dllmain.cpp">
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</CompileAsManaged>

View File

@@ -21,19 +21,40 @@
<ClInclude Include="targetver.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\yoga\Yoga.h">
<ClInclude Include="resource.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\yoga\Utils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\yoga\YGEnums.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\yoga\YGFloatOptional.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\yoga\YGLayout.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\yoga\YGMacros.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\yoga\YGNodeList.h">
<ClInclude Include="..\..\yoga\YGNode.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="YGInterop.h">
<ClInclude Include="..\..\yoga\YGNodePrint.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="resource.h">
<ClInclude Include="..\..\yoga\YGStyle.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\yoga\Yoga.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\yoga\Yoga-internal.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\yoga\YGConfig.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
@@ -44,15 +65,36 @@
<ClCompile Include="dllmain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\yoga\Yoga.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\yoga\YGNodeList.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="YGInterop.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\yoga\Utils.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\yoga\YGEnums.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\yoga\YGFloatOptional.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\yoga\YGLayout.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\yoga\YGNode.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\yoga\YGNodePrint.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\yoga\YGStyle.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\yoga\Yoga.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\yoga\YGConfig.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Yoga.rc">

19
yoga/YGConfig.cpp Normal file
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) {}

23
yoga/YGConfig.h Normal file
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;
}
@@ -594,8 +575,8 @@ float YGNodeStyleGetFlexShrink(const YGNodeRef node) {
type, name, paramName, instanceName) \
void YGNodeStyleSet##name(const YGNodeRef node, const type paramName) { \
YGValue value = { \
.value = YGFloatSanitize(paramName), \
.unit = YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPoint, \
YGFloatSanitize(paramName), \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPoint, \
}; \
if ((node->getStyle().instanceName.value != value.value && \
value.unit != YGUnitUndefined) || \
@@ -610,8 +591,7 @@ float YGNodeStyleGetFlexShrink(const YGNodeRef node) {
void YGNodeStyleSet##name##Percent( \
const YGNodeRef node, const type paramName) { \
YGValue value = { \
.value = YGFloatSanitize(paramName), \
.unit = \
YGFloatSanitize(paramName), \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPercent, \
}; \
if ((node->getStyle().instanceName.value != value.value && \
@@ -629,8 +609,8 @@ float YGNodeStyleGetFlexShrink(const YGNodeRef node) {
type, name, paramName, instanceName) \
void YGNodeStyleSet##name(const YGNodeRef node, const type paramName) { \
YGValue value = { \
.value = YGFloatSanitize(paramName), \
.unit = YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPoint, \
YGFloatSanitize(paramName), \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPoint, \
}; \
if ((node->getStyle().instanceName.value != value.value && \
value.unit != YGUnitUndefined) || \
@@ -713,8 +693,8 @@ float YGNodeStyleGetFlexShrink(const YGNodeRef node) {
void YGNodeStyleSet##name( \
const YGNodeRef node, const YGEdge edge, const float paramName) { \
YGValue value = { \
.value = YGFloatSanitize(paramName), \
.unit = YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPoint, \
YGFloatSanitize(paramName), \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPoint, \
}; \
if ((node->getStyle().instanceName[edge].value != value.value && \
value.unit != YGUnitUndefined) || \
@@ -729,8 +709,7 @@ float YGNodeStyleGetFlexShrink(const YGNodeRef node) {
void YGNodeStyleSet##name##Percent( \
const YGNodeRef node, const YGEdge edge, const float paramName) { \
YGValue value = { \
.value = YGFloatSanitize(paramName), \
.unit = \
YGFloatSanitize(paramName), \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPercent, \
}; \
if ((node->getStyle().instanceName[edge].value != value.value && \
@@ -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) {