Skip to content

Commit f0f21bb

Browse files
committed
refactor: add a generic getValue for numeric types
1 parent abc79b9 commit f0f21bb

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

packages/react-native-executorch/common/rnexecutorch/TypeConcepts.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ concept HasGenerate = requires(T t) {
1313
{ &T::generate };
1414
};
1515

16+
template <typename T>
17+
concept IsNumeric = std::is_arithmetic<T>::value;
18+
1619
} // namespace rnexecutorch

packages/react-native-executorch/common/rnexecutorch/host_objects/JsiConversions.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ using namespace facebook;
1818

1919
template <typename T> T getValue(const jsi::Value &val, jsi::Runtime &runtime);
2020

21-
template <>
22-
inline double getValue<double>(const jsi::Value &val, jsi::Runtime &runtime) {
23-
return val.asNumber();
24-
}
25-
26-
template <>
27-
inline int getValue<int>(const jsi::Value &val, jsi::Runtime &runtime) {
28-
return val.asNumber();
21+
template <typename T>
22+
requires IsNumeric<T>
23+
inline T getValue(const jsi::Value &val, jsi::Runtime &runtime) {
24+
static_assert(std::is_integral<T>::value || std::is_floating_point<T>::value,
25+
"Only integral and floating-point types are supported");
26+
return static_cast<T>(val.asNumber());
2927
}
3028

3129
template <>

0 commit comments

Comments
 (0)