Revert D48710796: C++ Cleanup 2/N: Reorganize YGConfig

Differential Revision:
D48710796

Original commit changeset: d548553f7ce8

Original Phabricator Diff: D48710796

fbshipit-source-id: c8b2de245f3894f6a87c262ec70d313020aa228e
This commit is contained in:
Zhiyao Zhou
2023-08-29 23:27:25 -07:00
committed by Facebook GitHub Bot
parent ea7f61a3db
commit 13c5ce2234
10 changed files with 107 additions and 122 deletions

View File

@@ -19,8 +19,6 @@
// API and use that
#include <yoga/YGNode.h>
using namespace facebook;
using namespace facebook::yoga;
using namespace facebook::yoga::vanillajni;
static inline ScopedLocalRef<jobject> YGNodeJobject(
@@ -196,7 +194,7 @@ static void jni_YGConfigSetLoggerJNI(
}
*context = newGlobalRef(env, logger);
static_cast<yoga::Config*>(config)->setLogger(YGJNILogFunc);
config->setLogger(YGJNILogFunc);
} else {
if (context != nullptr) {
delete context;

View File

@@ -9,16 +9,14 @@
#include <gtest/gtest.h>
#include <yoga/Yoga.h>
#include <yoga/config/Config.h>
#include <yoga/YGConfig.h>
#include <yoga/YGNode.h>
#include <functional>
#include <memory>
using namespace facebook;
struct ConfigCloningTest : public ::testing::Test {
std::unique_ptr<yoga::Config, std::function<void(yoga::Config*)>> config;
std::unique_ptr<YGConfig, std::function<void(YGConfig*)>> config;
void SetUp() override;
void TearDown() override;
@@ -58,7 +56,7 @@ TEST_F(ConfigCloningTest, can_clone_with_context) {
}
void ConfigCloningTest::SetUp() {
config = {static_cast<yoga::Config*>(YGConfigNew()), YGConfigFree};
config = {YGConfigNew(), YGConfigFree};
}
void ConfigCloningTest::TearDown() {

View File

@@ -5,129 +5,133 @@
* LICENSE file in the root directory of this source tree.
*/
#include <yoga/config/Config.h>
#include "YGConfig.h"
using namespace facebook::yoga;
namespace facebook::yoga {
bool configUpdateInvalidatesLayout(Config* a, Config* b) {
bool configUpdateInvalidatesLayout(YGConfigRef a, YGConfigRef b) {
return a->getErrata() != b->getErrata() ||
a->getEnabledExperiments() != b->getEnabledExperiments() ||
a->getPointScaleFactor() != b->getPointScaleFactor() ||
a->useWebDefaults() != b->useWebDefaults();
}
} // namespace facebook::yoga
Config::Config(YGLogger logger) : cloneNodeCallback_{nullptr} {
YGConfig::YGConfig(YGLogger logger) : cloneNodeCallback_{nullptr} {
setLogger(logger);
}
void Config::setUseWebDefaults(bool useWebDefaults) {
void YGConfig::setUseWebDefaults(bool useWebDefaults) {
flags_.useWebDefaults = useWebDefaults;
}
bool Config::useWebDefaults() const {
bool YGConfig::useWebDefaults() const {
return flags_.useWebDefaults;
}
void Config::setShouldPrintTree(bool printTree) {
void YGConfig::setShouldPrintTree(bool printTree) {
flags_.printTree = printTree;
}
bool Config::shouldPrintTree() const {
bool YGConfig::shouldPrintTree() const {
return flags_.printTree;
}
void Config::setExperimentalFeatureEnabled(
void YGConfig::setExperimentalFeatureEnabled(
YGExperimentalFeature feature,
bool enabled) {
experimentalFeatures_.set(feature, enabled);
}
bool Config::isExperimentalFeatureEnabled(YGExperimentalFeature feature) const {
bool YGConfig::isExperimentalFeatureEnabled(
YGExperimentalFeature feature) const {
return experimentalFeatures_.test(feature);
}
ExperimentalFeatureSet Config::getEnabledExperiments() const {
ExperimentalFeatureSet YGConfig::getEnabledExperiments() const {
return experimentalFeatures_;
}
void Config::setErrata(YGErrata errata) {
void YGConfig::setErrata(YGErrata errata) {
errata_ = errata;
}
void Config::addErrata(YGErrata errata) {
void YGConfig::addErrata(YGErrata errata) {
errata_ |= errata;
}
void Config::removeErrata(YGErrata errata) {
void YGConfig::removeErrata(YGErrata errata) {
errata_ &= (~errata);
}
YGErrata Config::getErrata() const {
YGErrata YGConfig::getErrata() const {
return errata_;
}
bool Config::hasErrata(YGErrata errata) const {
bool YGConfig::hasErrata(YGErrata errata) const {
return (errata_ & errata) != YGErrataNone;
}
void Config::setPointScaleFactor(float pointScaleFactor) {
void YGConfig::setPointScaleFactor(float pointScaleFactor) {
pointScaleFactor_ = pointScaleFactor;
}
float Config::getPointScaleFactor() const {
float YGConfig::getPointScaleFactor() const {
return pointScaleFactor_;
}
void Config::setContext(void* context) {
void YGConfig::setContext(void* context) {
context_ = context;
}
void* Config::getContext() const {
void* YGConfig::getContext() const {
return context_;
}
void Config::setLogger(YGLogger logger) {
void YGConfig::setLogger(YGLogger logger) {
logger_.noContext = logger;
flags_.loggerUsesContext = false;
}
void Config::setLogger(LogWithContextFn logger) {
void YGConfig::setLogger(LogWithContextFn logger) {
logger_.withContext = logger;
flags_.loggerUsesContext = true;
}
void Config::setLogger(std::nullptr_t) {
void YGConfig::setLogger(std::nullptr_t) {
setLogger(YGLogger{nullptr});
}
void Config::log(
YGNodeRef node,
void YGConfig::log(
YGConfig* config,
YGNode* node,
YGLogLevel logLevel,
void* logContext,
const char* format,
va_list args) {
va_list args) const {
if (flags_.loggerUsesContext) {
logger_.withContext(this, node, logLevel, logContext, format, args);
logger_.withContext(config, node, logLevel, logContext, format, args);
} else {
logger_.noContext(this, node, logLevel, format, args);
logger_.noContext(config, node, logLevel, format, args);
}
}
void Config::setCloneNodeCallback(YGCloneNodeFunc cloneNode) {
void YGConfig::setCloneNodeCallback(YGCloneNodeFunc cloneNode) {
cloneNodeCallback_.noContext = cloneNode;
flags_.cloneNodeUsesContext = false;
}
void Config::setCloneNodeCallback(CloneWithContextFn cloneNode) {
void YGConfig::setCloneNodeCallback(CloneWithContextFn cloneNode) {
cloneNodeCallback_.withContext = cloneNode;
flags_.cloneNodeUsesContext = true;
}
void Config::setCloneNodeCallback(std::nullptr_t) {
void YGConfig::setCloneNodeCallback(std::nullptr_t) {
setCloneNodeCallback(YGCloneNodeFunc{nullptr});
}
YGNodeRef Config::cloneNode(
YGNodeRef YGConfig::cloneNode(
YGNodeRef node,
YGNodeRef owner,
int childIndex,
@@ -143,5 +147,3 @@ YGNodeRef Config::cloneNode(
}
return clone;
}
} // namespace facebook::yoga

View File

@@ -12,16 +12,11 @@
#include <yoga/BitUtils.h>
#include <yoga/Yoga-internal.h>
// Tag struct used to form the opaque YGConfigRef for the public C API
struct YGConfig {};
namespace facebook::yoga {
class Config;
// Whether moving a node from config "a" to config "b" should dirty previously
// calculated layout results.
bool configUpdateInvalidatesLayout(Config* a, Config* b);
bool configUpdateInvalidatesLayout(YGConfigRef a, YGConfigRef b);
// Internal variants of log functions, currently used only by JNI bindings.
// TODO: Reconcile this with the public API
@@ -38,12 +33,13 @@ using CloneWithContextFn = YGNodeRef (*)(
int childIndex,
void* cloneContext);
using ExperimentalFeatureSet = detail::EnumBitset<YGExperimentalFeature>;
using ExperimentalFeatureSet =
facebook::yoga::detail::EnumBitset<YGExperimentalFeature>;
#pragma pack(push)
#pragma pack(1)
// Packed structure of <32-bit options to miminize size per node.
struct ConfigFlags {
struct YGConfigFlags {
bool useWebDefaults : 1;
bool printTree : 1;
bool cloneNodeUsesContext : 1;
@@ -51,9 +47,10 @@ struct ConfigFlags {
};
#pragma pack(pop)
class YOGA_EXPORT Config : public ::YGConfig {
public:
Config(YGLogger logger);
} // namespace facebook::yoga
struct YOGA_EXPORT YGConfig {
YGConfig(YGLogger logger);
void setUseWebDefaults(bool useWebDefaults);
bool useWebDefaults() const;
@@ -65,7 +62,7 @@ public:
YGExperimentalFeature feature,
bool enabled);
bool isExperimentalFeatureEnabled(YGExperimentalFeature feature) const;
ExperimentalFeatureSet getEnabledExperiments() const;
facebook::yoga::ExperimentalFeatureSet getEnabledExperiments() const;
void setErrata(YGErrata errata);
void addErrata(YGErrata errata);
@@ -80,12 +77,12 @@ public:
void* getContext() const;
void setLogger(YGLogger logger);
void setLogger(LogWithContextFn logger);
void setLogger(facebook::yoga::LogWithContextFn logger);
void setLogger(std::nullptr_t);
void log(YGNodeRef, YGLogLevel, void*, const char*, va_list);
void log(YGConfig*, YGNode*, YGLogLevel, void*, const char*, va_list) const;
void setCloneNodeCallback(YGCloneNodeFunc cloneNode);
void setCloneNodeCallback(CloneWithContextFn cloneNode);
void setCloneNodeCallback(facebook::yoga::CloneWithContextFn cloneNode);
void setCloneNodeCallback(std::nullptr_t);
YGNodeRef cloneNode(
YGNodeRef node,
@@ -95,19 +92,17 @@ public:
private:
union {
CloneWithContextFn withContext;
facebook::yoga::CloneWithContextFn withContext;
YGCloneNodeFunc noContext;
} cloneNodeCallback_;
union {
LogWithContextFn withContext;
facebook::yoga::LogWithContextFn withContext;
YGLogger noContext;
} logger_;
ConfigFlags flags_{};
ExperimentalFeatureSet experimentalFeatures_{};
facebook::yoga::YGConfigFlags flags_{};
facebook::yoga::ExperimentalFeatureSet experimentalFeatures_{};
YGErrata errata_ = YGErrataNone;
float pointScaleFactor_ = 1.0f;
void* context_ = nullptr;
};
} // namespace facebook::yoga

View File

@@ -14,7 +14,7 @@ using namespace facebook;
using namespace facebook::yoga;
using facebook::yoga::CompactValue;
YGNode::YGNode(yoga::Config* config) : config_{config} {
YGNode::YGNode(const YGConfigRef config) : config_{config} {
YGAssert(
config != nullptr, "Attempting to construct YGNode with null config");
@@ -264,7 +264,7 @@ void YGNode::insertChild(YGNodeRef child, uint32_t index) {
children_.insert(children_.begin() + index, child);
}
void YGNode::setConfig(yoga::Config* config) {
void YGNode::setConfig(YGConfigRef config) {
YGAssert(config != nullptr, "Attempting to set a null config on a YGNode");
YGAssertWithConfig(
config,

View File

@@ -9,13 +9,15 @@
#include <cstdint>
#include <stdio.h>
#include <yoga/config/Config.h>
#include "YGConfig.h"
#include "YGLayout.h"
#include <yoga/Yoga-internal.h>
#include <yoga/style/CompactValue.h>
#include <yoga/style/Style.h>
YGConfigRef YGConfigGetDefault();
#pragma pack(push)
#pragma pack(1)
struct YGNodeFlags {
@@ -56,7 +58,7 @@ private:
uint32_t lineIndex_ = 0;
YGNodeRef owner_ = nullptr;
YGVector children_ = {};
facebook::yoga::Config* config_;
YGConfigRef config_;
std::array<YGValue, 2> resolvedDimensions_ = {
{YGValueUndefined, YGValueUndefined}};
@@ -82,11 +84,8 @@ private:
using CompactValue = facebook::yoga::CompactValue;
public:
YGNode()
: YGNode{static_cast<facebook::yoga::Config*>(YGConfigGetDefault())} {
flags_.hasNewLayout = true;
}
explicit YGNode(facebook::yoga::Config* config);
YGNode() : YGNode{YGConfigGetDefault()} { flags_.hasNewLayout = true; }
explicit YGNode(const YGConfigRef config);
~YGNode() = default; // cleanup of owner/children relationships in YGNodeFree
YGNode(YGNode&&);
@@ -167,7 +166,7 @@ public:
YGNodeRef getChild(uint32_t index) const { return children_.at(index); }
facebook::yoga::Config* getConfig() const { return config_; }
YGConfigRef getConfig() const { return config_; }
bool isDirty() const { return flags_.isDirty; }
@@ -291,7 +290,7 @@ public:
// TODO: rvalue override for setChildren
void setConfig(facebook::yoga::Config* config);
void setConfig(YGConfigRef config);
void setDirty(bool isDirty);
void setLayoutLastOwnerDirection(YGDirection direction);

View File

@@ -20,7 +20,6 @@
#include <yoga/Yoga-internal.h>
#include "event/event.h"
using namespace facebook;
using namespace facebook::yoga;
using detail::Log;
@@ -120,7 +119,7 @@ YOGA_EXPORT YGConfigRef YGNodeGetConfig(YGNodeRef node) {
}
YOGA_EXPORT void YGNodeSetConfig(YGNodeRef node, YGConfigRef config) {
node->setConfig(static_cast<yoga::Config*>(config));
node->setConfig(config);
}
YOGA_EXPORT bool YGNodeHasMeasureFunc(YGNodeRef node) {
@@ -162,7 +161,7 @@ YOGA_EXPORT bool YGNodeGetHasNewLayout(YGNodeRef node) {
}
YOGA_EXPORT void YGConfigSetPrintTreeFlag(YGConfigRef config, bool enabled) {
static_cast<yoga::Config*>(config)->setShouldPrintTree(enabled);
config->setShouldPrintTree(enabled);
}
YOGA_EXPORT void YGNodeSetHasNewLayout(YGNodeRef node, bool hasNewLayout) {
@@ -189,7 +188,7 @@ YOGA_EXPORT void YGNodeMarkDirtyAndPropagateToDescendants(
int32_t gConfigInstanceCount = 0;
YOGA_EXPORT WIN_EXPORT YGNodeRef YGNodeNewWithConfig(const YGConfigRef config) {
const YGNodeRef node = new YGNode{static_cast<yoga::Config*>(config)};
const YGNodeRef node = new YGNode{config};
YGAssert(config != nullptr, "Tried to construct YGNode with null config");
YGAssertWithConfig(
config, node != nullptr, "Could not allocate memory for node");
@@ -273,19 +272,23 @@ YOGA_EXPORT int32_t YGConfigGetInstanceCount(void) {
YOGA_EXPORT YGConfigRef YGConfigNew(void) {
#ifdef ANDROID
const YGConfigRef config = new yoga::Config(YGAndroidLog);
const YGConfigRef config = new YGConfig(YGAndroidLog);
#else
const YGConfigRef config = new yoga::Config(YGDefaultLog);
const YGConfigRef config = new YGConfig(YGDefaultLog);
#endif
gConfigInstanceCount++;
return config;
}
YOGA_EXPORT void YGConfigFree(const YGConfigRef config) {
delete static_cast<yoga::Config*>(config);
delete config;
gConfigInstanceCount--;
}
void YGConfigCopy(const YGConfigRef dest, const YGConfigRef src) {
memcpy(dest, src, sizeof(YGConfig));
}
YOGA_EXPORT void YGNodeSetIsReferenceBaseline(
YGNodeRef node,
bool isReferenceBaseline) {
@@ -3712,22 +3715,22 @@ YOGA_EXPORT bool YGNodeCanUseCachedMeasurement(
return false;
}
bool useRoundedComparison =
config != nullptr && YGConfigGetPointScaleFactor(config) != 0;
config != nullptr && config->getPointScaleFactor() != 0;
const float effectiveWidth = useRoundedComparison
? YGRoundValueToPixelGrid(
width, YGConfigGetPointScaleFactor(config), false, false)
width, config->getPointScaleFactor(), false, false)
: width;
const float effectiveHeight = useRoundedComparison
? YGRoundValueToPixelGrid(
height, YGConfigGetPointScaleFactor(config), false, false)
height, config->getPointScaleFactor(), false, false)
: height;
const float effectiveLastWidth = useRoundedComparison
? YGRoundValueToPixelGrid(
lastWidth, YGConfigGetPointScaleFactor(config), false, false)
lastWidth, config->getPointScaleFactor(), false, false)
: lastWidth;
const float effectiveLastHeight = useRoundedComparison
? YGRoundValueToPixelGrid(
lastHeight, YGConfigGetPointScaleFactor(config), false, false)
lastHeight, config->getPointScaleFactor(), false, false)
: lastHeight;
const bool hasSameWidthSpec = lastWidthMode == widthMode &&
@@ -4054,14 +4057,14 @@ YOGA_EXPORT void YGConfigSetPointScaleFactor(
// We store points for Pixel as we will use it for rounding
if (pixelsInPoint == 0.0f) {
// Zero is used to skip rounding
static_cast<yoga::Config*>(config)->setPointScaleFactor(0.0f);
config->setPointScaleFactor(0.0f);
} else {
static_cast<yoga::Config*>(config)->setPointScaleFactor(pixelsInPoint);
config->setPointScaleFactor(pixelsInPoint);
}
}
YOGA_EXPORT float YGConfigGetPointScaleFactor(const YGConfigRef config) {
return static_cast<yoga::Config*>(config)->getPointScaleFactor();
return config->getPointScaleFactor();
}
static void YGRoundToPixelGrid(
@@ -4236,12 +4239,12 @@ YOGA_EXPORT void YGNodeCalculateLayout(
YOGA_EXPORT void YGConfigSetLogger(const YGConfigRef config, YGLogger logger) {
if (logger != nullptr) {
static_cast<yoga::Config*>(config)->setLogger(logger);
config->setLogger(logger);
} else {
#ifdef ANDROID
static_cast<yoga::Config*>(config)->setLogger(&YGAndroidLog);
config->setLogger(&YGAndroidLog);
#else
static_cast<yoga::Config*>(config)->setLogger(&YGDefaultLog);
config->setLogger(&YGDefaultLog);
#endif
}
}
@@ -4268,12 +4271,7 @@ void YGAssertWithConfig(
const bool condition,
const char* message) {
if (!condition) {
Log::log(
static_cast<yoga::Config*>(config),
YGLogLevelFatal,
nullptr,
"%s\n",
message);
Log::log(config, YGLogLevelFatal, nullptr, "%s\n", message);
throwLogicalErrorWithMessage(message);
}
}
@@ -4282,61 +4280,58 @@ YOGA_EXPORT void YGConfigSetExperimentalFeatureEnabled(
const YGConfigRef config,
const YGExperimentalFeature feature,
const bool enabled) {
static_cast<yoga::Config*>(config)->setExperimentalFeatureEnabled(
feature, enabled);
config->setExperimentalFeatureEnabled(feature, enabled);
}
YOGA_EXPORT bool YGConfigIsExperimentalFeatureEnabled(
const YGConfigRef config,
const YGExperimentalFeature feature) {
return static_cast<yoga::Config*>(config)->isExperimentalFeatureEnabled(
feature);
return config->isExperimentalFeatureEnabled(feature);
}
YOGA_EXPORT void YGConfigSetUseWebDefaults(
const YGConfigRef config,
const bool enabled) {
static_cast<yoga::Config*>(config)->setUseWebDefaults(enabled);
config->setUseWebDefaults(enabled);
}
YOGA_EXPORT bool YGConfigGetUseLegacyStretchBehaviour(
const YGConfigRef config) {
return static_cast<yoga::Config*>(config)->hasErrata(
YGErrataStretchFlexBasis);
return config->hasErrata(YGErrataStretchFlexBasis);
}
YOGA_EXPORT void YGConfigSetUseLegacyStretchBehaviour(
const YGConfigRef config,
const bool useLegacyStretchBehaviour) {
if (useLegacyStretchBehaviour) {
static_cast<yoga::Config*>(config)->addErrata(YGErrataStretchFlexBasis);
config->addErrata(YGErrataStretchFlexBasis);
} else {
static_cast<yoga::Config*>(config)->removeErrata(YGErrataStretchFlexBasis);
config->removeErrata(YGErrataStretchFlexBasis);
}
}
bool YGConfigGetUseWebDefaults(const YGConfigRef config) {
return static_cast<yoga::Config*>(config)->useWebDefaults();
return config->useWebDefaults();
}
YOGA_EXPORT void YGConfigSetContext(const YGConfigRef config, void* context) {
static_cast<yoga::Config*>(config)->setContext(context);
config->setContext(context);
}
YOGA_EXPORT void* YGConfigGetContext(const YGConfigRef config) {
return static_cast<yoga::Config*>(config)->getContext();
return config->getContext();
}
YOGA_EXPORT void YGConfigSetErrata(YGConfigRef config, YGErrata errata) {
static_cast<yoga::Config*>(config)->setErrata(errata);
config->setErrata(errata);
}
YOGA_EXPORT YGErrata YGConfigGetErrata(YGConfigRef config) {
return static_cast<yoga::Config*>(config)->getErrata();
return config->getErrata();
}
YOGA_EXPORT void YGConfigSetCloneNodeFunc(
const YGConfigRef config,
const YGCloneNodeFunc callback) {
static_cast<yoga::Config*>(config)->setCloneNodeCallback(callback);
config->setCloneNodeCallback(callback);
}

View File

@@ -343,6 +343,7 @@ void YGConfigSetUseLegacyStretchBehaviour(
// YGConfig
WIN_EXPORT YGConfigRef YGConfigNew(void);
WIN_EXPORT void YGConfigFree(YGConfigRef config);
WIN_EXPORT void YGConfigCopy(YGConfigRef dest, YGConfigRef src);
WIN_EXPORT int32_t YGConfigGetInstanceCount(void);
WIN_EXPORT void YGConfigSetExperimentalFeatureEnabled(

View File

@@ -8,7 +8,7 @@
#include <yoga/Yoga.h>
#include "log.h"
#include <yoga/config/Config.h>
#include "YGConfig.h"
#include "YGNode.h"
namespace facebook::yoga::detail {
@@ -16,16 +16,14 @@ namespace facebook::yoga::detail {
namespace {
void vlog(
yoga::Config* config,
YGConfig* config,
YGNode* node,
YGLogLevel level,
void* context,
const char* format,
va_list args) {
yoga::Config* logConfig = config != nullptr
? config
: static_cast<yoga::Config*>(YGConfigGetDefault());
logConfig->log(node, level, context, format, args);
YGConfig* logConfig = config != nullptr ? config : YGConfigGetDefault();
logConfig->log(logConfig, node, level, context, format, args);
}
} // namespace
@@ -48,7 +46,7 @@ YOGA_EXPORT void Log::log(
}
void Log::log(
yoga::Config* config,
YGConfig* config,
YGLogLevel level,
void* context,
const char* format,

View File

@@ -8,7 +8,6 @@
#pragma once
#include <yoga/YGEnums.h>
#include <yoga/config/Config.h>
struct YGNode;
struct YGConfig;
@@ -24,7 +23,7 @@ struct Log {
...) noexcept;
static void log(
yoga::Config* config,
YGConfig* config,
YGLogLevel level,
void*,
const char* format,