Skip to content

Commit

Permalink
* Add Info.upcast to support class hierarchies with virtual inheri…
Browse files Browse the repository at this point in the history
…tance (pull #671)
  • Loading branch information
HGuillemet authored Apr 21, 2023
1 parent 0afb7b4 commit bc24d34
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Add `Info.upcast` to support class hierarchies with virtual inheritance ([pull #671](https://github.com/bytedeco/javacpp/pull/671))
* Pick up `@Adapter`, `@SharedPtr`, etc annotations on `allocate()` as well ([pull #668](https://github.com/bytedeco/javacpp/pull/668))
* Provide `@Virtual(subclasses=false)` to prevent `Generator` from subclassing subclasses ([pull #660](https://github.com/bytedeco/javacpp/pull/660))
* Fix `Loader.getPlatform()` detection for `linux-armhf` with Temurin JDK ([issue bytedeco/javacv#2001](https://github.com/bytedeco/javacv/issues/2001))
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/bytedeco/javacpp/annotation/Virtual.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
/** Pure (abstract) or not. */
boolean value() default false;
boolean subclasses() default true;
}
String method() default "";
}
2 changes: 2 additions & 0 deletions src/main/java/org/bytedeco/javacpp/tools/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Context {
inaccessible = c.inaccessible;
beanify = c.beanify;
objectify = c.objectify;
upcast = c.upcast;
virtualize = c.virtualize;
variable = c.variable;
infoMap = c.infoMap;
Expand All @@ -65,6 +66,7 @@ class Context {
boolean inaccessible = false;
boolean beanify = false;
boolean objectify = false;
boolean upcast = false;
boolean virtualize = false;
Declarator variable = null;
InfoMap infoMap = null;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/bytedeco/javacpp/tools/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3406,8 +3406,10 @@ void callback(Class<?> cls, Method callbackMethod, String callbackName, int allo
}

if (methodInfo != null) {
Virtual virtual = callbackMethod.getAnnotation(Virtual.class);
String methodName = (virtual != null && virtual.method().length() > 0) ? virtual.method() : methodInfo.method.getName();
out.println(" if (" + fieldName + " == NULL) {");
out.println(" " + fieldName + " = JavaCPP_getMethodID(env, " + jclasses.index(cls) + ", \"" + methodInfo.method.getName() + "\", \"(" +
out.println(" " + fieldName + " = JavaCPP_getMethodID(env, " + jclasses.index(cls) + ", \"" + methodName + "\", \"(" +
signature(methodInfo.method.getParameterTypes()) + ")" + signature(methodInfo.method.getReturnType()) + "\");");
out.println(" }");
out.println(" jmethodID mid = " + fieldName + ";");
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/org/bytedeco/javacpp/tools/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public Info(Info i) {
skip = i.skip;
skipDefaults = i.skipDefaults;
purify = i.purify;
upcast = i.upcast;
virtualize = i.virtualize;
base = i.base;
cppText = i.cppText;
Expand Down Expand Up @@ -96,7 +97,7 @@ public Info(Info i) {
* To use as keys in maps, etc, intern() must be called on instances returned from native code. */
boolean enumerate = false;
/** Outputs declarations for this class into their subclasses as well.
* Also adds methods for explicit casting, as done for multiple inheritance by default. */
* Also adds methods for upcasting, as done for multiple inheritance by default. */
boolean flatten = false;
/** Maps friend functions. Only functions having in their argument list an instance of the class they are friend
* of are currently supported. They are mapped as instance methods of the class. */
Expand All @@ -116,6 +117,9 @@ public Info(Info i) {
boolean skipDefaults = false;
/** Forces a class to be treated as if it were abstract. */
boolean purify = false;
/** Whether a static_cast is needed to upcast a pointer to this cppName.
* This is necessary for polymorphic classes that are virtually inherited from. */
boolean upcast = false;
/** Annotates virtual functions with @{@link Virtual} and adds appropriate constructors. */
boolean virtualize = false;
/** Allows to override the base class of {@link #pointerTypes}. Defaults to {@link Pointer}. */
Expand Down Expand Up @@ -156,6 +160,8 @@ public Info(Info i) {
public Info skipDefaults(boolean skipDefaults) { this.skipDefaults = skipDefaults; return this; }
public Info purify() { this.purify = true; return this; }
public Info purify(boolean purify) { this.purify = purify; return this; }
public Info upcast() { this.upcast = true; return this; }
public Info upcast(boolean upcast) { this.upcast = upcast; return this; }
public Info virtualize() { this.virtualize = true; return this; }
public Info virtualize(boolean virtualize) { this.virtualize = virtualize; return this; }
public Info base(String base) { this.base = base; return this; }
Expand Down
Loading

0 comments on commit bc24d34

Please sign in to comment.