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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
#ifndef CLEVERSYNC_JNI_HPP
#define CLEVERSYNC_JNI_HPP
#include <jni.h>
#include <string>
#include <vector>
#define ABORT_IF_NOT_OK(x) (::jni::internal::_abort_if_not_ok(__FILE__, __LINE__, (x)))
#define ABORT_IF_NULL(env, x) (::jni::internal::_abort_if_null(__FILE__, __LINE__, (env), (x)))
namespace jni {
namespace internal {
void _abort_if_not_ok(const char *file, int line, jint ret);
void _abort_with_exception(const char* file, int line, JNIEnv* env);
} // namespace internal
template<class T>
class Ref {
public:
constexpr Ref() : env_(nullptr), ptr_(0) {}
Ref(const Ref<T>&) = delete;
[[nodiscard]] JNIEnv* env() const { return env_; }
[[nodiscard]] T get() const { return ptr_; }
[[nodiscard]] T release() {
auto ret = release_to_local();
ptr_ = 0;
return ret;
}
void reset() {
del();
ptr_ = 0;
}
explicit operator bool() const { return ptr_ != 0; }
protected:
Ref(JNIEnv* env, T ptr): env_(env), ptr_(ptr) {}
virtual ~Ref() = default;
virtual T release_to_local() = 0;
virtual void del() = 0;
JNIEnv* env_;
T ptr_;
};
template<class T>
class LocalRef : public Ref<T> {
public:
LocalRef(JNIEnv* env, T ptr): Ref<T>(env, ptr) {}
~LocalRef() override { free(); }
LocalRef(LocalRef<T> &&ref) noexcept : Ref<T>(ref.env_, ref.release()) {}
LocalRef& operator=(const Ref<T>& other) = delete;
protected:
T release_to_local() override { return this->ptr_; }
void del() override { free(); }
private:
void free() {
if (this->ptr_)
this->env_->DeleteLocalRef(this->ptr_);
}
};
template<class T>
class ParamRef : public Ref<T> {
public:
ParamRef(JNIEnv* env, T ptr) : Ref<T>(env, ptr) {}
~ParamRef() override = default;
ParamRef& operator=(const Ref<T>& other) = delete;
protected:
T release_to_local() override {
if (this->ptr_)
return static_cast<T>(this->env_->NewLocalRef(static_cast<jobject>(this->ptr_)));
return 0;
}
void del() override {}
};
template<class T>
class GlobalRef : public Ref<T> {
public:
GlobalRef(JNIEnv* env, T ptr) : Ref<T>(env, ptr ? static_cast<T>(env->NewGlobalRef(static_cast<jobject>(ptr))) : 0) {}
explicit GlobalRef(const Ref<T>& other) : Ref<T>(other.env(), other ? static_cast<T>(other.env()->NewGlobalRef(static_cast<jobject>(other.get()))) : 0) {}
~GlobalRef() override { free(); }
GlobalRef& operator=(const Ref<T>& other) {
free();
this->env_ = other.env();
this->ptr_ = other ? static_cast<T>(this->env_->NewGlobalRef(static_cast<jobject>(other.get()))) : 0;
return *this;
}
protected:
T release_to_local() override {
if (this->ptr_) {
auto ret = static_cast<T>(this->env_->NewLocalRef(
static_cast<jobject>(this->ptr_)));
free();
return ret;
}
return 0;
}
void del() override { free(); }
private:
void free() {
if (this->ptr_)
this->env_->DeleteGlobalRef(this->ptr_);
}
};
constexpr jint JNI_VERSION = JNI_VERSION_1_2;
JNIEnv* AttachCurrentThread();
JNIEnv* OnLoad(JavaVM* vm);
LocalRef<jclass> FindClass(JNIEnv *env, const char *name);
LocalRef<jthrowable> ExceptionOccurred(JNIEnv* env);
template<typename Out, typename In, typename... Params>
LocalRef<Out> CallObjectMethod(JNIEnv* env, const Ref<In>& object, jmethodID method, Params... params) {
return {env, static_cast<Out>(env->CallObjectMethod(object.get(), method, params...))};
}
template<typename Out, typename... Params>
LocalRef<Out> CallStaticObjectMethod(JNIEnv* env, const Ref<jclass>& clazz, jmethodID method, Params... params) {
return {env, static_cast<Out>(env->CallStaticObjectMethod(clazz.get(), method, params...))};
}
std::string StringToUTF8(JNIEnv* env, const Ref<jstring>& str);
LocalRef<jstring> UTF8ToString(JNIEnv* env, const std::string& str);
LocalRef<jobjectArray> CreateArray(JNIEnv* env, const Ref<jclass>& element_class, std::vector<LocalRef<jobject>> objects);
namespace internal {
template<typename T>
inline void _abort_if_null(const char* file, int line, JNIEnv* env, const jni::Ref<T>& ref) {
if (ref) [[likely]] return;
_abort_with_exception(file, line, env);
}
inline void _abort_if_null(const char* file, int line, JNIEnv* env, jmethodID ref) {
if (ref) [[likely]] return;
_abort_with_exception(file, line, env);
}
} // namespace internal
} // namespace jni
#endif // CLEVERSYNC_JNI_HPP
|