Non-breaking const-correctness fixes (#1365)

Summary:
Pull Request resolved: https://github.com/facebook/yoga/pull/1365

X-link: https://github.com/facebook/react-native/pull/39368

This changes public Yoga API to in more places accept const structures where before they required mutable ones.

`resolveRef` is added as a quick way to resolve overloaded opaque refs for different types which is a bit easier to read than static_casting, and which will propagate const-ness. We also add `YGConfigConstRef`, similar to `YGNodeConstRef`. I was a bit iffy on whether we should add something to make it easier to convert to private interface,  but this doesn't seem any easier to misuse than someone who looks at the internals to find the `static_cast`.

This tries to avoid more breaking changes yet, e.g. changing callbacks to require clients do not modify nodes when they are passed for logging. We also don't have const variants for returning child structures which would allow mutation of dependencies of the const object. These would need new names under the public API, since we do not have operator overloading in C.

Reviewed By: rshest

Differential Revision: D49130412

fbshipit-source-id: ee6b31b47f4622031c63dd52d8ac133d21bf29b7
This commit is contained in:
Nick Gerleman
2023-09-11 19:51:40 -07:00
committed by Facebook GitHub Bot
parent 3cb29e60a8
commit b12a6a340c
13 changed files with 230 additions and 228 deletions

View File

@@ -22,43 +22,27 @@ namespace facebook::yoga {
void assertFatal(const bool condition, const char* message) {
if (!condition) {
yoga::log(
static_cast<yoga::Node*>(nullptr),
YGLogLevelFatal,
nullptr,
"%s\n",
message);
yoga::log(YGLogLevelFatal, nullptr, "%s\n", message);
fatalWithMessage(message);
}
}
void assertFatalWithNode(
const YGNodeConstRef node,
const yoga::Node* const node,
const bool condition,
const char* message) {
if (!condition) {
yoga::log(
// TODO: Break log callbacks and make them const correct
static_cast<yoga::Node*>(const_cast<YGNodeRef>(node)),
YGLogLevelFatal,
nullptr,
"%s\n",
message);
yoga::log(node, YGLogLevelFatal, nullptr, "%s\n", message);
fatalWithMessage(message);
}
}
void assertFatalWithConfig(
YGConfigRef config,
const yoga::Config* const config,
const bool condition,
const char* message) {
if (!condition) {
yoga::log(
static_cast<yoga::Config*>(config),
YGLogLevelFatal,
nullptr,
"%s\n",
message);
yoga::log(config, YGLogLevelFatal, nullptr, "%s\n", message);
fatalWithMessage(message);
}
}

View File

@@ -17,11 +17,11 @@ namespace facebook::yoga {
void assertFatal(bool condition, const char* message);
void assertFatalWithNode(
YGNodeConstRef node,
const yoga::Node* node,
bool condition,
const char* message);
void assertFatalWithConfig(
YGConfigRef config,
const yoga::Config* config,
bool condition,
const char* message);

View File

@@ -12,21 +12,28 @@ namespace facebook::yoga {
namespace {
void vlog(
yoga::Config* config,
yoga::Node* node,
const yoga::Config* config,
const yoga::Node* 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);
const yoga::Config* logConfig =
config != nullptr ? config : resolveRef(YGConfigGetDefault());
logConfig->log(const_cast<yoga::Node*>(node), level, context, format, args);
}
} // namespace
void log(YGLogLevel level, void* context, const char* format, ...) noexcept {
va_list args;
va_start(args, format);
vlog(nullptr, nullptr, level, context, format, args);
va_end(args);
}
void log(
yoga::Node* node,
const yoga::Node* node,
YGLogLevel level,
void* context,
const char* format,
@@ -44,7 +51,7 @@ void log(
}
void log(
yoga::Config* config,
const yoga::Config* config,
YGLogLevel level,
void* context,
const char* format,

View File

@@ -13,15 +13,17 @@
namespace facebook::yoga {
void log(YGLogLevel level, void*, const char* format, ...) noexcept;
void log(
yoga::Node* node,
const yoga::Node* node,
YGLogLevel level,
void*,
const char* message,
...) noexcept;
void log(
yoga::Config* config,
const yoga::Config* config,
YGLogLevel level,
void*,
const char* format,

View File

@@ -117,7 +117,7 @@ static void appendEdgeIfNotUndefined(
void nodeToString(
std::string& str,
yoga::Node* node,
const yoga::Node* node,
YGPrintOptions options,
uint32_t level) {
indent(str, level);

View File

@@ -18,7 +18,7 @@ namespace facebook::yoga {
void nodeToString(
std::string& str,
yoga::Node* node,
const yoga::Node* node,
YGPrintOptions options,
uint32_t level);