2021SC@SDUSC
layout/android/jni
scoped_java_ref.h
template <typename T>
class JavaRef : public JavaRef<jobject> {
public:
JavaRef() {}
explicit JavaRef(std::nullptr_t) : JavaRef<jobject>(nullptr) {}
~JavaRef() {}
T obj() const { return static_cast<T>(JavaRef<jobject>::obj()); }
protected:
JavaRef(JNIEnv* env, T obj) : JavaRef<jobject>(env, obj) {}
private:
};
template <typename T>
class JavaParamRef : public JavaRef<T> {
public:
JavaParamRef(JNIEnv* env, T obj) : JavaRef<T>(env, obj) {}
explicit JavaParamRef(std::nullptr_t) : JavaRef<T>(nullptr) {}
~JavaParamRef() {}
operator T() const { return JavaRef<T>::obj(); }
private:
};
template <typename T>
class ScopedJavaLocalRef : public JavaRef<T> {
public:
ScopedJavaLocalRef() : env_(nullptr) {}
explicit ScopedJavaLocalRef(std::nullptr_t) : env_(nullptr) {}
ScopedJavaLocalRef(const ScopedJavaLocalRef<T>& other) : env_(other.env_) {
this->SetNewLocalRef(env_, other.obj());
}
ScopedJavaLocalRef(ScopedJavaLocalRef<T>&& other) : env_(other.env_) { this->swap(other); }
explicit ScopedJavaLocalRef(const JavaRef<T>& other) : env_(nullptr) { this->Reset(other); }
ScopedJavaLocalRef(JNIEnv* env, T obj) : JavaRef<T>(env, obj), env_(env) {}
~ScopedJavaLocalRef() { this->Reset(); }
void operator=(const ScopedJavaLocalRef<T>& other) { this->Reset(other); }
void operator=(ScopedJavaLocalRef<T>&& other) {
env_ = other.env_;
this->swap(other);
}
void Reset() { this->ResetLocalRef(env_); }
void Reset(const ScopedJavaLocalRef<T>& other) {
this->Reset(other.env_, other.obj());
}
void Reset(const JavaRef<T>& other) {
this->Reset(env_, other.obj());
}
void Reset(JNIEnv* env, T obj) { env_ = this->SetNewLocalRef(env, obj); }
T Release() { return static_cast<T>(this->ReleaseInternal()); }
private:
JNIEnv* env_;
ScopedJavaLocalRef(JNIEnv* env, const JavaParamRef<T>& other);
};
template <typename T>
class ScopedJavaGlobalRef : public JavaRef<T> {
public:
ScopedJavaGlobalRef() {}
explicit ScopedJavaGlobalRef(std::nullptr_t) {}
ScopedJavaGlobalRef(const ScopedJavaGlobalRef<T>& other) { this->Reset(other); }
ScopedJavaGlobalRef(ScopedJavaGlobalRef<T>&& other) { this->swap(other); }
ScopedJavaGlobalRef(JNIEnv* env, T obj) { this->Reset(env, obj); }
explicit ScopedJavaGlobalRef(const JavaRef<T>& other) { this->Reset(other); }
~ScopedJavaGlobalRef() { this->Reset(); }
void operator=(const ScopedJavaGlobalRef<T>& other) { this->Reset(other); }
void operator=(ScopedJavaGlobalRef<T>&& other) { this->swap(other); }
void Reset() { this->ResetGlobalRef(); }
void Reset(const JavaRef<T>& other) { this->Reset(nullptr, other.obj()); }
void Reset(JNIEnv* env, const JavaParamRef<T>& other) { this->Reset(env, other.obj()); }
void Reset(JNIEnv* env, T obj) { this->SetNewGlobalRef(env, obj); }
T Release() { return static_cast<T>(this->ReleaseInternal()); }
};
template <typename T>
class JavaRefOrBare {
public:
explicit JavaRefOrBare(std::nullptr_t) : obj_(nullptr) {}
explicit JavaRefOrBare(const JavaRef<T>& ref) : obj_(ref.obj()) {}
explicit JavaRefOrBare(T obj) : obj_(obj) {}
T obj() const { return obj_; }
private:
T obj_;
};
}
}
|