Add YGErrata Enum (#1256)

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

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

This adds a `YGErrata` bitset enum matching the API and guarantees described in https://github.com/facebook/yoga/issues/1247.

It is hooked up in later diffs. There are a couple of `YGExperimentalFeature` values that belong here, but keeping the current options means that the default `YGErrataNone` corresponds to existing default behavior, letting us stage the series of changes as:
1. Implement errata API
2. Update internal Yoga users we want to de-risk to `YGErrataClassic` or `YGErrataAll` (if setting `UseLegacyStretchBehaviour`)
3. Add new errata, changing Yoga defaults to be conformant, while letting internal apps opt into compatibility modes pending experimentation.

I also added a macro to let C++ users of Yoga perform bitwise operations on the enum without casting (already available for C users).

Reviewed By: rshest

Differential Revision: D45254098

fbshipit-source-id: d4b61271a8018f548f2d9d8c953db4b121a502d1
This commit is contained in:
Nick Gerleman
2023-04-27 03:15:14 -07:00
committed by Facebook GitHub Bot
parent fc68765314
commit 01c0c4ed3b
8 changed files with 166 additions and 0 deletions

View File

@@ -87,6 +87,20 @@ const char* YGEdgeToString(const YGEdge value) {
return "unknown";
}
const char* YGErrataToString(const YGErrata value) {
switch (value) {
case YGErrataNone:
return "none";
case YGErrataStretchFlexBasis:
return "stretch-flex-basis";
case YGErrataAll:
return "all";
case YGErrataClassic:
return "classic";
}
return "unknown";
}
const char* YGExperimentalFeatureToString(const YGExperimentalFeature value) {
switch (value) {
case YGExperimentalFeatureWebFlexBasis:

View File

@@ -54,6 +54,14 @@ YG_ENUM_SEQ_DECL(
YGEdgeVertical,
YGEdgeAll)
YG_ENUM_DECL(
YGErrata,
YGErrataNone = 0,
YGErrataStretchFlexBasis = 1,
YGErrataAll = 2147483647,
YGErrataClassic = 2147483646)
YG_DEFINE_ENUM_FLAG_OPERATORS(YGErrata)
YG_ENUM_SEQ_DECL(
YGExperimentalFeature,
YGExperimentalFeatureWebFlexBasis,

View File

@@ -7,6 +7,10 @@
#pragma once
#ifdef __cplusplus
#include <type_traits>
#endif
#ifdef __cplusplus
#define YG_EXTERN_C_BEGIN extern "C" {
#define YG_EXTERN_C_END }
@@ -40,6 +44,48 @@
#define YG_ENUM_END(name) name
#endif
#ifdef __cplusplus
#define YG_DEFINE_ENUM_FLAG_OPERATORS(name) \
extern "C++" { \
constexpr inline name operator~(name a) { \
return static_cast<name>( \
~static_cast<std::underlying_type<name>::type>(a)); \
} \
constexpr inline name operator|(name a, name b) { \
return static_cast<name>( \
static_cast<std::underlying_type<name>::type>(a) | \
static_cast<std::underlying_type<name>::type>(b)); \
} \
constexpr inline name operator&(name a, name b) { \
return static_cast<name>( \
static_cast<std::underlying_type<name>::type>(a) & \
static_cast<std::underlying_type<name>::type>(b)); \
} \
constexpr inline name operator^(name a, name b) { \
return static_cast<name>( \
static_cast<std::underlying_type<name>::type>(a) ^ \
static_cast<std::underlying_type<name>::type>(b)); \
} \
inline name& operator|=(name& a, name b) { \
return reinterpret_cast<name&>( \
reinterpret_cast<std::underlying_type<name>::type&>(a) |= \
static_cast<std::underlying_type<name>::type>(b)); \
} \
inline name& operator&=(name& a, name b) { \
return reinterpret_cast<name&>( \
reinterpret_cast<std::underlying_type<name>::type&>(a) &= \
static_cast<std::underlying_type<name>::type>(b)); \
} \
inline name& operator^=(name& a, name b) { \
return reinterpret_cast<name&>( \
reinterpret_cast<std::underlying_type<name>::type&>(a) ^= \
static_cast<std::underlying_type<name>::type>(b)); \
} \
}
#else
#define YG_DEFINE_ENUM_FLAG_OPERATORS(name)
#endif
#ifdef __cplusplus
namespace facebook {
namespace yoga {