JJB*_*JJB 0 c++ java-native-interface templates template-classes
我编写了以下使用成员指针函数的类:
#include <stdlib.h>
#include <vector>
template<class Type>
class PrimitiveAccessor {
public :
PrimitiveAccessor(
JNIEnv* env, const char name[], const char ctorSig[],
Type (JNIEnv::*callTypeMethodFunction) (jobject, jmethodID)
) {
this->env = env;
this->type = (jclass)env->NewGlobalRef(env->FindClass(name));
this->callTypeMethodFunction = callTypeMethodFunction;
}
~PrimitiveAccessor(){
env->DeleteGlobalRef(this->type);
}
private:
JNIEnv* env;
jclass type;
jmethodID constructorId;
jmethodID callTypeMethodId;
Type (JNIEnv::*callTypeMethodFunction) (jobject, jmethodID);
};
class Environment {
public:
Environment(JNIEnv* env) {
this->env = env;
this->init();
}
~Environment(){
this->env = 0;
delete(this->jintAccessor);
this->jintAccessor = 0;
}
private:
JNIEnv* env;
PrimitiveAccessor<jint>* jintAccessor;
void init() {
jintAccessor = new PrimitiveAccessor<jint>(
env, "java/lang/Integer",
"(I)V", &JNIEnv::CallIntMethod
);
}
};
Run Code Online (Sandbox Code Playgroud)
但在编译时我收到以下编译错误:
#include <stdlib.h>
#include <vector>
template<class Type>
class PrimitiveAccessor {
public :
PrimitiveAccessor(
JNIEnv* env, const char name[], const char ctorSig[],
Type (JNIEnv::*callTypeMethodFunction) (jobject, jmethodID)
) {
this->env = env;
this->type = (jclass)env->NewGlobalRef(env->FindClass(name));
this->callTypeMethodFunction = callTypeMethodFunction;
}
~PrimitiveAccessor(){
env->DeleteGlobalRef(this->type);
}
private:
JNIEnv* env;
jclass type;
jmethodID constructorId;
jmethodID callTypeMethodId;
Type (JNIEnv::*callTypeMethodFunction) (jobject, jmethodID);
};
class Environment {
public:
Environment(JNIEnv* env) {
this->env = env;
this->init();
}
~Environment(){
this->env = 0;
delete(this->jintAccessor);
this->jintAccessor = 0;
}
private:
JNIEnv* env;
PrimitiveAccessor<jint>* jintAccessor;
void init() {
jintAccessor = new PrimitiveAccessor<jint>(
env, "java/lang/Integer",
"(I)V", &JNIEnv::CallIntMethod
);
}
};
Run Code Online (Sandbox Code Playgroud)
我通过强制转换成员函数指针临时修复了它:
void init() {
jintAccessor = new PrimitiveAccessor<jint>(
env, "java/lang/Integer",
"(I)V", (long (JNIEnv::*) (jobject, jmethodID))&JNIEnv::CallIntMethod
);
}
Run Code Online (Sandbox Code Playgroud)
我注意到传递给模板的类型被扩展:有没有办法避免这种转换?
您需要指定匹配的类型,而不是相似的类型。
我还清理了很多不惯用的 C++。不要使用new;这不是必需的。如果您的数据成员自行清理,则不需要用户定义的析构函数。您应该在成员初始化列表中初始化数据成员,而不是在构造函数的主体中。用于std::string字符串。
#include <stdlib.h>
#include <vector>
#include <memory>
#include <string>
struct GlobalRefDeleter {
JNIEnv* env;
void operator()(jobject type) {
env->DeleteGlobalRef(type);
}
};
using JClass = std::unique_ptr<_jclass, GlobalRefDeleter>;
JClass getClass(JNIEnv* env, const std::string & name) {
return { static_cast<jclass>(env->NewGlobalRef(env->FindClass(name.c_str()))), env };
}
template<class Type>
class PrimitiveAccessor {
using CallMethod = Type (JNIEnv::*) (jobject, jmethodID, ...);
public :
PrimitiveAccessor(
JNIEnv* env, const std::string & name, const std::string & ctorSig,
CallMethod callMethod)
) : env(env), type(getClass(env, name)), callMethod(callMethod)
{
}
private:
JNIEnv* env;
JClass type;
jmethodID constructorId;
jmethodID callTypeMethodId;
CallMethod callMethod;
};
class Environment {
public:
Environment(JNIEnv* env) : env(env), jintAccessor(env, "java/lang/Integer", "(I)V", &JNIEnv::CallIntMethod)
{
}
private:
JNIEnv* env;
PrimitiveAccessor<jint> jintAccessor;
};
Run Code Online (Sandbox Code Playgroud)