common.h
3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// sherpa-onnx/jni/common.h
//
// Copyright (c) 2024 Xiaomi Corporation
#ifndef SHERPA_ONNX_JNI_COMMON_H_
#define SHERPA_ONNX_JNI_COMMON_H_
#include <string>
#if __ANDROID_API__ >= 9
#include <strstream>
#include "android/asset_manager.h"
#include "android/asset_manager_jni.h"
#endif
#if defined(_WIN32)
#if defined(SHERPA_ONNX_BUILD_SHARED_LIBS)
#define SHERPA_ONNX_EXPORT __declspec(dllexport)
#define SHERPA_ONNX_IMPORT __declspec(dllimport)
#else
#define SHERPA_ONNX_EXPORT
#define SHERPA_ONNX_IMPORT
#endif
#else // WIN32
#define SHERPA_ONNX_EXPORT __attribute__((visibility("default")))
#define SHERPA_ONNX_IMPORT SHERPA_ONNX_EXPORT
#endif // WIN32
#if defined(SHERPA_ONNX_BUILD_MAIN_LIB)
#define SHERPA_ONNX_API SHERPA_ONNX_EXPORT
#else
#define SHERPA_ONNX_API SHERPA_ONNX_IMPORT
#endif
// If you use ndk, you can find "jni.h" inside
// android-ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include
#include "jni.h" // NOLINT
#define SHERPA_ONNX_EXTERN_C extern "C" SHERPA_ONNX_API
// defined in jni.cc
jobject NewInteger(JNIEnv *env, int32_t value);
jobject NewFloat(JNIEnv *env, float value);
// Template function for non-void return types
template <typename Func, typename ReturnType>
ReturnType SafeJNI(JNIEnv *env, const char *functionName, Func func,
ReturnType defaultValue) {
try {
return func();
} catch (const std::exception &e) {
jclass exClass = env->FindClass("java/lang/RuntimeException");
if (exClass != nullptr) {
std::string errorMessage = std::string(functionName) + ": " + e.what();
env->ThrowNew(exClass, errorMessage.c_str());
}
} catch (...) {
jclass exClass = env->FindClass("java/lang/RuntimeException");
if (exClass != nullptr) {
std::string errorMessage = std::string(functionName) +
": Native exception: caught unknown exception";
env->ThrowNew(exClass, errorMessage.c_str());
}
}
return defaultValue;
}
// Specialization for void return type
template <typename Func>
void SafeJNI(JNIEnv *env, const char *functionName, Func func) {
try {
func();
} catch (const std::exception &e) {
jclass exClass = env->FindClass("java/lang/RuntimeException");
if (exClass != nullptr) {
std::string errorMessage = std::string(functionName) + ": " + e.what();
env->ThrowNew(exClass, errorMessage.c_str());
}
} catch (...) {
jclass exClass = env->FindClass("java/lang/RuntimeException");
if (exClass != nullptr) {
std::string errorMessage = std::string(functionName) +
": Native exception: caught unknown exception";
env->ThrowNew(exClass, errorMessage.c_str());
}
}
}
// Helper function to validate JNI pointers
inline bool ValidatePointer(JNIEnv *env, jlong ptr,
const char *functionName, const char *message) {
if (ptr == 0) {
jclass exClass = env->FindClass("java/lang/NullPointerException");
if (exClass != nullptr) {
std::string errorMessage = std::string(functionName) + ": " + message;
env->ThrowNew(exClass, errorMessage.c_str());
}
return false;
}
return true;
}
#endif // SHERPA_ONNX_JNI_COMMON_H_