Skip to content

Commit 3776252

Browse files
committed
refactor: add a generic getValue for numeric types
1 parent 0b0be90 commit 3776252

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
@@ -21,14 +21,12 @@ using namespace facebook;
2121

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

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

3432
template <>

0 commit comments

Comments
 (0)