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

@@ -11,6 +11,8 @@
#include <stdio.h>
#include <vector>
#include <yoga/Yoga.h>
#include <yoga/config/Config.h>
#include <yoga/node/LayoutResults.h>
#include <yoga/style/CompactValue.h>
@@ -139,7 +141,7 @@ public:
uint32_t getLineIndex() const { return lineIndex_; }
bool isReferenceBaseline() { return flags_.isReferenceBaseline; }
bool isReferenceBaseline() const { return flags_.isReferenceBaseline; }
// returns the Node that owns this Node. An owner is used to identify
// the YogaTree that a Node belongs to. This method will return the parent
@@ -152,23 +154,6 @@ public:
const std::vector<Node*>& getChildren() const { return children_; }
// Applies a callback to all children, after cloning them if they are not
// owned.
template <typename T>
void iterChildrenAfterCloningIfNeeded(T callback, void* cloneContext) {
int i = 0;
for (Node*& child : children_) {
if (child->getOwner() != this) {
child = static_cast<Node*>(
config_->cloneNode(child, this, i, cloneContext));
child->setOwner(this);
}
i += 1;
callback(child, cloneContext);
}
}
Node* getChild(size_t index) const { return children_.at(index); }
size_t getChildCount() const { return children_.size(); }
@@ -343,4 +328,12 @@ public:
void reset();
};
inline Node* resolveRef(const YGNodeRef ref) {
return static_cast<Node*>(ref);
}
inline const Node* resolveRef(const YGNodeConstRef ref) {
return static_cast<const Node*>(ref);
}
} // namespace facebook::yoga