Skip to content

Commit

Permalink
Add cpp callback overload solution
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Sep 1, 2024
1 parent 2cef09b commit 7dc4150
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions example/lib/lib-build/src/main/cpp/TestLib.idl
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ interface CallbackClassImpl {
unsigned long onUnsignedIntCallback(unsigned long unsignedInt);
[Const] unsigned short onUnsignedShortCallback(unsigned short unsignedShort);
[Const] void onAnyCallback(any anyPtr);
[Const] void onAnyCallback(any anyPtr, long value);
[Const] void onLongLongValue(unsigned long long longLongValue);
};
CallbackClassImpl implements CallbackClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ class CallbackClass
virtual unsigned int onUnsignedIntCallback(unsigned int unsignedInt) = 0;
virtual unsigned short onUnsignedShortCallback(unsigned short unsignedShort) const = 0;
virtual void onAnyCallback(void * anyPtr) const = 0;
virtual void onAnyCallback(void * anyPtr, int value) const = 0;
virtual void onLongLongValue(unsigned long long longLongValue) const = 0;

int addInt(int a, int b)
Expand Down Expand Up @@ -310,6 +311,8 @@ class DefaultCallbackClass : public CallbackClass
}
virtual void onAnyCallback(void * anyPtr) const {
}
virtual void onAnyCallback(void * anyPtr, int value) const {
}
virtual void onLongLongValue(unsigned long long longLongValue) const {
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ private String generateSetupCallbackMethod(IDLClass idlClass, MethodDeclaration

String staticVariables = "";
String methodIds = "";
String methodName = callbackDeclaration.getNameAsString();
String callbackMethodName = callbackDeclaration.getNameAsString();

for(int i = 0; i < methods.size(); i++) {
Pair<IDLMethod, Pair<MethodDeclaration, MethodDeclaration>> pair = methods.get(i);
Expand Down Expand Up @@ -494,11 +494,14 @@ else if(type.isClassOrInterfaceType()) {
}
}
}
String methodCode = generateMethodID(internalMethod);

paramCode = "(" + paramCode + ")" + JNITypeSignature.getJNIType(returnTypeStr);

String variable = variableTemplate.replace("[METHOD]", idlMethod.name);
String methodId = methodIdTemplate.replace("[METHOD]", idlMethod.name)
String methodName = idlMethod.name + methodCode;

String variable = variableTemplate.replace("[METHOD]", methodName);
String methodId = methodIdTemplate.replace("[METHOD]", methodName)
.replace("[CLASS_NAME]", className)
.replace("[INTERNAL_METHOD]", internalMethodName)
.replace("[PARAM_CODE]", paramCode);
Expand All @@ -507,21 +510,42 @@ else if(type.isClassOrInterfaceType()) {
}

String content = contentTemplate.replace("[VARIABLES]", staticVariables)
.replace("[METHOD]", methodName)
.replace("[METHOD]", callbackMethodName)
.replace("[CLASS_NAME]", className)
.replace("[METHOD_IDS]", methodIds);

return content;
}

private String generateMethodID(MethodDeclaration internalMethod) {
// Method is used when there is multiple overloaded methods
String methodCode = "";
NodeList<Parameter> parameters = internalMethod.getParameters();
for(int i1 = 0; i1 < parameters.size(); i1++) {
Parameter parameter = parameters.get(i1);
Type type = parameter.getType();
String typeStr = type.asString();
if(type.isPrimitiveType()) {
String jniType = JNITypeSignature.getJNIType(typeStr);
methodCode += jniType;
}
else if(type.isClassOrInterfaceType()) {
if(typeStr.equals("String")) {
methodCode += "S";
}
}
}
return methodCode;
}

private String generateMethodCallers(IDLClass idlClass, ArrayList<Pair<IDLMethod, Pair<MethodDeclaration, MethodDeclaration>>> methods) {
IDLClass callback = idlClass.callback;
String cppMethods = "";
String cppClassName = callback.name;

String methodTemplate = "" +
"virtual [RETURN_TYPE] [METHOD]([PARAMS])[CONST] {\n" +
" [RETURN]env->[CALL_METHOD](obj, [CPP_CLASS]::[METHOD]_ID[CALL_PARAMS]);\n" +
"virtual [RETURN_TYPE] [METHOD_NAME]([PARAMS])[CONST] {\n" +
" [RETURN]env->[CALL_METHOD](obj, [CPP_CLASS]::[METHOD_ID]_ID[CALL_PARAMS]);\n" +
"}\n";

for(int i = 0; i < methods.size(); i++) {
Expand All @@ -535,6 +559,8 @@ private String generateMethodCallers(IDLClass idlClass, ArrayList<Pair<IDLMethod
boolean isVoidType = type.isVoidType();
String typeStr = getCPPType(idlMethod.returnType);

String methodCode = generateMethodID(internalMethod);

String methodName = idlMethod.name;
String methodParams = "";
String callParams = "";
Expand Down Expand Up @@ -587,8 +613,9 @@ else if(!isPrimitive && !idlParameter.isValue) {
returnStr += "(" + typeStr + ")";
}
String callMethod = getCPPCallMethod(type);
String methodStr = methodTemplate.replace("[CALL_METHOD]", callMethod).replace("[CPP_CLASS]", cppClassName).replace("[METHOD]", methodName).replace("[CALL_PARAMS]", callParams).replace("[RETURN]", returnStr);
methodStr = methodStr.replace("[RETURN_TYPE]", typeStr).replace("[METHOD]", methodName).replace("[PARAMS]", methodParams).replace("[CONST]", constStr);
String methodStr = methodTemplate.replace("[CALL_METHOD]", callMethod).replace("[CPP_CLASS]", cppClassName)
.replace("[METHOD_NAME]", methodName).replace("[CALL_PARAMS]", callParams).replace("[RETURN]", returnStr).replace("[METHOD_ID]", methodName + methodCode);
methodStr = methodStr.replace("[RETURN_TYPE]", typeStr).replace("[METHOD_NAME]", methodName).replace("[PARAMS]", methodParams).replace("[CONST]", constStr);

cppMethods += methodStr;
}
Expand Down

0 comments on commit 7dc4150

Please sign in to comment.