From 13c5ce22345863053969013127b115a2c019d6d2 Mon Sep 17 00:00:00 2001 From: Zhiyao Zhou Date: Tue, 29 Aug 2023 23:27:25 -0700 Subject: [PATCH] Revert D48710796: C++ Cleanup 2/N: Reorganize YGConfig Differential Revision: D48710796 Original commit changeset: d548553f7ce8 Original Phabricator Diff: D48710796 fbshipit-source-id: c8b2de245f3894f6a87c262ec70d313020aa228e --- java/jni/YGJNIVanilla.cpp | 4 +- tests/generated/YGConfigTest.cpp | 8 +-- yoga/{config/Config.cpp => YGConfig.cpp} | 70 ++++++++++++----------- yoga/{config/Config.h => YGConfig.h} | 37 ++++++------ yoga/YGNode.cpp | 4 +- yoga/YGNode.h | 17 +++--- yoga/Yoga.cpp | 73 +++++++++++------------- yoga/Yoga.h | 1 + yoga/log.cpp | 12 ++-- yoga/log.h | 3 +- 10 files changed, 107 insertions(+), 122 deletions(-) rename yoga/{config/Config.cpp => YGConfig.cpp} (56%) rename yoga/{config/Config.h => YGConfig.h} (75%) diff --git a/java/jni/YGJNIVanilla.cpp b/java/jni/YGJNIVanilla.cpp index 5d802784..bc108b5d 100644 --- a/java/jni/YGJNIVanilla.cpp +++ b/java/jni/YGJNIVanilla.cpp @@ -19,8 +19,6 @@ // API and use that #include -using namespace facebook; -using namespace facebook::yoga; using namespace facebook::yoga::vanillajni; static inline ScopedLocalRef YGNodeJobject( @@ -196,7 +194,7 @@ static void jni_YGConfigSetLoggerJNI( } *context = newGlobalRef(env, logger); - static_cast(config)->setLogger(YGJNILogFunc); + config->setLogger(YGJNILogFunc); } else { if (context != nullptr) { delete context; diff --git a/tests/generated/YGConfigTest.cpp b/tests/generated/YGConfigTest.cpp index 7c0f61a3..b13a75b7 100644 --- a/tests/generated/YGConfigTest.cpp +++ b/tests/generated/YGConfigTest.cpp @@ -9,16 +9,14 @@ #include #include -#include +#include #include #include #include -using namespace facebook; - struct ConfigCloningTest : public ::testing::Test { - std::unique_ptr> config; + std::unique_ptr> config; void SetUp() override; void TearDown() override; @@ -58,7 +56,7 @@ TEST_F(ConfigCloningTest, can_clone_with_context) { } void ConfigCloningTest::SetUp() { - config = {static_cast(YGConfigNew()), YGConfigFree}; + config = {YGConfigNew(), YGConfigFree}; } void ConfigCloningTest::TearDown() { diff --git a/yoga/config/Config.cpp b/yoga/YGConfig.cpp similarity index 56% rename from yoga/config/Config.cpp rename to yoga/YGConfig.cpp index 2f07454b..93c16c69 100644 --- a/yoga/config/Config.cpp +++ b/yoga/YGConfig.cpp @@ -5,129 +5,133 @@ * LICENSE file in the root directory of this source tree. */ -#include +#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 diff --git a/yoga/config/Config.h b/yoga/YGConfig.h similarity index 75% rename from yoga/config/Config.h rename to yoga/YGConfig.h index 6470ebde..fa530fab 100644 --- a/yoga/config/Config.h +++ b/yoga/YGConfig.h @@ -12,16 +12,11 @@ #include #include -// 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; +using ExperimentalFeatureSet = + facebook::yoga::detail::EnumBitset; #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 diff --git a/yoga/YGNode.cpp b/yoga/YGNode.cpp index 7389354e..db537576 100644 --- a/yoga/YGNode.cpp +++ b/yoga/YGNode.cpp @@ -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, diff --git a/yoga/YGNode.h b/yoga/YGNode.h index 218cb989..b46ce4aa 100644 --- a/yoga/YGNode.h +++ b/yoga/YGNode.h @@ -9,13 +9,15 @@ #include #include -#include +#include "YGConfig.h" #include "YGLayout.h" #include #include #include +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 resolvedDimensions_ = { {YGValueUndefined, YGValueUndefined}}; @@ -82,11 +84,8 @@ private: using CompactValue = facebook::yoga::CompactValue; public: - YGNode() - : YGNode{static_cast(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); diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index b52ded0e..e8756ddf 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -20,7 +20,6 @@ #include #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(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(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(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(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(config)->setPointScaleFactor(0.0f); + config->setPointScaleFactor(0.0f); } else { - static_cast(config)->setPointScaleFactor(pixelsInPoint); + config->setPointScaleFactor(pixelsInPoint); } } YOGA_EXPORT float YGConfigGetPointScaleFactor(const YGConfigRef config) { - return static_cast(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(config)->setLogger(logger); + config->setLogger(logger); } else { #ifdef ANDROID - static_cast(config)->setLogger(&YGAndroidLog); + config->setLogger(&YGAndroidLog); #else - static_cast(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(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(config)->setExperimentalFeatureEnabled( - feature, enabled); + config->setExperimentalFeatureEnabled(feature, enabled); } YOGA_EXPORT bool YGConfigIsExperimentalFeatureEnabled( const YGConfigRef config, const YGExperimentalFeature feature) { - return static_cast(config)->isExperimentalFeatureEnabled( - feature); + return config->isExperimentalFeatureEnabled(feature); } YOGA_EXPORT void YGConfigSetUseWebDefaults( const YGConfigRef config, const bool enabled) { - static_cast(config)->setUseWebDefaults(enabled); + config->setUseWebDefaults(enabled); } YOGA_EXPORT bool YGConfigGetUseLegacyStretchBehaviour( const YGConfigRef config) { - return static_cast(config)->hasErrata( - YGErrataStretchFlexBasis); + return config->hasErrata(YGErrataStretchFlexBasis); } YOGA_EXPORT void YGConfigSetUseLegacyStretchBehaviour( const YGConfigRef config, const bool useLegacyStretchBehaviour) { if (useLegacyStretchBehaviour) { - static_cast(config)->addErrata(YGErrataStretchFlexBasis); + config->addErrata(YGErrataStretchFlexBasis); } else { - static_cast(config)->removeErrata(YGErrataStretchFlexBasis); + config->removeErrata(YGErrataStretchFlexBasis); } } bool YGConfigGetUseWebDefaults(const YGConfigRef config) { - return static_cast(config)->useWebDefaults(); + return config->useWebDefaults(); } YOGA_EXPORT void YGConfigSetContext(const YGConfigRef config, void* context) { - static_cast(config)->setContext(context); + config->setContext(context); } YOGA_EXPORT void* YGConfigGetContext(const YGConfigRef config) { - return static_cast(config)->getContext(); + return config->getContext(); } YOGA_EXPORT void YGConfigSetErrata(YGConfigRef config, YGErrata errata) { - static_cast(config)->setErrata(errata); + config->setErrata(errata); } YOGA_EXPORT YGErrata YGConfigGetErrata(YGConfigRef config) { - return static_cast(config)->getErrata(); + return config->getErrata(); } YOGA_EXPORT void YGConfigSetCloneNodeFunc( const YGConfigRef config, const YGCloneNodeFunc callback) { - static_cast(config)->setCloneNodeCallback(callback); + config->setCloneNodeCallback(callback); } diff --git a/yoga/Yoga.h b/yoga/Yoga.h index e8bf0aff..de07aeac 100644 --- a/yoga/Yoga.h +++ b/yoga/Yoga.h @@ -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( diff --git a/yoga/log.cpp b/yoga/log.cpp index eb776629..51040592 100644 --- a/yoga/log.cpp +++ b/yoga/log.cpp @@ -8,7 +8,7 @@ #include #include "log.h" -#include +#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(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, diff --git a/yoga/log.h b/yoga/log.h index ad0fe4d6..070a164d 100644 --- a/yoga/log.h +++ b/yoga/log.h @@ -8,7 +8,6 @@ #pragma once #include -#include 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,