Skip to content

Commit

Permalink
Support for shared pointers, fixes #57
Browse files Browse the repository at this point in the history
commit e35d696
Author: Guillaume Nodet <gnodet@gmail.com>
Date:   Tue May 5 08:58:57 2020 +0200

    Reformat code correctly

commit 844914e
Author: Guillaume Nodet <gnodet@gmail.com>
Date:   Tue May 5 08:58:45 2020 +0200

    Fix a few build issues in the example

commit 0726e13
Merge: b7277af e600e6a
Author: Guillaume Nodet <gnodet@gmail.com>
Date:   Wed Apr 1 13:20:24 2020 +0200

    Merge branch 'shared_ptr_support' of https://github.com/ossdev07/hawtjni into ossdev07-shared_ptr_support

commit e600e6a
Author: ossdev07 <ossdev@puresoftware.com>
Date:   Mon Mar 30 13:20:18 2020 +0000

    haetjni - shared_ptr_support: Improved shared pointer code generation and resolved review comment

    Signed-off-by: ossdev07 <ossdev@puresoftware.com>

commit 968ef79
Author: ossdev07 <39188636+ossdev07@users.noreply.github.com>
Date:   Thu Nov 14 12:51:44 2019 +0530

    Update StructsGenerator.java

commit 8ec40c5
Author: ossdev07 <39188636+ossdev07@users.noreply.github.com>
Date:   Thu Nov 14 12:45:16 2019 +0530

    Update foo.h

commit 44bb822
Author: ossdev07 <39188636+ossdev07@users.noreply.github.com>
Date:   Thu Nov 14 12:42:19 2019 +0530

    Update foo.cpp

commit 7c4d2f3
Author: ossdev07 <39188636+ossdev07@users.noreply.github.com>
Date:   Thu Nov 14 12:38:44 2019 +0530

    Update Example.java

commit a7af3f9
Author: ossdev07 <ossdev@puresoftware.com>
Date:   Fri Aug 23 10:23:00 2019 +0000

    Hawtjni: added Hawtjni example for shared_ptr

    Signed-off-by: ossdev07 <ossdev@puresoftware.com>

commit 12c8b29
Author: ossdev07 <39188636+ossdev07@users.noreply.github.com>
Date:   Mon Jul 1 17:41:54 2019 +0530

    Update StructsGenerator.java

commit 1e3eb26
Author: ossdev07 <39188636+ossdev07@users.noreply.github.com>
Date:   Mon Jul 1 17:35:41 2019 +0530

    Update ReflectField.java

commit 88bc56b
Author: ossdev07 <39188636+ossdev07@users.noreply.github.com>
Date:   Tue Jun 18 16:21:36 2019 +0530

    Update StructsGenerator.java

commit f91adf2
Author: ossdev07 <39188636+ossdev07@users.noreply.github.com>
Date:   Tue Jun 18 16:19:14 2019 +0530

    Update ReflectField.java

commit 9637909
Author: ossdev07 <39188636+ossdev07@users.noreply.github.com>
Date:   Tue Jun 18 12:44:09 2019 +0530

    Update StructsGenerator.java

commit c72b806
Author: ossdev <ossdev@puresoftware.com>
Date:   Mon Apr 29 19:58:40 2019 +0530

    Hawtjni: Provided the support of shared pointer with example

    Signed-off-by: ossdev <ossdev@puresoftware.com>
  • Loading branch information
gnodet committed May 5, 2020
1 parent 5b4e5ad commit c00e2d2
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 28 deletions.
4 changes: 4 additions & 0 deletions hawtjni-example/src/main/java/test/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public static final void main(String args[]) {
public static int O_WRONLY;
@JniField(flags={CONSTANT})
public static int O_RDWR;


@JniMethod(cast="void *")
public static final native long malloc(
Expand Down Expand Up @@ -225,6 +226,9 @@ static public class bar {
@JniField(getter = "get_d()", setter = "set_d()", flags = { GETTER_NONMEMBER, SETTER_NONMEMBER })
private float d;

@JniField(getter = "get_sp()", setter = "set_sp()", flags={ SHARED_PTR, GETTER_NONMEMBER, SETTER_NONMEMBER })
private long CheckStr;

@Override
public int hashCode() {
final int prime = 31;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ float get_d(struct foo *arg) {
void set_d(struct foo *arg, float d) {
}

std::shared_ptr<intptr_t> get_sp(long CheckStr) {
return std::make_shared<intptr_t> (CheckStr);
}
void set_sp(struct foo *arg, std::shared_ptr<intptr_t> ptr) {
}

void print_foo(struct foo *arg) {
printf("foo@%p: { a: %d, b: %d, c: \"%s\", prev: @%p, d: %f}\n", arg, arg->a, (int)arg->b, arg->c, arg->prev, get_d(arg));
printf("foo@%p: { a: %d, b: %d, c: \"%s\", prev: @%p, d: %f, Checkstr: %p}\n", arg, arg->a, (int)arg->b, arg->c, arg->prev, get_d(arg), get_sp(arg->CheckStr).get());
}

long foowork(struct foo **arg, int count) {
Expand All @@ -35,13 +41,13 @@ void callmeback(void (*thecallback)(int number)) {
}

struct foo * foo_add(struct foo *arg, int count) {
return arg+count;
return arg+count;
}

char * char_add(char *arg, int count) {
return arg+count;
return arg+count;
}

void passingtheenv (const char *who, JNIEnv *env) {
printf("%s, the JNIEnv is at: %x\n", who, env);
printf("%s, the JNIEnv is at: %p\n", who, env);
}
9 changes: 7 additions & 2 deletions hawtjni-example/src/main/native-package/src/foo.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define INCLUDED_FOO_H

#include <stdlib.h>
#include <memory>
#include "jni.h"

#ifdef __cplusplus
Expand All @@ -18,9 +19,10 @@ extern "C" {

struct foo {
int a;
size_t b;
char c[20];
size_t b;
char c[20];
struct foo *prev;
long CheckStr;
};

typedef struct _point {
Expand Down Expand Up @@ -55,4 +57,7 @@ void passingtheenv (const char *who, JNIEnv *env);
} /* extern "C" */
#endif

std::shared_ptr<intptr_t> get_sp(long CheckStr);
void set_sp(struct foo *arg, std::shared_ptr<intptr_t>);

#endif /* INCLUDED_FOO_H */
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define HAVE_STRINGS_H 1
#endif
#endif

#include <memory>
#ifdef __APPLE__
#import <objc/objc-runtime.h>
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public void generateCopyright() {

public void generateIncludes() {
if (header) {
outputln("#include \""+getOutputName()+".h\"");
outputln("#include \"" + getOutputName() + ".h\"");
} else {
outputln("#include \""+getOutputName()+".h\"");
outputln("#include \"" + getOutputName() + ".h\"");
outputln("#include \"hawtjni.h\"");
outputln("#include \""+getOutputName()+"_structs.h\"");
outputln("#include \"" + getOutputName() + "_structs.h\"");
}
outputln();
}
Expand Down Expand Up @@ -100,13 +100,13 @@ void generateSourceFile(JNIClass clazz) {

void generateSourceStart(JNIClass clazz) {
String conditional = clazz.getConditional();
if (conditional!=null) {
outputln("#if "+conditional);
if (conditional != null) {
outputln("#if " + conditional);
}
}

void generateSourceEnd(JNIClass clazz) {
if (clazz.getConditional()!=null) {
if (clazz.getConditional() != null) {
outputln("#endif");
}
}
Expand All @@ -120,11 +120,11 @@ void generateGlobalVar(JNIClass clazz) {
}

void generateBlankMacros(JNIClass clazz) {
if (clazz.getConditional()==null) {

if (clazz.getConditional() == null) {
return;
}

String simpleName = clazz.getSimpleName();
outputln("#else");
output("#define cache");
Expand Down Expand Up @@ -278,16 +278,18 @@ void generateGetFields(JNIClass clazz) {
}
}
List<JNIField> fields = clazz.getDeclaredFields();
int sharePtrIndex = 0;
for (JNIField field : fields) {
if (ignoreField(field))
continue;
String conditional = field.getConditional();
if (conditional!=null) {
outputln("#if "+conditional);
if (conditional != null) {
outputln("#if " + conditional);
}
JNIType type = field.getType(), type64 = field.getType64();
String simpleName = type.getSimpleName();
JNIFieldAccessor accessor = field.getAccessor();
boolean allowConversion = !type.equals(type64);
output("\t");
if (type.isPrimitive()) {
if (!accessor.isNonMemberSetter())
Expand All @@ -301,7 +303,11 @@ void generateGetFields(JNIClass clazz) {
output(accessor.setter());
output(" = ");
}
output(field.getCast());
if (field.isSharedPointer())
output("std::make_shared<" + type.getTypeSignature2(allowConversion) + ">(");
else
output(field.getCast());

if (field.isPointer()) {
output("(intptr_t)");
}
Expand All @@ -319,12 +325,19 @@ void generateGetFields(JNIClass clazz) {
output(field.getDeclaringClass().getSimpleName());
output("Fc.");
output(field.getName());
if (field.isSharedPointer())
output(")");
if (accessor.isMethodSetter())
output(")");
output(");");
} else if (type.isArray()) {
JNIType componentType = type.getComponentType(), componentType64 = type64.getComponentType();
if (componentType.isPrimitive()) {
if (field.isSharedPointer()) {
output("(&");
output("lpStruct->" + accessor);
output("));");
}
outputln("{");
output("\t");
output(type.getTypeSignature2(!type.equals(type64)));
Expand Down Expand Up @@ -389,7 +402,7 @@ void generateGetFields(JNIClass clazz) {
output("\t}");
}
outputln();
if (conditional!=null) {
if (conditional != null) {
outputln("#endif");
}
}
Expand All @@ -416,8 +429,8 @@ void generateGetFunction(JNIClass clazz) {
output("Fc.cached) cache");
output(simpleName);
outputln("Fields(env, lpObject);");
if( clazz.getFlag(ClassFlag.ZERO_OUT) ) {
outputln("memset(lpStruct, 0, sizeof(struct "+clazzName+"));");
if (clazz.getFlag(ClassFlag.ZERO_OUT)) {
outputln("memset(lpStruct, 0, sizeof(struct " + clazzName + "));");
}
generateGetFields(clazz);
outputln("\treturn lpStruct;");
Expand Down Expand Up @@ -448,12 +461,12 @@ void generateSetFields(JNIClass clazz) {
if (ignoreField(field))
continue;
String conditional = field.getConditional();
if (conditional!=null) {
outputln("#if "+conditional);
if (conditional != null) {
outputln("#if " + conditional);
}
JNIType type = field.getType(), type64 = field.getType64();
boolean allowConversion = !type.equals(type64);

String simpleName = type.getSimpleName();
JNIFieldAccessor accessor = field.getAccessor();
if (type.isPrimitive()) {
Expand All @@ -472,8 +485,8 @@ void generateSetFields(JNIClass clazz) {
output("Fc.");
output(field.getName());
output(", ");
output("("+type.getTypeSignature2(allowConversion)+")");
if( field.isPointer() ) {
output("(" + type.getTypeSignature2(allowConversion) + ")");
if (field.isPointer()) {
output("(intptr_t)");
}
if (!accessor.isNonMemberGetter())
Expand All @@ -483,10 +496,15 @@ void generateSetFields(JNIClass clazz) {
output(getterStart + "(");
if (accessor.isNonMemberGetter())
output("lpStruct");
if (field.isSharedPointer())
output("->" + field.getName());
output(")");
} else {
output(accessor.getter());
}
if (field.isSharedPointer()) {
output(".get()");
}
output(");");
} else if (type.isArray()) {
JNIType componentType = type.getComponentType(), componentType64 = type64.getComponentType();
Expand Down Expand Up @@ -563,7 +581,7 @@ void generateSetFields(JNIClass clazz) {
output("\t}");
}
outputln();
if (conditional!=null) {
if (conditional != null) {
outputln("#endif");
}
}
Expand Down Expand Up @@ -610,3 +628,4 @@ boolean hasNonIgnoredFields(JNIClass clazz) {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ public interface JNIField {
public String getConditional();
public boolean ignore();

public boolean isSharedPointer();
public boolean isPointer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ public boolean isPointer() {
return getFlag(POINTER_FIELD) || ( type.getWrappedClass() == Long.TYPE && getCast().endsWith("*") );
}

public boolean isSharedPointer() {
if (annotation == null) {
return false;
}
return getFlag(SHARED_PTR);
}

public String getConditional() {
String parentConditional = getDeclaringClass().getConditional();
String myConditional = annotation == null ? null : emptyFilter(annotation.conditional());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public enum FieldFlag {
*/
POINTER_FIELD,

/**
* Indicate that the field is a shared pointer.
*/

SHARED_PTR,

/**
* Indicate that the getter method used is not part of
* the structure. Useful for using wrappers to access
Expand Down

0 comments on commit c00e2d2

Please sign in to comment.