Skip to content

Commit 36212c2

Browse files
NickGerlemanmeta-codesync[bot]
authored andcommitted
Unify CSSCompoundDataType with its variant (#55738)
Summary: Pull Request resolved: #55738 Previously, CSS compound data types used two separate types: `CSSCompoundDataType<T1, T2, ...>` as an empty marker struct for template parameter deduction, and `CSSVariantWithTypes<CSSCompoundDataType<T1, T2, ...>>` which resolved to `std::variant<T1, T2, ...>` for actual storage. This resulted in redundant type aliases like `CSSTransformFunctionVariant`. This change makes `CSSCompoundDataType<T1, T2, ...>` a `using` alias for `std::variant<T1, T2, ...>` directly, so one type serves both roles: template parameter deduction for parsing AND storage. - Change `CSSCompoundDataType` from a marker struct to a `using` alias for `std::variant` - Replace `CSSValidCompoundDataType` concept with `is_variant_of_data_types` trait that pattern-matches `std::variant` where all types satisfy `CSSDataType` - Update `merge_data_types` and `merge_variant` specializations to use `std::variant` directly - Store `AllowedTypesT` directly in `CSSList` compound type specialization (since it IS now the variant) - Remove redundant `CSSTransformFunctionVariant`, `CSSFilterFunctionVariant`, and `CSSBackgroundImageVariant` aliases - Update all downstream references in conversions Changelog: [Internal] Reviewed By: jorge-cab Differential Revision: D94347088 fbshipit-source-id: 99cfe13400325c36682297263aa87d00ac13de13
1 parent 481134a commit 36212c2

File tree

9 files changed

+29
-38
lines changed

9 files changed

+29
-38
lines changed

packages/react-native/ReactCommon/react/renderer/components/view/BackgroundImagePropsConversions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ void fromCSSColorStop(
477477
}
478478

479479
std::optional<BackgroundImage> fromCSSBackgroundImage(
480-
const CSSBackgroundImageVariant& cssBackgroundImage) {
480+
const CSSBackgroundImage& cssBackgroundImage) {
481481
if (std::holds_alternative<CSSLinearGradientFunction>(cssBackgroundImage)) {
482482
const auto& gradient =
483483
std::get<CSSLinearGradientFunction>(cssBackgroundImage);

packages/react-native/ReactCommon/react/renderer/components/view/FilterPropsConversions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ parseProcessedFilter(const PropsParserContext &context, const RawValue &value, s
108108
result = filter;
109109
}
110110

111-
inline FilterType filterTypeFromVariant(const CSSFilterFunctionVariant &filter)
111+
inline FilterType filterTypeFromVariant(const CSSFilterFunction &filter)
112112
{
113113
return std::visit(
114114
[](auto &&filter) -> FilterType {
@@ -148,7 +148,7 @@ inline FilterType filterTypeFromVariant(const CSSFilterFunctionVariant &filter)
148148
filter);
149149
}
150150

151-
inline std::optional<FilterFunction> fromCSSFilter(const CSSFilterFunctionVariant &cssFilter)
151+
inline std::optional<FilterFunction> fromCSSFilter(const CSSFilterFunction &cssFilter)
152152
{
153153
return std::visit(
154154
[&](auto &&filter) -> std::optional<FilterFunction> {

packages/react-native/ReactCommon/react/renderer/components/view/conversions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ inline ValueUnit cssLengthPercentageToValueUnit(const std::variant<CSSLength, CS
550550
}
551551
}
552552

553-
inline std::optional<TransformOperation> fromCSSTransformFunction(const CSSTransformFunctionVariant &cssTransform)
553+
inline std::optional<TransformOperation> fromCSSTransformFunction(const CSSTransformFunction &cssTransform)
554554
{
555555
constexpr auto Zero = ValueUnit(0, UnitType::Point);
556556
constexpr auto One = ValueUnit(1, UnitType::Point);

packages/react-native/ReactCommon/react/renderer/css/CSSBackgroundImage.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -845,11 +845,6 @@ static_assert(CSSDataType<CSSLinearGradientFunction>);
845845
*/
846846
using CSSBackgroundImage = CSSCompoundDataType<CSSLinearGradientFunction, CSSRadialGradientFunction>;
847847

848-
/**
849-
* Variant of possible CSS background image types
850-
*/
851-
using CSSBackgroundImageVariant = CSSVariantWithTypes<CSSBackgroundImage>;
852-
853848
/**
854849
* Representation of <background-image-list>
855850
*/

packages/react-native/ReactCommon/react/renderer/css/CSSCompoundDataType.h

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,24 @@
1111

1212
namespace facebook::react {
1313

14-
namespace detail {
15-
struct CSSCompoundDataTypeMarker {};
16-
} // namespace detail
17-
1814
/**
1915
* Allows grouping together multiple possible CSSDataType to parse.
16+
* CSSCompoundDataType is a std::variant of the allowed types, serving both
17+
* as storage and for template parameter deduction during parsing.
2018
*/
2119
template <CSSDataType... AllowedTypesT>
22-
struct CSSCompoundDataType : public detail::CSSCompoundDataTypeMarker {};
20+
using CSSCompoundDataType = std::variant<AllowedTypesT...>;
21+
22+
namespace detail {
23+
template <typename T>
24+
struct is_variant_of_data_types : std::false_type {};
25+
26+
template <CSSDataType... Ts>
27+
struct is_variant_of_data_types<std::variant<Ts...>> : std::true_type {};
28+
} // namespace detail
2329

2430
template <typename T>
25-
concept CSSValidCompoundDataType = std::is_base_of_v<detail::CSSCompoundDataTypeMarker, T>;
31+
concept CSSValidCompoundDataType = detail::is_variant_of_data_types<T>::value;
2632

2733
/**
2834
* A concrete data type, or a compound data type which represents multiple other
@@ -38,40 +44,40 @@ template <CSSMaybeCompoundDataType... AllowedTypesT>
3844
struct merge_data_types;
3945

4046
template <CSSDataType... AlllowedTypes1T, CSSDataType... AlllowedTypes2T, CSSMaybeCompoundDataType... RestT>
41-
struct merge_data_types<CSSCompoundDataType<AlllowedTypes1T...>, CSSCompoundDataType<AlllowedTypes2T...>, RestT...> {
42-
using type = typename merge_data_types<CSSCompoundDataType<AlllowedTypes1T..., AlllowedTypes2T...>, RestT...>::type;
47+
struct merge_data_types<std::variant<AlllowedTypes1T...>, std::variant<AlllowedTypes2T...>, RestT...> {
48+
using type = typename merge_data_types<std::variant<AlllowedTypes1T..., AlllowedTypes2T...>, RestT...>::type;
4349
};
4450

4551
template <CSSDataType AlllowedType1T, CSSDataType... AlllowedTypes2T, CSSMaybeCompoundDataType... RestT>
46-
struct merge_data_types<AlllowedType1T, CSSCompoundDataType<AlllowedTypes2T...>, RestT...> {
47-
using type = typename merge_data_types<CSSCompoundDataType<AlllowedType1T, AlllowedTypes2T...>, RestT...>::type;
52+
struct merge_data_types<AlllowedType1T, std::variant<AlllowedTypes2T...>, RestT...> {
53+
using type = typename merge_data_types<std::variant<AlllowedType1T, AlllowedTypes2T...>, RestT...>::type;
4854
};
4955

5056
template <CSSDataType AlllowedType2T, CSSDataType... AlllowedTypes1T, CSSMaybeCompoundDataType... RestT>
51-
struct merge_data_types<CSSCompoundDataType<AlllowedTypes1T...>, AlllowedType2T, RestT...> {
52-
using type = typename merge_data_types<CSSCompoundDataType<AlllowedTypes1T..., AlllowedType2T>, RestT...>::type;
57+
struct merge_data_types<std::variant<AlllowedTypes1T...>, AlllowedType2T, RestT...> {
58+
using type = typename merge_data_types<std::variant<AlllowedTypes1T..., AlllowedType2T>, RestT...>::type;
5359
};
5460

5561
template <CSSDataType AlllowedType1T, CSSDataType AlllowedType2T, CSSMaybeCompoundDataType... RestT>
5662
struct merge_data_types<AlllowedType1T, AlllowedType2T, RestT...> {
57-
using type = typename merge_data_types<CSSCompoundDataType<AlllowedType1T, AlllowedType2T>, RestT...>::type;
63+
using type = typename merge_data_types<std::variant<AlllowedType1T, AlllowedType2T>, RestT...>::type;
5864
};
5965

6066
template <CSSDataType... AllowedTypesT>
61-
struct merge_data_types<CSSCompoundDataType<AllowedTypesT...>> {
62-
using type = CSSCompoundDataType<AllowedTypesT...>;
67+
struct merge_data_types<std::variant<AllowedTypesT...>> {
68+
using type = std::variant<AllowedTypesT...>;
6369
};
6470

6571
template <CSSDataType AllowedTypeT>
6672
struct merge_data_types<AllowedTypeT> {
67-
using type = CSSCompoundDataType<AllowedTypeT>;
73+
using type = std::variant<AllowedTypeT>;
6874
};
6975

7076
template <typename... T>
7177
struct merge_variant;
7278

7379
template <CSSDataType... AllowedTypesT, typename... RestT>
74-
struct merge_variant<CSSCompoundDataType<AllowedTypesT...>, RestT...> {
80+
struct merge_variant<std::variant<AllowedTypesT...>, RestT...> {
7581
using type = std::variant<RestT..., AllowedTypesT...>;
7682
};
7783
} // namespace detail

packages/react-native/ReactCommon/react/renderer/css/CSSFilter.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,6 @@ using CSSFilterFunction = CSSCompoundDataType<
307307
CSSSaturateFilter,
308308
CSSSepiaFilter>;
309309

310-
/**
311-
* Variant of possible CSS filter function types
312-
*/
313-
using CSSFilterFunctionVariant = CSSVariantWithTypes<CSSFilterFunction>;
314-
315310
/**
316311
* Representation of <filter-value-list>
317312
* https://www.w3.org/TR/filter-effects-1/#typedef-filter-value-list

packages/react-native/ReactCommon/react/renderer/css/CSSList.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ template <CSSDataType AllowedTypeT, CSSDelimiter Delim>
2424
struct CSSList<AllowedTypeT, Delim> : public std::vector<AllowedTypeT> {};
2525

2626
template <CSSValidCompoundDataType AllowedTypesT, CSSDelimiter Delim>
27-
struct CSSList<AllowedTypesT, Delim> : public std::vector<CSSVariantWithTypes<AllowedTypesT>> {};
27+
struct CSSList<AllowedTypesT, Delim> : public std::vector<AllowedTypesT> {};
2828

2929
template <CSSMaybeCompoundDataType AllowedTypeT, CSSDelimiter Delim>
3030
struct CSSDataTypeParser<CSSList<AllowedTypeT, Delim>> {

packages/react-native/ReactCommon/react/renderer/css/CSSTransform.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,11 +452,6 @@ using CSSTransformFunction = CSSCompoundDataType<
452452
CSSSkewY,
453453
CSSPerspective>;
454454

455-
/**
456-
* Variant of possible CSS transform function types
457-
*/
458-
using CSSTransformFunctionVariant = CSSVariantWithTypes<CSSTransformFunction>;
459-
460455
/**
461456
* Represents the <transform-list> type.
462457
* https://drafts.csswg.org/css-transforms-1/#typedef-transform-list

packages/react-native/ReactCommon/react/renderer/css/CSSValueParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CSSValueParser {
3333
template <CSSDataType... AllowedTypesT>
3434
constexpr std::variant<std::monostate, AllowedTypesT...> consumeValue(
3535
CSSDelimiter delimeter,
36-
CSSCompoundDataType<AllowedTypesT...> /*unused*/)
36+
std::variant<AllowedTypesT...> /*unused*/)
3737
{
3838
using ReturnT = std::variant<std::monostate, AllowedTypesT...>;
3939

0 commit comments

Comments
 (0)