1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkWrapJava.c
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include "vtkParse.h"
20 #include "vtkParseMain.h"
21 #include "vtkParseHierarchy.h"
22 #include "vtkWrap.h"
23 
24 HierarchyInfo *hierarchyInfo = NULL;
25 StringCache *stringCache = NULL;
26 int numberOfWrappedFunctions = 0;
27 FunctionInfo *wrappedFunctions[1000];
28 extern FunctionInfo *currentFunction;
29 ClassInfo *CurrentData;
30 
output_proto_vars(FILE * fp,int i)31 void output_proto_vars(FILE *fp, int i)
32 {
33   unsigned int aType =
34     (currentFunction->ArgTypes[i] & VTK_PARSE_UNQUALIFIED_TYPE);
35 
36   /* ignore void */
37   if (aType == VTK_PARSE_VOID)
38     {
39     return;
40     }
41 
42   if (currentFunction->ArgTypes[i] == VTK_PARSE_FUNCTION)
43     {
44     fprintf(fp,"jobject id0, jstring id1");
45     return;
46     }
47 
48   if ((aType == VTK_PARSE_CHAR_PTR) ||
49       (aType == VTK_PARSE_STRING) ||
50       (aType == VTK_PARSE_STRING_REF))
51     {
52     fprintf(fp,"jstring ");
53     fprintf(fp,"id%i",i);
54     return;
55     }
56 
57   if ((aType == VTK_PARSE_FLOAT_PTR) ||
58       (aType == VTK_PARSE_DOUBLE_PTR))
59     {
60     fprintf(fp,"jdoubleArray ");
61     fprintf(fp,"id%i",i);
62     return;
63     }
64 
65   if ((aType == VTK_PARSE_INT_PTR) ||
66       (aType == VTK_PARSE_SHORT_PTR) ||
67       (aType == VTK_PARSE_SIGNED_CHAR_PTR) ||
68       (aType == VTK_PARSE_LONG_PTR) ||
69       (aType == VTK_PARSE_ID_TYPE_PTR) ||
70       (aType == VTK_PARSE_LONG_LONG_PTR) ||
71       (aType == VTK_PARSE___INT64_PTR))
72     {
73     fprintf(fp,"jintArray ");
74     fprintf(fp,"id%i",i);
75     return;
76     }
77 
78 
79   switch ((aType & VTK_PARSE_BASE_TYPE) & ~VTK_PARSE_UNSIGNED)
80     {
81     case VTK_PARSE_FLOAT:   fprintf(fp,"jdouble "); break;
82     case VTK_PARSE_DOUBLE:   fprintf(fp,"jdouble "); break;
83     case VTK_PARSE_INT:   fprintf(fp,"jint "); break;
84     case VTK_PARSE_SHORT:   fprintf(fp,"jint "); break;
85     case VTK_PARSE_LONG:   fprintf(fp,"jint "); break;
86     case VTK_PARSE_ID_TYPE:   fprintf(fp,"jint "); break;
87     case VTK_PARSE_LONG_LONG:   fprintf(fp,"jint "); break;
88     case VTK_PARSE___INT64:   fprintf(fp,"jint "); break;
89     case VTK_PARSE_SIGNED_CHAR:   fprintf(fp,"jint "); break;
90     case VTK_PARSE_BOOL:   fprintf(fp,"jboolean "); break;
91     case VTK_PARSE_VOID:   fprintf(fp,"void "); break;
92     case VTK_PARSE_CHAR:   fprintf(fp,"jchar "); break;
93     case VTK_PARSE_OBJECT:   fprintf(fp,"jobject "); break;
94     case VTK_PARSE_UNKNOWN: fprintf(fp,"jint "); break;
95     }
96 
97   fprintf(fp,"id%i",i);
98 }
99 
100 /* when the cpp file doesn't have enough info use the hint file */
use_hints(FILE * fp)101 void use_hints(FILE *fp)
102 {
103   unsigned int rType =
104     (currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE);
105 
106   /* use the hint */
107   switch (rType)
108     {
109     case VTK_PARSE_UNSIGNED_CHAR_PTR:
110       /* for vtkDataWriter we want to handle this case specially */
111       if (strcmp(currentFunction->Name,"GetBinaryOutputString") ||
112           strcmp(CurrentData->Name,"vtkDataWriter"))
113         {
114         fprintf(fp,"    return vtkJavaMakeJArrayOfByteFromUnsignedChar(env,temp%i,%i);\n",
115                 MAX_ARGS, currentFunction->HintSize);
116         }
117       else
118         {
119         fprintf(fp,"    return vtkJavaMakeJArrayOfByteFromUnsignedChar(env,temp%i,op->GetOutputStringLength());\n", MAX_ARGS);
120         }
121       break;
122 
123     case VTK_PARSE_FLOAT_PTR:
124       fprintf(fp,"    return vtkJavaMakeJArrayOfDoubleFromFloat(env,temp%i,%i);\n",
125               MAX_ARGS, currentFunction->HintSize);
126       break;
127 
128     case VTK_PARSE_DOUBLE_PTR:
129       fprintf(fp,"    return vtkJavaMakeJArrayOfDoubleFromDouble(env,temp%i,%i);\n",
130               MAX_ARGS, currentFunction->HintSize);
131       break;
132 
133     case VTK_PARSE_INT_PTR:
134       fprintf(fp,"    return vtkJavaMakeJArrayOfIntFromInt(env,temp%i,%i);\n",
135               MAX_ARGS, currentFunction->HintSize);
136       break;
137 
138     case VTK_PARSE_ID_TYPE_PTR:
139       fprintf(fp,"    return vtkJavaMakeJArrayOfIntFromIdType(env,temp%i,%i);\n",
140               MAX_ARGS, currentFunction->HintSize);
141       break;
142 
143     case VTK_PARSE_LONG_LONG_PTR:
144       fprintf(fp,"    return vtkJavaMakeJArrayOfIntFromLongLong(env,temp%i,%i);\n",
145               MAX_ARGS, currentFunction->HintSize);
146       break;
147 
148     case VTK_PARSE___INT64_PTR:
149       fprintf(fp,"    return vtkJavaMakeJArrayOfIntFrom__Int64(env,temp%i,%i);\n",
150               MAX_ARGS, currentFunction->HintSize);
151       break;
152 
153     case VTK_PARSE_SIGNED_CHAR_PTR:
154       fprintf(fp,"    return vtkJavaMakeJArrayOfIntFromSignedChar(env,temp%i,%i);\n",
155               MAX_ARGS, currentFunction->HintSize);
156       break;
157 
158     case VTK_PARSE_BOOL_PTR:
159       fprintf(fp,"    return vtkJavaMakeJArrayOfIntFromBool(env,temp%i,%i);\n",
160               MAX_ARGS, currentFunction->HintSize);
161       break;
162 
163     case VTK_PARSE_SHORT_PTR:
164               fprintf(fp,"    return vtkJavaMakeJArrayOfShortFromShort(env,temp%i,%i);\n",
165               MAX_ARGS, currentFunction->HintSize);
166       break;
167 
168     case VTK_PARSE_LONG_PTR:
169               fprintf(fp,"    return vtkJavaMakeJArrayOfLongFromLong(env,temp%i,%i);\n",
170               MAX_ARGS, currentFunction->HintSize);
171       break;
172 
173     case VTK_PARSE_UNSIGNED_INT_PTR:
174     case VTK_PARSE_UNSIGNED_SHORT_PTR:
175     case VTK_PARSE_UNSIGNED_LONG_PTR:
176     case VTK_PARSE_UNSIGNED_ID_TYPE_PTR:
177     case VTK_PARSE_UNSIGNED_LONG_LONG_PTR:
178     case VTK_PARSE_UNSIGNED___INT64_PTR:
179       break;
180     }
181 }
182 
return_result(FILE * fp)183 void return_result(FILE *fp)
184 {
185   unsigned int rType =
186     (currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE);
187 
188   switch (rType)
189     {
190     case VTK_PARSE_FLOAT:
191       fprintf(fp,"jdouble ");
192       break;
193     case VTK_PARSE_VOID:
194       fprintf(fp,"void ");
195       break;
196     case VTK_PARSE_CHAR:
197       fprintf(fp,"jchar ");
198       break;
199     case VTK_PARSE_DOUBLE:
200       fprintf(fp,"jdouble ");
201       break;
202     case VTK_PARSE_INT:
203     case VTK_PARSE_SHORT:
204     case VTK_PARSE_LONG:
205     case VTK_PARSE_ID_TYPE:
206     case VTK_PARSE_LONG_LONG:
207     case VTK_PARSE___INT64:
208     case VTK_PARSE_SIGNED_CHAR:
209     case VTK_PARSE_UNSIGNED_CHAR:
210     case VTK_PARSE_UNSIGNED_INT:
211     case VTK_PARSE_UNSIGNED_SHORT:
212     case VTK_PARSE_UNSIGNED_LONG:
213     case VTK_PARSE_UNSIGNED_ID_TYPE:
214     case VTK_PARSE_UNSIGNED_LONG_LONG:
215     case VTK_PARSE_UNSIGNED___INT64:
216     case VTK_PARSE_UNKNOWN:
217       fprintf(fp,"jint ");
218       break;
219     case VTK_PARSE_BOOL:
220       fprintf(fp,"jboolean ");
221       break;
222     case VTK_PARSE_CHAR_PTR:
223     case VTK_PARSE_STRING:
224     case VTK_PARSE_STRING_REF:
225       fprintf(fp,"jstring ");
226       break;
227     case VTK_PARSE_OBJECT_PTR:
228       fprintf(fp,"jlong ");
229       break;
230     case VTK_PARSE_FLOAT_PTR:
231     case VTK_PARSE_DOUBLE_PTR:
232     case VTK_PARSE_UNSIGNED_CHAR_PTR:
233     case VTK_PARSE_INT_PTR:
234     case VTK_PARSE_SHORT_PTR:
235     case VTK_PARSE_LONG_PTR:
236     case VTK_PARSE_ID_TYPE_PTR:
237     case VTK_PARSE_LONG_LONG_PTR:
238     case VTK_PARSE___INT64_PTR:
239     case VTK_PARSE_SIGNED_CHAR_PTR:
240     case VTK_PARSE_BOOL_PTR:
241     case VTK_PARSE_UNSIGNED_ID_TYPE_PTR:
242     case VTK_PARSE_UNSIGNED_LONG_LONG_PTR:
243     case VTK_PARSE_UNSIGNED___INT64_PTR:
244       fprintf(fp,"jarray ");
245       break;
246     }
247 }
248 
249 
output_temp(FILE * fp,int i,unsigned int aType,const char * Id,int aCount)250 void output_temp(FILE *fp, int i, unsigned int aType, const char *Id,
251                  int aCount)
252 {
253   /* handle VAR FUNCTIONS */
254   if (aType == VTK_PARSE_FUNCTION)
255     {
256     fprintf(fp,"  vtkJavaVoidFuncArg *temp%i = new vtkJavaVoidFuncArg;\n",i);
257     return;
258     }
259 
260   /* ignore void */
261   if ((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_VOID)
262     {
263     return;
264     }
265 
266   /* for const * return types prototype with const */
267   if ((i == MAX_ARGS) &&
268       ((aType & VTK_PARSE_INDIRECT) != 0) &&
269       ((aType & VTK_PARSE_CONST) != 0))
270     {
271     fprintf(fp,"  const ");
272     }
273   else
274     {
275     fprintf(fp,"  ");
276     }
277 
278   if ((aType & VTK_PARSE_UNSIGNED) != 0)
279     {
280     fprintf(fp," unsigned ");
281     }
282 
283   switch ((aType & VTK_PARSE_BASE_TYPE) & ~VTK_PARSE_UNSIGNED)
284     {
285     case VTK_PARSE_FLOAT:   fprintf(fp,"float  "); break;
286     case VTK_PARSE_DOUBLE:   fprintf(fp,"double "); break;
287     case VTK_PARSE_INT:   fprintf(fp,"int    "); break;
288     case VTK_PARSE_SHORT:   fprintf(fp,"short  "); break;
289     case VTK_PARSE_LONG:   fprintf(fp,"long   "); break;
290     case VTK_PARSE_VOID:     fprintf(fp,"void   "); break;
291     case VTK_PARSE_CHAR:     fprintf(fp,"char   "); break;
292     case VTK_PARSE_ID_TYPE:   fprintf(fp,"vtkIdType "); break;
293     case VTK_PARSE_LONG_LONG:   fprintf(fp,"long long "); break;
294     case VTK_PARSE___INT64:   fprintf(fp,"__int64 "); break;
295     case VTK_PARSE_SIGNED_CHAR:     fprintf(fp,"signed char "); break;
296     case VTK_PARSE_BOOL:     fprintf(fp,"bool "); break;
297     case VTK_PARSE_OBJECT:     fprintf(fp,"%s ",Id); break;
298     case VTK_PARSE_STRING:  fprintf(fp,"%s ",Id); break;
299     case VTK_PARSE_UNKNOWN: fprintf(fp,"%s ",Id); break;
300     }
301 
302   switch (aType & VTK_PARSE_INDIRECT)
303     {
304     case VTK_PARSE_REF:
305       if (i == MAX_ARGS)
306         {
307         fprintf(fp, " *"); /* act " &" */
308         }
309       break;
310     case VTK_PARSE_POINTER:
311       if ((i == MAX_ARGS) ||
312           ((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_OBJECT_PTR) ||
313           ((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_CHAR_PTR))
314         {
315         fprintf(fp, " *");
316         }
317       break;
318     default:
319       fprintf(fp,"  ");
320       break;
321     }
322   fprintf(fp,"temp%i",i);
323 
324   /* handle arrays */
325   if (((aType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER)&&
326       (i != MAX_ARGS) &&
327       ((aType & VTK_PARSE_UNQUALIFIED_TYPE) != VTK_PARSE_OBJECT_PTR) &&
328       ((aType & VTK_PARSE_UNQUALIFIED_TYPE) != VTK_PARSE_CHAR_PTR))
329     {
330     fprintf(fp,"[%i]",aCount);
331     fprintf(fp,";\n  void *tempArray%i",i);
332     }
333 
334   fprintf(fp,";\n");
335 }
336 
get_args(FILE * fp,int i)337 void get_args(FILE *fp, int i)
338 {
339   unsigned int aType =
340     (currentFunction->ArgTypes[i] & VTK_PARSE_UNQUALIFIED_TYPE);
341   int j;
342 
343   /* handle VAR FUNCTIONS */
344   if (currentFunction->ArgTypes[i] == VTK_PARSE_FUNCTION)
345     {
346     fprintf(fp,"  env->GetJavaVM(&(temp%i->vm));\n",i);
347     fprintf(fp,"  temp%i->uobj = env->NewGlobalRef(id0);\n",i);
348     fprintf(fp,"  char *temp%i_str;\n",i);
349     fprintf(fp,"  temp%i_str = vtkJavaUTFToChar(env,id1);\n",i);
350     fprintf(fp,"  temp%i->mid = env->GetMethodID(env->GetObjectClass(id0),temp%i_str,\"()V\");\n",i,i);
351     return;
352     }
353 
354   /* ignore void */
355   if (aType == VTK_PARSE_VOID)
356     {
357     return;
358     }
359 
360   switch (aType)
361     {
362     case VTK_PARSE_CHAR:
363       fprintf(fp,"  temp%i = (char)(0xff & id%i);\n",i,i);
364       break;
365     case VTK_PARSE_BOOL:
366       fprintf(fp,"  temp%i = (id%i != 0) ? true : false;\n",i,i);
367       break;
368     case VTK_PARSE_CHAR_PTR:
369       fprintf(fp,"  temp%i = vtkJavaUTFToChar(env,id%i);\n",i,i);
370       break;
371     case VTK_PARSE_STRING:
372     case VTK_PARSE_STRING_REF:
373       fprintf(fp,"  vtkJavaUTFToString(env,id%i,temp%i);\n",i,i);
374       break;
375     case VTK_PARSE_OBJECT_PTR:
376       fprintf(fp,"  temp%i = (%s *)(vtkJavaGetPointerFromObject(env,id%i));\n",i,currentFunction->ArgClasses[i],i);
377       break;
378     case VTK_PARSE_FLOAT_PTR:
379     case VTK_PARSE_DOUBLE_PTR:
380       fprintf(fp,"  tempArray%i = (void *)(env->GetDoubleArrayElements(id%i,NULL));\n",i,i);
381       for (j = 0; j < currentFunction->ArgCounts[i]; j++)
382         {
383         fprintf(fp,"  temp%i[%i] = ((jdouble *)tempArray%i)[%i];\n",i,j,i,j);
384         }
385       break;
386     case VTK_PARSE_INT_PTR:
387     case VTK_PARSE_SHORT_PTR:
388     case VTK_PARSE_LONG_PTR:
389     case VTK_PARSE_ID_TYPE_PTR:
390     case VTK_PARSE_LONG_LONG_PTR:
391     case VTK_PARSE___INT64_PTR:
392     case VTK_PARSE_SIGNED_CHAR_PTR:
393     case VTK_PARSE_BOOL_PTR:
394       fprintf(fp,"  tempArray%i = (void *)(env->GetIntArrayElements(id%i,NULL));\n",i,i);
395       for (j = 0; j < currentFunction->ArgCounts[i]; j++)
396         {
397         fprintf(fp,"  temp%i[%i] = ((jint *)tempArray%i)[%i];\n",i,j,i,j);
398         }
399       break;
400     case VTK_PARSE_UNKNOWN:
401       fprintf(fp,"  temp%i = static_cast<%s>(id%i);\n",
402               i,currentFunction->ArgClasses[i],i);
403       break;
404     case VTK_PARSE_VOID:
405     case VTK_PARSE_OBJECT:
406     case VTK_PARSE_OBJECT_REF: break;
407     default: fprintf(fp,"  temp%i = id%i;\n",i,i); break;
408     }
409 }
410 
411 
copy_and_release_args(FILE * fp,int i)412 void copy_and_release_args(FILE *fp, int i)
413 {
414   unsigned int aType =
415     (currentFunction->ArgTypes[i] & VTK_PARSE_UNQUALIFIED_TYPE);
416   int j;
417 
418   /* handle VAR FUNCTIONS */
419   if (currentFunction->ArgTypes[i] == VTK_PARSE_FUNCTION)
420     {
421     fprintf(fp,"  delete[] temp%i_str;\n",i);
422     return;
423     }
424 
425   /* ignore void */
426   if (aType == VTK_PARSE_VOID)
427     {
428     return;
429     }
430 
431   switch (aType)
432     {
433     case VTK_PARSE_FLOAT_PTR:
434     case VTK_PARSE_DOUBLE_PTR:
435       for (j = 0; j < currentFunction->ArgCounts[i]; j++)
436         {
437         fprintf(fp,"  ((jdouble *)tempArray%i)[%i] = temp%i[%i];\n",i,j,i,j);
438         }
439       fprintf(fp,"  env->ReleaseDoubleArrayElements(id%i,(jdouble *)tempArray%i,0);\n",i,i);
440       break;
441     case VTK_PARSE_CHAR_PTR:
442       fprintf(fp,"  delete[] temp%i;\n",i);
443       break;
444     case VTK_PARSE_INT_PTR:
445     case VTK_PARSE_LONG_PTR:
446     case VTK_PARSE_SHORT_PTR:
447     case VTK_PARSE_ID_TYPE_PTR:
448     case VTK_PARSE_LONG_LONG_PTR:
449     case VTK_PARSE___INT64_PTR:
450     case VTK_PARSE_SIGNED_CHAR_PTR:
451     case VTK_PARSE_BOOL_PTR:
452       for (j = 0; j < currentFunction->ArgCounts[i]; j++)
453         {
454         fprintf(fp,"  ((jint *)tempArray%i)[%i] = temp%i[%i];\n",i,j,i,j);
455         }
456       fprintf(fp,"  env->ReleaseIntArrayElements(id%i,(jint *)tempArray%i,0);\n",i,i);
457       break;
458     default:
459       break;
460     }
461 }
462 
do_return(FILE * fp)463 void do_return(FILE *fp)
464 {
465   unsigned int rType =
466     (currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE);
467 
468   /* ignore void */
469   if (rType == VTK_PARSE_VOID)
470     {
471     return;
472     }
473 
474   switch (rType)
475     {
476     case VTK_PARSE_CHAR_PTR:
477       {
478       fprintf(fp,"  return vtkJavaMakeJavaString(env,temp%i);\n", MAX_ARGS);
479       break;
480       }
481     case VTK_PARSE_STRING:
482       {
483       fprintf(fp,"  return vtkJavaMakeJavaString(env,temp%i.c_str());\n", MAX_ARGS);
484       break;
485       }
486     case VTK_PARSE_STRING_REF:
487       {
488       fprintf(fp,"  return vtkJavaMakeJavaString(env,temp%i->c_str());\n", MAX_ARGS);
489       break;
490       }
491     case VTK_PARSE_OBJECT_PTR:
492       {
493       fprintf(fp,"  return (jlong)(size_t)temp%i;", MAX_ARGS);
494       break;
495       }
496 
497     /* handle functions returning vectors */
498     /* this is done by looking them up in a hint file */
499     case VTK_PARSE_FLOAT_PTR:
500     case VTK_PARSE_DOUBLE_PTR:
501     case VTK_PARSE_UNSIGNED_CHAR_PTR:
502     case VTK_PARSE_INT_PTR:
503     case VTK_PARSE_SHORT_PTR:
504     case VTK_PARSE_LONG_PTR:
505     case VTK_PARSE_ID_TYPE_PTR:
506     case VTK_PARSE_LONG_LONG_PTR:
507     case VTK_PARSE___INT64_PTR:
508     case VTK_PARSE_SIGNED_CHAR_PTR:
509     case VTK_PARSE_BOOL_PTR:
510       use_hints(fp);
511       break;
512     default: fprintf(fp,"  return temp%i;\n", MAX_ARGS); break;
513     }
514 }
515 
516 /* Check to see if two types will map to the same Java type,
517  * return 1 if type1 should take precedence,
518  * return 2 if type2 should take precedence,
519  * return 0 if the types do not map to the same type */
CheckMatch(unsigned int type1,unsigned int type2,const char * c1,const char * c2)520 static int CheckMatch(
521   unsigned int type1, unsigned int type2, const char *c1, const char *c2)
522 {
523   static unsigned int floatTypes[] = {
524     VTK_PARSE_DOUBLE, VTK_PARSE_FLOAT, 0 };
525 
526   static unsigned int intTypes[] = {
527     VTK_PARSE_UNSIGNED_LONG_LONG, VTK_PARSE_UNSIGNED___INT64,
528     VTK_PARSE_LONG_LONG, VTK_PARSE___INT64, VTK_PARSE_ID_TYPE,
529     VTK_PARSE_UNSIGNED_LONG, VTK_PARSE_LONG,
530     VTK_PARSE_UNSIGNED_INT, VTK_PARSE_INT,
531     VTK_PARSE_UNSIGNED_SHORT, VTK_PARSE_SHORT,
532     VTK_PARSE_UNSIGNED_CHAR, VTK_PARSE_SIGNED_CHAR, 0 };
533 
534   static unsigned int stringTypes[] = {
535     VTK_PARSE_CHAR_PTR, VTK_PARSE_STRING_REF, VTK_PARSE_STRING, 0 };
536 
537   static unsigned int *numericTypes[] = { floatTypes, intTypes, 0 };
538 
539   int i, j;
540   int hit1, hit2;
541 
542   if ((type1 & VTK_PARSE_UNQUALIFIED_TYPE) ==
543       (type2 & VTK_PARSE_UNQUALIFIED_TYPE))
544     {
545     if ((type1 & VTK_PARSE_BASE_TYPE) == VTK_PARSE_OBJECT)
546       {
547       if (strcmp(c1, c2) == 0)
548         {
549         return 1;
550         }
551       return 0;
552       }
553     else
554       {
555       return 1;
556       }
557     }
558 
559   for (i = 0; numericTypes[i]; i++)
560     {
561     hit1 = 0;
562     hit2 = 0;
563     for (j = 0; numericTypes[i][j]; j++)
564       {
565       if ((type1 & VTK_PARSE_BASE_TYPE) == numericTypes[i][j])
566         {
567         hit1 = j+1;
568         }
569       if ((type2 & VTK_PARSE_BASE_TYPE) == numericTypes[i][j])
570         {
571         hit2 = j+1;
572         }
573       }
574     if (hit1 && hit2 &&
575         (type1 & VTK_PARSE_INDIRECT) == (type2 & VTK_PARSE_INDIRECT))
576       {
577       if (hit1 < hit2)
578         {
579         return 1;
580         }
581       else
582         {
583         return 2;
584         }
585       }
586     }
587 
588   hit1 = 0;
589   hit2 = 0;
590   for (j = 0; stringTypes[j]; j++)
591     {
592     if ((type1 & VTK_PARSE_UNQUALIFIED_TYPE) == stringTypes[j])
593       {
594       hit1 = j+1;
595       }
596     if ((type2 & VTK_PARSE_UNQUALIFIED_TYPE) == stringTypes[j])
597       {
598       hit2 = j+1;
599       }
600     }
601   if (hit1 && hit2)
602     {
603     if (hit1 < hit2)
604       {
605       return 1;
606       }
607     else
608       {
609       return 2;
610       }
611     }
612 
613   return 0;
614 }
615 
616 /* have we done one of these yet */
DoneOne()617 int DoneOne()
618 {
619   int i,j;
620   int match;
621   FunctionInfo *fi;
622 
623   for (i = 0; i < numberOfWrappedFunctions; i++)
624     {
625     fi = wrappedFunctions[i];
626 
627     if ((!strcmp(fi->Name,currentFunction->Name))
628         &&(fi->NumberOfArguments == currentFunction->NumberOfArguments))
629       {
630       match = 1;
631       for (j = 0; j < fi->NumberOfArguments; j++)
632         {
633         if (!CheckMatch(currentFunction->ArgTypes[j], fi->ArgTypes[j],
634                         currentFunction->ArgClasses[j],fi->ArgClasses[j]))
635           {
636           match = 0;
637           }
638         }
639       if (!CheckMatch(currentFunction->ReturnType, fi->ReturnType,
640                       currentFunction->ReturnClass, fi->ReturnClass))
641         {
642         match = 0;
643         }
644       if (match) return 1;
645       }
646     }
647   return 0;
648 }
649 
HandleDataReader(FILE * fp,ClassInfo * data)650 void HandleDataReader(FILE *fp, ClassInfo *data)
651 {
652     fprintf(fp,"\n");
653     fprintf(fp,"extern \"C\" JNIEXPORT void");
654     fprintf(fp," JNICALL Java_vtk_%s_%s_1%i(JNIEnv *env, jobject obj, jbyteArray id0, jint id1)\n",
655             data->Name,currentFunction->Name, numberOfWrappedFunctions);
656     fprintf(fp,"{\n");
657     fprintf(fp,"  %s *op;\n",data->Name);
658     fprintf(fp,"  op = (%s *)vtkJavaGetPointerFromObject(env,obj);\n",
659             data->Name);
660     fprintf(fp,"  jboolean isCopy;\n");
661     fprintf(fp,"  jbyte *data = env->GetByteArrayElements(id0,&isCopy);\n");
662     fprintf(fp,"  op->SetBinaryInputString((const char *)data,id1);\n");
663     fprintf(fp,"  env->ReleaseByteArrayElements(id0,data,JNI_ABORT);\n");
664     fprintf(fp,"}\n");
665 }
666 
HandleDataArray(FILE * fp,ClassInfo * data)667 void HandleDataArray(FILE *fp, ClassInfo *data)
668 {
669   const char *type = 0;
670   const char *jtype = 0;
671   const char *fromtype = 0;
672   const char *jfromtype = 0;
673 
674   if (!strcmp("vtkCharArray",data->Name) )
675     {
676     type = "char";
677     fromtype = "Char";
678     jtype = "byte";
679     jfromtype = "Byte";
680     }
681   else if (!strcmp("vtkDoubleArray",data->Name) )
682     {
683     type = "double";
684     fromtype = "Double";
685     jtype = type;
686     jfromtype = fromtype;
687     }
688   else if (!strcmp("vtkFloatArray",data->Name) )
689     {
690     type = "float";
691     fromtype = "Float";
692     jtype = type;
693     jfromtype = fromtype;
694     }
695   else if (!strcmp("vtkIntArray",data->Name) )
696     {
697     type = "int";
698     fromtype = "Int";
699     jtype = type;
700     jfromtype = fromtype;
701     }
702   else if (!strcmp("vtkLongArray",data->Name) )
703     {
704     type = "long";
705     fromtype = "Long";
706     jtype = type;
707     jfromtype = fromtype;
708     }
709   else if (!strcmp("vtkShortArray",data->Name) )
710     {
711     type = "short";
712     fromtype = "Short";
713     jtype = type;
714     jfromtype = fromtype;
715     }
716   else if (!strcmp("vtkUnsignedCharArray",data->Name) )
717     {
718     type = "unsigned char";
719     fromtype = "UnsignedChar";
720     jtype = "byte";
721     jfromtype = "Byte";
722     }
723   else if (!strcmp("vtkUnsignedIntArray",data->Name) )
724     {
725     type = "unsigned int";
726     fromtype = "UnsignedInt";
727     jtype = "int";
728     jfromtype = "Int";
729     }
730   else if (!strcmp("vtkUnsignedLongArray",data->Name) )
731     {
732     type = "unsigned long";
733     fromtype = "UnsignedLong";
734     jtype = "long";
735     jfromtype = "Long";
736     }
737   else if (!strcmp("vtkUnsignedShortArray",data->Name) )
738     {
739     type = "unsigned short";
740     fromtype = "UnsignedShort";
741     jtype = "short";
742     jfromtype = "Short";
743     }
744   else
745     {
746     return;
747     }
748 
749   fprintf(fp,"// Array conversion routines\n");
750   fprintf(fp,"extern \"C\" JNIEXPORT jarray JNICALL Java_vtk_%s_GetJavaArray_10("
751     "JNIEnv *env, jobject obj)\n",
752     data->Name);
753   fprintf(fp,"{\n");
754   fprintf(fp,"  %s *op;\n", data->Name);
755   fprintf(fp,"  %s  *temp20;\n", type);
756   fprintf(fp,"  vtkIdType size;\n");
757   fprintf(fp,"\n");
758   fprintf(fp,"  op = (%s *)vtkJavaGetPointerFromObject(env,obj);\n",
759     data->Name);
760   fprintf(fp,"  temp20 = static_cast<%s*>(op->GetVoidPointer(0));\n", type);
761   fprintf(fp,"  size = op->GetMaxId()+1;\n");
762   fprintf(fp,"  return vtkJavaMakeJArrayOf%sFrom%s(env,temp20,size);\n", fromtype, fromtype);
763   fprintf(fp,"}\n");
764 
765   fprintf(fp,"extern \"C\" JNIEXPORT void  JNICALL Java_vtk_%s_SetJavaArray_10("
766     "JNIEnv *env, jobject obj,j%sArray id0)\n", data->Name, jtype);
767   fprintf(fp,"{\n");
768   fprintf(fp,"  %s *op;\n", data->Name);
769   fprintf(fp,"  %s *tempArray0;\n", type);
770   fprintf(fp,"  int length;\n");
771   fprintf(fp,"  tempArray0 = (%s *)(env->Get%sArrayElements(id0,NULL));\n", type, jfromtype);
772   fprintf(fp,"  length = env->GetArrayLength(id0);\n");
773   fprintf(fp,"  op = (%s *)vtkJavaGetPointerFromObject(env,obj);\n",
774     data->Name);
775   fprintf(fp,"  op->SetNumberOfTuples(length/op->GetNumberOfComponents());\n");
776   fprintf(fp,"  memcpy(op->GetVoidPointer(0), tempArray0, length*sizeof(%s));\n", type);
777   fprintf(fp,"  env->Release%sArrayElements(id0,(j%s *)tempArray0,0);\n", jfromtype, jtype);
778   fprintf(fp,"}\n");
779 }
780 
isClassWrapped(const char * classname)781 static int isClassWrapped(const char *classname)
782 {
783   HierarchyEntry *entry;
784 
785   if (hierarchyInfo)
786     {
787     entry = vtkParseHierarchy_FindEntry(hierarchyInfo, classname);
788 
789     if (entry == 0 ||
790         vtkParseHierarchy_GetProperty(entry, "WRAP_EXCLUDE") ||
791         !vtkParseHierarchy_IsTypeOf(hierarchyInfo, entry, "vtkObjectBase"))
792       {
793       return 0;
794       }
795     }
796 
797   return 1;
798 }
799 
checkFunctionSignature(ClassInfo * data)800 int checkFunctionSignature(ClassInfo *data)
801 {
802   static unsigned int supported_types[] = {
803     VTK_PARSE_VOID, VTK_PARSE_BOOL, VTK_PARSE_FLOAT, VTK_PARSE_DOUBLE,
804     VTK_PARSE_CHAR, VTK_PARSE_UNSIGNED_CHAR, VTK_PARSE_SIGNED_CHAR,
805     VTK_PARSE_INT, VTK_PARSE_UNSIGNED_INT,
806     VTK_PARSE_SHORT, VTK_PARSE_UNSIGNED_SHORT,
807     VTK_PARSE_LONG, VTK_PARSE_UNSIGNED_LONG,
808     VTK_PARSE_ID_TYPE, VTK_PARSE_UNSIGNED_ID_TYPE,
809     VTK_PARSE_LONG_LONG, VTK_PARSE_UNSIGNED_LONG_LONG,
810     VTK_PARSE___INT64, VTK_PARSE_UNSIGNED___INT64,
811     VTK_PARSE_OBJECT, VTK_PARSE_STRING, VTK_PARSE_UNKNOWN,
812     0
813   };
814 
815   int i, j;
816   int args_ok = 1;
817   unsigned int rType =
818     (currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE);
819   unsigned int aType = 0;
820   unsigned int baseType = 0;
821 
822   /* some functions will not get wrapped no matter what else */
823   if (currentFunction->IsOperator ||
824       currentFunction->ArrayFailure ||
825       !currentFunction->IsPublic ||
826       !currentFunction->Name)
827     {
828     return 0;
829     }
830 
831   /* NewInstance and SafeDownCast can not be wrapped because it is a
832      (non-virtual) method which returns a pointer of the same type as
833      the current pointer. Since all methods are virtual in Java, this
834      looks like polymorphic return type.  */
835   if (!strcmp("NewInstance",currentFunction->Name))
836     {
837     return 0;
838     }
839 
840   if (!strcmp("SafeDownCast",currentFunction->Name))
841     {
842     return 0;
843     }
844 
845   /* The GetInput() in vtkMapper cannot be overriden with a
846    * different return type, Java doesn't allow this */
847   if (strcmp(data->Name, "vtkMapper") == 0 &&
848       strcmp(currentFunction->Name, "GetInput") == 0)
849     {
850     return 0;
851     }
852 
853   /* function pointer arguments for callbacks */
854   if (currentFunction->NumberOfArguments == 2 &&
855       currentFunction->ArgTypes[0] == VTK_PARSE_FUNCTION &&
856       currentFunction->ArgTypes[1] == VTK_PARSE_VOID_PTR &&
857       rType == VTK_PARSE_VOID)
858     {
859     return 1;
860     }
861 
862   /* check to see if we can handle the args */
863   for (i = 0; i < currentFunction->NumberOfArguments; i++)
864     {
865     aType = (currentFunction->ArgTypes[i] & VTK_PARSE_UNQUALIFIED_TYPE);
866     baseType = (aType & VTK_PARSE_BASE_TYPE);
867 
868     for (j = 0; supported_types[j] != 0; j++)
869       {
870       if (baseType == supported_types[j]) { break; }
871       }
872     if (supported_types[j] == 0)
873       {
874       args_ok = 0;
875       }
876 
877     if (baseType == VTK_PARSE_UNKNOWN)
878       {
879       const char *qualified_name = 0;
880       if ((aType & VTK_PARSE_INDIRECT) == 0)
881         {
882         qualified_name = vtkParseHierarchy_QualifiedEnumName(
883           hierarchyInfo, data, stringCache,
884           currentFunction->ArgClasses[i]);
885         }
886       if (qualified_name)
887         {
888         currentFunction->ArgClasses[i] = qualified_name;
889         }
890       else
891         {
892         args_ok = 0;
893         }
894       }
895 
896     if (baseType == VTK_PARSE_OBJECT)
897       {
898       if ((aType & VTK_PARSE_INDIRECT) != VTK_PARSE_POINTER)
899         {
900         args_ok = 0;
901         }
902       else if (!isClassWrapped(currentFunction->ArgClasses[i]))
903         {
904         args_ok = 0;
905         }
906       }
907 
908     if (aType == VTK_PARSE_OBJECT) args_ok = 0;
909     if (((aType & VTK_PARSE_INDIRECT) != VTK_PARSE_POINTER) &&
910         ((aType & VTK_PARSE_INDIRECT) != 0) &&
911         (aType != VTK_PARSE_STRING_REF)) args_ok = 0;
912     if (aType == VTK_PARSE_STRING_PTR) args_ok = 0;
913     if (aType == VTK_PARSE_UNSIGNED_CHAR_PTR) args_ok = 0;
914     if (aType == VTK_PARSE_UNSIGNED_INT_PTR) args_ok = 0;
915     if (aType == VTK_PARSE_UNSIGNED_SHORT_PTR) args_ok = 0;
916     if (aType == VTK_PARSE_UNSIGNED_LONG_PTR) args_ok = 0;
917     if (aType == VTK_PARSE_UNSIGNED_ID_TYPE_PTR) args_ok = 0;
918     if (aType == VTK_PARSE_UNSIGNED_LONG_LONG_PTR) args_ok = 0;
919     if (aType == VTK_PARSE_UNSIGNED___INT64_PTR) args_ok = 0;
920     }
921 
922   baseType = (rType & VTK_PARSE_BASE_TYPE);
923 
924   for (j = 0; supported_types[j] != 0; j++)
925     {
926     if (baseType == supported_types[j]) { break; }
927     }
928   if (supported_types[j] == 0)
929     {
930     args_ok = 0;
931     }
932 
933   if (baseType == VTK_PARSE_UNKNOWN)
934     {
935     const char *qualified_name = 0;
936     if ((rType & VTK_PARSE_INDIRECT) == 0)
937       {
938       qualified_name = vtkParseHierarchy_QualifiedEnumName(
939         hierarchyInfo, data, stringCache,
940         currentFunction->ReturnClass);
941       }
942     if (qualified_name)
943       {
944       currentFunction->ReturnClass = qualified_name;
945       }
946     else
947       {
948       args_ok = 0;
949       }
950     }
951 
952   if (baseType == VTK_PARSE_OBJECT)
953     {
954     if ((rType & VTK_PARSE_INDIRECT) != VTK_PARSE_POINTER)
955       {
956       args_ok = 0;
957       }
958     else if (!isClassWrapped(currentFunction->ReturnClass))
959       {
960       args_ok = 0;
961       }
962     }
963 
964   if (((rType & VTK_PARSE_INDIRECT) != VTK_PARSE_POINTER) &&
965       ((rType & VTK_PARSE_INDIRECT) != 0) &&
966       (rType != VTK_PARSE_STRING_REF)) args_ok = 0;
967   if (rType == VTK_PARSE_STRING_PTR) args_ok = 0;
968 
969   /* eliminate unsigned char * and unsigned short * */
970   if (rType == VTK_PARSE_UNSIGNED_INT_PTR) args_ok = 0;
971   if (rType == VTK_PARSE_UNSIGNED_SHORT_PTR) args_ok = 0;
972   if (rType == VTK_PARSE_UNSIGNED_LONG_PTR) args_ok = 0;
973   if (rType == VTK_PARSE_UNSIGNED_ID_TYPE_PTR) args_ok = 0;
974   if (rType == VTK_PARSE_UNSIGNED_LONG_LONG_PTR) args_ok = 0;
975   if (rType == VTK_PARSE_UNSIGNED___INT64_PTR) args_ok = 0;
976 
977   /* make sure we have all the info we need for array arguments in */
978   for (i = 0; i < currentFunction->NumberOfArguments; i++)
979     {
980     aType = (currentFunction->ArgTypes[i] & VTK_PARSE_UNQUALIFIED_TYPE);
981 
982     if (((aType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER)&&
983         (currentFunction->ArgCounts[i] <= 0)&&
984         (aType != VTK_PARSE_OBJECT_PTR)&&
985         (aType != VTK_PARSE_CHAR_PTR)) args_ok = 0;
986     }
987 
988   /* if we need a return type hint make sure we have one */
989   switch (rType)
990     {
991     case VTK_PARSE_FLOAT_PTR:
992     case VTK_PARSE_VOID_PTR:
993     case VTK_PARSE_DOUBLE_PTR:
994     case VTK_PARSE_INT_PTR:
995     case VTK_PARSE_SHORT_PTR:
996     case VTK_PARSE_LONG_PTR:
997     case VTK_PARSE_ID_TYPE_PTR:
998     case VTK_PARSE_LONG_LONG_PTR:
999     case VTK_PARSE___INT64_PTR:
1000     case VTK_PARSE_SIGNED_CHAR_PTR:
1001     case VTK_PARSE_BOOL_PTR:
1002     case VTK_PARSE_UNSIGNED_CHAR_PTR:
1003       args_ok = currentFunction->HaveHint;
1004       break;
1005     }
1006 
1007   /* make sure there isn't a Java-specific override */
1008   if (!strcmp("vtkObject",data->Name))
1009     {
1010     /* remove the original vtkCommand observer methods */
1011     if (!strcmp(currentFunction->Name,"AddObserver") ||
1012         !strcmp(currentFunction->Name,"GetCommand") ||
1013         (!strcmp(currentFunction->Name,"RemoveObserver") &&
1014          (currentFunction->ArgTypes[0] != VTK_PARSE_UNSIGNED_LONG)) ||
1015         ((!strcmp(currentFunction->Name,"RemoveObservers") ||
1016           !strcmp(currentFunction->Name,"HasObserver")) &&
1017          (((currentFunction->ArgTypes[0] != VTK_PARSE_UNSIGNED_LONG) &&
1018            (currentFunction->ArgTypes[0] !=
1019             (VTK_PARSE_CHAR_PTR|VTK_PARSE_CONST))) ||
1020           (currentFunction->NumberOfArguments > 1))) ||
1021         (!strcmp(currentFunction->Name,"RemoveAllObservers") &&
1022          (currentFunction->NumberOfArguments > 0)))
1023       {
1024       args_ok = 0;
1025       }
1026     }
1027   else if (!strcmp("vtkObjectBase",data->Name))
1028     {
1029     /* remove the special vtkObjectBase methods */
1030     if (!strcmp(currentFunction->Name,"Print")
1031 #ifndef VTK_LEGACY_REMOVE
1032         || !strcmp(currentFunction->Name,"PrintRevisions")
1033 #endif
1034         )
1035       {
1036       args_ok = 0;
1037       }
1038     }
1039 
1040   /* make sure it isn't a Delete or New function */
1041   if (!strcmp("Delete",currentFunction->Name) ||
1042       !strcmp("New",currentFunction->Name))
1043     {
1044     args_ok = 0;
1045     }
1046 
1047   return args_ok;
1048 }
1049 
outputFunction(FILE * fp,ClassInfo * data)1050 void outputFunction(FILE *fp, ClassInfo *data)
1051 {
1052   int i;
1053   int args_ok = 1;
1054   unsigned int rType =
1055     (currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE);
1056   const char *jniFunction = 0;
1057   char *jniFunctionNew = 0;
1058   char *jniFunctionOld = 0;
1059   size_t j;
1060   CurrentData = data;
1061 
1062   args_ok = checkFunctionSignature(data);
1063 
1064   /* handle DataReader SetBinaryInputString as a special case */
1065   if (!strcmp("SetBinaryInputString",currentFunction->Name) &&
1066       (!strcmp("vtkDataReader",data->Name) ||
1067        !strcmp("vtkStructuredGridReader",data->Name) ||
1068        !strcmp("vtkRectilinearGridReader",data->Name) ||
1069        !strcmp("vtkUnstructuredGridReader",data->Name) ||
1070        !strcmp("vtkStructuredPointsReader",data->Name) ||
1071        !strcmp("vtkPolyDataReader",data->Name)))
1072     {
1073     if(currentFunction->IsLegacy)
1074       {
1075       fprintf(fp,"#if !defined(VTK_LEGACY_REMOVE)\n");
1076       }
1077     HandleDataReader(fp,data);
1078     if(currentFunction->IsLegacy)
1079       {
1080       fprintf(fp,"#endif\n");
1081       }
1082     wrappedFunctions[numberOfWrappedFunctions] = currentFunction;
1083     numberOfWrappedFunctions++;
1084     }
1085 
1086   if (currentFunction->IsPublic && args_ok &&
1087       strcmp(data->Name,currentFunction->Name) &&
1088       strcmp(data->Name, currentFunction->Name + 1))
1089     {
1090     /* make sure we haven't already done one of these */
1091     if (!DoneOne())
1092       {
1093       fprintf(fp,"\n");
1094 
1095       /* Underscores are escaped in method names, see
1096            http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/design.html#wp133
1097          VTK class names contain no underscore and do not need to be escaped.  */
1098       jniFunction = currentFunction->Name;
1099       jniFunctionOld = 0;
1100       j = 0;
1101       while (jniFunction[j] != '\0')
1102         {
1103         /* replace "_" with "_1" */
1104         if (jniFunction[j] == '_')
1105           {
1106           j++;
1107           jniFunctionNew = (char *)malloc(strlen(jniFunction) + 2);
1108           strncpy(jniFunctionNew, jniFunction, j);
1109           jniFunctionNew[j] = '1';
1110           strcpy(&jniFunctionNew[j+1], &jniFunction[j]);
1111           free(jniFunctionOld);
1112           jniFunctionOld = jniFunctionNew;
1113           jniFunction = jniFunctionNew;
1114           }
1115         j++;
1116         }
1117 
1118       if(currentFunction->IsLegacy)
1119         {
1120         fprintf(fp,"#if !defined(VTK_LEGACY_REMOVE)\n");
1121         }
1122       fprintf(fp,"extern \"C\" JNIEXPORT ");
1123       return_result(fp);
1124       fprintf(fp," JNICALL Java_vtk_%s_%s_1%i(JNIEnv *env, jobject obj",
1125               data->Name, jniFunction, numberOfWrappedFunctions);
1126 
1127       for (i = 0; i < currentFunction->NumberOfArguments; i++)
1128         {
1129         fprintf(fp,",");
1130         output_proto_vars(fp, i);
1131 
1132         /* ignore args after function pointer */
1133         if (currentFunction->ArgTypes[i] == VTK_PARSE_FUNCTION)
1134           {
1135           break;
1136           }
1137         }
1138       fprintf(fp,")\n{\n");
1139 
1140       /* get the object pointer */
1141       fprintf(fp,"  %s *op;\n",data->Name);
1142 
1143       /* process the args */
1144       for (i = 0; i < currentFunction->NumberOfArguments; i++)
1145         {
1146         output_temp(fp, i, currentFunction->ArgTypes[i],
1147                    currentFunction->ArgClasses[i],
1148                    currentFunction->ArgCounts[i]);
1149 
1150         /* ignore args after function pointer */
1151         if (currentFunction->ArgTypes[i] == VTK_PARSE_FUNCTION)
1152           {
1153           break;
1154           }
1155         }
1156       output_temp(fp, MAX_ARGS,currentFunction->ReturnType,
1157                   currentFunction->ReturnClass,0);
1158 
1159       /* now get the required args from the stack */
1160       for (i = 0; i < currentFunction->NumberOfArguments; i++)
1161         {
1162         get_args(fp, i);
1163 
1164         /* ignore args after function pointer */
1165         if (currentFunction->ArgTypes[i] == VTK_PARSE_FUNCTION)
1166           {
1167           break;
1168           }
1169         }
1170 
1171       fprintf(fp,"\n  op = (%s *)vtkJavaGetPointerFromObject(env,obj);\n",
1172               data->Name);
1173 
1174 
1175       switch (rType)
1176         {
1177         case VTK_PARSE_VOID:
1178           fprintf(fp,"  op->%s(",currentFunction->Name);
1179           break;
1180         default:
1181           if ((rType & VTK_PARSE_INDIRECT) == VTK_PARSE_REF)
1182             {
1183             fprintf(fp,"  temp%i = &(op)->%s(",MAX_ARGS,currentFunction->Name);
1184             }
1185           else
1186             {
1187             fprintf(fp,"  temp%i = (op)->%s(",MAX_ARGS,currentFunction->Name);
1188             }
1189           break;
1190         }
1191 
1192       for (i = 0; i < currentFunction->NumberOfArguments; i++)
1193         {
1194         if (i)
1195           {
1196           fprintf(fp,",");
1197           }
1198         if (currentFunction->ArgTypes[i] == VTK_PARSE_FUNCTION)
1199           {
1200           fprintf(fp,"vtkJavaVoidFunc,(void *)temp%i",i);
1201           break;
1202           }
1203         else
1204           {
1205           fprintf(fp,"temp%i",i);
1206           }
1207         } /* for */
1208 
1209       fprintf(fp,");\n");
1210 
1211       if (currentFunction->NumberOfArguments == 2 &&
1212           currentFunction->ArgTypes[0] == VTK_PARSE_FUNCTION)
1213         {
1214         fprintf(fp,"  op->%sArgDelete(vtkJavaVoidFuncArgDelete);\n",
1215                 jniFunction);
1216         }
1217 
1218       /* now copy and release any arrays */
1219       for (i = 0; i < currentFunction->NumberOfArguments; i++)
1220         {
1221         copy_and_release_args(fp, i);
1222 
1223         /* ignore args after function pointer */
1224         if (currentFunction->ArgTypes[i] == VTK_PARSE_FUNCTION)
1225           {
1226           break;
1227           }
1228         }
1229       do_return(fp);
1230       fprintf(fp,"}\n");
1231       if(currentFunction->IsLegacy)
1232         {
1233         fprintf(fp,"#endif\n");
1234         }
1235 
1236       wrappedFunctions[numberOfWrappedFunctions] = currentFunction;
1237       numberOfWrappedFunctions++;
1238       if (jniFunctionNew)
1239         {
1240         free(jniFunctionNew);
1241         jniFunctionNew = 0;
1242         }
1243     } /* isDone() */
1244   } /* isAbstract */
1245 }
1246 
1247 /* print the parsed structures */
main(int argc,char * argv[])1248 int main(int argc, char *argv[])
1249 {
1250   OptionInfo *options;
1251   FileInfo *file_info;
1252   ClassInfo *data;
1253   FILE *fp;
1254   int i;
1255 
1256   /* get command-line args and parse the header file */
1257   file_info = vtkParse_Main(argc, argv);
1258 
1259   /* some utility functions require the string cache */
1260   stringCache = file_info->Strings;
1261 
1262   /* get the command-line options */
1263   options = vtkParse_GetCommandLineOptions();
1264 
1265   /* get the output file */
1266   fp = fopen(options->OutputFileName, "w");
1267 
1268   if (!fp)
1269     {
1270     fprintf(stderr, "Error opening output file %s\n", options->OutputFileName);
1271     exit(1);
1272     }
1273 
1274   /* get the main class */
1275   if ((data = file_info->MainClass) == NULL)
1276     {
1277     fclose(fp);
1278     exit(0);
1279     }
1280 
1281   /* get the hierarchy info for accurate typing */
1282   if (options->HierarchyFileName)
1283     {
1284     hierarchyInfo = vtkParseHierarchy_ReadFile(options->HierarchyFileName);
1285     }
1286 
1287   fprintf(fp,"// java wrapper for %s object\n//\n",data->Name);
1288   fprintf(fp,"#define VTK_WRAPPING_CXX\n");
1289   if (strcmp("vtkObjectBase",data->Name) != 0)
1290     {
1291     /* Block inclusion of full streams.  */
1292     fprintf(fp,"#define VTK_STREAMS_FWD_ONLY\n");
1293     }
1294   fprintf(fp,"#include \"vtkSystemIncludes.h\"\n");
1295   fprintf(fp,"#include \"%s.h\"\n",data->Name);
1296   fprintf(fp,"#include \"vtkJavaUtil.h\"\n\n");
1297   fprintf(fp,"#include \"vtkStdString.h\"\n\n");
1298   fprintf(fp,"#include <vtksys/ios/sstream>\n");
1299 
1300   for (i = 0; i < data->NumberOfSuperClasses; i++)
1301     {
1302     char *safe_name = vtkWrap_SafeSuperclassName(data->SuperClasses[i]);
1303     const char *safe_superclass = safe_name ? safe_name : data->SuperClasses[i];
1304 
1305     /* if a template class is detected add a typedef */
1306     if (safe_name)
1307       {
1308       fprintf(fp,"typedef %s %s;\n",
1309               data->SuperClasses[i], safe_name);
1310       }
1311 
1312     fprintf(fp,"extern \"C\" JNIEXPORT void* %s_Typecast(void *op,char *dType);\n",
1313             safe_superclass);
1314 
1315     free(safe_name);
1316     }
1317 
1318   fprintf(fp,"\nextern \"C\" JNIEXPORT void* %s_Typecast(void *me,char *dType)\n{\n",data->Name);
1319   if (data->NumberOfSuperClasses > 0)
1320     {
1321     fprintf(fp,"  void* res;\n");
1322     }
1323   fprintf(fp,"  if (!strcmp(\"%s\",dType)) { return me; }\n", data->Name);
1324   /* check our superclasses */
1325   for (i = 0; i < data->NumberOfSuperClasses; i++)
1326     {
1327     char *safe_name = vtkWrap_SafeSuperclassName(data->SuperClasses[i]);
1328     const char *safe_superclass = safe_name ? safe_name : data->SuperClasses[i];
1329 
1330     fprintf(fp,"  if ((res= %s_Typecast(me,dType)) != NULL)",
1331             safe_superclass);
1332     fprintf(fp," { return res; }\n");
1333 
1334     free(safe_name);
1335     }
1336   fprintf(fp,"  return NULL;\n");
1337   fprintf(fp,"}\n\n");
1338 
1339   HandleDataArray(fp, data);
1340 
1341   /* insert function handling code here */
1342   for (i = 0; i < data->NumberOfFunctions; i++)
1343     {
1344     currentFunction = data->Functions[i];
1345     outputFunction(fp, data);
1346     }
1347 
1348   if ((!data->NumberOfSuperClasses)&&(data->HasDelete))
1349     {
1350     fprintf(fp,"\nextern \"C\" JNIEXPORT void JNICALL Java_vtk_%s_VTKDeleteReference(JNIEnv *,jclass,jlong id)\n",
1351             data->Name);
1352     fprintf(fp,"{\n  %s *op;\n",data->Name);
1353     fprintf(fp,"  op = reinterpret_cast<%s*>(id);\n",
1354             data->Name);
1355     fprintf(fp,"  op->Delete();\n");
1356     fprintf(fp,"}\n");
1357 
1358     fprintf(fp,"\nextern \"C\" JNIEXPORT jstring JNICALL Java_vtk_%s_VTKGetClassNameFromReference(JNIEnv *env,jclass,jlong id)\n",
1359             data->Name);
1360     fprintf(fp,"{\n");
1361     fprintf(fp,"  const char* name = \"\";\n");
1362     fprintf(fp,"  %s *op;\n", data->Name);
1363     fprintf(fp,"  if(id != 0)\n");
1364     fprintf(fp,"    {\n");
1365     fprintf(fp,"    op = reinterpret_cast<%s*>(id);\n", data->Name);
1366     //fprintf(fp,"    std::cout << \"cast pointer \" << id << std::endl;\n");
1367     fprintf(fp,"    name = op->GetClassName();\n");
1368     fprintf(fp,"    }\n");
1369     fprintf(fp,"  return vtkJavaMakeJavaString(env,name);\n");
1370     fprintf(fp,"}\n");
1371 
1372     fprintf(fp,"\nextern \"C\" JNIEXPORT void JNICALL Java_vtk_%s_VTKDelete(JNIEnv *env,jobject obj)\n",
1373             data->Name);
1374     fprintf(fp,"{\n  %s *op;\n",data->Name);
1375     fprintf(fp,"  op = (%s *)vtkJavaGetPointerFromObject(env,obj);\n",
1376             data->Name);
1377     fprintf(fp,"  op->Delete();\n");
1378     fprintf(fp,"}\n");
1379 
1380     fprintf(fp,"\nextern \"C\" JNIEXPORT void JNICALL Java_vtk_%s_VTKRegister(JNIEnv *env,jobject obj)\n",
1381             data->Name);
1382     fprintf(fp,"{\n  %s *op;\n",data->Name);
1383     fprintf(fp,"  op = (%s *)vtkJavaGetPointerFromObject(env,obj);\n",
1384             data->Name);
1385     fprintf(fp,"  op->Register(op);\n");
1386     fprintf(fp,"}\n");
1387     }
1388   if (!data->IsAbstract)
1389     {
1390     fprintf(fp,"\nextern \"C\" JNIEXPORT jlong JNICALL Java_vtk_%s_VTKInit(JNIEnv *, jobject)",
1391             data->Name);
1392     fprintf(fp,"\n{");
1393     fprintf(fp,"\n  %s *aNewOne = %s::New();",data->Name, data->Name);
1394     fprintf(fp,"\n  return (jlong)(size_t)(void*)aNewOne;");
1395     fprintf(fp,"\n}\n");
1396     }
1397 
1398   /* for vtkRenderWindow we want to add a special method to support
1399    * native AWT rendering
1400    *
1401    * Including vtkJavaAwt.h provides inline implementations of
1402    * Java_vtk_vtkPanel_RenderCreate, Java_vtk_vtkPanel_Lock and
1403    * Java_vtk_vtkPanel_UnLock. */
1404   if (!strcmp("vtkRenderWindow",data->Name))
1405     {
1406     fprintf(fp,"\n#include \"vtkJavaAwt.h\"\n\n");
1407     }
1408 
1409   if (!strcmp("vtkObject",data->Name))
1410     {
1411     /* Add the Print method to vtkObjectBase. */
1412     fprintf(fp,"\nextern \"C\" JNIEXPORT jstring JNICALL Java_vtk_vtkObjectBase_Print(JNIEnv *env,jobject obj)\n");
1413     fprintf(fp,"{\n  vtkObjectBase *op;\n");
1414     fprintf(fp,"  jstring tmp;\n\n");
1415     fprintf(fp,"  op = (vtkObjectBase *)vtkJavaGetPointerFromObject(env,obj);\n");
1416 
1417     fprintf(fp,"  vtksys_ios::ostringstream vtkmsg_with_warning_C4701;\n");
1418     fprintf(fp,"  op->Print(vtkmsg_with_warning_C4701);\n");
1419     fprintf(fp,"  vtkmsg_with_warning_C4701.put('\\0');\n");
1420     fprintf(fp,"  tmp = vtkJavaMakeJavaString(env,vtkmsg_with_warning_C4701.str().c_str());\n");
1421 
1422     fprintf(fp,"  return tmp;\n");
1423     fprintf(fp,"}\n");
1424 
1425 #ifndef VTK_LEGACY_REMOVE
1426     /* Add the PrintRevisions method to vtkObjectBase. */
1427     fprintf(fp,"\nextern \"C\" JNIEXPORT jstring JNICALL Java_vtk_vtkObjectBase_PrintRevisions(JNIEnv *env,jobject obj)\n");
1428     fprintf(fp,"{\n  vtkObjectBase *op;\n");
1429     fprintf(fp,"  jstring tmp;\n\n");
1430     fprintf(fp,"  op = (vtkObjectBase *)vtkJavaGetPointerFromObject(env,obj);\n");
1431 
1432     fprintf(fp,"  vtksys_ios::ostringstream vtkmsg_with_warning_C4701;\n");
1433     fprintf(fp,"  op->PrintRevisions(vtkmsg_with_warning_C4701);\n");
1434     fprintf(fp,"  vtkmsg_with_warning_C4701.put('\\0');\n");
1435     fprintf(fp,"  tmp = vtkJavaMakeJavaString(env,vtkmsg_with_warning_C4701.str().c_str());\n");
1436 
1437     fprintf(fp,"  return tmp;\n");
1438     fprintf(fp,"}\n");
1439 #endif
1440 
1441     fprintf(fp,"\nextern \"C\" JNIEXPORT jint JNICALL Java_vtk_vtkObject_AddObserver(JNIEnv *env,jobject obj, jstring id0, jobject id1, jstring id2)\n");
1442     fprintf(fp,"{\n  vtkObject *op;\n");
1443 
1444     fprintf(fp,"  vtkJavaCommand *cbc = vtkJavaCommand::New();\n");
1445     fprintf(fp,"  cbc->AssignJavaVM(env);\n");
1446     fprintf(fp,"  cbc->SetGlobalRef(env->NewGlobalRef(id1));\n");
1447     fprintf(fp,"  char    *temp2;\n");
1448     fprintf(fp,"  temp2 = vtkJavaUTFToChar(env,id2);\n");
1449     fprintf(fp,"  cbc->SetMethodID(env->GetMethodID(env->GetObjectClass(id1),temp2,\"()V\"));\n");
1450     fprintf(fp,"  char    *temp0;\n");
1451     fprintf(fp,"  temp0 = vtkJavaUTFToChar(env,id0);\n");
1452     fprintf(fp,"  op = (vtkObject *)vtkJavaGetPointerFromObject(env,obj);\n");
1453     fprintf(fp,"  unsigned long     temp20;\n");
1454     fprintf(fp,"  temp20 = op->AddObserver(temp0,cbc);\n");
1455     fprintf(fp,"  delete[] temp0;\n");
1456     fprintf(fp,"  delete[] temp2;\n");
1457     fprintf(fp,"  cbc->Delete();\n");
1458     fprintf(fp,"  return temp20;\n}\n");
1459    }
1460 
1461   vtkParse_Free(file_info);
1462 
1463   fclose(fp);
1464 
1465   return 0;
1466 }
1467