Skip to content

Commit

Permalink
* Add Generator support for enum classes with boolean values (…
Browse files Browse the repository at this point in the history
…issue #388)
  • Loading branch information
saudet committed Mar 31, 2020
1 parent 01c7278 commit a2fcb16
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Add `Generator` support for `enum` classes with `boolean` values ([issue #388](https://github.com/bytedeco/javacpp/issues/388))
* Fix `Parser` outputting invalid Java code for `enum` of `boolean`, `byte`, and `short` types ([issue #388](https://github.com/bytedeco/javacpp/issues/388))
* Pick up in `Generator` the `@Namespace` annotation from paired method too for global getters and setters ([issue #387](https://github.com/bytedeco/javacpp/issues/387))
* Add presets for `jnijavacpp` and `javacpp-platform` artifact to fix issues at load time ([issue bytedeco/javacv#1305](https://github.com/bytedeco/javacv/issues/1305))
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/bytedeco/javacpp/tools/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public Generator(Logger logger, Properties properties, String encoding) {
this.encoding = encoding;
}

static enum BooleanEnum { BOOLEAN; boolean value; }
static enum ByteEnum { BYTE; byte value; }
static enum ShortEnum { SHORT; short value; }
static enum IntEnum { INT; int value; }
Expand Down Expand Up @@ -452,6 +453,7 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, boolean conver
out.println("static jfieldID JavaCPP_deallocatorFID = NULL;");
out.println("static jfieldID JavaCPP_ownerAddressFID = NULL;");
if (declareEnums) {
out.println("static jfieldID JavaCPP_booleanValueFID = NULL;");
out.println("static jfieldID JavaCPP_byteValueFID = NULL;");
out.println("static jfieldID JavaCPP_shortValueFID = NULL;");
out.println("static jfieldID JavaCPP_intValueFID = NULL;");
Expand Down Expand Up @@ -1570,6 +1572,11 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, boolean conver
out.println(" return JNI_ERR;");
out.println(" }");
if (declareEnums) {
out.println(" JavaCPP_booleanValueFID = JavaCPP_getFieldID(env, \"" +
BooleanEnum.class.getName().replace('.', '/') + "\", \"value\", \"Z\");");
out.println(" if (JavaCPP_booleanValueFID == NULL) {");
out.println(" return JNI_ERR;");
out.println(" }");
out.println(" JavaCPP_byteValueFID = JavaCPP_getFieldID(env, \"" +
ByteEnum.class.getName().replace('.', '/') + "\", \"value\", \"B\");");
out.println(" if (JavaCPP_byteValueFID == NULL) {");
Expand Down
11 changes: 10 additions & 1 deletion src/test/java/org/bytedeco/javacpp/EnumTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 Samuel Audet
* Copyright (C) 2018-2020 Samuel Audet
*
* Licensed either under the Apache License, Version 2.0, or (at your option)
* under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -38,6 +38,13 @@
@Platform(compiler = "cpp11", include = "EnumTest.h")
public class EnumTest {

public static enum BoolEnum {
BOOL(true);

public final boolean value;
private BoolEnum(boolean v) { this.value = v; }
}

@Name("CharEnum") public static enum ByteEnum {
BYTE((byte)123);

Expand Down Expand Up @@ -77,6 +84,7 @@ public LongEnum call(ByteEnum e) {
}
}

public static native BoolEnum Char2Bool(ByteEnum e);
public static native ShortEnum Char2Short(ByteEnum e);
public static native LongEnum Int2Long(IntEnum e);
public static native LongEnum enumCallback(EnumCallback f);
Expand All @@ -94,6 +102,7 @@ public LongEnum call(ByteEnum e) {
@Test public void testEnum() {
System.out.println("Enum");

assertEquals(true, Char2Bool(ByteEnum.BYTE).value);
assertEquals(123, Char2Short(ByteEnum.BYTE).value);
assertEquals(789, Int2Long(IntEnum.INT).value);
assertEquals(101112, enumCallback(new EnumCallback()).value);
Expand Down
8 changes: 8 additions & 0 deletions src/test/resources/org/bytedeco/javacpp/EnumTest.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
enum class BoolEnum : bool {
BOOL_ENUM = 1
};

enum class CharEnum : char {
CHAR_ENUM = 42
};
Expand All @@ -14,6 +18,10 @@ enum /* no class */ LongEnum : long long {
LONG_ENUM = 121110
};

BoolEnum Char2Bool(CharEnum e) {
return (BoolEnum)e;
}

ShortEnum Char2Short(CharEnum e) {
return (ShortEnum)e;
}
Expand Down

0 comments on commit a2fcb16

Please sign in to comment.