jna

2020-08-14  本文已影响0人  hehehehe

cpp

#ifndef MYCLASS_H
#define MYCLASS_H

class MyClass {
private:
    int m_nb;
public:
    MyClass(int nb);
    int getValue() const;
    void increment();
};
#endif

#include "cpplib/MyClass.h"

MyClass::MyClass(int nb)
  : m_nb(nb)
{
}

int MyClass::getValue() const
{
    return m_nb;
}
void MyClass::increment()
{
    m_nb++;
}

jna

package edu.nyu.cpptest.cpplib;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;

public class MyClass {
    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary)Native.loadLibrary("cpplib_shared",
                CLibrary.class);
        Pointer MyClass_ctor(int nb);
        int MyClass_getValue(Pointer self);
        void MyClass_increment(Pointer self);
    }

    private Pointer self;
    public MyClass(int nb) {
        self = CLibrary.INSTANCE.MyClass_ctor(nb);
    }
    public int getValue(){
        return CLibrary.INSTANCE.MyClass_getValue(self);
    }
    public void increment(){
        CLibrary.INSTANCE.MyClass_increment(self);
    }
}

#include "cpplib/MyClass.h"

#ifdef _WIN32
#   define DLL_EXPORT __declspec(dllexport)
#else
#   define DLL_EXPORT
#endif

extern "C" DLL_EXPORT MyClass *MyClass_ctor(int nb);

MyClass *MyClass_ctor(int nb)
{
    return new MyClass(nb);
}

extern "C" DLL_EXPORT int MyClass_getValue(const MyClass *self)
{
    return self->getValue();
}

extern "C" DLL_EXPORT void MyClass_increment(MyClass *self)
{
    self->increment();
}

jni

package edu.nyu.cpptest.cpplib;

public class MyClass {

    static {
        System.loadLibrary("cpplib_jni");
    }

    private long self_ptr;

    public MyClass(int nb)
    {
        init(nb);
    }

    private native void init(int nb);

    public native int getValue();

    public native void increment();

    protected native void finalize();

}

#include "cpplib/MyClass.h"
#include "MyClass_jni.h"

static jfieldID _get_self_id(JNIEnv *env, jobject thisObj)
{
    static int init = 0;
    static jfieldID fidSelfPtr;
    if(!init)
    {
        jclass thisClass = env->GetObjectClass(thisObj);
        fidSelfPtr = env->GetFieldID(thisClass, "self_ptr", "J");
    }
    return fidSelfPtr;
}

static MyClass *_get_self(JNIEnv *env, jobject thisObj)
{
    jlong selfPtr = env->GetLongField(thisObj, _get_self_id(env, thisObj));
    return *(MyClass**)&selfPtr;
}

static void _set_self(JNIEnv *env, jobject thisObj, MyClass *self)
{
    jlong selfPtr = *(jlong*)&self;
    env->SetLongField(thisObj, _get_self_id(env, thisObj), selfPtr);
}


JNIEXPORT void JNICALL Java_edu_nyu_cpptest_cpplib_MyClass_init(JNIEnv *env, jobject thisObj, jint nb)
{
    MyClass *self = new MyClass(nb);
    _set_self(env, thisObj, self);
}

JNIEXPORT jint JNICALL Java_edu_nyu_cpptest_cpplib_MyClass_getValue(JNIEnv *env, jobject thisObj)
{
    MyClass *self = _get_self(env, thisObj);
    return self->getValue();
}

JNIEXPORT void JNICALL Java_edu_nyu_cpptest_cpplib_MyClass_increment(JNIEnv *env, jobject thisObj)
{
    MyClass *self = _get_self(env, thisObj);
    self->increment();
}

JNIEXPORT void JNICALL Java_edu_nyu_cpptest_cpplib_MyClass_finalize(JNIEnv *env, jobject thisObj)
{
    MyClass *self = _get_self(env, thisObj);
    if(self != NULL)
    {
        delete self;
        _set_self(env, thisObj, NULL);
    }
}

上一篇下一篇

猜你喜欢

热点阅读