From 7dc4150cf7cd4fb0110b068cec5d5f874b1c2614 Mon Sep 17 00:00:00 2001 From: Natan Date: Sun, 1 Sep 2024 20:46:27 -0300 Subject: [PATCH] Add cpp callback overload solution --- .../lib/lib-build/src/main/cpp/TestLib.idl | 1 + .../src/main/cpp/source/TestLib/src/TestLib.h | 3 ++ .../xpenatan/jparser/cpp/CppCodeParser.java | 43 +++++++++++++++---- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/example/lib/lib-build/src/main/cpp/TestLib.idl b/example/lib/lib-build/src/main/cpp/TestLib.idl index 328e9d2..542be58 100644 --- a/example/lib/lib-build/src/main/cpp/TestLib.idl +++ b/example/lib/lib-build/src/main/cpp/TestLib.idl @@ -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; diff --git a/example/lib/lib-build/src/main/cpp/source/TestLib/src/TestLib.h b/example/lib/lib-build/src/main/cpp/source/TestLib/src/TestLib.h index c08af8b..270d806 100644 --- a/example/lib/lib-build/src/main/cpp/source/TestLib/src/TestLib.h +++ b/example/lib/lib-build/src/main/cpp/source/TestLib/src/TestLib.h @@ -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) @@ -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 { } }; diff --git a/jParser/cpp/src/main/java/com/github/xpenatan/jparser/cpp/CppCodeParser.java b/jParser/cpp/src/main/java/com/github/xpenatan/jparser/cpp/CppCodeParser.java index 0bbf2f1..6fce04a 100644 --- a/jParser/cpp/src/main/java/com/github/xpenatan/jparser/cpp/CppCodeParser.java +++ b/jParser/cpp/src/main/java/com/github/xpenatan/jparser/cpp/CppCodeParser.java @@ -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> pair = methods.get(i); @@ -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); @@ -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 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>> 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++) { @@ -535,6 +559,8 @@ private String generateMethodCallers(IDLClass idlClass, ArrayList