Sto cercando di imparare le basi di Android NDK ma sono bloccato quando devo usarlo con una classe C++.Utilizzare una semplice classe C++ in Android NDK
Capisco come usarlo con una semplice funzione, ma cosa dovrei fare per essere in grado di manipolare i campi ei metodi di una classe C++?
che sto cercando di farlo con questa semplice C++ classe:
#include <cstdlib>
#include <jni.h>
using namespace std;
class Point {
int x, y; // coordonnées du point
public:
Point() {
this->x = 0;
this->y = 0;
}
Point(int x, int y) {
this->x = x;
this->y = y;
}
int getX() const {
return x;
}
int getY() const {
return y;
}
Point symetrique() const {
return Point(-x, -y);
}
bool operator ==(const Point &p) const {
return this->x == p.getX() && this->y == p.getY();
}
};
extern "C" {
JNIEXPORT jlong JNICALL Java_com_example_jnipoint_JPoint_createPoint__
(JNIEnv *, jobject);
JNIEXPORT jlong JNICALL Java_com_example_jnipoint_JPoint_createPoint__II
(JNIEnv *, jobject, jint, jint);
JNIEXPORT jint JNICALL Java_com_example_jnipoint_JPoint_nativeGetX
(JNIEnv *, jobject, jlong);
JNIEXPORT jint JNICALL Java_com_example_jnipoint_JPoint_nativeGetY
(JNIEnv *, jobject, jlong);
JNIEXPORT jlong JNICALL Java_com_example_jnipoint_JPoint_nativeSymetrique
(JNIEnv *, jobject, jlong);
};
JNIEXPORT jlong JNICALL Java_com_example_jnipoint_JPoint_createPoint__(JNIEnv* env, jobject thiz) {
return (jlong)(new Point());
}
JNIEXPORT jlong JNICALL Java_com_example_jnipoint_JPoint_createPoint__II(JNIEnv* env, jobject thiz, jint x, jint y) {
return (jlong)(new Point(x, y));
}
JNIEXPORT jint JNICALL Java_com_example_jnipoint_JPoint_nativeGetX(JNIEnv* env, jobject thiz, jlong nativePointer) {
return ((Point*)nativePointer)->getX();
}
JNIEXPORT jint JNICALL Java_com_example_jnipoint_JPoint_nativeGetY(JNIEnv* env, jobject thiz, jlong nativePointer) {
return ((Point*)nativePointer)->getY();
}
jlong Java_com_example_jnipoint_JPoint_nativeSymetrique(JNIEnv* env, jobject thiz, jlong nativePointer) {
return ((Point*)nativePointer)->symetrique();
}
ho cercato di trovare i campioni, ma nulla finora ... Forse non sto usando le parole chiave giuste
* AGGIORNAMENTO *
ho creato un wrapper Java per il C++ classe Point e aggiunto al C++ metodi di file JNI. Il codice è il seguente:
public class JPoint {
private long nativePointer;
public JPoint() {
nativePointer = createPoint();
}
public JPoint(int x, int y) {
nativePointer = createPoint(x, y);
}
public int getX() {
return nativeGetX(nativePointer);
}
public int getY() {
return nativeGetY(nativePointer);
}
public JPoint symetrique() {
JPoint tmp = new JPoint();
tmp.nativePointer = nativeSymetrique(nativePointer);
return tmp;
}
// TODO
/*public boolean equals(Object o) {
return nativeEquals(o);
}*/
private native long createPoint(); // Void constructor
private native long createPoint(int x, int y);
private native int nativeGetX(long nativePointer);
private native int nativeGetY(long nativePointer);
private native long nativeSymetrique(long nativePointer);
//private native boolean nativeEquals(Object p); TODO
}
adesso sto stucked con la funzione nativeSymetrique, si dice che non posso convertire il 'punto' a 'jlong'. Qualcuno può aiutarmi su questo? Grazie
* UPDATE 2 *
SWIG risolto i miei problemi, non c'è bisogno di scrivere a mano gli involucri e sembra essere una buona scelta per i grandi librerie.
buona domanda, se puoi usare qualcosa da "main()" allora puoi anche scrivere alcune funzioni in main che chiamano funzioni di classe, è un modo pessimo ma forse ti aiuterà :) –
usi JNI? Dov'è il wrapper JNI nel tuo codice? – dilix
Sì, ho intenzione di usare JNI ma questo codice è solo una semplice classe C++. Mi chiedo dove e come dovrei mettere JNI con quel codice – Fr4nz