Skip to content

Commit c7dcb42

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Add YGErrata Enum (#1256)
Summary: X-link: facebook/yoga#1256 Pull Request resolved: #37076 This adds a `YGErrata` bitset enum matching the API and guarantees described in facebook/yoga#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
1 parent 0dcf81b commit c7dcb42

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// @generated by enums.py
9+
10+
package com.facebook.yoga;
11+
12+
public enum YogaErrata {
13+
NONE(0),
14+
STRETCH_FLEX_BASIS(1),
15+
ALL(2147483647),
16+
CLASSIC(2147483646);
17+
18+
private final int mIntValue;
19+
20+
YogaErrata(int intValue) {
21+
mIntValue = intValue;
22+
}
23+
24+
public int intValue() {
25+
return mIntValue;
26+
}
27+
28+
public static YogaErrata fromInt(int value) {
29+
switch (value) {
30+
case 0: return NONE;
31+
case 1: return STRETCH_FLEX_BASIS;
32+
case 2147483647: return ALL;
33+
case 2147483646: return CLASSIC;
34+
default: throw new IllegalArgumentException("Unknown enum value: " + value);
35+
}
36+
}
37+
}

packages/react-native/ReactCommon/yoga/yoga/YGEnums.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ const char* YGEdgeToString(const YGEdge value) {
8787
return "unknown";
8888
}
8989

90+
const char* YGErrataToString(const YGErrata value) {
91+
switch (value) {
92+
case YGErrataNone:
93+
return "none";
94+
case YGErrataStretchFlexBasis:
95+
return "stretch-flex-basis";
96+
case YGErrataAll:
97+
return "all";
98+
case YGErrataClassic:
99+
return "classic";
100+
}
101+
return "unknown";
102+
}
103+
90104
const char* YGExperimentalFeatureToString(const YGExperimentalFeature value) {
91105
switch (value) {
92106
case YGExperimentalFeatureWebFlexBasis:

packages/react-native/ReactCommon/yoga/yoga/YGEnums.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ YG_ENUM_SEQ_DECL(
5454
YGEdgeVertical,
5555
YGEdgeAll)
5656

57+
YG_ENUM_DECL(
58+
YGErrata,
59+
YGErrataNone = 0,
60+
YGErrataStretchFlexBasis = 1,
61+
YGErrataAll = 2147483647,
62+
YGErrataClassic = 2147483646)
63+
YG_DEFINE_ENUM_FLAG_OPERATORS(YGErrata)
64+
5765
YG_ENUM_SEQ_DECL(
5866
YGExperimentalFeature,
5967
YGExperimentalFeatureWebFlexBasis,

packages/react-native/ReactCommon/yoga/yoga/YGMacros.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
#pragma once
99

10+
#ifdef __cplusplus
11+
#include <type_traits>
12+
#endif
13+
1014
#ifdef __cplusplus
1115
#define YG_EXTERN_C_BEGIN extern "C" {
1216
#define YG_EXTERN_C_END }
@@ -40,6 +44,48 @@
4044
#define YG_ENUM_END(name) name
4145
#endif
4246

47+
#ifdef __cplusplus
48+
#define YG_DEFINE_ENUM_FLAG_OPERATORS(name) \
49+
extern "C++" { \
50+
constexpr inline name operator~(name a) { \
51+
return static_cast<name>( \
52+
~static_cast<std::underlying_type<name>::type>(a)); \
53+
} \
54+
constexpr inline name operator|(name a, name b) { \
55+
return static_cast<name>( \
56+
static_cast<std::underlying_type<name>::type>(a) | \
57+
static_cast<std::underlying_type<name>::type>(b)); \
58+
} \
59+
constexpr inline name operator&(name a, name b) { \
60+
return static_cast<name>( \
61+
static_cast<std::underlying_type<name>::type>(a) & \
62+
static_cast<std::underlying_type<name>::type>(b)); \
63+
} \
64+
constexpr inline name operator^(name a, name b) { \
65+
return static_cast<name>( \
66+
static_cast<std::underlying_type<name>::type>(a) ^ \
67+
static_cast<std::underlying_type<name>::type>(b)); \
68+
} \
69+
inline name& operator|=(name& a, name b) { \
70+
return reinterpret_cast<name&>( \
71+
reinterpret_cast<std::underlying_type<name>::type&>(a) |= \
72+
static_cast<std::underlying_type<name>::type>(b)); \
73+
} \
74+
inline name& operator&=(name& a, name b) { \
75+
return reinterpret_cast<name&>( \
76+
reinterpret_cast<std::underlying_type<name>::type&>(a) &= \
77+
static_cast<std::underlying_type<name>::type>(b)); \
78+
} \
79+
inline name& operator^=(name& a, name b) { \
80+
return reinterpret_cast<name&>( \
81+
reinterpret_cast<std::underlying_type<name>::type&>(a) ^= \
82+
static_cast<std::underlying_type<name>::type>(b)); \
83+
} \
84+
}
85+
#else
86+
#define YG_DEFINE_ENUM_FLAG_OPERATORS(name)
87+
#endif
88+
4389
#ifdef __cplusplus
4490
namespace facebook {
4591
namespace yoga {

0 commit comments

Comments
 (0)