Remove NumericBitfield (#1463)
Summary: X-link: https://github.com/facebook/react-native/pull/41394 Pull Request resolved: https://github.com/facebook/yoga/pull/1463 Now that are enums are unsigned, and we don't have BitfieldRef, we can convert the last remaining user of NumericBitfield to a plain old bitfield, for better readability (e.g. the default values), debugability, and less complexity. We also break a cycle which lets us properly group public vs private members. Reviewed By: joevilches Differential Revision: D51159415 fbshipit-source-id: 7842a8330eed6061b863de3f175c761dcf4aa2be
This commit is contained in:
committed by
Facebook GitHub Bot
parent
9b0fd09ec6
commit
bb8fd593ff
3
enums.py
3
enums.py
@@ -131,10 +131,7 @@ with open(root + "/yoga/YGEnums.h", "w") as f:
|
|||||||
f.write("YG_EXTERN_C_BEGIN\n\n")
|
f.write("YG_EXTERN_C_BEGIN\n\n")
|
||||||
items = sorted(ENUMS.items())
|
items = sorted(ENUMS.items())
|
||||||
for name, values in items:
|
for name, values in items:
|
||||||
if isinstance(values[0], tuple):
|
|
||||||
f.write("YG_ENUM_DECL(\n")
|
f.write("YG_ENUM_DECL(\n")
|
||||||
else:
|
|
||||||
f.write("YG_ENUM_SEQ_DECL(\n")
|
|
||||||
|
|
||||||
f.write(" YG%s,\n" % name)
|
f.write(" YG%s,\n" % name)
|
||||||
for value in values:
|
for value in values:
|
||||||
|
@@ -1,205 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
|
||||||
|
|
||||||
#include <yoga/bits/NumericBitfield.h>
|
|
||||||
#include <cstdint>
|
|
||||||
|
|
||||||
namespace facebook::yoga {
|
|
||||||
|
|
||||||
TEST(NumericBitfield, one_boolean_defaults_to_false) {
|
|
||||||
constexpr uint32_t flags = 0;
|
|
||||||
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 0), false);
|
|
||||||
static_assert(
|
|
||||||
getBooleanData(flags, 0) == false,
|
|
||||||
"first boolean member must default to false");
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(NumericBitfield, one_boolean_can_be_initialized_to_true) {
|
|
||||||
constexpr uint32_t flags = 1;
|
|
||||||
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 0), true);
|
|
||||||
static_assert(
|
|
||||||
getBooleanData(flags, 0) == true,
|
|
||||||
"first boolean member must be initialized to true");
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(NumericBitfield, one_boolean_can_be_set_to_true) {
|
|
||||||
uint32_t flags = 0;
|
|
||||||
|
|
||||||
setBooleanData(flags, 0, true);
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 0), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(NumericBitfield, second_boolean_defaults_to_false) {
|
|
||||||
constexpr uint32_t flags = 0;
|
|
||||||
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 1), false);
|
|
||||||
static_assert(
|
|
||||||
getBooleanData(flags, 1) == false,
|
|
||||||
"second boolean member must default to false");
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(NumericBitfield, second_boolean_can_be_initialized_to_true) {
|
|
||||||
constexpr uint32_t flags = 2;
|
|
||||||
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 0), false);
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 1), true);
|
|
||||||
static_assert(
|
|
||||||
getBooleanData(flags, 0) == false,
|
|
||||||
"first boolean member must default to false");
|
|
||||||
static_assert(
|
|
||||||
getBooleanData(flags, 1) == true,
|
|
||||||
"second boolean member must be initialized to true");
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(NumericBitfield, second_boolean_can_be_set_to_true) {
|
|
||||||
uint32_t flags = 0;
|
|
||||||
|
|
||||||
setBooleanData(flags, 1, true);
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 0), false);
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 1), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(NumericBitfield, third_boolean_defaults_to_false) {
|
|
||||||
constexpr uint32_t flags = 0;
|
|
||||||
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 2), false);
|
|
||||||
static_assert(
|
|
||||||
getBooleanData(flags, 2) == false,
|
|
||||||
"second boolean member must default to false");
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(NumericBitfield, third_boolean_can_be_initialized_to_true) {
|
|
||||||
constexpr uint32_t flags = 4;
|
|
||||||
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 0), false);
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 1), false);
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 2), true);
|
|
||||||
static_assert(
|
|
||||||
getBooleanData(flags, 0) == false,
|
|
||||||
"first boolean member must default to false");
|
|
||||||
static_assert(
|
|
||||||
getBooleanData(flags, 1) == false,
|
|
||||||
"second boolean member must default to false");
|
|
||||||
static_assert(
|
|
||||||
getBooleanData(flags, 2) == true,
|
|
||||||
"second boolean member must be initialized to true");
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(NumericBitfield, third_boolean_can_be_set_to_true) {
|
|
||||||
uint32_t flags = 0;
|
|
||||||
|
|
||||||
setBooleanData(flags, 2, true);
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 0), false);
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 1), false);
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 2), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(NumericBitfield, setting_boolean_values_does_not_spill_over) {
|
|
||||||
uint32_t flags = 0;
|
|
||||||
|
|
||||||
setBooleanData(flags, 1, (bool)7);
|
|
||||||
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 0), false);
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 1), true);
|
|
||||||
ASSERT_EQ(getBooleanData(flags, 2), false);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(NumericBitfield, first_enum_defaults_to_0) {
|
|
||||||
constexpr uint32_t flags = 0;
|
|
||||||
|
|
||||||
ASSERT_EQ(getEnumData<YGAlign>(flags, 0), YGAlignAuto);
|
|
||||||
static_assert(
|
|
||||||
getEnumData<YGAlign>(flags, 0) == YGAlignAuto,
|
|
||||||
"first enum member must default to 0");
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(NumericBitfield, first_enum_can_be_set) {
|
|
||||||
uint32_t flags = 0;
|
|
||||||
|
|
||||||
setEnumData<YGAlign>(flags, 0, YGAlignSpaceBetween);
|
|
||||||
|
|
||||||
ASSERT_EQ(getEnumData<YGAlign>(flags, 0), YGAlignSpaceBetween);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(NumericBitfield, second_enum_defaults_to_0) {
|
|
||||||
constexpr uint32_t flags = 0;
|
|
||||||
static constexpr size_t alignOffset = 0;
|
|
||||||
static constexpr size_t edgeOffset = 3;
|
|
||||||
|
|
||||||
ASSERT_EQ(getEnumData<YGAlign>(flags, alignOffset), YGAlignAuto);
|
|
||||||
ASSERT_EQ(getEnumData<YGEdge>(flags, edgeOffset), YGEdgeLeft);
|
|
||||||
static_assert(
|
|
||||||
getEnumData<YGAlign>(flags, alignOffset) == YGAlignAuto,
|
|
||||||
"first enum member must default to 0");
|
|
||||||
static_assert(
|
|
||||||
getEnumData<YGEdge>(flags, edgeOffset) == YGEdgeLeft,
|
|
||||||
"second enum member must default to 0");
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(NumericBitfield, second_enum_can_be_set) {
|
|
||||||
uint32_t flags = 0;
|
|
||||||
static constexpr size_t alignOffset = 0;
|
|
||||||
static constexpr size_t edgeOffset = 3;
|
|
||||||
|
|
||||||
setEnumData<YGEdge>(flags, edgeOffset, YGEdgeAll);
|
|
||||||
|
|
||||||
ASSERT_EQ(getEnumData<YGAlign>(flags, alignOffset), YGAlignAuto);
|
|
||||||
ASSERT_EQ(getEnumData<YGEdge>(flags, edgeOffset), YGEdgeAll);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(NumericBitfield, third_enum_defaults_to_0) {
|
|
||||||
constexpr uint32_t flags = 0;
|
|
||||||
static constexpr size_t alignOffset = 0;
|
|
||||||
static constexpr size_t boolOffset = 3;
|
|
||||||
static constexpr size_t edgesOffset = 4;
|
|
||||||
|
|
||||||
ASSERT_EQ(getEnumData<YGAlign>(flags, alignOffset), YGAlignAuto);
|
|
||||||
ASSERT_EQ(getBooleanData(flags, boolOffset), false);
|
|
||||||
ASSERT_EQ(getEnumData<YGEdge>(flags, edgesOffset), YGEdgeLeft);
|
|
||||||
static_assert(
|
|
||||||
getEnumData<YGAlign>(flags, alignOffset) == YGAlignAuto,
|
|
||||||
"first enum member must default to 0");
|
|
||||||
static_assert(
|
|
||||||
getBooleanData(flags, boolOffset) == false,
|
|
||||||
"middle boolean member must default to false");
|
|
||||||
static_assert(
|
|
||||||
getEnumData<YGEdge>(flags, edgesOffset) == YGEdgeLeft,
|
|
||||||
"last enum member must default to 0");
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(NumericBitfield, third_enum_can_be_set) {
|
|
||||||
uint32_t flags = 0;
|
|
||||||
static constexpr size_t alignOffset = 0;
|
|
||||||
static constexpr size_t boolOffset = 3;
|
|
||||||
static constexpr size_t edgesOffset = 4;
|
|
||||||
|
|
||||||
setEnumData<YGEdge>(flags, edgesOffset, YGEdgeVertical);
|
|
||||||
|
|
||||||
ASSERT_EQ(getEnumData<YGAlign>(flags, alignOffset), YGAlignAuto);
|
|
||||||
ASSERT_EQ(getBooleanData(flags, boolOffset), false);
|
|
||||||
ASSERT_EQ(getEnumData<YGEdge>(flags, edgesOffset), YGEdgeVertical);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(NumericBitfield, setting_values_does_not_spill_over) {
|
|
||||||
uint32_t flags = 0;
|
|
||||||
static constexpr size_t alignOffset = 0;
|
|
||||||
static constexpr size_t edgesOffset = 4;
|
|
||||||
static constexpr size_t boolOffset = 8;
|
|
||||||
|
|
||||||
uint32_t edge = 0xffffff;
|
|
||||||
setEnumData<YGEdge>(flags, edgesOffset, (YGEdge)edge);
|
|
||||||
|
|
||||||
ASSERT_EQ(getEnumData<YGAlign>(flags, alignOffset), 0);
|
|
||||||
ASSERT_EQ(getBooleanData(flags, boolOffset), false);
|
|
||||||
ASSERT_EQ(getEnumData<YGEdge>(flags, edgesOffset), 0xf);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace facebook::yoga
|
|
@@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
YG_EXTERN_C_BEGIN
|
YG_EXTERN_C_BEGIN
|
||||||
|
|
||||||
YG_ENUM_SEQ_DECL(
|
YG_ENUM_DECL(
|
||||||
YGAlign,
|
YGAlign,
|
||||||
YGAlignAuto,
|
YGAlignAuto,
|
||||||
YGAlignFlexStart,
|
YGAlignFlexStart,
|
||||||
@@ -24,23 +24,23 @@ YG_ENUM_SEQ_DECL(
|
|||||||
YGAlignSpaceAround,
|
YGAlignSpaceAround,
|
||||||
YGAlignSpaceEvenly)
|
YGAlignSpaceEvenly)
|
||||||
|
|
||||||
YG_ENUM_SEQ_DECL(
|
YG_ENUM_DECL(
|
||||||
YGDimension,
|
YGDimension,
|
||||||
YGDimensionWidth,
|
YGDimensionWidth,
|
||||||
YGDimensionHeight)
|
YGDimensionHeight)
|
||||||
|
|
||||||
YG_ENUM_SEQ_DECL(
|
YG_ENUM_DECL(
|
||||||
YGDirection,
|
YGDirection,
|
||||||
YGDirectionInherit,
|
YGDirectionInherit,
|
||||||
YGDirectionLTR,
|
YGDirectionLTR,
|
||||||
YGDirectionRTL)
|
YGDirectionRTL)
|
||||||
|
|
||||||
YG_ENUM_SEQ_DECL(
|
YG_ENUM_DECL(
|
||||||
YGDisplay,
|
YGDisplay,
|
||||||
YGDisplayFlex,
|
YGDisplayFlex,
|
||||||
YGDisplayNone)
|
YGDisplayNone)
|
||||||
|
|
||||||
YG_ENUM_SEQ_DECL(
|
YG_ENUM_DECL(
|
||||||
YGEdge,
|
YGEdge,
|
||||||
YGEdgeLeft,
|
YGEdgeLeft,
|
||||||
YGEdgeTop,
|
YGEdgeTop,
|
||||||
@@ -62,25 +62,25 @@ YG_ENUM_DECL(
|
|||||||
YGErrataClassic = 2147483646)
|
YGErrataClassic = 2147483646)
|
||||||
YG_DEFINE_ENUM_FLAG_OPERATORS(YGErrata)
|
YG_DEFINE_ENUM_FLAG_OPERATORS(YGErrata)
|
||||||
|
|
||||||
YG_ENUM_SEQ_DECL(
|
YG_ENUM_DECL(
|
||||||
YGExperimentalFeature,
|
YGExperimentalFeature,
|
||||||
YGExperimentalFeatureWebFlexBasis,
|
YGExperimentalFeatureWebFlexBasis,
|
||||||
YGExperimentalFeatureAbsolutePercentageAgainstPaddingEdge)
|
YGExperimentalFeatureAbsolutePercentageAgainstPaddingEdge)
|
||||||
|
|
||||||
YG_ENUM_SEQ_DECL(
|
YG_ENUM_DECL(
|
||||||
YGFlexDirection,
|
YGFlexDirection,
|
||||||
YGFlexDirectionColumn,
|
YGFlexDirectionColumn,
|
||||||
YGFlexDirectionColumnReverse,
|
YGFlexDirectionColumnReverse,
|
||||||
YGFlexDirectionRow,
|
YGFlexDirectionRow,
|
||||||
YGFlexDirectionRowReverse)
|
YGFlexDirectionRowReverse)
|
||||||
|
|
||||||
YG_ENUM_SEQ_DECL(
|
YG_ENUM_DECL(
|
||||||
YGGutter,
|
YGGutter,
|
||||||
YGGutterColumn,
|
YGGutterColumn,
|
||||||
YGGutterRow,
|
YGGutterRow,
|
||||||
YGGutterAll)
|
YGGutterAll)
|
||||||
|
|
||||||
YG_ENUM_SEQ_DECL(
|
YG_ENUM_DECL(
|
||||||
YGJustify,
|
YGJustify,
|
||||||
YGJustifyFlexStart,
|
YGJustifyFlexStart,
|
||||||
YGJustifyCenter,
|
YGJustifyCenter,
|
||||||
@@ -89,7 +89,7 @@ YG_ENUM_SEQ_DECL(
|
|||||||
YGJustifySpaceAround,
|
YGJustifySpaceAround,
|
||||||
YGJustifySpaceEvenly)
|
YGJustifySpaceEvenly)
|
||||||
|
|
||||||
YG_ENUM_SEQ_DECL(
|
YG_ENUM_DECL(
|
||||||
YGLogLevel,
|
YGLogLevel,
|
||||||
YGLogLevelError,
|
YGLogLevelError,
|
||||||
YGLogLevelWarn,
|
YGLogLevelWarn,
|
||||||
@@ -98,24 +98,24 @@ YG_ENUM_SEQ_DECL(
|
|||||||
YGLogLevelVerbose,
|
YGLogLevelVerbose,
|
||||||
YGLogLevelFatal)
|
YGLogLevelFatal)
|
||||||
|
|
||||||
YG_ENUM_SEQ_DECL(
|
YG_ENUM_DECL(
|
||||||
YGMeasureMode,
|
YGMeasureMode,
|
||||||
YGMeasureModeUndefined,
|
YGMeasureModeUndefined,
|
||||||
YGMeasureModeExactly,
|
YGMeasureModeExactly,
|
||||||
YGMeasureModeAtMost)
|
YGMeasureModeAtMost)
|
||||||
|
|
||||||
YG_ENUM_SEQ_DECL(
|
YG_ENUM_DECL(
|
||||||
YGNodeType,
|
YGNodeType,
|
||||||
YGNodeTypeDefault,
|
YGNodeTypeDefault,
|
||||||
YGNodeTypeText)
|
YGNodeTypeText)
|
||||||
|
|
||||||
YG_ENUM_SEQ_DECL(
|
YG_ENUM_DECL(
|
||||||
YGOverflow,
|
YGOverflow,
|
||||||
YGOverflowVisible,
|
YGOverflowVisible,
|
||||||
YGOverflowHidden,
|
YGOverflowHidden,
|
||||||
YGOverflowScroll)
|
YGOverflowScroll)
|
||||||
|
|
||||||
YG_ENUM_SEQ_DECL(
|
YG_ENUM_DECL(
|
||||||
YGPositionType,
|
YGPositionType,
|
||||||
YGPositionTypeStatic,
|
YGPositionTypeStatic,
|
||||||
YGPositionTypeRelative,
|
YGPositionTypeRelative,
|
||||||
@@ -128,14 +128,14 @@ YG_ENUM_DECL(
|
|||||||
YGPrintOptionsChildren = 4)
|
YGPrintOptionsChildren = 4)
|
||||||
YG_DEFINE_ENUM_FLAG_OPERATORS(YGPrintOptions)
|
YG_DEFINE_ENUM_FLAG_OPERATORS(YGPrintOptions)
|
||||||
|
|
||||||
YG_ENUM_SEQ_DECL(
|
YG_ENUM_DECL(
|
||||||
YGUnit,
|
YGUnit,
|
||||||
YGUnitUndefined,
|
YGUnitUndefined,
|
||||||
YGUnitPoint,
|
YGUnitPoint,
|
||||||
YGUnitPercent,
|
YGUnitPercent,
|
||||||
YGUnitAuto)
|
YGUnitAuto)
|
||||||
|
|
||||||
YG_ENUM_SEQ_DECL(
|
YG_ENUM_DECL(
|
||||||
YGWrap,
|
YGWrap,
|
||||||
YGWrapNoWrap,
|
YGWrapNoWrap,
|
||||||
YGWrapWrap,
|
YGWrapWrap,
|
||||||
|
@@ -88,40 +88,6 @@
|
|||||||
#define YG_DEFINE_ENUM_FLAG_OPERATORS(name)
|
#define YG_DEFINE_ENUM_FLAG_OPERATORS(name)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
|
|
||||||
namespace facebook::yoga {
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
constexpr int
|
|
||||||
ordinalCount(); // can't use `= delete` due to a defect in clang < 3.9
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
template <int... xs>
|
|
||||||
constexpr int n() {
|
|
||||||
return sizeof...(xs);
|
|
||||||
}
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
} // namespace facebook::yoga
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define YG_ENUM_DECL(NAME, ...) \
|
#define YG_ENUM_DECL(NAME, ...) \
|
||||||
typedef YG_ENUM_BEGIN(NAME){__VA_ARGS__} YG_ENUM_END(NAME); \
|
typedef YG_ENUM_BEGIN(NAME){__VA_ARGS__} YG_ENUM_END(NAME); \
|
||||||
YG_EXPORT const char* NAME##ToString(NAME);
|
YG_EXPORT const char* NAME##ToString(NAME);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
#define YG_ENUM_SEQ_DECL(NAME, ...) \
|
|
||||||
YG_ENUM_DECL(NAME, __VA_ARGS__) \
|
|
||||||
YG_EXTERN_C_END \
|
|
||||||
\
|
|
||||||
namespace facebook::yoga { \
|
|
||||||
template <> \
|
|
||||||
constexpr int ordinalCount<NAME>() { \
|
|
||||||
return detail::n<__VA_ARGS__>(); \
|
|
||||||
} \
|
|
||||||
} \
|
|
||||||
YG_EXTERN_C_BEGIN
|
|
||||||
#else
|
|
||||||
#define YG_ENUM_SEQ_DECL YG_ENUM_DECL
|
|
||||||
#endif
|
|
||||||
|
@@ -1,67 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <bitset>
|
|
||||||
#include <cstdint>
|
|
||||||
#include <cstdio>
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
#include <yoga/YGEnums.h>
|
|
||||||
#include <yoga/enums/YogaEnums.h>
|
|
||||||
|
|
||||||
namespace facebook::yoga::details {
|
|
||||||
|
|
||||||
constexpr uint8_t log2ceilFn(uint8_t n) {
|
|
||||||
return n < 1 ? 0 : (1 + log2ceilFn(n / 2));
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr uint32_t mask(uint8_t bitWidth, uint8_t index) {
|
|
||||||
return ((1u << bitWidth) - 1u) << index;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace facebook::yoga::details
|
|
||||||
|
|
||||||
namespace facebook::yoga {
|
|
||||||
|
|
||||||
// The number of bits necessary to represent enums defined with YG_ENUM_SEQ_DECL
|
|
||||||
template <
|
|
||||||
typename Enum,
|
|
||||||
std::enable_if_t<(ordinalCount<Enum>() > 0), bool> = true>
|
|
||||||
constexpr uint8_t minimumBitCount() {
|
|
||||||
return details::log2ceilFn(static_cast<uint8_t>(ordinalCount<Enum>() - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Enum>
|
|
||||||
constexpr Enum getEnumData(uint32_t flags, uint8_t index) {
|
|
||||||
return static_cast<Enum>(
|
|
||||||
(flags & details::mask(minimumBitCount<Enum>(), index)) >> index);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Enum, typename Value>
|
|
||||||
void setEnumData(uint32_t& flags, uint8_t index, Value newValue) {
|
|
||||||
flags =
|
|
||||||
(flags &
|
|
||||||
~static_cast<uint32_t>(details::mask(minimumBitCount<Enum>(), index))) |
|
|
||||||
((static_cast<uint32_t>(newValue) << index) &
|
|
||||||
(details::mask(minimumBitCount<Enum>(), index)));
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr bool getBooleanData(uint32_t flags, uint8_t index) {
|
|
||||||
return (flags >> index) & 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void setBooleanData(uint32_t& flags, uint8_t index, bool value) {
|
|
||||||
if (value) {
|
|
||||||
flags |= 1 << index;
|
|
||||||
} else {
|
|
||||||
flags &= ~(1 << index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace facebook::yoga
|
|
@@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
#include <yoga/bits/NumericBitfield.h>
|
|
||||||
#include <yoga/debug/AssertFatal.h>
|
#include <yoga/debug/AssertFatal.h>
|
||||||
#include <yoga/enums/Dimension.h>
|
#include <yoga/enums/Dimension.h>
|
||||||
#include <yoga/enums/Direction.h>
|
#include <yoga/enums/Direction.h>
|
||||||
|
@@ -14,7 +14,6 @@
|
|||||||
|
|
||||||
#include <yoga/Yoga.h>
|
#include <yoga/Yoga.h>
|
||||||
|
|
||||||
#include <yoga/bits/NumericBitfield.h>
|
|
||||||
#include <yoga/enums/Align.h>
|
#include <yoga/enums/Align.h>
|
||||||
#include <yoga/enums/Dimension.h>
|
#include <yoga/enums/Dimension.h>
|
||||||
#include <yoga/enums/Direction.h>
|
#include <yoga/enums/Direction.h>
|
||||||
@@ -55,123 +54,74 @@ class YG_EXPORT Style {
|
|||||||
static constexpr float DefaultFlexShrink = 0.0f;
|
static constexpr float DefaultFlexShrink = 0.0f;
|
||||||
static constexpr float WebDefaultFlexShrink = 1.0f;
|
static constexpr float WebDefaultFlexShrink = 1.0f;
|
||||||
|
|
||||||
Style() {
|
|
||||||
setAlignContent(Align::FlexStart);
|
|
||||||
setAlignItems(Align::Stretch);
|
|
||||||
}
|
|
||||||
~Style() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
using Dimensions = std::array<Style::Length, ordinalCount<Dimension>()>;
|
|
||||||
using Edges = std::array<Style::Length, ordinalCount<Edge>()>;
|
|
||||||
using Gutters = std::array<Style::Length, ordinalCount<Gutter>()>;
|
|
||||||
|
|
||||||
static constexpr uint8_t directionOffset = 0;
|
|
||||||
static constexpr uint8_t flexdirectionOffset =
|
|
||||||
directionOffset + minimumBitCount<Direction>();
|
|
||||||
static constexpr uint8_t justifyContentOffset =
|
|
||||||
flexdirectionOffset + minimumBitCount<FlexDirection>();
|
|
||||||
static constexpr uint8_t alignContentOffset =
|
|
||||||
justifyContentOffset + minimumBitCount<Justify>();
|
|
||||||
static constexpr uint8_t alignItemsOffset =
|
|
||||||
alignContentOffset + minimumBitCount<Align>();
|
|
||||||
static constexpr uint8_t alignSelfOffset =
|
|
||||||
alignItemsOffset + minimumBitCount<Align>();
|
|
||||||
static constexpr uint8_t positionTypeOffset =
|
|
||||||
alignSelfOffset + minimumBitCount<Align>();
|
|
||||||
static constexpr uint8_t flexWrapOffset =
|
|
||||||
positionTypeOffset + minimumBitCount<PositionType>();
|
|
||||||
static constexpr uint8_t overflowOffset =
|
|
||||||
flexWrapOffset + minimumBitCount<Wrap>();
|
|
||||||
static constexpr uint8_t displayOffset =
|
|
||||||
overflowOffset + minimumBitCount<Overflow>();
|
|
||||||
|
|
||||||
uint32_t flags_ = 0;
|
|
||||||
|
|
||||||
FloatOptional flex_ = {};
|
|
||||||
FloatOptional flexGrow_ = {};
|
|
||||||
FloatOptional flexShrink_ = {};
|
|
||||||
Style::Length flexBasis_ = value::ofAuto();
|
|
||||||
Edges margin_ = {};
|
|
||||||
Edges position_ = {};
|
|
||||||
Edges padding_ = {};
|
|
||||||
Edges border_ = {};
|
|
||||||
Gutters gap_ = {};
|
|
||||||
Dimensions dimensions_{value::ofAuto(), value::ofAuto()};
|
|
||||||
Dimensions minDimensions_ = {};
|
|
||||||
Dimensions maxDimensions_ = {};
|
|
||||||
// Yoga specific properties, not compatible with flexbox specification
|
|
||||||
FloatOptional aspectRatio_ = {};
|
|
||||||
|
|
||||||
public:
|
|
||||||
Direction direction() const {
|
Direction direction() const {
|
||||||
return getEnumData<Direction>(flags_, directionOffset);
|
return direction_;
|
||||||
}
|
}
|
||||||
void setDirection(Direction value) {
|
void setDirection(Direction value) {
|
||||||
setEnumData<Direction>(flags_, directionOffset, value);
|
direction_ = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
FlexDirection flexDirection() const {
|
FlexDirection flexDirection() const {
|
||||||
return getEnumData<FlexDirection>(flags_, flexdirectionOffset);
|
return flexDirection_;
|
||||||
}
|
}
|
||||||
void setFlexDirection(FlexDirection value) {
|
void setFlexDirection(FlexDirection value) {
|
||||||
setEnumData<FlexDirection>(flags_, flexdirectionOffset, value);
|
flexDirection_ = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Justify justifyContent() const {
|
Justify justifyContent() const {
|
||||||
return getEnumData<Justify>(flags_, justifyContentOffset);
|
return justifyContent_;
|
||||||
}
|
}
|
||||||
void setJustifyContent(Justify value) {
|
void setJustifyContent(Justify value) {
|
||||||
setEnumData<Justify>(flags_, justifyContentOffset, value);
|
justifyContent_ = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Align alignContent() const {
|
Align alignContent() const {
|
||||||
return getEnumData<Align>(flags_, alignContentOffset);
|
return alignContent_;
|
||||||
}
|
}
|
||||||
void setAlignContent(Align value) {
|
void setAlignContent(Align value) {
|
||||||
setEnumData<Align>(flags_, alignContentOffset, value);
|
alignContent_ = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Align alignItems() const {
|
Align alignItems() const {
|
||||||
return getEnumData<Align>(flags_, alignItemsOffset);
|
return alignItems_;
|
||||||
}
|
}
|
||||||
void setAlignItems(Align value) {
|
void setAlignItems(Align value) {
|
||||||
setEnumData<Align>(flags_, alignItemsOffset, value);
|
alignItems_ = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Align alignSelf() const {
|
Align alignSelf() const {
|
||||||
return getEnumData<Align>(flags_, alignSelfOffset);
|
return alignSelf_;
|
||||||
}
|
}
|
||||||
void setAlignSelf(Align value) {
|
void setAlignSelf(Align value) {
|
||||||
setEnumData<Align>(flags_, alignSelfOffset, value);
|
alignSelf_ = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
PositionType positionType() const {
|
PositionType positionType() const {
|
||||||
return getEnumData<PositionType>(flags_, positionTypeOffset);
|
return positionType_;
|
||||||
}
|
}
|
||||||
void setPositionType(PositionType value) {
|
void setPositionType(PositionType value) {
|
||||||
setEnumData<PositionType>(flags_, positionTypeOffset, value);
|
positionType_ = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Wrap flexWrap() const {
|
Wrap flexWrap() const {
|
||||||
return getEnumData<Wrap>(flags_, flexWrapOffset);
|
return flexWrap_;
|
||||||
}
|
}
|
||||||
void setFlexWrap(Wrap value) {
|
void setFlexWrap(Wrap value) {
|
||||||
setEnumData<Wrap>(flags_, flexWrapOffset, value);
|
flexWrap_ = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Overflow overflow() const {
|
Overflow overflow() const {
|
||||||
return getEnumData<Overflow>(flags_, overflowOffset);
|
return overflow_;
|
||||||
}
|
}
|
||||||
void setOverflow(Overflow value) {
|
void setOverflow(Overflow value) {
|
||||||
setEnumData<Overflow>(flags_, overflowOffset, value);
|
overflow_ = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Display display() const {
|
Display display() const {
|
||||||
return getEnumData<Display>(flags_, displayOffset);
|
return display_;
|
||||||
}
|
}
|
||||||
void setDisplay(Display value) {
|
void setDisplay(Display value) {
|
||||||
setEnumData<Display>(flags_, displayOffset, value);
|
display_ = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
FloatOptional flex() const {
|
FloatOptional flex() const {
|
||||||
@@ -282,7 +232,14 @@ class YG_EXPORT Style {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const Style& other) const {
|
bool operator==(const Style& other) const {
|
||||||
return flags_ == other.flags_ && inexactEquals(flex_, other.flex_) &&
|
return direction_ == other.direction_ &&
|
||||||
|
flexDirection_ == other.flexDirection_ &&
|
||||||
|
justifyContent_ == other.justifyContent_ &&
|
||||||
|
alignContent_ == other.alignContent_ &&
|
||||||
|
alignItems_ == other.alignItems_ && alignSelf_ == other.alignSelf_ &&
|
||||||
|
positionType_ == other.positionType_ && flexWrap_ == other.flexWrap_ &&
|
||||||
|
overflow_ == other.overflow_ && display_ == other.display_ &&
|
||||||
|
inexactEquals(flex_, other.flex_) &&
|
||||||
inexactEquals(flexGrow_, other.flexGrow_) &&
|
inexactEquals(flexGrow_, other.flexGrow_) &&
|
||||||
inexactEquals(flexShrink_, other.flexShrink_) &&
|
inexactEquals(flexShrink_, other.flexShrink_) &&
|
||||||
inexactEquals(flexBasis_, other.flexBasis_) &&
|
inexactEquals(flexBasis_, other.flexBasis_) &&
|
||||||
@@ -300,6 +257,37 @@ class YG_EXPORT Style {
|
|||||||
bool operator!=(const Style& other) const {
|
bool operator!=(const Style& other) const {
|
||||||
return !(*this == other);
|
return !(*this == other);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
using Dimensions = std::array<Style::Length, ordinalCount<Dimension>()>;
|
||||||
|
using Edges = std::array<Style::Length, ordinalCount<Edge>()>;
|
||||||
|
using Gutters = std::array<Style::Length, ordinalCount<Gutter>()>;
|
||||||
|
|
||||||
|
Direction direction_ : bitCount<Direction>() = Direction::Inherit;
|
||||||
|
FlexDirection flexDirection_
|
||||||
|
: bitCount<FlexDirection>() = FlexDirection::Column;
|
||||||
|
Justify justifyContent_ : bitCount<Justify>() = Justify::FlexStart;
|
||||||
|
Align alignContent_ : bitCount<Align>() = Align::FlexStart;
|
||||||
|
Align alignItems_ : bitCount<Align>() = Align::Stretch;
|
||||||
|
Align alignSelf_ : bitCount<Align>() = Align::Auto;
|
||||||
|
PositionType positionType_ : bitCount<PositionType>() = PositionType::Static;
|
||||||
|
Wrap flexWrap_ : bitCount<Wrap>() = Wrap::NoWrap;
|
||||||
|
Overflow overflow_ : bitCount<Overflow>() = Overflow::Visible;
|
||||||
|
Display display_ : bitCount<Display>() = Display::Flex;
|
||||||
|
|
||||||
|
FloatOptional flex_{};
|
||||||
|
FloatOptional flexGrow_{};
|
||||||
|
FloatOptional flexShrink_{};
|
||||||
|
Style::Length flexBasis_{value::ofAuto()};
|
||||||
|
Edges margin_{};
|
||||||
|
Edges position_{};
|
||||||
|
Edges padding_{};
|
||||||
|
Edges border_{};
|
||||||
|
Gutters gap_{};
|
||||||
|
Dimensions dimensions_{value::ofAuto(), value::ofAuto()};
|
||||||
|
Dimensions minDimensions_{};
|
||||||
|
Dimensions maxDimensions_{};
|
||||||
|
FloatOptional aspectRatio_{};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace facebook::yoga
|
} // namespace facebook::yoga
|
||||||
|
Reference in New Issue
Block a user