Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicitUpcast #671

Merged
merged 13 commits into from
Apr 21, 2023
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 javaName() default "";
HGuillemet marked this conversation as resolved.
Show resolved Hide resolved
}
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 @@ -53,6 +53,7 @@ class Context {
templateMap = c.templateMap;
usingList = c.usingList;
namespaceMap = c.namespaceMap;
upcast = c.upcast;
}

String namespace = null;
Expand All @@ -71,6 +72,7 @@ class Context {
TemplateMap templateMap = null;
List<String> usingList = null;
Map<String,String> namespaceMap = null;
boolean upcast = false;

/** Return all likely combinations of namespaces and template arguments for this C++ type */
String[] qualify(String cppName) {
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 virtualAnnotation = callbackMethod.getAnnotation(Virtual.class);
String javaName = (virtualAnnotation != null && virtualAnnotation.javaName().length() > 0) ? virtualAnnotation.javaName() : 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) + ", \"" + javaName + "\", \"(" +
signature(methodInfo.method.getParameterTypes()) + ")" + signature(methodInfo.method.getReturnType()) + "\");");
out.println(" }");
out.println(" jmethodID mid = " + fieldName + ";");
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/bytedeco/javacpp/tools/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,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 Down Expand Up @@ -124,6 +124,11 @@ public Info(Info i) {
String cppText = null;
/** Outputs the given code, instead of the result parsed from the declaration of C++ identifiers. */
String javaText = null;
/** 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;



public Info cppNames(String... cppNames) { this.cppNames = cppNames; return this; }
public Info javaNames(String... javaNames) { this.javaNames = javaNames; return this; }
Expand Down Expand Up @@ -161,4 +166,6 @@ public Info(Info i) {
public Info base(String base) { this.base = base; return this; }
public Info cppText(String cppText) { this.cppText = cppText; return this; }
public Info javaText(String javaText) { this.javaText = javaText; return this; }
public Info upcast() { this.upcast = true; return this; }
public Info upcast(boolean eu) { this.upcast = eu; return this; }
}
Loading