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;
explicitUpcast = c.explicitUpcast;
HGuillemet marked this conversation as resolved.
Show resolved Hide resolved
}

String namespace = null;
Expand All @@ -71,6 +72,7 @@ class Context {
TemplateMap templateMap = null;
List<String> usingList = null;
Map<String,String> namespaceMap = null;
boolean explicitUpcast = 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
7 changes: 7 additions & 0 deletions src/main/java/org/bytedeco/javacpp/tools/Info.java
Original file line number Diff line number Diff line change
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 an explicit static_cast is needed to upcast a pointer to this cppName.
* This is necessary for polymorphic classes that are virtually inherited from. */
boolean explicitUpcast = 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 explicitUpcast() { this.explicitUpcast = true; return this; }
public Info explicitUpcast(boolean eu) { this.explicitUpcast = eu; return this; }
}
185 changes: 154 additions & 31 deletions src/main/java/org/bytedeco/javacpp/tools/Parser.java

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/main/java/org/bytedeco/javacpp/tools/Templates.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
class Templates {

static final Pattern templatePattern = Pattern.compile("<[^<>=]*>");
static final Pattern smartPointerPattern = Pattern.compile("\\w+::(?:unique|weak|shared)_ptr<");

/** Remove template arguments from s, taking care of nested templates, default arguments (xxx<>), operator <=>, ->, etc... */
static String strip(String s) {
Expand Down Expand Up @@ -74,4 +75,8 @@ static List<String> splitNamespace(String s) {
comps.add(s.substring(start));
return comps;
}

public static boolean isSmartPointer(String cppName) {
return smartPointerPattern.matcher(cppName).lookingAt();
}
}
3 changes: 2 additions & 1 deletion src/main/java/org/bytedeco/javacpp/tools/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class Type {
int indirections = 0;
boolean anonymous = false, constExpr = false, constPointer = false, constValue = false, constructor = false,
destructor = false, operator = false, simple = false, staticMember = false, using = false,
reference = false, rvalue = false, value = false, friend = false, typedef = false, virtual = false;
reference = false, rvalue = false, value = false, friend = false, typedef = false, virtual = false,
explicitUpcast = false;
String annotations = "", cppName = "", javaName = "", javaNames[] = null;
Type[] arguments = null;
Attribute[] attributes = null;
Expand Down