small refactoring in the Java binding

This commit is contained in:
kr.angelov
2013-08-27 22:37:31 +00:00
parent 8d82dc66bf
commit 75c1100575
4 changed files with 26 additions and 30 deletions

View File

@@ -51,16 +51,10 @@ j2gu_string(JNIEnv *env, jstring s, GuPool* pool) {
return gu_str_string(str, pool); return gu_str_string(str, pool);
} }
static PgfPGF* static void*
get_pgf(JNIEnv *env, jobject self) { get_ref(JNIEnv *env, jobject self) {
jfieldID grId = (*env)->GetFieldID(env, (*env)->GetObjectClass(env, self), "gr", "J"); jfieldID refId = (*env)->GetFieldID(env, (*env)->GetObjectClass(env, self), "ref", "J");
return (PgfPGF*) (*env)->GetLongField(env, self, grId); return (void*) (*env)->GetLongField(env, self, refId);
}
static PgfPGF*
get_concr(JNIEnv *env, jobject self) {
jfieldID concrId = (*env)->GetFieldID(env, (*env)->GetObjectClass(env, self), "concr", "J");
return (PgfPGF*) (*env)->GetLongField(env, self, concrId);
} }
static void static void
@@ -123,7 +117,7 @@ Java_org_grammaticalframework_pgf_PGF_readPGF(JNIEnv *env, jclass cls, jstring s
JNIEXPORT jstring JNICALL JNIEXPORT jstring JNICALL
Java_org_grammaticalframework_pgf_PGF_getAbstractName(JNIEnv* env, jobject self) Java_org_grammaticalframework_pgf_PGF_getAbstractName(JNIEnv* env, jobject self)
{ {
return gu2j_string(env, pgf_abstract_name(get_pgf(env, self))); return gu2j_string(env, pgf_abstract_name(get_ref(env, self)));
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
@@ -171,7 +165,7 @@ Java_org_grammaticalframework_pgf_PGF_getLanguages(JNIEnv* env, jobject self)
if (!languages) if (!languages)
return NULL; return NULL;
PgfPGF* pgf = get_pgf(env, self); PgfPGF* pgf = get_ref(env, self);
GuPool* tmp_pool = gu_local_pool(); GuPool* tmp_pool = gu_local_pool();
@@ -193,7 +187,7 @@ Java_org_grammaticalframework_pgf_PGF_getLanguages(JNIEnv* env, jobject self)
JNIEXPORT jstring JNICALL JNIEXPORT jstring JNICALL
Java_org_grammaticalframework_pgf_Concr_getName(JNIEnv* env, jobject self) Java_org_grammaticalframework_pgf_Concr_getName(JNIEnv* env, jobject self)
{ {
return gu2j_string(env, pgf_concrete_name(get_concr(env, self))); return gu2j_string(env, pgf_concrete_name(get_ref(env, self)));
} }
JNIEXPORT jobject JNICALL JNIEXPORT jobject JNICALL
@@ -210,7 +204,7 @@ Java_org_grammaticalframework_pgf_Parser_parse
PgfLexer *lexer = pgf_new_simple_lexer(rdr, pool); PgfLexer *lexer = pgf_new_simple_lexer(rdr, pool);
GuEnum* res = GuEnum* res =
pgf_parse(get_concr(env, concr), startCat, lexer, pool, out_pool); pgf_parse(get_ref(env, concr), startCat, lexer, pool, out_pool);
if (res == NULL) { if (res == NULL) {
PgfToken tok = PgfToken tok =

View File

@@ -14,10 +14,10 @@ public class Concr {
// private stuff // private stuff
private PGF gr; private PGF gr;
public long concr; public long ref;
private Concr(PGF gr, long concr) { private Concr(PGF gr, long ref) {
this.gr = gr; this.gr = gr;
this.concr = concr; this.ref = ref;
} }
} }

View File

@@ -3,20 +3,22 @@ package org.grammaticalframework.pgf;
import java.util.*; import java.util.*;
class ExprIterator implements Iterator<ExprProb> { class ExprIterator implements Iterator<ExprProb> {
private Concr concr;
private Pool pool, out_pool; private Pool pool, out_pool;
private long enumRef; private long ref;
private ExprProb ep; private ExprProb ep;
private boolean fetched; private boolean fetched;
public ExprIterator(long pool, long out_pool, long enumRef) { public ExprIterator(Concr concr, long pool, long out_pool, long ref) {
this.pool = new Pool(pool); this.concr = concr;
this.pool = new Pool(pool);
this.out_pool = new Pool(out_pool); this.out_pool = new Pool(out_pool);
this.enumRef = enumRef; this.ref = ref;
this.ep = null; this.ep = null;
this.fetched = false; this.fetched = false;
} }
private native ExprProb fetchExprProb(long enumRef, Pool out_pool); private native ExprProb fetchExprProb(long ref, Pool out_pool);
private void fetch() { private void fetch() {
if (!fetched) { if (!fetched) {

View File

@@ -30,18 +30,18 @@ public class PGF {
private static native void free(long pool); private static native void free(long pool);
private long pool; private long pool;
private long gr; private long ref;
private PGF(long pool, long gr) { private PGF(long pool, long ref) {
this.pool = pool; this.pool = pool;
this.gr = gr; this.ref = ref;
} }
protected void finalize () throws Throwable { protected void finalize () throws Throwable {
if (pool != 0) { if (pool != 0) {
free(pool); free(pool);
pool = 0; pool = 0;
gr = 0; ref = 0;
} }
} }