JFunc.java revision 427f585f726af6e3bd1fb835f26b2af9f609c483
1
2import java.util.ArrayList;
3import java.util.List;
4
5public class JFunc {
6
7    String className = "com.google.android.gles_jni.GL11Impl";
8
9    CFunc cfunc;
10    JType ftype;
11    String fname;
12
13    List<String> argNames = new ArrayList<String>();
14    List<JType> argTypes = new ArrayList<JType>();
15    List<Integer> argCIndices = new ArrayList<Integer>();
16
17    boolean hasBufferArg = false;
18    boolean hasTypedBufferArg = false;
19    ArrayList<String> bufferArgNames = new ArrayList<String>();
20
21    public JFunc(CFunc cfunc) {
22        this.cfunc = cfunc;
23    }
24
25    public CFunc getCFunc() {
26        return cfunc;
27    }
28
29    public void setName(String fname) {
30        this.fname = fname;
31    }
32
33    public String getName() {
34        return fname;
35    }
36
37    public void setType(JType ftype) {
38        this.ftype = ftype;
39    }
40
41    public JType getType() {
42        return ftype;
43    }
44
45    public void setClassName(String className) {
46        this.className = className;
47    }
48
49    public String getClassName() {
50        return className;
51    }
52
53    public boolean hasBufferArg() {
54        return hasBufferArg;
55    }
56
57    public boolean hasTypedBufferArg() {
58        return hasTypedBufferArg;
59    }
60
61    public String getBufferArgName(int index) {
62        return bufferArgNames.get(index);
63    }
64
65    public void addArgument(String argName, JType argType, int cindex) {
66        argNames.add(argName);
67        argTypes.add(argType);
68        argCIndices.add(new Integer(cindex));
69
70        if (argType.isBuffer()) {
71            hasBufferArg = true;
72            bufferArgNames.add(argName);
73        }
74        if (argType.isTypedBuffer()) {
75            hasTypedBufferArg = true;
76            bufferArgNames.add(argName);
77        }
78    }
79
80    public int getNumArgs() {
81        return argNames.size();
82    }
83
84    public int getArgIndex(String name) {
85        int len = argNames.size();
86        for (int i = 0; i < len; i++) {
87            if (name.equals(argNames.get(i))) {
88                return i;
89            }
90        }
91        return -1;
92    }
93
94    public String getArgName(int index) {
95        return argNames.get(index);
96    }
97
98    public JType getArgType(int index) {
99        return argTypes.get(index);
100    }
101
102    public int getArgCIndex(int index) {
103        return argCIndices.get(index).intValue();
104    }
105
106    public static JFunc convert(CFunc cfunc, boolean useArray) {
107        try {
108            JFunc jfunc = new JFunc(cfunc);
109            jfunc.setName(cfunc.getName());
110            jfunc.setType(JType.convert(cfunc.getType(), false));
111
112            int numArgs = cfunc.getNumArgs();
113            int numOffsets = 0;
114            for (int i = 0; i < numArgs; i++) {
115                CType cArgType = cfunc.getArgType(i);
116                if (cArgType.isTypedPointer() && useArray) {
117                    ++numOffsets;
118                }
119            }
120
121            for (int i = 0; i < numArgs; i++) {
122                String cArgName = cfunc.getArgName(i);
123                CType cArgType = cfunc.getArgType(i);
124
125                jfunc.addArgument(cArgName, JType.convert(cArgType, useArray), i);
126                if (cArgType.isTypedPointer() && useArray) {
127                    if (numOffsets > 1) {
128                        jfunc.addArgument(cArgName + "Offset", new JType("int"), i);
129                    } else {
130                        jfunc.addArgument("offset", new JType("int"), i);
131                    }
132                }
133            }
134
135            return jfunc;
136        } catch (RuntimeException e) {
137            System.err.println("Failed to convert function " + cfunc);
138            throw e;
139        }
140    }
141
142    @Override
143    public String toString() {
144        String s =  "Function " + fname + " returns " + ftype + ": ";
145        for (int i = 0; i < argNames.size(); i++) {
146            if (i > 0) {
147                s += ", ";
148            }
149            s += argTypes.get(i) + " " + argNames.get(i);
150        }
151        return s;
152    }
153
154}
155