L'utilisation d'une simple classe c++ sur Android NDK

Je suis en train d'apprendre les bases de Android NDK mais je suis coincé quand je dois l'utiliser avec une classe c++.

Je comprends comment l'utiliser avec une fonction simple, mais que dois-je faire pour être en mesure de manipuler les champs et les méthodes d'une classe c++?

Je suis en train de faire avec cette simple classe c++:

#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();
}

J'ai essayé de trouver des échantillons, mais rien pour l'instant... Peut-être que je ne suis pas en utilisant les bons mots-clés

* Mise à JOUR *

J'ai créé un wrapper Java pour le c++ de la classe Point et ajouté au fichier c++ JNI méthodes. Le code est le suivant :

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
}

Maintenant je suis coincé avec le nativeSymetrique fonction, il est dit que je ne peut pas convertir de 'Point' à 'jlong'. Quelqu'un peut-il m'aider sur ce point ? Grâce

* Mise à JOUR 2 *

RASADE résolu mes problèmes, vous n'avez pas à écrire à la main les wrappers et il semble être un bon choix pour les grandes bibliothèques.

bonne question, si vous pouvez utiliser quelque chose de "main()" que vous pouvez également écrire quelques fonctions principales qui vont appeler des fonctions de la classe , c'est très mal, mais peut-être que ça va vous aider 🙂
utilisez-vous JNI? Où est JNI wrapper dans votre code?
Oui j'ai l'intention d'utiliser JNI, mais ce code n'est qu'une simple classe c++. Je me demande où et comment je dois mettre JNI avec ce code

OriginalL'auteur Fr4nz | 2012-12-13