#ifndef CLEVERSYNC_JNI_HPP #define CLEVERSYNC_JNI_HPP #include #include #include #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 Ref { public: constexpr Ref() : env_(nullptr), ptr_(0) {} Ref(const Ref&) = 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 LocalRef : public Ref { public: LocalRef(JNIEnv* env, T ptr): Ref(env, ptr) {} ~LocalRef() override { free(); } LocalRef(LocalRef &&ref) noexcept : Ref(ref.env_, ref.release()) {} LocalRef& operator=(const Ref& 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 ParamRef : public Ref { public: ParamRef(JNIEnv* env, T ptr) : Ref(env, ptr) {} ~ParamRef() override = default; ParamRef& operator=(const Ref& other) = delete; protected: T release_to_local() override { if (this->ptr_) return static_cast(this->env_->NewLocalRef(static_cast(this->ptr_))); return 0; } void del() override {} }; template class GlobalRef : public Ref { public: GlobalRef(JNIEnv* env, T ptr) : Ref(env, ptr ? static_cast(env->NewGlobalRef(static_cast(ptr))) : 0) {} explicit GlobalRef(const Ref& other) : Ref(other.env(), other ? static_cast(other.env()->NewGlobalRef(static_cast(other.get()))) : 0) {} ~GlobalRef() override { free(); } GlobalRef& operator=(const Ref& other) { free(); this->env_ = other.env(); this->ptr_ = other ? static_cast(this->env_->NewGlobalRef(static_cast(other.get()))) : 0; return *this; } protected: T release_to_local() override { if (this->ptr_) { auto ret = static_cast(this->env_->NewLocalRef( static_cast(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 FindClass(JNIEnv *env, const char *name); LocalRef ExceptionOccurred(JNIEnv* env); template LocalRef CallObjectMethod(JNIEnv* env, const Ref& object, jmethodID method, Params... params) { return {env, static_cast(env->CallObjectMethod(object.get(), method, params...))}; } template LocalRef CallStaticObjectMethod(JNIEnv* env, const Ref& clazz, jmethodID method, Params... params) { return {env, static_cast(env->CallStaticObjectMethod(clazz.get(), method, params...))}; } std::string StringToUTF8(JNIEnv* env, const Ref& str); LocalRef UTF8ToString(JNIEnv* env, const std::string& str); LocalRef CreateArray(JNIEnv* env, const Ref& element_class, std::vector> objects); namespace internal { template inline void _abort_if_null(const char* file, int line, JNIEnv* env, const jni::Ref& 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