Use Bitfield in YGNode and YGStyle

Summary:
@public

Replaces the usage of C++ bitfields with our portable `Bitfield` class.

Reviewed By: SidharthGuglani

Differential Revision: D16649875

fbshipit-source-id: 539f016d5e1c9a8c48cc9bacbbf6ed985e385e69
This commit is contained in:
David Aurelio
2019-08-07 16:17:00 -07:00
committed by Facebook Github Bot
parent 3ed9bec05c
commit 884e064a99
4 changed files with 125 additions and 159 deletions

View File

@@ -7,6 +7,7 @@
#pragma once
#include <cstdint>
#include <stdio.h>
#include "Bitfield.h"
#include "CompactValue.h"
#include "YGConfig.h"
#include "YGLayout.h"
@@ -23,15 +24,20 @@ struct YGNode {
using PrintWithContextFn = void (*)(YGNode*, void*);
private:
static constexpr size_t hasNewLayout_ = 0;
static constexpr size_t isReferenceBaseline_ = 1;
static constexpr size_t isDirty_ = 2;
static constexpr size_t nodeType_ = 3;
static constexpr size_t measureUsesContext_ = 4;
static constexpr size_t baselineUsesContext_ = 5;
static constexpr size_t printUsesContext_ = 6;
static constexpr size_t useWebDefaults_ = 7;
void* context_ = nullptr;
bool hasNewLayout_ : 1;
bool isReferenceBaseline_ : 1;
bool isDirty_ : 1;
YGNodeType nodeType_ : 1;
bool measureUsesContext_ : 1;
bool baselineUsesContext_ : 1;
bool printUsesContext_ : 1;
bool useWebDefaults_ : 1;
using Flags = facebook::yoga::
Bitfield<uint8_t, bool, bool, bool, YGNodeType, bool, bool, bool, bool>;
Flags flags_ =
{true, false, false, YGNodeTypeDefault, false, false, false, false};
uint8_t reserved_ = 0;
union {
YGMeasureFunc noContext;
@@ -63,7 +69,7 @@ private:
void setBaselineFunc(decltype(baseline_));
void useWebDefaults() {
useWebDefaults_ = true;
flags_.at<useWebDefaults_>() = true;
style_.flexDirection() = YGFlexDirectionRow;
style_.alignContent() = YGAlignStretch;
}
@@ -79,17 +85,8 @@ private:
public:
YGNode() : YGNode{YGConfigGetDefault()} {}
explicit YGNode(const YGConfigRef config)
: hasNewLayout_{true},
isReferenceBaseline_{false},
isDirty_{false},
nodeType_{YGNodeTypeDefault},
measureUsesContext_{false},
baselineUsesContext_{false},
printUsesContext_{false},
useWebDefaults_{config->useWebDefaults},
config_{config} {
if (useWebDefaults_) {
explicit YGNode(const YGConfigRef config) : config_{config} {
if (config->useWebDefaults) {
useWebDefaults();
}
};
@@ -116,9 +113,9 @@ public:
void print(void*);
bool getHasNewLayout() const { return hasNewLayout_; }
bool getHasNewLayout() const { return flags_.at<hasNewLayout_>(); }
YGNodeType getNodeType() const { return nodeType_; }
YGNodeType getNodeType() const { return flags_.at<nodeType_>(); }
bool hasMeasureFunc() const noexcept { return measure_.noContext != nullptr; }
@@ -144,7 +141,7 @@ public:
uint32_t getLineIndex() const { return lineIndex_; }
bool isReferenceBaseline() { return isReferenceBaseline_; }
bool isReferenceBaseline() { return flags_.at<isReferenceBaseline_>(); }
// returns the YGNodeRef that owns this YGNode. An owner is used to identify
// the YogaTree that a YGNode belongs to. This method will return the parent
@@ -177,7 +174,7 @@ public:
YGConfigRef getConfig() const { return config_; }
bool isDirty() const { return isDirty_; }
bool isDirty() const { return flags_.at<isDirty_>(); }
std::array<YGValue, 2> getResolvedDimensions() const {
return resolvedDimensions_;
@@ -225,17 +222,19 @@ public:
void setPrintFunc(YGPrintFunc printFunc) {
print_.noContext = printFunc;
printUsesContext_ = false;
flags_.at<printUsesContext_>() = false;
}
void setPrintFunc(PrintWithContextFn printFunc) {
print_.withContext = printFunc;
printUsesContext_ = true;
flags_.at<printUsesContext_>() = true;
}
void setPrintFunc(std::nullptr_t) { setPrintFunc(YGPrintFunc{nullptr}); }
void setHasNewLayout(bool hasNewLayout) { hasNewLayout_ = hasNewLayout; }
void setHasNewLayout(bool hasNewLayout) {
flags_.at<hasNewLayout_>() = hasNewLayout;
}
void setNodeType(YGNodeType nodeType) { nodeType_ = nodeType; }
void setNodeType(YGNodeType nodeType) { flags_.at<nodeType_>() = nodeType; }
void setMeasureFunc(YGMeasureFunc measureFunc);
void setMeasureFunc(MeasureWithContextFn);
@@ -244,11 +243,11 @@ public:
}
void setBaselineFunc(YGBaselineFunc baseLineFunc) {
baselineUsesContext_ = false;
flags_.at<baselineUsesContext_>() = false;
baseline_.noContext = baseLineFunc;
}
void setBaselineFunc(BaselineWithContextFn baseLineFunc) {
baselineUsesContext_ = true;
flags_.at<baselineUsesContext_>() = true;
baseline_.withContext = baseLineFunc;
}
void setBaselineFunc(std::nullptr_t) {
@@ -264,7 +263,7 @@ public:
void setLineIndex(uint32_t lineIndex) { lineIndex_ = lineIndex; }
void setIsReferenceBaseline(bool isReferenceBaseline) {
isReferenceBaseline_ = isReferenceBaseline;
flags_.at<isReferenceBaseline_>() = isReferenceBaseline;
}
void setOwner(YGNodeRef owner) { owner_ = owner; }