Skip to content

Commit 5dbdb09

Browse files
committed
Improve custom pointer to pointer array
1 parent d88f856 commit 5dbdb09

File tree

9 files changed

+57
-46
lines changed

9 files changed

+57
-46
lines changed

example/app/core/src/main/java/com/github/xpenatan/jparser/example/app/TestLib.java

+17-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import com.github.xpenatan.jparser.example.testlib.CallbackClass;
44
import com.github.xpenatan.jparser.example.testlib.CallbackClassManual;
55
import com.github.xpenatan.jparser.example.testlib.DefaultCallbackClass;
6+
import com.github.xpenatan.jparser.example.testlib.IDLArrayTestObjectClass;
7+
import com.github.xpenatan.jparser.example.testlib.TestAttributeArrayClass;
68
import com.github.xpenatan.jparser.example.testlib.TestCallbackClass;
79
import com.github.xpenatan.jparser.example.testlib.TestConstructorClass;
810
import com.github.xpenatan.jparser.example.testlib.TestEnumClassWithinClass;
911
import com.github.xpenatan.jparser.example.testlib.TestEnumLib;
1012
import com.github.xpenatan.jparser.example.testlib.TestMethodClass;
1113
import com.github.xpenatan.jparser.example.testlib.TestObjectClass;
12-
import com.github.xpenatan.jparser.example.testlib.TestObjectClassArray;
1314
import com.github.xpenatan.jparser.example.testlib.core.enums.TestEnumWithinClass;
1415
import com.github.xpenatan.jparser.example.testlib.core.op.TestOperatorClass;
1516
import com.github.xpenatan.jparser.example.testlib.core.sub.TestNamespaceClass;
@@ -195,8 +196,19 @@ private static boolean testStaticAttributeClass() {
195196
}
196197

197198
private static boolean testAttributeArrayClass() {
198-
199-
199+
try {
200+
TestAttributeArrayClass attributeArrayClass = new TestAttributeArrayClass();
201+
TestObjectClass valueObjectArray1 = attributeArrayClass.get_valueObjectArray(0);
202+
valueObjectArray1.set_intValue01(11);
203+
TestObjectClass valueObjectArray2 = attributeArrayClass.get_valueObjectArray(0);
204+
int value = valueObjectArray2.get_intValue01();
205+
if(!(value == 11)) {
206+
throw new RuntimeException("testAttributeArrayClass !(value == 11)");
207+
}
208+
} catch(Throwable e) {
209+
e.printStackTrace();
210+
return false;
211+
}
200212
return true;
201213
}
202214

@@ -219,7 +231,7 @@ private static boolean testMethodClass() {
219231
}
220232
{
221233
TestMethodClass test = new TestMethodClass();
222-
TestObjectClassArray array = new TestObjectClassArray(2);
234+
IDLArrayTestObjectClass array = new IDLArrayTestObjectClass(2);
223235
TestObjectClass obj1 = new TestObjectClass();
224236
TestObjectClass obj2 = new TestObjectClass();
225237
obj1.set_floatValue01(20.5f);
@@ -229,7 +241,7 @@ private static boolean testMethodClass() {
229241
array.setValue(0, obj1);
230242
array.setValue(1, obj2);
231243
try {
232-
TestMethodClass.native_setMethod07(test.getNativeData().getCPointer(), array.getPointer());
244+
test.setMethod07(array);
233245
{
234246
float intValue01 = obj1.get_intValue01();
235247
if(!(intValue01 == 20)) {

example/lib/lib-build/src/main/cpp/TestLib.idl

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ interface TestObjectClass {
1919
attribute float floatValue01;
2020
};
2121

22-
interface TestObjectClassArray {
23-
void TestObjectClassArray(long size);
22+
interface IDLArrayTestObjectClass {
23+
void IDLArrayTestObjectClass(long size);
2424
void resize(long size);
2525
void clear();
2626
TestObjectClass getValue(long index);
@@ -93,7 +93,7 @@ interface TestMethodClass {
9393
void setMethod04(long intValue01, long[] intArray, float[] floatArray);
9494
void setMethod05([Const] DOMString strValue01);
9595
void setMethod06([Const] TestObjectClass pointerObject01, TestObjectClass pointerObject02, [Const, Ref] TestObjectClass refObject01, [Ref] TestObjectClass refObject02);
96-
void setMethod07(TestObjectClass pointerObjectArray);
96+
void setMethod07(TestObjectClass[] pointerObjectArray);
9797
void setMethod08(long long longLongValue01);
9898

9999
long getIntValue01();

example/lib/lib-build/src/main/cpp/custom/CustomCode.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ using TestEnumWithinClass = TestEnumClass::TestEnumWithinClass;
55
using TestEnumClassWithinClass = TestEnumClass::TestEnumClassWithinClass;
66
using TestEnumInNamespace = TestEnumNamespace::TestEnumInNamespace;
77

8-
using TestObjectClassArray = IDLArray<TestObjectClass*>;
8+
using IDLArrayTestObjectClass = IDLArray<TestObjectClass*>;

example/lib/lib-build/src/main/cpp/source/TestLib/src/TestLib.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ class TestMethodClass {
170170
this->refObject01 = refObject01;
171171
this->refObject02 = refObject02;
172172
};
173-
void setMethod07(TestObjectClass* pointerObjectArray) {
174-
this->pointerObjectArray = ((TestObjectClass** )pointerObjectArray);
173+
void setMethod07(TestObjectClass** pointerObjectArray) {
174+
this->pointerObjectArray = pointerObjectArray;
175175
// this->pointerObjectArray = &pointerObjectArray[0];
176176
// this->pointerObjectArray = &(*array[0]);
177177

jParser/cpp/src/main/java/com/github/xpenatan/jparser/cpp/CppCodeParser.java

+20-7
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ private static String getParams(NodeList<Parameter> parameters, ArrayList<IDLPar
780780
Parameter parameter = parameters.get(i);
781781
IDLParameter idlParameter = idParameters.get(i);
782782
Type type = parameter.getType();
783-
String paramName = getParam(idlParameter.idlFile, type, idlParameter.name, idlParameter.getCPPType(), idlParameter.isAny, idlParameter.isRef, idlParameter.isValue, idlParameter.isArray);
783+
String paramName = getParam(idlParameter, type);
784784
if(i > 0) {
785785
param += ", ";
786786
}
@@ -789,21 +789,34 @@ private static String getParams(NodeList<Parameter> parameters, ArrayList<IDLPar
789789
return param;
790790
}
791791

792-
private static String getParam(IDLFile idlFile, Type type, String paramName, String classType, boolean isAny, boolean isRef, boolean isValue, boolean isArray) {
792+
private static String getParam(IDLParameter idlParameter, Type type) {
793+
IDLFile idlFile = idlParameter.idlFile;
794+
String paramName = idlParameter.name;
795+
String cppType = idlParameter.getCPPType();
796+
String classType = cppType;
797+
boolean isAny = idlParameter.isAny;
798+
boolean isRef = idlParameter.isRef;
799+
boolean isValue = idlParameter.isValue;
800+
boolean isArray = idlParameter.isArray;
793801
boolean isObject = type.isClassOrInterfaceType();
802+
794803
if(isObject && !classType.equals("char*")) {
795-
String idlArrayOrNull = IDLHelper.getIDLArrayOrNull(classType);
804+
String idlArrayOrNull = IDLHelper.getIDLArrayClassOrNull(classType);
796805
if(idlArrayOrNull != null) {
797806
classType = idlArrayOrNull;
798807
}
799808

800809
paramName += "_addr";
801-
IDLClass paramClass = idlFile.getClass(classType);
802-
String cArray = IDLHelper.getCArray(classType);
803-
if(isArray && cArray != null) {
804-
paramName = "(" + cArray + ")" + paramName;
810+
if(isArray) {
811+
String idlType = cppType.replace("[]", "*");
812+
if(idlParameter.idlClassOrEnum != null && !isRef) {
813+
// Is a class object and pointer of pointer
814+
idlType += "*";
815+
}
816+
paramName = "(" + idlType + ")" + paramName;
805817
}
806818
else {
819+
IDLClass paramClass = idlFile.getClass(classType);
807820
if(paramClass != null) {
808821
classType = paramClass.getCPPName();
809822
}

jParser/idl/src/main/java/com/github/xpenatan/jparser/idl/IDLHelper.java

+6-26
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,6 @@ public static boolean isString(ClassOrInterfaceType classOrInterfaceType) {
1717
return classOrInterfaceType.getNameAsString().equals("String");
1818
}
1919

20-
public static String getCArray(String type) {
21-
if(type.equals("IDLBoolArray")) {
22-
return "bool *";
23-
}
24-
else if(type.equals("IDLIntArray")) {
25-
return "int *";
26-
}
27-
else if(type.equals("IDLLongArray")) {
28-
return "long long *";
29-
}
30-
else if(type.equals("IDLFloatArray")) {
31-
return "float *";
32-
}
33-
else if(type.equals("IDLDoubleArray")) {
34-
return "double *";
35-
}
36-
else if(type.equals("IDLByteArray")) {
37-
return "char *";
38-
}
39-
return null;
40-
}
41-
4220
public static boolean isString(Type type) {
4321
return type.toString().equals("String");
4422
}
@@ -155,15 +133,15 @@ else if(idlType.equals("octet")) {
155133
type = type + "[]";
156134
}
157135
if(useIDLArray) {
158-
String idlArrayOrNull = getIDLArrayOrNull(type);
136+
String idlArrayOrNull = getIDLArrayClassOrNull(type);
159137
if(idlArrayOrNull != null) {
160138
type = idlArrayOrNull;
161139
}
162140
}
163141
return type;
164142
}
165143

166-
public static String getIDLArrayOrNull(String type) {
144+
public static String getIDLArrayClassOrNull(String type) {
167145
// Convert array to IDL object arrays
168146
if(type.equals("int[]")) {
169147
type = "IDLIntArray";
@@ -183,8 +161,10 @@ else if(type.equals("boolean[]")) {
183161
else if(type.equals("double[]")) {
184162
type = "IDLDoubleArray";
185163
}
186-
else {
187-
return null;
164+
else if(type.endsWith("[]")) {
165+
type = type.replace("[]", "");
166+
type = "IDLArray" + type;
167+
return type;
188168
}
189169

190170
return type;

jParser/idl/src/main/java/com/github/xpenatan/jparser/idl/IDLParameter.java

+3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ public String getCPPType() {
8383
if(idlClassOrEnum != null && idlClassOrEnum.isClass()) {
8484
IDLClass aClass = idlClassOrEnum.asClass();
8585
fullType = aClass.getCPPName();
86+
if(isArray && !fullType.endsWith("[]")) {
87+
fullType += "[]";
88+
}
8689
}
8790
return IDLHelper.getCPPReturnType(fullType);
8891
}

jParser/idl/src/main/java/com/github/xpenatan/jparser/idl/IDLReader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private static void configClassType(IDLReader idlReader, ArrayList<IDLClassOrEnu
244244
}
245245
for(IDLMethod method : idlClass.methods) {
246246
for(IDLParameter parameter : method.parameters) {
247-
String idlType = parameter.idlType;
247+
String idlType = parameter.idlType.replace("[]", "");
248248
IDLClassOrEnum childClassOrEnum = idlReader.getClassOrEnum(idlType);
249249
parameter.idlClassOrEnum = childClassOrEnum;
250250
}

jParser/idl/src/main/java/com/github/xpenatan/jparser/idl/parser/IDLMethodParser.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ public static MethodCallExpr createCaller(MethodDeclaration nativeMethodDeclarat
223223
}
224224

225225
public static void setupCallerParam(NativeMethodData paramData, MethodCallExpr caller, NodeList<Parameter> methodParameters, ArrayList<IDLParameter> idlParameters) {
226+
boolean isAttribute = idlParameters == null;
227+
226228
if(!paramData.isStatic) {
227229
caller.addArgument("(long)" + IDLDefaultCodeParser.CPOINTER_METHOD);
228230
}
@@ -242,7 +244,8 @@ public static void setupCallerParam(NativeMethodData paramData, MethodCallExpr c
242244
//TODO create IDLParameter when is comming from attribute
243245
isArray = idlParameter.isArray;
244246
}
245-
if(isArray && IDLHelper.getCArray(type.asClassOrInterfaceType().getNameAsString()) != null) {
247+
if(isArray && !isAttribute) {
248+
// Only methods parameter array needs to call getPointer()
246249
String methodCall = paramName + "." + IDLDefaultCodeParser.CPOINTER_ARRAY_METHOD;
247250
paramName = "(long)(" + variableName + " != null ? " + methodCall + " : 0)";
248251
}

0 commit comments

Comments
 (0)