@@ -58,11 +58,8 @@ concept DynamicConstructibleCSSValue = requires(const folly::dynamic &value) {
58
58
*
59
59
* A std::variant-based container for multiple CSSValue-derived types.
60
60
*/
61
- template <typename ... AllowedTypes>
61
+ template <CSSValueDerived ... AllowedTypes>
62
62
class CSSValueVariant final : public CSSValue {
63
- static_assert (
64
- (CSSValueDerived<AllowedTypes> && ...),
65
- " CSSValueVariant accepts only CSSValue-derived types" );
66
63
static_assert (
67
64
(JSIConstructibleCSSValue<AllowedTypes> && ...),
68
65
" CSSValueVariant accepts only types that can be constructed from a jsi::Value" );
@@ -74,42 +71,22 @@ class CSSValueVariant final : public CSSValue {
74
71
CSSValueVariant () = default ;
75
72
76
73
/* *
77
- * Construct from any value that is or can construct one of the AllowedTypes
74
+ * Construct from any TValue that is or can construct one of the AllowedTypes
78
75
* (chooses the first one that matches)
79
76
*/
80
- explicit CSSValueVariant (auto &&value)
81
- requires((std::is_constructible_v<AllowedTypes, decltype (value)> || ...))
77
+ template <typename TValue>
78
+ explicit CSSValueVariant (TValue &&value)
79
+ requires((std::is_constructible_v<AllowedTypes, TValue> || ...))
82
80
{ // NOLINT(whitespace/braces)
83
- using ValueType = decltype (value);
84
-
85
- // If value type exactly matches one of AllowedTypes, store it directly:
81
+ // If TValue exactly matches one of AllowedTypes, store it directly:
86
82
if constexpr ((std::is_same_v<
87
- std::remove_reference_t <ValueType >,
83
+ std::remove_reference_t <TValue >,
88
84
AllowedTypes> ||
89
85
...)) {
90
- storage_ = std::forward<ValueType >(value);
86
+ storage_ = std::forward<TValue >(value);
91
87
} else {
92
- // Otherwise, try to construct the CSSValue from each type in turn
93
- auto tryOne = [&]<typename TCSSValue>() -> bool {
94
- if constexpr (std::is_constructible_v<TCSSValue, ValueType>) {
95
- if constexpr (ValueConstructibleCSSValue<TCSSValue, ValueType>) {
96
- // For construction from a non-jsi::Value, we perform a runtime
97
- // canConstruct check only if the type has a canConstruct method.
98
- // (this is needed e.g. when different CSS value types can be
99
- // constructed from the same value type, like CSSLength and
100
- // CSSKeyword)
101
- if (!TCSSValue::canConstruct (std::forward<ValueType>(value))) {
102
- return false ;
103
- }
104
- }
105
- storage_ = TCSSValue (std::forward<ValueType>(value));
106
- return true ;
107
- }
108
- return false ;
109
- };
110
-
111
- // Try constructing with each allowed type until one succeeds
112
- if (!(tryOne.template operator ()<AllowedTypes>() || ...)) {
88
+ // Otherwise, try each type in turn
89
+ if (!tryConstruct (std::forward<TValue>(value))) {
113
90
throw std::runtime_error (
114
91
" [Reanimated] No compatible type found for construction" );
115
92
}
@@ -121,43 +98,15 @@ class CSSValueVariant final : public CSSValue {
121
98
* (chooses the first one that matches)
122
99
*/
123
100
CSSValueVariant (jsi::Runtime &rt, const jsi::Value &jsiValue) {
124
- auto tryOne = [&]<typename TCSSValue>() -> bool {
125
- // We have to check in a runtime if the type can be constructed from the
126
- // provided jsi::Value. The first match will be used to construct the
127
- // CSS value.
128
- if (!TCSSValue::canConstruct (rt, jsiValue)) {
129
- return false ;
130
- }
131
- storage_ = TCSSValue (rt, jsiValue);
132
- return true ;
133
- };
134
-
135
- // Try constructing with each allowed type until one succeeds
136
- if (!(tryOne.template operator ()<AllowedTypes>() || ...)) {
101
+ if (!tryConstruct (rt, jsiValue)) {
137
102
throw std::runtime_error (
138
103
" [Reanimated] No compatible type found for construction from: " +
139
104
stringifyJSIValue (rt, jsiValue));
140
105
}
141
106
}
142
107
143
- /* *
144
- * Construct from folly::dynamic if it matches any AllowedType's constructor
145
- * (chooses the first one that matches)
146
- */
147
108
explicit CSSValueVariant (const folly::dynamic &value) {
148
- auto tryOne = [&]<typename TCSSValue>() -> bool {
149
- // We have to check in a runtime if the type can be constructed from the
150
- // provided folly::dynamic. The first match will be used to construct the
151
- // CSS value.
152
- if (!TCSSValue::canConstruct (value)) {
153
- return false ;
154
- }
155
- storage_ = TCSSValue (value);
156
- return true ;
157
- };
158
-
159
- // Try constructing with each allowed type until one succeeds
160
- if (!(tryOne.template operator ()<AllowedTypes>() || ...)) {
109
+ if (!tryConstruct (value)) {
161
110
throw std::runtime_error (
162
111
" [Reanimated] No compatible type found for construction from: " +
163
112
folly::toJson (value));
@@ -201,7 +150,7 @@ class CSSValueVariant final : public CSSValue {
201
150
CSSValueVariant interpolate (
202
151
const double progress,
203
152
const CSSValueVariant &to,
204
- const CSSValueInterpolationContext &context) const {
153
+ const ValueInterpolationContext &context) const {
205
154
if (storage_.index () != to.storage_ .index ()) {
206
155
return fallbackInterpolate (
207
156
progress, to, context.fallbackInterpolateThreshold );
@@ -230,7 +179,7 @@ class CSSValueVariant final : public CSSValue {
230
179
CSSValueVariant interpolate (
231
180
const double progress,
232
181
const CSSValueVariant &to,
233
- const CSSResolvableValueInterpolationContext &context) const {
182
+ const ResolvableValueInterpolationContext &context) const {
234
183
if (storage_.index () != to.storage_ .index ()) {
235
184
return fallbackInterpolate (
236
185
progress, to, context.fallbackInterpolateThreshold );
@@ -263,6 +212,70 @@ class CSSValueVariant final : public CSSValue {
263
212
const double fallbackInterpolateThreshold) const {
264
213
return (progress < fallbackInterpolateThreshold) ? *this : to;
265
214
}
215
+
216
+ /* *
217
+ * Tries to construct type from a given value
218
+ */
219
+ template <typename TValue>
220
+ bool tryConstruct (TValue &&value) {
221
+ auto tryOne = [&]<typename TCSSValue>() -> bool {
222
+ if constexpr (std::is_constructible_v<TCSSValue, TValue>) {
223
+ if constexpr (ValueConstructibleCSSValue<TCSSValue, TValue>) {
224
+ // For construction from a non-jsi::Value, we perform a runtime
225
+ // canConstruct check only if the type has a canConstruct method.
226
+ // (this is needed e.g. when different CSS value types can be
227
+ // constructed from the same value type, like CSSLength and
228
+ // CSSKeyword)
229
+ if (!TCSSValue::canConstruct (std::forward<TValue>(value))) {
230
+ return false ;
231
+ }
232
+ }
233
+ storage_ = TCSSValue (std::forward<TValue>(value));
234
+ return true ;
235
+ }
236
+ return false ;
237
+ };
238
+
239
+ // Try constructing with each allowed type until one succeeds
240
+ return (tryOne.template operator ()<AllowedTypes>() || ...);
241
+ }
242
+
243
+ /* *
244
+ * Tries to construct type from a given jsi::Value
245
+ */
246
+ bool tryConstruct (jsi::Runtime &rt, const jsi::Value &jsiValue) {
247
+ auto tryOne = [&]<typename TCSSValue>() -> bool {
248
+ // We have to check in a runtime if the type can be constructed from the
249
+ // provided jsi::Value. The first match will be used to construct the
250
+ // CSS value.
251
+ if (!TCSSValue::canConstruct (rt, jsiValue)) {
252
+ return false ;
253
+ }
254
+ storage_ = TCSSValue (rt, jsiValue);
255
+ return true ;
256
+ };
257
+
258
+ // Try constructing with each allowed type until one succeeds
259
+ return (tryOne.template operator ()<AllowedTypes>() || ...);
260
+ }
261
+
262
+ /* *
263
+ * Tries to construct type from a given folly::dynamic
264
+ */
265
+ bool tryConstruct (const folly::dynamic &value) {
266
+ auto tryOne = [&]<typename TCSSValue>() -> bool {
267
+ // We have to check in a runtime if the type can be constructed from the
268
+ // provided folly::dynamic. The first match will be used to construct the
269
+ // CSS value.
270
+ if (!TCSSValue::canConstruct (value)) {
271
+ return false ;
272
+ }
273
+ storage_ = TCSSValue (value);
274
+ return true ;
275
+ };
276
+ // Try constructing with each allowed type until one succeeds
277
+ return (tryOne.template operator ()<AllowedTypes>() || ...);
278
+ }
266
279
};
267
280
268
281
} // namespace reanimated::css
0 commit comments